OSDN Git Service

(thumb_find_work_register): Check all of the argument registers to see if they
[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 int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
75 static rtx emit_sfm (int, int);
76 #ifndef AOF_ASSEMBLER
77 static bool arm_assemble_integer (rtx, unsigned int, int);
78 #endif
79 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
80 static arm_cc get_arm_condition_code (rtx);
81 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
82 static rtx is_jump_table (rtx);
83 static const char *output_multi_immediate (rtx *, const char *, const char *,
84                                            int, HOST_WIDE_INT);
85 static const char *shift_op (rtx, HOST_WIDE_INT *);
86 static struct machine_function *arm_init_machine_status (void);
87 static void replace_symbols_in_block (tree, rtx, rtx);
88 static void thumb_exit (FILE *, int);
89 static rtx is_jump_table (rtx);
90 static HOST_WIDE_INT get_jump_table_size (rtx);
91 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
92 static Mnode *add_minipool_forward_ref (Mfix *);
93 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
94 static Mnode *add_minipool_backward_ref (Mfix *);
95 static void assign_minipool_offsets (Mfix *);
96 static void arm_print_value (FILE *, rtx);
97 static void dump_minipool (rtx);
98 static int arm_barrier_cost (rtx);
99 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
100 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
101 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
102                                rtx);
103 static void arm_reorg (void);
104 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
105 static int current_file_function_operand (rtx);
106 static unsigned long arm_compute_save_reg0_reg12_mask (void);
107 static unsigned long arm_compute_save_reg_mask (void);
108 static unsigned long arm_isr_value (tree);
109 static unsigned long arm_compute_func_type (void);
110 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
111 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
112 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
113 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
114 #endif
115 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
116 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
117 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
118 static int arm_comp_type_attributes (tree, tree);
119 static void arm_set_default_type_attributes (tree);
120 static int arm_adjust_cost (rtx, rtx, rtx, int);
121 static int count_insns_for_constant (HOST_WIDE_INT, int);
122 static int arm_get_strip_length (int);
123 static bool arm_function_ok_for_sibcall (tree, tree);
124 static void arm_internal_label (FILE *, const char *, unsigned long);
125 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
126                                  tree);
127 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
128 static bool arm_size_rtx_costs (rtx, int, int, int *);
129 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
130 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
131 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
132 static bool arm_9e_rtx_costs (rtx, int, int, int *);
133 static int arm_address_cost (rtx);
134 static bool arm_memory_load_p (rtx);
135 static bool arm_cirrus_insn_p (rtx);
136 static void cirrus_reorg (rtx);
137 static void arm_init_builtins (void);
138 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
139 static void arm_init_iwmmxt_builtins (void);
140 static rtx safe_vector_operand (rtx, enum machine_mode);
141 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
142 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
143 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
144 static void emit_constant_insn (rtx cond, rtx pattern);
145 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
146                                   tree, bool);
147
148 #ifndef ARM_PE
149 static void arm_encode_section_info (tree, rtx, int);
150 #endif
151
152 static void arm_file_end (void);
153
154 #ifdef AOF_ASSEMBLER
155 static void aof_globalize_label (FILE *, const char *);
156 static void aof_dump_imports (FILE *);
157 static void aof_dump_pic_table (FILE *);
158 static void aof_file_start (void);
159 static void aof_file_end (void);
160 #endif
161 static rtx arm_struct_value_rtx (tree, int);
162 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
163                                         tree, int *, int);
164 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
165                                    enum machine_mode, tree, bool);
166 static bool arm_promote_prototypes (tree);
167 static bool arm_default_short_enums (void);
168 static bool arm_align_anon_bitfield (void);
169
170 static tree arm_cxx_guard_type (void);
171 static bool arm_cxx_guard_mask_bit (void);
172 static tree arm_get_cookie_size (tree);
173 static bool arm_cookie_has_size (void);
174 static bool arm_cxx_cdtor_returns_this (void);
175 static bool arm_cxx_key_method_may_be_inline (void);
176 static bool arm_cxx_export_class_data (void);
177 static void arm_init_libfuncs (void);
178 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
179 \f
180 /* Initialize the GCC target structure.  */
181 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
182 #undef  TARGET_MERGE_DECL_ATTRIBUTES
183 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
184 #endif
185
186 #undef  TARGET_ATTRIBUTE_TABLE
187 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
188
189 #undef TARGET_ASM_FILE_END
190 #define TARGET_ASM_FILE_END arm_file_end
191
192 #ifdef AOF_ASSEMBLER
193 #undef  TARGET_ASM_BYTE_OP
194 #define TARGET_ASM_BYTE_OP "\tDCB\t"
195 #undef  TARGET_ASM_ALIGNED_HI_OP
196 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
197 #undef  TARGET_ASM_ALIGNED_SI_OP
198 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
199 #undef TARGET_ASM_GLOBALIZE_LABEL
200 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
201 #undef TARGET_ASM_FILE_START
202 #define TARGET_ASM_FILE_START aof_file_start
203 #undef TARGET_ASM_FILE_END
204 #define TARGET_ASM_FILE_END aof_file_end
205 #else
206 #undef  TARGET_ASM_ALIGNED_SI_OP
207 #define TARGET_ASM_ALIGNED_SI_OP NULL
208 #undef  TARGET_ASM_INTEGER
209 #define TARGET_ASM_INTEGER arm_assemble_integer
210 #endif
211
212 #undef  TARGET_ASM_FUNCTION_PROLOGUE
213 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
214
215 #undef  TARGET_ASM_FUNCTION_EPILOGUE
216 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
217
218 #undef  TARGET_COMP_TYPE_ATTRIBUTES
219 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
220
221 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
222 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
223
224 #undef  TARGET_SCHED_ADJUST_COST
225 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
226
227 #undef TARGET_ENCODE_SECTION_INFO
228 #ifdef ARM_PE
229 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
230 #else
231 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
232 #endif
233
234 #undef  TARGET_STRIP_NAME_ENCODING
235 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
236
237 #undef  TARGET_ASM_INTERNAL_LABEL
238 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
239
240 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
241 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
242
243 #undef  TARGET_ASM_OUTPUT_MI_THUNK
244 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
245 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
246 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
247
248 /* This will be overridden in arm_override_options.  */
249 #undef  TARGET_RTX_COSTS
250 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
251 #undef  TARGET_ADDRESS_COST
252 #define TARGET_ADDRESS_COST arm_address_cost
253
254 #undef TARGET_SHIFT_TRUNCATION_MASK
255 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
256 #undef TARGET_VECTOR_MODE_SUPPORTED_P
257 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
258
259 #undef  TARGET_MACHINE_DEPENDENT_REORG
260 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
261
262 #undef  TARGET_INIT_BUILTINS
263 #define TARGET_INIT_BUILTINS  arm_init_builtins
264 #undef  TARGET_EXPAND_BUILTIN
265 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
266
267 #undef TARGET_INIT_LIBFUNCS
268 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
269
270 #undef TARGET_PROMOTE_FUNCTION_ARGS
271 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
272 #undef TARGET_PROMOTE_FUNCTION_RETURN
273 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
274 #undef TARGET_PROMOTE_PROTOTYPES
275 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
276 #undef TARGET_PASS_BY_REFERENCE
277 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
278 #undef TARGET_ARG_PARTIAL_BYTES
279 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
280
281 #undef TARGET_STRUCT_VALUE_RTX
282 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
283
284 #undef  TARGET_SETUP_INCOMING_VARARGS
285 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
286
287 #undef TARGET_DEFAULT_SHORT_ENUMS
288 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
289
290 #undef TARGET_ALIGN_ANON_BITFIELD
291 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
292
293 #undef TARGET_CXX_GUARD_TYPE
294 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
295
296 #undef TARGET_CXX_GUARD_MASK_BIT
297 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
298
299 #undef TARGET_CXX_GET_COOKIE_SIZE
300 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
301
302 #undef TARGET_CXX_COOKIE_HAS_SIZE
303 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
304
305 #undef TARGET_CXX_CDTOR_RETURNS_THIS
306 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
307
308 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
309 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
310
311 #undef TARGET_CXX_EXPORT_CLASS_DATA
312 #define TARGET_CXX_EXPORT_CLASS_DATA arm_cxx_export_class_data
313
314 struct gcc_target targetm = TARGET_INITIALIZER;
315 \f
316 /* Obstack for minipool constant handling.  */
317 static struct obstack minipool_obstack;
318 static char *         minipool_startobj;
319
320 /* The maximum number of insns skipped which
321    will be conditionalised if possible.  */
322 static int max_insns_skipped = 5;
323
324 extern FILE * asm_out_file;
325
326 /* True if we are currently building a constant table.  */
327 int making_const_table;
328
329 /* Define the information needed to generate branch insns.  This is
330    stored from the compare operation.  */
331 rtx arm_compare_op0, arm_compare_op1;
332
333 /* The processor for which instructions should be scheduled.  */
334 enum processor_type arm_tune = arm_none;
335
336 /* Which floating point model to use.  */
337 enum arm_fp_model arm_fp_model;
338
339 /* Which floating point hardware is available.  */
340 enum fputype arm_fpu_arch;
341
342 /* Which floating point hardware to schedule for.  */
343 enum fputype arm_fpu_tune;
344
345 /* Whether to use floating point hardware.  */
346 enum float_abi_type arm_float_abi;
347
348 /* Which ABI to use.  */
349 enum arm_abi_type arm_abi;
350
351 /* Set by the -mfpu=... option.  */
352 const char * target_fpu_name = NULL;
353
354 /* Set by the -mfpe=... option.  */
355 const char * target_fpe_name = NULL;
356
357 /* Set by the -mfloat-abi=... option.  */
358 const char * target_float_abi_name = NULL;
359
360 /* Set by the legacy -mhard-float and -msoft-float options.  */
361 const char * target_float_switch = NULL;
362
363 /* Set by the -mabi=... option.  */
364 const char * target_abi_name = NULL;
365
366 /* Used to parse -mstructure_size_boundary command line option.  */
367 const char * structure_size_string = NULL;
368 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
369
370 /* Used for Thumb call_via trampolines.  */
371 rtx thumb_call_via_label[13];
372 static int thumb_call_reg_needed;
373
374 /* Bit values used to identify processor capabilities.  */
375 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
376 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
377 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
378 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
379 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
380 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
381 #define FL_THUMB      (1 << 6)        /* Thumb aware */
382 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
383 #define FL_STRONG     (1 << 8)        /* StrongARM */
384 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
385 #define FL_XSCALE     (1 << 10)       /* XScale */
386 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
387 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
388                                          media instructions.  */
389 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
390
391 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
392
393 #define FL_FOR_ARCH2    0
394 #define FL_FOR_ARCH3    FL_MODE32
395 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
396 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
397 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
398 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
399 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
400 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
401 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
402 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
403 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
404 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
405 #define FL_FOR_ARCH6K   FL_FOR_ARCH6
406 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
407 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6
408
409 /* The bits in this mask specify which
410    instructions we are allowed to generate.  */
411 static unsigned long insn_flags = 0;
412
413 /* The bits in this mask specify which instruction scheduling options should
414    be used.  */
415 static unsigned long tune_flags = 0;
416
417 /* The following are used in the arm.md file as equivalents to bits
418    in the above two flag variables.  */
419
420 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
421 int arm_arch3m = 0;
422
423 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
424 int arm_arch4 = 0;
425
426 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
427 int arm_arch4t = 0;
428
429 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
430 int arm_arch5 = 0;
431
432 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
433 int arm_arch5e = 0;
434
435 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
436 int arm_arch6 = 0;
437
438 /* Nonzero if this chip can benefit from load scheduling.  */
439 int arm_ld_sched = 0;
440
441 /* Nonzero if this chip is a StrongARM.  */
442 int arm_is_strong = 0;
443
444 /* Nonzero if this chip is a Cirrus variant.  */
445 int arm_arch_cirrus = 0;
446
447 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
448 int arm_arch_iwmmxt = 0;
449
450 /* Nonzero if this chip is an XScale.  */
451 int arm_arch_xscale = 0;
452
453 /* Nonzero if tuning for XScale  */
454 int arm_tune_xscale = 0;
455
456 /* Nonzero if this chip is an ARM6 or an ARM7.  */
457 int arm_is_6_or_7 = 0;
458
459 /* Nonzero if generating Thumb instructions.  */
460 int thumb_code = 0;
461
462 /* Nonzero if we should define __THUMB_INTERWORK__ in the
463    preprocessor.
464    XXX This is a bit of a hack, it's intended to help work around
465    problems in GLD which doesn't understand that armv5t code is
466    interworking clean.  */
467 int arm_cpp_interwork = 0;
468
469 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
470    must report the mode of the memory reference from PRINT_OPERAND to
471    PRINT_OPERAND_ADDRESS.  */
472 enum machine_mode output_memory_reference_mode;
473
474 /* The register number to be used for the PIC offset register.  */
475 const char * arm_pic_register_string = NULL;
476 int arm_pic_register = INVALID_REGNUM;
477
478 /* Set to 1 when a return insn is output, this means that the epilogue
479    is not needed.  */
480 int return_used_this_function;
481
482 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
483    the next function.  */
484 static int after_arm_reorg = 0;
485
486 /* The maximum number of insns to be used when loading a constant.  */
487 static int arm_constant_limit = 3;
488
489 /* For an explanation of these variables, see final_prescan_insn below.  */
490 int arm_ccfsm_state;
491 enum arm_cond_code arm_current_cc;
492 rtx arm_target_insn;
493 int arm_target_label;
494
495 /* The condition codes of the ARM, and the inverse function.  */
496 static const char * const arm_condition_codes[] =
497 {
498   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
499   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
500 };
501
502 #define streq(string1, string2) (strcmp (string1, string2) == 0)
503 \f
504 /* Initialization code.  */
505
506 struct processors
507 {
508   const char *const name;
509   enum processor_type core;
510   const char *arch;
511   const unsigned long flags;
512   bool (* rtx_costs) (rtx, int, int, int *);
513 };
514
515 /* Not all of these give usefully different compilation alternatives,
516    but there is no simple way of generalizing them.  */
517 static const struct processors all_cores[] =
518 {
519   /* ARM Cores */
520 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
521   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
522 #include "arm-cores.def"
523 #undef ARM_CORE
524   {NULL, arm_none, NULL, 0, NULL}
525 };
526
527 static const struct processors all_architectures[] =
528 {
529   /* ARM Architectures */
530   /* We don't specify rtx_costs here as it will be figured out
531      from the core.  */
532
533   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
534   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
535   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
536   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
537   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
538   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
539      implementations that support it, so we will leave it out for now.  */
540   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
541   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
542   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
543   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
544   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
545   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
546   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
547   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
548   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
549   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
550   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
551   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
552   {NULL, arm_none, NULL, 0 , NULL}
553 };
554
555 /* This is a magic structure.  The 'string' field is magically filled in
556    with a pointer to the value specified by the user on the command line
557    assuming that the user has specified such a value.  */
558
559 struct arm_cpu_select arm_select[] =
560 {
561   /* string       name            processors  */
562   { NULL,       "-mcpu=",       all_cores  },
563   { NULL,       "-march=",      all_architectures },
564   { NULL,       "-mtune=",      all_cores }
565 };
566
567
568 /* The name of the proprocessor macro to define for this architecture.  */
569
570 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
571
572 struct fpu_desc
573 {
574   const char * name;
575   enum fputype fpu;
576 };
577
578
579 /* Available values for for -mfpu=.  */
580
581 static const struct fpu_desc all_fpus[] =
582 {
583   {"fpa",       FPUTYPE_FPA},
584   {"fpe2",      FPUTYPE_FPA_EMU2},
585   {"fpe3",      FPUTYPE_FPA_EMU2},
586   {"maverick",  FPUTYPE_MAVERICK},
587   {"vfp",       FPUTYPE_VFP}
588 };
589
590
591 /* Floating point models used by the different hardware.
592    See fputype in arm.h.  */
593
594 static const enum fputype fp_model_for_fpu[] =
595 {
596   /* No FP hardware.  */
597   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
598   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
599   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
600   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
601   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
602   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
603 };
604
605
606 struct float_abi
607 {
608   const char * name;
609   enum float_abi_type abi_type;
610 };
611
612
613 /* Available values for -mfloat-abi=.  */
614
615 static const struct float_abi all_float_abis[] =
616 {
617   {"soft",      ARM_FLOAT_ABI_SOFT},
618   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
619   {"hard",      ARM_FLOAT_ABI_HARD}
620 };
621
622
623 struct abi_name
624 {
625   const char *name;
626   enum arm_abi_type abi_type;
627 };
628
629
630 /* Available values for -mabi=.  */
631
632 static const struct abi_name arm_all_abis[] =
633 {
634   {"apcs-gnu",    ARM_ABI_APCS},
635   {"atpcs",   ARM_ABI_ATPCS},
636   {"aapcs",   ARM_ABI_AAPCS},
637   {"iwmmxt",  ARM_ABI_IWMMXT}
638 };
639
640 /* Return the number of bits set in VALUE.  */
641 static unsigned
642 bit_count (unsigned long value)
643 {
644   unsigned long count = 0;
645
646   while (value)
647     {
648       count++;
649       value &= value - 1;  /* Clear the least-significant set bit.  */
650     }
651
652   return count;
653 }
654
655 /* Set up library functions unique to ARM.  */
656
657 static void
658 arm_init_libfuncs (void)
659 {
660   /* There are no special library functions unless we are using the
661      ARM BPABI.  */
662   if (!TARGET_BPABI)
663     return;
664
665   /* The functions below are described in Section 4 of the "Run-Time
666      ABI for the ARM architecture", Version 1.0.  */
667
668   /* Double-precision floating-point arithmetic.  Table 2.  */
669   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
670   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
671   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
672   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
673   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
674
675   /* Double-precision comparisons.  Table 3.  */
676   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
677   set_optab_libfunc (ne_optab, DFmode, NULL);
678   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
679   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
680   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
681   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
682   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
683
684   /* Single-precision floating-point arithmetic.  Table 4.  */
685   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
686   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
687   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
688   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
689   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
690
691   /* Single-precision comparisons.  Table 5.  */
692   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
693   set_optab_libfunc (ne_optab, SFmode, NULL);
694   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
695   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
696   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
697   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
698   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
699
700   /* Floating-point to integer conversions.  Table 6.  */
701   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
702   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
703   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
704   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
705   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
706   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
707   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
708   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
709
710   /* Conversions between floating types.  Table 7.  */
711   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
712   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
713
714   /* Integer to floating-point conversions.  Table 8.  */
715   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
716   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
717   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
718   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
719   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
720   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
721   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
722   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
723
724   /* Long long.  Table 9.  */
725   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
726   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
727   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
728   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
729   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
730   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
731   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
732   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
733
734   /* Integer (32/32->32) division.  \S 4.3.1.  */
735   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
736   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
737
738   /* The divmod functions are designed so that they can be used for
739      plain division, even though they return both the quotient and the
740      remainder.  The quotient is returned in the usual location (i.e.,
741      r0 for SImode, {r0, r1} for DImode), just as would be expected
742      for an ordinary division routine.  Because the AAPCS calling
743      conventions specify that all of { r0, r1, r2, r3 } are
744      callee-saved registers, there is no need to tell the compiler
745      explicitly that those registers are clobbered by these
746      routines.  */
747   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
748   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
749   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idivmod");
750   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidivmod");
751 }
752
753 /* Fix up any incompatible options that the user has specified.
754    This has now turned into a maze.  */
755 void
756 arm_override_options (void)
757 {
758   unsigned i;
759
760   /* Set up the flags based on the cpu/architecture selected by the user.  */
761   for (i = ARRAY_SIZE (arm_select); i--;)
762     {
763       struct arm_cpu_select * ptr = arm_select + i;
764
765       if (ptr->string != NULL && ptr->string[0] != '\0')
766         {
767           const struct processors * sel;
768
769           for (sel = ptr->processors; sel->name != NULL; sel++)
770             if (streq (ptr->string, sel->name))
771               {
772                 /* Set the architecture define.  */
773                 if (i != 2)
774                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
775
776                 /* Determine the processor core for which we should
777                    tune code-generation.  */
778                 if (/* -mcpu= is a sensible default.  */
779                     i == 0
780                     /* If -march= is used, and -mcpu= has not been used,
781                        assume that we should tune for a representative
782                        CPU from that architecture.  */
783                     || i == 1
784                     /* -mtune= overrides -mcpu= and -march=.  */
785                     || i == 2)
786                   arm_tune = (enum processor_type) (sel - ptr->processors);
787
788                 if (i != 2)
789                   {
790                     /* If we have been given an architecture and a processor
791                        make sure that they are compatible.  We only generate
792                        a warning though, and we prefer the CPU over the
793                        architecture.  */
794                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
795                       warning ("switch -mcpu=%s conflicts with -march= switch",
796                                ptr->string);
797
798                     insn_flags = sel->flags;
799                   }
800
801                 break;
802               }
803
804           if (sel->name == NULL)
805             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
806         }
807     }
808
809   /* If the user did not specify a processor, choose one for them.  */
810   if (insn_flags == 0)
811     {
812       const struct processors * sel;
813       unsigned int        sought;
814       enum processor_type cpu;
815
816       cpu = TARGET_CPU_DEFAULT;
817       if (cpu == arm_none)
818         {
819 #ifdef SUBTARGET_CPU_DEFAULT
820           /* Use the subtarget default CPU if none was specified by
821              configure.  */
822           cpu = SUBTARGET_CPU_DEFAULT;
823 #endif
824           /* Default to ARM6.  */
825           if (cpu == arm_none)
826             cpu = arm6;
827         }
828       sel = &all_cores[cpu];
829
830       insn_flags = sel->flags;
831
832       /* Now check to see if the user has specified some command line
833          switch that require certain abilities from the cpu.  */
834       sought = 0;
835
836       if (TARGET_INTERWORK || TARGET_THUMB)
837         {
838           sought |= (FL_THUMB | FL_MODE32);
839
840           /* There are no ARM processors that support both APCS-26 and
841              interworking.  Therefore we force FL_MODE26 to be removed
842              from insn_flags here (if it was set), so that the search
843              below will always be able to find a compatible processor.  */
844           insn_flags &= ~FL_MODE26;
845         }
846
847       if (sought != 0 && ((sought & insn_flags) != sought))
848         {
849           /* Try to locate a CPU type that supports all of the abilities
850              of the default CPU, plus the extra abilities requested by
851              the user.  */
852           for (sel = all_cores; sel->name != NULL; sel++)
853             if ((sel->flags & sought) == (sought | insn_flags))
854               break;
855
856           if (sel->name == NULL)
857             {
858               unsigned current_bit_count = 0;
859               const struct processors * best_fit = NULL;
860
861               /* Ideally we would like to issue an error message here
862                  saying that it was not possible to find a CPU compatible
863                  with the default CPU, but which also supports the command
864                  line options specified by the programmer, and so they
865                  ought to use the -mcpu=<name> command line option to
866                  override the default CPU type.
867
868                  If we cannot find a cpu that has both the
869                  characteristics of the default cpu and the given
870                  command line options we scan the array again looking
871                  for a best match.  */
872               for (sel = all_cores; sel->name != NULL; sel++)
873                 if ((sel->flags & sought) == sought)
874                   {
875                     unsigned count;
876
877                     count = bit_count (sel->flags & insn_flags);
878
879                     if (count >= current_bit_count)
880                       {
881                         best_fit = sel;
882                         current_bit_count = count;
883                       }
884                   }
885
886               if (best_fit == NULL)
887                 abort ();
888               else
889                 sel = best_fit;
890             }
891
892           insn_flags = sel->flags;
893         }
894       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
895       if (arm_tune == arm_none)
896         arm_tune = (enum processor_type) (sel - all_cores);
897     }
898
899   /* The processor for which we should tune should now have been
900      chosen.  */
901   if (arm_tune == arm_none)
902     abort ();
903
904   tune_flags = all_cores[(int)arm_tune].flags;
905   if (optimize_size)
906     targetm.rtx_costs = arm_size_rtx_costs;
907   else
908     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
909
910   /* Make sure that the processor choice does not conflict with any of the
911      other command line choices.  */
912   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
913     {
914       warning ("target CPU does not support interworking" );
915       target_flags &= ~ARM_FLAG_INTERWORK;
916     }
917
918   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
919     {
920       warning ("target CPU does not support THUMB instructions");
921       target_flags &= ~ARM_FLAG_THUMB;
922     }
923
924   if (TARGET_APCS_FRAME && TARGET_THUMB)
925     {
926       /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
927       target_flags &= ~ARM_FLAG_APCS_FRAME;
928     }
929
930   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
931      from here where no function is being compiled currently.  */
932   if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
933       && TARGET_ARM)
934     warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
935
936   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
937     warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
938
939   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
940     warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
941
942   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
943     {
944       warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
945       target_flags |= ARM_FLAG_APCS_FRAME;
946     }
947
948   if (TARGET_POKE_FUNCTION_NAME)
949     target_flags |= ARM_FLAG_APCS_FRAME;
950
951   if (TARGET_APCS_REENT && flag_pic)
952     error ("-fpic and -mapcs-reent are incompatible");
953
954   if (TARGET_APCS_REENT)
955     warning ("APCS reentrant code not supported.  Ignored");
956
957   /* If this target is normally configured to use APCS frames, warn if they
958      are turned off and debugging is turned on.  */
959   if (TARGET_ARM
960       && write_symbols != NO_DEBUG
961       && !TARGET_APCS_FRAME
962       && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
963     warning ("-g with -mno-apcs-frame may not give sensible debugging");
964
965   /* If stack checking is disabled, we can use r10 as the PIC register,
966      which keeps r9 available.  */
967   if (flag_pic)
968     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
969
970   if (TARGET_APCS_FLOAT)
971     warning ("passing floating point arguments in fp regs not yet supported");
972
973   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
974   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
975   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
976   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
977   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
978   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
979   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
980   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
981   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
982
983   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
984   arm_is_strong = (tune_flags & FL_STRONG) != 0;
985   thumb_code = (TARGET_ARM == 0);
986   arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
987                     && !(tune_flags & FL_ARCH4))) != 0;
988   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
989   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
990
991   /* V5 code we generate is completely interworking capable, so we turn off
992      TARGET_INTERWORK here to avoid many tests later on.  */
993
994   /* XXX However, we must pass the right pre-processor defines to CPP
995      or GLD can get confused.  This is a hack.  */
996   if (TARGET_INTERWORK)
997     arm_cpp_interwork = 1;
998
999   if (arm_arch5)
1000     target_flags &= ~ARM_FLAG_INTERWORK;
1001
1002   if (target_abi_name)
1003     {
1004       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1005         {
1006           if (streq (arm_all_abis[i].name, target_abi_name))
1007             {
1008               arm_abi = arm_all_abis[i].abi_type;
1009               break;
1010             }
1011         }
1012       if (i == ARRAY_SIZE (arm_all_abis))
1013         error ("invalid ABI option: -mabi=%s", target_abi_name);
1014     }
1015   else
1016     arm_abi = ARM_DEFAULT_ABI;
1017
1018   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1019     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1020
1021   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1022     error ("iwmmxt abi requires an iwmmxt capable cpu");
1023
1024   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1025   if (target_fpu_name == NULL && target_fpe_name != NULL)
1026     {
1027       if (streq (target_fpe_name, "2"))
1028         target_fpu_name = "fpe2";
1029       else if (streq (target_fpe_name, "3"))
1030         target_fpu_name = "fpe3";
1031       else
1032         error ("invalid floating point emulation option: -mfpe=%s",
1033                target_fpe_name);
1034     }
1035   if (target_fpu_name != NULL)
1036     {
1037       /* The user specified a FPU.  */
1038       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1039         {
1040           if (streq (all_fpus[i].name, target_fpu_name))
1041             {
1042               arm_fpu_arch = all_fpus[i].fpu;
1043               arm_fpu_tune = arm_fpu_arch;
1044               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1045               break;
1046             }
1047         }
1048       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1049         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1050     }
1051   else
1052     {
1053 #ifdef FPUTYPE_DEFAULT
1054       /* Use the default if it is specified for this platform.  */
1055       arm_fpu_arch = FPUTYPE_DEFAULT;
1056       arm_fpu_tune = FPUTYPE_DEFAULT;
1057 #else
1058       /* Pick one based on CPU type.  */
1059       /* ??? Some targets assume FPA is the default.
1060       if ((insn_flags & FL_VFP) != 0)
1061         arm_fpu_arch = FPUTYPE_VFP;
1062       else
1063       */
1064       if (arm_arch_cirrus)
1065         arm_fpu_arch = FPUTYPE_MAVERICK;
1066       else
1067         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1068 #endif
1069       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1070         arm_fpu_tune = FPUTYPE_FPA;
1071       else
1072         arm_fpu_tune = arm_fpu_arch;
1073       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1074       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1075         abort ();
1076     }
1077
1078   if (target_float_abi_name != NULL)
1079     {
1080       /* The user specified a FP ABI.  */
1081       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1082         {
1083           if (streq (all_float_abis[i].name, target_float_abi_name))
1084             {
1085               arm_float_abi = all_float_abis[i].abi_type;
1086               break;
1087             }
1088         }
1089       if (i == ARRAY_SIZE (all_float_abis))
1090         error ("invalid floating point abi: -mfloat-abi=%s",
1091                target_float_abi_name);
1092     }
1093   else if (target_float_switch)
1094     {
1095       /* This is a bit of a hack to avoid needing target flags for these.  */
1096       if (target_float_switch[0] == 'h')
1097         arm_float_abi = ARM_FLOAT_ABI_HARD;
1098       else
1099         arm_float_abi = ARM_FLOAT_ABI_SOFT;
1100     }
1101   else
1102     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1103
1104   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1105     sorry ("-mfloat-abi=hard and VFP");
1106
1107   /* If soft-float is specified then don't use FPU.  */
1108   if (TARGET_SOFT_FLOAT)
1109     arm_fpu_arch = FPUTYPE_NONE;
1110
1111   /* For arm2/3 there is no need to do any scheduling if there is only
1112      a floating point emulator, or we are doing software floating-point.  */
1113   if ((TARGET_SOFT_FLOAT
1114        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1115        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1116       && (tune_flags & FL_MODE32) == 0)
1117     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1118
1119   /* Override the default structure alignment for AAPCS ABI.  */
1120   if (arm_abi == ARM_ABI_AAPCS)
1121     arm_structure_size_boundary = 8;
1122
1123   if (structure_size_string != NULL)
1124     {
1125       int size = strtol (structure_size_string, NULL, 0);
1126
1127       if (size == 8 || size == 32
1128           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1129         arm_structure_size_boundary = size;
1130       else
1131         warning ("structure size boundary can only be set to %s",
1132                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1133     }
1134
1135   if (arm_pic_register_string != NULL)
1136     {
1137       int pic_register = decode_reg_name (arm_pic_register_string);
1138
1139       if (!flag_pic)
1140         warning ("-mpic-register= is useless without -fpic");
1141
1142       /* Prevent the user from choosing an obviously stupid PIC register.  */
1143       else if (pic_register < 0 || call_used_regs[pic_register]
1144                || pic_register == HARD_FRAME_POINTER_REGNUM
1145                || pic_register == STACK_POINTER_REGNUM
1146                || pic_register >= PC_REGNUM)
1147         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1148       else
1149         arm_pic_register = pic_register;
1150     }
1151
1152   if (TARGET_THUMB && flag_schedule_insns)
1153     {
1154       /* Don't warn since it's on by default in -O2.  */
1155       flag_schedule_insns = 0;
1156     }
1157
1158   if (optimize_size)
1159     {
1160       /* There's some dispute as to whether this should be 1 or 2.  However,
1161          experiments seem to show that in pathological cases a setting of
1162          1 degrades less severely than a setting of 2.  This could change if
1163          other parts of the compiler change their behavior.  */
1164       arm_constant_limit = 1;
1165
1166       /* If optimizing for size, bump the number of instructions that we
1167          are prepared to conditionally execute (even on a StrongARM).  */
1168       max_insns_skipped = 6;
1169     }
1170   else
1171     {
1172       /* For processors with load scheduling, it never costs more than
1173          2 cycles to load a constant, and the load scheduler may well
1174          reduce that to 1.  */
1175       if (arm_ld_sched)
1176         arm_constant_limit = 1;
1177
1178       /* On XScale the longer latency of a load makes it more difficult
1179          to achieve a good schedule, so it's faster to synthesize
1180          constants that can be done in two insns.  */
1181       if (arm_tune_xscale)
1182         arm_constant_limit = 2;
1183
1184       /* StrongARM has early execution of branches, so a sequence
1185          that is worth skipping is shorter.  */
1186       if (arm_is_strong)
1187         max_insns_skipped = 3;
1188     }
1189
1190   /* Register global variables with the garbage collector.  */
1191   arm_add_gc_roots ();
1192 }
1193
1194 static void
1195 arm_add_gc_roots (void)
1196 {
1197   gcc_obstack_init(&minipool_obstack);
1198   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1199 }
1200 \f
1201 /* A table of known ARM exception types.
1202    For use with the interrupt function attribute.  */
1203
1204 typedef struct
1205 {
1206   const char *const arg;
1207   const unsigned long return_value;
1208 }
1209 isr_attribute_arg;
1210
1211 static const isr_attribute_arg isr_attribute_args [] =
1212 {
1213   { "IRQ",   ARM_FT_ISR },
1214   { "irq",   ARM_FT_ISR },
1215   { "FIQ",   ARM_FT_FIQ },
1216   { "fiq",   ARM_FT_FIQ },
1217   { "ABORT", ARM_FT_ISR },
1218   { "abort", ARM_FT_ISR },
1219   { "ABORT", ARM_FT_ISR },
1220   { "abort", ARM_FT_ISR },
1221   { "UNDEF", ARM_FT_EXCEPTION },
1222   { "undef", ARM_FT_EXCEPTION },
1223   { "SWI",   ARM_FT_EXCEPTION },
1224   { "swi",   ARM_FT_EXCEPTION },
1225   { NULL,    ARM_FT_NORMAL }
1226 };
1227
1228 /* Returns the (interrupt) function type of the current
1229    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1230
1231 static unsigned long
1232 arm_isr_value (tree argument)
1233 {
1234   const isr_attribute_arg * ptr;
1235   const char *              arg;
1236
1237   /* No argument - default to IRQ.  */
1238   if (argument == NULL_TREE)
1239     return ARM_FT_ISR;
1240
1241   /* Get the value of the argument.  */
1242   if (TREE_VALUE (argument) == NULL_TREE
1243       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1244     return ARM_FT_UNKNOWN;
1245
1246   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1247
1248   /* Check it against the list of known arguments.  */
1249   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1250     if (streq (arg, ptr->arg))
1251       return ptr->return_value;
1252
1253   /* An unrecognized interrupt type.  */
1254   return ARM_FT_UNKNOWN;
1255 }
1256
1257 /* Computes the type of the current function.  */
1258
1259 static unsigned long
1260 arm_compute_func_type (void)
1261 {
1262   unsigned long type = ARM_FT_UNKNOWN;
1263   tree a;
1264   tree attr;
1265
1266   if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
1267     abort ();
1268
1269   /* Decide if the current function is volatile.  Such functions
1270      never return, and many memory cycles can be saved by not storing
1271      register values that will never be needed again.  This optimization
1272      was added to speed up context switching in a kernel application.  */
1273   if (optimize > 0
1274       && TREE_NOTHROW (current_function_decl)
1275       && TREE_THIS_VOLATILE (current_function_decl))
1276     type |= ARM_FT_VOLATILE;
1277
1278   if (cfun->static_chain_decl != NULL)
1279     type |= ARM_FT_NESTED;
1280
1281   attr = DECL_ATTRIBUTES (current_function_decl);
1282
1283   a = lookup_attribute ("naked", attr);
1284   if (a != NULL_TREE)
1285     type |= ARM_FT_NAKED;
1286
1287   a = lookup_attribute ("isr", attr);
1288   if (a == NULL_TREE)
1289     a = lookup_attribute ("interrupt", attr);
1290
1291   if (a == NULL_TREE)
1292     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1293   else
1294     type |= arm_isr_value (TREE_VALUE (a));
1295
1296   return type;
1297 }
1298
1299 /* Returns the type of the current function.  */
1300
1301 unsigned long
1302 arm_current_func_type (void)
1303 {
1304   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1305     cfun->machine->func_type = arm_compute_func_type ();
1306
1307   return cfun->machine->func_type;
1308 }
1309 \f
1310 /* Return 1 if it is possible to return using a single instruction.
1311    If SIBLING is non-null, this is a test for a return before a sibling
1312    call.  SIBLING is the call insn, so we can examine its register usage.  */
1313
1314 int
1315 use_return_insn (int iscond, rtx sibling)
1316 {
1317   int regno;
1318   unsigned int func_type;
1319   unsigned long saved_int_regs;
1320   unsigned HOST_WIDE_INT stack_adjust;
1321   arm_stack_offsets *offsets;
1322
1323   /* Never use a return instruction before reload has run.  */
1324   if (!reload_completed)
1325     return 0;
1326
1327   func_type = arm_current_func_type ();
1328
1329   /* Naked functions and volatile functions need special
1330      consideration.  */
1331   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1332     return 0;
1333
1334   /* So do interrupt functions that use the frame pointer.  */
1335   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1336     return 0;
1337
1338   offsets = arm_get_frame_offsets ();
1339   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1340
1341   /* As do variadic functions.  */
1342   if (current_function_pretend_args_size
1343       || cfun->machine->uses_anonymous_args
1344       /* Or if the function calls __builtin_eh_return () */
1345       || current_function_calls_eh_return
1346       /* Or if the function calls alloca */
1347       || current_function_calls_alloca
1348       /* Or if there is a stack adjustment.  However, if the stack pointer
1349          is saved on the stack, we can use a pre-incrementing stack load.  */
1350       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1351     return 0;
1352
1353   saved_int_regs = arm_compute_save_reg_mask ();
1354
1355   /* Unfortunately, the insn
1356
1357        ldmib sp, {..., sp, ...}
1358
1359      triggers a bug on most SA-110 based devices, such that the stack
1360      pointer won't be correctly restored if the instruction takes a
1361      page fault.  We work around this problem by popping r3 along with
1362      the other registers, since that is never slower than executing
1363      another instruction.
1364
1365      We test for !arm_arch5 here, because code for any architecture
1366      less than this could potentially be run on one of the buggy
1367      chips.  */
1368   if (stack_adjust == 4 && !arm_arch5)
1369     {
1370       /* Validate that r3 is a call-clobbered register (always true in
1371          the default abi) ...  */
1372       if (!call_used_regs[3])
1373         return 0;
1374
1375       /* ... that it isn't being used for a return value (always true
1376          until we implement return-in-regs), or for a tail-call
1377          argument ...  */
1378       if (sibling)
1379         {
1380           if (GET_CODE (sibling) != CALL_INSN)
1381             abort ();
1382
1383           if (find_regno_fusage (sibling, USE, 3))
1384             return 0;
1385         }
1386
1387       /* ... and that there are no call-saved registers in r0-r2
1388          (always true in the default ABI).  */
1389       if (saved_int_regs & 0x7)
1390         return 0;
1391     }
1392
1393   /* Can't be done if interworking with Thumb, and any registers have been
1394      stacked.  */
1395   if (TARGET_INTERWORK && saved_int_regs != 0)
1396     return 0;
1397
1398   /* On StrongARM, conditional returns are expensive if they aren't
1399      taken and multiple registers have been stacked.  */
1400   if (iscond && arm_is_strong)
1401     {
1402       /* Conditional return when just the LR is stored is a simple
1403          conditional-load instruction, that's not expensive.  */
1404       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1405         return 0;
1406
1407       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1408         return 0;
1409     }
1410
1411   /* If there are saved registers but the LR isn't saved, then we need
1412      two instructions for the return.  */
1413   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1414     return 0;
1415
1416   /* Can't be done if any of the FPA regs are pushed,
1417      since this also requires an insn.  */
1418   if (TARGET_HARD_FLOAT && TARGET_FPA)
1419     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1420       if (regs_ever_live[regno] && !call_used_regs[regno])
1421         return 0;
1422
1423   /* Likewise VFP regs.  */
1424   if (TARGET_HARD_FLOAT && TARGET_VFP)
1425     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1426       if (regs_ever_live[regno] && !call_used_regs[regno])
1427         return 0;
1428
1429   if (TARGET_REALLY_IWMMXT)
1430     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1431       if (regs_ever_live[regno] && ! call_used_regs [regno])
1432         return 0;
1433
1434   return 1;
1435 }
1436
1437 /* Return TRUE if int I is a valid immediate ARM constant.  */
1438
1439 int
1440 const_ok_for_arm (HOST_WIDE_INT i)
1441 {
1442   unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1443
1444   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1445      be all zero, or all one.  */
1446   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1447       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1448           != ((~(unsigned HOST_WIDE_INT) 0)
1449               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1450     return FALSE;
1451
1452   /* Fast return for 0 and powers of 2 */
1453   if ((i & (i - 1)) == 0)
1454     return TRUE;
1455
1456   do
1457     {
1458       if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1459         return TRUE;
1460       mask =
1461           (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1462                           >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1463     }
1464   while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1465
1466   return FALSE;
1467 }
1468
1469 /* Return true if I is a valid constant for the operation CODE.  */
1470 static int
1471 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1472 {
1473   if (const_ok_for_arm (i))
1474     return 1;
1475
1476   switch (code)
1477     {
1478     case PLUS:
1479       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1480
1481     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1482     case XOR:
1483     case IOR:
1484       return 0;
1485
1486     case AND:
1487       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1488
1489     default:
1490       abort ();
1491     }
1492 }
1493
1494 /* Emit a sequence of insns to handle a large constant.
1495    CODE is the code of the operation required, it can be any of SET, PLUS,
1496    IOR, AND, XOR, MINUS;
1497    MODE is the mode in which the operation is being performed;
1498    VAL is the integer to operate on;
1499    SOURCE is the other operand (a register, or a null-pointer for SET);
1500    SUBTARGETS means it is safe to create scratch registers if that will
1501    either produce a simpler sequence, or we will want to cse the values.
1502    Return value is the number of insns emitted.  */
1503
1504 int
1505 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1506                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1507 {
1508   rtx cond;
1509
1510   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1511     cond = COND_EXEC_TEST (PATTERN (insn));
1512   else
1513     cond = NULL_RTX;
1514
1515   if (subtargets || code == SET
1516       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1517           && REGNO (target) != REGNO (source)))
1518     {
1519       /* After arm_reorg has been called, we can't fix up expensive
1520          constants by pushing them into memory so we must synthesize
1521          them in-line, regardless of the cost.  This is only likely to
1522          be more costly on chips that have load delay slots and we are
1523          compiling without running the scheduler (so no splitting
1524          occurred before the final instruction emission).
1525
1526          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1527       */
1528       if (!after_arm_reorg
1529           && !cond
1530           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1531                                 1, 0)
1532               > arm_constant_limit + (code != SET)))
1533         {
1534           if (code == SET)
1535             {
1536               /* Currently SET is the only monadic value for CODE, all
1537                  the rest are diadic.  */
1538               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1539               return 1;
1540             }
1541           else
1542             {
1543               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1544
1545               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1546               /* For MINUS, the value is subtracted from, since we never
1547                  have subtraction of a constant.  */
1548               if (code == MINUS)
1549                 emit_insn (gen_rtx_SET (VOIDmode, target,
1550                                         gen_rtx_MINUS (mode, temp, source)));
1551               else
1552                 emit_insn (gen_rtx_SET (VOIDmode, target,
1553                                         gen_rtx_fmt_ee (code, mode, source, temp)));
1554               return 2;
1555             }
1556         }
1557     }
1558
1559   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1560                            1);
1561 }
1562
1563 static int
1564 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1565 {
1566   HOST_WIDE_INT temp1;
1567   int num_insns = 0;
1568   do
1569     {
1570       int end;
1571
1572       if (i <= 0)
1573         i += 32;
1574       if (remainder & (3 << (i - 2)))
1575         {
1576           end = i - 8;
1577           if (end < 0)
1578             end += 32;
1579           temp1 = remainder & ((0x0ff << end)
1580                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1581           remainder &= ~temp1;
1582           num_insns++;
1583           i -= 6;
1584         }
1585       i -= 2;
1586     } while (remainder);
1587   return num_insns;
1588 }
1589
1590 /* Emit an instruction with the indicated PATTERN.  If COND is
1591    non-NULL, conditionalize the execution of the instruction on COND
1592    being true.  */
1593
1594 static void
1595 emit_constant_insn (rtx cond, rtx pattern)
1596 {
1597   if (cond)
1598     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1599   emit_insn (pattern);
1600 }
1601
1602 /* As above, but extra parameter GENERATE which, if clear, suppresses
1603    RTL generation.  */
1604
1605 static int
1606 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1607                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1608                   int generate)
1609 {
1610   int can_invert = 0;
1611   int can_negate = 0;
1612   int can_negate_initial = 0;
1613   int can_shift = 0;
1614   int i;
1615   int num_bits_set = 0;
1616   int set_sign_bit_copies = 0;
1617   int clear_sign_bit_copies = 0;
1618   int clear_zero_bit_copies = 0;
1619   int set_zero_bit_copies = 0;
1620   int insns = 0;
1621   unsigned HOST_WIDE_INT temp1, temp2;
1622   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1623
1624   /* Find out which operations are safe for a given CODE.  Also do a quick
1625      check for degenerate cases; these can occur when DImode operations
1626      are split.  */
1627   switch (code)
1628     {
1629     case SET:
1630       can_invert = 1;
1631       can_shift = 1;
1632       can_negate = 1;
1633       break;
1634
1635     case PLUS:
1636       can_negate = 1;
1637       can_negate_initial = 1;
1638       break;
1639
1640     case IOR:
1641       if (remainder == 0xffffffff)
1642         {
1643           if (generate)
1644             emit_constant_insn (cond,
1645                                 gen_rtx_SET (VOIDmode, target,
1646                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1647           return 1;
1648         }
1649       if (remainder == 0)
1650         {
1651           if (reload_completed && rtx_equal_p (target, source))
1652             return 0;
1653           if (generate)
1654             emit_constant_insn (cond,
1655                                 gen_rtx_SET (VOIDmode, target, source));
1656           return 1;
1657         }
1658       break;
1659
1660     case AND:
1661       if (remainder == 0)
1662         {
1663           if (generate)
1664             emit_constant_insn (cond,
1665                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1666           return 1;
1667         }
1668       if (remainder == 0xffffffff)
1669         {
1670           if (reload_completed && rtx_equal_p (target, source))
1671             return 0;
1672           if (generate)
1673             emit_constant_insn (cond,
1674                                 gen_rtx_SET (VOIDmode, target, source));
1675           return 1;
1676         }
1677       can_invert = 1;
1678       break;
1679
1680     case XOR:
1681       if (remainder == 0)
1682         {
1683           if (reload_completed && rtx_equal_p (target, source))
1684             return 0;
1685           if (generate)
1686             emit_constant_insn (cond,
1687                                 gen_rtx_SET (VOIDmode, target, source));
1688           return 1;
1689         }
1690       if (remainder == 0xffffffff)
1691         {
1692           if (generate)
1693             emit_constant_insn (cond,
1694                                 gen_rtx_SET (VOIDmode, target,
1695                                              gen_rtx_NOT (mode, source)));
1696           return 1;
1697         }
1698
1699       /* We don't know how to handle this yet below.  */
1700       abort ();
1701
1702     case MINUS:
1703       /* We treat MINUS as (val - source), since (source - val) is always
1704          passed as (source + (-val)).  */
1705       if (remainder == 0)
1706         {
1707           if (generate)
1708             emit_constant_insn (cond,
1709                                 gen_rtx_SET (VOIDmode, target,
1710                                              gen_rtx_NEG (mode, source)));
1711           return 1;
1712         }
1713       if (const_ok_for_arm (val))
1714         {
1715           if (generate)
1716             emit_constant_insn (cond,
1717                                 gen_rtx_SET (VOIDmode, target,
1718                                              gen_rtx_MINUS (mode, GEN_INT (val),
1719                                                             source)));
1720           return 1;
1721         }
1722       can_negate = 1;
1723
1724       break;
1725
1726     default:
1727       abort ();
1728     }
1729
1730   /* If we can do it in one insn get out quickly.  */
1731   if (const_ok_for_arm (val)
1732       || (can_negate_initial && const_ok_for_arm (-val))
1733       || (can_invert && const_ok_for_arm (~val)))
1734     {
1735       if (generate)
1736         emit_constant_insn (cond,
1737                             gen_rtx_SET (VOIDmode, target,
1738                                          (source
1739                                           ? gen_rtx_fmt_ee (code, mode, source,
1740                                                             GEN_INT (val))
1741                                           : GEN_INT (val))));
1742       return 1;
1743     }
1744
1745   /* Calculate a few attributes that may be useful for specific
1746      optimizations.  */
1747   for (i = 31; i >= 0; i--)
1748     {
1749       if ((remainder & (1 << i)) == 0)
1750         clear_sign_bit_copies++;
1751       else
1752         break;
1753     }
1754
1755   for (i = 31; i >= 0; i--)
1756     {
1757       if ((remainder & (1 << i)) != 0)
1758         set_sign_bit_copies++;
1759       else
1760         break;
1761     }
1762
1763   for (i = 0; i <= 31; i++)
1764     {
1765       if ((remainder & (1 << i)) == 0)
1766         clear_zero_bit_copies++;
1767       else
1768         break;
1769     }
1770
1771   for (i = 0; i <= 31; i++)
1772     {
1773       if ((remainder & (1 << i)) != 0)
1774         set_zero_bit_copies++;
1775       else
1776         break;
1777     }
1778
1779   switch (code)
1780     {
1781     case SET:
1782       /* See if we can do this by sign_extending a constant that is known
1783          to be negative.  This is a good, way of doing it, since the shift
1784          may well merge into a subsequent insn.  */
1785       if (set_sign_bit_copies > 1)
1786         {
1787           if (const_ok_for_arm
1788               (temp1 = ARM_SIGN_EXTEND (remainder
1789                                         << (set_sign_bit_copies - 1))))
1790             {
1791               if (generate)
1792                 {
1793                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1794                   emit_constant_insn (cond,
1795                                       gen_rtx_SET (VOIDmode, new_src,
1796                                                    GEN_INT (temp1)));
1797                   emit_constant_insn (cond,
1798                                       gen_ashrsi3 (target, new_src,
1799                                                    GEN_INT (set_sign_bit_copies - 1)));
1800                 }
1801               return 2;
1802             }
1803           /* For an inverted constant, we will need to set the low bits,
1804              these will be shifted out of harm's way.  */
1805           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1806           if (const_ok_for_arm (~temp1))
1807             {
1808               if (generate)
1809                 {
1810                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1811                   emit_constant_insn (cond,
1812                                       gen_rtx_SET (VOIDmode, new_src,
1813                                                    GEN_INT (temp1)));
1814                   emit_constant_insn (cond,
1815                                       gen_ashrsi3 (target, new_src,
1816                                                    GEN_INT (set_sign_bit_copies - 1)));
1817                 }
1818               return 2;
1819             }
1820         }
1821
1822       /* See if we can generate this by setting the bottom (or the top)
1823          16 bits, and then shifting these into the other half of the
1824          word.  We only look for the simplest cases, to do more would cost
1825          too much.  Be careful, however, not to generate this when the
1826          alternative would take fewer insns.  */
1827       if (val & 0xffff0000)
1828         {
1829           temp1 = remainder & 0xffff0000;
1830           temp2 = remainder & 0x0000ffff;
1831
1832           /* Overlaps outside this range are best done using other methods.  */
1833           for (i = 9; i < 24; i++)
1834             {
1835               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1836                   && !const_ok_for_arm (temp2))
1837                 {
1838                   rtx new_src = (subtargets
1839                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1840                                  : target);
1841                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1842                                             source, subtargets, generate);
1843                   source = new_src;
1844                   if (generate)
1845                     emit_constant_insn
1846                       (cond,
1847                        gen_rtx_SET
1848                        (VOIDmode, target,
1849                         gen_rtx_IOR (mode,
1850                                      gen_rtx_ASHIFT (mode, source,
1851                                                      GEN_INT (i)),
1852                                      source)));
1853                   return insns + 1;
1854                 }
1855             }
1856
1857           /* Don't duplicate cases already considered.  */
1858           for (i = 17; i < 24; i++)
1859             {
1860               if (((temp1 | (temp1 >> i)) == remainder)
1861                   && !const_ok_for_arm (temp1))
1862                 {
1863                   rtx new_src = (subtargets
1864                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1865                                  : target);
1866                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1867                                             source, subtargets, generate);
1868                   source = new_src;
1869                   if (generate)
1870                     emit_constant_insn
1871                       (cond,
1872                        gen_rtx_SET (VOIDmode, target,
1873                                     gen_rtx_IOR
1874                                     (mode,
1875                                      gen_rtx_LSHIFTRT (mode, source,
1876                                                        GEN_INT (i)),
1877                                      source)));
1878                   return insns + 1;
1879                 }
1880             }
1881         }
1882       break;
1883
1884     case IOR:
1885     case XOR:
1886       /* If we have IOR or XOR, and the constant can be loaded in a
1887          single instruction, and we can find a temporary to put it in,
1888          then this can be done in two instructions instead of 3-4.  */
1889       if (subtargets
1890           /* TARGET can't be NULL if SUBTARGETS is 0 */
1891           || (reload_completed && !reg_mentioned_p (target, source)))
1892         {
1893           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1894             {
1895               if (generate)
1896                 {
1897                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1898
1899                   emit_constant_insn (cond,
1900                                       gen_rtx_SET (VOIDmode, sub,
1901                                                    GEN_INT (val)));
1902                   emit_constant_insn (cond,
1903                                       gen_rtx_SET (VOIDmode, target,
1904                                                    gen_rtx_fmt_ee (code, mode,
1905                                                                    source, sub)));
1906                 }
1907               return 2;
1908             }
1909         }
1910
1911       if (code == XOR)
1912         break;
1913
1914       if (set_sign_bit_copies > 8
1915           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1916         {
1917           if (generate)
1918             {
1919               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1920               rtx shift = GEN_INT (set_sign_bit_copies);
1921
1922               emit_constant_insn
1923                 (cond,
1924                  gen_rtx_SET (VOIDmode, sub,
1925                               gen_rtx_NOT (mode,
1926                                            gen_rtx_ASHIFT (mode,
1927                                                            source,
1928                                                            shift))));
1929               emit_constant_insn
1930                 (cond,
1931                  gen_rtx_SET (VOIDmode, target,
1932                               gen_rtx_NOT (mode,
1933                                            gen_rtx_LSHIFTRT (mode, sub,
1934                                                              shift))));
1935             }
1936           return 2;
1937         }
1938
1939       if (set_zero_bit_copies > 8
1940           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1941         {
1942           if (generate)
1943             {
1944               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1945               rtx shift = GEN_INT (set_zero_bit_copies);
1946
1947               emit_constant_insn
1948                 (cond,
1949                  gen_rtx_SET (VOIDmode, sub,
1950                               gen_rtx_NOT (mode,
1951                                            gen_rtx_LSHIFTRT (mode,
1952                                                              source,
1953                                                              shift))));
1954               emit_constant_insn
1955                 (cond,
1956                  gen_rtx_SET (VOIDmode, target,
1957                               gen_rtx_NOT (mode,
1958                                            gen_rtx_ASHIFT (mode, sub,
1959                                                            shift))));
1960             }
1961           return 2;
1962         }
1963
1964       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1965         {
1966           if (generate)
1967             {
1968               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1969               emit_constant_insn (cond,
1970                                   gen_rtx_SET (VOIDmode, sub,
1971                                                gen_rtx_NOT (mode, source)));
1972               source = sub;
1973               if (subtargets)
1974                 sub = gen_reg_rtx (mode);
1975               emit_constant_insn (cond,
1976                                   gen_rtx_SET (VOIDmode, sub,
1977                                                gen_rtx_AND (mode, source,
1978                                                             GEN_INT (temp1))));
1979               emit_constant_insn (cond,
1980                                   gen_rtx_SET (VOIDmode, target,
1981                                                gen_rtx_NOT (mode, sub)));
1982             }
1983           return 3;
1984         }
1985       break;
1986
1987     case AND:
1988       /* See if two shifts will do 2 or more insn's worth of work.  */
1989       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1990         {
1991           HOST_WIDE_INT shift_mask = ((0xffffffff
1992                                        << (32 - clear_sign_bit_copies))
1993                                       & 0xffffffff);
1994
1995           if ((remainder | shift_mask) != 0xffffffff)
1996             {
1997               if (generate)
1998                 {
1999                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2000                   insns = arm_gen_constant (AND, mode, cond,
2001                                             remainder | shift_mask,
2002                                             new_src, source, subtargets, 1);
2003                   source = new_src;
2004                 }
2005               else
2006                 {
2007                   rtx targ = subtargets ? NULL_RTX : target;
2008                   insns = arm_gen_constant (AND, mode, cond,
2009                                             remainder | shift_mask,
2010                                             targ, source, subtargets, 0);
2011                 }
2012             }
2013
2014           if (generate)
2015             {
2016               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2017               rtx shift = GEN_INT (clear_sign_bit_copies);
2018
2019               emit_insn (gen_ashlsi3 (new_src, source, shift));
2020               emit_insn (gen_lshrsi3 (target, new_src, shift));
2021             }
2022
2023           return insns + 2;
2024         }
2025
2026       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2027         {
2028           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2029
2030           if ((remainder | shift_mask) != 0xffffffff)
2031             {
2032               if (generate)
2033                 {
2034                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2035
2036                   insns = arm_gen_constant (AND, mode, cond,
2037                                             remainder | shift_mask,
2038                                             new_src, source, subtargets, 1);
2039                   source = new_src;
2040                 }
2041               else
2042                 {
2043                   rtx targ = subtargets ? NULL_RTX : target;
2044
2045                   insns = arm_gen_constant (AND, mode, cond,
2046                                             remainder | shift_mask,
2047                                             targ, source, subtargets, 0);
2048                 }
2049             }
2050
2051           if (generate)
2052             {
2053               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2054               rtx shift = GEN_INT (clear_zero_bit_copies);
2055
2056               emit_insn (gen_lshrsi3 (new_src, source, shift));
2057               emit_insn (gen_ashlsi3 (target, new_src, shift));
2058             }
2059
2060           return insns + 2;
2061         }
2062
2063       break;
2064
2065     default:
2066       break;
2067     }
2068
2069   for (i = 0; i < 32; i++)
2070     if (remainder & (1 << i))
2071       num_bits_set++;
2072
2073   if (code == AND || (can_invert && num_bits_set > 16))
2074     remainder = (~remainder) & 0xffffffff;
2075   else if (code == PLUS && num_bits_set > 16)
2076     remainder = (-remainder) & 0xffffffff;
2077   else
2078     {
2079       can_invert = 0;
2080       can_negate = 0;
2081     }
2082
2083   /* Now try and find a way of doing the job in either two or three
2084      instructions.
2085      We start by looking for the largest block of zeros that are aligned on
2086      a 2-bit boundary, we then fill up the temps, wrapping around to the
2087      top of the word when we drop off the bottom.
2088      In the worst case this code should produce no more than four insns.  */
2089   {
2090     int best_start = 0;
2091     int best_consecutive_zeros = 0;
2092
2093     for (i = 0; i < 32; i += 2)
2094       {
2095         int consecutive_zeros = 0;
2096
2097         if (!(remainder & (3 << i)))
2098           {
2099             while ((i < 32) && !(remainder & (3 << i)))
2100               {
2101                 consecutive_zeros += 2;
2102                 i += 2;
2103               }
2104             if (consecutive_zeros > best_consecutive_zeros)
2105               {
2106                 best_consecutive_zeros = consecutive_zeros;
2107                 best_start = i - consecutive_zeros;
2108               }
2109             i -= 2;
2110           }
2111       }
2112
2113     /* So long as it won't require any more insns to do so, it's
2114        desirable to emit a small constant (in bits 0...9) in the last
2115        insn.  This way there is more chance that it can be combined with
2116        a later addressing insn to form a pre-indexed load or store
2117        operation.  Consider:
2118
2119                *((volatile int *)0xe0000100) = 1;
2120                *((volatile int *)0xe0000110) = 2;
2121
2122        We want this to wind up as:
2123
2124                 mov rA, #0xe0000000
2125                 mov rB, #1
2126                 str rB, [rA, #0x100]
2127                 mov rB, #2
2128                 str rB, [rA, #0x110]
2129
2130        rather than having to synthesize both large constants from scratch.
2131
2132        Therefore, we calculate how many insns would be required to emit
2133        the constant starting from `best_start', and also starting from
2134        zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2135        yield a shorter sequence, we may as well use zero.  */
2136     if (best_start != 0
2137         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2138         && (count_insns_for_constant (remainder, 0) <=
2139             count_insns_for_constant (remainder, best_start)))
2140       best_start = 0;
2141
2142     /* Now start emitting the insns.  */
2143     i = best_start;
2144     do
2145       {
2146         int end;
2147
2148         if (i <= 0)
2149           i += 32;
2150         if (remainder & (3 << (i - 2)))
2151           {
2152             end = i - 8;
2153             if (end < 0)
2154               end += 32;
2155             temp1 = remainder & ((0x0ff << end)
2156                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2157             remainder &= ~temp1;
2158
2159             if (generate)
2160               {
2161                 rtx new_src, temp1_rtx;
2162
2163                 if (code == SET || code == MINUS)
2164                   {
2165                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2166                     if (can_invert && code != MINUS)
2167                       temp1 = ~temp1;
2168                   }
2169                 else
2170                   {
2171                     if (remainder && subtargets)
2172                       new_src = gen_reg_rtx (mode);
2173                     else
2174                       new_src = target;
2175                     if (can_invert)
2176                       temp1 = ~temp1;
2177                     else if (can_negate)
2178                       temp1 = -temp1;
2179                   }
2180
2181                 temp1 = trunc_int_for_mode (temp1, mode);
2182                 temp1_rtx = GEN_INT (temp1);
2183
2184                 if (code == SET)
2185                   ;
2186                 else if (code == MINUS)
2187                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2188                 else
2189                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2190
2191                 emit_constant_insn (cond,
2192                                     gen_rtx_SET (VOIDmode, new_src,
2193                                                  temp1_rtx));
2194                 source = new_src;
2195               }
2196
2197             if (code == SET)
2198               {
2199                 can_invert = 0;
2200                 code = PLUS;
2201               }
2202             else if (code == MINUS)
2203               code = PLUS;
2204
2205             insns++;
2206             i -= 6;
2207           }
2208         i -= 2;
2209       }
2210     while (remainder);
2211   }
2212
2213   return insns;
2214 }
2215
2216 /* Canonicalize a comparison so that we are more likely to recognize it.
2217    This can be done for a few constant compares, where we can make the
2218    immediate value easier to load.  */
2219
2220 enum rtx_code
2221 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2222 {
2223   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2224
2225   switch (code)
2226     {
2227     case EQ:
2228     case NE:
2229       return code;
2230
2231     case GT:
2232     case LE:
2233       if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2234           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2235         {
2236           *op1 = GEN_INT (i + 1);
2237           return code == GT ? GE : LT;
2238         }
2239       break;
2240
2241     case GE:
2242     case LT:
2243       if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2244           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2245         {
2246           *op1 = GEN_INT (i - 1);
2247           return code == GE ? GT : LE;
2248         }
2249       break;
2250
2251     case GTU:
2252     case LEU:
2253       if (i != ~((unsigned HOST_WIDE_INT) 0)
2254           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2255         {
2256           *op1 = GEN_INT (i + 1);
2257           return code == GTU ? GEU : LTU;
2258         }
2259       break;
2260
2261     case GEU:
2262     case LTU:
2263       if (i != 0
2264           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2265         {
2266           *op1 = GEN_INT (i - 1);
2267           return code == GEU ? GTU : LEU;
2268         }
2269       break;
2270
2271     default:
2272       abort ();
2273     }
2274
2275   return code;
2276 }
2277
2278
2279 /* Define how to find the value returned by a function.  */
2280
2281 rtx
2282 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2283 {
2284   enum machine_mode mode;
2285   int unsignedp ATTRIBUTE_UNUSED;
2286   rtx r ATTRIBUTE_UNUSED;
2287
2288
2289   mode = TYPE_MODE (type);
2290   /* Promote integer types.  */
2291   if (INTEGRAL_TYPE_P (type))
2292     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2293   return LIBCALL_VALUE(mode);
2294 }
2295
2296 /* Determine the amount of memory needed to store the possible return 
2297    registers of an untyped call.  */
2298 int
2299 arm_apply_result_size (void)
2300 {
2301   int size = 16;
2302
2303   if (TARGET_ARM)
2304     {
2305       if (TARGET_HARD_FLOAT_ABI)
2306         {
2307           if (TARGET_FPA)
2308             size += 12;
2309           if (TARGET_MAVERICK)
2310             size += 8;
2311         }
2312       if (TARGET_IWMMXT_ABI)
2313         size += 8;
2314     }
2315
2316   return size;
2317 }
2318
2319 /* Decide whether a type should be returned in memory (true)
2320    or in a register (false).  This is called by the macro
2321    RETURN_IN_MEMORY.  */
2322 int
2323 arm_return_in_memory (tree type)
2324 {
2325   HOST_WIDE_INT size;
2326
2327   if (!AGGREGATE_TYPE_P (type) &&
2328       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2329     /* All simple types are returned in registers.
2330        For AAPCS, complex types are treated the same as aggregates.  */
2331     return 0;
2332
2333   size = int_size_in_bytes (type);
2334
2335   if (arm_abi != ARM_ABI_APCS)
2336     {
2337       /* ATPCS and later return aggregate types in memory only if they are
2338          larger than a word (or are variable size).  */
2339       return (size < 0 || size > UNITS_PER_WORD);
2340     }
2341
2342   /* For the arm-wince targets we choose to be compatible with Microsoft's
2343      ARM and Thumb compilers, which always return aggregates in memory.  */
2344 #ifndef ARM_WINCE
2345   /* All structures/unions bigger than one word are returned in memory.
2346      Also catch the case where int_size_in_bytes returns -1.  In this case
2347      the aggregate is either huge or of variable size, and in either case
2348      we will want to return it via memory and not in a register.  */
2349   if (size < 0 || size > UNITS_PER_WORD)
2350     return 1;
2351
2352   if (TREE_CODE (type) == RECORD_TYPE)
2353     {
2354       tree field;
2355
2356       /* For a struct the APCS says that we only return in a register
2357          if the type is 'integer like' and every addressable element
2358          has an offset of zero.  For practical purposes this means
2359          that the structure can have at most one non bit-field element
2360          and that this element must be the first one in the structure.  */
2361
2362       /* Find the first field, ignoring non FIELD_DECL things which will
2363          have been created by C++.  */
2364       for (field = TYPE_FIELDS (type);
2365            field && TREE_CODE (field) != FIELD_DECL;
2366            field = TREE_CHAIN (field))
2367         continue;
2368
2369       if (field == NULL)
2370         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2371
2372       /* Check that the first field is valid for returning in a register.  */
2373
2374       /* ... Floats are not allowed */
2375       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2376         return 1;
2377
2378       /* ... Aggregates that are not themselves valid for returning in
2379          a register are not allowed.  */
2380       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2381         return 1;
2382
2383       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2384          since they are not addressable.  */
2385       for (field = TREE_CHAIN (field);
2386            field;
2387            field = TREE_CHAIN (field))
2388         {
2389           if (TREE_CODE (field) != FIELD_DECL)
2390             continue;
2391
2392           if (!DECL_BIT_FIELD_TYPE (field))
2393             return 1;
2394         }
2395
2396       return 0;
2397     }
2398
2399   if (TREE_CODE (type) == UNION_TYPE)
2400     {
2401       tree field;
2402
2403       /* Unions can be returned in registers if every element is
2404          integral, or can be returned in an integer register.  */
2405       for (field = TYPE_FIELDS (type);
2406            field;
2407            field = TREE_CHAIN (field))
2408         {
2409           if (TREE_CODE (field) != FIELD_DECL)
2410             continue;
2411
2412           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2413             return 1;
2414
2415           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2416             return 1;
2417         }
2418
2419       return 0;
2420     }
2421 #endif /* not ARM_WINCE */
2422
2423   /* Return all other types in memory.  */
2424   return 1;
2425 }
2426
2427 /* Indicate whether or not words of a double are in big-endian order.  */
2428
2429 int
2430 arm_float_words_big_endian (void)
2431 {
2432   if (TARGET_MAVERICK)
2433     return 0;
2434
2435   /* For FPA, float words are always big-endian.  For VFP, floats words
2436      follow the memory system mode.  */
2437
2438   if (TARGET_FPA)
2439     {
2440       return 1;
2441     }
2442
2443   if (TARGET_VFP)
2444     return (TARGET_BIG_END ? 1 : 0);
2445
2446   return 1;
2447 }
2448
2449 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2450    for a call to a function whose data type is FNTYPE.
2451    For a library call, FNTYPE is NULL.  */
2452 void
2453 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2454                           rtx libname  ATTRIBUTE_UNUSED,
2455                           tree fndecl ATTRIBUTE_UNUSED)
2456 {
2457   /* On the ARM, the offset starts at 0.  */
2458   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2459   pcum->iwmmxt_nregs = 0;
2460   pcum->can_split = true;
2461
2462   pcum->call_cookie = CALL_NORMAL;
2463
2464   if (TARGET_LONG_CALLS)
2465     pcum->call_cookie = CALL_LONG;
2466
2467   /* Check for long call/short call attributes.  The attributes
2468      override any command line option.  */
2469   if (fntype)
2470     {
2471       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2472         pcum->call_cookie = CALL_SHORT;
2473       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2474         pcum->call_cookie = CALL_LONG;
2475     }
2476
2477   /* Varargs vectors are treated the same as long long.
2478      named_count avoids having to change the way arm handles 'named' */
2479   pcum->named_count = 0;
2480   pcum->nargs = 0;
2481
2482   if (TARGET_REALLY_IWMMXT && fntype)
2483     {
2484       tree fn_arg;
2485
2486       for (fn_arg = TYPE_ARG_TYPES (fntype);
2487            fn_arg;
2488            fn_arg = TREE_CHAIN (fn_arg))
2489         pcum->named_count += 1;
2490
2491       if (! pcum->named_count)
2492         pcum->named_count = INT_MAX;
2493     }
2494 }
2495
2496
2497 /* Return true if mode/type need doubleword alignment.  */
2498 bool
2499 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2500 {
2501   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2502           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2503 }
2504
2505
2506 /* Determine where to put an argument to a function.
2507    Value is zero to push the argument on the stack,
2508    or a hard register in which to store the argument.
2509
2510    MODE is the argument's machine mode.
2511    TYPE is the data type of the argument (as a tree).
2512     This is null for libcalls where that information may
2513     not be available.
2514    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2515     the preceding args and about the function being called.
2516    NAMED is nonzero if this argument is a named parameter
2517     (otherwise it is an extra parameter matching an ellipsis).  */
2518
2519 rtx
2520 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2521                   tree type, int named)
2522 {
2523   int nregs;
2524
2525   /* Varargs vectors are treated the same as long long.
2526      named_count avoids having to change the way arm handles 'named' */
2527   if (TARGET_IWMMXT_ABI
2528       && arm_vector_mode_supported_p (mode)
2529       && pcum->named_count > pcum->nargs + 1)
2530     {
2531       if (pcum->iwmmxt_nregs <= 9)
2532         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2533       else
2534         {
2535           pcum->can_split = false;
2536           return NULL_RTX;
2537         }
2538     }
2539
2540   /* Put doubleword aligned quantities in even register pairs.  */
2541   if (pcum->nregs & 1
2542       && ARM_DOUBLEWORD_ALIGN
2543       && arm_needs_doubleword_align (mode, type))
2544     pcum->nregs++;
2545
2546   if (mode == VOIDmode)
2547     /* Compute operand 2 of the call insn.  */
2548     return GEN_INT (pcum->call_cookie);
2549
2550   /* Only allow splitting an arg between regs and memory if all preceding
2551      args were allocated to regs.  For args passed by reference we only count
2552      the reference pointer.  */
2553   if (pcum->can_split)
2554     nregs = 1;
2555   else
2556     nregs = ARM_NUM_REGS2 (mode, type);
2557
2558   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2559     return NULL_RTX;
2560
2561   return gen_rtx_REG (mode, pcum->nregs);
2562 }
2563
2564 static int
2565 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2566                        tree type, bool named ATTRIBUTE_UNUSED)
2567 {
2568   int nregs = pcum->nregs;
2569
2570   if (arm_vector_mode_supported_p (mode))
2571     return 0;
2572
2573   if (NUM_ARG_REGS > nregs
2574       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2575       && pcum->can_split)
2576     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2577
2578   return 0;
2579 }
2580
2581 /* Variable sized types are passed by reference.  This is a GCC
2582    extension to the ARM ABI.  */
2583
2584 static bool
2585 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2586                        enum machine_mode mode ATTRIBUTE_UNUSED,
2587                        tree type, bool named ATTRIBUTE_UNUSED)
2588 {
2589   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2590 }
2591 \f
2592 /* Encode the current state of the #pragma [no_]long_calls.  */
2593 typedef enum
2594 {
2595   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2596   LONG,         /* #pragma long_calls is in effect.  */
2597   SHORT         /* #pragma no_long_calls is in effect.  */
2598 } arm_pragma_enum;
2599
2600 static arm_pragma_enum arm_pragma_long_calls = OFF;
2601
2602 void
2603 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2604 {
2605   arm_pragma_long_calls = LONG;
2606 }
2607
2608 void
2609 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2610 {
2611   arm_pragma_long_calls = SHORT;
2612 }
2613
2614 void
2615 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2616 {
2617   arm_pragma_long_calls = OFF;
2618 }
2619 \f
2620 /* Table of machine attributes.  */
2621 const struct attribute_spec arm_attribute_table[] =
2622 {
2623   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2624   /* Function calls made to this symbol must be done indirectly, because
2625      it may lie outside of the 26 bit addressing range of a normal function
2626      call.  */
2627   { "long_call",    0, 0, false, true,  true,  NULL },
2628   /* Whereas these functions are always known to reside within the 26 bit
2629      addressing range.  */
2630   { "short_call",   0, 0, false, true,  true,  NULL },
2631   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
2632   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2633   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2634   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2635 #ifdef ARM_PE
2636   /* ARM/PE has three new attributes:
2637      interfacearm - ?
2638      dllexport - for exporting a function/variable that will live in a dll
2639      dllimport - for importing a function/variable from a dll
2640
2641      Microsoft allows multiple declspecs in one __declspec, separating
2642      them with spaces.  We do NOT support this.  Instead, use __declspec
2643      multiple times.
2644   */
2645   { "dllimport",    0, 0, true,  false, false, NULL },
2646   { "dllexport",    0, 0, true,  false, false, NULL },
2647   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2648 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2649   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
2650   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
2651   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
2652 #endif
2653   { NULL,           0, 0, false, false, false, NULL }
2654 };
2655
2656 /* Handle an attribute requiring a FUNCTION_DECL;
2657    arguments as in struct attribute_spec.handler.  */
2658 static tree
2659 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2660                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
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
2669   return NULL_TREE;
2670 }
2671
2672 /* Handle an "interrupt" or "isr" attribute;
2673    arguments as in struct attribute_spec.handler.  */
2674 static tree
2675 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2676                           bool *no_add_attrs)
2677 {
2678   if (DECL_P (*node))
2679     {
2680       if (TREE_CODE (*node) != FUNCTION_DECL)
2681         {
2682           warning ("%qs attribute only applies to functions",
2683                    IDENTIFIER_POINTER (name));
2684           *no_add_attrs = true;
2685         }
2686       /* FIXME: the argument if any is checked for type attributes;
2687          should it be checked for decl ones?  */
2688     }
2689   else
2690     {
2691       if (TREE_CODE (*node) == FUNCTION_TYPE
2692           || TREE_CODE (*node) == METHOD_TYPE)
2693         {
2694           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2695             {
2696               warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
2697               *no_add_attrs = true;
2698             }
2699         }
2700       else if (TREE_CODE (*node) == POINTER_TYPE
2701                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2702                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2703                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2704         {
2705           *node = build_variant_type_copy (*node);
2706           TREE_TYPE (*node) = build_type_attribute_variant
2707             (TREE_TYPE (*node),
2708              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2709           *no_add_attrs = true;
2710         }
2711       else
2712         {
2713           /* Possibly pass this attribute on from the type to a decl.  */
2714           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2715                        | (int) ATTR_FLAG_FUNCTION_NEXT
2716                        | (int) ATTR_FLAG_ARRAY_NEXT))
2717             {
2718               *no_add_attrs = true;
2719               return tree_cons (name, args, NULL_TREE);
2720             }
2721           else
2722             {
2723               warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
2724             }
2725         }
2726     }
2727
2728   return NULL_TREE;
2729 }
2730
2731 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2732 /* Handle the "notshared" attribute.  This attribute is another way of
2733    requesting hidden visibility.  ARM's compiler supports
2734    "__declspec(notshared)"; we support the same thing via an
2735    attribute.  */
2736
2737 static tree
2738 arm_handle_notshared_attribute (tree *node, 
2739                                 tree name ATTRIBUTE_UNUSED, 
2740                                 tree args ATTRIBUTE_UNUSED, 
2741                                 int flags ATTRIBUTE_UNUSED, 
2742                                 bool *no_add_attrs)
2743 {
2744   tree decl = TYPE_NAME (*node);
2745
2746   if (decl)
2747     {
2748       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2749       DECL_VISIBILITY_SPECIFIED (decl) = 1;
2750       *no_add_attrs = false;
2751     }
2752   return NULL_TREE;
2753 }
2754 #endif
2755
2756 /* Return 0 if the attributes for two types are incompatible, 1 if they
2757    are compatible, and 2 if they are nearly compatible (which causes a
2758    warning to be generated).  */
2759 static int
2760 arm_comp_type_attributes (tree type1, tree type2)
2761 {
2762   int l1, l2, s1, s2;
2763
2764   /* Check for mismatch of non-default calling convention.  */
2765   if (TREE_CODE (type1) != FUNCTION_TYPE)
2766     return 1;
2767
2768   /* Check for mismatched call attributes.  */
2769   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2770   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2771   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2772   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2773
2774   /* Only bother to check if an attribute is defined.  */
2775   if (l1 | l2 | s1 | s2)
2776     {
2777       /* If one type has an attribute, the other must have the same attribute.  */
2778       if ((l1 != l2) || (s1 != s2))
2779         return 0;
2780
2781       /* Disallow mixed attributes.  */
2782       if ((l1 & s2) || (l2 & s1))
2783         return 0;
2784     }
2785
2786   /* Check for mismatched ISR attribute.  */
2787   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2788   if (! l1)
2789     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2790   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2791   if (! l2)
2792     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2793   if (l1 != l2)
2794     return 0;
2795
2796   return 1;
2797 }
2798
2799 /*  Encode long_call or short_call attribute by prefixing
2800     symbol name in DECL with a special character FLAG.  */
2801 void
2802 arm_encode_call_attribute (tree decl, int flag)
2803 {
2804   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2805   int          len = strlen (str);
2806   char *       newstr;
2807
2808   /* Do not allow weak functions to be treated as short call.  */
2809   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2810     return;
2811
2812   newstr = alloca (len + 2);
2813   newstr[0] = flag;
2814   strcpy (newstr + 1, str);
2815
2816   newstr = (char *) ggc_alloc_string (newstr, len + 1);
2817   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2818 }
2819
2820 /*  Assigns default attributes to newly defined type.  This is used to
2821     set short_call/long_call attributes for function types of
2822     functions defined inside corresponding #pragma scopes.  */
2823 static void
2824 arm_set_default_type_attributes (tree type)
2825 {
2826   /* Add __attribute__ ((long_call)) to all functions, when
2827      inside #pragma long_calls or __attribute__ ((short_call)),
2828      when inside #pragma no_long_calls.  */
2829   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2830     {
2831       tree type_attr_list, attr_name;
2832       type_attr_list = TYPE_ATTRIBUTES (type);
2833
2834       if (arm_pragma_long_calls == LONG)
2835         attr_name = get_identifier ("long_call");
2836       else if (arm_pragma_long_calls == SHORT)
2837         attr_name = get_identifier ("short_call");
2838       else
2839         return;
2840
2841       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2842       TYPE_ATTRIBUTES (type) = type_attr_list;
2843     }
2844 }
2845 \f
2846 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2847    defined within the current compilation unit.  If this cannot be
2848    determined, then 0 is returned.  */
2849 static int
2850 current_file_function_operand (rtx sym_ref)
2851 {
2852   /* This is a bit of a fib.  A function will have a short call flag
2853      applied to its name if it has the short call attribute, or it has
2854      already been defined within the current compilation unit.  */
2855   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2856     return 1;
2857
2858   /* The current function is always defined within the current compilation
2859      unit.  If it s a weak definition however, then this may not be the real
2860      definition of the function, and so we have to say no.  */
2861   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2862       && !DECL_WEAK (current_function_decl))
2863     return 1;
2864
2865   /* We cannot make the determination - default to returning 0.  */
2866   return 0;
2867 }
2868
2869 /* Return nonzero if a 32 bit "long_call" should be generated for
2870    this call.  We generate a long_call if the function:
2871
2872         a.  has an __attribute__((long call))
2873      or b.  is within the scope of a #pragma long_calls
2874      or c.  the -mlong-calls command line switch has been specified
2875          .  and either:
2876                 1. -ffunction-sections is in effect
2877              or 2. the current function has __attribute__ ((section))
2878              or 3. the target function has __attribute__ ((section))
2879
2880    However we do not generate a long call if the function:
2881
2882         d.  has an __attribute__ ((short_call))
2883      or e.  is inside the scope of a #pragma no_long_calls
2884      or f.  is defined within the current compilation unit.
2885
2886    This function will be called by C fragments contained in the machine
2887    description file.  SYM_REF and CALL_COOKIE correspond to the matched
2888    rtl operands.  CALL_SYMBOL is used to distinguish between
2889    two different callers of the function.  It is set to 1 in the
2890    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2891    and "call_value" patterns.  This is because of the difference in the
2892    SYM_REFs passed by these patterns.  */
2893 int
2894 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2895 {
2896   if (!call_symbol)
2897     {
2898       if (GET_CODE (sym_ref) != MEM)
2899         return 0;
2900
2901       sym_ref = XEXP (sym_ref, 0);
2902     }
2903
2904   if (GET_CODE (sym_ref) != SYMBOL_REF)
2905     return 0;
2906
2907   if (call_cookie & CALL_SHORT)
2908     return 0;
2909
2910   if (TARGET_LONG_CALLS)
2911     {
2912       if (flag_function_sections
2913           || DECL_SECTION_NAME (current_function_decl))
2914         /* c.3 is handled by the definition of the
2915            ARM_DECLARE_FUNCTION_SIZE macro.  */
2916         return 1;
2917     }
2918
2919   if (current_file_function_operand (sym_ref))
2920     return 0;
2921
2922   return (call_cookie & CALL_LONG)
2923     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2924     || TARGET_LONG_CALLS;
2925 }
2926
2927 /* Return nonzero if it is ok to make a tail-call to DECL.  */
2928 static bool
2929 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
2930 {
2931   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2932
2933   if (cfun->machine->sibcall_blocked)
2934     return false;
2935
2936   /* Never tailcall something for which we have no decl, or if we
2937      are in Thumb mode.  */
2938   if (decl == NULL || TARGET_THUMB)
2939     return false;
2940
2941   /* Get the calling method.  */
2942   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2943     call_type = CALL_SHORT;
2944   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2945     call_type = CALL_LONG;
2946
2947   /* Cannot tail-call to long calls, since these are out of range of
2948      a branch instruction.  However, if not compiling PIC, we know
2949      we can reach the symbol if it is in this compilation unit.  */
2950   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2951     return false;
2952
2953   /* If we are interworking and the function is not declared static
2954      then we can't tail-call it unless we know that it exists in this
2955      compilation unit (since it might be a Thumb routine).  */
2956   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2957     return false;
2958
2959   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
2960   if (IS_INTERRUPT (arm_current_func_type ()))
2961     return false;
2962
2963   /* Everything else is ok.  */
2964   return true;
2965 }
2966
2967 \f
2968 /* Addressing mode support functions.  */
2969
2970 /* Return nonzero if X is a legitimate immediate operand when compiling
2971    for PIC.  */
2972 int
2973 legitimate_pic_operand_p (rtx x)
2974 {
2975   if (CONSTANT_P (x)
2976       && flag_pic
2977       && (GET_CODE (x) == SYMBOL_REF
2978           || (GET_CODE (x) == CONST
2979               && GET_CODE (XEXP (x, 0)) == PLUS
2980               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2981     return 0;
2982
2983   return 1;
2984 }
2985
2986 rtx
2987 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
2988 {
2989   if (GET_CODE (orig) == SYMBOL_REF
2990       || GET_CODE (orig) == LABEL_REF)
2991     {
2992 #ifndef AOF_ASSEMBLER
2993       rtx pic_ref, address;
2994 #endif
2995       rtx insn;
2996       int subregs = 0;
2997
2998       if (reg == 0)
2999         {
3000           if (no_new_pseudos)
3001             abort ();
3002           else
3003             reg = gen_reg_rtx (Pmode);
3004
3005           subregs = 1;
3006         }
3007
3008 #ifdef AOF_ASSEMBLER
3009       /* The AOF assembler can generate relocations for these directly, and
3010          understands that the PIC register has to be added into the offset.  */
3011       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3012 #else
3013       if (subregs)
3014         address = gen_reg_rtx (Pmode);
3015       else
3016         address = reg;
3017
3018       if (TARGET_ARM)
3019         emit_insn (gen_pic_load_addr_arm (address, orig));
3020       else
3021         emit_insn (gen_pic_load_addr_thumb (address, orig));
3022
3023       if ((GET_CODE (orig) == LABEL_REF
3024            || (GET_CODE (orig) == SYMBOL_REF &&
3025                SYMBOL_REF_LOCAL_P (orig)))
3026           && NEED_GOT_RELOC)
3027         pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
3028       else
3029         {
3030           pic_ref = gen_const_mem (Pmode,
3031                                    gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3032                                                  address));
3033         }
3034
3035       insn = emit_move_insn (reg, pic_ref);
3036 #endif
3037       current_function_uses_pic_offset_table = 1;
3038       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3039          by loop.  */
3040       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3041                                             REG_NOTES (insn));
3042       return reg;
3043     }
3044   else if (GET_CODE (orig) == CONST)
3045     {
3046       rtx base, offset;
3047
3048       if (GET_CODE (XEXP (orig, 0)) == PLUS
3049           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3050         return orig;
3051
3052       if (reg == 0)
3053         {
3054           if (no_new_pseudos)
3055             abort ();
3056           else
3057             reg = gen_reg_rtx (Pmode);
3058         }
3059
3060       if (GET_CODE (XEXP (orig, 0)) == PLUS)
3061         {
3062           base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3063           offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3064                                            base == reg ? 0 : reg);
3065         }
3066       else
3067         abort ();
3068
3069       if (GET_CODE (offset) == CONST_INT)
3070         {
3071           /* The base register doesn't really matter, we only want to
3072              test the index for the appropriate mode.  */
3073           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3074             {
3075               if (!no_new_pseudos)
3076                 offset = force_reg (Pmode, offset);
3077               else
3078                 abort ();
3079             }
3080
3081           if (GET_CODE (offset) == CONST_INT)
3082             return plus_constant (base, INTVAL (offset));
3083         }
3084
3085       if (GET_MODE_SIZE (mode) > 4
3086           && (GET_MODE_CLASS (mode) == MODE_INT
3087               || TARGET_SOFT_FLOAT))
3088         {
3089           emit_insn (gen_addsi3 (reg, base, offset));
3090           return reg;
3091         }
3092
3093       return gen_rtx_PLUS (Pmode, base, offset);
3094     }
3095
3096   return orig;
3097 }
3098
3099
3100 /* Find a spare low register to use during the prolog of a function.  */
3101
3102 static int
3103 thumb_find_work_register (unsigned long pushed_regs_mask)
3104 {
3105   int reg;
3106
3107   /* Check the argument registers first as these are call-used.  The
3108      register allocation order means that sometimes r3 might be used
3109      but earlier argument registers might not, so check them all.  */
3110   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3111     if (!regs_ever_live[reg])
3112       return reg;
3113
3114   /* Before going on to check the call-saved registers we can try a couple
3115      more ways of deducing that r3 is available.  The first is when we are
3116      pushing anonymous arguments onto the stack and we have less than 4
3117      registers worth of fixed arguments(*).  In this case r3 will be part of
3118      the variable argument list and so we can be sure that it will be
3119      pushed right at the start of the function.  Hence it will be available
3120      for the rest of the prologue.
3121      (*): ie current_function_pretend_args_size is greater than 0.  */
3122   if (cfun->machine->uses_anonymous_args
3123       && current_function_pretend_args_size > 0)
3124     return LAST_ARG_REGNUM;
3125
3126   /* The other case is when we have fixed arguments but less than 4 registers
3127      worth.  In this case r3 might be used in the body of the function, but
3128      it is not being used to convey an argument into the function.  In theory
3129      we could just check current_function_args_size to see how many bytes are
3130      being passed in argument registers, but it seems that it is unreliable.
3131      Sometimes it will have the value 0 when in fact arguments are being
3132      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3133      check the args_info.nregs field as well.  The problem with this field is
3134      that it makes no allowances for arguments that are passed to the
3135      function but which are not used.  Hence we could miss an opportunity
3136      when a function has an unused argument in r3.  But it is better to be
3137      safe than to be sorry.  */
3138   if (! cfun->machine->uses_anonymous_args
3139       && current_function_args_size >= 0
3140       && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3141       && cfun->args_info.nregs < 4)
3142     return LAST_ARG_REGNUM;
3143   
3144   /* Otherwise look for a call-saved register that is going to be pushed.  */
3145   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3146     if (pushed_regs_mask & (1 << reg))
3147       return reg;
3148
3149   /* Something went wrong - thumb_compute_save_reg_mask()
3150      should have arranged for a suitable register to be pushed.  */
3151   abort ();
3152 }
3153
3154
3155 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3156    low register.  */
3157
3158 void
3159 arm_load_pic_register (unsigned int scratch)
3160 {
3161 #ifndef AOF_ASSEMBLER
3162   rtx l1, pic_tmp, pic_tmp2, pic_rtx;
3163   rtx global_offset_table;
3164
3165   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3166     return;
3167
3168   if (!flag_pic)
3169     abort ();
3170
3171   l1 = gen_label_rtx ();
3172
3173   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3174   /* On the ARM the PC register contains 'dot + 8' at the time of the
3175      addition, on the Thumb it is 'dot + 4'.  */
3176   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
3177   if (GOT_PCREL)
3178     pic_tmp2 = gen_rtx_CONST (VOIDmode,
3179                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3180   else
3181     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3182
3183   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3184
3185   if (TARGET_ARM)
3186     {
3187       emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3188       emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
3189     }
3190   else
3191     {
3192       if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3193         {
3194           /* We will have pushed the pic register, so should always be
3195              able to find a work register.  */
3196           pic_tmp = gen_rtx_REG (SImode, scratch);
3197           emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3198           emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3199         }
3200       else
3201         emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3202       emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
3203     }
3204
3205   /* Need to emit this whether or not we obey regdecls,
3206      since setjmp/longjmp can cause life info to screw up.  */
3207   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3208 #endif /* AOF_ASSEMBLER */
3209 }
3210
3211
3212 /* Return nonzero if X is valid as an ARM state addressing register.  */
3213 static int
3214 arm_address_register_rtx_p (rtx x, int strict_p)
3215 {
3216   int regno;
3217
3218   if (GET_CODE (x) != REG)
3219     return 0;
3220
3221   regno = REGNO (x);
3222
3223   if (strict_p)
3224     return ARM_REGNO_OK_FOR_BASE_P (regno);
3225
3226   return (regno <= LAST_ARM_REGNUM
3227           || regno >= FIRST_PSEUDO_REGISTER
3228           || regno == FRAME_POINTER_REGNUM
3229           || regno == ARG_POINTER_REGNUM);
3230 }
3231
3232 /* Return nonzero if X is a valid ARM state address operand.  */
3233 int
3234 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3235                           int strict_p)
3236 {
3237   bool use_ldrd;
3238   enum rtx_code code = GET_CODE (x);
3239
3240   if (arm_address_register_rtx_p (x, strict_p))
3241     return 1;
3242
3243   use_ldrd = (TARGET_LDRD
3244               && (mode == DImode
3245                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3246
3247   if (code == POST_INC || code == PRE_DEC
3248       || ((code == PRE_INC || code == POST_DEC)
3249           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3250     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3251
3252   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3253            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3254            && GET_CODE (XEXP (x, 1)) == PLUS
3255            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3256     {
3257       rtx addend = XEXP (XEXP (x, 1), 1);
3258
3259       /* Don't allow ldrd post increment by register because it's hard
3260          to fixup invalid register choices.  */
3261       if (use_ldrd
3262           && GET_CODE (x) == POST_MODIFY
3263           && GET_CODE (addend) == REG)
3264         return 0;
3265
3266       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3267               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3268     }
3269
3270   /* After reload constants split into minipools will have addresses
3271      from a LABEL_REF.  */
3272   else if (reload_completed
3273            && (code == LABEL_REF
3274                || (code == CONST
3275                    && GET_CODE (XEXP (x, 0)) == PLUS
3276                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3277                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3278     return 1;
3279
3280   else if (mode == TImode)
3281     return 0;
3282
3283   else if (code == PLUS)
3284     {
3285       rtx xop0 = XEXP (x, 0);
3286       rtx xop1 = XEXP (x, 1);
3287
3288       return ((arm_address_register_rtx_p (xop0, strict_p)
3289                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3290               || (arm_address_register_rtx_p (xop1, strict_p)
3291                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3292     }
3293
3294 #if 0
3295   /* Reload currently can't handle MINUS, so disable this for now */
3296   else if (GET_CODE (x) == MINUS)
3297     {
3298       rtx xop0 = XEXP (x, 0);
3299       rtx xop1 = XEXP (x, 1);
3300
3301       return (arm_address_register_rtx_p (xop0, strict_p)
3302               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3303     }
3304 #endif
3305
3306   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3307            && code == SYMBOL_REF
3308            && CONSTANT_POOL_ADDRESS_P (x)
3309            && ! (flag_pic
3310                  && symbol_mentioned_p (get_pool_constant (x))))
3311     return 1;
3312
3313   return 0;
3314 }
3315
3316 /* Return nonzero if INDEX is valid for an address index operand in
3317    ARM state.  */
3318 static int
3319 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3320                         int strict_p)
3321 {
3322   HOST_WIDE_INT range;
3323   enum rtx_code code = GET_CODE (index);
3324
3325   /* Standard coprocessor addressing modes.  */
3326   if (TARGET_HARD_FLOAT
3327       && (TARGET_FPA || TARGET_MAVERICK)
3328       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3329           || (TARGET_MAVERICK && mode == DImode)))
3330     return (code == CONST_INT && INTVAL (index) < 1024
3331             && INTVAL (index) > -1024
3332             && (INTVAL (index) & 3) == 0);
3333
3334   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3335     return (code == CONST_INT
3336             && INTVAL (index) < 1024
3337             && INTVAL (index) > -1024
3338             && (INTVAL (index) & 3) == 0);
3339
3340   if (arm_address_register_rtx_p (index, strict_p)
3341       && (GET_MODE_SIZE (mode) <= 4))
3342     return 1;
3343
3344   if (mode == DImode || mode == DFmode)
3345     {
3346       if (code == CONST_INT)
3347         {
3348           HOST_WIDE_INT val = INTVAL (index);
3349
3350           if (TARGET_LDRD)
3351             return val > -256 && val < 256;
3352           else
3353             return val > -4096 && val < 4092;
3354         }
3355
3356       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3357     }
3358
3359   if (GET_MODE_SIZE (mode) <= 4
3360       && ! (arm_arch4
3361             && (mode == HImode
3362                 || (mode == QImode && outer == SIGN_EXTEND))))
3363     {
3364       if (code == MULT)
3365         {
3366           rtx xiop0 = XEXP (index, 0);
3367           rtx xiop1 = XEXP (index, 1);
3368
3369           return ((arm_address_register_rtx_p (xiop0, strict_p)
3370                    && power_of_two_operand (xiop1, SImode))
3371                   || (arm_address_register_rtx_p (xiop1, strict_p)
3372                       && power_of_two_operand (xiop0, SImode)));
3373         }
3374       else if (code == LSHIFTRT || code == ASHIFTRT
3375                || code == ASHIFT || code == ROTATERT)
3376         {
3377           rtx op = XEXP (index, 1);
3378
3379           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3380                   && GET_CODE (op) == CONST_INT
3381                   && INTVAL (op) > 0
3382                   && INTVAL (op) <= 31);
3383         }
3384     }
3385
3386   /* For ARM v4 we may be doing a sign-extend operation during the
3387      load.  */
3388   if (arm_arch4)
3389     {
3390       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3391         range = 256;
3392       else
3393         range = 4096;
3394     }
3395   else
3396     range = (mode == HImode) ? 4095 : 4096;
3397
3398   return (code == CONST_INT
3399           && INTVAL (index) < range
3400           && INTVAL (index) > -range);
3401 }
3402
3403 /* Return nonzero if X is valid as a Thumb state base register.  */
3404 static int
3405 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3406 {
3407   int regno;
3408
3409   if (GET_CODE (x) != REG)
3410     return 0;
3411
3412   regno = REGNO (x);
3413
3414   if (strict_p)
3415     return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3416
3417   return (regno <= LAST_LO_REGNUM
3418           || regno > LAST_VIRTUAL_REGISTER
3419           || regno == FRAME_POINTER_REGNUM
3420           || (GET_MODE_SIZE (mode) >= 4
3421               && (regno == STACK_POINTER_REGNUM
3422                   || regno >= FIRST_PSEUDO_REGISTER
3423                   || x == hard_frame_pointer_rtx
3424                   || x == arg_pointer_rtx)));
3425 }
3426
3427 /* Return nonzero if x is a legitimate index register.  This is the case
3428    for any base register that can access a QImode object.  */
3429 inline static int
3430 thumb_index_register_rtx_p (rtx x, int strict_p)
3431 {
3432   return thumb_base_register_rtx_p (x, QImode, strict_p);
3433 }
3434
3435 /* Return nonzero if x is a legitimate Thumb-state address.
3436
3437    The AP may be eliminated to either the SP or the FP, so we use the
3438    least common denominator, e.g. SImode, and offsets from 0 to 64.
3439
3440    ??? Verify whether the above is the right approach.
3441
3442    ??? Also, the FP may be eliminated to the SP, so perhaps that
3443    needs special handling also.
3444
3445    ??? Look at how the mips16 port solves this problem.  It probably uses
3446    better ways to solve some of these problems.
3447
3448    Although it is not incorrect, we don't accept QImode and HImode
3449    addresses based on the frame pointer or arg pointer until the
3450    reload pass starts.  This is so that eliminating such addresses
3451    into stack based ones won't produce impossible code.  */
3452 int
3453 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3454 {
3455   /* ??? Not clear if this is right.  Experiment.  */
3456   if (GET_MODE_SIZE (mode) < 4
3457       && !(reload_in_progress || reload_completed)
3458       && (reg_mentioned_p (frame_pointer_rtx, x)
3459           || reg_mentioned_p (arg_pointer_rtx, x)
3460           || reg_mentioned_p (virtual_incoming_args_rtx, x)
3461           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3462           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3463           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3464     return 0;
3465
3466   /* Accept any base register.  SP only in SImode or larger.  */
3467   else if (thumb_base_register_rtx_p (x, mode, strict_p))
3468     return 1;
3469
3470   /* This is PC relative data before arm_reorg runs.  */
3471   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3472            && GET_CODE (x) == SYMBOL_REF
3473            && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3474     return 1;
3475
3476   /* This is PC relative data after arm_reorg runs.  */
3477   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3478            && (GET_CODE (x) == LABEL_REF
3479                || (GET_CODE (x) == CONST
3480                    && GET_CODE (XEXP (x, 0)) == PLUS
3481                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3482                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3483     return 1;
3484
3485   /* Post-inc indexing only supported for SImode and larger.  */
3486   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3487            && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3488     return 1;
3489
3490   else if (GET_CODE (x) == PLUS)
3491     {
3492       /* REG+REG address can be any two index registers.  */
3493       /* We disallow FRAME+REG addressing since we know that FRAME
3494          will be replaced with STACK, and SP relative addressing only
3495          permits SP+OFFSET.  */
3496       if (GET_MODE_SIZE (mode) <= 4
3497           && XEXP (x, 0) != frame_pointer_rtx
3498           && XEXP (x, 1) != frame_pointer_rtx
3499           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3500           && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3501         return 1;
3502
3503       /* REG+const has 5-7 bit offset for non-SP registers.  */
3504       else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3505                 || XEXP (x, 0) == arg_pointer_rtx)
3506                && GET_CODE (XEXP (x, 1)) == CONST_INT
3507                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3508         return 1;
3509
3510       /* REG+const has 10 bit offset for SP, but only SImode and
3511          larger is supported.  */
3512       /* ??? Should probably check for DI/DFmode overflow here
3513          just like GO_IF_LEGITIMATE_OFFSET does.  */
3514       else if (GET_CODE (XEXP (x, 0)) == REG
3515                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3516                && GET_MODE_SIZE (mode) >= 4
3517                && GET_CODE (XEXP (x, 1)) == CONST_INT
3518                && INTVAL (XEXP (x, 1)) >= 0
3519                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3520                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3521         return 1;
3522
3523       else if (GET_CODE (XEXP (x, 0)) == REG
3524                && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3525                && GET_MODE_SIZE (mode) >= 4
3526                && GET_CODE (XEXP (x, 1)) == CONST_INT
3527                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3528         return 1;
3529     }
3530
3531   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3532            && GET_MODE_SIZE (mode) == 4
3533            && GET_CODE (x) == SYMBOL_REF
3534            && CONSTANT_POOL_ADDRESS_P (x)
3535            && !(flag_pic
3536                 && symbol_mentioned_p (get_pool_constant (x))))
3537     return 1;
3538
3539   return 0;
3540 }
3541
3542 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3543    instruction of mode MODE.  */
3544 int
3545 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3546 {
3547   switch (GET_MODE_SIZE (mode))
3548     {
3549     case 1:
3550       return val >= 0 && val < 32;
3551
3552     case 2:
3553       return val >= 0 && val < 64 && (val & 1) == 0;
3554
3555     default:
3556       return (val >= 0
3557               && (val + GET_MODE_SIZE (mode)) <= 128
3558               && (val & 3) == 0);
3559     }
3560 }
3561
3562 /* Try machine-dependent ways of modifying an illegitimate address
3563    to be legitimate.  If we find one, return the new, valid address.  */
3564 rtx
3565 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3566 {
3567   if (GET_CODE (x) == PLUS)
3568     {
3569       rtx xop0 = XEXP (x, 0);
3570       rtx xop1 = XEXP (x, 1);
3571
3572       if (CONSTANT_P (xop0) && !symbol_mentioned_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 (ARM_BASE_REGISTER_RTX_P (xop0)
3579           && GET_CODE (xop1) == CONST_INT)
3580         {
3581           HOST_WIDE_INT n, low_n;
3582           rtx base_reg, val;
3583           n = INTVAL (xop1);
3584
3585           /* VFP addressing modes actually allow greater offsets, but for
3586              now we just stick with the lowest common denominator.  */
3587           if (mode == DImode
3588               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3589             {
3590               low_n = n & 0x0f;
3591               n &= ~0x0f;
3592               if (low_n > 4)
3593                 {
3594                   n += 16;
3595                   low_n -= 16;
3596                 }
3597             }
3598           else
3599             {
3600               low_n = ((mode) == TImode ? 0
3601                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3602               n -= low_n;
3603             }
3604
3605           base_reg = gen_reg_rtx (SImode);
3606           val = force_operand (gen_rtx_PLUS (SImode, xop0,
3607                                              GEN_INT (n)), NULL_RTX);
3608           emit_move_insn (base_reg, val);
3609           x = (low_n == 0 ? base_reg
3610                : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3611         }
3612       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3613         x = gen_rtx_PLUS (SImode, xop0, xop1);
3614     }
3615
3616   /* XXX We don't allow MINUS any more -- see comment in
3617      arm_legitimate_address_p ().  */
3618   else if (GET_CODE (x) == MINUS)
3619     {
3620       rtx xop0 = XEXP (x, 0);
3621       rtx xop1 = XEXP (x, 1);
3622
3623       if (CONSTANT_P (xop0))
3624         xop0 = force_reg (SImode, xop0);
3625
3626       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3627         xop1 = force_reg (SImode, xop1);
3628
3629       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3630         x = gen_rtx_MINUS (SImode, xop0, xop1);
3631     }
3632
3633   if (flag_pic)
3634     {
3635       /* We need to find and carefully transform any SYMBOL and LABEL
3636          references; so go back to the original address expression.  */
3637       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3638
3639       if (new_x != orig_x)
3640         x = new_x;
3641     }
3642
3643   return x;
3644 }
3645
3646
3647 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3648    to be legitimate.  If we find one, return the new, valid address.  */
3649 rtx
3650 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3651 {
3652   if (GET_CODE (x) == PLUS
3653       && GET_CODE (XEXP (x, 1)) == CONST_INT
3654       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3655           || INTVAL (XEXP (x, 1)) < 0))
3656     {
3657       rtx xop0 = XEXP (x, 0);
3658       rtx xop1 = XEXP (x, 1);
3659       HOST_WIDE_INT offset = INTVAL (xop1);
3660
3661       /* Try and fold the offset into a biasing of the base register and
3662          then offsetting that.  Don't do this when optimizing for space
3663          since it can cause too many CSEs.  */
3664       if (optimize_size && offset >= 0
3665           && offset < 256 + 31 * GET_MODE_SIZE (mode))
3666         {
3667           HOST_WIDE_INT delta;
3668
3669           if (offset >= 256)
3670             delta = offset - (256 - GET_MODE_SIZE (mode));
3671           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3672             delta = 31 * GET_MODE_SIZE (mode);
3673           else
3674             delta = offset & (~31 * GET_MODE_SIZE (mode));
3675
3676           xop0 = force_operand (plus_constant (xop0, offset - delta),
3677                                 NULL_RTX);
3678           x = plus_constant (xop0, delta);
3679         }
3680       else if (offset < 0 && offset > -256)
3681         /* Small negative offsets are best done with a subtract before the
3682            dereference, forcing these into a register normally takes two
3683            instructions.  */
3684         x = force_operand (x, NULL_RTX);
3685       else
3686         {
3687           /* For the remaining cases, force the constant into a register.  */
3688           xop1 = force_reg (SImode, xop1);
3689           x = gen_rtx_PLUS (SImode, xop0, xop1);
3690         }
3691     }
3692   else if (GET_CODE (x) == PLUS
3693            && s_register_operand (XEXP (x, 1), SImode)
3694            && !s_register_operand (XEXP (x, 0), SImode))
3695     {
3696       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3697
3698       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3699     }
3700
3701   if (flag_pic)
3702     {
3703       /* We need to find and carefully transform any SYMBOL and LABEL
3704          references; so go back to the original address expression.  */
3705       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3706
3707       if (new_x != orig_x)
3708         x = new_x;
3709     }
3710
3711   return x;
3712 }
3713
3714 \f
3715
3716 #define REG_OR_SUBREG_REG(X)                                            \
3717   (GET_CODE (X) == REG                                                  \
3718    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3719
3720 #define REG_OR_SUBREG_RTX(X)                    \
3721    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3722
3723 #ifndef COSTS_N_INSNS
3724 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3725 #endif
3726 static inline int
3727 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3728 {
3729   enum machine_mode mode = GET_MODE (x);
3730
3731   switch (code)
3732     {
3733     case ASHIFT:
3734     case ASHIFTRT:
3735     case LSHIFTRT:
3736     case ROTATERT:
3737     case PLUS:
3738     case MINUS:
3739     case COMPARE:
3740     case NEG:
3741     case NOT:
3742       return COSTS_N_INSNS (1);
3743
3744     case MULT:
3745       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3746         {
3747           int cycles = 0;
3748           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3749
3750           while (i)
3751             {
3752               i >>= 2;
3753               cycles++;
3754             }
3755           return COSTS_N_INSNS (2) + cycles;
3756         }
3757       return COSTS_N_INSNS (1) + 16;
3758
3759     case SET:
3760       return (COSTS_N_INSNS (1)
3761               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3762                      + GET_CODE (SET_DEST (x)) == MEM));
3763
3764     case CONST_INT:
3765       if (outer == SET)
3766         {
3767           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3768             return 0;
3769           if (thumb_shiftable_const (INTVAL (x)))
3770             return COSTS_N_INSNS (2);
3771           return COSTS_N_INSNS (3);
3772         }
3773       else if ((outer == PLUS || outer == COMPARE)
3774                && INTVAL (x) < 256 && INTVAL (x) > -256)
3775         return 0;
3776       else if (outer == AND
3777                && INTVAL (x) < 256 && INTVAL (x) >= -256)
3778         return COSTS_N_INSNS (1);
3779       else if (outer == ASHIFT || outer == ASHIFTRT
3780                || outer == LSHIFTRT)
3781         return 0;
3782       return COSTS_N_INSNS (2);
3783
3784     case CONST:
3785     case CONST_DOUBLE:
3786     case LABEL_REF:
3787     case SYMBOL_REF:
3788       return COSTS_N_INSNS (3);
3789
3790     case UDIV:
3791     case UMOD:
3792     case DIV:
3793     case MOD:
3794       return 100;
3795
3796     case TRUNCATE:
3797       return 99;
3798
3799     case AND:
3800     case XOR:
3801     case IOR:
3802       /* XXX guess.  */
3803       return 8;
3804
3805     case MEM:
3806       /* XXX another guess.  */
3807       /* Memory costs quite a lot for the first word, but subsequent words
3808          load at the equivalent of a single insn each.  */
3809       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3810               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3811                  ? 4 : 0));
3812
3813     case IF_THEN_ELSE:
3814       /* XXX a guess.  */
3815       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3816         return 14;
3817       return 2;
3818
3819     case ZERO_EXTEND:
3820       /* XXX still guessing.  */
3821       switch (GET_MODE (XEXP (x, 0)))
3822         {
3823         case QImode:
3824           return (1 + (mode == DImode ? 4 : 0)
3825                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3826
3827         case HImode:
3828           return (4 + (mode == DImode ? 4 : 0)
3829                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3830
3831         case SImode:
3832           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3833
3834         default:
3835           return 99;
3836         }
3837
3838     default:
3839       return 99;
3840     }
3841 }
3842
3843
3844 /* Worker routine for arm_rtx_costs.  */
3845 static inline int
3846 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3847 {
3848   enum machine_mode mode = GET_MODE (x);
3849   enum rtx_code subcode;
3850   int extra_cost;
3851
3852   switch (code)
3853     {
3854     case MEM:
3855       /* Memory costs quite a lot for the first word, but subsequent words
3856          load at the equivalent of a single insn each.  */
3857       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3858               + (GET_CODE (x) == SYMBOL_REF
3859                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3860
3861     case DIV:
3862     case MOD:
3863     case UDIV:
3864     case UMOD:
3865       return optimize_size ? COSTS_N_INSNS (2) : 100;
3866
3867     case ROTATE:
3868       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3869         return 4;
3870       /* Fall through */
3871     case ROTATERT:
3872       if (mode != SImode)
3873         return 8;
3874       /* Fall through */
3875     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3876       if (mode == DImode)
3877         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3878                 + ((GET_CODE (XEXP (x, 0)) == REG
3879                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3880                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3881                    ? 0 : 8));
3882       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3883                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3884                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3885                    ? 0 : 4)
3886               + ((GET_CODE (XEXP (x, 1)) == REG
3887                   || (GET_CODE (XEXP (x, 1)) == SUBREG
3888                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3889                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3890                  ? 0 : 4));
3891
3892     case MINUS:
3893       if (mode == DImode)
3894         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3895                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3896                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
3897                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3898                    ? 0 : 8));
3899
3900       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3901         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3902                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3903                           && arm_const_double_rtx (XEXP (x, 1))))
3904                      ? 0 : 8)
3905                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3906                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3907                         && arm_const_double_rtx (XEXP (x, 0))))
3908                    ? 0 : 8));
3909
3910       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3911             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3912             && REG_OR_SUBREG_REG (XEXP (x, 1))))
3913           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3914                || subcode == ASHIFTRT || subcode == LSHIFTRT
3915                || subcode == ROTATE || subcode == ROTATERT
3916                || (subcode == MULT
3917                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3918                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3919                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3920               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3921               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3922                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3923               && REG_OR_SUBREG_REG (XEXP (x, 0))))
3924         return 1;
3925       /* Fall through */
3926
3927     case PLUS:
3928       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3929         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3930                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3931                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3932                         && arm_const_double_rtx (XEXP (x, 1))))
3933                    ? 0 : 8));
3934
3935       /* Fall through */
3936     case AND: case XOR: case IOR:
3937       extra_cost = 0;
3938
3939       /* Normally the frame registers will be spilt into reg+const during
3940          reload, so it is a bad idea to combine them with other instructions,
3941          since then they might not be moved outside of loops.  As a compromise
3942          we allow integration with ops that have a constant as their second
3943          operand.  */
3944       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3945            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3946            && GET_CODE (XEXP (x, 1)) != CONST_INT)
3947           || (REG_OR_SUBREG_REG (XEXP (x, 0))
3948               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3949         extra_cost = 4;
3950
3951       if (mode == DImode)
3952         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3953                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3954                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3955                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3956                    ? 0 : 8));
3957
3958       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3959         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3960                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3961                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3962                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3963                    ? 0 : 4));
3964
3965       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3966         return (1 + extra_cost
3967                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3968                      || subcode == LSHIFTRT || subcode == ASHIFTRT
3969                      || subcode == ROTATE || subcode == ROTATERT
3970                      || (subcode == MULT
3971                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3972                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3973                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3974                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3975                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3976                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3977                    ? 0 : 4));
3978
3979       return 8;
3980
3981     case MULT:
3982       /* This should have been handled by the CPU specific routines.  */
3983       abort ();
3984
3985     case TRUNCATE:
3986       if (arm_arch3m && mode == SImode
3987           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
3988           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
3989           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
3990               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
3991           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
3992               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
3993         return 8;
3994       return 99;
3995
3996     case NEG:
3997       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3998         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
3999       /* Fall through */
4000     case NOT:
4001       if (mode == DImode)
4002         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4003
4004       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4005
4006     case IF_THEN_ELSE:
4007       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4008         return 14;
4009       return 2;
4010
4011     case COMPARE:
4012       return 1;
4013
4014     case ABS:
4015       return 4 + (mode == DImode ? 4 : 0);
4016
4017     case SIGN_EXTEND:
4018       if (GET_MODE (XEXP (x, 0)) == QImode)
4019         return (4 + (mode == DImode ? 4 : 0)
4020                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4021       /* Fall through */
4022     case ZERO_EXTEND:
4023       switch (GET_MODE (XEXP (x, 0)))
4024         {
4025         case QImode:
4026           return (1 + (mode == DImode ? 4 : 0)
4027                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4028
4029         case HImode:
4030           return (4 + (mode == DImode ? 4 : 0)
4031                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4032
4033         case SImode:
4034           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4035
4036         case V8QImode:
4037         case V4HImode:
4038         case V2SImode:
4039         case V4QImode:
4040         case V2HImode:
4041             return 1;
4042
4043         default:
4044           break;
4045         }
4046       abort ();
4047
4048     case CONST_INT:
4049       if (const_ok_for_arm (INTVAL (x)))
4050         return outer == SET ? 2 : -1;
4051       else if (outer == AND
4052                && const_ok_for_arm (~INTVAL (x)))
4053         return -1;
4054       else if ((outer == COMPARE
4055                 || outer == PLUS || outer == MINUS)
4056                && const_ok_for_arm (-INTVAL (x)))
4057         return -1;
4058       else
4059         return 5;
4060
4061     case CONST:
4062     case LABEL_REF:
4063     case SYMBOL_REF:
4064       return 6;
4065
4066     case CONST_DOUBLE:
4067       if (arm_const_double_rtx (x))
4068         return outer == SET ? 2 : -1;
4069       else if ((outer == COMPARE || outer == PLUS)
4070                && neg_const_double_rtx_ok_for_fpa (x))
4071         return -1;
4072       return 7;
4073
4074     default:
4075       return 99;
4076     }
4077 }
4078
4079 /* RTX costs when optimizing for size.  */
4080 static bool
4081 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4082 {
4083   enum machine_mode mode = GET_MODE (x);
4084
4085   if (TARGET_THUMB)
4086     {
4087       /* XXX TBD.  For now, use the standard costs.  */
4088       *total = thumb_rtx_costs (x, code, outer_code);
4089       return true;
4090     }
4091
4092   switch (code)
4093     {
4094     case MEM:
4095       /* A memory access costs 1 insn if the mode is small, or the address is
4096          a single register, otherwise it costs one insn per word.  */
4097       if (REG_P (XEXP (x, 0)))
4098         *total = COSTS_N_INSNS (1);
4099       else
4100         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4101       return true;
4102
4103     case DIV:
4104     case MOD:
4105     case UDIV:
4106     case UMOD:
4107       /* Needs a libcall, so it costs about this.  */
4108       *total = COSTS_N_INSNS (2);
4109       return false;
4110
4111     case ROTATE:
4112       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4113         {
4114           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4115           return true;
4116         }
4117       /* Fall through */
4118     case ROTATERT:
4119     case ASHIFT:
4120     case LSHIFTRT:
4121     case ASHIFTRT:
4122       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4123         {
4124           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4125           return true;
4126         }
4127       else if (mode == SImode)
4128         {
4129           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4130           /* Slightly disparage register shifts, but not by much.  */
4131           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4132             *total += 1 + rtx_cost (XEXP (x, 1), code);
4133           return true;
4134         }
4135
4136       /* Needs a libcall.  */
4137       *total = COSTS_N_INSNS (2);
4138       return false;
4139
4140     case MINUS:
4141       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4142         {
4143           *total = COSTS_N_INSNS (1);
4144           return false;
4145         }
4146
4147       if (mode == SImode)
4148         {
4149           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4150           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4151
4152           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4153               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4154               || subcode1 == ROTATE || subcode1 == ROTATERT
4155               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4156               || subcode1 == ASHIFTRT)
4157             {
4158               /* It's just the cost of the two operands.  */
4159               *total = 0;
4160               return false;
4161             }
4162
4163           *total = COSTS_N_INSNS (1);
4164           return false;
4165         }
4166
4167       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4168       return false;
4169
4170     case PLUS:
4171       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4172         {
4173           *total = COSTS_N_INSNS (1);
4174           return false;
4175         }
4176
4177       /* Fall through */
4178     case AND: case XOR: case IOR:
4179       if (mode == SImode)
4180         {
4181           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4182
4183           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4184               || subcode == LSHIFTRT || subcode == ASHIFTRT
4185               || (code == AND && subcode == NOT))
4186             {
4187               /* It's just the cost of the two operands.  */
4188               *total = 0;
4189               return false;
4190             }
4191         }
4192
4193       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4194       return false;
4195
4196     case MULT:
4197       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4198       return false;
4199
4200     case NEG:
4201       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4202         *total = COSTS_N_INSNS (1);
4203       /* Fall through */
4204     case NOT:
4205       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4206
4207       return false;
4208
4209     case IF_THEN_ELSE:
4210       *total = 0;
4211       return false;
4212
4213     case COMPARE:
4214       if (cc_register (XEXP (x, 0), VOIDmode))
4215         * total = 0;
4216       else
4217         *total = COSTS_N_INSNS (1);
4218       return false;
4219
4220     case ABS:
4221       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4222         *total = COSTS_N_INSNS (1);
4223       else
4224         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4225       return false;
4226
4227     case SIGN_EXTEND:
4228       *total = 0;
4229       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4230         {
4231           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4232             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4233         }
4234       if (mode == DImode)
4235         *total += COSTS_N_INSNS (1);
4236       return false;
4237
4238     case ZERO_EXTEND:
4239       *total = 0;
4240       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4241         {
4242           switch (GET_MODE (XEXP (x, 0)))
4243             {
4244             case QImode:
4245               *total += COSTS_N_INSNS (1);
4246               break;
4247
4248             case HImode:
4249               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4250
4251             case SImode:
4252               break;
4253
4254             default:
4255               *total += COSTS_N_INSNS (2);
4256             }
4257         }
4258
4259       if (mode == DImode)
4260         *total += COSTS_N_INSNS (1);
4261
4262       return false;
4263
4264     case CONST_INT:
4265       if (const_ok_for_arm (INTVAL (x)))
4266         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4267       else if (const_ok_for_arm (~INTVAL (x)))
4268         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4269       else if (const_ok_for_arm (-INTVAL (x)))
4270         {
4271           if (outer_code == COMPARE || outer_code == PLUS
4272               || outer_code == MINUS)
4273             *total = 0;
4274           else
4275             *total = COSTS_N_INSNS (1);
4276         }
4277       else
4278         *total = COSTS_N_INSNS (2);
4279       return true;
4280
4281     case CONST:
4282     case LABEL_REF:
4283     case SYMBOL_REF:
4284       *total = COSTS_N_INSNS (2);
4285       return true;
4286
4287     case CONST_DOUBLE:
4288       *total = COSTS_N_INSNS (4);
4289       return true;
4290
4291     default:
4292       if (mode != VOIDmode)
4293         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4294       else
4295         *total = COSTS_N_INSNS (4); /* How knows?  */
4296       return false;
4297     }
4298 }
4299
4300 /* RTX costs for cores with a slow MUL implementation.  */
4301
4302 static bool
4303 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4304 {
4305   enum machine_mode mode = GET_MODE (x);
4306
4307   if (TARGET_THUMB)
4308     {
4309       *total = thumb_rtx_costs (x, code, outer_code);
4310       return true;
4311     }
4312
4313   switch (code)
4314     {
4315     case MULT:
4316       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4317           || mode == DImode)
4318         {
4319           *total = 30;
4320           return true;
4321         }
4322
4323       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4324         {
4325           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4326                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4327           int cost, const_ok = const_ok_for_arm (i);
4328           int j, booth_unit_size;
4329
4330           /* Tune as appropriate.  */
4331           cost = const_ok ? 4 : 8;
4332           booth_unit_size = 2;
4333           for (j = 0; i && j < 32; j += booth_unit_size)
4334             {
4335               i >>= booth_unit_size;
4336               cost += 2;
4337             }
4338
4339           *total = cost;
4340           return true;
4341         }
4342
4343       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4344                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4345       return true;
4346
4347     default:
4348       *total = arm_rtx_costs_1 (x, code, outer_code);
4349       return true;
4350     }
4351 }
4352
4353
4354 /* RTX cost for cores with a fast multiply unit (M variants).  */
4355
4356 static bool
4357 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4358 {
4359   enum machine_mode mode = GET_MODE (x);
4360
4361   if (TARGET_THUMB)
4362     {
4363       *total = thumb_rtx_costs (x, code, outer_code);
4364       return true;
4365     }
4366
4367   switch (code)
4368     {
4369     case MULT:
4370       /* There is no point basing this on the tuning, since it is always the
4371          fast variant if it exists at all.  */
4372       if (mode == DImode
4373           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4374           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4375               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4376         {
4377           *total = 8;
4378           return true;
4379         }
4380
4381
4382       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4383           || mode == DImode)
4384         {
4385           *total = 30;
4386           return true;
4387         }
4388
4389       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4390         {
4391           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4392                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4393           int cost, const_ok = const_ok_for_arm (i);
4394           int j, booth_unit_size;
4395
4396           /* Tune as appropriate.  */
4397           cost = const_ok ? 4 : 8;
4398           booth_unit_size = 8;
4399           for (j = 0; i && j < 32; j += booth_unit_size)
4400             {
4401               i >>= booth_unit_size;
4402               cost += 2;
4403             }
4404
4405           *total = cost;
4406           return true;
4407         }
4408
4409       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4410                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4411       return true;
4412
4413     default:
4414       *total = arm_rtx_costs_1 (x, code, outer_code);
4415       return true;
4416     }
4417 }
4418
4419
4420 /* RTX cost for XScale CPUs.  */
4421
4422 static bool
4423 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4424 {
4425   enum machine_mode mode = GET_MODE (x);
4426
4427   if (TARGET_THUMB)
4428     {
4429       *total = thumb_rtx_costs (x, code, outer_code);
4430       return true;
4431     }
4432
4433   switch (code)
4434     {
4435     case MULT:
4436       /* There is no point basing this on the tuning, since it is always the
4437          fast variant if it exists at all.  */
4438       if (mode == DImode
4439           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4440           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4441               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4442         {
4443           *total = 8;
4444           return true;
4445         }
4446
4447
4448       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4449           || mode == DImode)
4450         {
4451           *total = 30;
4452           return true;
4453         }
4454
4455       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4456         {
4457           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4458                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4459           int cost, const_ok = const_ok_for_arm (i);
4460           unsigned HOST_WIDE_INT masked_const;
4461
4462           /* The cost will be related to two insns.
4463              First a load of the constant (MOV or LDR), then a multiply.  */
4464           cost = 2;
4465           if (! const_ok)
4466             cost += 1;      /* LDR is probably more expensive because
4467                                of longer result latency.  */
4468           masked_const = i & 0xffff8000;
4469           if (masked_const != 0 && masked_const != 0xffff8000)
4470             {
4471               masked_const = i & 0xf8000000;
4472               if (masked_const == 0 || masked_const == 0xf8000000)
4473                 cost += 1;
4474               else
4475                 cost += 2;
4476             }
4477           *total = cost;
4478           return true;
4479         }
4480
4481       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4482                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4483       return true;
4484
4485     case COMPARE:
4486       /* A COMPARE of a MULT is slow on XScale; the muls instruction
4487          will stall until the multiplication is complete.  */
4488       if (GET_CODE (XEXP (x, 0)) == MULT)
4489         *total = 4 + rtx_cost (XEXP (x, 0), code);
4490       else
4491         *total = arm_rtx_costs_1 (x, code, outer_code);
4492       return true;
4493
4494     default:
4495       *total = arm_rtx_costs_1 (x, code, outer_code);
4496       return true;
4497     }
4498 }
4499
4500
4501 /* RTX costs for 9e (and later) cores.  */
4502
4503 static bool
4504 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4505 {
4506   enum machine_mode mode = GET_MODE (x);
4507   int nonreg_cost;
4508   int cost;
4509
4510   if (TARGET_THUMB)
4511     {
4512       switch (code)
4513         {
4514         case MULT:
4515           *total = COSTS_N_INSNS (3);
4516           return true;
4517
4518         default:
4519           *total = thumb_rtx_costs (x, code, outer_code);
4520           return true;
4521         }
4522     }
4523
4524   switch (code)
4525     {
4526     case MULT:
4527       /* There is no point basing this on the tuning, since it is always the
4528          fast variant if it exists at all.  */
4529       if (mode == DImode
4530           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4531           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4532               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4533         {
4534           *total = 3;
4535           return true;
4536         }
4537
4538
4539       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4540         {
4541           *total = 30;
4542           return true;
4543         }
4544       if (mode == DImode)
4545         {
4546           cost = 7;
4547           nonreg_cost = 8;
4548         }
4549       else
4550         {
4551           cost = 2;
4552           nonreg_cost = 4;
4553         }
4554
4555
4556       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4557                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4558       return true;
4559
4560     default:
4561       *total = arm_rtx_costs_1 (x, code, outer_code);
4562       return true;
4563     }
4564 }
4565 /* All address computations that can be done are free, but rtx cost returns
4566    the same for practically all of them.  So we weight the different types
4567    of address here in the order (most pref first):
4568    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
4569 static inline int
4570 arm_arm_address_cost (rtx x)
4571 {
4572   enum rtx_code c  = GET_CODE (x);
4573
4574   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4575     return 0;
4576   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4577     return 10;
4578
4579   if (c == PLUS || c == MINUS)
4580     {
4581       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4582         return 2;
4583
4584       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4585         return 3;
4586
4587       return 4;
4588     }
4589
4590   return 6;
4591 }
4592
4593 static inline int
4594 arm_thumb_address_cost (rtx x)
4595 {
4596   enum rtx_code c  = GET_CODE (x);
4597
4598   if (c == REG)
4599     return 1;
4600   if (c == PLUS
4601       && GET_CODE (XEXP (x, 0)) == REG
4602       && GET_CODE (XEXP (x, 1)) == CONST_INT)
4603     return 1;
4604
4605   return 2;
4606 }
4607
4608 static int
4609 arm_address_cost (rtx x)
4610 {
4611   return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4612 }
4613
4614 static int
4615 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4616 {
4617   rtx i_pat, d_pat;
4618
4619   /* Some true dependencies can have a higher cost depending
4620      on precisely how certain input operands are used.  */
4621   if (arm_tune_xscale
4622       && REG_NOTE_KIND (link) == 0
4623       && recog_memoized (insn) >= 0
4624       && recog_memoized (dep) >= 0)
4625     {
4626       int shift_opnum = get_attr_shift (insn);
4627       enum attr_type attr_type = get_attr_type (dep);
4628
4629       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4630          operand for INSN.  If we have a shifted input operand and the
4631          instruction we depend on is another ALU instruction, then we may
4632          have to account for an additional stall.  */
4633       if (shift_opnum != 0
4634           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4635         {
4636           rtx shifted_operand;
4637           int opno;
4638
4639           /* Get the shifted operand.  */
4640           extract_insn (insn);
4641           shifted_operand = recog_data.operand[shift_opnum];
4642
4643           /* Iterate over all the operands in DEP.  If we write an operand
4644              that overlaps with SHIFTED_OPERAND, then we have increase the
4645              cost of this dependency.  */
4646           extract_insn (dep);
4647           preprocess_constraints ();
4648           for (opno = 0; opno < recog_data.n_operands; opno++)
4649             {
4650               /* We can ignore strict inputs.  */
4651               if (recog_data.operand_type[opno] == OP_IN)
4652                 continue;
4653
4654               if (reg_overlap_mentioned_p (recog_data.operand[opno],
4655                                            shifted_operand))
4656                 return 2;
4657             }
4658         }
4659     }
4660
4661   /* XXX This is not strictly true for the FPA.  */
4662   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4663       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4664     return 0;
4665
4666   /* Call insns don't incur a stall, even if they follow a load.  */
4667   if (REG_NOTE_KIND (link) == 0
4668       && GET_CODE (insn) == CALL_INSN)
4669     return 1;
4670
4671   if ((i_pat = single_set (insn)) != NULL
4672       && GET_CODE (SET_SRC (i_pat)) == MEM
4673       && (d_pat = single_set (dep)) != NULL
4674       && GET_CODE (SET_DEST (d_pat)) == MEM)
4675     {
4676       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4677       /* This is a load after a store, there is no conflict if the load reads
4678          from a cached area.  Assume that loads from the stack, and from the
4679          constant pool are cached, and that others will miss.  This is a
4680          hack.  */
4681
4682       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4683           || reg_mentioned_p (stack_pointer_rtx, src_mem)
4684           || reg_mentioned_p (frame_pointer_rtx, src_mem)
4685           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4686         return 1;
4687     }
4688
4689   return cost;
4690 }
4691
4692 static int fp_consts_inited = 0;
4693
4694 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
4695 static const char * const strings_fp[8] =
4696 {
4697   "0",   "1",   "2",   "3",
4698   "4",   "5",   "0.5", "10"
4699 };
4700
4701 static REAL_VALUE_TYPE values_fp[8];
4702
4703 static void
4704 init_fp_table (void)
4705 {
4706   int i;
4707   REAL_VALUE_TYPE r;
4708
4709   if (TARGET_VFP)
4710     fp_consts_inited = 1;
4711   else
4712     fp_consts_inited = 8;
4713
4714   for (i = 0; i < fp_consts_inited; i++)
4715     {
4716       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4717       values_fp[i] = r;
4718     }
4719 }
4720
4721 /* Return TRUE if rtx X is a valid immediate FP constant.  */
4722 int
4723 arm_const_double_rtx (rtx x)
4724 {
4725   REAL_VALUE_TYPE r;
4726   int i;
4727
4728   if (!fp_consts_inited)
4729     init_fp_table ();
4730
4731   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4732   if (REAL_VALUE_MINUS_ZERO (r))
4733     return 0;
4734
4735   for (i = 0; i < fp_consts_inited; i++)
4736     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4737       return 1;
4738
4739   return 0;
4740 }
4741
4742 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
4743 int
4744 neg_const_double_rtx_ok_for_fpa (rtx x)
4745 {
4746   REAL_VALUE_TYPE r;
4747   int i;
4748
4749   if (!fp_consts_inited)
4750     init_fp_table ();
4751
4752   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4753   r = REAL_VALUE_NEGATE (r);
4754   if (REAL_VALUE_MINUS_ZERO (r))
4755     return 0;
4756
4757   for (i = 0; i < 8; i++)
4758     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4759       return 1;
4760
4761   return 0;
4762 }
4763 \f
4764 /* Predicates for `match_operand' and `match_operator'.  */
4765
4766 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
4767 int
4768 cirrus_memory_offset (rtx op)
4769 {
4770   /* Reject eliminable registers.  */
4771   if (! (reload_in_progress || reload_completed)
4772       && (   reg_mentioned_p (frame_pointer_rtx, op)
4773           || reg_mentioned_p (arg_pointer_rtx, op)
4774           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4775           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4776           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4777           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4778     return 0;
4779
4780   if (GET_CODE (op) == MEM)
4781     {
4782       rtx ind;
4783
4784       ind = XEXP (op, 0);
4785
4786       /* Match: (mem (reg)).  */
4787       if (GET_CODE (ind) == REG)
4788         return 1;
4789
4790       /* Match:
4791          (mem (plus (reg)
4792                     (const))).  */
4793       if (GET_CODE (ind) == PLUS
4794           && GET_CODE (XEXP (ind, 0)) == REG
4795           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4796           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4797         return 1;
4798     }
4799
4800   return 0;
4801 }
4802
4803 /* Return TRUE if OP is a valid VFP memory address pattern.
4804    WB if true if writeback address modes are allowed.  */
4805
4806 int
4807 arm_coproc_mem_operand (rtx op, bool wb)
4808 {
4809   rtx ind;
4810
4811   /* Reject eliminable registers.  */
4812   if (! (reload_in_progress || reload_completed)
4813       && (   reg_mentioned_p (frame_pointer_rtx, op)
4814           || reg_mentioned_p (arg_pointer_rtx, op)
4815           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4816           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4817           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4818           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4819     return FALSE;
4820
4821   /* Constants are converted into offsets from labels.  */
4822   if (GET_CODE (op) != MEM)
4823     return FALSE;
4824
4825   ind = XEXP (op, 0);
4826
4827   if (reload_completed
4828       && (GET_CODE (ind) == LABEL_REF
4829           || (GET_CODE (ind) == CONST
4830               && GET_CODE (XEXP (ind, 0)) == PLUS
4831               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4832               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4833     return TRUE;
4834
4835   /* Match: (mem (reg)).  */
4836   if (GET_CODE (ind) == REG)
4837     return arm_address_register_rtx_p (ind, 0);
4838
4839   /* Autoincremment addressing modes.  */
4840   if (wb
4841       && (GET_CODE (ind) == PRE_INC
4842           || GET_CODE (ind) == POST_INC
4843           || GET_CODE (ind) == PRE_DEC
4844           || GET_CODE (ind) == POST_DEC))
4845     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
4846
4847   if (wb
4848       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
4849       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
4850       && GET_CODE (XEXP (ind, 1)) == PLUS
4851       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
4852     ind = XEXP (ind, 1);
4853
4854   /* Match:
4855      (plus (reg)
4856            (const)).  */
4857   if (GET_CODE (ind) == PLUS
4858       && GET_CODE (XEXP (ind, 0)) == REG
4859       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4860       && GET_CODE (XEXP (ind, 1)) == CONST_INT
4861       && INTVAL (XEXP (ind, 1)) > -1024
4862       && INTVAL (XEXP (ind, 1)) <  1024
4863       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
4864     return TRUE;
4865
4866   return FALSE;
4867 }
4868
4869 /* Return true if X is a register that will be eliminated later on.  */
4870 int
4871 arm_eliminable_register (rtx x)
4872 {
4873   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
4874                        || REGNO (x) == ARG_POINTER_REGNUM
4875                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
4876                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
4877 }
4878
4879 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4880    VFP registers.  Otherwise return NO_REGS.  */
4881
4882 enum reg_class
4883 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4884 {
4885   if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
4886     return NO_REGS;
4887
4888   return GENERAL_REGS;
4889 }
4890
4891
4892 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4893    Use by the Cirrus Maverick code which has to workaround
4894    a hardware bug triggered by such instructions.  */
4895 static bool
4896 arm_memory_load_p (rtx insn)
4897 {
4898   rtx body, lhs, rhs;;
4899
4900   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4901     return false;
4902
4903   body = PATTERN (insn);
4904
4905   if (GET_CODE (body) != SET)
4906     return false;
4907
4908   lhs = XEXP (body, 0);
4909   rhs = XEXP (body, 1);
4910
4911   lhs = REG_OR_SUBREG_RTX (lhs);
4912
4913   /* If the destination is not a general purpose
4914      register we do not have to worry.  */
4915   if (GET_CODE (lhs) != REG
4916       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4917     return false;
4918
4919   /* As well as loads from memory we also have to react
4920      to loads of invalid constants which will be turned
4921      into loads from the minipool.  */
4922   return (GET_CODE (rhs) == MEM
4923           || GET_CODE (rhs) == SYMBOL_REF
4924           || note_invalid_constants (insn, -1, false));
4925 }
4926
4927 /* Return TRUE if INSN is a Cirrus instruction.  */
4928 static bool
4929 arm_cirrus_insn_p (rtx insn)
4930 {
4931   enum attr_cirrus attr;
4932
4933   /* get_attr aborts on USE and CLOBBER.  */
4934   if (!insn
4935       || GET_CODE (insn) != INSN
4936       || GET_CODE (PATTERN (insn)) == USE
4937       || GET_CODE (PATTERN (insn)) == CLOBBER)
4938     return 0;
4939
4940   attr = get_attr_cirrus (insn);
4941
4942   return attr != CIRRUS_NOT;
4943 }
4944
4945 /* Cirrus reorg for invalid instruction combinations.  */
4946 static void
4947 cirrus_reorg (rtx first)
4948 {
4949   enum attr_cirrus attr;
4950   rtx body = PATTERN (first);
4951   rtx t;
4952   int nops;
4953
4954   /* Any branch must be followed by 2 non Cirrus instructions.  */
4955   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4956     {
4957       nops = 0;
4958       t = next_nonnote_insn (first);
4959
4960       if (arm_cirrus_insn_p (t))
4961         ++ nops;
4962
4963       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4964         ++ nops;
4965
4966       while (nops --)
4967         emit_insn_after (gen_nop (), first);
4968
4969       return;
4970     }
4971
4972   /* (float (blah)) is in parallel with a clobber.  */
4973   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4974     body = XVECEXP (body, 0, 0);
4975
4976   if (GET_CODE (body) == SET)
4977     {
4978       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4979
4980       /* cfldrd, cfldr64, cfstrd, cfstr64 must
4981          be followed by a non Cirrus insn.  */
4982       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
4983         {
4984           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
4985             emit_insn_after (gen_nop (), first);
4986
4987           return;
4988         }
4989       else if (arm_memory_load_p (first))
4990         {
4991           unsigned int arm_regno;
4992
4993           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
4994              ldr/cfmv64hr combination where the Rd field is the same
4995              in both instructions must be split with a non Cirrus
4996              insn.  Example:
4997
4998              ldr r0, blah
4999              nop
5000              cfmvsr mvf0, r0.  */
5001
5002           /* Get Arm register number for ldr insn.  */
5003           if (GET_CODE (lhs) == REG)
5004             arm_regno = REGNO (lhs);
5005           else if (GET_CODE (rhs) == REG)
5006             arm_regno = REGNO (rhs);
5007           else
5008             abort ();
5009
5010           /* Next insn.  */
5011           first = next_nonnote_insn (first);
5012
5013           if (! arm_cirrus_insn_p (first))
5014             return;
5015
5016           body = PATTERN (first);
5017
5018           /* (float (blah)) is in parallel with a clobber.  */
5019           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5020             body = XVECEXP (body, 0, 0);
5021
5022           if (GET_CODE (body) == FLOAT)
5023             body = XEXP (body, 0);
5024
5025           if (get_attr_cirrus (first) == CIRRUS_MOVE
5026               && GET_CODE (XEXP (body, 1)) == REG
5027               && arm_regno == REGNO (XEXP (body, 1)))
5028             emit_insn_after (gen_nop (), first);
5029
5030           return;
5031         }
5032     }
5033
5034   /* get_attr aborts on USE and CLOBBER.  */
5035   if (!first
5036       || GET_CODE (first) != INSN
5037       || GET_CODE (PATTERN (first)) == USE
5038       || GET_CODE (PATTERN (first)) == CLOBBER)
5039     return;
5040
5041   attr = get_attr_cirrus (first);
5042
5043   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5044      must be followed by a non-coprocessor instruction.  */
5045   if (attr == CIRRUS_COMPARE)
5046     {
5047       nops = 0;
5048
5049       t = next_nonnote_insn (first);
5050
5051       if (arm_cirrus_insn_p (t))
5052         ++ nops;
5053
5054       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5055         ++ nops;
5056
5057       while (nops --)
5058         emit_insn_after (gen_nop (), first);
5059
5060       return;
5061     }
5062 }
5063
5064 /* Return TRUE if X references a SYMBOL_REF.  */
5065 int
5066 symbol_mentioned_p (rtx x)
5067 {
5068   const char * fmt;
5069   int i;
5070
5071   if (GET_CODE (x) == SYMBOL_REF)
5072     return 1;
5073
5074   fmt = GET_RTX_FORMAT (GET_CODE (x));
5075
5076   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5077     {
5078       if (fmt[i] == 'E')
5079         {
5080           int j;
5081
5082           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5083             if (symbol_mentioned_p (XVECEXP (x, i, j)))
5084               return 1;
5085         }
5086       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5087         return 1;
5088     }
5089
5090   return 0;
5091 }
5092
5093 /* Return TRUE if X references a LABEL_REF.  */
5094 int
5095 label_mentioned_p (rtx x)
5096 {
5097   const char * fmt;
5098   int i;
5099
5100   if (GET_CODE (x) == LABEL_REF)
5101     return 1;
5102
5103   fmt = GET_RTX_FORMAT (GET_CODE (x));
5104   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5105     {
5106       if (fmt[i] == 'E')
5107         {
5108           int j;
5109
5110           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5111             if (label_mentioned_p (XVECEXP (x, i, j)))
5112               return 1;
5113         }
5114       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5115         return 1;
5116     }
5117
5118   return 0;
5119 }
5120
5121 enum rtx_code
5122 minmax_code (rtx x)
5123 {
5124   enum rtx_code code = GET_CODE (x);
5125
5126   if (code == SMAX)
5127     return GE;
5128   else if (code == SMIN)
5129     return LE;
5130   else if (code == UMIN)
5131     return LEU;
5132   else if (code == UMAX)
5133     return GEU;
5134
5135   abort ();
5136 }
5137
5138 /* Return 1 if memory locations are adjacent.  */
5139 int
5140 adjacent_mem_locations (rtx a, rtx b)
5141 {
5142   if ((GET_CODE (XEXP (a, 0)) == REG
5143        || (GET_CODE (XEXP (a, 0)) == PLUS
5144            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5145       && (GET_CODE (XEXP (b, 0)) == REG
5146           || (GET_CODE (XEXP (b, 0)) == PLUS
5147               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5148     {
5149       HOST_WIDE_INT val0 = 0, val1 = 0;
5150       rtx reg0, reg1;
5151       int val_diff;
5152
5153       if (GET_CODE (XEXP (a, 0)) == PLUS)
5154         {
5155           reg0 = XEXP (XEXP (a, 0), 0);
5156           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5157         }
5158       else
5159         reg0 = XEXP (a, 0);
5160
5161       if (GET_CODE (XEXP (b, 0)) == PLUS)
5162         {
5163           reg1 = XEXP (XEXP (b, 0), 0);
5164           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5165         }
5166       else
5167         reg1 = XEXP (b, 0);
5168
5169       /* Don't accept any offset that will require multiple
5170          instructions to handle, since this would cause the
5171          arith_adjacentmem pattern to output an overlong sequence.  */
5172       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5173         return 0;
5174
5175       /* Don't allow an eliminable register: register elimination can make
5176          the offset too large.  */
5177       if (arm_eliminable_register (reg0))
5178         return 0;
5179
5180       val_diff = val1 - val0;
5181       return ((REGNO (reg0) == REGNO (reg1))
5182               && (val_diff == 4 || val_diff == -4));
5183     }
5184
5185   return 0;
5186 }
5187
5188 int
5189 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5190                         HOST_WIDE_INT *load_offset)
5191 {
5192   int unsorted_regs[4];
5193   HOST_WIDE_INT unsorted_offsets[4];
5194   int order[4];
5195   int base_reg = -1;
5196   int i;
5197
5198   /* Can only handle 2, 3, or 4 insns at present,
5199      though could be easily extended if required.  */
5200   if (nops < 2 || nops > 4)
5201     abort ();
5202
5203   /* Loop over the operands and check that the memory references are
5204      suitable (i.e. immediate offsets from the same base register).  At
5205      the same time, extract the target register, and the memory
5206      offsets.  */
5207   for (i = 0; i < nops; i++)
5208     {
5209       rtx reg;
5210       rtx offset;
5211
5212       /* Convert a subreg of a mem into the mem itself.  */
5213       if (GET_CODE (operands[nops + i]) == SUBREG)
5214         operands[nops + i] = alter_subreg (operands + (nops + i));
5215
5216       if (GET_CODE (operands[nops + i]) != MEM)
5217         abort ();
5218
5219       /* Don't reorder volatile memory references; it doesn't seem worth
5220          looking for the case where the order is ok anyway.  */
5221       if (MEM_VOLATILE_P (operands[nops + i]))
5222         return 0;
5223
5224       offset = const0_rtx;
5225
5226       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5227            || (GET_CODE (reg) == SUBREG
5228                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5229           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5230               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5231                    == REG)
5232                   || (GET_CODE (reg) == SUBREG
5233                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5234               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5235                   == CONST_INT)))
5236         {
5237           if (i == 0)
5238             {
5239               base_reg = REGNO (reg);
5240               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5241                                   ? REGNO (operands[i])
5242                                   : REGNO (SUBREG_REG (operands[i])));
5243               order[0] = 0;
5244             }
5245           else
5246             {
5247               if (base_reg != (int) REGNO (reg))
5248                 /* Not addressed from the same base register.  */
5249                 return 0;
5250
5251               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5252                                   ? REGNO (operands[i])
5253                                   : REGNO (SUBREG_REG (operands[i])));
5254               if (unsorted_regs[i] < unsorted_regs[order[0]])
5255                 order[0] = i;
5256             }
5257
5258           /* If it isn't an integer register, or if it overwrites the
5259              base register but isn't the last insn in the list, then
5260              we can't do this.  */
5261           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5262               || (i != nops - 1 && unsorted_regs[i] == base_reg))
5263             return 0;
5264
5265           unsorted_offsets[i] = INTVAL (offset);
5266         }
5267       else
5268         /* Not a suitable memory address.  */
5269         return 0;
5270     }
5271
5272   /* All the useful information has now been extracted from the
5273      operands into unsorted_regs and unsorted_offsets; additionally,
5274      order[0] has been set to the lowest numbered register in the
5275      list.  Sort the registers into order, and check that the memory
5276      offsets are ascending and adjacent.  */
5277
5278   for (i = 1; i < nops; i++)
5279     {
5280       int j;
5281
5282       order[i] = order[i - 1];
5283       for (j = 0; j < nops; j++)
5284         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5285             && (order[i] == order[i - 1]
5286                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5287           order[i] = j;
5288
5289       /* Have we found a suitable register? if not, one must be used more
5290          than once.  */
5291       if (order[i] == order[i - 1])
5292         return 0;
5293
5294       /* Is the memory address adjacent and ascending? */
5295       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5296         return 0;
5297     }
5298
5299   if (base)
5300     {
5301       *base = base_reg;
5302
5303       for (i = 0; i < nops; i++)
5304         regs[i] = unsorted_regs[order[i]];
5305
5306       *load_offset = unsorted_offsets[order[0]];
5307     }
5308
5309   if (unsorted_offsets[order[0]] == 0)
5310     return 1; /* ldmia */
5311
5312   if (unsorted_offsets[order[0]] == 4)
5313     return 2; /* ldmib */
5314
5315   if (unsorted_offsets[order[nops - 1]] == 0)
5316     return 3; /* ldmda */
5317
5318   if (unsorted_offsets[order[nops - 1]] == -4)
5319     return 4; /* ldmdb */
5320
5321   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5322      if the offset isn't small enough.  The reason 2 ldrs are faster
5323      is because these ARMs are able to do more than one cache access
5324      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
5325      whilst the ARM8 has a double bandwidth cache.  This means that
5326      these cores can do both an instruction fetch and a data fetch in
5327      a single cycle, so the trick of calculating the address into a
5328      scratch register (one of the result regs) and then doing a load
5329      multiple actually becomes slower (and no smaller in code size).
5330      That is the transformation
5331
5332         ldr     rd1, [rbase + offset]
5333         ldr     rd2, [rbase + offset + 4]
5334
5335      to
5336
5337         add     rd1, rbase, offset
5338         ldmia   rd1, {rd1, rd2}
5339
5340      produces worse code -- '3 cycles + any stalls on rd2' instead of
5341      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
5342      access per cycle, the first sequence could never complete in less
5343      than 6 cycles, whereas the ldm sequence would only take 5 and
5344      would make better use of sequential accesses if not hitting the
5345      cache.
5346
5347      We cheat here and test 'arm_ld_sched' which we currently know to
5348      only be true for the ARM8, ARM9 and StrongARM.  If this ever
5349      changes, then the test below needs to be reworked.  */
5350   if (nops == 2 && arm_ld_sched)
5351     return 0;
5352
5353   /* Can't do it without setting up the offset, only do this if it takes
5354      no more than one insn.  */
5355   return (const_ok_for_arm (unsorted_offsets[order[0]])
5356           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5357 }
5358
5359 const char *
5360 emit_ldm_seq (rtx *operands, int nops)
5361 {
5362   int regs[4];
5363   int base_reg;
5364   HOST_WIDE_INT offset;
5365   char buf[100];
5366   int i;
5367
5368   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5369     {
5370     case 1:
5371       strcpy (buf, "ldm%?ia\t");
5372       break;
5373
5374     case 2:
5375       strcpy (buf, "ldm%?ib\t");
5376       break;
5377
5378     case 3:
5379       strcpy (buf, "ldm%?da\t");
5380       break;
5381
5382     case 4:
5383       strcpy (buf, "ldm%?db\t");
5384       break;
5385
5386     case 5:
5387       if (offset >= 0)
5388         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5389                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5390                  (long) offset);
5391       else
5392         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5393                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5394                  (long) -offset);
5395       output_asm_insn (buf, operands);
5396       base_reg = regs[0];
5397       strcpy (buf, "ldm%?ia\t");
5398       break;
5399
5400     default:
5401       abort ();
5402     }
5403
5404   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5405            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5406
5407   for (i = 1; i < nops; i++)
5408     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5409              reg_names[regs[i]]);
5410
5411   strcat (buf, "}\t%@ phole ldm");
5412
5413   output_asm_insn (buf, operands);
5414   return "";
5415 }
5416
5417 int
5418 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5419                          HOST_WIDE_INT * load_offset)
5420 {
5421   int unsorted_regs[4];
5422   HOST_WIDE_INT unsorted_offsets[4];
5423   int order[4];
5424   int base_reg = -1;
5425   int i;
5426
5427   /* Can only handle 2, 3, or 4 insns at present, though could be easily
5428      extended if required.  */
5429   if (nops < 2 || nops > 4)
5430     abort ();
5431
5432   /* Loop over the operands and check that the memory references are
5433      suitable (i.e. immediate offsets from the same base register).  At
5434      the same time, extract the target register, and the memory
5435      offsets.  */
5436   for (i = 0; i < nops; i++)
5437     {
5438       rtx reg;
5439       rtx offset;
5440
5441       /* Convert a subreg of a mem into the mem itself.  */
5442       if (GET_CODE (operands[nops + i]) == SUBREG)
5443         operands[nops + i] = alter_subreg (operands + (nops + i));
5444
5445       if (GET_CODE (operands[nops + i]) != MEM)
5446         abort ();
5447
5448       /* Don't reorder volatile memory references; it doesn't seem worth
5449          looking for the case where the order is ok anyway.  */
5450       if (MEM_VOLATILE_P (operands[nops + i]))
5451         return 0;
5452
5453       offset = const0_rtx;
5454
5455       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5456            || (GET_CODE (reg) == SUBREG
5457                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5458           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5459               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5460                    == REG)
5461                   || (GET_CODE (reg) == SUBREG
5462                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5463               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5464                   == CONST_INT)))
5465         {
5466           if (i == 0)
5467             {
5468               base_reg = REGNO (reg);
5469               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5470                                   ? REGNO (operands[i])
5471                                   : REGNO (SUBREG_REG (operands[i])));
5472               order[0] = 0;
5473             }
5474           else
5475             {
5476               if (base_reg != (int) REGNO (reg))
5477                 /* Not addressed from the same base register.  */
5478                 return 0;
5479
5480               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5481                                   ? REGNO (operands[i])
5482                                   : REGNO (SUBREG_REG (operands[i])));
5483               if (unsorted_regs[i] < unsorted_regs[order[0]])
5484                 order[0] = i;
5485             }
5486
5487           /* If it isn't an integer register, then we can't do this.  */
5488           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5489             return 0;
5490
5491           unsorted_offsets[i] = INTVAL (offset);
5492         }
5493       else
5494         /* Not a suitable memory address.  */
5495         return 0;
5496     }
5497
5498   /* All the useful information has now been extracted from the
5499      operands into unsorted_regs and unsorted_offsets; additionally,
5500      order[0] has been set to the lowest numbered register in the
5501      list.  Sort the registers into order, and check that the memory
5502      offsets are ascending and adjacent.  */
5503
5504   for (i = 1; i < nops; i++)
5505     {
5506       int j;
5507
5508       order[i] = order[i - 1];
5509       for (j = 0; j < nops; j++)
5510         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5511             && (order[i] == order[i - 1]
5512                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5513           order[i] = j;
5514
5515       /* Have we found a suitable register? if not, one must be used more
5516          than once.  */
5517       if (order[i] == order[i - 1])
5518         return 0;
5519
5520       /* Is the memory address adjacent and ascending? */
5521       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5522         return 0;
5523     }
5524
5525   if (base)
5526     {
5527       *base = base_reg;
5528
5529       for (i = 0; i < nops; i++)
5530         regs[i] = unsorted_regs[order[i]];
5531
5532       *load_offset = unsorted_offsets[order[0]];
5533     }
5534
5535   if (unsorted_offsets[order[0]] == 0)
5536     return 1; /* stmia */
5537
5538   if (unsorted_offsets[order[0]] == 4)
5539     return 2; /* stmib */
5540
5541   if (unsorted_offsets[order[nops - 1]] == 0)
5542     return 3; /* stmda */
5543
5544   if (unsorted_offsets[order[nops - 1]] == -4)
5545     return 4; /* stmdb */
5546
5547   return 0;
5548 }
5549
5550 const char *
5551 emit_stm_seq (rtx *operands, int nops)
5552 {
5553   int regs[4];
5554   int base_reg;
5555   HOST_WIDE_INT offset;
5556   char buf[100];
5557   int i;
5558
5559   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5560     {
5561     case 1:
5562       strcpy (buf, "stm%?ia\t");
5563       break;
5564
5565     case 2:
5566       strcpy (buf, "stm%?ib\t");
5567       break;
5568
5569     case 3:
5570       strcpy (buf, "stm%?da\t");
5571       break;
5572
5573     case 4:
5574       strcpy (buf, "stm%?db\t");
5575       break;
5576
5577     default:
5578       abort ();
5579     }
5580
5581   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5582            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5583
5584   for (i = 1; i < nops; i++)
5585     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5586              reg_names[regs[i]]);
5587
5588   strcat (buf, "}\t%@ phole stm");
5589
5590   output_asm_insn (buf, operands);
5591   return "";
5592 }
5593
5594 \f
5595 /* Routines for use in generating RTL.  */
5596
5597 rtx
5598 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5599                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5600 {
5601   HOST_WIDE_INT offset = *offsetp;
5602   int i = 0, j;
5603   rtx result;
5604   int sign = up ? 1 : -1;
5605   rtx mem, addr;
5606
5607   /* XScale has load-store double instructions, but they have stricter
5608      alignment requirements than load-store multiple, so we cannot
5609      use them.
5610
5611      For XScale ldm requires 2 + NREGS cycles to complete and blocks
5612      the pipeline until completion.
5613
5614         NREGS           CYCLES
5615           1               3
5616           2               4
5617           3               5
5618           4               6
5619
5620      An ldr instruction takes 1-3 cycles, but does not block the
5621      pipeline.
5622
5623         NREGS           CYCLES
5624           1              1-3
5625           2              2-6
5626           3              3-9
5627           4              4-12
5628
5629      Best case ldr will always win.  However, the more ldr instructions
5630      we issue, the less likely we are to be able to schedule them well.
5631      Using ldr instructions also increases code size.
5632
5633      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5634      for counts of 3 or 4 regs.  */
5635   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5636     {
5637       rtx seq;
5638
5639       start_sequence ();
5640
5641       for (i = 0; i < count; i++)
5642         {
5643           addr = plus_constant (from, i * 4 * sign);
5644           mem = adjust_automodify_address (basemem, SImode, addr, offset);
5645           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5646           offset += 4 * sign;
5647         }
5648
5649       if (write_back)
5650         {
5651           emit_move_insn (from, plus_constant (from, count * 4 * sign));
5652           *offsetp = offset;
5653         }
5654
5655       seq = get_insns ();
5656       end_sequence ();
5657
5658       return seq;
5659     }
5660
5661   result = gen_rtx_PARALLEL (VOIDmode,
5662                              rtvec_alloc (count + (write_back ? 1 : 0)));
5663   if (write_back)
5664     {
5665       XVECEXP (result, 0, 0)
5666         = gen_rtx_SET (GET_MODE (from), from,
5667                        plus_constant (from, count * 4 * sign));
5668       i = 1;
5669       count++;
5670     }
5671
5672   for (j = 0; i < count; i++, j++)
5673     {
5674       addr = plus_constant (from, j * 4 * sign);
5675       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5676       XVECEXP (result, 0, i)
5677         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5678       offset += 4 * sign;
5679     }
5680
5681   if (write_back)
5682     *offsetp = offset;
5683
5684   return result;
5685 }
5686
5687 rtx
5688 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5689                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5690 {
5691   HOST_WIDE_INT offset = *offsetp;
5692   int i = 0, j;
5693   rtx result;
5694   int sign = up ? 1 : -1;
5695   rtx mem, addr;
5696
5697   /* See arm_gen_load_multiple for discussion of
5698      the pros/cons of ldm/stm usage for XScale.  */
5699   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5700     {
5701       rtx seq;
5702
5703       start_sequence ();
5704
5705       for (i = 0; i < count; i++)
5706         {
5707           addr = plus_constant (to, i * 4 * sign);
5708           mem = adjust_automodify_address (basemem, SImode, addr, offset);
5709           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5710           offset += 4 * sign;
5711         }
5712
5713       if (write_back)
5714         {
5715           emit_move_insn (to, plus_constant (to, count * 4 * sign));
5716           *offsetp = offset;
5717         }
5718
5719       seq = get_insns ();
5720       end_sequence ();
5721
5722       return seq;
5723     }
5724
5725   result = gen_rtx_PARALLEL (VOIDmode,
5726                              rtvec_alloc (count + (write_back ? 1 : 0)));
5727   if (write_back)
5728     {
5729       XVECEXP (result, 0, 0)
5730         = gen_rtx_SET (GET_MODE (to), to,
5731                        plus_constant (to, count * 4 * sign));
5732       i = 1;
5733       count++;
5734     }
5735
5736   for (j = 0; i < count; i++, j++)
5737     {
5738       addr = plus_constant (to, j * 4 * sign);
5739       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5740       XVECEXP (result, 0, i)
5741         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5742       offset += 4 * sign;
5743     }
5744
5745   if (write_back)
5746     *offsetp = offset;
5747
5748   return result;
5749 }
5750
5751 int
5752 arm_gen_movmemqi (rtx *operands)
5753 {
5754   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5755   HOST_WIDE_INT srcoffset, dstoffset;
5756   int i;
5757   rtx src, dst, srcbase, dstbase;
5758   rtx part_bytes_reg = NULL;
5759   rtx mem;
5760
5761   if (GET_CODE (operands[2]) != CONST_INT
5762       || GET_CODE (operands[3]) != CONST_INT
5763       || INTVAL (operands[2]) > 64
5764       || INTVAL (operands[3]) & 3)
5765     return 0;
5766
5767   dstbase = operands[0];
5768   srcbase = operands[1];
5769
5770   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
5771   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
5772
5773   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5774   out_words_to_go = INTVAL (operands[2]) / 4;
5775   last_bytes = INTVAL (operands[2]) & 3;
5776   dstoffset = srcoffset = 0;
5777
5778   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5779     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5780
5781   for (i = 0; in_words_to_go >= 2; i+=4)
5782     {
5783       if (in_words_to_go > 4)
5784         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5785                                           srcbase, &srcoffset));
5786       else
5787         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
5788                                           FALSE, srcbase, &srcoffset));
5789
5790       if (out_words_to_go)
5791         {
5792           if (out_words_to_go > 4)
5793             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5794                                                dstbase, &dstoffset));
5795           else if (out_words_to_go != 1)
5796             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5797                                                dst, TRUE,
5798                                                (last_bytes == 0
5799                                                 ? FALSE : TRUE),
5800                                                dstbase, &dstoffset));
5801           else
5802             {
5803               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5804               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5805               if (last_bytes != 0)
5806                 {
5807                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5808                   dstoffset += 4;
5809                 }
5810             }
5811         }
5812
5813       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5814       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
5815     }
5816
5817   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
5818   if (out_words_to_go)
5819     {
5820       rtx sreg;
5821
5822       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5823       sreg = copy_to_reg (mem);
5824
5825       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5826       emit_move_insn (mem, sreg);
5827       in_words_to_go--;
5828
5829       if (in_words_to_go)       /* Sanity check */
5830         abort ();
5831     }
5832
5833   if (in_words_to_go)
5834     {
5835       if (in_words_to_go < 0)
5836         abort ();
5837
5838       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5839       part_bytes_reg = copy_to_mode_reg (SImode, mem);
5840     }
5841
5842   if (last_bytes && part_bytes_reg == NULL)
5843     abort ();
5844
5845   if (BYTES_BIG_ENDIAN && last_bytes)
5846     {
5847       rtx tmp = gen_reg_rtx (SImode);
5848
5849       /* The bytes we want are in the top end of the word.  */
5850       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
5851                               GEN_INT (8 * (4 - last_bytes))));
5852       part_bytes_reg = tmp;
5853
5854       while (last_bytes)
5855         {
5856           mem = adjust_automodify_address (dstbase, QImode,
5857                                            plus_constant (dst, last_bytes - 1),
5858                                            dstoffset + last_bytes - 1);
5859           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5860
5861           if (--last_bytes)
5862             {
5863               tmp = gen_reg_rtx (SImode);
5864               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
5865               part_bytes_reg = tmp;
5866             }
5867         }
5868
5869     }
5870   else
5871     {
5872       if (last_bytes > 1)
5873         {
5874           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
5875           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
5876           last_bytes -= 2;
5877           if (last_bytes)
5878             {
5879               rtx tmp = gen_reg_rtx (SImode);
5880               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
5881               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
5882               part_bytes_reg = tmp;
5883               dstoffset += 2;
5884             }
5885         }
5886
5887       if (last_bytes)
5888         {
5889           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
5890           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5891         }
5892     }
5893
5894   return 1;
5895 }
5896
5897 /* Generate a memory reference for a half word, such that it will be loaded
5898    into the top 16 bits of the word.  We can assume that the address is
5899    known to be alignable and of the form reg, or plus (reg, const).  */
5900
5901 rtx
5902 arm_gen_rotated_half_load (rtx memref)
5903 {
5904   HOST_WIDE_INT offset = 0;
5905   rtx base = XEXP (memref, 0);
5906
5907   if (GET_CODE (base) == PLUS)
5908     {
5909       offset = INTVAL (XEXP (base, 1));
5910       base = XEXP (base, 0);
5911     }
5912
5913   /* If we aren't allowed to generate unaligned addresses, then fail.  */
5914   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0))
5915     return NULL;
5916
5917   base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
5918
5919   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
5920     return base;
5921
5922   return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
5923 }
5924
5925 /* Select a dominance comparison mode if possible for a test of the general
5926    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
5927    COND_OR == DOM_CC_X_AND_Y => (X && Y)
5928    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
5929    COND_OR == DOM_CC_X_OR_Y => (X || Y)
5930    In all cases OP will be either EQ or NE, but we don't need to know which
5931    here.  If we are unable to support a dominance comparison we return
5932    CC mode.  This will then fail to match for the RTL expressions that
5933    generate this call.  */
5934 enum machine_mode
5935 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
5936 {
5937   enum rtx_code cond1, cond2;
5938   int swapped = 0;
5939
5940   /* Currently we will probably get the wrong result if the individual
5941      comparisons are not simple.  This also ensures that it is safe to
5942      reverse a comparison if necessary.  */
5943   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
5944        != CCmode)
5945       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
5946           != CCmode))
5947     return CCmode;
5948
5949   /* The if_then_else variant of this tests the second condition if the
5950      first passes, but is true if the first fails.  Reverse the first
5951      condition to get a true "inclusive-or" expression.  */
5952   if (cond_or == DOM_CC_NX_OR_Y)
5953     cond1 = reverse_condition (cond1);
5954
5955   /* If the comparisons are not equal, and one doesn't dominate the other,
5956      then we can't do this.  */
5957   if (cond1 != cond2
5958       && !comparison_dominates_p (cond1, cond2)
5959       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
5960     return CCmode;
5961
5962   if (swapped)
5963     {
5964       enum rtx_code temp = cond1;
5965       cond1 = cond2;
5966       cond2 = temp;
5967     }
5968
5969   switch (cond1)
5970     {
5971     case EQ:
5972       if (cond2 == EQ || cond_or == DOM_CC_X_AND_Y)
5973         return CC_DEQmode;
5974
5975       switch (cond2)
5976         {
5977         case LE: return CC_DLEmode;
5978         case LEU: return CC_DLEUmode;
5979         case GE: return CC_DGEmode;
5980         case GEU: return CC_DGEUmode;
5981         default: break;
5982         }
5983
5984       break;
5985
5986     case LT:
5987       if (cond2 == LT || cond_or == DOM_CC_X_AND_Y)
5988         return CC_DLTmode;
5989       if (cond2 == LE)
5990         return CC_DLEmode;
5991       if (cond2 == NE)
5992         return CC_DNEmode;
5993       break;
5994
5995     case GT:
5996       if (cond2 == GT || cond_or == DOM_CC_X_AND_Y)
5997         return CC_DGTmode;
5998       if (cond2 == GE)
5999         return CC_DGEmode;
6000       if (cond2 == NE)
6001         return CC_DNEmode;
6002       break;
6003
6004     case LTU:
6005       if (cond2 == LTU || cond_or == DOM_CC_X_AND_Y)
6006         return CC_DLTUmode;
6007       if (cond2 == LEU)
6008         return CC_DLEUmode;
6009       if (cond2 == NE)
6010         return CC_DNEmode;
6011       break;
6012
6013     case GTU:
6014       if (cond2 == GTU || cond_or == DOM_CC_X_AND_Y)
6015         return CC_DGTUmode;
6016       if (cond2 == GEU)
6017         return CC_DGEUmode;
6018       if (cond2 == NE)
6019         return CC_DNEmode;
6020       break;
6021
6022     /* The remaining cases only occur when both comparisons are the
6023        same.  */
6024     case NE:
6025       return CC_DNEmode;
6026
6027     case LE:
6028       return CC_DLEmode;
6029
6030     case GE:
6031       return CC_DGEmode;
6032
6033     case LEU:
6034       return CC_DLEUmode;
6035
6036     case GEU:
6037       return CC_DGEUmode;
6038
6039     default:
6040       break;
6041     }
6042
6043   abort ();
6044 }
6045
6046 enum machine_mode
6047 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6048 {
6049   /* All floating point compares return CCFP if it is an equality
6050      comparison, and CCFPE otherwise.  */
6051   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6052     {
6053       switch (op)
6054         {
6055         case EQ:
6056         case NE:
6057         case UNORDERED:
6058         case ORDERED:
6059         case UNLT:
6060         case UNLE:
6061         case UNGT:
6062         case UNGE:
6063         case UNEQ:
6064         case LTGT:
6065           return CCFPmode;
6066
6067         case LT:
6068         case LE:
6069         case GT:
6070         case GE:
6071           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6072             return CCFPmode;
6073           return CCFPEmode;
6074
6075         default:
6076           abort ();
6077         }
6078     }
6079
6080   /* A compare with a shifted operand.  Because of canonicalization, the
6081      comparison will have to be swapped when we emit the assembler.  */
6082   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6083       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6084           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6085           || GET_CODE (x) == ROTATERT))
6086     return CC_SWPmode;
6087
6088   /* This is a special case that is used by combine to allow a
6089      comparison of a shifted byte load to be split into a zero-extend
6090      followed by a comparison of the shifted integer (only valid for
6091      equalities and unsigned inequalities).  */
6092   if (GET_MODE (x) == SImode
6093       && GET_CODE (x) == ASHIFT
6094       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6095       && GET_CODE (XEXP (x, 0)) == SUBREG
6096       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6097       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6098       && (op == EQ || op == NE
6099           || op == GEU || op == GTU || op == LTU || op == LEU)
6100       && GET_CODE (y) == CONST_INT)
6101     return CC_Zmode;
6102
6103   /* A construct for a conditional compare, if the false arm contains
6104      0, then both conditions must be true, otherwise either condition
6105      must be true.  Not all conditions are possible, so CCmode is
6106      returned if it can't be done.  */
6107   if (GET_CODE (x) == IF_THEN_ELSE
6108       && (XEXP (x, 2) == const0_rtx
6109           || XEXP (x, 2) == const1_rtx)
6110       && COMPARISON_P (XEXP (x, 0))
6111       && COMPARISON_P (XEXP (x, 1)))
6112     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6113                                          INTVAL (XEXP (x, 2)));
6114
6115   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
6116   if (GET_CODE (x) == AND
6117       && COMPARISON_P (XEXP (x, 0))
6118       && COMPARISON_P (XEXP (x, 1)))
6119     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6120                                          DOM_CC_X_AND_Y);
6121
6122   if (GET_CODE (x) == IOR
6123       && COMPARISON_P (XEXP (x, 0))
6124       && COMPARISON_P (XEXP (x, 1)))
6125     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6126                                          DOM_CC_X_OR_Y);
6127
6128   /* An operation (on Thumb) where we want to test for a single bit.
6129      This is done by shifting that bit up into the top bit of a
6130      scratch register; we can then branch on the sign bit.  */
6131   if (TARGET_THUMB
6132       && GET_MODE (x) == SImode
6133       && (op == EQ || op == NE)
6134       && (GET_CODE (x) == ZERO_EXTRACT))
6135     return CC_Nmode;
6136
6137   /* An operation that sets the condition codes as a side-effect, the
6138      V flag is not set correctly, so we can only use comparisons where
6139      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
6140      instead.)  */
6141   if (GET_MODE (x) == SImode
6142       && y == const0_rtx
6143       && (op == EQ || op == NE || op == LT || op == GE)
6144       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6145           || GET_CODE (x) == AND || GET_CODE (x) == IOR
6146           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6147           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6148           || GET_CODE (x) == LSHIFTRT
6149           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6150           || GET_CODE (x) == ROTATERT
6151           || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6152     return CC_NOOVmode;
6153
6154   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6155     return CC_Zmode;
6156
6157   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6158       && GET_CODE (x) == PLUS
6159       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6160     return CC_Cmode;
6161
6162   return CCmode;
6163 }
6164
6165 /* X and Y are two things to compare using CODE.  Emit the compare insn and
6166    return the rtx for register 0 in the proper mode.  FP means this is a
6167    floating point compare: I don't think that it is needed on the arm.  */
6168 rtx
6169 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6170 {
6171   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6172   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6173
6174   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6175                           gen_rtx_COMPARE (mode, x, y)));
6176
6177   return cc_reg;
6178 }
6179
6180 /* Generate a sequence of insns that will generate the correct return
6181    address mask depending on the physical architecture that the program
6182    is running on.  */
6183 rtx
6184 arm_gen_return_addr_mask (void)
6185 {
6186   rtx reg = gen_reg_rtx (Pmode);
6187
6188   emit_insn (gen_return_addr_mask (reg));
6189   return reg;
6190 }
6191
6192 void
6193 arm_reload_in_hi (rtx *operands)
6194 {
6195   rtx ref = operands[1];
6196   rtx base, scratch;
6197   HOST_WIDE_INT offset = 0;
6198
6199   if (GET_CODE (ref) == SUBREG)
6200     {
6201       offset = SUBREG_BYTE (ref);
6202       ref = SUBREG_REG (ref);
6203     }
6204
6205   if (GET_CODE (ref) == REG)
6206     {
6207       /* We have a pseudo which has been spilt onto the stack; there
6208          are two cases here: the first where there is a simple
6209          stack-slot replacement and a second where the stack-slot is
6210          out of range, or is used as a subreg.  */
6211       if (reg_equiv_mem[REGNO (ref)])
6212         {
6213           ref = reg_equiv_mem[REGNO (ref)];
6214           base = find_replacement (&XEXP (ref, 0));
6215         }
6216       else
6217         /* The slot is out of range, or was dressed up in a SUBREG.  */
6218         base = reg_equiv_address[REGNO (ref)];
6219     }
6220   else
6221     base = find_replacement (&XEXP (ref, 0));
6222
6223   /* Handle the case where the address is too complex to be offset by 1.  */
6224   if (GET_CODE (base) == MINUS
6225       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6226     {
6227       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6228
6229       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6230       base = base_plus;
6231     }
6232   else if (GET_CODE (base) == PLUS)
6233     {
6234       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6235       HOST_WIDE_INT hi, lo;
6236
6237       offset += INTVAL (XEXP (base, 1));
6238       base = XEXP (base, 0);
6239
6240       /* Rework the address into a legal sequence of insns.  */
6241       /* Valid range for lo is -4095 -> 4095 */
6242       lo = (offset >= 0
6243             ? (offset & 0xfff)
6244             : -((-offset) & 0xfff));
6245
6246       /* Corner case, if lo is the max offset then we would be out of range
6247          once we have added the additional 1 below, so bump the msb into the
6248          pre-loading insn(s).  */
6249       if (lo == 4095)
6250         lo &= 0x7ff;
6251
6252       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6253              ^ (HOST_WIDE_INT) 0x80000000)
6254             - (HOST_WIDE_INT) 0x80000000);
6255
6256       if (hi + lo != offset)
6257         abort ();
6258
6259       if (hi != 0)
6260         {
6261           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6262
6263           /* Get the base address; addsi3 knows how to handle constants
6264              that require more than one insn.  */
6265           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6266           base = base_plus;
6267           offset = lo;
6268         }
6269     }
6270
6271   /* Operands[2] may overlap operands[0] (though it won't overlap
6272      operands[1]), that's why we asked for a DImode reg -- so we can
6273      use the bit that does not overlap.  */
6274   if (REGNO (operands[2]) == REGNO (operands[0]))
6275     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6276   else
6277     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6278
6279   emit_insn (gen_zero_extendqisi2 (scratch,
6280                                    gen_rtx_MEM (QImode,
6281                                                 plus_constant (base,
6282                                                                offset))));
6283   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6284                                    gen_rtx_MEM (QImode,
6285                                                 plus_constant (base,
6286                                                                offset + 1))));
6287   if (!BYTES_BIG_ENDIAN)
6288     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6289                         gen_rtx_IOR (SImode,
6290                                      gen_rtx_ASHIFT
6291                                      (SImode,
6292                                       gen_rtx_SUBREG (SImode, operands[0], 0),
6293                                       GEN_INT (8)),
6294                                      scratch)));
6295   else
6296     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6297                             gen_rtx_IOR (SImode,
6298                                          gen_rtx_ASHIFT (SImode, scratch,
6299                                                          GEN_INT (8)),
6300                                          gen_rtx_SUBREG (SImode, operands[0],
6301                                                          0))));
6302 }
6303
6304 /* Handle storing a half-word to memory during reload by synthesizing as two
6305    byte stores.  Take care not to clobber the input values until after we
6306    have moved them somewhere safe.  This code assumes that if the DImode
6307    scratch in operands[2] overlaps either the input value or output address
6308    in some way, then that value must die in this insn (we absolutely need
6309    two scratch registers for some corner cases).  */
6310 void
6311 arm_reload_out_hi (rtx *operands)
6312 {
6313   rtx ref = operands[0];
6314   rtx outval = operands[1];
6315   rtx base, scratch;
6316   HOST_WIDE_INT offset = 0;
6317
6318   if (GET_CODE (ref) == SUBREG)
6319     {
6320       offset = SUBREG_BYTE (ref);
6321       ref = SUBREG_REG (ref);
6322     }
6323
6324   if (GET_CODE (ref) == REG)
6325     {
6326       /* We have a pseudo which has been spilt onto the stack; there
6327          are two cases here: the first where there is a simple
6328          stack-slot replacement and a second where the stack-slot is
6329          out of range, or is used as a subreg.  */
6330       if (reg_equiv_mem[REGNO (ref)])
6331         {
6332           ref = reg_equiv_mem[REGNO (ref)];
6333           base = find_replacement (&XEXP (ref, 0));
6334         }
6335       else
6336         /* The slot is out of range, or was dressed up in a SUBREG.  */
6337         base = reg_equiv_address[REGNO (ref)];
6338     }
6339   else
6340     base = find_replacement (&XEXP (ref, 0));
6341
6342   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6343
6344   /* Handle the case where the address is too complex to be offset by 1.  */
6345   if (GET_CODE (base) == MINUS
6346       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6347     {
6348       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6349
6350       /* Be careful not to destroy OUTVAL.  */
6351       if (reg_overlap_mentioned_p (base_plus, outval))
6352         {
6353           /* Updating base_plus might destroy outval, see if we can
6354              swap the scratch and base_plus.  */
6355           if (!reg_overlap_mentioned_p (scratch, outval))
6356             {
6357               rtx tmp = scratch;
6358               scratch = base_plus;
6359               base_plus = tmp;
6360             }
6361           else
6362             {
6363               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6364
6365               /* Be conservative and copy OUTVAL into the scratch now,
6366                  this should only be necessary if outval is a subreg
6367                  of something larger than a word.  */
6368               /* XXX Might this clobber base?  I can't see how it can,
6369                  since scratch is known to overlap with OUTVAL, and
6370                  must be wider than a word.  */
6371               emit_insn (gen_movhi (scratch_hi, outval));
6372               outval = scratch_hi;
6373             }
6374         }
6375
6376       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6377       base = base_plus;
6378     }
6379   else if (GET_CODE (base) == PLUS)
6380     {
6381       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6382       HOST_WIDE_INT hi, lo;
6383
6384       offset += INTVAL (XEXP (base, 1));
6385       base = XEXP (base, 0);
6386
6387       /* Rework the address into a legal sequence of insns.  */
6388       /* Valid range for lo is -4095 -> 4095 */
6389       lo = (offset >= 0
6390             ? (offset & 0xfff)
6391             : -((-offset) & 0xfff));
6392
6393       /* Corner case, if lo is the max offset then we would be out of range
6394          once we have added the additional 1 below, so bump the msb into the
6395          pre-loading insn(s).  */
6396       if (lo == 4095)
6397         lo &= 0x7ff;
6398
6399       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6400              ^ (HOST_WIDE_INT) 0x80000000)
6401             - (HOST_WIDE_INT) 0x80000000);
6402
6403       if (hi + lo != offset)
6404         abort ();
6405
6406       if (hi != 0)
6407         {
6408           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6409
6410           /* Be careful not to destroy OUTVAL.  */
6411           if (reg_overlap_mentioned_p (base_plus, outval))
6412             {
6413               /* Updating base_plus might destroy outval, see if we
6414                  can swap the scratch and base_plus.  */
6415               if (!reg_overlap_mentioned_p (scratch, outval))
6416                 {
6417                   rtx tmp = scratch;
6418                   scratch = base_plus;
6419                   base_plus = tmp;
6420                 }
6421               else
6422                 {
6423                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6424
6425                   /* Be conservative and copy outval into scratch now,
6426                      this should only be necessary if outval is a
6427                      subreg of something larger than a word.  */
6428                   /* XXX Might this clobber base?  I can't see how it
6429                      can, since scratch is known to overlap with
6430                      outval.  */
6431                   emit_insn (gen_movhi (scratch_hi, outval));
6432                   outval = scratch_hi;
6433                 }
6434             }
6435
6436           /* Get the base address; addsi3 knows how to handle constants
6437              that require more than one insn.  */
6438           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6439           base = base_plus;
6440           offset = lo;
6441         }
6442     }
6443
6444   if (BYTES_BIG_ENDIAN)
6445     {
6446       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6447                                          plus_constant (base, offset + 1)),
6448                             gen_lowpart (QImode, outval)));
6449       emit_insn (gen_lshrsi3 (scratch,
6450                               gen_rtx_SUBREG (SImode, outval, 0),
6451                               GEN_INT (8)));
6452       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6453                             gen_lowpart (QImode, scratch)));
6454     }
6455   else
6456     {
6457       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6458                             gen_lowpart (QImode, outval)));
6459       emit_insn (gen_lshrsi3 (scratch,
6460                               gen_rtx_SUBREG (SImode, outval, 0),
6461                               GEN_INT (8)));
6462       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6463                                          plus_constant (base, offset + 1)),
6464                             gen_lowpart (QImode, scratch)));
6465     }
6466 }
6467 \f
6468 /* Print a symbolic form of X to the debug file, F.  */
6469 static void
6470 arm_print_value (FILE *f, rtx x)
6471 {
6472   switch (GET_CODE (x))
6473     {
6474     case CONST_INT:
6475       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6476       return;
6477
6478     case CONST_DOUBLE:
6479       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6480       return;
6481
6482     case CONST_VECTOR:
6483       {
6484         int i;
6485
6486         fprintf (f, "<");
6487         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6488           {
6489             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6490             if (i < (CONST_VECTOR_NUNITS (x) - 1))
6491               fputc (',', f);
6492           }
6493         fprintf (f, ">");
6494       }
6495       return;
6496
6497     case CONST_STRING:
6498       fprintf (f, "\"%s\"", XSTR (x, 0));
6499       return;
6500
6501     case SYMBOL_REF:
6502       fprintf (f, "`%s'", XSTR (x, 0));
6503       return;
6504
6505     case LABEL_REF:
6506       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6507       return;
6508
6509     case CONST:
6510       arm_print_value (f, XEXP (x, 0));
6511       return;
6512
6513     case PLUS:
6514       arm_print_value (f, XEXP (x, 0));
6515       fprintf (f, "+");
6516       arm_print_value (f, XEXP (x, 1));
6517       return;
6518
6519     case PC:
6520       fprintf (f, "pc");
6521       return;
6522
6523     default:
6524       fprintf (f, "????");
6525       return;
6526     }
6527 }
6528 \f
6529 /* Routines for manipulation of the constant pool.  */
6530
6531 /* Arm instructions cannot load a large constant directly into a
6532    register; they have to come from a pc relative load.  The constant
6533    must therefore be placed in the addressable range of the pc
6534    relative load.  Depending on the precise pc relative load
6535    instruction the range is somewhere between 256 bytes and 4k.  This
6536    means that we often have to dump a constant inside a function, and
6537    generate code to branch around it.
6538
6539    It is important to minimize this, since the branches will slow
6540    things down and make the code larger.
6541
6542    Normally we can hide the table after an existing unconditional
6543    branch so that there is no interruption of the flow, but in the
6544    worst case the code looks like this:
6545
6546         ldr     rn, L1
6547         ...
6548         b       L2
6549         align
6550         L1:     .long value
6551         L2:
6552         ...
6553
6554         ldr     rn, L3
6555         ...
6556         b       L4
6557         align
6558         L3:     .long value
6559         L4:
6560         ...
6561
6562    We fix this by performing a scan after scheduling, which notices
6563    which instructions need to have their operands fetched from the
6564    constant table and builds the table.
6565
6566    The algorithm starts by building a table of all the constants that
6567    need fixing up and all the natural barriers in the function (places
6568    where a constant table can be dropped without breaking the flow).
6569    For each fixup we note how far the pc-relative replacement will be
6570    able to reach and the offset of the instruction into the function.
6571
6572    Having built the table we then group the fixes together to form
6573    tables that are as large as possible (subject to addressing
6574    constraints) and emit each table of constants after the last
6575    barrier that is within range of all the instructions in the group.
6576    If a group does not contain a barrier, then we forcibly create one
6577    by inserting a jump instruction into the flow.  Once the table has
6578    been inserted, the insns are then modified to reference the
6579    relevant entry in the pool.
6580
6581    Possible enhancements to the algorithm (not implemented) are:
6582
6583    1) For some processors and object formats, there may be benefit in
6584    aligning the pools to the start of cache lines; this alignment
6585    would need to be taken into account when calculating addressability
6586    of a pool.  */
6587
6588 /* These typedefs are located at the start of this file, so that
6589    they can be used in the prototypes there.  This comment is to
6590    remind readers of that fact so that the following structures
6591    can be understood more easily.
6592
6593      typedef struct minipool_node    Mnode;
6594      typedef struct minipool_fixup   Mfix;  */
6595
6596 struct minipool_node
6597 {
6598   /* Doubly linked chain of entries.  */
6599   Mnode * next;
6600   Mnode * prev;
6601   /* The maximum offset into the code that this entry can be placed.  While
6602      pushing fixes for forward references, all entries are sorted in order
6603      of increasing max_address.  */
6604   HOST_WIDE_INT max_address;
6605   /* Similarly for an entry inserted for a backwards ref.  */
6606   HOST_WIDE_INT min_address;
6607   /* The number of fixes referencing this entry.  This can become zero
6608      if we "unpush" an entry.  In this case we ignore the entry when we
6609      come to emit the code.  */
6610   int refcount;
6611   /* The offset from the start of the minipool.  */
6612   HOST_WIDE_INT offset;
6613   /* The value in table.  */
6614   rtx value;
6615   /* The mode of value.  */
6616   enum machine_mode mode;
6617   /* The size of the value.  With iWMMXt enabled
6618      sizes > 4 also imply an alignment of 8-bytes.  */
6619   int fix_size;
6620 };
6621
6622 struct minipool_fixup
6623 {
6624   Mfix *            next;
6625   rtx               insn;
6626   HOST_WIDE_INT     address;
6627   rtx *             loc;
6628   enum machine_mode mode;
6629   int               fix_size;
6630   rtx               value;
6631   Mnode *           minipool;
6632   HOST_WIDE_INT     forwards;
6633   HOST_WIDE_INT     backwards;
6634 };
6635
6636 /* Fixes less than a word need padding out to a word boundary.  */
6637 #define MINIPOOL_FIX_SIZE(mode) \
6638   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6639
6640 static Mnode *  minipool_vector_head;
6641 static Mnode *  minipool_vector_tail;
6642 static rtx      minipool_vector_label;
6643
6644 /* The linked list of all minipool fixes required for this function.  */
6645 Mfix *          minipool_fix_head;
6646 Mfix *          minipool_fix_tail;
6647 /* The fix entry for the current minipool, once it has been placed.  */
6648 Mfix *          minipool_barrier;
6649
6650 /* Determines if INSN is the start of a jump table.  Returns the end
6651    of the TABLE or NULL_RTX.  */
6652 static rtx
6653 is_jump_table (rtx insn)
6654 {
6655   rtx table;
6656
6657   if (GET_CODE (insn) == JUMP_INSN
6658       && JUMP_LABEL (insn) != NULL
6659       && ((table = next_real_insn (JUMP_LABEL (insn)))
6660           == next_real_insn (insn))
6661       && table != NULL
6662       && GET_CODE (table) == JUMP_INSN
6663       && (GET_CODE (PATTERN (table)) == ADDR_VEC
6664           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6665     return table;
6666
6667   return NULL_RTX;
6668 }
6669
6670 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6671 #define JUMP_TABLES_IN_TEXT_SECTION 0
6672 #endif
6673
6674 static HOST_WIDE_INT
6675 get_jump_table_size (rtx insn)
6676 {
6677   /* ADDR_VECs only take room if read-only data does into the text
6678      section.  */
6679   if (JUMP_TABLES_IN_TEXT_SECTION
6680 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6681       || 1
6682 #endif
6683       )
6684     {
6685       rtx body = PATTERN (insn);
6686       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6687
6688       return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6689     }
6690
6691   return 0;
6692 }
6693
6694 /* Move a minipool fix MP from its current location to before MAX_MP.
6695    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6696    constraints may need updating.  */
6697 static Mnode *
6698 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6699                                HOST_WIDE_INT max_address)
6700 {
6701   /* This should never be true and the code below assumes these are
6702      different.  */
6703   if (mp == max_mp)
6704     abort ();
6705
6706   if (max_mp == NULL)
6707     {
6708       if (max_address < mp->max_address)
6709         mp->max_address = max_address;
6710     }
6711   else
6712     {
6713       if (max_address > max_mp->max_address - mp->fix_size)
6714         mp->max_address = max_mp->max_address - mp->fix_size;
6715       else
6716         mp->max_address = max_address;
6717
6718       /* Unlink MP from its current position.  Since max_mp is non-null,
6719        mp->prev must be non-null.  */
6720       mp->prev->next = mp->next;
6721       if (mp->next != NULL)
6722         mp->next->prev = mp->prev;
6723       else
6724         minipool_vector_tail = mp->prev;
6725
6726       /* Re-insert it before MAX_MP.  */
6727       mp->next = max_mp;
6728       mp->prev = max_mp->prev;
6729       max_mp->prev = mp;
6730
6731       if (mp->prev != NULL)
6732         mp->prev->next = mp;
6733       else
6734         minipool_vector_head = mp;
6735     }
6736
6737   /* Save the new entry.  */
6738   max_mp = mp;
6739
6740   /* Scan over the preceding entries and adjust their addresses as
6741      required.  */
6742   while (mp->prev != NULL
6743          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6744     {
6745       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6746       mp = mp->prev;
6747     }
6748
6749   return max_mp;
6750 }
6751
6752 /* Add a constant to the minipool for a forward reference.  Returns the
6753    node added or NULL if the constant will not fit in this pool.  */
6754 static Mnode *
6755 add_minipool_forward_ref (Mfix *fix)
6756 {
6757   /* If set, max_mp is the first pool_entry that has a lower
6758      constraint than the one we are trying to add.  */
6759   Mnode *       max_mp = NULL;
6760   HOST_WIDE_INT max_address = fix->address + fix->forwards;
6761   Mnode *       mp;
6762
6763   /* If this fix's address is greater than the address of the first
6764      entry, then we can't put the fix in this pool.  We subtract the
6765      size of the current fix to ensure that if the table is fully
6766      packed we still have enough room to insert this value by suffling
6767      the other fixes forwards.  */
6768   if (minipool_vector_head &&
6769       fix->address >= minipool_vector_head->max_address - fix->fix_size)
6770     return NULL;
6771
6772   /* Scan the pool to see if a constant with the same value has
6773      already been added.  While we are doing this, also note the
6774      location where we must insert the constant if it doesn't already
6775      exist.  */
6776   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6777     {
6778       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6779           && fix->mode == mp->mode
6780           && (GET_CODE (fix->value) != CODE_LABEL
6781               || (CODE_LABEL_NUMBER (fix->value)
6782                   == CODE_LABEL_NUMBER (mp->value)))
6783           && rtx_equal_p (fix->value, mp->value))
6784         {
6785           /* More than one fix references this entry.  */
6786           mp->refcount++;
6787           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6788         }
6789
6790       /* Note the insertion point if necessary.  */
6791       if (max_mp == NULL
6792           && mp->max_address > max_address)
6793         max_mp = mp;
6794
6795       /* If we are inserting an 8-bytes aligned quantity and
6796          we have not already found an insertion point, then
6797          make sure that all such 8-byte aligned quantities are
6798          placed at the start of the pool.  */
6799       if (ARM_DOUBLEWORD_ALIGN
6800           && max_mp == NULL
6801           && fix->fix_size == 8
6802           && mp->fix_size != 8)
6803         {
6804           max_mp = mp;
6805           max_address = mp->max_address;
6806         }
6807     }
6808
6809   /* The value is not currently in the minipool, so we need to create
6810      a new entry for it.  If MAX_MP is NULL, the entry will be put on
6811      the end of the list since the placement is less constrained than
6812      any existing entry.  Otherwise, we insert the new fix before
6813      MAX_MP and, if necessary, adjust the constraints on the other
6814      entries.  */
6815   mp = xmalloc (sizeof (* mp));
6816   mp->fix_size = fix->fix_size;
6817   mp->mode = fix->mode;
6818   mp->value = fix->value;
6819   mp->refcount = 1;
6820   /* Not yet required for a backwards ref.  */
6821   mp->min_address = -65536;
6822
6823   if (max_mp == NULL)
6824     {
6825       mp->max_address = max_address;
6826       mp->next = NULL;
6827       mp->prev = minipool_vector_tail;
6828
6829       if (mp->prev == NULL)
6830         {
6831           minipool_vector_head = mp;
6832           minipool_vector_label = gen_label_rtx ();
6833         }
6834       else
6835         mp->prev->next = mp;
6836
6837       minipool_vector_tail = mp;
6838     }
6839   else
6840     {
6841       if (max_address > max_mp->max_address - mp->fix_size)
6842         mp->max_address = max_mp->max_address - mp->fix_size;
6843       else
6844         mp->max_address = max_address;
6845
6846       mp->next = max_mp;
6847       mp->prev = max_mp->prev;
6848       max_mp->prev = mp;
6849       if (mp->prev != NULL)
6850         mp->prev->next = mp;
6851       else
6852         minipool_vector_head = mp;
6853     }
6854
6855   /* Save the new entry.  */
6856   max_mp = mp;
6857
6858   /* Scan over the preceding entries and adjust their addresses as
6859      required.  */
6860   while (mp->prev != NULL
6861          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6862     {
6863       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6864       mp = mp->prev;
6865     }
6866
6867   return max_mp;
6868 }
6869
6870 static Mnode *
6871 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
6872                                 HOST_WIDE_INT  min_address)
6873 {
6874   HOST_WIDE_INT offset;
6875
6876   /* This should never be true, and the code below assumes these are
6877      different.  */
6878   if (mp == min_mp)
6879     abort ();
6880
6881   if (min_mp == NULL)
6882     {
6883       if (min_address > mp->min_address)
6884         mp->min_address = min_address;
6885     }
6886   else
6887     {
6888       /* We will adjust this below if it is too loose.  */
6889       mp->min_address = min_address;
6890
6891       /* Unlink MP from its current position.  Since min_mp is non-null,
6892          mp->next must be non-null.  */
6893       mp->next->prev = mp->prev;
6894       if (mp->prev != NULL)
6895         mp->prev->next = mp->next;
6896       else
6897         minipool_vector_head = mp->next;
6898
6899       /* Reinsert it after MIN_MP.  */
6900       mp->prev = min_mp;
6901       mp->next = min_mp->next;
6902       min_mp->next = mp;
6903       if (mp->next != NULL)
6904         mp->next->prev = mp;
6905       else
6906         minipool_vector_tail = mp;
6907     }
6908
6909   min_mp = mp;
6910
6911   offset = 0;
6912   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6913     {
6914       mp->offset = offset;
6915       if (mp->refcount > 0)
6916         offset += mp->fix_size;
6917
6918       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
6919         mp->next->min_address = mp->min_address + mp->fix_size;
6920     }
6921
6922   return min_mp;
6923 }
6924
6925 /* Add a constant to the minipool for a backward reference.  Returns the
6926    node added or NULL if the constant will not fit in this pool.
6927
6928    Note that the code for insertion for a backwards reference can be
6929    somewhat confusing because the calculated offsets for each fix do
6930    not take into account the size of the pool (which is still under
6931    construction.  */
6932 static Mnode *
6933 add_minipool_backward_ref (Mfix *fix)
6934 {
6935   /* If set, min_mp is the last pool_entry that has a lower constraint
6936      than the one we are trying to add.  */
6937   Mnode *min_mp = NULL;
6938   /* This can be negative, since it is only a constraint.  */
6939   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
6940   Mnode *mp;
6941
6942   /* If we can't reach the current pool from this insn, or if we can't
6943      insert this entry at the end of the pool without pushing other
6944      fixes out of range, then we don't try.  This ensures that we
6945      can't fail later on.  */
6946   if (min_address >= minipool_barrier->address
6947       || (minipool_vector_tail->min_address + fix->fix_size
6948           >= minipool_barrier->address))
6949     return NULL;
6950
6951   /* Scan the pool to see if a constant with the same value has
6952      already been added.  While we are doing this, also note the
6953      location where we must insert the constant if it doesn't already
6954      exist.  */
6955   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
6956     {
6957       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6958           && fix->mode == mp->mode
6959           && (GET_CODE (fix->value) != CODE_LABEL
6960               || (CODE_LABEL_NUMBER (fix->value)
6961                   == CODE_LABEL_NUMBER (mp->value)))
6962           && rtx_equal_p (fix->value, mp->value)
6963           /* Check that there is enough slack to move this entry to the
6964              end of the table (this is conservative).  */
6965           && (mp->max_address
6966               > (minipool_barrier->address
6967                  + minipool_vector_tail->offset
6968                  + minipool_vector_tail->fix_size)))
6969         {
6970           mp->refcount++;
6971           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
6972         }
6973
6974       if (min_mp != NULL)
6975         mp->min_address += fix->fix_size;
6976       else
6977         {
6978           /* Note the insertion point if necessary.  */
6979           if (mp->min_address < min_address)
6980             {
6981               /* For now, we do not allow the insertion of 8-byte alignment
6982                  requiring nodes anywhere but at the start of the pool.  */
6983               if (ARM_DOUBLEWORD_ALIGN
6984                   && fix->fix_size == 8 && mp->fix_size != 8)
6985                 return NULL;
6986               else
6987                 min_mp = mp;
6988             }
6989           else if (mp->max_address
6990                    < minipool_barrier->address + mp->offset + fix->fix_size)
6991             {
6992               /* Inserting before this entry would push the fix beyond
6993                  its maximum address (which can happen if we have
6994                  re-located a forwards fix); force the new fix to come
6995                  after it.  */
6996               min_mp = mp;
6997               min_address = mp->min_address + fix->fix_size;
6998             }
6999           /* If we are inserting an 8-bytes aligned quantity and
7000              we have not already found an insertion point, then
7001              make sure that all such 8-byte aligned quantities are
7002              placed at the start of the pool.  */
7003           else if (ARM_DOUBLEWORD_ALIGN
7004                    && min_mp == NULL
7005                    && fix->fix_size == 8
7006                    && mp->fix_size < 8)
7007             {
7008               min_mp = mp;
7009               min_address = mp->min_address + fix->fix_size;
7010             }
7011         }
7012     }
7013
7014   /* We need to create a new entry.  */
7015   mp = xmalloc (sizeof (* mp));
7016   mp->fix_size = fix->fix_size;
7017   mp->mode = fix->mode;
7018   mp->value = fix->value;
7019   mp->refcount = 1;
7020   mp->max_address = minipool_barrier->address + 65536;
7021
7022   mp->min_address = min_address;
7023
7024   if (min_mp == NULL)
7025     {
7026       mp->prev = NULL;
7027       mp->next = minipool_vector_head;
7028
7029       if (mp->next == NULL)
7030         {
7031           minipool_vector_tail = mp;
7032           minipool_vector_label = gen_label_rtx ();
7033         }
7034       else
7035         mp->next->prev = mp;
7036
7037       minipool_vector_head = mp;
7038     }
7039   else
7040     {
7041       mp->next = min_mp->next;
7042       mp->prev = min_mp;
7043       min_mp->next = mp;
7044
7045       if (mp->next != NULL)
7046         mp->next->prev = mp;
7047       else
7048         minipool_vector_tail = mp;
7049     }
7050
7051   /* Save the new entry.  */
7052   min_mp = mp;
7053
7054   if (mp->prev)
7055     mp = mp->prev;
7056   else
7057     mp->offset = 0;
7058
7059   /* Scan over the following entries and adjust their offsets.  */
7060   while (mp->next != NULL)
7061     {
7062       if (mp->next->min_address < mp->min_address + mp->fix_size)
7063         mp->next->min_address = mp->min_address + mp->fix_size;
7064
7065       if (mp->refcount)
7066         mp->next->offset = mp->offset + mp->fix_size;
7067       else
7068         mp->next->offset = mp->offset;
7069
7070       mp = mp->next;
7071     }
7072
7073   return min_mp;
7074 }
7075
7076 static void
7077 assign_minipool_offsets (Mfix *barrier)
7078 {
7079   HOST_WIDE_INT offset = 0;
7080   Mnode *mp;
7081
7082   minipool_barrier = barrier;
7083
7084   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7085     {
7086       mp->offset = offset;
7087
7088       if (mp->refcount > 0)
7089         offset += mp->fix_size;
7090     }
7091 }
7092
7093 /* Output the literal table */
7094 static void
7095 dump_minipool (rtx scan)
7096 {
7097   Mnode * mp;
7098   Mnode * nmp;
7099   int align64 = 0;
7100
7101   if (ARM_DOUBLEWORD_ALIGN)
7102     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7103       if (mp->refcount > 0 && mp->fix_size == 8)
7104         {
7105           align64 = 1;
7106           break;
7107         }
7108
7109   if (dump_file)
7110     fprintf (dump_file,
7111              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7112              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7113
7114   scan = emit_label_after (gen_label_rtx (), scan);
7115   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7116   scan = emit_label_after (minipool_vector_label, scan);
7117
7118   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7119     {
7120       if (mp->refcount > 0)
7121         {
7122           if (dump_file)
7123             {
7124               fprintf (dump_file,
7125                        ";;  Offset %u, min %ld, max %ld ",
7126                        (unsigned) mp->offset, (unsigned long) mp->min_address,
7127                        (unsigned long) mp->max_address);
7128               arm_print_value (dump_file, mp->value);
7129               fputc ('\n', dump_file);
7130             }
7131
7132           switch (mp->fix_size)
7133             {
7134 #ifdef HAVE_consttable_1
7135             case 1:
7136               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7137               break;
7138
7139 #endif
7140 #ifdef HAVE_consttable_2
7141             case 2:
7142               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7143               break;
7144
7145 #endif
7146 #ifdef HAVE_consttable_4
7147             case 4:
7148               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7149               break;
7150
7151 #endif
7152 #ifdef HAVE_consttable_8
7153             case 8:
7154               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7155               break;
7156
7157 #endif
7158             default:
7159               abort ();
7160               break;
7161             }
7162         }
7163
7164       nmp = mp->next;
7165       free (mp);
7166     }
7167
7168   minipool_vector_head = minipool_vector_tail = NULL;
7169   scan = emit_insn_after (gen_consttable_end (), scan);
7170   scan = emit_barrier_after (scan);
7171 }
7172
7173 /* Return the cost of forcibly inserting a barrier after INSN.  */
7174 static int
7175 arm_barrier_cost (rtx insn)
7176 {
7177   /* Basing the location of the pool on the loop depth is preferable,
7178      but at the moment, the basic block information seems to be
7179      corrupt by this stage of the compilation.  */
7180   int base_cost = 50;
7181   rtx next = next_nonnote_insn (insn);
7182
7183   if (next != NULL && GET_CODE (next) == CODE_LABEL)
7184     base_cost -= 20;
7185
7186   switch (GET_CODE (insn))
7187     {
7188     case CODE_LABEL:
7189       /* It will always be better to place the table before the label, rather
7190          than after it.  */
7191       return 50;
7192
7193     case INSN:
7194     case CALL_INSN:
7195       return base_cost;
7196
7197     case JUMP_INSN:
7198       return base_cost - 10;
7199
7200     default:
7201       return base_cost + 10;
7202     }
7203 }
7204
7205 /* Find the best place in the insn stream in the range
7206    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7207    Create the barrier by inserting a jump and add a new fix entry for
7208    it.  */
7209 static Mfix *
7210 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7211 {
7212   HOST_WIDE_INT count = 0;
7213   rtx barrier;
7214   rtx from = fix->insn;
7215   rtx selected = from;
7216   int selected_cost;
7217   HOST_WIDE_INT selected_address;
7218   Mfix * new_fix;
7219   HOST_WIDE_INT max_count = max_address - fix->address;
7220   rtx label = gen_label_rtx ();
7221
7222   selected_cost = arm_barrier_cost (from);
7223   selected_address = fix->address;
7224
7225   while (from && count < max_count)
7226     {
7227       rtx tmp;
7228       int new_cost;
7229
7230       /* This code shouldn't have been called if there was a natural barrier
7231          within range.  */
7232       if (GET_CODE (from) == BARRIER)
7233         abort ();
7234
7235       /* Count the length of this insn.  */
7236       count += get_attr_length (from);
7237
7238       /* If there is a jump table, add its length.  */
7239       tmp = is_jump_table (from);
7240       if (tmp != NULL)
7241         {
7242           count += get_jump_table_size (tmp);
7243
7244           /* Jump tables aren't in a basic block, so base the cost on
7245              the dispatch insn.  If we select this location, we will
7246              still put the pool after the table.  */
7247           new_cost = arm_barrier_cost (from);
7248
7249           if (count < max_count && new_cost <= selected_cost)
7250             {
7251               selected = tmp;
7252               selected_cost = new_cost;
7253               selected_address = fix->address + count;
7254             }
7255
7256           /* Continue after the dispatch table.  */
7257           from = NEXT_INSN (tmp);
7258           continue;
7259         }
7260
7261       new_cost = arm_barrier_cost (from);
7262
7263       if (count < max_count && new_cost <= selected_cost)
7264         {
7265           selected = from;
7266           selected_cost = new_cost;
7267           selected_address = fix->address + count;
7268         }
7269
7270       from = NEXT_INSN (from);
7271     }
7272
7273   /* Create a new JUMP_INSN that branches around a barrier.  */
7274   from = emit_jump_insn_after (gen_jump (label), selected);
7275   JUMP_LABEL (from) = label;
7276   barrier = emit_barrier_after (from);
7277   emit_label_after (label, barrier);
7278
7279   /* Create a minipool barrier entry for the new barrier.  */
7280   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7281   new_fix->insn = barrier;
7282   new_fix->address = selected_address;
7283   new_fix->next = fix->next;
7284   fix->next = new_fix;
7285
7286   return new_fix;
7287 }
7288
7289 /* Record that there is a natural barrier in the insn stream at
7290    ADDRESS.  */
7291 static void
7292 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7293 {
7294   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7295
7296   fix->insn = insn;
7297   fix->address = address;
7298
7299   fix->next = NULL;
7300   if (minipool_fix_head != NULL)
7301     minipool_fix_tail->next = fix;
7302   else
7303     minipool_fix_head = fix;
7304
7305   minipool_fix_tail = fix;
7306 }
7307
7308 /* Record INSN, which will need fixing up to load a value from the
7309    minipool.  ADDRESS is the offset of the insn since the start of the
7310    function; LOC is a pointer to the part of the insn which requires
7311    fixing; VALUE is the constant that must be loaded, which is of type
7312    MODE.  */
7313 static void
7314 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7315                    enum machine_mode mode, rtx value)
7316 {
7317   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7318
7319 #ifdef AOF_ASSEMBLER
7320   /* PIC symbol references need to be converted into offsets into the
7321      based area.  */
7322   /* XXX This shouldn't be done here.  */
7323   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7324     value = aof_pic_entry (value);
7325 #endif /* AOF_ASSEMBLER */
7326
7327   fix->insn = insn;
7328   fix->address = address;
7329   fix->loc = loc;
7330   fix->mode = mode;
7331   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7332   fix->value = value;
7333   fix->forwards = get_attr_pool_range (insn);
7334   fix->backwards = get_attr_neg_pool_range (insn);
7335   fix->minipool = NULL;
7336
7337   /* If an insn doesn't have a range defined for it, then it isn't
7338      expecting to be reworked by this code.  Better to abort now than
7339      to generate duff assembly code.  */
7340   if (fix->forwards == 0 && fix->backwards == 0)
7341     abort ();
7342
7343   /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7344      So there might be an empty word before the start of the pool.
7345      Hence we reduce the forward range by 4 to allow for this
7346      possibility.  */
7347   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7348     fix->forwards -= 4;
7349
7350   if (dump_file)
7351     {
7352       fprintf (dump_file,
7353                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7354                GET_MODE_NAME (mode),
7355                INSN_UID (insn), (unsigned long) address,
7356                -1 * (long)fix->backwards, (long)fix->forwards);
7357       arm_print_value (dump_file, fix->value);
7358       fprintf (dump_file, "\n");
7359     }
7360
7361   /* Add it to the chain of fixes.  */
7362   fix->next = NULL;
7363
7364   if (minipool_fix_head != NULL)
7365     minipool_fix_tail->next = fix;
7366   else
7367     minipool_fix_head = fix;
7368
7369   minipool_fix_tail = fix;
7370 }
7371
7372 /* Return the cost of synthesizing the const_double VAL inline.
7373    Returns the number of insns needed, or 99 if we don't know how to
7374    do it.  */
7375 int
7376 arm_const_double_inline_cost (rtx val)
7377 {
7378   long parts[2];
7379   
7380   if (GET_MODE (val) == DFmode)
7381     {
7382       REAL_VALUE_TYPE r;
7383       if (!TARGET_SOFT_FLOAT)
7384         return 99;
7385       REAL_VALUE_FROM_CONST_DOUBLE (r, val);
7386       REAL_VALUE_TO_TARGET_DOUBLE (r, parts);
7387     }
7388   else if (GET_MODE (val) != VOIDmode)
7389     return 99;
7390   else
7391     {
7392       parts[0] = CONST_DOUBLE_LOW (val);
7393       parts[1] = CONST_DOUBLE_HIGH (val);
7394     }
7395
7396   return (arm_gen_constant (SET, SImode, NULL_RTX, parts[0],
7397                             NULL_RTX, NULL_RTX, 0, 0)
7398           + arm_gen_constant (SET, SImode, NULL_RTX, parts[1],
7399                               NULL_RTX, NULL_RTX, 0, 0));
7400 }
7401
7402 /* Determine if a CONST_DOUBLE should be pushed to the minipool */
7403 static bool
7404 const_double_needs_minipool (rtx val)
7405 {
7406   /* thumb only knows to load a CONST_DOUBLE from memory at the moment */
7407   if (TARGET_THUMB)
7408     return true;
7409
7410   /* Don't push anything to the minipool if a CONST_DOUBLE can be built with
7411      a few ALU insns directly. On balance, the optimum is likely to be around
7412      3 insns, except when there are no load delay slots where it should be 4.
7413      When optimizing for size, a limit of 3 allows saving at least one word
7414      except for cases where a single minipool entry could be shared more than
7415      2 times which is rather unlikely to outweight the overall savings. */
7416   return (arm_const_double_inline_cost (val)
7417           > ((optimize_size || arm_ld_sched) ? 3 : 4));
7418 }
7419
7420 /* Scan INSN and note any of its operands that need fixing.
7421    If DO_PUSHES is false we do not actually push any of the fixups
7422    needed.  The function returns TRUE is any fixups were needed/pushed.
7423    This is used by arm_memory_load_p() which needs to know about loads
7424    of constants that will be converted into minipool loads.  */
7425 static bool
7426 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7427 {
7428   bool result = false;
7429   int opno;
7430
7431   extract_insn (insn);
7432
7433   if (!constrain_operands (1))
7434     fatal_insn_not_found (insn);
7435
7436   if (recog_data.n_alternatives == 0)
7437     return false;
7438
7439   /* Fill in recog_op_alt with information about the constraints of this insn.  */
7440   preprocess_constraints ();
7441
7442   for (opno = 0; opno < recog_data.n_operands; opno++)
7443     {
7444       /* Things we need to fix can only occur in inputs.  */
7445       if (recog_data.operand_type[opno] != OP_IN)
7446         continue;
7447
7448       /* If this alternative is a memory reference, then any mention
7449          of constants in this alternative is really to fool reload
7450          into allowing us to accept one there.  We need to fix them up
7451          now so that we output the right code.  */
7452       if (recog_op_alt[opno][which_alternative].memory_ok)
7453         {
7454           rtx op = recog_data.operand[opno];
7455
7456           if (CONSTANT_P (op)
7457               && (GET_CODE (op) != CONST_DOUBLE
7458                   || const_double_needs_minipool (op)))
7459             {
7460               if (do_pushes)
7461                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7462                                    recog_data.operand_mode[opno], op);
7463               result = true;
7464             }
7465           else if (GET_CODE (op) == MEM
7466                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7467                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7468             {
7469               if (do_pushes)
7470                 {
7471                   rtx cop = avoid_constant_pool_reference (op);
7472
7473                   /* Casting the address of something to a mode narrower
7474                      than a word can cause avoid_constant_pool_reference()
7475                      to return the pool reference itself.  That's no good to
7476                      us here.  Lets just hope that we can use the
7477                      constant pool value directly.  */
7478                   if (op == cop)
7479                     cop = get_pool_constant (XEXP (op, 0));
7480
7481                   push_minipool_fix (insn, address,
7482                                      recog_data.operand_loc[opno],
7483                                      recog_data.operand_mode[opno], cop);
7484                 }
7485
7486               result = true;
7487             }
7488         }
7489     }
7490
7491   return result;
7492 }
7493
7494 /* Gcc puts the pool in the wrong place for ARM, since we can only
7495    load addresses a limited distance around the pc.  We do some
7496    special munging to move the constant pool values to the correct
7497    point in the code.  */
7498 static void
7499 arm_reorg (void)
7500 {
7501   rtx insn;
7502   HOST_WIDE_INT address = 0;
7503   Mfix * fix;
7504
7505   minipool_fix_head = minipool_fix_tail = NULL;
7506
7507   /* The first insn must always be a note, or the code below won't
7508      scan it properly.  */
7509   insn = get_insns ();
7510   if (GET_CODE (insn) != NOTE)
7511     abort ();
7512
7513   /* Scan all the insns and record the operands that will need fixing.  */
7514   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7515     {
7516       if (TARGET_CIRRUS_FIX_INVALID_INSNS
7517           && (arm_cirrus_insn_p (insn)
7518               || GET_CODE (insn) == JUMP_INSN
7519               || arm_memory_load_p (insn)))
7520         cirrus_reorg (insn);
7521
7522       if (GET_CODE (insn) == BARRIER)
7523         push_minipool_barrier (insn, address);
7524       else if (INSN_P (insn))
7525         {
7526           rtx table;
7527
7528           note_invalid_constants (insn, address, true);
7529           address += get_attr_length (insn);
7530
7531           /* If the insn is a vector jump, add the size of the table
7532              and skip the table.  */
7533           if ((table = is_jump_table (insn)) != NULL)
7534             {
7535               address += get_jump_table_size (table);
7536               insn = table;
7537             }
7538         }
7539     }
7540
7541   fix = minipool_fix_head;
7542
7543   /* Now scan the fixups and perform the required changes.  */
7544   while (fix)
7545     {
7546       Mfix * ftmp;
7547       Mfix * fdel;
7548       Mfix *  last_added_fix;
7549       Mfix * last_barrier = NULL;
7550       Mfix * this_fix;
7551
7552       /* Skip any further barriers before the next fix.  */
7553       while (fix && GET_CODE (fix->insn) == BARRIER)
7554         fix = fix->next;
7555
7556       /* No more fixes.  */
7557       if (fix == NULL)
7558         break;
7559
7560       last_added_fix = NULL;
7561
7562       for (ftmp = fix; ftmp; ftmp = ftmp->next)
7563         {
7564           if (GET_CODE (ftmp->insn) == BARRIER)
7565             {
7566               if (ftmp->address >= minipool_vector_head->max_address)
7567                 break;
7568
7569               last_barrier = ftmp;
7570             }
7571           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7572             break;
7573
7574           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
7575         }
7576
7577       /* If we found a barrier, drop back to that; any fixes that we
7578          could have reached but come after the barrier will now go in
7579          the next mini-pool.  */
7580       if (last_barrier != NULL)
7581         {
7582           /* Reduce the refcount for those fixes that won't go into this
7583              pool after all.  */
7584           for (fdel = last_barrier->next;
7585                fdel && fdel != ftmp;
7586                fdel = fdel->next)
7587             {
7588               fdel->minipool->refcount--;
7589               fdel->minipool = NULL;
7590             }
7591
7592           ftmp = last_barrier;
7593         }
7594       else
7595         {
7596           /* ftmp is first fix that we can't fit into this pool and
7597              there no natural barriers that we could use.  Insert a
7598              new barrier in the code somewhere between the previous
7599              fix and this one, and arrange to jump around it.  */
7600           HOST_WIDE_INT max_address;
7601
7602           /* The last item on the list of fixes must be a barrier, so
7603              we can never run off the end of the list of fixes without
7604              last_barrier being set.  */
7605           if (ftmp == NULL)
7606             abort ();
7607
7608           max_address = minipool_vector_head->max_address;
7609           /* Check that there isn't another fix that is in range that
7610              we couldn't fit into this pool because the pool was
7611              already too large: we need to put the pool before such an
7612              instruction.  */
7613           if (ftmp->address < max_address)
7614             max_address = ftmp->address;
7615
7616           last_barrier = create_fix_barrier (last_added_fix, max_address);
7617         }
7618
7619       assign_minipool_offsets (last_barrier);
7620
7621       while (ftmp)
7622         {
7623           if (GET_CODE (ftmp->insn) != BARRIER
7624               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7625                   == NULL))
7626             break;
7627
7628           ftmp = ftmp->next;
7629         }
7630
7631       /* Scan over the fixes we have identified for this pool, fixing them
7632          up and adding the constants to the pool itself.  */
7633       for (this_fix = fix; this_fix && ftmp != this_fix;
7634            this_fix = this_fix->next)
7635         if (GET_CODE (this_fix->insn) != BARRIER)
7636           {
7637             rtx addr
7638               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
7639                                                   minipool_vector_label),
7640                                this_fix->minipool->offset);
7641             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7642           }
7643
7644       dump_minipool (last_barrier->insn);
7645       fix = ftmp;
7646     }
7647
7648   /* From now on we must synthesize any constants that we can't handle
7649      directly.  This can happen if the RTL gets split during final
7650      instruction generation.  */
7651   after_arm_reorg = 1;
7652
7653   /* Free the minipool memory.  */
7654   obstack_free (&minipool_obstack, minipool_startobj);
7655 }
7656 \f
7657 /* Routines to output assembly language.  */
7658
7659 /* If the rtx is the correct value then return the string of the number.
7660    In this way we can ensure that valid double constants are generated even
7661    when cross compiling.  */
7662 const char *
7663 fp_immediate_constant (rtx x)
7664 {
7665   REAL_VALUE_TYPE r;
7666   int i;
7667
7668   if (!fp_consts_inited)
7669     init_fp_table ();
7670
7671   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7672   for (i = 0; i < 8; i++)
7673     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7674       return strings_fp[i];
7675
7676   abort ();
7677 }
7678
7679 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
7680 static const char *
7681 fp_const_from_val (REAL_VALUE_TYPE *r)
7682 {
7683   int i;
7684
7685   if (!fp_consts_inited)
7686     init_fp_table ();
7687
7688   for (i = 0; i < 8; i++)
7689     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7690       return strings_fp[i];
7691
7692   abort ();
7693 }
7694
7695 /* Output the operands of a LDM/STM instruction to STREAM.
7696    MASK is the ARM register set mask of which only bits 0-15 are important.
7697    REG is the base register, either the frame pointer or the stack pointer,
7698    INSTR is the possibly suffixed load or store instruction.  */
7699
7700 static void
7701 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
7702                  unsigned long mask)
7703 {
7704   unsigned i;
7705   bool not_first = FALSE;
7706
7707   fputc ('\t', stream);
7708   asm_fprintf (stream, instr, reg);
7709   fputs (", {", stream);
7710
7711   for (i = 0; i <= LAST_ARM_REGNUM; i++)
7712     if (mask & (1 << i))
7713       {
7714         if (not_first)
7715           fprintf (stream, ", ");
7716
7717         asm_fprintf (stream, "%r", i);
7718         not_first = TRUE;
7719       }
7720
7721   fprintf (stream, "}\n");
7722 }
7723
7724
7725 /* Output a FLDMX instruction to STREAM.
7726    BASE if the register containing the address.
7727    REG and COUNT specify the register range.
7728    Extra registers may be added to avoid hardware bugs.  */
7729
7730 static void
7731 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7732 {
7733   int i;
7734
7735   /* Workaround ARM10 VFPr1 bug.  */
7736   if (count == 2 && !arm_arch6)
7737     {
7738       if (reg == 15)
7739         reg--;
7740       count++;
7741     }
7742
7743   fputc ('\t', stream);
7744   asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7745
7746   for (i = reg; i < reg + count; i++)
7747     {
7748       if (i > reg)
7749         fputs (", ", stream);
7750       asm_fprintf (stream, "d%d", i);
7751     }
7752   fputs ("}\n", stream);
7753
7754 }
7755
7756
7757 /* Output the assembly for a store multiple.  */
7758
7759 const char *
7760 vfp_output_fstmx (rtx * operands)
7761 {
7762   char pattern[100];
7763   int p;
7764   int base;
7765   int i;
7766
7767   strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7768   p = strlen (pattern);
7769
7770   if (GET_CODE (operands[1]) != REG)
7771     abort ();
7772
7773   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7774   for (i = 1; i < XVECLEN (operands[2], 0); i++)
7775     {
7776       p += sprintf (&pattern[p], ", d%d", base + i);
7777     }
7778   strcpy (&pattern[p], "}");
7779
7780   output_asm_insn (pattern, operands);
7781   return "";
7782 }
7783
7784
7785 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
7786    number of bytes pushed.  */
7787
7788 static int
7789 vfp_emit_fstmx (int base_reg, int count)
7790 {
7791   rtx par;
7792   rtx dwarf;
7793   rtx tmp, reg;
7794   int i;
7795
7796   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
7797      register pairs are stored by a store multiple insn.  We avoid this
7798      by pushing an extra pair.  */
7799   if (count == 2 && !arm_arch6)
7800     {
7801       if (base_reg == LAST_VFP_REGNUM - 3)
7802         base_reg -= 2;
7803       count++;
7804     }
7805
7806   /* ??? The frame layout is implementation defined.  We describe
7807      standard format 1 (equivalent to a FSTMD insn and unused pad word).
7808      We really need some way of representing the whole block so that the
7809      unwinder can figure it out at runtime.  */
7810   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7811   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
7812
7813   reg = gen_rtx_REG (DFmode, base_reg);
7814   base_reg += 2;
7815
7816   XVECEXP (par, 0, 0)
7817     = gen_rtx_SET (VOIDmode,
7818                    gen_rtx_MEM (BLKmode,
7819                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7820                    gen_rtx_UNSPEC (BLKmode,
7821                                    gen_rtvec (1, reg),
7822                                    UNSPEC_PUSH_MULT));
7823
7824   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7825                      gen_rtx_PLUS (SImode, stack_pointer_rtx,
7826                                    GEN_INT (-(count * 8 + 4))));
7827   RTX_FRAME_RELATED_P (tmp) = 1;
7828   XVECEXP (dwarf, 0, 0) = tmp;
7829
7830   tmp = gen_rtx_SET (VOIDmode,
7831                      gen_rtx_MEM (DFmode, stack_pointer_rtx),
7832                      reg);
7833   RTX_FRAME_RELATED_P (tmp) = 1;
7834   XVECEXP (dwarf, 0, 1) = tmp;
7835
7836   for (i = 1; i < count; i++)
7837     {
7838       reg = gen_rtx_REG (DFmode, base_reg);
7839       base_reg += 2;
7840       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
7841
7842       tmp = gen_rtx_SET (VOIDmode,
7843                          gen_rtx_MEM (DFmode,
7844                                       gen_rtx_PLUS (SImode,
7845                                                     stack_pointer_rtx,
7846                                                     GEN_INT (i * 8))),
7847                          reg);
7848       RTX_FRAME_RELATED_P (tmp) = 1;
7849       XVECEXP (dwarf, 0, i + 1) = tmp;
7850     }
7851
7852   par = emit_insn (par);
7853   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7854                                        REG_NOTES (par));
7855   RTX_FRAME_RELATED_P (par) = 1;
7856
7857   return count * 8 + 4;
7858 }
7859
7860
7861 /* Output a 'call' insn.  */
7862 const char *
7863 output_call (rtx *operands)
7864 {
7865   if (arm_arch5)
7866     abort ();           /* Patterns should call blx <reg> directly.  */
7867
7868   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
7869   if (REGNO (operands[0]) == LR_REGNUM)
7870     {
7871       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
7872       output_asm_insn ("mov%?\t%0, %|lr", operands);
7873     }
7874
7875   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7876
7877   if (TARGET_INTERWORK || arm_arch4t)
7878     output_asm_insn ("bx%?\t%0", operands);
7879   else
7880     output_asm_insn ("mov%?\t%|pc, %0", operands);
7881
7882   return "";
7883 }
7884
7885 /* Output a 'call' insn that is a reference in memory.  */
7886 const char *
7887 output_call_mem (rtx *operands)
7888 {
7889   if (TARGET_INTERWORK && !arm_arch5)
7890     {
7891       output_asm_insn ("ldr%?\t%|ip, %0", operands);
7892       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7893       output_asm_insn ("bx%?\t%|ip", operands);
7894     }
7895   else if (regno_use_in (LR_REGNUM, operands[0]))
7896     {
7897       /* LR is used in the memory address.  We load the address in the
7898          first instruction.  It's safe to use IP as the target of the
7899          load since the call will kill it anyway.  */
7900       output_asm_insn ("ldr%?\t%|ip, %0", operands);
7901       if (arm_arch5)
7902         output_asm_insn ("blx%?\t%|ip", operands);
7903       else
7904         {
7905           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7906           if (arm_arch4t)
7907             output_asm_insn ("bx%?\t%|ip", operands);
7908           else
7909             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
7910         }
7911     }
7912   else
7913     {
7914       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7915       output_asm_insn ("ldr%?\t%|pc, %0", operands);
7916     }
7917
7918   return "";
7919 }
7920
7921
7922 /* Output a move from arm registers to an fpa registers.
7923    OPERANDS[0] is an fpa register.
7924    OPERANDS[1] is the first registers of an arm register pair.  */
7925 const char *
7926 output_mov_long_double_fpa_from_arm (rtx *operands)
7927 {
7928   int arm_reg0 = REGNO (operands[1]);
7929   rtx ops[3];
7930
7931   if (arm_reg0 == IP_REGNUM)
7932     abort ();
7933
7934   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7935   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7936   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
7937
7938   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
7939   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
7940
7941   return "";
7942 }
7943
7944 /* Output a move from an fpa register to arm registers.
7945    OPERANDS[0] is the first registers of an arm register pair.
7946    OPERANDS[1] is an fpa register.  */
7947 const char *
7948 output_mov_long_double_arm_from_fpa (rtx *operands)
7949 {
7950   int arm_reg0 = REGNO (operands[0]);
7951   rtx ops[3];
7952
7953   if (arm_reg0 == IP_REGNUM)
7954     abort ();
7955
7956   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7957   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7958   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
7959
7960   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
7961   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
7962   return "";
7963 }
7964
7965 /* Output a move from arm registers to arm registers of a long double
7966    OPERANDS[0] is the destination.
7967    OPERANDS[1] is the source.  */
7968 const char *
7969 output_mov_long_double_arm_from_arm (rtx *operands)
7970 {
7971   /* We have to be careful here because the two might overlap.  */
7972   int dest_start = REGNO (operands[0]);
7973   int src_start = REGNO (operands[1]);
7974   rtx ops[2];
7975   int i;
7976
7977   if (dest_start < src_start)
7978     {
7979       for (i = 0; i < 3; i++)
7980         {
7981           ops[0] = gen_rtx_REG (SImode, dest_start + i);
7982           ops[1] = gen_rtx_REG (SImode, src_start + i);
7983           output_asm_insn ("mov%?\t%0, %1", ops);
7984         }
7985     }
7986   else
7987     {
7988       for (i = 2; i >= 0; i--)
7989         {
7990           ops[0] = gen_rtx_REG (SImode, dest_start + i);
7991           ops[1] = gen_rtx_REG (SImode, src_start + i);
7992           output_asm_insn ("mov%?\t%0, %1", ops);
7993         }
7994     }
7995
7996   return "";
7997 }
7998
7999
8000 /* Output a move from arm registers to an fpa registers.
8001    OPERANDS[0] is an fpa register.
8002    OPERANDS[1] is the first registers of an arm register pair.  */
8003 const char *
8004 output_mov_double_fpa_from_arm (rtx *operands)
8005 {
8006   int arm_reg0 = REGNO (operands[1]);
8007   rtx ops[2];
8008
8009   if (arm_reg0 == IP_REGNUM)
8010     abort ();
8011
8012   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8013   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8014   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8015   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8016   return "";
8017 }
8018
8019 /* Output a move from an fpa register to arm registers.
8020    OPERANDS[0] is the first registers of an arm register pair.
8021    OPERANDS[1] is an fpa register.  */
8022 const char *
8023 output_mov_double_arm_from_fpa (rtx *operands)
8024 {
8025   int arm_reg0 = REGNO (operands[0]);
8026   rtx ops[2];
8027
8028   if (arm_reg0 == IP_REGNUM)
8029     abort ();
8030
8031   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8032   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8033   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8034   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8035   return "";
8036 }
8037
8038 /* Output a move between double words.
8039    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8040    or MEM<-REG and all MEMs must be offsettable addresses.  */
8041 const char *
8042 output_move_double (rtx *operands)
8043 {
8044   enum rtx_code code0 = GET_CODE (operands[0]);
8045   enum rtx_code code1 = GET_CODE (operands[1]);
8046   rtx otherops[3];
8047
8048   if (code0 == REG)
8049     {
8050       int reg0 = REGNO (operands[0]);
8051
8052       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8053
8054       if (code1 == REG)
8055         {
8056           int reg1 = REGNO (operands[1]);
8057           if (reg1 == IP_REGNUM)
8058             abort ();
8059
8060           /* Ensure the second source is not overwritten.  */
8061           if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
8062             output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
8063           else
8064             output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
8065         }
8066       else if (code1 == CONST_VECTOR)
8067         {
8068           HOST_WIDE_INT hint = 0;
8069
8070           switch (GET_MODE (operands[1]))
8071             {
8072             case V2SImode:
8073               otherops[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 1)));
8074               operands[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 0)));
8075               break;
8076
8077             case V4HImode:
8078               if (BYTES_BIG_ENDIAN)
8079                 {
8080                   hint = INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8081                   hint <<= 16;
8082                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8083                 }
8084               else
8085                 {
8086                   hint = INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8087                   hint <<= 16;
8088                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8089                 }
8090
8091               otherops[1] = GEN_INT (hint);
8092               hint = 0;
8093
8094               if (BYTES_BIG_ENDIAN)
8095                 {
8096                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8097                   hint <<= 16;
8098                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8099                 }
8100               else
8101                 {
8102                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8103                   hint <<= 16;
8104                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8105                 }
8106
8107               operands[1] = GEN_INT (hint);
8108               break;
8109
8110             case V8QImode:
8111               if (BYTES_BIG_ENDIAN)
8112                 {
8113                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8114                   hint <<= 8;
8115                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8116                   hint <<= 8;
8117                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8118                   hint <<= 8;
8119                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8120                 }
8121               else
8122                 {
8123                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8124                   hint <<= 8;
8125                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8126                   hint <<= 8;
8127                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8128                   hint <<= 8;
8129                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8130                 }
8131
8132               otherops[1] = GEN_INT (hint);
8133               hint = 0;
8134
8135               if (BYTES_BIG_ENDIAN)
8136                 {
8137                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8138                   hint <<= 8;
8139                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8140                   hint <<= 8;
8141                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8142                   hint <<= 8;
8143                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8144                 }
8145               else
8146                 {
8147                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8148                   hint <<= 8;
8149                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8150                   hint <<= 8;
8151                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8152                   hint <<= 8;
8153                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8154                 }
8155
8156               operands[1] = GEN_INT (hint);
8157               break;
8158
8159             default:
8160               abort ();
8161             }
8162           output_mov_immediate (operands);
8163           output_mov_immediate (otherops);
8164         }
8165       else if (code1 == CONST_DOUBLE)
8166         {
8167           if (GET_MODE (operands[1]) == DFmode)
8168             {
8169               REAL_VALUE_TYPE r;
8170               long l[2];
8171
8172               REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
8173               REAL_VALUE_TO_TARGET_DOUBLE (r, l);
8174               otherops[1] = GEN_INT (l[1]);
8175               operands[1] = GEN_INT (l[0]);
8176             }
8177           else if (GET_MODE (operands[1]) != VOIDmode)
8178             abort ();
8179           else if (WORDS_BIG_ENDIAN)
8180             {
8181               otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8182               operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8183             }
8184           else
8185             {
8186               otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8187               operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8188             }
8189
8190           output_mov_immediate (operands);
8191           output_mov_immediate (otherops);
8192         }
8193       else if (code1 == CONST_INT)
8194         {
8195 #if HOST_BITS_PER_WIDE_INT > 32
8196           /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
8197              what the upper word is.  */
8198           if (WORDS_BIG_ENDIAN)
8199             {
8200               otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8201               operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8202             }
8203           else
8204             {
8205               otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8206               operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8207             }
8208 #else
8209           /* Sign extend the intval into the high-order word.  */
8210           if (WORDS_BIG_ENDIAN)
8211             {
8212               otherops[1] = operands[1];
8213               operands[1] = (INTVAL (operands[1]) < 0
8214                              ? constm1_rtx : const0_rtx);
8215             }
8216           else
8217             otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
8218 #endif
8219           output_mov_immediate (otherops);
8220           output_mov_immediate (operands);
8221         }
8222       else if (code1 == MEM)
8223         {
8224           switch (GET_CODE (XEXP (operands[1], 0)))
8225             {
8226             case REG:
8227               output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8228               break;
8229
8230             case PRE_INC:
8231               if (!TARGET_LDRD)
8232                 abort (); /* Should never happen now.  */
8233               output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8234               break;
8235
8236             case PRE_DEC:
8237               output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8238               break;
8239
8240             case POST_INC:
8241               output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8242               break;
8243
8244             case POST_DEC:
8245               if (!TARGET_LDRD)
8246                 abort (); /* Should never happen now.  */
8247               output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8248               break;
8249
8250             case PRE_MODIFY:
8251             case POST_MODIFY:
8252               otherops[0] = operands[0];
8253               otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8254               otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8255
8256               if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8257                 {
8258                   if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8259                     {
8260                       /* Registers overlap so split out the increment.  */
8261                       output_asm_insn ("add%?\t%1, %1, %2", otherops);
8262                       output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8263                     }
8264                   else
8265                     output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8266                 }
8267               else
8268                 {
8269                   /* We only allow constant increments, so this is safe.  */
8270                   output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8271                 }
8272               break;
8273
8274             case LABEL_REF:
8275             case CONST:
8276               output_asm_insn ("adr%?\t%0, %1", operands);
8277               output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8278               break;
8279
8280             default:
8281               if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8282                                    GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8283                 {
8284                   otherops[0] = operands[0];
8285                   otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8286                   otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8287
8288                   if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8289                     {
8290                       if (GET_CODE (otherops[2]) == CONST_INT)
8291                         {
8292                           switch ((int) INTVAL (otherops[2]))
8293                             {
8294                             case -8:
8295                               output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8296                               return "";
8297                             case -4:
8298                               output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8299                               return "";
8300                             case 4:
8301                               output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8302                               return "";
8303                             }
8304                         }
8305                       if (TARGET_LDRD
8306                           && (GET_CODE (otherops[2]) == REG
8307                               || (GET_CODE (otherops[2]) == CONST_INT
8308                                   && INTVAL (otherops[2]) > -256
8309                                   && INTVAL (otherops[2]) < 256)))
8310                         {
8311                           if (reg_overlap_mentioned_p (otherops[0],
8312                                                        otherops[2]))
8313                             {
8314                               /* Swap base and index registers over to
8315                                  avoid a conflict.  */
8316                               otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8317                               otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8318
8319                             }
8320                           /* If both registers conflict, it will usually
8321                              have been fixed by a splitter.  */
8322                           if (reg_overlap_mentioned_p (otherops[0],
8323                                                         otherops[2]))
8324                             {
8325                               output_asm_insn ("add%?\t%1, %1, %2", otherops);
8326                               output_asm_insn ("ldr%?d\t%0, [%1]",
8327                                                otherops);
8328                               return "";
8329                             }
8330                           else
8331                             {
8332                               output_asm_insn ("ldr%?d\t%0, [%1, %2]",
8333                                                otherops);
8334                               return "";
8335                             }
8336                         }
8337                       if (GET_CODE (otherops[2]) == CONST_INT)
8338                         {
8339                           if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8340                             output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8341                           else
8342                             output_asm_insn ("add%?\t%0, %1, %2", otherops);
8343                         }
8344                       else
8345                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
8346                     }
8347                   else
8348                     output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8349
8350                   return "ldm%?ia\t%0, %M0";
8351                 }
8352               else
8353                 {
8354                   otherops[1] = adjust_address (operands[1], SImode, 4);
8355                   /* Take care of overlapping base/data reg.  */
8356                   if (reg_mentioned_p (operands[0], operands[1]))
8357                     {
8358                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8359                       output_asm_insn ("ldr%?\t%0, %1", operands);
8360                     }
8361                   else
8362                     {
8363                       output_asm_insn ("ldr%?\t%0, %1", operands);
8364                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8365                     }
8366                 }
8367             }
8368         }
8369       else
8370         abort ();  /* Constraints should prevent this.  */
8371     }
8372   else if (code0 == MEM && code1 == REG)
8373     {
8374       if (REGNO (operands[1]) == IP_REGNUM)
8375         abort ();
8376
8377       switch (GET_CODE (XEXP (operands[0], 0)))
8378         {
8379         case REG:
8380           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8381           break;
8382
8383         case PRE_INC:
8384           if (!TARGET_LDRD)
8385             abort (); /* Should never happen now.  */
8386           output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8387           break;
8388
8389         case PRE_DEC:
8390           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8391           break;
8392
8393         case POST_INC:
8394           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8395           break;
8396
8397         case POST_DEC:
8398           if (!TARGET_LDRD)
8399             abort (); /* Should never happen now.  */
8400           output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8401           break;
8402
8403         case PRE_MODIFY:
8404         case POST_MODIFY:
8405           otherops[0] = operands[1];
8406           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8407           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8408
8409           if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8410             output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8411           else
8412             output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8413           break;
8414
8415         case PLUS:
8416           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8417           if (GET_CODE (otherops[2]) == CONST_INT)
8418             {
8419               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8420                 {
8421                 case -8:
8422                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
8423                   return "";
8424
8425                 case -4:
8426                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
8427                   return "";
8428
8429                 case 4:
8430                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8431                   return "";
8432                 }
8433             }
8434           if (TARGET_LDRD
8435               && (GET_CODE (otherops[2]) == REG
8436                   || (GET_CODE (otherops[2]) == CONST_INT
8437                       && INTVAL (otherops[2]) > -256
8438                       && INTVAL (otherops[2]) < 256)))
8439             {
8440               otherops[0] = operands[1];
8441               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8442               output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8443               return "";
8444             }
8445           /* Fall through */
8446
8447         default:
8448           otherops[0] = adjust_address (operands[0], SImode, 4);
8449           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8450           output_asm_insn ("str%?\t%1, %0", operands);
8451           output_asm_insn ("str%?\t%1, %0", otherops);
8452         }
8453     }
8454   else
8455     /* Constraints should prevent this.  */
8456     abort ();
8457
8458   return "";
8459 }
8460
8461
8462 /* Output an arbitrary MOV reg, #n.
8463    OPERANDS[0] is a register.  OPERANDS[1] is a const_int.  */
8464 const char *
8465 output_mov_immediate (rtx *operands)
8466 {
8467   HOST_WIDE_INT n = INTVAL (operands[1]);
8468
8469   /* Try to use one MOV.  */
8470   if (const_ok_for_arm (n))
8471     output_asm_insn ("mov%?\t%0, %1", operands);
8472
8473   /* Try to use one MVN.  */
8474   else if (const_ok_for_arm (~n))
8475     {
8476       operands[1] = GEN_INT (~n);
8477       output_asm_insn ("mvn%?\t%0, %1", operands);
8478     }
8479   else
8480     {
8481       int n_ones = 0;
8482       int i;
8483
8484       /* If all else fails, make it out of ORRs or BICs as appropriate.  */
8485       for (i = 0; i < 32; i++)
8486         if (n & 1 << i)
8487           n_ones++;
8488
8489       if (n_ones > 16)  /* Shorter to use MVN with BIC in this case.  */
8490         output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1, ~ n);
8491       else
8492         output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
8493     }
8494
8495   return "";
8496 }
8497
8498 /* Output an ADD r, s, #n where n may be too big for one instruction.
8499    If adding zero to one register, output nothing.  */
8500 const char *
8501 output_add_immediate (rtx *operands)
8502 {
8503   HOST_WIDE_INT n = INTVAL (operands[2]);
8504
8505   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8506     {
8507       if (n < 0)
8508         output_multi_immediate (operands,
8509                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8510                                 -n);
8511       else
8512         output_multi_immediate (operands,
8513                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8514                                 n);
8515     }
8516
8517   return "";
8518 }
8519
8520 /* Output a multiple immediate operation.
8521    OPERANDS is the vector of operands referred to in the output patterns.
8522    INSTR1 is the output pattern to use for the first constant.
8523    INSTR2 is the output pattern to use for subsequent constants.
8524    IMMED_OP is the index of the constant slot in OPERANDS.
8525    N is the constant value.  */
8526 static const char *
8527 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8528                         int immed_op, HOST_WIDE_INT n)
8529 {
8530 #if HOST_BITS_PER_WIDE_INT > 32
8531   n &= 0xffffffff;
8532 #endif
8533
8534   if (n == 0)
8535     {
8536       /* Quick and easy output.  */
8537       operands[immed_op] = const0_rtx;
8538       output_asm_insn (instr1, operands);
8539     }
8540   else
8541     {
8542       int i;
8543       const char * instr = instr1;
8544
8545       /* Note that n is never zero here (which would give no output).  */
8546       for (i = 0; i < 32; i += 2)
8547         {
8548           if (n & (3 << i))
8549             {
8550               operands[immed_op] = GEN_INT (n & (255 << i));
8551               output_asm_insn (instr, operands);
8552               instr = instr2;
8553               i += 6;
8554             }
8555         }
8556     }
8557
8558   return "";
8559 }
8560
8561 /* Return the appropriate ARM instruction for the operation code.
8562    The returned result should not be overwritten.  OP is the rtx of the
8563    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8564    was shifted.  */
8565 const char *
8566 arithmetic_instr (rtx op, int shift_first_arg)
8567 {
8568   switch (GET_CODE (op))
8569     {
8570     case PLUS:
8571       return "add";
8572
8573     case MINUS:
8574       return shift_first_arg ? "rsb" : "sub";
8575
8576     case IOR:
8577       return "orr";
8578
8579     case XOR:
8580       return "eor";
8581
8582     case AND:
8583       return "and";
8584
8585     default:
8586       abort ();
8587     }
8588 }
8589
8590 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8591    for the operation code.  The returned result should not be overwritten.
8592    OP is the rtx code of the shift.
8593    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8594    shift.  */
8595 static const char *
8596 shift_op (rtx op, HOST_WIDE_INT *amountp)
8597 {
8598   const char * mnem;
8599   enum rtx_code code = GET_CODE (op);
8600
8601   if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
8602     *amountp = -1;
8603   else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
8604     *amountp = INTVAL (XEXP (op, 1));
8605   else
8606     abort ();
8607
8608   switch (code)
8609     {
8610     case ASHIFT:
8611       mnem = "asl";
8612       break;
8613
8614     case ASHIFTRT:
8615       mnem = "asr";
8616       break;
8617
8618     case LSHIFTRT:
8619       mnem = "lsr";
8620       break;
8621
8622     case ROTATE:
8623       if (*amountp == -1)
8624         abort ();
8625       *amountp = 32 - *amountp;
8626
8627       /* Fall through.  */
8628
8629     case ROTATERT:
8630       mnem = "ror";
8631       break;
8632
8633     case MULT:
8634       /* We never have to worry about the amount being other than a
8635          power of 2, since this case can never be reloaded from a reg.  */
8636       if (*amountp != -1)
8637         *amountp = int_log2 (*amountp);
8638       else
8639         abort ();
8640       return "asl";
8641
8642     default:
8643       abort ();
8644     }
8645
8646   if (*amountp != -1)
8647     {
8648       /* This is not 100% correct, but follows from the desire to merge
8649          multiplication by a power of 2 with the recognizer for a
8650          shift.  >=32 is not a valid shift for "asl", so we must try and
8651          output a shift that produces the correct arithmetical result.
8652          Using lsr #32 is identical except for the fact that the carry bit
8653          is not set correctly if we set the flags; but we never use the
8654          carry bit from such an operation, so we can ignore that.  */
8655       if (code == ROTATERT)
8656         /* Rotate is just modulo 32.  */
8657         *amountp &= 31;
8658       else if (*amountp != (*amountp & 31))
8659         {
8660           if (code == ASHIFT)
8661             mnem = "lsr";
8662           *amountp = 32;
8663         }
8664
8665       /* Shifts of 0 are no-ops.  */
8666       if (*amountp == 0)
8667         return NULL;
8668     }
8669
8670   return mnem;
8671 }
8672
8673 /* Obtain the shift from the POWER of two.  */
8674
8675 static HOST_WIDE_INT
8676 int_log2 (HOST_WIDE_INT power)
8677 {
8678   HOST_WIDE_INT shift = 0;
8679
8680   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8681     {
8682       if (shift > 31)
8683         abort ();
8684       shift++;
8685     }
8686
8687   return shift;
8688 }
8689
8690 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
8691    because /bin/as is horribly restrictive.  The judgement about
8692    whether or not each character is 'printable' (and can be output as
8693    is) or not (and must be printed with an octal escape) must be made
8694    with reference to the *host* character set -- the situation is
8695    similar to that discussed in the comments above pp_c_char in
8696    c-pretty-print.c.  */
8697
8698 #define MAX_ASCII_LEN 51
8699
8700 void
8701 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8702 {
8703   int i;
8704   int len_so_far = 0;
8705
8706   fputs ("\t.ascii\t\"", stream);
8707
8708   for (i = 0; i < len; i++)
8709     {
8710       int c = p[i];
8711
8712       if (len_so_far >= MAX_ASCII_LEN)
8713         {
8714           fputs ("\"\n\t.ascii\t\"", stream);
8715           len_so_far = 0;
8716         }
8717
8718       if (ISPRINT (c))
8719         {
8720           if (c == '\\' || c == '\"')
8721             {
8722               putc ('\\', stream);
8723               len_so_far++;
8724             }
8725           putc (c, stream);
8726           len_so_far++;
8727         }
8728       else
8729         {
8730           fprintf (stream, "\\%03o", c);
8731           len_so_far += 4;
8732         }
8733     }
8734
8735   fputs ("\"\n", stream);
8736 }
8737 \f
8738 /* Compute the register save mask for registers 0 through 12
8739    inclusive.  This code is used by arm_compute_save_reg_mask.  */
8740
8741 static unsigned long
8742 arm_compute_save_reg0_reg12_mask (void)
8743 {
8744   unsigned long func_type = arm_current_func_type ();
8745   unsigned long save_reg_mask = 0;
8746   unsigned int reg;
8747
8748   if (IS_INTERRUPT (func_type))
8749     {
8750       unsigned int max_reg;
8751       /* Interrupt functions must not corrupt any registers,
8752          even call clobbered ones.  If this is a leaf function
8753          we can just examine the registers used by the RTL, but
8754          otherwise we have to assume that whatever function is
8755          called might clobber anything, and so we have to save
8756          all the call-clobbered registers as well.  */
8757       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8758         /* FIQ handlers have registers r8 - r12 banked, so
8759            we only need to check r0 - r7, Normal ISRs only
8760            bank r14 and r15, so we must check up to r12.
8761            r13 is the stack pointer which is always preserved,
8762            so we do not need to consider it here.  */
8763         max_reg = 7;
8764       else
8765         max_reg = 12;
8766
8767       for (reg = 0; reg <= max_reg; reg++)
8768         if (regs_ever_live[reg]
8769             || (! current_function_is_leaf && call_used_regs [reg]))
8770           save_reg_mask |= (1 << reg);
8771
8772       /* Also save the pic base register if necessary.  */
8773       if (flag_pic
8774           && !TARGET_SINGLE_PIC_BASE
8775           && current_function_uses_pic_offset_table)
8776         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8777     }
8778   else
8779     {
8780       /* In the normal case we only need to save those registers
8781          which are call saved and which are used by this function.  */
8782       for (reg = 0; reg <= 10; reg++)
8783         if (regs_ever_live[reg] && ! call_used_regs [reg])
8784           save_reg_mask |= (1 << reg);
8785
8786       /* Handle the frame pointer as a special case.  */
8787       if (! TARGET_APCS_FRAME
8788           && ! frame_pointer_needed
8789           && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8790           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8791         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8792
8793       /* If we aren't loading the PIC register,
8794          don't stack it even though it may be live.  */
8795       if (flag_pic
8796           && !TARGET_SINGLE_PIC_BASE 
8797           && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
8798               || current_function_uses_pic_offset_table))
8799         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8800     }
8801
8802   /* Save registers so the exception handler can modify them.  */
8803   if (current_function_calls_eh_return)
8804     {
8805       unsigned int i;
8806
8807       for (i = 0; ; i++)
8808         {
8809           reg = EH_RETURN_DATA_REGNO (i);
8810           if (reg == INVALID_REGNUM)
8811             break;
8812           save_reg_mask |= 1 << reg;
8813         }
8814     }
8815
8816   return save_reg_mask;
8817 }
8818
8819 /* Compute a bit mask of which registers need to be
8820    saved on the stack for the current function.  */
8821
8822 static unsigned long
8823 arm_compute_save_reg_mask (void)
8824 {
8825   unsigned int save_reg_mask = 0;
8826   unsigned long func_type = arm_current_func_type ();
8827
8828   if (IS_NAKED (func_type))
8829     /* This should never really happen.  */
8830     return 0;
8831
8832   /* If we are creating a stack frame, then we must save the frame pointer,
8833      IP (which will hold the old stack pointer), LR and the PC.  */
8834   if (frame_pointer_needed)
8835     save_reg_mask |=
8836       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8837       | (1 << IP_REGNUM)
8838       | (1 << LR_REGNUM)
8839       | (1 << PC_REGNUM);
8840
8841   /* Volatile functions do not return, so there
8842      is no need to save any other registers.  */
8843   if (IS_VOLATILE (func_type))
8844     return save_reg_mask;
8845
8846   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8847
8848   /* Decide if we need to save the link register.
8849      Interrupt routines have their own banked link register,
8850      so they never need to save it.
8851      Otherwise if we do not use the link register we do not need to save
8852      it.  If we are pushing other registers onto the stack however, we
8853      can save an instruction in the epilogue by pushing the link register
8854      now and then popping it back into the PC.  This incurs extra memory
8855      accesses though, so we only do it when optimizing for size, and only
8856      if we know that we will not need a fancy return sequence.  */
8857   if (regs_ever_live [LR_REGNUM]
8858           || (save_reg_mask
8859               && optimize_size
8860               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
8861               && !current_function_calls_eh_return))
8862     save_reg_mask |= 1 << LR_REGNUM;
8863
8864   if (cfun->machine->lr_save_eliminated)
8865     save_reg_mask &= ~ (1 << LR_REGNUM);
8866
8867   if (TARGET_REALLY_IWMMXT
8868       && ((bit_count (save_reg_mask)
8869            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8870     {
8871       unsigned int reg;
8872
8873       /* The total number of registers that are going to be pushed
8874          onto the stack is odd.  We need to ensure that the stack
8875          is 64-bit aligned before we start to save iWMMXt registers,
8876          and also before we start to create locals.  (A local variable
8877          might be a double or long long which we will load/store using
8878          an iWMMXt instruction).  Therefore we need to push another
8879          ARM register, so that the stack will be 64-bit aligned.  We
8880          try to avoid using the arg registers (r0 -r3) as they might be
8881          used to pass values in a tail call.  */
8882       for (reg = 4; reg <= 12; reg++)
8883         if ((save_reg_mask & (1 << reg)) == 0)
8884           break;
8885
8886       if (reg <= 12)
8887         save_reg_mask |= (1 << reg);
8888       else
8889         {
8890           cfun->machine->sibcall_blocked = 1;
8891           save_reg_mask |= (1 << 3);
8892         }
8893     }
8894
8895   return save_reg_mask;
8896 }
8897
8898
8899 /* Compute a bit mask of which registers need to be
8900    saved on the stack for the current function.  */
8901 static unsigned long
8902 thumb_compute_save_reg_mask (void)
8903 {
8904   unsigned long mask;
8905   unsigned reg;
8906
8907   mask = 0;
8908   for (reg = 0; reg < 12; reg ++)
8909     if (regs_ever_live[reg] && !call_used_regs[reg])
8910       mask |= 1 << reg;
8911
8912   if (flag_pic && !TARGET_SINGLE_PIC_BASE)
8913     mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
8914
8915   if (TARGET_SINGLE_PIC_BASE)
8916     mask &= ~(1 << arm_pic_register);
8917
8918   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
8919   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
8920     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
8921
8922   /* LR will also be pushed if any lo regs are pushed.  */
8923   if (mask & 0xff || thumb_force_lr_save ())
8924     mask |= (1 << LR_REGNUM);
8925
8926   /* Make sure we have a low work register if we need one.
8927      We will need one if we are going to push a high register,
8928      but we are not currently intending to push a low register.  */
8929   if ((mask & 0xff) == 0
8930       && ((mask & 0x0f00) || TARGET_BACKTRACE))
8931     {
8932       /* Use thumb_find_work_register to choose which register
8933          we will use.  If the register is live then we will
8934          have to push it.  Use LAST_LO_REGNUM as our fallback
8935          choice for the register to select.  */
8936       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
8937
8938       if (! call_used_regs[reg])
8939         mask |= 1 << reg;
8940     }
8941
8942   return mask;
8943 }
8944
8945
8946 /* Return the number of bytes required to save VFP registers.  */
8947 static int
8948 arm_get_vfp_saved_size (void)
8949 {
8950   unsigned int regno;
8951   int count;
8952   int saved;
8953
8954   saved = 0;
8955   /* Space for saved VFP registers.  */
8956   if (TARGET_HARD_FLOAT && TARGET_VFP)
8957     {
8958       count = 0;
8959       for (regno = FIRST_VFP_REGNUM;
8960            regno < LAST_VFP_REGNUM;
8961            regno += 2)
8962         {
8963           if ((!regs_ever_live[regno] || call_used_regs[regno])
8964               && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8965             {
8966               if (count > 0)
8967                 {
8968                   /* Workaround ARM10 VFPr1 bug.  */
8969                   if (count == 2 && !arm_arch6)
8970                     count++;
8971                   saved += count * 8 + 4;
8972                 }
8973               count = 0;
8974             }
8975           else
8976             count++;
8977         }
8978       if (count > 0)
8979         {
8980           if (count == 2 && !arm_arch6)
8981             count++;
8982           saved += count * 8 + 4;
8983         }
8984     }
8985   return saved;
8986 }
8987
8988
8989 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
8990    everything bar the final return instruction.  */
8991 const char *
8992 output_return_instruction (rtx operand, int really_return, int reverse)
8993 {
8994   char conditional[10];
8995   char instr[100];
8996   unsigned reg;
8997   unsigned long live_regs_mask;
8998   unsigned long func_type;
8999   arm_stack_offsets *offsets;
9000
9001   func_type = arm_current_func_type ();
9002
9003   if (IS_NAKED (func_type))
9004     return "";
9005
9006   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9007     {
9008       /* If this function was declared non-returning, and we have
9009          found a tail call, then we have to trust that the called
9010          function won't return.  */
9011       if (really_return)
9012         {
9013           rtx ops[2];
9014
9015           /* Otherwise, trap an attempted return by aborting.  */
9016           ops[0] = operand;
9017           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
9018                                        : "abort");
9019           assemble_external_libcall (ops[1]);
9020           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9021         }
9022
9023       return "";
9024     }
9025
9026   if (current_function_calls_alloca && !really_return)
9027     abort ();
9028
9029   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9030
9031   return_used_this_function = 1;
9032
9033   live_regs_mask = arm_compute_save_reg_mask ();
9034
9035   if (live_regs_mask)
9036     {
9037       const char * return_reg;
9038
9039       /* If we do not have any special requirements for function exit
9040          (e.g. interworking, or ISR) then we can load the return address
9041          directly into the PC.  Otherwise we must load it into LR.  */
9042       if (really_return
9043           && ! TARGET_INTERWORK)
9044         return_reg = reg_names[PC_REGNUM];
9045       else
9046         return_reg = reg_names[LR_REGNUM];
9047
9048       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9049         {
9050           /* There are three possible reasons for the IP register
9051              being saved.  1) a stack frame was created, in which case
9052              IP contains the old stack pointer, or 2) an ISR routine
9053              corrupted it, or 3) it was saved to align the stack on
9054              iWMMXt.  In case 1, restore IP into SP, otherwise just
9055              restore IP.  */
9056           if (frame_pointer_needed)
9057             {
9058               live_regs_mask &= ~ (1 << IP_REGNUM);
9059               live_regs_mask |=   (1 << SP_REGNUM);
9060             }
9061           else
9062             {
9063               if (! IS_INTERRUPT (func_type)
9064                   && ! TARGET_REALLY_IWMMXT)
9065                 abort ();
9066             }
9067         }
9068
9069       /* On some ARM architectures it is faster to use LDR rather than
9070          LDM to load a single register.  On other architectures, the
9071          cost is the same.  In 26 bit mode, or for exception handlers,
9072          we have to use LDM to load the PC so that the CPSR is also
9073          restored.  */
9074       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9075         if (live_regs_mask == (1U << reg))
9076           break;
9077
9078       if (reg <= LAST_ARM_REGNUM
9079           && (reg != LR_REGNUM
9080               || ! really_return
9081               || ! IS_INTERRUPT (func_type)))
9082         {
9083           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
9084                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9085         }
9086       else
9087         {
9088           char *p;
9089           int first = 1;
9090
9091           /* Generate the load multiple instruction to restore the
9092              registers.  Note we can get here, even if
9093              frame_pointer_needed is true, but only if sp already
9094              points to the base of the saved core registers.  */
9095           if (live_regs_mask & (1 << SP_REGNUM))
9096             {
9097               unsigned HOST_WIDE_INT stack_adjust;
9098
9099               offsets = arm_get_frame_offsets ();
9100               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9101               if (stack_adjust != 0 && stack_adjust != 4)
9102                 abort ();
9103
9104               if (stack_adjust && arm_arch5)
9105                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9106               else
9107                 {
9108                   /* If we can't use ldmib (SA110 bug),
9109                      then try to pop r3 instead.  */
9110                   if (stack_adjust)
9111                     live_regs_mask |= 1 << 3;
9112                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9113                 }
9114             }
9115           else
9116             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9117
9118           p = instr + strlen (instr);
9119
9120           for (reg = 0; reg <= SP_REGNUM; reg++)
9121             if (live_regs_mask & (1 << reg))
9122               {
9123                 int l = strlen (reg_names[reg]);
9124
9125                 if (first)
9126                   first = 0;
9127                 else
9128                   {
9129                     memcpy (p, ", ", 2);
9130                     p += 2;
9131                   }
9132
9133                 memcpy (p, "%|", 2);
9134                 memcpy (p + 2, reg_names[reg], l);
9135                 p += l + 2;
9136               }
9137
9138           if (live_regs_mask & (1 << LR_REGNUM))
9139             {
9140               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9141               /* If returning from an interrupt, restore the CPSR.  */
9142               if (IS_INTERRUPT (func_type))
9143                 strcat (p, "^");
9144             }
9145           else
9146             strcpy (p, "}");
9147         }
9148
9149       output_asm_insn (instr, & operand);
9150
9151       /* See if we need to generate an extra instruction to
9152          perform the actual function return.  */
9153       if (really_return
9154           && func_type != ARM_FT_INTERWORKED
9155           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9156         {
9157           /* The return has already been handled
9158              by loading the LR into the PC.  */
9159           really_return = 0;
9160         }
9161     }
9162
9163   if (really_return)
9164     {
9165       switch ((int) ARM_FUNC_TYPE (func_type))
9166         {
9167         case ARM_FT_ISR:
9168         case ARM_FT_FIQ:
9169           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9170           break;
9171
9172         case ARM_FT_INTERWORKED:
9173           sprintf (instr, "bx%s\t%%|lr", conditional);
9174           break;
9175
9176         case ARM_FT_EXCEPTION:
9177           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9178           break;
9179
9180         default:
9181           /* Use bx if it's available.  */
9182           if (arm_arch5 || arm_arch4t)
9183             sprintf (instr, "bx%s\t%%|lr", conditional);
9184           else
9185             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9186           break;
9187         }
9188
9189       output_asm_insn (instr, & operand);
9190     }
9191
9192   return "";
9193 }
9194
9195 /* Write the function name into the code section, directly preceding
9196    the function prologue.
9197
9198    Code will be output similar to this:
9199      t0
9200          .ascii "arm_poke_function_name", 0
9201          .align
9202      t1
9203          .word 0xff000000 + (t1 - t0)
9204      arm_poke_function_name
9205          mov     ip, sp
9206          stmfd   sp!, {fp, ip, lr, pc}
9207          sub     fp, ip, #4
9208
9209    When performing a stack backtrace, code can inspect the value
9210    of 'pc' stored at 'fp' + 0.  If the trace function then looks
9211    at location pc - 12 and the top 8 bits are set, then we know
9212    that there is a function name embedded immediately preceding this
9213    location and has length ((pc[-3]) & 0xff000000).
9214
9215    We assume that pc is declared as a pointer to an unsigned long.
9216
9217    It is of no benefit to output the function name if we are assembling
9218    a leaf function.  These function types will not contain a stack
9219    backtrace structure, therefore it is not possible to determine the
9220    function name.  */
9221 void
9222 arm_poke_function_name (FILE *stream, const char *name)
9223 {
9224   unsigned long alignlength;
9225   unsigned long length;
9226   rtx           x;
9227
9228   length      = strlen (name) + 1;
9229   alignlength = ROUND_UP_WORD (length);
9230
9231   ASM_OUTPUT_ASCII (stream, name, length);
9232   ASM_OUTPUT_ALIGN (stream, 2);
9233   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9234   assemble_aligned_integer (UNITS_PER_WORD, x);
9235 }
9236
9237 /* Place some comments into the assembler stream
9238    describing the current function.  */
9239 static void
9240 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9241 {
9242   unsigned long func_type;
9243
9244   if (!TARGET_ARM)
9245     {
9246       thumb_output_function_prologue (f, frame_size);
9247       return;
9248     }
9249
9250   /* Sanity check.  */
9251   if (arm_ccfsm_state || arm_target_insn)
9252     abort ();
9253
9254   func_type = arm_current_func_type ();
9255
9256   switch ((int) ARM_FUNC_TYPE (func_type))
9257     {
9258     default:
9259     case ARM_FT_NORMAL:
9260       break;
9261     case ARM_FT_INTERWORKED:
9262       asm_fprintf (f, "\t%@ Function supports interworking.\n");
9263       break;
9264     case ARM_FT_ISR:
9265       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9266       break;
9267     case ARM_FT_FIQ:
9268       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9269       break;
9270     case ARM_FT_EXCEPTION:
9271       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9272       break;
9273     }
9274
9275   if (IS_NAKED (func_type))
9276     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9277
9278   if (IS_VOLATILE (func_type))
9279     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9280
9281   if (IS_NESTED (func_type))
9282     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9283
9284   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9285                current_function_args_size,
9286                current_function_pretend_args_size, frame_size);
9287
9288   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9289                frame_pointer_needed,
9290                cfun->machine->uses_anonymous_args);
9291
9292   if (cfun->machine->lr_save_eliminated)
9293     asm_fprintf (f, "\t%@ link register save eliminated.\n");
9294
9295   if (current_function_calls_eh_return)
9296     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9297
9298 #ifdef AOF_ASSEMBLER
9299   if (flag_pic)
9300     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9301 #endif
9302
9303   return_used_this_function = 0;
9304 }
9305
9306 const char *
9307 arm_output_epilogue (rtx sibling)
9308 {
9309   int reg;
9310   unsigned long saved_regs_mask;
9311   unsigned long func_type;
9312   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
9313      frame that is $fp + 4 for a non-variadic function.  */
9314   int floats_offset = 0;
9315   rtx operands[3];
9316   FILE * f = asm_out_file;
9317   unsigned int lrm_count = 0;
9318   int really_return = (sibling == NULL);
9319   int start_reg;
9320   arm_stack_offsets *offsets;
9321
9322   /* If we have already generated the return instruction
9323      then it is futile to generate anything else.  */
9324   if (use_return_insn (FALSE, sibling) && return_used_this_function)
9325     return "";
9326
9327   func_type = arm_current_func_type ();
9328
9329   if (IS_NAKED (func_type))
9330     /* Naked functions don't have epilogues.  */
9331     return "";
9332
9333   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9334     {
9335       rtx op;
9336
9337       /* A volatile function should never return.  Call abort.  */
9338       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9339       assemble_external_libcall (op);
9340       output_asm_insn ("bl\t%a0", &op);
9341
9342       return "";
9343     }
9344
9345   if (current_function_calls_eh_return
9346       && ! really_return)
9347     /* If we are throwing an exception, then we really must
9348        be doing a return,  so we can't tail-call.  */
9349     abort ();
9350
9351   offsets = arm_get_frame_offsets ();
9352   saved_regs_mask = arm_compute_save_reg_mask ();
9353
9354   if (TARGET_IWMMXT)
9355     lrm_count = bit_count (saved_regs_mask);
9356
9357   floats_offset = offsets->saved_args;
9358   /* Compute how far away the floats will be.  */
9359   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9360     if (saved_regs_mask & (1 << reg))
9361       floats_offset += 4;
9362
9363   if (frame_pointer_needed)
9364     {
9365       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
9366       int vfp_offset = offsets->frame;
9367
9368       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9369         {
9370           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9371             if (regs_ever_live[reg] && !call_used_regs[reg])
9372               {
9373                 floats_offset += 12;
9374                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9375                              reg, FP_REGNUM, floats_offset - vfp_offset);
9376               }
9377         }
9378       else
9379         {
9380           start_reg = LAST_FPA_REGNUM;
9381
9382           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9383             {
9384               if (regs_ever_live[reg] && !call_used_regs[reg])
9385                 {
9386                   floats_offset += 12;
9387
9388                   /* We can't unstack more than four registers at once.  */
9389                   if (start_reg - reg == 3)
9390                     {
9391                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9392                                    reg, FP_REGNUM, floats_offset - vfp_offset);
9393                       start_reg = reg - 1;
9394                     }
9395                 }
9396               else
9397                 {
9398                   if (reg != start_reg)
9399                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9400                                  reg + 1, start_reg - reg,
9401                                  FP_REGNUM, floats_offset - vfp_offset);
9402                   start_reg = reg - 1;
9403                 }
9404             }
9405
9406           /* Just in case the last register checked also needs unstacking.  */
9407           if (reg != start_reg)
9408             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9409                          reg + 1, start_reg - reg,
9410                          FP_REGNUM, floats_offset - vfp_offset);
9411         }
9412
9413       if (TARGET_HARD_FLOAT && TARGET_VFP)
9414         {
9415           int saved_size;
9416
9417           /* The fldmx insn does not have base+offset addressing modes,
9418              so we use IP to hold the address.  */
9419           saved_size = arm_get_vfp_saved_size ();
9420
9421           if (saved_size > 0)
9422             {
9423               floats_offset += saved_size;
9424               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9425                            FP_REGNUM, floats_offset - vfp_offset);
9426             }
9427           start_reg = FIRST_VFP_REGNUM;
9428           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9429             {
9430               if ((!regs_ever_live[reg] || call_used_regs[reg])
9431                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9432                 {
9433                   if (start_reg != reg)
9434                     arm_output_fldmx (f, IP_REGNUM,
9435                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9436                                       (reg - start_reg) / 2);
9437                   start_reg = reg + 2;
9438                 }
9439             }
9440           if (start_reg != reg)
9441             arm_output_fldmx (f, IP_REGNUM,
9442                               (start_reg - FIRST_VFP_REGNUM) / 2,
9443                               (reg - start_reg) / 2);
9444         }
9445
9446       if (TARGET_IWMMXT)
9447         {
9448           /* The frame pointer is guaranteed to be non-double-word aligned.
9449              This is because it is set to (old_stack_pointer - 4) and the
9450              old_stack_pointer was double word aligned.  Thus the offset to
9451              the iWMMXt registers to be loaded must also be non-double-word
9452              sized, so that the resultant address *is* double-word aligned.
9453              We can ignore floats_offset since that was already included in
9454              the live_regs_mask.  */
9455           lrm_count += (lrm_count % 2 ? 2 : 1);
9456
9457           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9458             if (regs_ever_live[reg] && !call_used_regs[reg])
9459               {
9460                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9461                              reg, FP_REGNUM, lrm_count * 4);
9462                 lrm_count += 2;
9463               }
9464         }
9465
9466       /* saved_regs_mask should contain the IP, which at the time of stack
9467          frame generation actually contains the old stack pointer.  So a
9468          quick way to unwind the stack is just pop the IP register directly
9469          into the stack pointer.  */
9470       if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
9471         abort ();
9472       saved_regs_mask &= ~ (1 << IP_REGNUM);
9473       saved_regs_mask |=   (1 << SP_REGNUM);
9474
9475       /* There are two registers left in saved_regs_mask - LR and PC.  We
9476          only need to restore the LR register (the return address), but to
9477          save time we can load it directly into the PC, unless we need a
9478          special function exit sequence, or we are not really returning.  */
9479       if (really_return
9480           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9481           && !current_function_calls_eh_return)
9482         /* Delete the LR from the register mask, so that the LR on
9483            the stack is loaded into the PC in the register mask.  */
9484         saved_regs_mask &= ~ (1 << LR_REGNUM);
9485       else
9486         saved_regs_mask &= ~ (1 << PC_REGNUM);
9487
9488       /* We must use SP as the base register, because SP is one of the
9489          registers being restored.  If an interrupt or page fault
9490          happens in the ldm instruction, the SP might or might not
9491          have been restored.  That would be bad, as then SP will no
9492          longer indicate the safe area of stack, and we can get stack
9493          corruption.  Using SP as the base register means that it will
9494          be reset correctly to the original value, should an interrupt
9495          occur.  If the stack pointer already points at the right
9496          place, then omit the subtraction.  */
9497       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9498           || current_function_calls_alloca)
9499         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9500                      4 * bit_count (saved_regs_mask));
9501       print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9502
9503       if (IS_INTERRUPT (func_type))
9504         /* Interrupt handlers will have pushed the
9505            IP onto the stack, so restore it now.  */
9506         print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9507     }
9508   else
9509     {
9510       /* Restore stack pointer if necessary.  */
9511       if (offsets->outgoing_args != offsets->saved_regs)
9512         {
9513           operands[0] = operands[1] = stack_pointer_rtx;
9514           operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9515           output_add_immediate (operands);
9516         }
9517
9518       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9519         {
9520           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9521             if (regs_ever_live[reg] && !call_used_regs[reg])
9522               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9523                            reg, SP_REGNUM);
9524         }
9525       else
9526         {
9527           start_reg = FIRST_FPA_REGNUM;
9528
9529           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9530             {
9531               if (regs_ever_live[reg] && !call_used_regs[reg])
9532                 {
9533                   if (reg - start_reg == 3)
9534                     {
9535                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9536                                    start_reg, SP_REGNUM);
9537                       start_reg = reg + 1;
9538                     }
9539                 }
9540               else
9541                 {
9542                   if (reg != start_reg)
9543                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9544                                  start_reg, reg - start_reg,
9545                                  SP_REGNUM);
9546
9547                   start_reg = reg + 1;
9548                 }
9549             }
9550
9551           /* Just in case the last register checked also needs unstacking.  */
9552           if (reg != start_reg)
9553             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9554                          start_reg, reg - start_reg, SP_REGNUM);
9555         }
9556
9557       if (TARGET_HARD_FLOAT && TARGET_VFP)
9558         {
9559           start_reg = FIRST_VFP_REGNUM;
9560           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9561             {
9562               if ((!regs_ever_live[reg] || call_used_regs[reg])
9563                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9564                 {
9565                   if (start_reg != reg)
9566                     arm_output_fldmx (f, SP_REGNUM,
9567                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9568                                       (reg - start_reg) / 2);
9569                   start_reg = reg + 2;
9570                 }
9571             }
9572           if (start_reg != reg)
9573             arm_output_fldmx (f, SP_REGNUM,
9574                               (start_reg - FIRST_VFP_REGNUM) / 2,
9575                               (reg - start_reg) / 2);
9576         }
9577       if (TARGET_IWMMXT)
9578         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9579           if (regs_ever_live[reg] && !call_used_regs[reg])
9580             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9581
9582       /* If we can, restore the LR into the PC.  */
9583       if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9584           && really_return
9585           && current_function_pretend_args_size == 0
9586           && saved_regs_mask & (1 << LR_REGNUM)
9587           && !current_function_calls_eh_return)
9588         {
9589           saved_regs_mask &= ~ (1 << LR_REGNUM);
9590           saved_regs_mask |=   (1 << PC_REGNUM);
9591         }
9592
9593       /* Load the registers off the stack.  If we only have one register
9594          to load use the LDR instruction - it is faster.  */
9595       if (saved_regs_mask == (1 << LR_REGNUM))
9596         {
9597           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9598         }
9599       else if (saved_regs_mask)
9600         {
9601           if (saved_regs_mask & (1 << SP_REGNUM))
9602             /* Note - write back to the stack register is not enabled
9603                (i.e. "ldmfd sp!...").  We know that the stack pointer is
9604                in the list of registers and if we add writeback the
9605                instruction becomes UNPREDICTABLE.  */
9606             print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9607           else
9608             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9609         }
9610
9611       if (current_function_pretend_args_size)
9612         {
9613           /* Unwind the pre-pushed regs.  */
9614           operands[0] = operands[1] = stack_pointer_rtx;
9615           operands[2] = GEN_INT (current_function_pretend_args_size);
9616           output_add_immediate (operands);
9617         }
9618     }
9619
9620   /* We may have already restored PC directly from the stack.  */
9621   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
9622     return "";
9623
9624   /* Stack adjustment for exception handler.  */
9625   if (current_function_calls_eh_return)
9626     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
9627                  ARM_EH_STACKADJ_REGNUM);
9628
9629   /* Generate the return instruction.  */
9630   switch ((int) ARM_FUNC_TYPE (func_type))
9631     {
9632     case ARM_FT_ISR:
9633     case ARM_FT_FIQ:
9634       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9635       break;
9636
9637     case ARM_FT_EXCEPTION:
9638       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9639       break;
9640
9641     case ARM_FT_INTERWORKED:
9642       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9643       break;
9644
9645     default:
9646       if (arm_arch5 || arm_arch4t)
9647         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9648       else
9649         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9650       break;
9651     }
9652
9653   return "";
9654 }
9655
9656 static void
9657 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9658                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9659 {
9660   arm_stack_offsets *offsets;
9661
9662   if (TARGET_THUMB)
9663     {
9664       int regno;
9665
9666       /* Emit any call-via-reg trampolines that are needed for v4t support
9667          of call_reg and call_value_reg type insns.  */
9668       for (regno = 0; regno < SP_REGNUM; regno++)
9669         {
9670           rtx label = cfun->machine->call_via[regno];
9671
9672           if (label != NULL)
9673             {
9674               function_section (current_function_decl);
9675               targetm.asm_out.internal_label (asm_out_file, "L",
9676                                               CODE_LABEL_NUMBER (label));
9677               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
9678             }
9679         }
9680
9681       /* ??? Probably not safe to set this here, since it assumes that a
9682          function will be emitted as assembly immediately after we generate
9683          RTL for it.  This does not happen for inline functions.  */
9684       return_used_this_function = 0;
9685     }
9686   else
9687     {
9688       /* We need to take into account any stack-frame rounding.  */
9689       offsets = arm_get_frame_offsets ();
9690
9691       if (use_return_insn (FALSE, NULL)
9692           && return_used_this_function
9693           && offsets->saved_regs != offsets->outgoing_args
9694           && !frame_pointer_needed)
9695         abort ();
9696
9697       /* Reset the ARM-specific per-function variables.  */
9698       after_arm_reorg = 0;
9699     }
9700 }
9701
9702 /* Generate and emit an insn that we will recognize as a push_multi.
9703    Unfortunately, since this insn does not reflect very well the actual
9704    semantics of the operation, we need to annotate the insn for the benefit
9705    of DWARF2 frame unwind information.  */
9706 static rtx
9707 emit_multi_reg_push (unsigned long mask)
9708 {
9709   int num_regs = 0;
9710   int num_dwarf_regs;
9711   int i, j;
9712   rtx par;
9713   rtx dwarf;
9714   int dwarf_par_index;
9715   rtx tmp, reg;
9716
9717   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9718     if (mask & (1 << i))
9719       num_regs++;
9720
9721   if (num_regs == 0 || num_regs > 16)
9722     abort ();
9723
9724   /* We don't record the PC in the dwarf frame information.  */
9725   num_dwarf_regs = num_regs;
9726   if (mask & (1 << PC_REGNUM))
9727     num_dwarf_regs--;
9728
9729   /* For the body of the insn we are going to generate an UNSPEC in
9730      parallel with several USEs.  This allows the insn to be recognized
9731      by the push_multi pattern in the arm.md file.  The insn looks
9732      something like this:
9733
9734        (parallel [
9735            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9736                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9737            (use (reg:SI 11 fp))
9738            (use (reg:SI 12 ip))
9739            (use (reg:SI 14 lr))
9740            (use (reg:SI 15 pc))
9741         ])
9742
9743      For the frame note however, we try to be more explicit and actually
9744      show each register being stored into the stack frame, plus a (single)
9745      decrement of the stack pointer.  We do it this way in order to be
9746      friendly to the stack unwinding code, which only wants to see a single
9747      stack decrement per instruction.  The RTL we generate for the note looks
9748      something like this:
9749
9750       (sequence [
9751            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9752            (set (mem:SI (reg:SI sp)) (reg:SI r4))
9753            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9754            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9755            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9756         ])
9757
9758       This sequence is used both by the code to support stack unwinding for
9759       exceptions handlers and the code to generate dwarf2 frame debugging.  */
9760
9761   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9762   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9763   dwarf_par_index = 1;
9764
9765   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9766     {
9767       if (mask & (1 << i))
9768         {
9769           reg = gen_rtx_REG (SImode, i);
9770
9771           XVECEXP (par, 0, 0)
9772             = gen_rtx_SET (VOIDmode,
9773                            gen_rtx_MEM (BLKmode,
9774                                         gen_rtx_PRE_DEC (BLKmode,
9775                                                          stack_pointer_rtx)),
9776                            gen_rtx_UNSPEC (BLKmode,
9777                                            gen_rtvec (1, reg),
9778                                            UNSPEC_PUSH_MULT));
9779
9780           if (i != PC_REGNUM)
9781             {
9782               tmp = gen_rtx_SET (VOIDmode,
9783                                  gen_rtx_MEM (SImode, stack_pointer_rtx),
9784                                  reg);
9785               RTX_FRAME_RELATED_P (tmp) = 1;
9786               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9787               dwarf_par_index++;
9788             }
9789
9790           break;
9791         }
9792     }
9793
9794   for (j = 1, i++; j < num_regs; i++)
9795     {
9796       if (mask & (1 << i))
9797         {
9798           reg = gen_rtx_REG (SImode, i);
9799
9800           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9801
9802           if (i != PC_REGNUM)
9803             {
9804               tmp = gen_rtx_SET (VOIDmode,
9805                                  gen_rtx_MEM (SImode,
9806                                               plus_constant (stack_pointer_rtx,
9807                                                              4 * j)),
9808                                  reg);
9809               RTX_FRAME_RELATED_P (tmp) = 1;
9810               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9811             }
9812
9813           j++;
9814         }
9815     }
9816
9817   par = emit_insn (par);
9818
9819   tmp = gen_rtx_SET (SImode,
9820                      stack_pointer_rtx,
9821                      gen_rtx_PLUS (SImode,
9822                                    stack_pointer_rtx,
9823                                    GEN_INT (-4 * num_regs)));
9824   RTX_FRAME_RELATED_P (tmp) = 1;
9825   XVECEXP (dwarf, 0, 0) = tmp;
9826
9827   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9828                                        REG_NOTES (par));
9829   return par;
9830 }
9831
9832 static rtx
9833 emit_sfm (int base_reg, int count)
9834 {
9835   rtx par;
9836   rtx dwarf;
9837   rtx tmp, reg;
9838   int i;
9839
9840   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9841   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9842
9843   reg = gen_rtx_REG (XFmode, base_reg++);
9844
9845   XVECEXP (par, 0, 0)
9846     = gen_rtx_SET (VOIDmode,
9847                    gen_rtx_MEM (BLKmode,
9848                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9849                    gen_rtx_UNSPEC (BLKmode,
9850                                    gen_rtvec (1, reg),
9851                                    UNSPEC_PUSH_MULT));
9852   tmp = gen_rtx_SET (VOIDmode,
9853                      gen_rtx_MEM (XFmode, stack_pointer_rtx), reg);
9854   RTX_FRAME_RELATED_P (tmp) = 1;
9855   XVECEXP (dwarf, 0, 1) = tmp;
9856
9857   for (i = 1; i < count; i++)
9858     {
9859       reg = gen_rtx_REG (XFmode, base_reg++);
9860       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9861
9862       tmp = gen_rtx_SET (VOIDmode,
9863                          gen_rtx_MEM (XFmode,
9864                                       plus_constant (stack_pointer_rtx,
9865                                                      i * 12)),
9866                          reg);
9867       RTX_FRAME_RELATED_P (tmp) = 1;
9868       XVECEXP (dwarf, 0, i + 1) = tmp;
9869     }
9870
9871   tmp = gen_rtx_SET (VOIDmode,
9872                      stack_pointer_rtx,
9873                      gen_rtx_PLUS (SImode,
9874                                    stack_pointer_rtx,
9875                                    GEN_INT (-12 * count)));
9876   RTX_FRAME_RELATED_P (tmp) = 1;
9877   XVECEXP (dwarf, 0, 0) = tmp;
9878
9879   par = emit_insn (par);
9880   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9881                                        REG_NOTES (par));
9882   return par;
9883 }
9884
9885
9886 /* Return true if the current function needs to save/restore LR.  */
9887
9888 static bool
9889 thumb_force_lr_save (void)
9890 {
9891   return !cfun->machine->lr_save_eliminated
9892          && (!leaf_function_p ()
9893              || thumb_far_jump_used_p ()
9894              || regs_ever_live [LR_REGNUM]);
9895 }
9896
9897
9898 /* Compute the distance from register FROM to register TO.
9899    These can be the arg pointer (26), the soft frame pointer (25),
9900    the stack pointer (13) or the hard frame pointer (11).
9901    In thumb mode r7 is used as the soft frame pointer, if needed.
9902    Typical stack layout looks like this:
9903
9904        old stack pointer -> |    |
9905                              ----
9906                             |    | \
9907                             |    |   saved arguments for
9908                             |    |   vararg functions
9909                             |    | /
9910                               --
9911    hard FP & arg pointer -> |    | \
9912                             |    |   stack
9913                             |    |   frame
9914                             |    | /
9915                               --
9916                             |    | \
9917                             |    |   call saved
9918                             |    |   registers
9919       soft frame pointer -> |    | /
9920                               --
9921                             |    | \
9922                             |    |   local
9923                             |    |   variables
9924                             |    | /
9925                               --
9926                             |    | \
9927                             |    |   outgoing
9928                             |    |   arguments
9929    current stack pointer -> |    | /
9930                               --
9931
9932   For a given function some or all of these stack components
9933   may not be needed, giving rise to the possibility of
9934   eliminating some of the registers.
9935
9936   The values returned by this function must reflect the behavior
9937   of arm_expand_prologue() and arm_compute_save_reg_mask().
9938
9939   The sign of the number returned reflects the direction of stack
9940   growth, so the values are positive for all eliminations except
9941   from the soft frame pointer to the hard frame pointer.
9942
9943   SFP may point just inside the local variables block to ensure correct
9944   alignment.  */
9945
9946
9947 /* Calculate stack offsets.  These are used to calculate register elimination
9948    offsets and in prologue/epilogue code.  */
9949
9950 static arm_stack_offsets *
9951 arm_get_frame_offsets (void)
9952 {
9953   struct arm_stack_offsets *offsets;
9954   unsigned long func_type;
9955   int leaf;
9956   int saved;
9957   HOST_WIDE_INT frame_size;
9958
9959   offsets = &cfun->machine->stack_offsets;
9960
9961   /* We need to know if we are a leaf function.  Unfortunately, it
9962      is possible to be called after start_sequence has been called,
9963      which causes get_insns to return the insns for the sequence,
9964      not the function, which will cause leaf_function_p to return
9965      the incorrect result.
9966
9967      to know about leaf functions once reload has completed, and the
9968      frame size cannot be changed after that time, so we can safely
9969      use the cached value.  */
9970
9971   if (reload_completed)
9972     return offsets;
9973
9974   /* Initially this is the size of the local variables.  It will translated
9975      into an offset once we have determined the size of preceding data.  */
9976   frame_size = ROUND_UP_WORD (get_frame_size ());
9977
9978   leaf = leaf_function_p ();
9979
9980   /* Space for variadic functions.  */
9981   offsets->saved_args = current_function_pretend_args_size;
9982
9983   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
9984
9985   if (TARGET_ARM)
9986     {
9987       unsigned int regno;
9988
9989       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
9990
9991       /* We know that SP will be doubleword aligned on entry, and we must
9992          preserve that condition at any subroutine call.  We also require the
9993          soft frame pointer to be doubleword aligned.  */
9994
9995       if (TARGET_REALLY_IWMMXT)
9996         {
9997           /* Check for the call-saved iWMMXt registers.  */
9998           for (regno = FIRST_IWMMXT_REGNUM;
9999                regno <= LAST_IWMMXT_REGNUM;
10000                regno++)
10001             if (regs_ever_live [regno] && ! call_used_regs [regno])
10002               saved += 8;
10003         }
10004
10005       func_type = arm_current_func_type ();
10006       if (! IS_VOLATILE (func_type))
10007         {
10008           /* Space for saved FPA registers.  */
10009           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10010           if (regs_ever_live[regno] && ! call_used_regs[regno])
10011             saved += 12;
10012
10013           /* Space for saved VFP registers.  */
10014           if (TARGET_HARD_FLOAT && TARGET_VFP)
10015             saved += arm_get_vfp_saved_size ();
10016         }
10017     }
10018   else /* TARGET_THUMB */
10019     {
10020       saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
10021       if (TARGET_BACKTRACE)
10022         saved += 16;
10023     }
10024
10025   /* Saved registers include the stack frame.  */
10026   offsets->saved_regs = offsets->saved_args + saved;
10027   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
10028   /* A leaf function does not need any stack alignment if it has nothing
10029      on the stack.  */
10030   if (leaf && frame_size == 0)
10031     {
10032       offsets->outgoing_args = offsets->soft_frame;
10033       return offsets;
10034     }
10035
10036   /* Ensure SFP has the correct alignment.  */
10037   if (ARM_DOUBLEWORD_ALIGN
10038       && (offsets->soft_frame & 7))
10039     offsets->soft_frame += 4;
10040
10041   offsets->outgoing_args = offsets->soft_frame + frame_size
10042                            + current_function_outgoing_args_size;
10043
10044   if (ARM_DOUBLEWORD_ALIGN)
10045     {
10046       /* Ensure SP remains doubleword aligned.  */
10047       if (offsets->outgoing_args & 7)
10048         offsets->outgoing_args += 4;
10049       if (offsets->outgoing_args & 7)
10050         abort ();
10051     }
10052
10053   return offsets;
10054 }
10055
10056
10057 /* Calculate the relative offsets for the different stack pointers.  Positive
10058    offsets are in the direction of stack growth.  */
10059
10060 HOST_WIDE_INT
10061 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10062 {
10063   arm_stack_offsets *offsets;
10064
10065   offsets = arm_get_frame_offsets ();
10066
10067   /* OK, now we have enough information to compute the distances.
10068      There must be an entry in these switch tables for each pair
10069      of registers in ELIMINABLE_REGS, even if some of the entries
10070      seem to be redundant or useless.  */
10071   switch (from)
10072     {
10073     case ARG_POINTER_REGNUM:
10074       switch (to)
10075         {
10076         case THUMB_HARD_FRAME_POINTER_REGNUM:
10077           return 0;
10078
10079         case FRAME_POINTER_REGNUM:
10080           /* This is the reverse of the soft frame pointer
10081              to hard frame pointer elimination below.  */
10082           return offsets->soft_frame - offsets->saved_args;
10083
10084         case ARM_HARD_FRAME_POINTER_REGNUM:
10085           /* If there is no stack frame then the hard
10086              frame pointer and the arg pointer coincide.  */
10087           if (offsets->frame == offsets->saved_regs)
10088             return 0;
10089           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
10090           return (frame_pointer_needed
10091                   && cfun->static_chain_decl != NULL
10092                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10093
10094         case STACK_POINTER_REGNUM:
10095           /* If nothing has been pushed on the stack at all
10096              then this will return -4.  This *is* correct!  */
10097           return offsets->outgoing_args - (offsets->saved_args + 4);
10098
10099         default:
10100           abort ();
10101         }
10102       break;
10103
10104     case FRAME_POINTER_REGNUM:
10105       switch (to)
10106         {
10107         case THUMB_HARD_FRAME_POINTER_REGNUM:
10108           return 0;
10109
10110         case ARM_HARD_FRAME_POINTER_REGNUM:
10111           /* The hard frame pointer points to the top entry in the
10112              stack frame.  The soft frame pointer to the bottom entry
10113              in the stack frame.  If there is no stack frame at all,
10114              then they are identical.  */
10115
10116           return offsets->frame - offsets->soft_frame;
10117
10118         case STACK_POINTER_REGNUM:
10119           return offsets->outgoing_args - offsets->soft_frame;
10120
10121         default:
10122           abort ();
10123         }
10124       break;
10125
10126     default:
10127       /* You cannot eliminate from the stack pointer.
10128          In theory you could eliminate from the hard frame
10129          pointer to the stack pointer, but this will never
10130          happen, since if a stack frame is not needed the
10131          hard frame pointer will never be used.  */
10132       abort ();
10133     }
10134 }
10135
10136
10137 /* Generate the prologue instructions for entry into an ARM function.  */
10138 void
10139 arm_expand_prologue (void)
10140 {
10141   int reg;
10142   rtx amount;
10143   rtx insn;
10144   rtx ip_rtx;
10145   unsigned long live_regs_mask;
10146   unsigned long func_type;
10147   int fp_offset = 0;
10148   int saved_pretend_args = 0;
10149   int saved_regs = 0;
10150   unsigned HOST_WIDE_INT args_to_push;
10151   arm_stack_offsets *offsets;
10152
10153   func_type = arm_current_func_type ();
10154
10155   /* Naked functions don't have prologues.  */
10156   if (IS_NAKED (func_type))
10157     return;
10158
10159   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
10160   args_to_push = current_function_pretend_args_size;
10161
10162   /* Compute which register we will have to save onto the stack.  */
10163   live_regs_mask = arm_compute_save_reg_mask ();
10164
10165   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10166
10167   if (frame_pointer_needed)
10168     {
10169       if (IS_INTERRUPT (func_type))
10170         {
10171           /* Interrupt functions must not corrupt any registers.
10172              Creating a frame pointer however, corrupts the IP
10173              register, so we must push it first.  */
10174           insn = emit_multi_reg_push (1 << IP_REGNUM);
10175
10176           /* Do not set RTX_FRAME_RELATED_P on this insn.
10177              The dwarf stack unwinding code only wants to see one
10178              stack decrement per function, and this is not it.  If
10179              this instruction is labeled as being part of the frame
10180              creation sequence then dwarf2out_frame_debug_expr will
10181              abort when it encounters the assignment of IP to FP
10182              later on, since the use of SP here establishes SP as
10183              the CFA register and not IP.
10184
10185              Anyway this instruction is not really part of the stack
10186              frame creation although it is part of the prologue.  */
10187         }
10188       else if (IS_NESTED (func_type))
10189         {
10190           /* The Static chain register is the same as the IP register
10191              used as a scratch register during stack frame creation.
10192              To get around this need to find somewhere to store IP
10193              whilst the frame is being created.  We try the following
10194              places in order:
10195
10196                1. The last argument register.
10197                2. A slot on the stack above the frame.  (This only
10198                   works if the function is not a varargs function).
10199                3. Register r3, after pushing the argument registers
10200                   onto the stack.
10201
10202              Note - we only need to tell the dwarf2 backend about the SP
10203              adjustment in the second variant; the static chain register
10204              doesn't need to be unwound, as it doesn't contain a value
10205              inherited from the caller.  */
10206
10207           if (regs_ever_live[3] == 0)
10208             {
10209               insn = gen_rtx_REG (SImode, 3);
10210               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10211               insn = emit_insn (insn);
10212             }
10213           else if (args_to_push == 0)
10214             {
10215               rtx dwarf;
10216               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10217               insn = gen_rtx_MEM (SImode, insn);
10218               insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10219               insn = emit_insn (insn);
10220
10221               fp_offset = 4;
10222
10223               /* Just tell the dwarf backend that we adjusted SP.  */
10224               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10225                                    gen_rtx_PLUS (SImode, stack_pointer_rtx,
10226                                                  GEN_INT (-fp_offset)));
10227               RTX_FRAME_RELATED_P (insn) = 1;
10228               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10229                                                     dwarf, REG_NOTES (insn));
10230             }
10231           else
10232             {
10233               /* Store the args on the stack.  */
10234               if (cfun->machine->uses_anonymous_args)
10235                 insn = emit_multi_reg_push
10236                   ((0xf0 >> (args_to_push / 4)) & 0xf);
10237               else
10238                 insn = emit_insn
10239                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10240                                GEN_INT (- args_to_push)));
10241
10242               RTX_FRAME_RELATED_P (insn) = 1;
10243
10244               saved_pretend_args = 1;
10245               fp_offset = args_to_push;
10246               args_to_push = 0;
10247
10248               /* Now reuse r3 to preserve IP.  */
10249               insn = gen_rtx_REG (SImode, 3);
10250               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10251               (void) emit_insn (insn);
10252             }
10253         }
10254
10255       if (fp_offset)
10256         {
10257           insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10258           insn = gen_rtx_SET  (SImode, ip_rtx, insn);
10259         }
10260       else
10261         insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10262
10263       insn = emit_insn (insn);
10264       RTX_FRAME_RELATED_P (insn) = 1;
10265     }
10266
10267   if (args_to_push)
10268     {
10269       /* Push the argument registers, or reserve space for them.  */
10270       if (cfun->machine->uses_anonymous_args)
10271         insn = emit_multi_reg_push
10272           ((0xf0 >> (args_to_push / 4)) & 0xf);
10273       else
10274         insn = emit_insn
10275           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10276                        GEN_INT (- args_to_push)));
10277       RTX_FRAME_RELATED_P (insn) = 1;
10278     }
10279
10280   /* If this is an interrupt service routine, and the link register
10281      is going to be pushed, and we are not creating a stack frame,
10282      (which would involve an extra push of IP and a pop in the epilogue)
10283      subtracting four from LR now will mean that the function return
10284      can be done with a single instruction.  */
10285   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10286       && (live_regs_mask & (1 << LR_REGNUM)) != 0
10287       && ! frame_pointer_needed)
10288     emit_insn (gen_rtx_SET (SImode,
10289                             gen_rtx_REG (SImode, LR_REGNUM),
10290                             gen_rtx_PLUS (SImode,
10291                                           gen_rtx_REG (SImode, LR_REGNUM),
10292                                           GEN_INT (-4))));
10293
10294   if (live_regs_mask)
10295     {
10296       insn = emit_multi_reg_push (live_regs_mask);
10297       saved_regs += bit_count (live_regs_mask) * 4;
10298       RTX_FRAME_RELATED_P (insn) = 1;
10299     }
10300
10301   if (TARGET_IWMMXT)
10302     for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10303       if (regs_ever_live[reg] && ! call_used_regs [reg])
10304         {
10305           insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10306           insn = gen_rtx_MEM (V2SImode, insn);
10307           insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10308                                          gen_rtx_REG (V2SImode, reg)));
10309           RTX_FRAME_RELATED_P (insn) = 1;
10310           saved_regs += 8;
10311         }
10312
10313   if (! IS_VOLATILE (func_type))
10314     {
10315       int start_reg;
10316
10317       /* Save any floating point call-saved registers used by this
10318          function.  */
10319       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10320         {
10321           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10322             if (regs_ever_live[reg] && !call_used_regs[reg])
10323               {
10324                 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10325                 insn = gen_rtx_MEM (XFmode, insn);
10326                 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10327                                                gen_rtx_REG (XFmode, reg)));
10328                 RTX_FRAME_RELATED_P (insn) = 1;
10329                 saved_regs += 12;
10330               }
10331         }
10332       else
10333         {
10334           start_reg = LAST_FPA_REGNUM;
10335
10336           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10337             {
10338               if (regs_ever_live[reg] && !call_used_regs[reg])
10339                 {
10340                   if (start_reg - reg == 3)
10341                     {
10342                       insn = emit_sfm (reg, 4);
10343                       RTX_FRAME_RELATED_P (insn) = 1;
10344                       saved_regs += 48;
10345                       start_reg = reg - 1;
10346                     }
10347                 }
10348               else
10349                 {
10350                   if (start_reg != reg)
10351                     {
10352                       insn = emit_sfm (reg + 1, start_reg - reg);
10353                       RTX_FRAME_RELATED_P (insn) = 1;
10354                       saved_regs += (start_reg - reg) * 12;
10355                     }
10356                   start_reg = reg - 1;
10357                 }
10358             }
10359
10360           if (start_reg != reg)
10361             {
10362               insn = emit_sfm (reg + 1, start_reg - reg);
10363               saved_regs += (start_reg - reg) * 12;
10364               RTX_FRAME_RELATED_P (insn) = 1;
10365             }
10366         }
10367       if (TARGET_HARD_FLOAT && TARGET_VFP)
10368         {
10369           start_reg = FIRST_VFP_REGNUM;
10370
10371           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10372             {
10373               if ((!regs_ever_live[reg] || call_used_regs[reg])
10374                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10375                 {
10376                   if (start_reg != reg)
10377                     saved_regs += vfp_emit_fstmx (start_reg,
10378                                                   (reg - start_reg) / 2);
10379                   start_reg = reg + 2;
10380                 }
10381             }
10382           if (start_reg != reg)
10383             saved_regs += vfp_emit_fstmx (start_reg,
10384                                           (reg - start_reg) / 2);
10385         }
10386     }
10387
10388   if (frame_pointer_needed)
10389     {
10390       /* Create the new frame pointer.  */
10391       insn = GEN_INT (-(4 + args_to_push + fp_offset));
10392       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10393       RTX_FRAME_RELATED_P (insn) = 1;
10394
10395       if (IS_NESTED (func_type))
10396         {
10397           /* Recover the static chain register.  */
10398           if (regs_ever_live [3] == 0
10399               || saved_pretend_args)
10400             insn = gen_rtx_REG (SImode, 3);
10401           else /* if (current_function_pretend_args_size == 0) */
10402             {
10403               insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10404                                    GEN_INT (4));
10405               insn = gen_rtx_MEM (SImode, insn);
10406             }
10407
10408           emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10409           /* Add a USE to stop propagate_one_insn() from barfing.  */
10410           emit_insn (gen_prologue_use (ip_rtx));
10411         }
10412     }
10413
10414   offsets = arm_get_frame_offsets ();
10415   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10416     {
10417       /* This add can produce multiple insns for a large constant, so we
10418          need to get tricky.  */
10419       rtx last = get_last_insn ();
10420
10421       amount = GEN_INT (offsets->saved_args + saved_regs
10422                         - offsets->outgoing_args);
10423
10424       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10425                                     amount));
10426       do
10427         {
10428           last = last ? NEXT_INSN (last) : get_insns ();
10429           RTX_FRAME_RELATED_P (last) = 1;
10430         }
10431       while (last != insn);
10432
10433       /* If the frame pointer is needed, emit a special barrier that
10434          will prevent the scheduler from moving stores to the frame
10435          before the stack adjustment.  */
10436       if (frame_pointer_needed)
10437         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10438                                          hard_frame_pointer_rtx));
10439     }
10440
10441
10442   if (flag_pic)
10443     arm_load_pic_register (INVALID_REGNUM);
10444
10445   /* If we are profiling, make sure no instructions are scheduled before
10446      the call to mcount.  Similarly if the user has requested no
10447      scheduling in the prolog.  */
10448   if (current_function_profile || TARGET_NO_SCHED_PRO)
10449     emit_insn (gen_blockage ());
10450
10451   /* If the link register is being kept alive, with the return address in it,
10452      then make sure that it does not get reused by the ce2 pass.  */
10453   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10454     {
10455       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10456       cfun->machine->lr_save_eliminated = 1;
10457     }
10458 }
10459 \f
10460 /* If CODE is 'd', then the X is a condition operand and the instruction
10461    should only be executed if the condition is true.
10462    if CODE is 'D', then the X is a condition operand and the instruction
10463    should only be executed if the condition is false: however, if the mode
10464    of the comparison is CCFPEmode, then always execute the instruction -- we
10465    do this because in these circumstances !GE does not necessarily imply LT;
10466    in these cases the instruction pattern will take care to make sure that
10467    an instruction containing %d will follow, thereby undoing the effects of
10468    doing this instruction unconditionally.
10469    If CODE is 'N' then X is a floating point operand that must be negated
10470    before output.
10471    If CODE is 'B' then output a bitwise inverted value of X (a const int).
10472    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
10473 void
10474 arm_print_operand (FILE *stream, rtx x, int code)
10475 {
10476   switch (code)
10477     {
10478     case '@':
10479       fputs (ASM_COMMENT_START, stream);
10480       return;
10481
10482     case '_':
10483       fputs (user_label_prefix, stream);
10484       return;
10485
10486     case '|':
10487       fputs (REGISTER_PREFIX, stream);
10488       return;
10489
10490     case '?':
10491       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10492         {
10493           if (TARGET_THUMB)
10494             {
10495               output_operand_lossage ("predicated Thumb instruction");
10496               break;
10497             }
10498           if (current_insn_predicate != NULL)
10499             {
10500               output_operand_lossage
10501                 ("predicated instruction in conditional sequence");
10502               break;
10503             }
10504
10505           fputs (arm_condition_codes[arm_current_cc], stream);
10506         }
10507       else if (current_insn_predicate)
10508         {
10509           enum arm_cond_code code;
10510
10511           if (TARGET_THUMB)
10512             {
10513               output_operand_lossage ("predicated Thumb instruction");
10514               break;
10515             }
10516
10517           code = get_arm_condition_code (current_insn_predicate);
10518           fputs (arm_condition_codes[code], stream);
10519         }
10520       return;
10521
10522     case 'N':
10523       {
10524         REAL_VALUE_TYPE r;
10525         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10526         r = REAL_VALUE_NEGATE (r);
10527         fprintf (stream, "%s", fp_const_from_val (&r));
10528       }
10529       return;
10530
10531     case 'B':
10532       if (GET_CODE (x) == CONST_INT)
10533         {
10534           HOST_WIDE_INT val;
10535           val = ARM_SIGN_EXTEND (~INTVAL (x));
10536           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10537         }
10538       else
10539         {
10540           putc ('~', stream);
10541           output_addr_const (stream, x);
10542         }
10543       return;
10544
10545     case 'i':
10546       fprintf (stream, "%s", arithmetic_instr (x, 1));
10547       return;
10548
10549     /* Truncate Cirrus shift counts.  */
10550     case 's':
10551       if (GET_CODE (x) == CONST_INT)
10552         {
10553           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10554           return;
10555         }
10556       arm_print_operand (stream, x, 0);
10557       return;
10558
10559     case 'I':
10560       fprintf (stream, "%s", arithmetic_instr (x, 0));
10561       return;
10562
10563     case 'S':
10564       {
10565         HOST_WIDE_INT val;
10566         const char * shift = shift_op (x, &val);
10567
10568         if (shift)
10569           {
10570             fprintf (stream, ", %s ", shift_op (x, &val));
10571             if (val == -1)
10572               arm_print_operand (stream, XEXP (x, 1), 0);
10573             else
10574               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10575           }
10576       }
10577       return;
10578
10579       /* An explanation of the 'Q', 'R' and 'H' register operands:
10580
10581          In a pair of registers containing a DI or DF value the 'Q'
10582          operand returns the register number of the register containing
10583          the least significant part of the value.  The 'R' operand returns
10584          the register number of the register containing the most
10585          significant part of the value.
10586
10587          The 'H' operand returns the higher of the two register numbers.
10588          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10589          same as the 'Q' operand, since the most significant part of the
10590          value is held in the lower number register.  The reverse is true
10591          on systems where WORDS_BIG_ENDIAN is false.
10592
10593          The purpose of these operands is to distinguish between cases
10594          where the endian-ness of the values is important (for example
10595          when they are added together), and cases where the endian-ness
10596          is irrelevant, but the order of register operations is important.
10597          For example when loading a value from memory into a register
10598          pair, the endian-ness does not matter.  Provided that the value
10599          from the lower memory address is put into the lower numbered
10600          register, and the value from the higher address is put into the
10601          higher numbered register, the load will work regardless of whether
10602          the value being loaded is big-wordian or little-wordian.  The
10603          order of the two register loads can matter however, if the address
10604          of the memory location is actually held in one of the registers
10605          being overwritten by the load.  */
10606     case 'Q':
10607       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10608         {
10609           output_operand_lossage ("invalid operand for code '%c'", code);
10610           return;
10611         }
10612
10613       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10614       return;
10615
10616     case 'R':
10617       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10618         {
10619           output_operand_lossage ("invalid operand for code '%c'", code);
10620           return;
10621         }
10622
10623       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10624       return;
10625
10626     case 'H':
10627       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10628         {
10629           output_operand_lossage ("invalid operand for code '%c'", code);
10630           return;
10631         }
10632
10633       asm_fprintf (stream, "%r", REGNO (x) + 1);
10634       return;
10635
10636     case 'm':
10637       asm_fprintf (stream, "%r",
10638                    GET_CODE (XEXP (x, 0)) == REG
10639                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10640       return;
10641
10642     case 'M':
10643       asm_fprintf (stream, "{%r-%r}",
10644                    REGNO (x),
10645                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10646       return;
10647
10648     case 'd':
10649       /* CONST_TRUE_RTX means always -- that's the default.  */
10650       if (x == const_true_rtx)
10651         return;
10652
10653       if (!COMPARISON_P (x))
10654         {
10655           output_operand_lossage ("invalid operand for code '%c'", code);
10656           return;
10657         }
10658
10659       fputs (arm_condition_codes[get_arm_condition_code (x)],
10660              stream);
10661       return;
10662
10663     case 'D':
10664       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
10665          want to do that.  */
10666       if (x == const_true_rtx)
10667         {
10668           output_operand_lossage ("instruction never exectued");
10669           return;
10670         }
10671       if (!COMPARISON_P (x))
10672         {
10673           output_operand_lossage ("invalid operand for code '%c'", code);
10674           return;
10675         }
10676
10677       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10678                                  (get_arm_condition_code (x))],
10679              stream);
10680       return;
10681
10682     /* Cirrus registers can be accessed in a variety of ways:
10683          single floating point (f)
10684          double floating point (d)
10685          32bit integer         (fx)
10686          64bit integer         (dx).  */
10687     case 'W':                   /* Cirrus register in F mode.  */
10688     case 'X':                   /* Cirrus register in D mode.  */
10689     case 'Y':                   /* Cirrus register in FX mode.  */
10690     case 'Z':                   /* Cirrus register in DX mode.  */
10691       if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10692         abort ();
10693
10694       fprintf (stream, "mv%s%s",
10695                code == 'W' ? "f"
10696                : code == 'X' ? "d"
10697                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10698
10699       return;
10700
10701     /* Print cirrus register in the mode specified by the register's mode.  */
10702     case 'V':
10703       {
10704         int mode = GET_MODE (x);
10705
10706         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10707           {
10708             output_operand_lossage ("invalid operand for code '%c'", code);
10709             return;
10710           }
10711
10712         fprintf (stream, "mv%s%s",
10713                  mode == DFmode ? "d"
10714                  : mode == SImode ? "fx"
10715                  : mode == DImode ? "dx"
10716                  : "f", reg_names[REGNO (x)] + 2);
10717
10718         return;
10719       }
10720
10721     case 'U':
10722       if (GET_CODE (x) != REG
10723           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10724           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10725         /* Bad value for wCG register number.  */
10726         {
10727           output_operand_lossage ("invalid operand for code '%c'", code);
10728           return;
10729         }
10730
10731       else
10732         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10733       return;
10734
10735       /* Print an iWMMXt control register name.  */
10736     case 'w':
10737       if (GET_CODE (x) != CONST_INT
10738           || INTVAL (x) < 0
10739           || INTVAL (x) >= 16)
10740         /* Bad value for wC register number.  */
10741         {
10742           output_operand_lossage ("invalid operand for code '%c'", code);
10743           return;
10744         }
10745
10746       else
10747         {
10748           static const char * wc_reg_names [16] =
10749             {
10750               "wCID",  "wCon",  "wCSSF", "wCASF",
10751               "wC4",   "wC5",   "wC6",   "wC7",
10752               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10753               "wC12",  "wC13",  "wC14",  "wC15"
10754             };
10755
10756           fprintf (stream, wc_reg_names [INTVAL (x)]);
10757         }
10758       return;
10759
10760       /* Print a VFP double precision register name.  */
10761     case 'P':
10762       {
10763         int mode = GET_MODE (x);
10764         int num;
10765
10766         if (mode != DImode && mode != DFmode)
10767           {
10768             output_operand_lossage ("invalid operand for code '%c'", code);
10769             return;
10770           }
10771
10772         if (GET_CODE (x) != REG
10773             || !IS_VFP_REGNUM (REGNO (x)))
10774           {
10775             output_operand_lossage ("invalid operand for code '%c'", code);
10776             return;
10777           }
10778
10779         num = REGNO(x) - FIRST_VFP_REGNUM;
10780         if (num & 1)
10781           {
10782             output_operand_lossage ("invalid operand for code '%c'", code);
10783             return;
10784           }
10785
10786         fprintf (stream, "d%d", num >> 1);
10787       }
10788       return;
10789
10790     default:
10791       if (x == 0)
10792         {
10793           output_operand_lossage ("missing operand");
10794           return;
10795         }
10796
10797       if (GET_CODE (x) == REG)
10798         asm_fprintf (stream, "%r", REGNO (x));
10799       else if (GET_CODE (x) == MEM)
10800         {
10801           output_memory_reference_mode = GET_MODE (x);
10802           output_address (XEXP (x, 0));
10803         }
10804       else if (GET_CODE (x) == CONST_DOUBLE)
10805         fprintf (stream, "#%s", fp_immediate_constant (x));
10806       else if (GET_CODE (x) == NEG)
10807         abort (); /* This should never happen now.  */
10808       else
10809         {
10810           fputc ('#', stream);
10811           output_addr_const (stream, x);
10812         }
10813     }
10814 }
10815 \f
10816 #ifndef AOF_ASSEMBLER
10817 /* Target hook for assembling integer objects.  The ARM version needs to
10818    handle word-sized values specially.  */
10819 static bool
10820 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10821 {
10822   if (size == UNITS_PER_WORD && aligned_p)
10823     {
10824       fputs ("\t.word\t", asm_out_file);
10825       output_addr_const (asm_out_file, x);
10826
10827       /* Mark symbols as position independent.  We only do this in the
10828          .text segment, not in the .data segment.  */
10829       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10830           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10831         {
10832           if (GET_CODE (x) == SYMBOL_REF
10833               && (CONSTANT_POOL_ADDRESS_P (x)
10834                   || SYMBOL_REF_LOCAL_P (x)))
10835             fputs ("(GOTOFF)", asm_out_file);
10836           else if (GET_CODE (x) == LABEL_REF)
10837             fputs ("(GOTOFF)", asm_out_file);
10838           else
10839             fputs ("(GOT)", asm_out_file);
10840         }
10841       fputc ('\n', asm_out_file);
10842       return true;
10843     }
10844
10845   if (arm_vector_mode_supported_p (GET_MODE (x)))
10846     {
10847       int i, units;
10848
10849       if (GET_CODE (x) != CONST_VECTOR)
10850         abort ();
10851
10852       units = CONST_VECTOR_NUNITS (x);
10853
10854       switch (GET_MODE (x))
10855         {
10856         case V2SImode: size = 4; break;
10857         case V4HImode: size = 2; break;
10858         case V8QImode: size = 1; break;
10859         default:
10860           abort ();
10861         }
10862
10863       for (i = 0; i < units; i++)
10864         {
10865           rtx elt;
10866
10867           elt = CONST_VECTOR_ELT (x, i);
10868           assemble_integer
10869             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10870         }
10871
10872       return true;
10873     }
10874
10875   return default_assemble_integer (x, size, aligned_p);
10876 }
10877 #endif
10878 \f
10879 /* A finite state machine takes care of noticing whether or not instructions
10880    can be conditionally executed, and thus decrease execution time and code
10881    size by deleting branch instructions.  The fsm is controlled by
10882    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
10883
10884 /* The state of the fsm controlling condition codes are:
10885    0: normal, do nothing special
10886    1: make ASM_OUTPUT_OPCODE not output this instruction
10887    2: make ASM_OUTPUT_OPCODE not output this instruction
10888    3: make instructions conditional
10889    4: make instructions conditional
10890
10891    State transitions (state->state by whom under condition):
10892    0 -> 1 final_prescan_insn if the `target' is a label
10893    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10894    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10895    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10896    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10897           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10898    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10899           (the target insn is arm_target_insn).
10900
10901    If the jump clobbers the conditions then we use states 2 and 4.
10902
10903    A similar thing can be done with conditional return insns.
10904
10905    XXX In case the `target' is an unconditional branch, this conditionalising
10906    of the instructions always reduces code size, but not always execution
10907    time.  But then, I want to reduce the code size to somewhere near what
10908    /bin/cc produces.  */
10909
10910 /* Returns the index of the ARM condition code string in
10911    `arm_condition_codes'.  COMPARISON should be an rtx like
10912    `(eq (...) (...))'.  */
10913 static enum arm_cond_code
10914 get_arm_condition_code (rtx comparison)
10915 {
10916   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10917   int code;
10918   enum rtx_code comp_code = GET_CODE (comparison);
10919
10920   if (GET_MODE_CLASS (mode) != MODE_CC)
10921     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10922                            XEXP (comparison, 1));
10923
10924   switch (mode)
10925     {
10926     case CC_DNEmode: code = ARM_NE; goto dominance;
10927     case CC_DEQmode: code = ARM_EQ; goto dominance;
10928     case CC_DGEmode: code = ARM_GE; goto dominance;
10929     case CC_DGTmode: code = ARM_GT; goto dominance;
10930     case CC_DLEmode: code = ARM_LE; goto dominance;
10931     case CC_DLTmode: code = ARM_LT; goto dominance;
10932     case CC_DGEUmode: code = ARM_CS; goto dominance;
10933     case CC_DGTUmode: code = ARM_HI; goto dominance;
10934     case CC_DLEUmode: code = ARM_LS; goto dominance;
10935     case CC_DLTUmode: code = ARM_CC;
10936
10937     dominance:
10938       if (comp_code != EQ && comp_code != NE)
10939         abort ();
10940
10941       if (comp_code == EQ)
10942         return ARM_INVERSE_CONDITION_CODE (code);
10943       return code;
10944
10945     case CC_NOOVmode:
10946       switch (comp_code)
10947         {
10948         case NE: return ARM_NE;
10949         case EQ: return ARM_EQ;
10950         case GE: return ARM_PL;
10951         case LT: return ARM_MI;
10952         default: abort ();
10953         }
10954
10955     case CC_Zmode:
10956       switch (comp_code)
10957         {
10958         case NE: return ARM_NE;
10959         case EQ: return ARM_EQ;
10960         default: abort ();
10961         }
10962
10963     case CC_Nmode:
10964       switch (comp_code)
10965         {
10966         case NE: return ARM_MI;
10967         case EQ: return ARM_PL;
10968         default: abort ();
10969         }
10970
10971     case CCFPEmode:
10972     case CCFPmode:
10973       /* These encodings assume that AC=1 in the FPA system control
10974          byte.  This allows us to handle all cases except UNEQ and
10975          LTGT.  */
10976       switch (comp_code)
10977         {
10978         case GE: return ARM_GE;
10979         case GT: return ARM_GT;
10980         case LE: return ARM_LS;
10981         case LT: return ARM_MI;
10982         case NE: return ARM_NE;
10983         case EQ: return ARM_EQ;
10984         case ORDERED: return ARM_VC;
10985         case UNORDERED: return ARM_VS;
10986         case UNLT: return ARM_LT;
10987         case UNLE: return ARM_LE;
10988         case UNGT: return ARM_HI;
10989         case UNGE: return ARM_PL;
10990           /* UNEQ and LTGT do not have a representation.  */
10991         case UNEQ: /* Fall through.  */
10992         case LTGT: /* Fall through.  */
10993         default: abort ();
10994         }
10995
10996     case CC_SWPmode:
10997       switch (comp_code)
10998         {
10999         case NE: return ARM_NE;
11000         case EQ: return ARM_EQ;
11001         case GE: return ARM_LE;
11002         case GT: return ARM_LT;
11003         case LE: return ARM_GE;
11004         case LT: return ARM_GT;
11005         case GEU: return ARM_LS;
11006         case GTU: return ARM_CC;
11007         case LEU: return ARM_CS;
11008         case LTU: return ARM_HI;
11009         default: abort ();
11010         }
11011
11012     case CC_Cmode:
11013       switch (comp_code)
11014       {
11015       case LTU: return ARM_CS;
11016       case GEU: return ARM_CC;
11017       default: abort ();
11018       }
11019
11020     case CCmode:
11021       switch (comp_code)
11022         {
11023         case NE: return ARM_NE;
11024         case EQ: return ARM_EQ;
11025         case GE: return ARM_GE;
11026         case GT: return ARM_GT;
11027         case LE: return ARM_LE;
11028         case LT: return ARM_LT;
11029         case GEU: return ARM_CS;
11030         case GTU: return ARM_HI;
11031         case LEU: return ARM_LS;
11032         case LTU: return ARM_CC;
11033         default: abort ();
11034         }
11035
11036     default: abort ();
11037     }
11038
11039   abort ();
11040 }
11041
11042 void
11043 arm_final_prescan_insn (rtx insn)
11044 {
11045   /* BODY will hold the body of INSN.  */
11046   rtx body = PATTERN (insn);
11047
11048   /* This will be 1 if trying to repeat the trick, and things need to be
11049      reversed if it appears to fail.  */
11050   int reverse = 0;
11051
11052   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11053      taken are clobbered, even if the rtl suggests otherwise.  It also
11054      means that we have to grub around within the jump expression to find
11055      out what the conditions are when the jump isn't taken.  */
11056   int jump_clobbers = 0;
11057
11058   /* If we start with a return insn, we only succeed if we find another one.  */
11059   int seeking_return = 0;
11060
11061   /* START_INSN will hold the insn from where we start looking.  This is the
11062      first insn after the following code_label if REVERSE is true.  */
11063   rtx start_insn = insn;
11064
11065   /* If in state 4, check if the target branch is reached, in order to
11066      change back to state 0.  */
11067   if (arm_ccfsm_state == 4)
11068     {
11069       if (insn == arm_target_insn)
11070         {
11071           arm_target_insn = NULL;
11072           arm_ccfsm_state = 0;
11073         }
11074       return;
11075     }
11076
11077   /* If in state 3, it is possible to repeat the trick, if this insn is an
11078      unconditional branch to a label, and immediately following this branch
11079      is the previous target label which is only used once, and the label this
11080      branch jumps to is not too far off.  */
11081   if (arm_ccfsm_state == 3)
11082     {
11083       if (simplejump_p (insn))
11084         {
11085           start_insn = next_nonnote_insn (start_insn);
11086           if (GET_CODE (start_insn) == BARRIER)
11087             {
11088               /* XXX Isn't this always a barrier?  */
11089               start_insn = next_nonnote_insn (start_insn);
11090             }
11091           if (GET_CODE (start_insn) == CODE_LABEL
11092               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11093               && LABEL_NUSES (start_insn) == 1)
11094             reverse = TRUE;
11095           else
11096             return;
11097         }
11098       else if (GET_CODE (body) == RETURN)
11099         {
11100           start_insn = next_nonnote_insn (start_insn);
11101           if (GET_CODE (start_insn) == BARRIER)
11102             start_insn = next_nonnote_insn (start_insn);
11103           if (GET_CODE (start_insn) == CODE_LABEL
11104               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11105               && LABEL_NUSES (start_insn) == 1)
11106             {
11107               reverse = TRUE;
11108               seeking_return = 1;
11109             }
11110           else
11111             return;
11112         }
11113       else
11114         return;
11115     }
11116
11117   if (arm_ccfsm_state != 0 && !reverse)
11118     abort ();
11119   if (GET_CODE (insn) != JUMP_INSN)
11120     return;
11121
11122   /* This jump might be paralleled with a clobber of the condition codes
11123      the jump should always come first */
11124   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11125     body = XVECEXP (body, 0, 0);
11126
11127   if (reverse
11128       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11129           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11130     {
11131       int insns_skipped;
11132       int fail = FALSE, succeed = FALSE;
11133       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
11134       int then_not_else = TRUE;
11135       rtx this_insn = start_insn, label = 0;
11136
11137       /* If the jump cannot be done with one instruction, we cannot
11138          conditionally execute the instruction in the inverse case.  */
11139       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11140         {
11141           jump_clobbers = 1;
11142           return;
11143         }
11144
11145       /* Register the insn jumped to.  */
11146       if (reverse)
11147         {
11148           if (!seeking_return)
11149             label = XEXP (SET_SRC (body), 0);
11150         }
11151       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11152         label = XEXP (XEXP (SET_SRC (body), 1), 0);
11153       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11154         {
11155           label = XEXP (XEXP (SET_SRC (body), 2), 0);
11156           then_not_else = FALSE;
11157         }
11158       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11159         seeking_return = 1;
11160       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11161         {
11162           seeking_return = 1;
11163           then_not_else = FALSE;
11164         }
11165       else
11166         abort ();
11167
11168       /* See how many insns this branch skips, and what kind of insns.  If all
11169          insns are okay, and the label or unconditional branch to the same
11170          label is not too far away, succeed.  */
11171       for (insns_skipped = 0;
11172            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11173         {
11174           rtx scanbody;
11175
11176           this_insn = next_nonnote_insn (this_insn);
11177           if (!this_insn)
11178             break;
11179
11180           switch (GET_CODE (this_insn))
11181             {
11182             case CODE_LABEL:
11183               /* Succeed if it is the target label, otherwise fail since
11184                  control falls in from somewhere else.  */
11185               if (this_insn == label)
11186                 {
11187                   if (jump_clobbers)
11188                     {
11189                       arm_ccfsm_state = 2;
11190                       this_insn = next_nonnote_insn (this_insn);
11191                     }
11192                   else
11193                     arm_ccfsm_state = 1;
11194                   succeed = TRUE;
11195                 }
11196               else
11197                 fail = TRUE;
11198               break;
11199
11200             case BARRIER:
11201               /* Succeed if the following insn is the target label.
11202                  Otherwise fail.
11203                  If return insns are used then the last insn in a function
11204                  will be a barrier.  */
11205               this_insn = next_nonnote_insn (this_insn);
11206               if (this_insn && this_insn == label)
11207                 {
11208                   if (jump_clobbers)
11209                     {
11210                       arm_ccfsm_state = 2;
11211                       this_insn = next_nonnote_insn (this_insn);
11212                     }
11213                   else
11214                     arm_ccfsm_state = 1;
11215                   succeed = TRUE;
11216                 }
11217               else
11218                 fail = TRUE;
11219               break;
11220
11221             case CALL_INSN:
11222               /* The AAPCS says that conditional calls should not be
11223                  used since they make interworking inefficient (the
11224                  linker can't transform BL<cond> into BLX).  That's
11225                  only a problem if the machine has BLX.  */
11226               if (arm_arch5)
11227                 {
11228                   fail = TRUE;
11229                   break;
11230                 }
11231
11232               /* Succeed if the following insn is the target label, or
11233                  if the following two insns are a barrier and the
11234                  target label.  */
11235               this_insn = next_nonnote_insn (this_insn);
11236               if (this_insn && GET_CODE (this_insn) == BARRIER)
11237                 this_insn = next_nonnote_insn (this_insn);
11238
11239               if (this_insn && this_insn == label
11240                   && insns_skipped < max_insns_skipped)
11241                 {
11242                   if (jump_clobbers)
11243                     {
11244                       arm_ccfsm_state = 2;
11245                       this_insn = next_nonnote_insn (this_insn);
11246                     }
11247                   else
11248                     arm_ccfsm_state = 1;
11249                   succeed = TRUE;
11250                 }
11251               else
11252                 fail = TRUE;
11253               break;
11254
11255             case JUMP_INSN:
11256               /* If this is an unconditional branch to the same label, succeed.
11257                  If it is to another label, do nothing.  If it is conditional,
11258                  fail.  */
11259               /* XXX Probably, the tests for SET and the PC are
11260                  unnecessary.  */
11261
11262               scanbody = PATTERN (this_insn);
11263               if (GET_CODE (scanbody) == SET
11264                   && GET_CODE (SET_DEST (scanbody)) == PC)
11265                 {
11266                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11267                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11268                     {
11269                       arm_ccfsm_state = 2;
11270                       succeed = TRUE;
11271                     }
11272                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11273                     fail = TRUE;
11274                 }
11275               /* Fail if a conditional return is undesirable (e.g. on a
11276                  StrongARM), but still allow this if optimizing for size.  */
11277               else if (GET_CODE (scanbody) == RETURN
11278                        && !use_return_insn (TRUE, NULL)
11279                        && !optimize_size)
11280                 fail = TRUE;
11281               else if (GET_CODE (scanbody) == RETURN
11282                        && seeking_return)
11283                 {
11284                   arm_ccfsm_state = 2;
11285                   succeed = TRUE;
11286                 }
11287               else if (GET_CODE (scanbody) == PARALLEL)
11288                 {
11289                   switch (get_attr_conds (this_insn))
11290                     {
11291                     case CONDS_NOCOND:
11292                       break;
11293                     default:
11294                       fail = TRUE;
11295                       break;
11296                     }
11297                 }
11298               else
11299                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
11300
11301               break;
11302
11303             case INSN:
11304               /* Instructions using or affecting the condition codes make it
11305                  fail.  */
11306               scanbody = PATTERN (this_insn);
11307               if (!(GET_CODE (scanbody) == SET
11308                     || GET_CODE (scanbody) == PARALLEL)
11309                   || get_attr_conds (this_insn) != CONDS_NOCOND)
11310                 fail = TRUE;
11311
11312               /* A conditional cirrus instruction must be followed by
11313                  a non Cirrus instruction.  However, since we
11314                  conditionalize instructions in this function and by
11315                  the time we get here we can't add instructions
11316                  (nops), because shorten_branches() has already been
11317                  called, we will disable conditionalizing Cirrus
11318                  instructions to be safe.  */
11319               if (GET_CODE (scanbody) != USE
11320                   && GET_CODE (scanbody) != CLOBBER
11321                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11322                 fail = TRUE;
11323               break;
11324
11325             default:
11326               break;
11327             }
11328         }
11329       if (succeed)
11330         {
11331           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11332             arm_target_label = CODE_LABEL_NUMBER (label);
11333           else if (seeking_return || arm_ccfsm_state == 2)
11334             {
11335               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11336                 {
11337                   this_insn = next_nonnote_insn (this_insn);
11338                   if (this_insn && (GET_CODE (this_insn) == BARRIER
11339                                     || GET_CODE (this_insn) == CODE_LABEL))
11340                     abort ();
11341                 }
11342               if (!this_insn)
11343                 {
11344                   /* Oh, dear! we ran off the end.. give up.  */
11345                   recog (PATTERN (insn), insn, NULL);
11346                   arm_ccfsm_state = 0;
11347                   arm_target_insn = NULL;
11348                   return;
11349                 }
11350               arm_target_insn = this_insn;
11351             }
11352           else
11353             abort ();
11354           if (jump_clobbers)
11355             {
11356               if (reverse)
11357                 abort ();
11358               arm_current_cc =
11359                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11360                                                             0), 0), 1));
11361               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11362                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11363               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11364                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11365             }
11366           else
11367             {
11368               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11369                  what it was.  */
11370               if (!reverse)
11371                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11372                                                                0));
11373             }
11374
11375           if (reverse || then_not_else)
11376             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11377         }
11378
11379       /* Restore recog_data (getting the attributes of other insns can
11380          destroy this array, but final.c assumes that it remains intact
11381          across this call; since the insn has been recognized already we
11382          call recog direct).  */
11383       recog (PATTERN (insn), insn, NULL);
11384     }
11385 }
11386
11387 /* Returns true if REGNO is a valid register
11388    for holding a quantity of type MODE.  */
11389 int
11390 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11391 {
11392   if (GET_MODE_CLASS (mode) == MODE_CC)
11393     return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11394
11395   if (TARGET_THUMB)
11396     /* For the Thumb we only allow values bigger than SImode in
11397        registers 0 - 6, so that there is always a second low
11398        register available to hold the upper part of the value.
11399        We probably we ought to ensure that the register is the
11400        start of an even numbered register pair.  */
11401     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11402
11403   if (IS_CIRRUS_REGNUM (regno))
11404     /* We have outlawed SI values in Cirrus registers because they
11405        reside in the lower 32 bits, but SF values reside in the
11406        upper 32 bits.  This causes gcc all sorts of grief.  We can't
11407        even split the registers into pairs because Cirrus SI values
11408        get sign extended to 64bits-- aldyh.  */
11409     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11410
11411   if (IS_VFP_REGNUM (regno))
11412     {
11413       if (mode == SFmode || mode == SImode)
11414         return TRUE;
11415
11416       /* DFmode values are only valid in even register pairs.  */
11417       if (mode == DFmode)
11418         return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11419       return FALSE;
11420     }
11421
11422   if (IS_IWMMXT_GR_REGNUM (regno))
11423     return mode == SImode;
11424
11425   if (IS_IWMMXT_REGNUM (regno))
11426     return VALID_IWMMXT_REG_MODE (mode);
11427
11428   /* We allow any value to be stored in the general registers.
11429      Restrict doubleword quantities to even register pairs so that we can
11430      use ldrd.  */
11431   if (regno <= LAST_ARM_REGNUM)
11432     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11433
11434   if (   regno == FRAME_POINTER_REGNUM
11435       || regno == ARG_POINTER_REGNUM)
11436     /* We only allow integers in the fake hard registers.  */
11437     return GET_MODE_CLASS (mode) == MODE_INT;
11438
11439   /* The only registers left are the FPA registers
11440      which we only allow to hold FP values.  */
11441   return GET_MODE_CLASS (mode) == MODE_FLOAT
11442     && regno >= FIRST_FPA_REGNUM
11443     && regno <= LAST_FPA_REGNUM;
11444 }
11445
11446 int
11447 arm_regno_class (int regno)
11448 {
11449   if (TARGET_THUMB)
11450     {
11451       if (regno == STACK_POINTER_REGNUM)
11452         return STACK_REG;
11453       if (regno == CC_REGNUM)
11454         return CC_REG;
11455       if (regno < 8)
11456         return LO_REGS;
11457       return HI_REGS;
11458     }
11459
11460   if (   regno <= LAST_ARM_REGNUM
11461       || regno == FRAME_POINTER_REGNUM
11462       || regno == ARG_POINTER_REGNUM)
11463     return GENERAL_REGS;
11464
11465   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11466     return NO_REGS;
11467
11468   if (IS_CIRRUS_REGNUM (regno))
11469     return CIRRUS_REGS;
11470
11471   if (IS_VFP_REGNUM (regno))
11472     return VFP_REGS;
11473
11474   if (IS_IWMMXT_REGNUM (regno))
11475     return IWMMXT_REGS;
11476
11477   if (IS_IWMMXT_GR_REGNUM (regno))
11478     return IWMMXT_GR_REGS;
11479
11480   return FPA_REGS;
11481 }
11482
11483 /* Handle a special case when computing the offset
11484    of an argument from the frame pointer.  */
11485 int
11486 arm_debugger_arg_offset (int value, rtx addr)
11487 {
11488   rtx insn;
11489
11490   /* We are only interested if dbxout_parms() failed to compute the offset.  */
11491   if (value != 0)
11492     return 0;
11493
11494   /* We can only cope with the case where the address is held in a register.  */
11495   if (GET_CODE (addr) != REG)
11496     return 0;
11497
11498   /* If we are using the frame pointer to point at the argument, then
11499      an offset of 0 is correct.  */
11500   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11501     return 0;
11502
11503   /* If we are using the stack pointer to point at the
11504      argument, then an offset of 0 is correct.  */
11505   if ((TARGET_THUMB || !frame_pointer_needed)
11506       && REGNO (addr) == SP_REGNUM)
11507     return 0;
11508
11509   /* Oh dear.  The argument is pointed to by a register rather
11510      than being held in a register, or being stored at a known
11511      offset from the frame pointer.  Since GDB only understands
11512      those two kinds of argument we must translate the address
11513      held in the register into an offset from the frame pointer.
11514      We do this by searching through the insns for the function
11515      looking to see where this register gets its value.  If the
11516      register is initialized from the frame pointer plus an offset
11517      then we are in luck and we can continue, otherwise we give up.
11518
11519      This code is exercised by producing debugging information
11520      for a function with arguments like this:
11521
11522            double func (double a, double b, int c, double d) {return d;}
11523
11524      Without this code the stab for parameter 'd' will be set to
11525      an offset of 0 from the frame pointer, rather than 8.  */
11526
11527   /* The if() statement says:
11528
11529      If the insn is a normal instruction
11530      and if the insn is setting the value in a register
11531      and if the register being set is the register holding the address of the argument
11532      and if the address is computing by an addition
11533      that involves adding to a register
11534      which is the frame pointer
11535      a constant integer
11536
11537      then...  */
11538
11539   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11540     {
11541       if (   GET_CODE (insn) == INSN
11542           && GET_CODE (PATTERN (insn)) == SET
11543           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11544           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11545           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11546           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11547           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11548              )
11549         {
11550           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11551
11552           break;
11553         }
11554     }
11555
11556   if (value == 0)
11557     {
11558       debug_rtx (addr);
11559       warning ("unable to compute real location of stacked parameter");
11560       value = 8; /* XXX magic hack */
11561     }
11562
11563   return value;
11564 }
11565 \f
11566 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
11567   do                                                                    \
11568     {                                                                   \
11569       if ((MASK) & insn_flags)                                          \
11570         lang_hooks.builtin_function ((NAME), (TYPE), (CODE),            \
11571                                      BUILT_IN_MD, NULL, NULL_TREE);     \
11572     }                                                                   \
11573   while (0)
11574
11575 struct builtin_description
11576 {
11577   const unsigned int       mask;
11578   const enum insn_code     icode;
11579   const char * const       name;
11580   const enum arm_builtins  code;
11581   const enum rtx_code      comparison;
11582   const unsigned int       flag;
11583 };
11584
11585 static const struct builtin_description bdesc_2arg[] =
11586 {
11587 #define IWMMXT_BUILTIN(code, string, builtin) \
11588   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11589     ARM_BUILTIN_##builtin, 0, 0 },
11590
11591   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11592   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11593   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11594   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11595   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11596   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11597   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11598   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11599   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11600   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11601   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11602   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11603   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11604   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11605   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11606   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11607   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11608   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11609   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11610   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11611   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11612   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11613   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11614   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11615   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11616   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11617   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11618   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11619   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11620   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11621   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11622   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11623   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11624   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11625   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11626   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11627   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11628   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11629   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11630   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11631   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11632   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11633   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11634   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11635   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11636   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11637   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11638   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11639   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11640   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11641   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11642   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11643   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11644   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11645   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11646   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11647   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11648   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11649
11650 #define IWMMXT_BUILTIN2(code, builtin) \
11651   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11652
11653   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11654   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11655   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11656   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11657   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11658   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11659   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
11660   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
11661   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
11662   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
11663   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
11664   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
11665   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
11666   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
11667   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
11668   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
11669   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
11670   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
11671   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
11672   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
11673   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
11674   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
11675   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
11676   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
11677   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
11678   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
11679   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
11680   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
11681   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
11682   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
11683   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
11684   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
11685 };
11686
11687 static const struct builtin_description bdesc_1arg[] =
11688 {
11689   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11690   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11691   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11692   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11693   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11694   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11695   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11696   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11697   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11698   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11699   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11700   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11701   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11702   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11703   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11704   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11705   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11706   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11707 };
11708
11709 /* Set up all the iWMMXt builtins.  This is
11710    not called if TARGET_IWMMXT is zero.  */
11711
11712 static void
11713 arm_init_iwmmxt_builtins (void)
11714 {
11715   const struct builtin_description * d;
11716   size_t i;
11717   tree endlink = void_list_node;
11718
11719   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11720   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11721   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11722
11723   tree int_ftype_int
11724     = build_function_type (integer_type_node,
11725                            tree_cons (NULL_TREE, integer_type_node, endlink));
11726   tree v8qi_ftype_v8qi_v8qi_int
11727     = build_function_type (V8QI_type_node,
11728                            tree_cons (NULL_TREE, V8QI_type_node,
11729                                       tree_cons (NULL_TREE, V8QI_type_node,
11730                                                  tree_cons (NULL_TREE,
11731                                                             integer_type_node,
11732                                                             endlink))));
11733   tree v4hi_ftype_v4hi_int
11734     = build_function_type (V4HI_type_node,
11735                            tree_cons (NULL_TREE, V4HI_type_node,
11736                                       tree_cons (NULL_TREE, integer_type_node,
11737                                                  endlink)));
11738   tree v2si_ftype_v2si_int
11739     = build_function_type (V2SI_type_node,
11740                            tree_cons (NULL_TREE, V2SI_type_node,
11741                                       tree_cons (NULL_TREE, integer_type_node,
11742                                                  endlink)));
11743   tree v2si_ftype_di_di
11744     = build_function_type (V2SI_type_node,
11745                            tree_cons (NULL_TREE, long_long_integer_type_node,
11746                                       tree_cons (NULL_TREE, long_long_integer_type_node,
11747                                                  endlink)));
11748   tree di_ftype_di_int
11749     = build_function_type (long_long_integer_type_node,
11750                            tree_cons (NULL_TREE, long_long_integer_type_node,
11751                                       tree_cons (NULL_TREE, integer_type_node,
11752                                                  endlink)));
11753   tree di_ftype_di_int_int
11754     = build_function_type (long_long_integer_type_node,
11755                            tree_cons (NULL_TREE, long_long_integer_type_node,
11756                                       tree_cons (NULL_TREE, integer_type_node,
11757                                                  tree_cons (NULL_TREE,
11758                                                             integer_type_node,
11759                                                             endlink))));
11760   tree int_ftype_v8qi
11761     = build_function_type (integer_type_node,
11762                            tree_cons (NULL_TREE, V8QI_type_node,
11763                                       endlink));
11764   tree int_ftype_v4hi
11765     = build_function_type (integer_type_node,
11766                            tree_cons (NULL_TREE, V4HI_type_node,
11767                                       endlink));
11768   tree int_ftype_v2si
11769     = build_function_type (integer_type_node,
11770                            tree_cons (NULL_TREE, V2SI_type_node,
11771                                       endlink));
11772   tree int_ftype_v8qi_int
11773     = build_function_type (integer_type_node,
11774                            tree_cons (NULL_TREE, V8QI_type_node,
11775                                       tree_cons (NULL_TREE, integer_type_node,
11776                                                  endlink)));
11777   tree int_ftype_v4hi_int
11778     = build_function_type (integer_type_node,
11779                            tree_cons (NULL_TREE, V4HI_type_node,
11780                                       tree_cons (NULL_TREE, integer_type_node,
11781                                                  endlink)));
11782   tree int_ftype_v2si_int
11783     = build_function_type (integer_type_node,
11784                            tree_cons (NULL_TREE, V2SI_type_node,
11785                                       tree_cons (NULL_TREE, integer_type_node,
11786                                                  endlink)));
11787   tree v8qi_ftype_v8qi_int_int
11788     = build_function_type (V8QI_type_node,
11789                            tree_cons (NULL_TREE, V8QI_type_node,
11790                                       tree_cons (NULL_TREE, integer_type_node,
11791                                                  tree_cons (NULL_TREE,
11792                                                             integer_type_node,
11793                                                             endlink))));
11794   tree v4hi_ftype_v4hi_int_int
11795     = build_function_type (V4HI_type_node,
11796                            tree_cons (NULL_TREE, V4HI_type_node,
11797                                       tree_cons (NULL_TREE, integer_type_node,
11798                                                  tree_cons (NULL_TREE,
11799                                                             integer_type_node,
11800                                                             endlink))));
11801   tree v2si_ftype_v2si_int_int
11802     = build_function_type (V2SI_type_node,
11803                            tree_cons (NULL_TREE, V2SI_type_node,
11804                                       tree_cons (NULL_TREE, integer_type_node,
11805                                                  tree_cons (NULL_TREE,
11806                                                             integer_type_node,
11807                                                             endlink))));
11808   /* Miscellaneous.  */
11809   tree v8qi_ftype_v4hi_v4hi
11810     = build_function_type (V8QI_type_node,
11811                            tree_cons (NULL_TREE, V4HI_type_node,
11812                                       tree_cons (NULL_TREE, V4HI_type_node,
11813                                                  endlink)));
11814   tree v4hi_ftype_v2si_v2si
11815     = build_function_type (V4HI_type_node,
11816                            tree_cons (NULL_TREE, V2SI_type_node,
11817                                       tree_cons (NULL_TREE, V2SI_type_node,
11818                                                  endlink)));
11819   tree v2si_ftype_v4hi_v4hi
11820     = build_function_type (V2SI_type_node,
11821                            tree_cons (NULL_TREE, V4HI_type_node,
11822                                       tree_cons (NULL_TREE, V4HI_type_node,
11823                                                  endlink)));
11824   tree v2si_ftype_v8qi_v8qi
11825     = build_function_type (V2SI_type_node,
11826                            tree_cons (NULL_TREE, V8QI_type_node,
11827                                       tree_cons (NULL_TREE, V8QI_type_node,
11828                                                  endlink)));
11829   tree v4hi_ftype_v4hi_di
11830     = build_function_type (V4HI_type_node,
11831                            tree_cons (NULL_TREE, V4HI_type_node,
11832                                       tree_cons (NULL_TREE,
11833                                                  long_long_integer_type_node,
11834                                                  endlink)));
11835   tree v2si_ftype_v2si_di
11836     = build_function_type (V2SI_type_node,
11837                            tree_cons (NULL_TREE, V2SI_type_node,
11838                                       tree_cons (NULL_TREE,
11839                                                  long_long_integer_type_node,
11840                                                  endlink)));
11841   tree void_ftype_int_int
11842     = build_function_type (void_type_node,
11843                            tree_cons (NULL_TREE, integer_type_node,
11844                                       tree_cons (NULL_TREE, integer_type_node,
11845                                                  endlink)));
11846   tree di_ftype_void
11847     = build_function_type (long_long_unsigned_type_node, endlink);
11848   tree di_ftype_v8qi
11849     = build_function_type (long_long_integer_type_node,
11850                            tree_cons (NULL_TREE, V8QI_type_node,
11851                                       endlink));
11852   tree di_ftype_v4hi
11853     = build_function_type (long_long_integer_type_node,
11854                            tree_cons (NULL_TREE, V4HI_type_node,
11855                                       endlink));
11856   tree di_ftype_v2si
11857     = build_function_type (long_long_integer_type_node,
11858                            tree_cons (NULL_TREE, V2SI_type_node,
11859                                       endlink));
11860   tree v2si_ftype_v4hi
11861     = build_function_type (V2SI_type_node,
11862                            tree_cons (NULL_TREE, V4HI_type_node,
11863                                       endlink));
11864   tree v4hi_ftype_v8qi
11865     = build_function_type (V4HI_type_node,
11866                            tree_cons (NULL_TREE, V8QI_type_node,
11867                                       endlink));
11868
11869   tree di_ftype_di_v4hi_v4hi
11870     = build_function_type (long_long_unsigned_type_node,
11871                            tree_cons (NULL_TREE,
11872                                       long_long_unsigned_type_node,
11873                                       tree_cons (NULL_TREE, V4HI_type_node,
11874                                                  tree_cons (NULL_TREE,
11875                                                             V4HI_type_node,
11876                                                             endlink))));
11877
11878   tree di_ftype_v4hi_v4hi
11879     = build_function_type (long_long_unsigned_type_node,
11880                            tree_cons (NULL_TREE, V4HI_type_node,
11881                                       tree_cons (NULL_TREE, V4HI_type_node,
11882                                                  endlink)));
11883
11884   /* Normal vector binops.  */
11885   tree v8qi_ftype_v8qi_v8qi
11886     = build_function_type (V8QI_type_node,
11887                            tree_cons (NULL_TREE, V8QI_type_node,
11888                                       tree_cons (NULL_TREE, V8QI_type_node,
11889                                                  endlink)));
11890   tree v4hi_ftype_v4hi_v4hi
11891     = build_function_type (V4HI_type_node,
11892                            tree_cons (NULL_TREE, V4HI_type_node,
11893                                       tree_cons (NULL_TREE, V4HI_type_node,
11894                                                  endlink)));
11895   tree v2si_ftype_v2si_v2si
11896     = build_function_type (V2SI_type_node,
11897                            tree_cons (NULL_TREE, V2SI_type_node,
11898                                       tree_cons (NULL_TREE, V2SI_type_node,
11899                                                  endlink)));
11900   tree di_ftype_di_di
11901     = build_function_type (long_long_unsigned_type_node,
11902                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
11903                                       tree_cons (NULL_TREE,
11904                                                  long_long_unsigned_type_node,
11905                                                  endlink)));
11906
11907   /* Add all builtins that are more or less simple operations on two
11908      operands.  */
11909   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11910     {
11911       /* Use one of the operands; the target can have a different mode for
11912          mask-generating compares.  */
11913       enum machine_mode mode;
11914       tree type;
11915
11916       if (d->name == 0)
11917         continue;
11918
11919       mode = insn_data[d->icode].operand[1].mode;
11920
11921       switch (mode)
11922         {
11923         case V8QImode:
11924           type = v8qi_ftype_v8qi_v8qi;
11925           break;
11926         case V4HImode:
11927           type = v4hi_ftype_v4hi_v4hi;
11928           break;
11929         case V2SImode:
11930           type = v2si_ftype_v2si_v2si;
11931           break;
11932         case DImode:
11933           type = di_ftype_di_di;
11934           break;
11935
11936         default:
11937           abort ();
11938         }
11939
11940       def_mbuiltin (d->mask, d->name, type, d->code);
11941     }
11942
11943   /* Add the remaining MMX insns with somewhat more complicated types.  */
11944   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11945   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11946   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11947
11948   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11949   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11950   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11951   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11952   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11953   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11954
11955   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11956   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11957   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11958   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11959   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11960   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11961
11962   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11963   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11964   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11965   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11966   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11967   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11968
11969   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11970   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11971   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11972   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11973   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11974   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11975
11976   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11977
11978   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11979   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11980   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11981   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11982
11983   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11984   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11985   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11986   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11987   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11988   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11989   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11990   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11991   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11992
11993   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11994   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11995   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11996
11997   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11998   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11999   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
12000
12001   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
12002   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
12003   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
12004   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
12005   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
12006   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
12007
12008   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
12009   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
12010   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
12011   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
12012   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
12013   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
12014   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
12015   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
12016   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
12017   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
12018   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
12019   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
12020
12021   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
12022   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
12023   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
12024   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
12025
12026   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
12027   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
12028   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
12029   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
12030   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
12031   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
12032   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
12033 }
12034
12035 static void
12036 arm_init_builtins (void)
12037 {
12038   if (TARGET_REALLY_IWMMXT)
12039     arm_init_iwmmxt_builtins ();
12040 }
12041
12042 /* Errors in the source file can cause expand_expr to return const0_rtx
12043    where we expect a vector.  To avoid crashing, use one of the vector
12044    clear instructions.  */
12045
12046 static rtx
12047 safe_vector_operand (rtx x, enum machine_mode mode)
12048 {
12049   if (x != const0_rtx)
12050     return x;
12051   x = gen_reg_rtx (mode);
12052
12053   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12054                                : gen_rtx_SUBREG (DImode, x, 0)));
12055   return x;
12056 }
12057
12058 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
12059
12060 static rtx
12061 arm_expand_binop_builtin (enum insn_code icode,
12062                           tree arglist, rtx target)
12063 {
12064   rtx pat;
12065   tree arg0 = TREE_VALUE (arglist);
12066   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12067   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12068   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12069   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12070   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12071   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12072
12073   if (VECTOR_MODE_P (mode0))
12074     op0 = safe_vector_operand (op0, mode0);
12075   if (VECTOR_MODE_P (mode1))
12076     op1 = safe_vector_operand (op1, mode1);
12077
12078   if (! target
12079       || GET_MODE (target) != tmode
12080       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12081     target = gen_reg_rtx (tmode);
12082
12083   /* In case the insn wants input operands in modes different from
12084      the result, abort.  */
12085   if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
12086     abort ();
12087
12088   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12089     op0 = copy_to_mode_reg (mode0, op0);
12090   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12091     op1 = copy_to_mode_reg (mode1, op1);
12092
12093   pat = GEN_FCN (icode) (target, op0, op1);
12094   if (! pat)
12095     return 0;
12096   emit_insn (pat);
12097   return target;
12098 }
12099
12100 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
12101
12102 static rtx
12103 arm_expand_unop_builtin (enum insn_code icode,
12104                          tree arglist, rtx target, int do_load)
12105 {
12106   rtx pat;
12107   tree arg0 = TREE_VALUE (arglist);
12108   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12109   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12110   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12111
12112   if (! target
12113       || GET_MODE (target) != tmode
12114       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12115     target = gen_reg_rtx (tmode);
12116   if (do_load)
12117     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12118   else
12119     {
12120       if (VECTOR_MODE_P (mode0))
12121         op0 = safe_vector_operand (op0, mode0);
12122
12123       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12124         op0 = copy_to_mode_reg (mode0, op0);
12125     }
12126
12127   pat = GEN_FCN (icode) (target, op0);
12128   if (! pat)
12129     return 0;
12130   emit_insn (pat);
12131   return target;
12132 }
12133
12134 /* Expand an expression EXP that calls a built-in function,
12135    with result going to TARGET if that's convenient
12136    (and in mode MODE if that's convenient).
12137    SUBTARGET may be used as the target for computing one of EXP's operands.
12138    IGNORE is nonzero if the value is to be ignored.  */
12139
12140 static rtx
12141 arm_expand_builtin (tree exp,
12142                     rtx target,
12143                     rtx subtarget ATTRIBUTE_UNUSED,
12144                     enum machine_mode mode ATTRIBUTE_UNUSED,
12145                     int ignore ATTRIBUTE_UNUSED)
12146 {
12147   const struct builtin_description * d;
12148   enum insn_code    icode;
12149   tree              fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12150   tree              arglist = TREE_OPERAND (exp, 1);
12151   tree              arg0;
12152   tree              arg1;
12153   tree              arg2;
12154   rtx               op0;
12155   rtx               op1;
12156   rtx               op2;
12157   rtx               pat;
12158   int               fcode = DECL_FUNCTION_CODE (fndecl);
12159   size_t            i;
12160   enum machine_mode tmode;
12161   enum machine_mode mode0;
12162   enum machine_mode mode1;
12163   enum machine_mode mode2;
12164
12165   switch (fcode)
12166     {
12167     case ARM_BUILTIN_TEXTRMSB:
12168     case ARM_BUILTIN_TEXTRMUB:
12169     case ARM_BUILTIN_TEXTRMSH:
12170     case ARM_BUILTIN_TEXTRMUH:
12171     case ARM_BUILTIN_TEXTRMSW:
12172     case ARM_BUILTIN_TEXTRMUW:
12173       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12174                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12175                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12176                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12177                : CODE_FOR_iwmmxt_textrmw);
12178
12179       arg0 = TREE_VALUE (arglist);
12180       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12181       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12182       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12183       tmode = insn_data[icode].operand[0].mode;
12184       mode0 = insn_data[icode].operand[1].mode;
12185       mode1 = insn_data[icode].operand[2].mode;
12186
12187       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12188         op0 = copy_to_mode_reg (mode0, op0);
12189       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12190         {
12191           /* @@@ better error message */
12192           error ("selector must be an immediate");
12193           return gen_reg_rtx (tmode);
12194         }
12195       if (target == 0
12196           || GET_MODE (target) != tmode
12197           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12198         target = gen_reg_rtx (tmode);
12199       pat = GEN_FCN (icode) (target, op0, op1);
12200       if (! pat)
12201         return 0;
12202       emit_insn (pat);
12203       return target;
12204
12205     case ARM_BUILTIN_TINSRB:
12206     case ARM_BUILTIN_TINSRH:
12207     case ARM_BUILTIN_TINSRW:
12208       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12209                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12210                : CODE_FOR_iwmmxt_tinsrw);
12211       arg0 = TREE_VALUE (arglist);
12212       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12213       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12214       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12215       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12216       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12217       tmode = insn_data[icode].operand[0].mode;
12218       mode0 = insn_data[icode].operand[1].mode;
12219       mode1 = insn_data[icode].operand[2].mode;
12220       mode2 = insn_data[icode].operand[3].mode;
12221
12222       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12223         op0 = copy_to_mode_reg (mode0, op0);
12224       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12225         op1 = copy_to_mode_reg (mode1, op1);
12226       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12227         {
12228           /* @@@ better error message */
12229           error ("selector must be an immediate");
12230           return const0_rtx;
12231         }
12232       if (target == 0
12233           || GET_MODE (target) != tmode
12234           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12235         target = gen_reg_rtx (tmode);
12236       pat = GEN_FCN (icode) (target, op0, op1, op2);
12237       if (! pat)
12238         return 0;
12239       emit_insn (pat);
12240       return target;
12241
12242     case ARM_BUILTIN_SETWCX:
12243       arg0 = TREE_VALUE (arglist);
12244       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12245       op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12246       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12247       emit_insn (gen_iwmmxt_tmcr (op1, op0));
12248       return 0;
12249
12250     case ARM_BUILTIN_GETWCX:
12251       arg0 = TREE_VALUE (arglist);
12252       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12253       target = gen_reg_rtx (SImode);
12254       emit_insn (gen_iwmmxt_tmrc (target, op0));
12255       return target;
12256
12257     case ARM_BUILTIN_WSHUFH:
12258       icode = CODE_FOR_iwmmxt_wshufh;
12259       arg0 = TREE_VALUE (arglist);
12260       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12261       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12262       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12263       tmode = insn_data[icode].operand[0].mode;
12264       mode1 = insn_data[icode].operand[1].mode;
12265       mode2 = insn_data[icode].operand[2].mode;
12266
12267       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12268         op0 = copy_to_mode_reg (mode1, op0);
12269       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12270         {
12271           /* @@@ better error message */
12272           error ("mask must be an immediate");
12273           return const0_rtx;
12274         }
12275       if (target == 0
12276           || GET_MODE (target) != tmode
12277           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12278         target = gen_reg_rtx (tmode);
12279       pat = GEN_FCN (icode) (target, op0, op1);
12280       if (! pat)
12281         return 0;
12282       emit_insn (pat);
12283       return target;
12284
12285     case ARM_BUILTIN_WSADB:
12286       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12287     case ARM_BUILTIN_WSADH:
12288       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12289     case ARM_BUILTIN_WSADBZ:
12290       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12291     case ARM_BUILTIN_WSADHZ:
12292       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12293
12294       /* Several three-argument builtins.  */
12295     case ARM_BUILTIN_WMACS:
12296     case ARM_BUILTIN_WMACU:
12297     case ARM_BUILTIN_WALIGN:
12298     case ARM_BUILTIN_TMIA:
12299     case ARM_BUILTIN_TMIAPH:
12300     case ARM_BUILTIN_TMIATT:
12301     case ARM_BUILTIN_TMIATB:
12302     case ARM_BUILTIN_TMIABT:
12303     case ARM_BUILTIN_TMIABB:
12304       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12305                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12306                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12307                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12308                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12309                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12310                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12311                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12312                : CODE_FOR_iwmmxt_walign);
12313       arg0 = TREE_VALUE (arglist);
12314       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12315       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12316       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12317       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12318       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12319       tmode = insn_data[icode].operand[0].mode;
12320       mode0 = insn_data[icode].operand[1].mode;
12321       mode1 = insn_data[icode].operand[2].mode;
12322       mode2 = insn_data[icode].operand[3].mode;
12323
12324       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12325         op0 = copy_to_mode_reg (mode0, op0);
12326       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12327         op1 = copy_to_mode_reg (mode1, op1);
12328       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12329         op2 = copy_to_mode_reg (mode2, op2);
12330       if (target == 0
12331           || GET_MODE (target) != tmode
12332           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12333         target = gen_reg_rtx (tmode);
12334       pat = GEN_FCN (icode) (target, op0, op1, op2);
12335       if (! pat)
12336         return 0;
12337       emit_insn (pat);
12338       return target;
12339
12340     case ARM_BUILTIN_WZERO:
12341       target = gen_reg_rtx (DImode);
12342       emit_insn (gen_iwmmxt_clrdi (target));
12343       return target;
12344
12345     default:
12346       break;
12347     }
12348
12349   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12350     if (d->code == (const enum arm_builtins) fcode)
12351       return arm_expand_binop_builtin (d->icode, arglist, target);
12352
12353   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12354     if (d->code == (const enum arm_builtins) fcode)
12355       return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12356
12357   /* @@@ Should really do something sensible here.  */
12358   return NULL_RTX;
12359 }
12360 \f
12361 /* Recursively search through all of the blocks in a function
12362    checking to see if any of the variables created in that
12363    function match the RTX called 'orig'.  If they do then
12364    replace them with the RTX called 'new'.  */
12365 static void
12366 replace_symbols_in_block (tree block, rtx orig, rtx new)
12367 {
12368   for (; block; block = BLOCK_CHAIN (block))
12369     {
12370       tree sym;
12371
12372       if (!TREE_USED (block))
12373         continue;
12374
12375       for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
12376         {
12377           if (  (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
12378               || DECL_IGNORED_P (sym)
12379               || TREE_CODE (sym) != VAR_DECL
12380               || DECL_EXTERNAL (sym)
12381               || !rtx_equal_p (DECL_RTL (sym), orig)
12382               )
12383             continue;
12384
12385           SET_DECL_RTL (sym, new);
12386         }
12387
12388       replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
12389     }
12390 }
12391
12392 /* Return the number (counting from 0) of
12393    the least significant set bit in MASK.  */
12394
12395 inline static int
12396 number_of_first_bit_set (unsigned mask)
12397 {
12398   int bit;
12399
12400   for (bit = 0;
12401        (mask & (1 << bit)) == 0;
12402        ++bit)
12403     continue;
12404
12405   return bit;
12406 }
12407
12408 /* Emit code to push or pop registers to or from the stack.  F is the
12409    assembly file.  MASK is the registers to push or pop.  PUSH is
12410    nonzero if we should push, and zero if we should pop.  For debugging
12411    output, if pushing, adjust CFA_OFFSET by the amount of space added
12412    to the stack.  REAL_REGS should have the same number of bits set as
12413    MASK, and will be used instead (in the same order) to describe which
12414    registers were saved - this is used to mark the save slots when we
12415    push high registers after moving them to low registers.  */
12416 static void
12417 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
12418                unsigned long real_regs)
12419 {
12420   int regno;
12421   int lo_mask = mask & 0xFF;
12422   int pushed_words = 0;
12423
12424   if (mask == 0)
12425     abort ();
12426
12427   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12428     {
12429       /* Special case.  Do not generate a POP PC statement here, do it in
12430          thumb_exit() */
12431       thumb_exit (f, -1);
12432       return;
12433     }
12434
12435   fprintf (f, "\t%s\t{", push ? "push" : "pop");
12436
12437   /* Look at the low registers first.  */
12438   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12439     {
12440       if (lo_mask & 1)
12441         {
12442           asm_fprintf (f, "%r", regno);
12443
12444           if ((lo_mask & ~1) != 0)
12445             fprintf (f, ", ");
12446
12447           pushed_words++;
12448         }
12449     }
12450
12451   if (push && (mask & (1 << LR_REGNUM)))
12452     {
12453       /* Catch pushing the LR.  */
12454       if (mask & 0xFF)
12455         fprintf (f, ", ");
12456
12457       asm_fprintf (f, "%r", LR_REGNUM);
12458
12459       pushed_words++;
12460     }
12461   else if (!push && (mask & (1 << PC_REGNUM)))
12462     {
12463       /* Catch popping the PC.  */
12464       if (TARGET_INTERWORK || TARGET_BACKTRACE
12465           || current_function_calls_eh_return)
12466         {
12467           /* The PC is never poped directly, instead
12468              it is popped into r3 and then BX is used.  */
12469           fprintf (f, "}\n");
12470
12471           thumb_exit (f, -1);
12472
12473           return;
12474         }
12475       else
12476         {
12477           if (mask & 0xFF)
12478             fprintf (f, ", ");
12479
12480           asm_fprintf (f, "%r", PC_REGNUM);
12481         }
12482     }
12483
12484   fprintf (f, "}\n");
12485
12486   if (push && pushed_words && dwarf2out_do_frame ())
12487     {
12488       char *l = dwarf2out_cfi_label ();
12489       int pushed_mask = real_regs;
12490
12491       *cfa_offset += pushed_words * 4;
12492       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12493
12494       pushed_words = 0;
12495       pushed_mask = real_regs;
12496       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12497         {
12498           if (pushed_mask & 1)
12499             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12500         }
12501     }
12502 }
12503
12504 /* Generate code to return from a thumb function.
12505    If 'reg_containing_return_addr' is -1, then the return address is
12506    actually on the stack, at the stack pointer.  */
12507 static void
12508 thumb_exit (FILE *f, int reg_containing_return_addr)
12509 {
12510   unsigned regs_available_for_popping;
12511   unsigned regs_to_pop;
12512   int pops_needed;
12513   unsigned available;
12514   unsigned required;
12515   int mode;
12516   int size;
12517   int restore_a4 = FALSE;
12518
12519   /* Compute the registers we need to pop.  */
12520   regs_to_pop = 0;
12521   pops_needed = 0;
12522
12523   if (reg_containing_return_addr == -1)
12524     {
12525       regs_to_pop |= 1 << LR_REGNUM;
12526       ++pops_needed;
12527     }
12528
12529   if (TARGET_BACKTRACE)
12530     {
12531       /* Restore the (ARM) frame pointer and stack pointer.  */
12532       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12533       pops_needed += 2;
12534     }
12535
12536   /* If there is nothing to pop then just emit the BX instruction and
12537      return.  */
12538   if (pops_needed == 0)
12539     {
12540       if (current_function_calls_eh_return)
12541         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12542
12543       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12544       return;
12545     }
12546   /* Otherwise if we are not supporting interworking and we have not created
12547      a backtrace structure and the function was not entered in ARM mode then
12548      just pop the return address straight into the PC.  */
12549   else if (!TARGET_INTERWORK
12550            && !TARGET_BACKTRACE
12551            && !is_called_in_ARM_mode (current_function_decl)
12552            && !current_function_calls_eh_return)
12553     {
12554       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12555       return;
12556     }
12557
12558   /* Find out how many of the (return) argument registers we can corrupt.  */
12559   regs_available_for_popping = 0;
12560
12561   /* If returning via __builtin_eh_return, the bottom three registers
12562      all contain information needed for the return.  */
12563   if (current_function_calls_eh_return)
12564     size = 12;
12565   else
12566     {
12567       /* If we can deduce the registers used from the function's
12568          return value.  This is more reliable that examining
12569          regs_ever_live[] because that will be set if the register is
12570          ever used in the function, not just if the register is used
12571          to hold a return value.  */
12572
12573       if (current_function_return_rtx != 0)
12574         mode = GET_MODE (current_function_return_rtx);
12575       else
12576         mode = DECL_MODE (DECL_RESULT (current_function_decl));
12577
12578       size = GET_MODE_SIZE (mode);
12579
12580       if (size == 0)
12581         {
12582           /* In a void function we can use any argument register.
12583              In a function that returns a structure on the stack
12584              we can use the second and third argument registers.  */
12585           if (mode == VOIDmode)
12586             regs_available_for_popping =
12587               (1 << ARG_REGISTER (1))
12588               | (1 << ARG_REGISTER (2))
12589               | (1 << ARG_REGISTER (3));
12590           else
12591             regs_available_for_popping =
12592               (1 << ARG_REGISTER (2))
12593               | (1 << ARG_REGISTER (3));
12594         }
12595       else if (size <= 4)
12596         regs_available_for_popping =
12597           (1 << ARG_REGISTER (2))
12598           | (1 << ARG_REGISTER (3));
12599       else if (size <= 8)
12600         regs_available_for_popping =
12601           (1 << ARG_REGISTER (3));
12602     }
12603
12604   /* Match registers to be popped with registers into which we pop them.  */
12605   for (available = regs_available_for_popping,
12606        required  = regs_to_pop;
12607        required != 0 && available != 0;
12608        available &= ~(available & - available),
12609        required  &= ~(required  & - required))
12610     -- pops_needed;
12611
12612   /* If we have any popping registers left over, remove them.  */
12613   if (available > 0)
12614     regs_available_for_popping &= ~available;
12615
12616   /* Otherwise if we need another popping register we can use
12617      the fourth argument register.  */
12618   else if (pops_needed)
12619     {
12620       /* If we have not found any free argument registers and
12621          reg a4 contains the return address, we must move it.  */
12622       if (regs_available_for_popping == 0
12623           && reg_containing_return_addr == LAST_ARG_REGNUM)
12624         {
12625           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12626           reg_containing_return_addr = LR_REGNUM;
12627         }
12628       else if (size > 12)
12629         {
12630           /* Register a4 is being used to hold part of the return value,
12631              but we have dire need of a free, low register.  */
12632           restore_a4 = TRUE;
12633
12634           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12635         }
12636
12637       if (reg_containing_return_addr != LAST_ARG_REGNUM)
12638         {
12639           /* The fourth argument register is available.  */
12640           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12641
12642           --pops_needed;
12643         }
12644     }
12645
12646   /* Pop as many registers as we can.  */
12647   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12648                  regs_available_for_popping);
12649
12650   /* Process the registers we popped.  */
12651   if (reg_containing_return_addr == -1)
12652     {
12653       /* The return address was popped into the lowest numbered register.  */
12654       regs_to_pop &= ~(1 << LR_REGNUM);
12655
12656       reg_containing_return_addr =
12657         number_of_first_bit_set (regs_available_for_popping);
12658
12659       /* Remove this register for the mask of available registers, so that
12660          the return address will not be corrupted by further pops.  */
12661       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12662     }
12663
12664   /* If we popped other registers then handle them here.  */
12665   if (regs_available_for_popping)
12666     {
12667       int frame_pointer;
12668
12669       /* Work out which register currently contains the frame pointer.  */
12670       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12671
12672       /* Move it into the correct place.  */
12673       asm_fprintf (f, "\tmov\t%r, %r\n",
12674                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12675
12676       /* (Temporarily) remove it from the mask of popped registers.  */
12677       regs_available_for_popping &= ~(1 << frame_pointer);
12678       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12679
12680       if (regs_available_for_popping)
12681         {
12682           int stack_pointer;
12683
12684           /* We popped the stack pointer as well,
12685              find the register that contains it.  */
12686           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12687
12688           /* Move it into the stack register.  */
12689           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12690
12691           /* At this point we have popped all necessary registers, so
12692              do not worry about restoring regs_available_for_popping
12693              to its correct value:
12694
12695              assert (pops_needed == 0)
12696              assert (regs_available_for_popping == (1 << frame_pointer))
12697              assert (regs_to_pop == (1 << STACK_POINTER))  */
12698         }
12699       else
12700         {
12701           /* Since we have just move the popped value into the frame
12702              pointer, the popping register is available for reuse, and
12703              we know that we still have the stack pointer left to pop.  */
12704           regs_available_for_popping |= (1 << frame_pointer);
12705         }
12706     }
12707
12708   /* If we still have registers left on the stack, but we no longer have
12709      any registers into which we can pop them, then we must move the return
12710      address into the link register and make available the register that
12711      contained it.  */
12712   if (regs_available_for_popping == 0 && pops_needed > 0)
12713     {
12714       regs_available_for_popping |= 1 << reg_containing_return_addr;
12715
12716       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12717                    reg_containing_return_addr);
12718
12719       reg_containing_return_addr = LR_REGNUM;
12720     }
12721
12722   /* If we have registers left on the stack then pop some more.
12723      We know that at most we will want to pop FP and SP.  */
12724   if (pops_needed > 0)
12725     {
12726       int  popped_into;
12727       int  move_to;
12728
12729       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12730                      regs_available_for_popping);
12731
12732       /* We have popped either FP or SP.
12733          Move whichever one it is into the correct register.  */
12734       popped_into = number_of_first_bit_set (regs_available_for_popping);
12735       move_to     = number_of_first_bit_set (regs_to_pop);
12736
12737       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12738
12739       regs_to_pop &= ~(1 << move_to);
12740
12741       --pops_needed;
12742     }
12743
12744   /* If we still have not popped everything then we must have only
12745      had one register available to us and we are now popping the SP.  */
12746   if (pops_needed > 0)
12747     {
12748       int  popped_into;
12749
12750       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12751                      regs_available_for_popping);
12752
12753       popped_into = number_of_first_bit_set (regs_available_for_popping);
12754
12755       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12756       /*
12757         assert (regs_to_pop == (1 << STACK_POINTER))
12758         assert (pops_needed == 1)
12759       */
12760     }
12761
12762   /* If necessary restore the a4 register.  */
12763   if (restore_a4)
12764     {
12765       if (reg_containing_return_addr != LR_REGNUM)
12766         {
12767           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12768           reg_containing_return_addr = LR_REGNUM;
12769         }
12770
12771       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12772     }
12773
12774   if (current_function_calls_eh_return)
12775     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12776
12777   /* Return to caller.  */
12778   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12779 }
12780
12781 \f
12782 void
12783 thumb_final_prescan_insn (rtx insn)
12784 {
12785   if (flag_print_asm_name)
12786     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12787                  INSN_ADDRESSES (INSN_UID (insn)));
12788 }
12789
12790 int
12791 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12792 {
12793   unsigned HOST_WIDE_INT mask = 0xff;
12794   int i;
12795
12796   if (val == 0) /* XXX */
12797     return 0;
12798
12799   for (i = 0; i < 25; i++)
12800     if ((val & (mask << i)) == val)
12801       return 1;
12802
12803   return 0;
12804 }
12805
12806 /* Returns nonzero if the current function contains,
12807    or might contain a far jump.  */
12808 static int
12809 thumb_far_jump_used_p (void)
12810 {
12811   rtx insn;
12812
12813   /* This test is only important for leaf functions.  */
12814   /* assert (!leaf_function_p ()); */
12815
12816   /* If we have already decided that far jumps may be used,
12817      do not bother checking again, and always return true even if
12818      it turns out that they are not being used.  Once we have made
12819      the decision that far jumps are present (and that hence the link
12820      register will be pushed onto the stack) we cannot go back on it.  */
12821   if (cfun->machine->far_jump_used)
12822     return 1;
12823
12824   /* If this function is not being called from the prologue/epilogue
12825      generation code then it must be being called from the
12826      INITIAL_ELIMINATION_OFFSET macro.  */
12827   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12828     {
12829       /* In this case we know that we are being asked about the elimination
12830          of the arg pointer register.  If that register is not being used,
12831          then there are no arguments on the stack, and we do not have to
12832          worry that a far jump might force the prologue to push the link
12833          register, changing the stack offsets.  In this case we can just
12834          return false, since the presence of far jumps in the function will
12835          not affect stack offsets.
12836
12837          If the arg pointer is live (or if it was live, but has now been
12838          eliminated and so set to dead) then we do have to test to see if
12839          the function might contain a far jump.  This test can lead to some
12840          false negatives, since before reload is completed, then length of
12841          branch instructions is not known, so gcc defaults to returning their
12842          longest length, which in turn sets the far jump attribute to true.
12843
12844          A false negative will not result in bad code being generated, but it
12845          will result in a needless push and pop of the link register.  We
12846          hope that this does not occur too often.
12847
12848          If we need doubleword stack alignment this could affect the other
12849          elimination offsets so we can't risk getting it wrong.  */
12850       if (regs_ever_live [ARG_POINTER_REGNUM])
12851         cfun->machine->arg_pointer_live = 1;
12852       else if (!cfun->machine->arg_pointer_live)
12853         return 0;
12854     }
12855
12856   /* Check to see if the function contains a branch
12857      insn with the far jump attribute set.  */
12858   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12859     {
12860       if (GET_CODE (insn) == JUMP_INSN
12861           /* Ignore tablejump patterns.  */
12862           && GET_CODE (PATTERN (insn)) != ADDR_VEC
12863           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12864           && get_attr_far_jump (insn) == FAR_JUMP_YES
12865           )
12866         {
12867           /* Record the fact that we have decided that
12868              the function does use far jumps.  */
12869           cfun->machine->far_jump_used = 1;
12870           return 1;
12871         }
12872     }
12873
12874   return 0;
12875 }
12876
12877 /* Return nonzero if FUNC must be entered in ARM mode.  */
12878 int
12879 is_called_in_ARM_mode (tree func)
12880 {
12881   if (TREE_CODE (func) != FUNCTION_DECL)
12882     abort ();
12883
12884   /* Ignore the problem about functions whoes address is taken.  */
12885   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12886     return TRUE;
12887
12888 #ifdef ARM_PE
12889   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12890 #else
12891   return FALSE;
12892 #endif
12893 }
12894
12895 /* The bits which aren't usefully expanded as rtl.  */
12896 const char *
12897 thumb_unexpanded_epilogue (void)
12898 {
12899   int regno;
12900   unsigned long live_regs_mask = 0;
12901   int high_regs_pushed = 0;
12902   int had_to_push_lr;
12903   int size;
12904   int mode;
12905
12906   if (return_used_this_function)
12907     return "";
12908
12909   if (IS_NAKED (arm_current_func_type ()))
12910     return "";
12911
12912   live_regs_mask = thumb_compute_save_reg_mask ();
12913   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
12914
12915   /* If we can deduce the registers used from the function's return value.
12916      This is more reliable that examining regs_ever_live[] because that
12917      will be set if the register is ever used in the function, not just if
12918      the register is used to hold a return value.  */
12919
12920   if (current_function_return_rtx != 0)
12921     mode = GET_MODE (current_function_return_rtx);
12922   else
12923     mode = DECL_MODE (DECL_RESULT (current_function_decl));
12924
12925   size = GET_MODE_SIZE (mode);
12926
12927   /* The prolog may have pushed some high registers to use as
12928      work registers.  e.g. the testsuite file:
12929      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12930      compiles to produce:
12931         push    {r4, r5, r6, r7, lr}
12932         mov     r7, r9
12933         mov     r6, r8
12934         push    {r6, r7}
12935      as part of the prolog.  We have to undo that pushing here.  */
12936
12937   if (high_regs_pushed)
12938     {
12939       unsigned long mask = live_regs_mask & 0xff;
12940       int next_hi_reg;
12941
12942       /* The available low registers depend on the size of the value we are
12943          returning.  */
12944       if (size <= 12)
12945         mask |=  1 << 3;
12946       if (size <= 8)
12947         mask |= 1 << 2;
12948
12949       if (mask == 0)
12950         /* Oh dear!  We have no low registers into which we can pop
12951            high registers!  */
12952         internal_error
12953           ("no low registers available for popping high registers");
12954
12955       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12956         if (live_regs_mask & (1 << next_hi_reg))
12957           break;
12958
12959       while (high_regs_pushed)
12960         {
12961           /* Find lo register(s) into which the high register(s) can
12962              be popped.  */
12963           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12964             {
12965               if (mask & (1 << regno))
12966                 high_regs_pushed--;
12967               if (high_regs_pushed == 0)
12968                 break;
12969             }
12970
12971           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
12972
12973           /* Pop the values into the low register(s).  */
12974           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
12975
12976           /* Move the value(s) into the high registers.  */
12977           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12978             {
12979               if (mask & (1 << regno))
12980                 {
12981                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12982                                regno);
12983
12984                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
12985                     if (live_regs_mask & (1 << next_hi_reg))
12986                       break;
12987                 }
12988             }
12989         }
12990       live_regs_mask &= ~0x0f00;
12991     }
12992
12993   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
12994   live_regs_mask &= 0xff;
12995
12996   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12997     {
12998       /* Pop the return address into the PC.  */
12999       if (had_to_push_lr)
13000         live_regs_mask |= 1 << PC_REGNUM;
13001
13002       /* Either no argument registers were pushed or a backtrace
13003          structure was created which includes an adjusted stack
13004          pointer, so just pop everything.  */
13005       if (live_regs_mask)
13006         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13007                        live_regs_mask);
13008
13009       /* We have either just popped the return address into the
13010          PC or it is was kept in LR for the entire function.  */
13011       if (!had_to_push_lr)
13012         thumb_exit (asm_out_file, LR_REGNUM);
13013     }
13014   else
13015     {
13016       /* Pop everything but the return address.  */
13017       if (live_regs_mask)
13018         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13019                        live_regs_mask);
13020
13021       if (had_to_push_lr)
13022         {
13023           if (size > 12)
13024             {
13025               /* We have no free low regs, so save one.  */
13026               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
13027                            LAST_ARG_REGNUM);
13028             }
13029
13030           /* Get the return address into a temporary register.  */
13031           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
13032                          1 << LAST_ARG_REGNUM);
13033
13034           if (size > 12)
13035             {
13036               /* Move the return address to lr.  */
13037               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
13038                            LAST_ARG_REGNUM);
13039               /* Restore the low register.  */
13040               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
13041                            IP_REGNUM);
13042               regno = LR_REGNUM;
13043             }
13044           else
13045             regno = LAST_ARG_REGNUM;
13046         }
13047       else
13048         regno = LR_REGNUM;
13049
13050       /* Remove the argument registers that were pushed onto the stack.  */
13051       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13052                    SP_REGNUM, SP_REGNUM,
13053                    current_function_pretend_args_size);
13054
13055       thumb_exit (asm_out_file, regno);
13056     }
13057
13058   return "";
13059 }
13060
13061 /* Functions to save and restore machine-specific function data.  */
13062 static struct machine_function *
13063 arm_init_machine_status (void)
13064 {
13065   struct machine_function *machine;
13066   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13067
13068 #if ARM_FT_UNKNOWN != 0
13069   machine->func_type = ARM_FT_UNKNOWN;
13070 #endif
13071   return machine;
13072 }
13073
13074 /* Return an RTX indicating where the return address to the
13075    calling function can be found.  */
13076 rtx
13077 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13078 {
13079   if (count != 0)
13080     return NULL_RTX;
13081
13082   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13083 }
13084
13085 /* Do anything needed before RTL is emitted for each function.  */
13086 void
13087 arm_init_expanders (void)
13088 {
13089   /* Arrange to initialize and mark the machine per-function status.  */
13090   init_machine_status = arm_init_machine_status;
13091
13092   /* This is to stop the combine pass optimizing away the alignment
13093      adjustment of va_arg.  */
13094   /* ??? It is claimed that this should not be necessary.  */
13095   if (cfun)
13096     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
13097 }
13098
13099
13100 /* Like arm_compute_initial_elimination offset.  Simpler because
13101    THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer.  */
13102
13103 HOST_WIDE_INT
13104 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13105 {
13106   arm_stack_offsets *offsets;
13107
13108   offsets = arm_get_frame_offsets ();
13109
13110   switch (from)
13111     {
13112     case ARG_POINTER_REGNUM:
13113       switch (to)
13114         {
13115         case STACK_POINTER_REGNUM:
13116           return offsets->outgoing_args - offsets->saved_args;
13117
13118         case FRAME_POINTER_REGNUM:
13119           return offsets->soft_frame - offsets->saved_args;
13120
13121         case THUMB_HARD_FRAME_POINTER_REGNUM:
13122         case ARM_HARD_FRAME_POINTER_REGNUM:
13123           return offsets->saved_regs - offsets->saved_args;
13124
13125         default:
13126           abort();
13127         }
13128       break;
13129
13130     case FRAME_POINTER_REGNUM:
13131       switch (to)
13132         {
13133         case STACK_POINTER_REGNUM:
13134           return offsets->outgoing_args - offsets->soft_frame;
13135
13136         case THUMB_HARD_FRAME_POINTER_REGNUM:
13137         case ARM_HARD_FRAME_POINTER_REGNUM:
13138           return offsets->saved_regs - offsets->soft_frame;
13139
13140         default:
13141           abort();
13142         }
13143       break;
13144
13145     default:
13146       abort ();
13147     }
13148 }
13149
13150
13151 /* Generate the rest of a function's prologue.  */
13152 void
13153 thumb_expand_prologue (void)
13154 {
13155   rtx insn, dwarf;
13156
13157   HOST_WIDE_INT amount;
13158   arm_stack_offsets *offsets;
13159   unsigned long func_type;
13160   int regno;
13161   unsigned long live_regs_mask;
13162
13163   func_type = arm_current_func_type ();
13164
13165   /* Naked functions don't have prologues.  */
13166   if (IS_NAKED (func_type))
13167     return;
13168
13169   if (IS_INTERRUPT (func_type))
13170     {
13171       error ("interrupt Service Routines cannot be coded in Thumb mode");
13172       return;
13173     }
13174
13175   live_regs_mask = thumb_compute_save_reg_mask ();
13176   /* Load the pic register before setting the frame pointer,
13177      so we can use r7 as a temporary work register.  */
13178   if (flag_pic)
13179     arm_load_pic_register (thumb_find_work_register (live_regs_mask));
13180
13181   offsets = arm_get_frame_offsets ();
13182
13183   if (frame_pointer_needed)
13184     {
13185       insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
13186                                    stack_pointer_rtx));
13187       RTX_FRAME_RELATED_P (insn) = 1;
13188     }
13189   else if (CALLER_INTERWORKING_SLOT_SIZE > 0)
13190     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
13191                     stack_pointer_rtx);
13192
13193   amount = offsets->outgoing_args - offsets->saved_regs;
13194   if (amount)
13195     {
13196       if (amount < 512)
13197         {
13198           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13199                                         GEN_INT (- amount)));
13200           RTX_FRAME_RELATED_P (insn) = 1;
13201         }
13202       else
13203         {
13204           rtx reg;
13205
13206           /* The stack decrement is too big for an immediate value in a single
13207              insn.  In theory we could issue multiple subtracts, but after
13208              three of them it becomes more space efficient to place the full
13209              value in the constant pool and load into a register.  (Also the
13210              ARM debugger really likes to see only one stack decrement per
13211              function).  So instead we look for a scratch register into which
13212              we can load the decrement, and then we subtract this from the
13213              stack pointer.  Unfortunately on the thumb the only available
13214              scratch registers are the argument registers, and we cannot use
13215              these as they may hold arguments to the function.  Instead we
13216              attempt to locate a call preserved register which is used by this
13217              function.  If we can find one, then we know that it will have
13218              been pushed at the start of the prologue and so we can corrupt
13219              it now.  */
13220           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13221             if (live_regs_mask & (1 << regno)
13222                 && !(frame_pointer_needed
13223                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13224               break;
13225
13226           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
13227             {
13228               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13229
13230               /* Choose an arbitrary, non-argument low register.  */
13231               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13232
13233               /* Save it by copying it into a high, scratch register.  */
13234               emit_insn (gen_movsi (spare, reg));
13235               /* Add a USE to stop propagate_one_insn() from barfing.  */
13236               emit_insn (gen_prologue_use (spare));
13237
13238               /* Decrement the stack.  */
13239               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13240               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13241                                             stack_pointer_rtx, reg));
13242               RTX_FRAME_RELATED_P (insn) = 1;
13243               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13244                                    plus_constant (stack_pointer_rtx,
13245                                                   -amount));
13246               RTX_FRAME_RELATED_P (dwarf) = 1;
13247               REG_NOTES (insn)
13248                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13249                                      REG_NOTES (insn));
13250
13251               /* Restore the low register's original value.  */
13252               emit_insn (gen_movsi (reg, spare));
13253
13254               /* Emit a USE of the restored scratch register, so that flow
13255                  analysis will not consider the restore redundant.  The
13256                  register won't be used again in this function and isn't
13257                  restored by the epilogue.  */
13258               emit_insn (gen_prologue_use (reg));
13259             }
13260           else
13261             {
13262               reg = gen_rtx_REG (SImode, regno);
13263
13264               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13265
13266               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13267                                             stack_pointer_rtx, reg));
13268               RTX_FRAME_RELATED_P (insn) = 1;
13269               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13270                                    plus_constant (stack_pointer_rtx,
13271                                                   -amount));
13272               RTX_FRAME_RELATED_P (dwarf) = 1;
13273               REG_NOTES (insn)
13274                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13275                                      REG_NOTES (insn));
13276             }
13277         }
13278       /* If the frame pointer is needed, emit a special barrier that
13279          will prevent the scheduler from moving stores to the frame
13280          before the stack adjustment.  */
13281       if (frame_pointer_needed)
13282         emit_insn (gen_stack_tie (stack_pointer_rtx,
13283                                   hard_frame_pointer_rtx));
13284     }
13285
13286   if (current_function_profile || TARGET_NO_SCHED_PRO)
13287     emit_insn (gen_blockage ());
13288
13289   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13290   if (live_regs_mask & 0xff)
13291     cfun->machine->lr_save_eliminated = 0;
13292
13293   /* If the link register is being kept alive, with the return address in it,
13294      then make sure that it does not get reused by the ce2 pass.  */
13295   if (cfun->machine->lr_save_eliminated)
13296     emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13297 }
13298
13299
13300 void
13301 thumb_expand_epilogue (void)
13302 {
13303   HOST_WIDE_INT amount;
13304   arm_stack_offsets *offsets;
13305   int regno;
13306
13307   /* Naked functions don't have prologues.  */
13308   if (IS_NAKED (arm_current_func_type ()))
13309     return;
13310
13311   offsets = arm_get_frame_offsets ();
13312   amount = offsets->outgoing_args - offsets->saved_regs;
13313
13314   if (frame_pointer_needed)
13315     emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13316   else if (amount)
13317     {
13318       if (amount < 512)
13319         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13320                                GEN_INT (amount)));
13321       else
13322         {
13323           /* r3 is always free in the epilogue.  */
13324           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13325
13326           emit_insn (gen_movsi (reg, GEN_INT (amount)));
13327           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13328         }
13329     }
13330
13331   /* Emit a USE (stack_pointer_rtx), so that
13332      the stack adjustment will not be deleted.  */
13333   emit_insn (gen_prologue_use (stack_pointer_rtx));
13334
13335   if (current_function_profile || TARGET_NO_SCHED_PRO)
13336     emit_insn (gen_blockage ());
13337
13338   /* Emit a clobber for each insn that will be restored in the epilogue,
13339      so that flow2 will get register lifetimes correct.  */
13340   for (regno = 0; regno < 13; regno++)
13341     if (regs_ever_live[regno] && !call_used_regs[regno])
13342       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13343
13344   if (! regs_ever_live[LR_REGNUM])
13345     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13346 }
13347
13348 static void
13349 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13350 {
13351   unsigned long live_regs_mask = 0;
13352   unsigned long l_mask;
13353   unsigned high_regs_pushed = 0;
13354   int cfa_offset = 0;
13355   int regno;
13356
13357   if (IS_NAKED (arm_current_func_type ()))
13358     return;
13359
13360   if (is_called_in_ARM_mode (current_function_decl))
13361     {
13362       const char * name;
13363
13364       if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
13365         abort ();
13366       if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
13367         abort ();
13368       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
13369
13370       /* Generate code sequence to switch us into Thumb mode.  */
13371       /* The .code 32 directive has already been emitted by
13372          ASM_DECLARE_FUNCTION_NAME.  */
13373       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13374       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13375
13376       /* Generate a label, so that the debugger will notice the
13377          change in instruction sets.  This label is also used by
13378          the assembler to bypass the ARM code when this function
13379          is called from a Thumb encoded function elsewhere in the
13380          same file.  Hence the definition of STUB_NAME here must
13381          agree with the definition in gas/config/tc-arm.c.  */
13382
13383 #define STUB_NAME ".real_start_of"
13384
13385       fprintf (f, "\t.code\t16\n");
13386 #ifdef ARM_PE
13387       if (arm_dllexport_name_p (name))
13388         name = arm_strip_name_encoding (name);
13389 #endif
13390       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13391       fprintf (f, "\t.thumb_func\n");
13392       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13393     }
13394
13395   if (current_function_pretend_args_size)
13396     {
13397       if (cfun->machine->uses_anonymous_args)
13398         {
13399           int num_pushes;
13400
13401           fprintf (f, "\tpush\t{");
13402
13403           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13404
13405           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13406                regno <= LAST_ARG_REGNUM;
13407                regno++)
13408             asm_fprintf (f, "%r%s", regno,
13409                          regno == LAST_ARG_REGNUM ? "" : ", ");
13410
13411           fprintf (f, "}\n");
13412         }
13413       else
13414         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13415                      SP_REGNUM, SP_REGNUM,
13416                      current_function_pretend_args_size);
13417
13418       /* We don't need to record the stores for unwinding (would it
13419          help the debugger any if we did?), but record the change in
13420          the stack pointer.  */
13421       if (dwarf2out_do_frame ())
13422         {
13423           char *l = dwarf2out_cfi_label ();
13424
13425           cfa_offset = cfa_offset + current_function_pretend_args_size;
13426           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13427         }
13428     }
13429
13430   /* Get the registers we are going to push.  */
13431   live_regs_mask = thumb_compute_save_reg_mask ();
13432   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
13433   l_mask = live_regs_mask & 0x40ff;
13434   /* Then count how many other high registers will need to be pushed.  */
13435   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13436
13437   if (TARGET_BACKTRACE)
13438     {
13439       unsigned offset;
13440       unsigned work_register;
13441
13442       /* We have been asked to create a stack backtrace structure.
13443          The code looks like this:
13444
13445          0   .align 2
13446          0   func:
13447          0     sub   SP, #16         Reserve space for 4 registers.
13448          2     push  {R7}            Push low registers.
13449          4     add   R7, SP, #20     Get the stack pointer before the push.
13450          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
13451          8     mov   R7, PC          Get hold of the start of this code plus 12.
13452         10     str   R7, [SP, #16]   Store it.
13453         12     mov   R7, FP          Get hold of the current frame pointer.
13454         14     str   R7, [SP, #4]    Store it.
13455         16     mov   R7, LR          Get hold of the current return address.
13456         18     str   R7, [SP, #12]   Store it.
13457         20     add   R7, SP, #16     Point at the start of the backtrace structure.
13458         22     mov   FP, R7          Put this value into the frame pointer.  */
13459
13460       work_register = thumb_find_work_register (live_regs_mask);
13461
13462       asm_fprintf
13463         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13464          SP_REGNUM, SP_REGNUM);
13465
13466       if (dwarf2out_do_frame ())
13467         {
13468           char *l = dwarf2out_cfi_label ();
13469
13470           cfa_offset = cfa_offset + 16;
13471           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13472         }
13473
13474       if (l_mask)
13475         {
13476           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13477           offset = bit_count (l_mask);
13478         }
13479       else
13480         offset = 0;
13481
13482       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13483                    offset + 16 + current_function_pretend_args_size);
13484
13485       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13486                    offset + 4);
13487
13488       /* Make sure that the instruction fetching the PC is in the right place
13489          to calculate "start of backtrace creation code + 12".  */
13490       if (l_mask)
13491         {
13492           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13493           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13494                        offset + 12);
13495           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13496                        ARM_HARD_FRAME_POINTER_REGNUM);
13497           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13498                        offset);
13499         }
13500       else
13501         {
13502           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13503                        ARM_HARD_FRAME_POINTER_REGNUM);
13504           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13505                        offset);
13506           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13507           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13508                        offset + 12);
13509         }
13510
13511       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13512       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13513                    offset + 8);
13514       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13515                    offset + 12);
13516       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13517                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13518     }
13519   /* Optimisation:  If we are not pushing any low registers but we are going
13520      to push some high registers then delay our first push.  This will just
13521      be a push of LR and we can combine it with the push of the first high
13522      register.  */
13523   else if ((l_mask & 0xff) != 0
13524            || (high_regs_pushed == 0 && l_mask))
13525     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13526
13527   if (high_regs_pushed)
13528     {
13529       unsigned pushable_regs;
13530       unsigned next_hi_reg;
13531
13532       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13533         if (live_regs_mask & (1 << next_hi_reg))
13534           break;
13535
13536       pushable_regs = l_mask & 0xff;
13537
13538       if (pushable_regs == 0)
13539         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
13540
13541       while (high_regs_pushed > 0)
13542         {
13543           unsigned long real_regs_mask = 0;
13544
13545           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
13546             {
13547               if (pushable_regs & (1 << regno))
13548                 {
13549                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13550
13551                   high_regs_pushed --;
13552                   real_regs_mask |= (1 << next_hi_reg);
13553
13554                   if (high_regs_pushed)
13555                     {
13556                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
13557                            next_hi_reg --)
13558                         if (live_regs_mask & (1 << next_hi_reg))
13559                           break;
13560                     }
13561                   else
13562                     {
13563                       pushable_regs &= ~((1 << regno) - 1);
13564                       break;
13565                     }
13566                 }
13567             }
13568
13569           /* If we had to find a work register and we have not yet
13570              saved the LR then add it to the list of regs to push.  */
13571           if (l_mask == (1 << LR_REGNUM))
13572             {
13573               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
13574                              1, &cfa_offset,
13575                              real_regs_mask | (1 << LR_REGNUM));
13576               l_mask = 0;
13577             }
13578           else
13579             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
13580         }
13581     }
13582 }
13583
13584 /* Handle the case of a double word load into a low register from
13585    a computed memory address.  The computed address may involve a
13586    register which is overwritten by the load.  */
13587 const char *
13588 thumb_load_double_from_address (rtx *operands)
13589 {
13590   rtx addr;
13591   rtx base;
13592   rtx offset;
13593   rtx arg1;
13594   rtx arg2;
13595
13596   if (GET_CODE (operands[0]) != REG)
13597     abort ();
13598
13599   if (GET_CODE (operands[1]) != MEM)
13600     abort ();
13601
13602   /* Get the memory address.  */
13603   addr = XEXP (operands[1], 0);
13604
13605   /* Work out how the memory address is computed.  */
13606   switch (GET_CODE (addr))
13607     {
13608     case REG:
13609       operands[2] = gen_rtx_MEM (SImode,
13610                                  plus_constant (XEXP (operands[1], 0), 4));
13611
13612       if (REGNO (operands[0]) == REGNO (addr))
13613         {
13614           output_asm_insn ("ldr\t%H0, %2", operands);
13615           output_asm_insn ("ldr\t%0, %1", operands);
13616         }
13617       else
13618         {
13619           output_asm_insn ("ldr\t%0, %1", operands);
13620           output_asm_insn ("ldr\t%H0, %2", operands);
13621         }
13622       break;
13623
13624     case CONST:
13625       /* Compute <address> + 4 for the high order load.  */
13626       operands[2] = gen_rtx_MEM (SImode,
13627                                  plus_constant (XEXP (operands[1], 0), 4));
13628
13629       output_asm_insn ("ldr\t%0, %1", operands);
13630       output_asm_insn ("ldr\t%H0, %2", operands);
13631       break;
13632
13633     case PLUS:
13634       arg1   = XEXP (addr, 0);
13635       arg2   = XEXP (addr, 1);
13636
13637       if (CONSTANT_P (arg1))
13638         base = arg2, offset = arg1;
13639       else
13640         base = arg1, offset = arg2;
13641
13642       if (GET_CODE (base) != REG)
13643         abort ();
13644
13645       /* Catch the case of <address> = <reg> + <reg> */
13646       if (GET_CODE (offset) == REG)
13647         {
13648           int reg_offset = REGNO (offset);
13649           int reg_base   = REGNO (base);
13650           int reg_dest   = REGNO (operands[0]);
13651
13652           /* Add the base and offset registers together into the
13653              higher destination register.  */
13654           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13655                        reg_dest + 1, reg_base, reg_offset);
13656
13657           /* Load the lower destination register from the address in
13658              the higher destination register.  */
13659           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13660                        reg_dest, reg_dest + 1);
13661
13662           /* Load the higher destination register from its own address
13663              plus 4.  */
13664           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13665                        reg_dest + 1, reg_dest + 1);
13666         }
13667       else
13668         {
13669           /* Compute <address> + 4 for the high order load.  */
13670           operands[2] = gen_rtx_MEM (SImode,
13671                                      plus_constant (XEXP (operands[1], 0), 4));
13672
13673           /* If the computed address is held in the low order register
13674              then load the high order register first, otherwise always
13675              load the low order register first.  */
13676           if (REGNO (operands[0]) == REGNO (base))
13677             {
13678               output_asm_insn ("ldr\t%H0, %2", operands);
13679               output_asm_insn ("ldr\t%0, %1", operands);
13680             }
13681           else
13682             {
13683               output_asm_insn ("ldr\t%0, %1", operands);
13684               output_asm_insn ("ldr\t%H0, %2", operands);
13685             }
13686         }
13687       break;
13688
13689     case LABEL_REF:
13690       /* With no registers to worry about we can just load the value
13691          directly.  */
13692       operands[2] = gen_rtx_MEM (SImode,
13693                                  plus_constant (XEXP (operands[1], 0), 4));
13694
13695       output_asm_insn ("ldr\t%H0, %2", operands);
13696       output_asm_insn ("ldr\t%0, %1", operands);
13697       break;
13698
13699     default:
13700       abort ();
13701       break;
13702     }
13703
13704   return "";
13705 }
13706
13707 const char *
13708 thumb_output_move_mem_multiple (int n, rtx *operands)
13709 {
13710   rtx tmp;
13711
13712   switch (n)
13713     {
13714     case 2:
13715       if (REGNO (operands[4]) > REGNO (operands[5]))
13716         {
13717           tmp = operands[4];
13718           operands[4] = operands[5];
13719           operands[5] = tmp;
13720         }
13721       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13722       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13723       break;
13724
13725     case 3:
13726       if (REGNO (operands[4]) > REGNO (operands[5]))
13727         {
13728           tmp = operands[4];
13729           operands[4] = operands[5];
13730           operands[5] = tmp;
13731         }
13732       if (REGNO (operands[5]) > REGNO (operands[6]))
13733         {
13734           tmp = operands[5];
13735           operands[5] = operands[6];
13736           operands[6] = tmp;
13737         }
13738       if (REGNO (operands[4]) > REGNO (operands[5]))
13739         {
13740           tmp = operands[4];
13741           operands[4] = operands[5];
13742           operands[5] = tmp;
13743         }
13744
13745       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13746       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13747       break;
13748
13749     default:
13750       abort ();
13751     }
13752
13753   return "";
13754 }
13755
13756 /* Output a call-via instruction for thumb state.  */
13757 const char *
13758 thumb_call_via_reg (rtx reg)
13759 {
13760   int regno = REGNO (reg);
13761   rtx *labelp;
13762
13763   gcc_assert (regno < SP_REGNUM);
13764
13765   /* If we are in the normal text section we can use a single instance
13766      per compilation unit.  If we are doing function sections, then we need
13767      an entry per section, since we can't rely on reachability.  */
13768   if (in_text_section ())
13769     {
13770       thumb_call_reg_needed = 1;
13771
13772       if (thumb_call_via_label[regno] == NULL)
13773         thumb_call_via_label[regno] = gen_label_rtx ();
13774       labelp = thumb_call_via_label + regno;
13775     }
13776   else
13777     {
13778       if (cfun->machine->call_via[regno] == NULL)
13779         cfun->machine->call_via[regno] = gen_label_rtx ();
13780       labelp = cfun->machine->call_via + regno;
13781     }
13782
13783   output_asm_insn ("bl\t%a0", labelp);
13784   return "";
13785 }
13786
13787 /* Routines for generating rtl.  */
13788 void
13789 thumb_expand_movmemqi (rtx *operands)
13790 {
13791   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13792   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13793   HOST_WIDE_INT len = INTVAL (operands[2]);
13794   HOST_WIDE_INT offset = 0;
13795
13796   while (len >= 12)
13797     {
13798       emit_insn (gen_movmem12b (out, in, out, in));
13799       len -= 12;
13800     }
13801
13802   if (len >= 8)
13803     {
13804       emit_insn (gen_movmem8b (out, in, out, in));
13805       len -= 8;
13806     }
13807
13808   if (len >= 4)
13809     {
13810       rtx reg = gen_reg_rtx (SImode);
13811       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13812       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13813       len -= 4;
13814       offset += 4;
13815     }
13816
13817   if (len >= 2)
13818     {
13819       rtx reg = gen_reg_rtx (HImode);
13820       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13821                                               plus_constant (in, offset))));
13822       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13823                             reg));
13824       len -= 2;
13825       offset += 2;
13826     }
13827
13828   if (len)
13829     {
13830       rtx reg = gen_reg_rtx (QImode);
13831       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13832                                               plus_constant (in, offset))));
13833       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13834                             reg));
13835     }
13836 }
13837
13838 void
13839 thumb_reload_out_hi (rtx *operands)
13840 {
13841   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13842 }
13843
13844 /* Handle reading a half-word from memory during reload.  */
13845 void
13846 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13847 {
13848   abort ();
13849 }
13850
13851 /* Return the length of a function name prefix
13852     that starts with the character 'c'.  */
13853 static int
13854 arm_get_strip_length (int c)
13855 {
13856   switch (c)
13857     {
13858     ARM_NAME_ENCODING_LENGTHS
13859       default: return 0;
13860     }
13861 }
13862
13863 /* Return a pointer to a function's name with any
13864    and all prefix encodings stripped from it.  */
13865 const char *
13866 arm_strip_name_encoding (const char *name)
13867 {
13868   int skip;
13869
13870   while ((skip = arm_get_strip_length (* name)))
13871     name += skip;
13872
13873   return name;
13874 }
13875
13876 /* If there is a '*' anywhere in the name's prefix, then
13877    emit the stripped name verbatim, otherwise prepend an
13878    underscore if leading underscores are being used.  */
13879 void
13880 arm_asm_output_labelref (FILE *stream, const char *name)
13881 {
13882   int skip;
13883   int verbatim = 0;
13884
13885   while ((skip = arm_get_strip_length (* name)))
13886     {
13887       verbatim |= (*name == '*');
13888       name += skip;
13889     }
13890
13891   if (verbatim)
13892     fputs (name, stream);
13893   else
13894     asm_fprintf (stream, "%U%s", name);
13895 }
13896
13897 static void
13898 arm_file_end (void)
13899 {
13900   int regno;
13901
13902   if (! thumb_call_reg_needed)
13903     return;
13904
13905   text_section ();
13906   asm_fprintf (asm_out_file, "\t.code 16\n");
13907   ASM_OUTPUT_ALIGN (asm_out_file, 1);
13908
13909   for (regno = 0; regno < SP_REGNUM; regno++)
13910     {
13911       rtx label = thumb_call_via_label[regno];
13912
13913       if (label != 0)
13914         {
13915           targetm.asm_out.internal_label (asm_out_file, "L",
13916                                           CODE_LABEL_NUMBER (label));
13917           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
13918         }
13919     }
13920 }
13921
13922 rtx aof_pic_label;
13923
13924 #ifdef AOF_ASSEMBLER
13925 /* Special functions only needed when producing AOF syntax assembler.  */
13926
13927 struct pic_chain
13928 {
13929   struct pic_chain * next;
13930   const char * symname;
13931 };
13932
13933 static struct pic_chain * aof_pic_chain = NULL;
13934
13935 rtx
13936 aof_pic_entry (rtx x)
13937 {
13938   struct pic_chain ** chainp;
13939   int offset;
13940
13941   if (aof_pic_label == NULL_RTX)
13942     {
13943       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13944     }
13945
13946   for (offset = 0, chainp = &aof_pic_chain; *chainp;
13947        offset += 4, chainp = &(*chainp)->next)
13948     if ((*chainp)->symname == XSTR (x, 0))
13949       return plus_constant (aof_pic_label, offset);
13950
13951   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13952   (*chainp)->next = NULL;
13953   (*chainp)->symname = XSTR (x, 0);
13954   return plus_constant (aof_pic_label, offset);
13955 }
13956
13957 void
13958 aof_dump_pic_table (FILE *f)
13959 {
13960   struct pic_chain * chain;
13961
13962   if (aof_pic_chain == NULL)
13963     return;
13964
13965   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13966                PIC_OFFSET_TABLE_REGNUM,
13967                PIC_OFFSET_TABLE_REGNUM);
13968   fputs ("|x$adcons|\n", f);
13969
13970   for (chain = aof_pic_chain; chain; chain = chain->next)
13971     {
13972       fputs ("\tDCD\t", f);
13973       assemble_name (f, chain->symname);
13974       fputs ("\n", f);
13975     }
13976 }
13977
13978 int arm_text_section_count = 1;
13979
13980 char *
13981 aof_text_section (void )
13982 {
13983   static char buf[100];
13984   sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13985            arm_text_section_count++);
13986   if (flag_pic)
13987     strcat (buf, ", PIC, REENTRANT");
13988   return buf;
13989 }
13990
13991 static int arm_data_section_count = 1;
13992
13993 char *
13994 aof_data_section (void)
13995 {
13996   static char buf[100];
13997   sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13998   return buf;
13999 }
14000
14001 /* The AOF assembler is religiously strict about declarations of
14002    imported and exported symbols, so that it is impossible to declare
14003    a function as imported near the beginning of the file, and then to
14004    export it later on.  It is, however, possible to delay the decision
14005    until all the functions in the file have been compiled.  To get
14006    around this, we maintain a list of the imports and exports, and
14007    delete from it any that are subsequently defined.  At the end of
14008    compilation we spit the remainder of the list out before the END
14009    directive.  */
14010
14011 struct import
14012 {
14013   struct import * next;
14014   const char * name;
14015 };
14016
14017 static struct import * imports_list = NULL;
14018
14019 void
14020 aof_add_import (const char *name)
14021 {
14022   struct import * new;
14023
14024   for (new = imports_list; new; new = new->next)
14025     if (new->name == name)
14026       return;
14027
14028   new = (struct import *) xmalloc (sizeof (struct import));
14029   new->next = imports_list;
14030   imports_list = new;
14031   new->name = name;
14032 }
14033
14034 void
14035 aof_delete_import (const char *name)
14036 {
14037   struct import ** old;
14038
14039   for (old = &imports_list; *old; old = & (*old)->next)
14040     {
14041       if ((*old)->name == name)
14042         {
14043           *old = (*old)->next;
14044           return;
14045         }
14046     }
14047 }
14048
14049 int arm_main_function = 0;
14050
14051 static void
14052 aof_dump_imports (FILE *f)
14053 {
14054   /* The AOF assembler needs this to cause the startup code to be extracted
14055      from the library.  Brining in __main causes the whole thing to work
14056      automagically.  */
14057   if (arm_main_function)
14058     {
14059       text_section ();
14060       fputs ("\tIMPORT __main\n", f);
14061       fputs ("\tDCD __main\n", f);
14062     }
14063
14064   /* Now dump the remaining imports.  */
14065   while (imports_list)
14066     {
14067       fprintf (f, "\tIMPORT\t");
14068       assemble_name (f, imports_list->name);
14069       fputc ('\n', f);
14070       imports_list = imports_list->next;
14071     }
14072 }
14073
14074 static void
14075 aof_globalize_label (FILE *stream, const char *name)
14076 {
14077   default_globalize_label (stream, name);
14078   if (! strcmp (name, "main"))
14079     arm_main_function = 1;
14080 }
14081
14082 static void
14083 aof_file_start (void)
14084 {
14085   fputs ("__r0\tRN\t0\n", asm_out_file);
14086   fputs ("__a1\tRN\t0\n", asm_out_file);
14087   fputs ("__a2\tRN\t1\n", asm_out_file);
14088   fputs ("__a3\tRN\t2\n", asm_out_file);
14089   fputs ("__a4\tRN\t3\n", asm_out_file);
14090   fputs ("__v1\tRN\t4\n", asm_out_file);
14091   fputs ("__v2\tRN\t5\n", asm_out_file);
14092   fputs ("__v3\tRN\t6\n", asm_out_file);
14093   fputs ("__v4\tRN\t7\n", asm_out_file);
14094   fputs ("__v5\tRN\t8\n", asm_out_file);
14095   fputs ("__v6\tRN\t9\n", asm_out_file);
14096   fputs ("__sl\tRN\t10\n", asm_out_file);
14097   fputs ("__fp\tRN\t11\n", asm_out_file);
14098   fputs ("__ip\tRN\t12\n", asm_out_file);
14099   fputs ("__sp\tRN\t13\n", asm_out_file);
14100   fputs ("__lr\tRN\t14\n", asm_out_file);
14101   fputs ("__pc\tRN\t15\n", asm_out_file);
14102   fputs ("__f0\tFN\t0\n", asm_out_file);
14103   fputs ("__f1\tFN\t1\n", asm_out_file);
14104   fputs ("__f2\tFN\t2\n", asm_out_file);
14105   fputs ("__f3\tFN\t3\n", asm_out_file);
14106   fputs ("__f4\tFN\t4\n", asm_out_file);
14107   fputs ("__f5\tFN\t5\n", asm_out_file);
14108   fputs ("__f6\tFN\t6\n", asm_out_file);
14109   fputs ("__f7\tFN\t7\n", asm_out_file);
14110   text_section ();
14111 }
14112
14113 static void
14114 aof_file_end (void)
14115 {
14116   if (flag_pic)
14117     aof_dump_pic_table (asm_out_file);
14118   arm_file_end ();
14119   aof_dump_imports (asm_out_file);
14120   fputs ("\tEND\n", asm_out_file);
14121 }
14122 #endif /* AOF_ASSEMBLER */
14123
14124 #ifndef ARM_PE
14125 /* Symbols in the text segment can be accessed without indirecting via the
14126    constant pool; it may take an extra binary operation, but this is still
14127    faster than indirecting via memory.  Don't do this when not optimizing,
14128    since we won't be calculating al of the offsets necessary to do this
14129    simplification.  */
14130
14131 static void
14132 arm_encode_section_info (tree decl, rtx rtl, int first)
14133 {
14134   /* This doesn't work with AOF syntax, since the string table may be in
14135      a different AREA.  */
14136 #ifndef AOF_ASSEMBLER
14137   if (optimize > 0 && TREE_CONSTANT (decl))
14138     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14139 #endif
14140
14141   /* If we are referencing a function that is weak then encode a long call
14142      flag in the function name, otherwise if the function is static or
14143      or known to be defined in this file then encode a short call flag.  */
14144   if (first && DECL_P (decl))
14145     {
14146       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14147         arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14148       else if (! TREE_PUBLIC (decl))
14149         arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14150     }
14151 }
14152 #endif /* !ARM_PE */
14153
14154 static void
14155 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14156 {
14157   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14158       && !strcmp (prefix, "L"))
14159     {
14160       arm_ccfsm_state = 0;
14161       arm_target_insn = NULL;
14162     }
14163   default_internal_label (stream, prefix, labelno);
14164 }
14165
14166 /* Output code to add DELTA to the first argument, and then jump
14167    to FUNCTION.  Used for C++ multiple inheritance.  */
14168 static void
14169 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14170                      HOST_WIDE_INT delta,
14171                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14172                      tree function)
14173 {
14174   static int thunk_label = 0;
14175   char label[256];
14176   int mi_delta = delta;
14177   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14178   int shift = 0;
14179   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14180                     ? 1 : 0);
14181   if (mi_delta < 0)
14182     mi_delta = - mi_delta;
14183   if (TARGET_THUMB)
14184     {
14185       int labelno = thunk_label++;
14186       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14187       fputs ("\tldr\tr12, ", file);
14188       assemble_name (file, label);
14189       fputc ('\n', file);
14190     }
14191   while (mi_delta != 0)
14192     {
14193       if ((mi_delta & (3 << shift)) == 0)
14194         shift += 2;
14195       else
14196         {
14197           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14198                        mi_op, this_regno, this_regno,
14199                        mi_delta & (0xff << shift));
14200           mi_delta &= ~(0xff << shift);
14201           shift += 8;
14202         }
14203     }
14204   if (TARGET_THUMB)
14205     {
14206       fprintf (file, "\tbx\tr12\n");
14207       ASM_OUTPUT_ALIGN (file, 2);
14208       assemble_name (file, label);
14209       fputs (":\n", file);
14210       assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14211     }
14212   else
14213     {
14214       fputs ("\tb\t", file);
14215       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14216       if (NEED_PLT_RELOC)
14217         fputs ("(PLT)", file);
14218       fputc ('\n', file);
14219     }
14220 }
14221
14222 int
14223 arm_emit_vector_const (FILE *file, rtx x)
14224 {
14225   int i;
14226   const char * pattern;
14227
14228   if (GET_CODE (x) != CONST_VECTOR)
14229     abort ();
14230
14231   switch (GET_MODE (x))
14232     {
14233     case V2SImode: pattern = "%08x"; break;
14234     case V4HImode: pattern = "%04x"; break;
14235     case V8QImode: pattern = "%02x"; break;
14236     default:       abort ();
14237     }
14238
14239   fprintf (file, "0x");
14240   for (i = CONST_VECTOR_NUNITS (x); i--;)
14241     {
14242       rtx element;
14243
14244       element = CONST_VECTOR_ELT (x, i);
14245       fprintf (file, pattern, INTVAL (element));
14246     }
14247
14248   return 1;
14249 }
14250
14251 const char *
14252 arm_output_load_gr (rtx *operands)
14253 {
14254   rtx reg;
14255   rtx offset;
14256   rtx wcgr;
14257   rtx sum;
14258
14259   if (GET_CODE (operands [1]) != MEM
14260       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14261       || GET_CODE (reg = XEXP (sum, 0)) != REG
14262       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14263       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14264     return "wldrw%?\t%0, %1";
14265
14266   /* Fix up an out-of-range load of a GR register.  */
14267   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14268   wcgr = operands[0];
14269   operands[0] = reg;
14270   output_asm_insn ("ldr%?\t%0, %1", operands);
14271
14272   operands[0] = wcgr;
14273   operands[1] = reg;
14274   output_asm_insn ("tmcr%?\t%0, %1", operands);
14275   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14276
14277   return "";
14278 }
14279
14280 static rtx
14281 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14282                       int incoming ATTRIBUTE_UNUSED)
14283 {
14284 #if 0
14285   /* FIXME: The ARM backend has special code to handle structure
14286          returns, and will reserve its own hidden first argument.  So
14287          if this macro is enabled a *second* hidden argument will be
14288          reserved, which will break binary compatibility with old
14289          toolchains and also thunk handling.  One day this should be
14290          fixed.  */
14291   return 0;
14292 #else
14293   /* Register in which address to store a structure value
14294      is passed to a function.  */
14295   return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14296 #endif
14297 }
14298
14299 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14300
14301    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14302    named arg and all anonymous args onto the stack.
14303    XXX I know the prologue shouldn't be pushing registers, but it is faster
14304    that way.  */
14305
14306 static void
14307 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14308                             enum machine_mode mode ATTRIBUTE_UNUSED,
14309                             tree type ATTRIBUTE_UNUSED,
14310                             int *pretend_size,
14311                             int second_time ATTRIBUTE_UNUSED)
14312 {
14313   cfun->machine->uses_anonymous_args = 1;
14314   if (cum->nregs < NUM_ARG_REGS)
14315     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14316 }
14317
14318 /* Return nonzero if the CONSUMER instruction (a store) does not need
14319    PRODUCER's value to calculate the address.  */
14320
14321 int
14322 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14323 {
14324   rtx value = PATTERN (producer);
14325   rtx addr = PATTERN (consumer);
14326
14327   if (GET_CODE (value) == COND_EXEC)
14328     value = COND_EXEC_CODE (value);
14329   if (GET_CODE (value) == PARALLEL)
14330     value = XVECEXP (value, 0, 0);
14331   value = XEXP (value, 0);
14332   if (GET_CODE (addr) == COND_EXEC)
14333     addr = COND_EXEC_CODE (addr);
14334   if (GET_CODE (addr) == PARALLEL)
14335     addr = XVECEXP (addr, 0, 0);
14336   addr = XEXP (addr, 0);
14337
14338   return !reg_overlap_mentioned_p (value, addr);
14339 }
14340
14341 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14342    have an early register shift value or amount dependency on the
14343    result of PRODUCER.  */
14344
14345 int
14346 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14347 {
14348   rtx value = PATTERN (producer);
14349   rtx op = PATTERN (consumer);
14350   rtx early_op;
14351
14352   if (GET_CODE (value) == COND_EXEC)
14353     value = COND_EXEC_CODE (value);
14354   if (GET_CODE (value) == PARALLEL)
14355     value = XVECEXP (value, 0, 0);
14356   value = XEXP (value, 0);
14357   if (GET_CODE (op) == COND_EXEC)
14358     op = COND_EXEC_CODE (op);
14359   if (GET_CODE (op) == PARALLEL)
14360     op = XVECEXP (op, 0, 0);
14361   op = XEXP (op, 1);
14362
14363   early_op = XEXP (op, 0);
14364   /* This is either an actual independent shift, or a shift applied to
14365      the first operand of another operation.  We want the whole shift
14366      operation.  */
14367   if (GET_CODE (early_op) == REG)
14368     early_op = op;
14369
14370   return !reg_overlap_mentioned_p (value, early_op);
14371 }
14372
14373 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14374    have an early register shift value dependency on the result of
14375    PRODUCER.  */
14376
14377 int
14378 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14379 {
14380   rtx value = PATTERN (producer);
14381   rtx op = PATTERN (consumer);
14382   rtx early_op;
14383
14384   if (GET_CODE (value) == COND_EXEC)
14385     value = COND_EXEC_CODE (value);
14386   if (GET_CODE (value) == PARALLEL)
14387     value = XVECEXP (value, 0, 0);
14388   value = XEXP (value, 0);
14389   if (GET_CODE (op) == COND_EXEC)
14390     op = COND_EXEC_CODE (op);
14391   if (GET_CODE (op) == PARALLEL)
14392     op = XVECEXP (op, 0, 0);
14393   op = XEXP (op, 1);
14394
14395   early_op = XEXP (op, 0);
14396
14397   /* This is either an actual independent shift, or a shift applied to
14398      the first operand of another operation.  We want the value being
14399      shifted, in either case.  */
14400   if (GET_CODE (early_op) != REG)
14401     early_op = XEXP (early_op, 0);
14402
14403   return !reg_overlap_mentioned_p (value, early_op);
14404 }
14405
14406 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14407    have an early register mult dependency on the result of
14408    PRODUCER.  */
14409
14410 int
14411 arm_no_early_mul_dep (rtx producer, rtx consumer)
14412 {
14413   rtx value = PATTERN (producer);
14414   rtx op = PATTERN (consumer);
14415
14416   if (GET_CODE (value) == COND_EXEC)
14417     value = COND_EXEC_CODE (value);
14418   if (GET_CODE (value) == PARALLEL)
14419     value = XVECEXP (value, 0, 0);
14420   value = XEXP (value, 0);
14421   if (GET_CODE (op) == COND_EXEC)
14422     op = COND_EXEC_CODE (op);
14423   if (GET_CODE (op) == PARALLEL)
14424     op = XVECEXP (op, 0, 0);
14425   op = XEXP (op, 1);
14426
14427   return (GET_CODE (op) == PLUS
14428           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14429 }
14430
14431
14432 /* We can't rely on the caller doing the proper promotion when
14433    using APCS or ATPCS.  */
14434
14435 static bool
14436 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14437 {
14438     return !TARGET_AAPCS_BASED;
14439 }
14440
14441
14442 /* AAPCS based ABIs use short enums by default.  */
14443
14444 static bool
14445 arm_default_short_enums (void)
14446 {
14447   return TARGET_AAPCS_BASED;
14448 }
14449
14450
14451 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
14452
14453 static bool
14454 arm_align_anon_bitfield (void)
14455 {
14456   return TARGET_AAPCS_BASED;
14457 }
14458
14459
14460 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
14461
14462 static tree
14463 arm_cxx_guard_type (void)
14464 {
14465   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14466 }
14467
14468
14469 /* The EABI says test the least significan bit of a guard variable.  */
14470
14471 static bool
14472 arm_cxx_guard_mask_bit (void)
14473 {
14474   return TARGET_AAPCS_BASED;
14475 }
14476
14477
14478 /* The EABI specifies that all array cookies are 8 bytes long.  */
14479
14480 static tree
14481 arm_get_cookie_size (tree type)
14482 {
14483   tree size;
14484
14485   if (!TARGET_AAPCS_BASED)
14486     return default_cxx_get_cookie_size (type);
14487
14488   size = build_int_cst (sizetype, 8);
14489   return size;
14490 }
14491
14492
14493 /* The EABI says that array cookies should also contain the element size.  */
14494
14495 static bool
14496 arm_cookie_has_size (void)
14497 {
14498   return TARGET_AAPCS_BASED;
14499 }
14500
14501
14502 /* The EABI says constructors and destructors should return a pointer to
14503    the object constructed/destroyed.  */
14504
14505 static bool
14506 arm_cxx_cdtor_returns_this (void)
14507 {
14508   return TARGET_AAPCS_BASED;
14509 }
14510
14511 /* The EABI says that an inline function may never be the key
14512    method.  */
14513
14514 static bool
14515 arm_cxx_key_method_may_be_inline (void)
14516 {
14517   return !TARGET_AAPCS_BASED;
14518 }
14519
14520 /* The EABI says that the virtual table, etc., for a class must be
14521    exported if it has a key method.  The EABI does not specific the
14522    behavior if there is no key method, but there is no harm in
14523    exporting the class data in that case too.  */
14524
14525 static bool
14526 arm_cxx_export_class_data (void)
14527 {
14528   return TARGET_AAPCS_BASED;
14529 }
14530
14531 void
14532 arm_set_return_address (rtx source, rtx scratch)
14533 {
14534   arm_stack_offsets *offsets;
14535   HOST_WIDE_INT delta;
14536   rtx addr;
14537   unsigned long saved_regs;
14538
14539   saved_regs = arm_compute_save_reg_mask ();
14540
14541   if ((saved_regs & (1 << LR_REGNUM)) == 0)
14542     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14543   else
14544     {
14545       if (frame_pointer_needed)
14546         addr = plus_constant(hard_frame_pointer_rtx, -4);
14547       else
14548         {
14549           /* LR will be the first saved register.  */
14550           offsets = arm_get_frame_offsets ();
14551           delta = offsets->outgoing_args - (offsets->frame + 4);
14552
14553
14554           if (delta >= 4096)
14555             {
14556               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
14557                                      GEN_INT (delta & ~4095)));
14558               addr = scratch;
14559               delta &= 4095;
14560             }
14561           else
14562             addr = stack_pointer_rtx;
14563
14564           addr = plus_constant (addr, delta);
14565         }
14566       emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14567     }
14568 }
14569
14570
14571 void
14572 thumb_set_return_address (rtx source, rtx scratch)
14573 {
14574   arm_stack_offsets *offsets;
14575   HOST_WIDE_INT delta;
14576   int reg;
14577   rtx addr;
14578   unsigned long mask;
14579
14580   emit_insn (gen_rtx_USE (VOIDmode, source));
14581
14582   mask = thumb_compute_save_reg_mask ();
14583   if (mask & (1 << LR_REGNUM))
14584     {
14585       offsets = arm_get_frame_offsets ();
14586
14587       /* Find the saved regs.  */
14588       if (frame_pointer_needed)
14589         {
14590           delta = offsets->soft_frame - offsets->saved_args;
14591           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
14592         }
14593       else
14594         {
14595           delta = offsets->outgoing_args - offsets->saved_args;
14596           reg = SP_REGNUM;
14597         }
14598       /* Allow for the stack frame.  */
14599       if (TARGET_BACKTRACE)
14600         delta -= 16;
14601       /* The link register is always the first saved register.  */
14602       delta -= 4;
14603
14604       /* Construct the address.  */
14605       addr = gen_rtx_REG (SImode, reg);
14606       if ((reg != SP_REGNUM && delta >= 128)
14607           || delta >= 1024)
14608         {
14609           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
14610           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
14611           addr = scratch;
14612         }
14613       else
14614         addr = plus_constant (addr, delta);
14615
14616       emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14617     }
14618   else
14619     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14620 }
14621
14622 /* Implements target hook vector_mode_supported_p.  */
14623 bool
14624 arm_vector_mode_supported_p (enum machine_mode mode)
14625 {
14626   if ((mode == V2SImode)
14627       || (mode == V4HImode)
14628       || (mode == V8QImode))
14629     return true;
14630
14631   return false;
14632 }
14633
14634 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
14635    ARM insns and therefore guarantee that the shift count is modulo 256.
14636    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
14637    guarantee no particular behavior for out-of-range counts.  */
14638
14639 static unsigned HOST_WIDE_INT
14640 arm_shift_truncation_mask (enum machine_mode mode)
14641 {
14642   return mode == SImode ? 255 : 0;
14643 }