OSDN Git Service

* arm.c (arm_const_double_inline_cost): Handle any constant by
[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 thumb_exit (FILE *, int);
88 static rtx is_jump_table (rtx);
89 static HOST_WIDE_INT get_jump_table_size (rtx);
90 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
91 static Mnode *add_minipool_forward_ref (Mfix *);
92 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
93 static Mnode *add_minipool_backward_ref (Mfix *);
94 static void assign_minipool_offsets (Mfix *);
95 static void arm_print_value (FILE *, rtx);
96 static void dump_minipool (rtx);
97 static int arm_barrier_cost (rtx);
98 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
99 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
100 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
101                                rtx);
102 static void arm_reorg (void);
103 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
104 static int current_file_function_operand (rtx);
105 static unsigned long arm_compute_save_reg0_reg12_mask (void);
106 static unsigned long arm_compute_save_reg_mask (void);
107 static unsigned long arm_isr_value (tree);
108 static unsigned long arm_compute_func_type (void);
109 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
110 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
111 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
112 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
113 #endif
114 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
115 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
116 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
117 static int arm_comp_type_attributes (tree, tree);
118 static void arm_set_default_type_attributes (tree);
119 static int arm_adjust_cost (rtx, rtx, rtx, int);
120 static int count_insns_for_constant (HOST_WIDE_INT, int);
121 static int arm_get_strip_length (int);
122 static bool arm_function_ok_for_sibcall (tree, tree);
123 static void arm_internal_label (FILE *, const char *, unsigned long);
124 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
125                                  tree);
126 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
127 static bool arm_size_rtx_costs (rtx, int, int, int *);
128 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
129 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
130 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
131 static bool arm_9e_rtx_costs (rtx, int, int, int *);
132 static int arm_address_cost (rtx);
133 static bool arm_memory_load_p (rtx);
134 static bool arm_cirrus_insn_p (rtx);
135 static void cirrus_reorg (rtx);
136 static void arm_init_builtins (void);
137 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
138 static void arm_init_iwmmxt_builtins (void);
139 static rtx safe_vector_operand (rtx, enum machine_mode);
140 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
141 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
142 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
143 static void emit_constant_insn (rtx cond, rtx pattern);
144 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
145                                   tree, bool);
146
147 #ifndef ARM_PE
148 static void arm_encode_section_info (tree, rtx, int);
149 #endif
150
151 static void arm_file_end (void);
152
153 #ifdef AOF_ASSEMBLER
154 static void aof_globalize_label (FILE *, const char *);
155 static void aof_dump_imports (FILE *);
156 static void aof_dump_pic_table (FILE *);
157 static void aof_file_start (void);
158 static void aof_file_end (void);
159 #endif
160 static rtx arm_struct_value_rtx (tree, int);
161 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
162                                         tree, int *, int);
163 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
164                                    enum machine_mode, tree, bool);
165 static bool arm_promote_prototypes (tree);
166 static bool arm_default_short_enums (void);
167 static bool arm_align_anon_bitfield (void);
168
169 static tree arm_cxx_guard_type (void);
170 static bool arm_cxx_guard_mask_bit (void);
171 static tree arm_get_cookie_size (tree);
172 static bool arm_cookie_has_size (void);
173 static bool arm_cxx_cdtor_returns_this (void);
174 static bool arm_cxx_key_method_may_be_inline (void);
175 static bool arm_cxx_export_class_data (void);
176 static void arm_init_libfuncs (void);
177 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
178 \f
179 /* Initialize the GCC target structure.  */
180 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
181 #undef  TARGET_MERGE_DECL_ATTRIBUTES
182 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
183 #endif
184
185 #undef  TARGET_ATTRIBUTE_TABLE
186 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
187
188 #undef TARGET_ASM_FILE_END
189 #define TARGET_ASM_FILE_END arm_file_end
190
191 #ifdef AOF_ASSEMBLER
192 #undef  TARGET_ASM_BYTE_OP
193 #define TARGET_ASM_BYTE_OP "\tDCB\t"
194 #undef  TARGET_ASM_ALIGNED_HI_OP
195 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
196 #undef  TARGET_ASM_ALIGNED_SI_OP
197 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
198 #undef TARGET_ASM_GLOBALIZE_LABEL
199 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
200 #undef TARGET_ASM_FILE_START
201 #define TARGET_ASM_FILE_START aof_file_start
202 #undef TARGET_ASM_FILE_END
203 #define TARGET_ASM_FILE_END aof_file_end
204 #else
205 #undef  TARGET_ASM_ALIGNED_SI_OP
206 #define TARGET_ASM_ALIGNED_SI_OP NULL
207 #undef  TARGET_ASM_INTEGER
208 #define TARGET_ASM_INTEGER arm_assemble_integer
209 #endif
210
211 #undef  TARGET_ASM_FUNCTION_PROLOGUE
212 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
213
214 #undef  TARGET_ASM_FUNCTION_EPILOGUE
215 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
216
217 #undef  TARGET_COMP_TYPE_ATTRIBUTES
218 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
219
220 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
221 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
222
223 #undef  TARGET_SCHED_ADJUST_COST
224 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
225
226 #undef TARGET_ENCODE_SECTION_INFO
227 #ifdef ARM_PE
228 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
229 #else
230 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
231 #endif
232
233 #undef  TARGET_STRIP_NAME_ENCODING
234 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
235
236 #undef  TARGET_ASM_INTERNAL_LABEL
237 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
238
239 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
240 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
241
242 #undef  TARGET_ASM_OUTPUT_MI_THUNK
243 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
244 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
245 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
246
247 /* This will be overridden in arm_override_options.  */
248 #undef  TARGET_RTX_COSTS
249 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
250 #undef  TARGET_ADDRESS_COST
251 #define TARGET_ADDRESS_COST arm_address_cost
252
253 #undef TARGET_SHIFT_TRUNCATION_MASK
254 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
255 #undef TARGET_VECTOR_MODE_SUPPORTED_P
256 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
257
258 #undef  TARGET_MACHINE_DEPENDENT_REORG
259 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
260
261 #undef  TARGET_INIT_BUILTINS
262 #define TARGET_INIT_BUILTINS  arm_init_builtins
263 #undef  TARGET_EXPAND_BUILTIN
264 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
265
266 #undef TARGET_INIT_LIBFUNCS
267 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
268
269 #undef TARGET_PROMOTE_FUNCTION_ARGS
270 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
271 #undef TARGET_PROMOTE_FUNCTION_RETURN
272 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
273 #undef TARGET_PROMOTE_PROTOTYPES
274 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
275 #undef TARGET_PASS_BY_REFERENCE
276 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
277 #undef TARGET_ARG_PARTIAL_BYTES
278 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
279
280 #undef TARGET_STRUCT_VALUE_RTX
281 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
282
283 #undef  TARGET_SETUP_INCOMING_VARARGS
284 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
285
286 #undef TARGET_DEFAULT_SHORT_ENUMS
287 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
288
289 #undef TARGET_ALIGN_ANON_BITFIELD
290 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
291
292 #undef TARGET_CXX_GUARD_TYPE
293 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
294
295 #undef TARGET_CXX_GUARD_MASK_BIT
296 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
297
298 #undef TARGET_CXX_GET_COOKIE_SIZE
299 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
300
301 #undef TARGET_CXX_COOKIE_HAS_SIZE
302 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
303
304 #undef TARGET_CXX_CDTOR_RETURNS_THIS
305 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
306
307 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
308 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
309
310 #undef TARGET_CXX_EXPORT_CLASS_DATA
311 #define TARGET_CXX_EXPORT_CLASS_DATA arm_cxx_export_class_data
312
313 struct gcc_target targetm = TARGET_INITIALIZER;
314 \f
315 /* Obstack for minipool constant handling.  */
316 static struct obstack minipool_obstack;
317 static char *         minipool_startobj;
318
319 /* The maximum number of insns skipped which
320    will be conditionalised if possible.  */
321 static int max_insns_skipped = 5;
322
323 extern FILE * asm_out_file;
324
325 /* True if we are currently building a constant table.  */
326 int making_const_table;
327
328 /* Define the information needed to generate branch insns.  This is
329    stored from the compare operation.  */
330 rtx arm_compare_op0, arm_compare_op1;
331
332 /* The processor for which instructions should be scheduled.  */
333 enum processor_type arm_tune = arm_none;
334
335 /* Which floating point model to use.  */
336 enum arm_fp_model arm_fp_model;
337
338 /* Which floating point hardware is available.  */
339 enum fputype arm_fpu_arch;
340
341 /* Which floating point hardware to schedule for.  */
342 enum fputype arm_fpu_tune;
343
344 /* Whether to use floating point hardware.  */
345 enum float_abi_type arm_float_abi;
346
347 /* Which ABI to use.  */
348 enum arm_abi_type arm_abi;
349
350 /* Set by the -mfpu=... option.  */
351 const char * target_fpu_name = NULL;
352
353 /* Set by the -mfpe=... option.  */
354 const char * target_fpe_name = NULL;
355
356 /* Set by the -mfloat-abi=... option.  */
357 const char * target_float_abi_name = NULL;
358
359 /* Set by the legacy -mhard-float and -msoft-float options.  */
360 const char * target_float_switch = NULL;
361
362 /* Set by the -mabi=... option.  */
363 const char * target_abi_name = NULL;
364
365 /* Used to parse -mstructure_size_boundary command line option.  */
366 const char * structure_size_string = NULL;
367 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
368
369 /* Used for Thumb call_via trampolines.  */
370 rtx thumb_call_via_label[14];
371 static int thumb_call_reg_needed;
372
373 /* Bit values used to identify processor capabilities.  */
374 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
375 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
376 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
377 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
378 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
379 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
380 #define FL_THUMB      (1 << 6)        /* Thumb aware */
381 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
382 #define FL_STRONG     (1 << 8)        /* StrongARM */
383 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
384 #define FL_XSCALE     (1 << 10)       /* XScale */
385 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
386 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
387                                          media instructions.  */
388 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
389
390 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
391
392 #define FL_FOR_ARCH2    0
393 #define FL_FOR_ARCH3    FL_MODE32
394 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
395 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
396 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
397 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
398 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
399 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
400 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
401 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
402 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
403 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
404 #define FL_FOR_ARCH6K   FL_FOR_ARCH6
405 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
406 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6
407
408 /* The bits in this mask specify which
409    instructions we are allowed to generate.  */
410 static unsigned long insn_flags = 0;
411
412 /* The bits in this mask specify which instruction scheduling options should
413    be used.  */
414 static unsigned long tune_flags = 0;
415
416 /* The following are used in the arm.md file as equivalents to bits
417    in the above two flag variables.  */
418
419 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
420 int arm_arch3m = 0;
421
422 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
423 int arm_arch4 = 0;
424
425 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
426 int arm_arch4t = 0;
427
428 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
429 int arm_arch5 = 0;
430
431 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
432 int arm_arch5e = 0;
433
434 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
435 int arm_arch6 = 0;
436
437 /* Nonzero if this chip can benefit from load scheduling.  */
438 int arm_ld_sched = 0;
439
440 /* Nonzero if this chip is a StrongARM.  */
441 int arm_is_strong = 0;
442
443 /* Nonzero if this chip is a Cirrus variant.  */
444 int arm_arch_cirrus = 0;
445
446 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
447 int arm_arch_iwmmxt = 0;
448
449 /* Nonzero if this chip is an XScale.  */
450 int arm_arch_xscale = 0;
451
452 /* Nonzero if tuning for XScale  */
453 int arm_tune_xscale = 0;
454
455 /* Nonzero if this chip is an ARM6 or an ARM7.  */
456 int arm_is_6_or_7 = 0;
457
458 /* Nonzero if generating Thumb instructions.  */
459 int thumb_code = 0;
460
461 /* Nonzero if we should define __THUMB_INTERWORK__ in the
462    preprocessor.
463    XXX This is a bit of a hack, it's intended to help work around
464    problems in GLD which doesn't understand that armv5t code is
465    interworking clean.  */
466 int arm_cpp_interwork = 0;
467
468 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
469    must report the mode of the memory reference from PRINT_OPERAND to
470    PRINT_OPERAND_ADDRESS.  */
471 enum machine_mode output_memory_reference_mode;
472
473 /* The register number to be used for the PIC offset register.  */
474 const char * arm_pic_register_string = NULL;
475 int arm_pic_register = INVALID_REGNUM;
476
477 /* Set to 1 when a return insn is output, this means that the epilogue
478    is not needed.  */
479 int return_used_this_function;
480
481 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
482    the next function.  */
483 static int after_arm_reorg = 0;
484
485 /* The maximum number of insns to be used when loading a constant.  */
486 static int arm_constant_limit = 3;
487
488 /* For an explanation of these variables, see final_prescan_insn below.  */
489 int arm_ccfsm_state;
490 enum arm_cond_code arm_current_cc;
491 rtx arm_target_insn;
492 int arm_target_label;
493
494 /* The condition codes of the ARM, and the inverse function.  */
495 static const char * const arm_condition_codes[] =
496 {
497   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
498   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
499 };
500
501 #define streq(string1, string2) (strcmp (string1, string2) == 0)
502 \f
503 /* Initialization code.  */
504
505 struct processors
506 {
507   const char *const name;
508   enum processor_type core;
509   const char *arch;
510   const unsigned long flags;
511   bool (* rtx_costs) (rtx, int, int, int *);
512 };
513
514 /* Not all of these give usefully different compilation alternatives,
515    but there is no simple way of generalizing them.  */
516 static const struct processors all_cores[] =
517 {
518   /* ARM Cores */
519 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
520   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
521 #include "arm-cores.def"
522 #undef ARM_CORE
523   {NULL, arm_none, NULL, 0, NULL}
524 };
525
526 static const struct processors all_architectures[] =
527 {
528   /* ARM Architectures */
529   /* We don't specify rtx_costs here as it will be figured out
530      from the core.  */
531
532   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
533   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
534   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
535   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
536   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
537   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
538      implementations that support it, so we will leave it out for now.  */
539   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
540   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
541   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
542   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
543   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
544   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
545   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
546   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
547   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
548   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
549   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
550   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
551   {NULL, arm_none, NULL, 0 , NULL}
552 };
553
554 /* This is a magic structure.  The 'string' field is magically filled in
555    with a pointer to the value specified by the user on the command line
556    assuming that the user has specified such a value.  */
557
558 struct arm_cpu_select arm_select[] =
559 {
560   /* string       name            processors  */
561   { NULL,       "-mcpu=",       all_cores  },
562   { NULL,       "-march=",      all_architectures },
563   { NULL,       "-mtune=",      all_cores }
564 };
565
566
567 /* The name of the proprocessor macro to define for this architecture.  */
568
569 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
570
571 struct fpu_desc
572 {
573   const char * name;
574   enum fputype fpu;
575 };
576
577
578 /* Available values for for -mfpu=.  */
579
580 static const struct fpu_desc all_fpus[] =
581 {
582   {"fpa",       FPUTYPE_FPA},
583   {"fpe2",      FPUTYPE_FPA_EMU2},
584   {"fpe3",      FPUTYPE_FPA_EMU2},
585   {"maverick",  FPUTYPE_MAVERICK},
586   {"vfp",       FPUTYPE_VFP}
587 };
588
589
590 /* Floating point models used by the different hardware.
591    See fputype in arm.h.  */
592
593 static const enum fputype fp_model_for_fpu[] =
594 {
595   /* No FP hardware.  */
596   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
597   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
598   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
599   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
600   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
601   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
602 };
603
604
605 struct float_abi
606 {
607   const char * name;
608   enum float_abi_type abi_type;
609 };
610
611
612 /* Available values for -mfloat-abi=.  */
613
614 static const struct float_abi all_float_abis[] =
615 {
616   {"soft",      ARM_FLOAT_ABI_SOFT},
617   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
618   {"hard",      ARM_FLOAT_ABI_HARD}
619 };
620
621
622 struct abi_name
623 {
624   const char *name;
625   enum arm_abi_type abi_type;
626 };
627
628
629 /* Available values for -mabi=.  */
630
631 static const struct abi_name arm_all_abis[] =
632 {
633   {"apcs-gnu",    ARM_ABI_APCS},
634   {"atpcs",   ARM_ABI_ATPCS},
635   {"aapcs",   ARM_ABI_AAPCS},
636   {"iwmmxt",  ARM_ABI_IWMMXT}
637 };
638
639 /* Return the number of bits set in VALUE.  */
640 static unsigned
641 bit_count (unsigned long value)
642 {
643   unsigned long count = 0;
644
645   while (value)
646     {
647       count++;
648       value &= value - 1;  /* Clear the least-significant set bit.  */
649     }
650
651   return count;
652 }
653
654 /* Set up library functions unique to ARM.  */
655
656 static void
657 arm_init_libfuncs (void)
658 {
659   /* There are no special library functions unless we are using the
660      ARM BPABI.  */
661   if (!TARGET_BPABI)
662     return;
663
664   /* The functions below are described in Section 4 of the "Run-Time
665      ABI for the ARM architecture", Version 1.0.  */
666
667   /* Double-precision floating-point arithmetic.  Table 2.  */
668   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
669   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
670   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
671   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
672   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
673
674   /* Double-precision comparisons.  Table 3.  */
675   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
676   set_optab_libfunc (ne_optab, DFmode, NULL);
677   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
678   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
679   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
680   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
681   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
682
683   /* Single-precision floating-point arithmetic.  Table 4.  */
684   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
685   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
686   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
687   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
688   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
689
690   /* Single-precision comparisons.  Table 5.  */
691   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
692   set_optab_libfunc (ne_optab, SFmode, NULL);
693   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
694   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
695   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
696   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
697   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
698
699   /* Floating-point to integer conversions.  Table 6.  */
700   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
701   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
702   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
703   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
704   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
705   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
706   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
707   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
708
709   /* Conversions between floating types.  Table 7.  */
710   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
711   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
712
713   /* Integer to floating-point conversions.  Table 8.  */
714   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
715   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
716   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
717   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
718   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
719   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
720   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
721   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
722
723   /* Long long.  Table 9.  */
724   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
725   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
726   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
727   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
728   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
729   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
730   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
731   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
732
733   /* Integer (32/32->32) division.  \S 4.3.1.  */
734   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
735   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
736
737   /* The divmod functions are designed so that they can be used for
738      plain division, even though they return both the quotient and the
739      remainder.  The quotient is returned in the usual location (i.e.,
740      r0 for SImode, {r0, r1} for DImode), just as would be expected
741      for an ordinary division routine.  Because the AAPCS calling
742      conventions specify that all of { r0, r1, r2, r3 } are
743      callee-saved registers, there is no need to tell the compiler
744      explicitly that those registers are clobbered by these
745      routines.  */
746   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
747   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
748   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idivmod");
749   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidivmod");
750 }
751
752 /* Fix up any incompatible options that the user has specified.
753    This has now turned into a maze.  */
754 void
755 arm_override_options (void)
756 {
757   unsigned i;
758
759   /* Set up the flags based on the cpu/architecture selected by the user.  */
760   for (i = ARRAY_SIZE (arm_select); i--;)
761     {
762       struct arm_cpu_select * ptr = arm_select + i;
763
764       if (ptr->string != NULL && ptr->string[0] != '\0')
765         {
766           const struct processors * sel;
767
768           for (sel = ptr->processors; sel->name != NULL; sel++)
769             if (streq (ptr->string, sel->name))
770               {
771                 /* Set the architecture define.  */
772                 if (i != 2)
773                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
774
775                 /* Determine the processor core for which we should
776                    tune code-generation.  */
777                 if (/* -mcpu= is a sensible default.  */
778                     i == 0
779                     /* If -march= is used, and -mcpu= has not been used,
780                        assume that we should tune for a representative
781                        CPU from that architecture.  */
782                     || i == 1
783                     /* -mtune= overrides -mcpu= and -march=.  */
784                     || i == 2)
785                   arm_tune = (enum processor_type) (sel - ptr->processors);
786
787                 if (i != 2)
788                   {
789                     /* If we have been given an architecture and a processor
790                        make sure that they are compatible.  We only generate
791                        a warning though, and we prefer the CPU over the
792                        architecture.  */
793                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
794                       warning ("switch -mcpu=%s conflicts with -march= switch",
795                                ptr->string);
796
797                     insn_flags = sel->flags;
798                   }
799
800                 break;
801               }
802
803           if (sel->name == NULL)
804             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
805         }
806     }
807
808   /* If the user did not specify a processor, choose one for them.  */
809   if (insn_flags == 0)
810     {
811       const struct processors * sel;
812       unsigned int        sought;
813       enum processor_type cpu;
814
815       cpu = TARGET_CPU_DEFAULT;
816       if (cpu == arm_none)
817         {
818 #ifdef SUBTARGET_CPU_DEFAULT
819           /* Use the subtarget default CPU if none was specified by
820              configure.  */
821           cpu = SUBTARGET_CPU_DEFAULT;
822 #endif
823           /* Default to ARM6.  */
824           if (cpu == arm_none)
825             cpu = arm6;
826         }
827       sel = &all_cores[cpu];
828
829       insn_flags = sel->flags;
830
831       /* Now check to see if the user has specified some command line
832          switch that require certain abilities from the cpu.  */
833       sought = 0;
834
835       if (TARGET_INTERWORK || TARGET_THUMB)
836         {
837           sought |= (FL_THUMB | FL_MODE32);
838
839           /* There are no ARM processors that support both APCS-26 and
840              interworking.  Therefore we force FL_MODE26 to be removed
841              from insn_flags here (if it was set), so that the search
842              below will always be able to find a compatible processor.  */
843           insn_flags &= ~FL_MODE26;
844         }
845
846       if (sought != 0 && ((sought & insn_flags) != sought))
847         {
848           /* Try to locate a CPU type that supports all of the abilities
849              of the default CPU, plus the extra abilities requested by
850              the user.  */
851           for (sel = all_cores; sel->name != NULL; sel++)
852             if ((sel->flags & sought) == (sought | insn_flags))
853               break;
854
855           if (sel->name == NULL)
856             {
857               unsigned current_bit_count = 0;
858               const struct processors * best_fit = NULL;
859
860               /* Ideally we would like to issue an error message here
861                  saying that it was not possible to find a CPU compatible
862                  with the default CPU, but which also supports the command
863                  line options specified by the programmer, and so they
864                  ought to use the -mcpu=<name> command line option to
865                  override the default CPU type.
866
867                  If we cannot find a cpu that has both the
868                  characteristics of the default cpu and the given
869                  command line options we scan the array again looking
870                  for a best match.  */
871               for (sel = all_cores; sel->name != NULL; sel++)
872                 if ((sel->flags & sought) == sought)
873                   {
874                     unsigned count;
875
876                     count = bit_count (sel->flags & insn_flags);
877
878                     if (count >= current_bit_count)
879                       {
880                         best_fit = sel;
881                         current_bit_count = count;
882                       }
883                   }
884
885               if (best_fit == NULL)
886                 abort ();
887               else
888                 sel = best_fit;
889             }
890
891           insn_flags = sel->flags;
892         }
893       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
894       if (arm_tune == arm_none)
895         arm_tune = (enum processor_type) (sel - all_cores);
896     }
897
898   /* The processor for which we should tune should now have been
899      chosen.  */
900   if (arm_tune == arm_none)
901     abort ();
902
903   tune_flags = all_cores[(int)arm_tune].flags;
904   if (optimize_size)
905     targetm.rtx_costs = arm_size_rtx_costs;
906   else
907     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
908
909   /* Make sure that the processor choice does not conflict with any of the
910      other command line choices.  */
911   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
912     {
913       warning ("target CPU does not support interworking" );
914       target_flags &= ~ARM_FLAG_INTERWORK;
915     }
916
917   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
918     {
919       warning ("target CPU does not support THUMB instructions");
920       target_flags &= ~ARM_FLAG_THUMB;
921     }
922
923   if (TARGET_APCS_FRAME && TARGET_THUMB)
924     {
925       /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
926       target_flags &= ~ARM_FLAG_APCS_FRAME;
927     }
928
929   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
930      from here where no function is being compiled currently.  */
931   if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
932       && TARGET_ARM)
933     warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
934
935   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
936     warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
937
938   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
939     warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
940
941   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
942     {
943       warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
944       target_flags |= ARM_FLAG_APCS_FRAME;
945     }
946
947   if (TARGET_POKE_FUNCTION_NAME)
948     target_flags |= ARM_FLAG_APCS_FRAME;
949
950   if (TARGET_APCS_REENT && flag_pic)
951     error ("-fpic and -mapcs-reent are incompatible");
952
953   if (TARGET_APCS_REENT)
954     warning ("APCS reentrant code not supported.  Ignored");
955
956   /* If this target is normally configured to use APCS frames, warn if they
957      are turned off and debugging is turned on.  */
958   if (TARGET_ARM
959       && write_symbols != NO_DEBUG
960       && !TARGET_APCS_FRAME
961       && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
962     warning ("-g with -mno-apcs-frame may not give sensible debugging");
963
964   /* If stack checking is disabled, we can use r10 as the PIC register,
965      which keeps r9 available.  */
966   if (flag_pic)
967     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
968
969   if (TARGET_APCS_FLOAT)
970     warning ("passing floating point arguments in fp regs not yet supported");
971
972   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
973   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
974   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
975   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
976   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
977   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
978   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
979   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
980   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
981
982   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
983   arm_is_strong = (tune_flags & FL_STRONG) != 0;
984   thumb_code = (TARGET_ARM == 0);
985   arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
986                     && !(tune_flags & FL_ARCH4))) != 0;
987   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
988   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
989
990   /* V5 code we generate is completely interworking capable, so we turn off
991      TARGET_INTERWORK here to avoid many tests later on.  */
992
993   /* XXX However, we must pass the right pre-processor defines to CPP
994      or GLD can get confused.  This is a hack.  */
995   if (TARGET_INTERWORK)
996     arm_cpp_interwork = 1;
997
998   if (arm_arch5)
999     target_flags &= ~ARM_FLAG_INTERWORK;
1000
1001   if (target_abi_name)
1002     {
1003       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1004         {
1005           if (streq (arm_all_abis[i].name, target_abi_name))
1006             {
1007               arm_abi = arm_all_abis[i].abi_type;
1008               break;
1009             }
1010         }
1011       if (i == ARRAY_SIZE (arm_all_abis))
1012         error ("invalid ABI option: -mabi=%s", target_abi_name);
1013     }
1014   else
1015     arm_abi = ARM_DEFAULT_ABI;
1016
1017   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1018     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1019
1020   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1021     error ("iwmmxt abi requires an iwmmxt capable cpu");
1022
1023   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1024   if (target_fpu_name == NULL && target_fpe_name != NULL)
1025     {
1026       if (streq (target_fpe_name, "2"))
1027         target_fpu_name = "fpe2";
1028       else if (streq (target_fpe_name, "3"))
1029         target_fpu_name = "fpe3";
1030       else
1031         error ("invalid floating point emulation option: -mfpe=%s",
1032                target_fpe_name);
1033     }
1034   if (target_fpu_name != NULL)
1035     {
1036       /* The user specified a FPU.  */
1037       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1038         {
1039           if (streq (all_fpus[i].name, target_fpu_name))
1040             {
1041               arm_fpu_arch = all_fpus[i].fpu;
1042               arm_fpu_tune = arm_fpu_arch;
1043               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1044               break;
1045             }
1046         }
1047       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1048         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1049     }
1050   else
1051     {
1052 #ifdef FPUTYPE_DEFAULT
1053       /* Use the default if it is specified for this platform.  */
1054       arm_fpu_arch = FPUTYPE_DEFAULT;
1055       arm_fpu_tune = FPUTYPE_DEFAULT;
1056 #else
1057       /* Pick one based on CPU type.  */
1058       /* ??? Some targets assume FPA is the default.
1059       if ((insn_flags & FL_VFP) != 0)
1060         arm_fpu_arch = FPUTYPE_VFP;
1061       else
1062       */
1063       if (arm_arch_cirrus)
1064         arm_fpu_arch = FPUTYPE_MAVERICK;
1065       else
1066         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1067 #endif
1068       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1069         arm_fpu_tune = FPUTYPE_FPA;
1070       else
1071         arm_fpu_tune = arm_fpu_arch;
1072       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1073       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1074         abort ();
1075     }
1076
1077   if (target_float_abi_name != NULL)
1078     {
1079       /* The user specified a FP ABI.  */
1080       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1081         {
1082           if (streq (all_float_abis[i].name, target_float_abi_name))
1083             {
1084               arm_float_abi = all_float_abis[i].abi_type;
1085               break;
1086             }
1087         }
1088       if (i == ARRAY_SIZE (all_float_abis))
1089         error ("invalid floating point abi: -mfloat-abi=%s",
1090                target_float_abi_name);
1091     }
1092   else if (target_float_switch)
1093     {
1094       /* This is a bit of a hack to avoid needing target flags for these.  */
1095       if (target_float_switch[0] == 'h')
1096         arm_float_abi = ARM_FLOAT_ABI_HARD;
1097       else
1098         arm_float_abi = ARM_FLOAT_ABI_SOFT;
1099     }
1100   else
1101     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1102
1103   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1104     sorry ("-mfloat-abi=hard and VFP");
1105
1106   /* If soft-float is specified then don't use FPU.  */
1107   if (TARGET_SOFT_FLOAT)
1108     arm_fpu_arch = FPUTYPE_NONE;
1109
1110   /* For arm2/3 there is no need to do any scheduling if there is only
1111      a floating point emulator, or we are doing software floating-point.  */
1112   if ((TARGET_SOFT_FLOAT
1113        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1114        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1115       && (tune_flags & FL_MODE32) == 0)
1116     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1117
1118   /* Override the default structure alignment for AAPCS ABI.  */
1119   if (arm_abi == ARM_ABI_AAPCS)
1120     arm_structure_size_boundary = 8;
1121
1122   if (structure_size_string != NULL)
1123     {
1124       int size = strtol (structure_size_string, NULL, 0);
1125
1126       if (size == 8 || size == 32
1127           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1128         arm_structure_size_boundary = size;
1129       else
1130         warning ("structure size boundary can only be set to %s",
1131                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1132     }
1133
1134   if (arm_pic_register_string != NULL)
1135     {
1136       int pic_register = decode_reg_name (arm_pic_register_string);
1137
1138       if (!flag_pic)
1139         warning ("-mpic-register= is useless without -fpic");
1140
1141       /* Prevent the user from choosing an obviously stupid PIC register.  */
1142       else if (pic_register < 0 || call_used_regs[pic_register]
1143                || pic_register == HARD_FRAME_POINTER_REGNUM
1144                || pic_register == STACK_POINTER_REGNUM
1145                || pic_register >= PC_REGNUM)
1146         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1147       else
1148         arm_pic_register = pic_register;
1149     }
1150
1151   if (TARGET_THUMB && flag_schedule_insns)
1152     {
1153       /* Don't warn since it's on by default in -O2.  */
1154       flag_schedule_insns = 0;
1155     }
1156
1157   if (optimize_size)
1158     {
1159       /* There's some dispute as to whether this should be 1 or 2.  However,
1160          experiments seem to show that in pathological cases a setting of
1161          1 degrades less severely than a setting of 2.  This could change if
1162          other parts of the compiler change their behavior.  */
1163       arm_constant_limit = 1;
1164
1165       /* If optimizing for size, bump the number of instructions that we
1166          are prepared to conditionally execute (even on a StrongARM).  */
1167       max_insns_skipped = 6;
1168     }
1169   else
1170     {
1171       /* For processors with load scheduling, it never costs more than
1172          2 cycles to load a constant, and the load scheduler may well
1173          reduce that to 1.  */
1174       if (arm_ld_sched)
1175         arm_constant_limit = 1;
1176
1177       /* On XScale the longer latency of a load makes it more difficult
1178          to achieve a good schedule, so it's faster to synthesize
1179          constants that can be done in two insns.  */
1180       if (arm_tune_xscale)
1181         arm_constant_limit = 2;
1182
1183       /* StrongARM has early execution of branches, so a sequence
1184          that is worth skipping is shorter.  */
1185       if (arm_is_strong)
1186         max_insns_skipped = 3;
1187     }
1188
1189   /* Register global variables with the garbage collector.  */
1190   arm_add_gc_roots ();
1191 }
1192
1193 static void
1194 arm_add_gc_roots (void)
1195 {
1196   gcc_obstack_init(&minipool_obstack);
1197   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1198 }
1199 \f
1200 /* A table of known ARM exception types.
1201    For use with the interrupt function attribute.  */
1202
1203 typedef struct
1204 {
1205   const char *const arg;
1206   const unsigned long return_value;
1207 }
1208 isr_attribute_arg;
1209
1210 static const isr_attribute_arg isr_attribute_args [] =
1211 {
1212   { "IRQ",   ARM_FT_ISR },
1213   { "irq",   ARM_FT_ISR },
1214   { "FIQ",   ARM_FT_FIQ },
1215   { "fiq",   ARM_FT_FIQ },
1216   { "ABORT", ARM_FT_ISR },
1217   { "abort", ARM_FT_ISR },
1218   { "ABORT", ARM_FT_ISR },
1219   { "abort", ARM_FT_ISR },
1220   { "UNDEF", ARM_FT_EXCEPTION },
1221   { "undef", ARM_FT_EXCEPTION },
1222   { "SWI",   ARM_FT_EXCEPTION },
1223   { "swi",   ARM_FT_EXCEPTION },
1224   { NULL,    ARM_FT_NORMAL }
1225 };
1226
1227 /* Returns the (interrupt) function type of the current
1228    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1229
1230 static unsigned long
1231 arm_isr_value (tree argument)
1232 {
1233   const isr_attribute_arg * ptr;
1234   const char *              arg;
1235
1236   /* No argument - default to IRQ.  */
1237   if (argument == NULL_TREE)
1238     return ARM_FT_ISR;
1239
1240   /* Get the value of the argument.  */
1241   if (TREE_VALUE (argument) == NULL_TREE
1242       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1243     return ARM_FT_UNKNOWN;
1244
1245   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1246
1247   /* Check it against the list of known arguments.  */
1248   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1249     if (streq (arg, ptr->arg))
1250       return ptr->return_value;
1251
1252   /* An unrecognized interrupt type.  */
1253   return ARM_FT_UNKNOWN;
1254 }
1255
1256 /* Computes the type of the current function.  */
1257
1258 static unsigned long
1259 arm_compute_func_type (void)
1260 {
1261   unsigned long type = ARM_FT_UNKNOWN;
1262   tree a;
1263   tree attr;
1264
1265   if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
1266     abort ();
1267
1268   /* Decide if the current function is volatile.  Such functions
1269      never return, and many memory cycles can be saved by not storing
1270      register values that will never be needed again.  This optimization
1271      was added to speed up context switching in a kernel application.  */
1272   if (optimize > 0
1273       && TREE_NOTHROW (current_function_decl)
1274       && TREE_THIS_VOLATILE (current_function_decl))
1275     type |= ARM_FT_VOLATILE;
1276
1277   if (cfun->static_chain_decl != NULL)
1278     type |= ARM_FT_NESTED;
1279
1280   attr = DECL_ATTRIBUTES (current_function_decl);
1281
1282   a = lookup_attribute ("naked", attr);
1283   if (a != NULL_TREE)
1284     type |= ARM_FT_NAKED;
1285
1286   a = lookup_attribute ("isr", attr);
1287   if (a == NULL_TREE)
1288     a = lookup_attribute ("interrupt", attr);
1289
1290   if (a == NULL_TREE)
1291     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1292   else
1293     type |= arm_isr_value (TREE_VALUE (a));
1294
1295   return type;
1296 }
1297
1298 /* Returns the type of the current function.  */
1299
1300 unsigned long
1301 arm_current_func_type (void)
1302 {
1303   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1304     cfun->machine->func_type = arm_compute_func_type ();
1305
1306   return cfun->machine->func_type;
1307 }
1308 \f
1309 /* Return 1 if it is possible to return using a single instruction.
1310    If SIBLING is non-null, this is a test for a return before a sibling
1311    call.  SIBLING is the call insn, so we can examine its register usage.  */
1312
1313 int
1314 use_return_insn (int iscond, rtx sibling)
1315 {
1316   int regno;
1317   unsigned int func_type;
1318   unsigned long saved_int_regs;
1319   unsigned HOST_WIDE_INT stack_adjust;
1320   arm_stack_offsets *offsets;
1321
1322   /* Never use a return instruction before reload has run.  */
1323   if (!reload_completed)
1324     return 0;
1325
1326   func_type = arm_current_func_type ();
1327
1328   /* Naked functions and volatile functions need special
1329      consideration.  */
1330   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1331     return 0;
1332
1333   /* So do interrupt functions that use the frame pointer.  */
1334   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1335     return 0;
1336
1337   offsets = arm_get_frame_offsets ();
1338   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1339
1340   /* As do variadic functions.  */
1341   if (current_function_pretend_args_size
1342       || cfun->machine->uses_anonymous_args
1343       /* Or if the function calls __builtin_eh_return () */
1344       || current_function_calls_eh_return
1345       /* Or if the function calls alloca */
1346       || current_function_calls_alloca
1347       /* Or if there is a stack adjustment.  However, if the stack pointer
1348          is saved on the stack, we can use a pre-incrementing stack load.  */
1349       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1350     return 0;
1351
1352   saved_int_regs = arm_compute_save_reg_mask ();
1353
1354   /* Unfortunately, the insn
1355
1356        ldmib sp, {..., sp, ...}
1357
1358      triggers a bug on most SA-110 based devices, such that the stack
1359      pointer won't be correctly restored if the instruction takes a
1360      page fault.  We work around this problem by popping r3 along with
1361      the other registers, since that is never slower than executing
1362      another instruction.
1363
1364      We test for !arm_arch5 here, because code for any architecture
1365      less than this could potentially be run on one of the buggy
1366      chips.  */
1367   if (stack_adjust == 4 && !arm_arch5)
1368     {
1369       /* Validate that r3 is a call-clobbered register (always true in
1370          the default abi) ...  */
1371       if (!call_used_regs[3])
1372         return 0;
1373
1374       /* ... that it isn't being used for a return value (always true
1375          until we implement return-in-regs), or for a tail-call
1376          argument ...  */
1377       if (sibling)
1378         {
1379           if (GET_CODE (sibling) != CALL_INSN)
1380             abort ();
1381
1382           if (find_regno_fusage (sibling, USE, 3))
1383             return 0;
1384         }
1385
1386       /* ... and that there are no call-saved registers in r0-r2
1387          (always true in the default ABI).  */
1388       if (saved_int_regs & 0x7)
1389         return 0;
1390     }
1391
1392   /* Can't be done if interworking with Thumb, and any registers have been
1393      stacked.  */
1394   if (TARGET_INTERWORK && saved_int_regs != 0)
1395     return 0;
1396
1397   /* On StrongARM, conditional returns are expensive if they aren't
1398      taken and multiple registers have been stacked.  */
1399   if (iscond && arm_is_strong)
1400     {
1401       /* Conditional return when just the LR is stored is a simple
1402          conditional-load instruction, that's not expensive.  */
1403       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1404         return 0;
1405
1406       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1407         return 0;
1408     }
1409
1410   /* If there are saved registers but the LR isn't saved, then we need
1411      two instructions for the return.  */
1412   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1413     return 0;
1414
1415   /* Can't be done if any of the FPA regs are pushed,
1416      since this also requires an insn.  */
1417   if (TARGET_HARD_FLOAT && TARGET_FPA)
1418     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1419       if (regs_ever_live[regno] && !call_used_regs[regno])
1420         return 0;
1421
1422   /* Likewise VFP regs.  */
1423   if (TARGET_HARD_FLOAT && TARGET_VFP)
1424     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1425       if (regs_ever_live[regno] && !call_used_regs[regno])
1426         return 0;
1427
1428   if (TARGET_REALLY_IWMMXT)
1429     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1430       if (regs_ever_live[regno] && ! call_used_regs [regno])
1431         return 0;
1432
1433   return 1;
1434 }
1435
1436 /* Return TRUE if int I is a valid immediate ARM constant.  */
1437
1438 int
1439 const_ok_for_arm (HOST_WIDE_INT i)
1440 {
1441   unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1442
1443   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1444      be all zero, or all one.  */
1445   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1446       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1447           != ((~(unsigned HOST_WIDE_INT) 0)
1448               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1449     return FALSE;
1450
1451   /* Fast return for 0 and powers of 2 */
1452   if ((i & (i - 1)) == 0)
1453     return TRUE;
1454
1455   do
1456     {
1457       if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1458         return TRUE;
1459       mask =
1460           (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1461                           >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1462     }
1463   while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1464
1465   return FALSE;
1466 }
1467
1468 /* Return true if I is a valid constant for the operation CODE.  */
1469 static int
1470 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1471 {
1472   if (const_ok_for_arm (i))
1473     return 1;
1474
1475   switch (code)
1476     {
1477     case PLUS:
1478       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1479
1480     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1481     case XOR:
1482     case IOR:
1483       return 0;
1484
1485     case AND:
1486       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1487
1488     default:
1489       abort ();
1490     }
1491 }
1492
1493 /* Emit a sequence of insns to handle a large constant.
1494    CODE is the code of the operation required, it can be any of SET, PLUS,
1495    IOR, AND, XOR, MINUS;
1496    MODE is the mode in which the operation is being performed;
1497    VAL is the integer to operate on;
1498    SOURCE is the other operand (a register, or a null-pointer for SET);
1499    SUBTARGETS means it is safe to create scratch registers if that will
1500    either produce a simpler sequence, or we will want to cse the values.
1501    Return value is the number of insns emitted.  */
1502
1503 int
1504 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1505                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1506 {
1507   rtx cond;
1508
1509   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1510     cond = COND_EXEC_TEST (PATTERN (insn));
1511   else
1512     cond = NULL_RTX;
1513
1514   if (subtargets || code == SET
1515       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1516           && REGNO (target) != REGNO (source)))
1517     {
1518       /* After arm_reorg has been called, we can't fix up expensive
1519          constants by pushing them into memory so we must synthesize
1520          them in-line, regardless of the cost.  This is only likely to
1521          be more costly on chips that have load delay slots and we are
1522          compiling without running the scheduler (so no splitting
1523          occurred before the final instruction emission).
1524
1525          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1526       */
1527       if (!after_arm_reorg
1528           && !cond
1529           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1530                                 1, 0)
1531               > arm_constant_limit + (code != SET)))
1532         {
1533           if (code == SET)
1534             {
1535               /* Currently SET is the only monadic value for CODE, all
1536                  the rest are diadic.  */
1537               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1538               return 1;
1539             }
1540           else
1541             {
1542               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1543
1544               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1545               /* For MINUS, the value is subtracted from, since we never
1546                  have subtraction of a constant.  */
1547               if (code == MINUS)
1548                 emit_insn (gen_rtx_SET (VOIDmode, target,
1549                                         gen_rtx_MINUS (mode, temp, source)));
1550               else
1551                 emit_insn (gen_rtx_SET (VOIDmode, target,
1552                                         gen_rtx_fmt_ee (code, mode, source, temp)));
1553               return 2;
1554             }
1555         }
1556     }
1557
1558   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1559                            1);
1560 }
1561
1562 static int
1563 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1564 {
1565   HOST_WIDE_INT temp1;
1566   int num_insns = 0;
1567   do
1568     {
1569       int end;
1570
1571       if (i <= 0)
1572         i += 32;
1573       if (remainder & (3 << (i - 2)))
1574         {
1575           end = i - 8;
1576           if (end < 0)
1577             end += 32;
1578           temp1 = remainder & ((0x0ff << end)
1579                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1580           remainder &= ~temp1;
1581           num_insns++;
1582           i -= 6;
1583         }
1584       i -= 2;
1585     } while (remainder);
1586   return num_insns;
1587 }
1588
1589 /* Emit an instruction with the indicated PATTERN.  If COND is
1590    non-NULL, conditionalize the execution of the instruction on COND
1591    being true.  */
1592
1593 static void
1594 emit_constant_insn (rtx cond, rtx pattern)
1595 {
1596   if (cond)
1597     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1598   emit_insn (pattern);
1599 }
1600
1601 /* As above, but extra parameter GENERATE which, if clear, suppresses
1602    RTL generation.  */
1603
1604 static int
1605 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1606                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1607                   int generate)
1608 {
1609   int can_invert = 0;
1610   int can_negate = 0;
1611   int can_negate_initial = 0;
1612   int can_shift = 0;
1613   int i;
1614   int num_bits_set = 0;
1615   int set_sign_bit_copies = 0;
1616   int clear_sign_bit_copies = 0;
1617   int clear_zero_bit_copies = 0;
1618   int set_zero_bit_copies = 0;
1619   int insns = 0;
1620   unsigned HOST_WIDE_INT temp1, temp2;
1621   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1622
1623   /* Find out which operations are safe for a given CODE.  Also do a quick
1624      check for degenerate cases; these can occur when DImode operations
1625      are split.  */
1626   switch (code)
1627     {
1628     case SET:
1629       can_invert = 1;
1630       can_shift = 1;
1631       can_negate = 1;
1632       break;
1633
1634     case PLUS:
1635       can_negate = 1;
1636       can_negate_initial = 1;
1637       break;
1638
1639     case IOR:
1640       if (remainder == 0xffffffff)
1641         {
1642           if (generate)
1643             emit_constant_insn (cond,
1644                                 gen_rtx_SET (VOIDmode, target,
1645                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1646           return 1;
1647         }
1648       if (remainder == 0)
1649         {
1650           if (reload_completed && rtx_equal_p (target, source))
1651             return 0;
1652           if (generate)
1653             emit_constant_insn (cond,
1654                                 gen_rtx_SET (VOIDmode, target, source));
1655           return 1;
1656         }
1657       break;
1658
1659     case AND:
1660       if (remainder == 0)
1661         {
1662           if (generate)
1663             emit_constant_insn (cond,
1664                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1665           return 1;
1666         }
1667       if (remainder == 0xffffffff)
1668         {
1669           if (reload_completed && rtx_equal_p (target, source))
1670             return 0;
1671           if (generate)
1672             emit_constant_insn (cond,
1673                                 gen_rtx_SET (VOIDmode, target, source));
1674           return 1;
1675         }
1676       can_invert = 1;
1677       break;
1678
1679     case XOR:
1680       if (remainder == 0)
1681         {
1682           if (reload_completed && rtx_equal_p (target, source))
1683             return 0;
1684           if (generate)
1685             emit_constant_insn (cond,
1686                                 gen_rtx_SET (VOIDmode, target, source));
1687           return 1;
1688         }
1689       if (remainder == 0xffffffff)
1690         {
1691           if (generate)
1692             emit_constant_insn (cond,
1693                                 gen_rtx_SET (VOIDmode, target,
1694                                              gen_rtx_NOT (mode, source)));
1695           return 1;
1696         }
1697
1698       /* We don't know how to handle this yet below.  */
1699       abort ();
1700
1701     case MINUS:
1702       /* We treat MINUS as (val - source), since (source - val) is always
1703          passed as (source + (-val)).  */
1704       if (remainder == 0)
1705         {
1706           if (generate)
1707             emit_constant_insn (cond,
1708                                 gen_rtx_SET (VOIDmode, target,
1709                                              gen_rtx_NEG (mode, source)));
1710           return 1;
1711         }
1712       if (const_ok_for_arm (val))
1713         {
1714           if (generate)
1715             emit_constant_insn (cond,
1716                                 gen_rtx_SET (VOIDmode, target,
1717                                              gen_rtx_MINUS (mode, GEN_INT (val),
1718                                                             source)));
1719           return 1;
1720         }
1721       can_negate = 1;
1722
1723       break;
1724
1725     default:
1726       abort ();
1727     }
1728
1729   /* If we can do it in one insn get out quickly.  */
1730   if (const_ok_for_arm (val)
1731       || (can_negate_initial && const_ok_for_arm (-val))
1732       || (can_invert && const_ok_for_arm (~val)))
1733     {
1734       if (generate)
1735         emit_constant_insn (cond,
1736                             gen_rtx_SET (VOIDmode, target,
1737                                          (source
1738                                           ? gen_rtx_fmt_ee (code, mode, source,
1739                                                             GEN_INT (val))
1740                                           : GEN_INT (val))));
1741       return 1;
1742     }
1743
1744   /* Calculate a few attributes that may be useful for specific
1745      optimizations.  */
1746   for (i = 31; i >= 0; i--)
1747     {
1748       if ((remainder & (1 << i)) == 0)
1749         clear_sign_bit_copies++;
1750       else
1751         break;
1752     }
1753
1754   for (i = 31; i >= 0; i--)
1755     {
1756       if ((remainder & (1 << i)) != 0)
1757         set_sign_bit_copies++;
1758       else
1759         break;
1760     }
1761
1762   for (i = 0; i <= 31; i++)
1763     {
1764       if ((remainder & (1 << i)) == 0)
1765         clear_zero_bit_copies++;
1766       else
1767         break;
1768     }
1769
1770   for (i = 0; i <= 31; i++)
1771     {
1772       if ((remainder & (1 << i)) != 0)
1773         set_zero_bit_copies++;
1774       else
1775         break;
1776     }
1777
1778   switch (code)
1779     {
1780     case SET:
1781       /* See if we can do this by sign_extending a constant that is known
1782          to be negative.  This is a good, way of doing it, since the shift
1783          may well merge into a subsequent insn.  */
1784       if (set_sign_bit_copies > 1)
1785         {
1786           if (const_ok_for_arm
1787               (temp1 = ARM_SIGN_EXTEND (remainder
1788                                         << (set_sign_bit_copies - 1))))
1789             {
1790               if (generate)
1791                 {
1792                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1793                   emit_constant_insn (cond,
1794                                       gen_rtx_SET (VOIDmode, new_src,
1795                                                    GEN_INT (temp1)));
1796                   emit_constant_insn (cond,
1797                                       gen_ashrsi3 (target, new_src,
1798                                                    GEN_INT (set_sign_bit_copies - 1)));
1799                 }
1800               return 2;
1801             }
1802           /* For an inverted constant, we will need to set the low bits,
1803              these will be shifted out of harm's way.  */
1804           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1805           if (const_ok_for_arm (~temp1))
1806             {
1807               if (generate)
1808                 {
1809                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1810                   emit_constant_insn (cond,
1811                                       gen_rtx_SET (VOIDmode, new_src,
1812                                                    GEN_INT (temp1)));
1813                   emit_constant_insn (cond,
1814                                       gen_ashrsi3 (target, new_src,
1815                                                    GEN_INT (set_sign_bit_copies - 1)));
1816                 }
1817               return 2;
1818             }
1819         }
1820
1821       /* See if we can generate this by setting the bottom (or the top)
1822          16 bits, and then shifting these into the other half of the
1823          word.  We only look for the simplest cases, to do more would cost
1824          too much.  Be careful, however, not to generate this when the
1825          alternative would take fewer insns.  */
1826       if (val & 0xffff0000)
1827         {
1828           temp1 = remainder & 0xffff0000;
1829           temp2 = remainder & 0x0000ffff;
1830
1831           /* Overlaps outside this range are best done using other methods.  */
1832           for (i = 9; i < 24; i++)
1833             {
1834               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1835                   && !const_ok_for_arm (temp2))
1836                 {
1837                   rtx new_src = (subtargets
1838                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1839                                  : target);
1840                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1841                                             source, subtargets, generate);
1842                   source = new_src;
1843                   if (generate)
1844                     emit_constant_insn
1845                       (cond,
1846                        gen_rtx_SET
1847                        (VOIDmode, target,
1848                         gen_rtx_IOR (mode,
1849                                      gen_rtx_ASHIFT (mode, source,
1850                                                      GEN_INT (i)),
1851                                      source)));
1852                   return insns + 1;
1853                 }
1854             }
1855
1856           /* Don't duplicate cases already considered.  */
1857           for (i = 17; i < 24; i++)
1858             {
1859               if (((temp1 | (temp1 >> i)) == remainder)
1860                   && !const_ok_for_arm (temp1))
1861                 {
1862                   rtx new_src = (subtargets
1863                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1864                                  : target);
1865                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1866                                             source, subtargets, generate);
1867                   source = new_src;
1868                   if (generate)
1869                     emit_constant_insn
1870                       (cond,
1871                        gen_rtx_SET (VOIDmode, target,
1872                                     gen_rtx_IOR
1873                                     (mode,
1874                                      gen_rtx_LSHIFTRT (mode, source,
1875                                                        GEN_INT (i)),
1876                                      source)));
1877                   return insns + 1;
1878                 }
1879             }
1880         }
1881       break;
1882
1883     case IOR:
1884     case XOR:
1885       /* If we have IOR or XOR, and the constant can be loaded in a
1886          single instruction, and we can find a temporary to put it in,
1887          then this can be done in two instructions instead of 3-4.  */
1888       if (subtargets
1889           /* TARGET can't be NULL if SUBTARGETS is 0 */
1890           || (reload_completed && !reg_mentioned_p (target, source)))
1891         {
1892           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1893             {
1894               if (generate)
1895                 {
1896                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1897
1898                   emit_constant_insn (cond,
1899                                       gen_rtx_SET (VOIDmode, sub,
1900                                                    GEN_INT (val)));
1901                   emit_constant_insn (cond,
1902                                       gen_rtx_SET (VOIDmode, target,
1903                                                    gen_rtx_fmt_ee (code, mode,
1904                                                                    source, sub)));
1905                 }
1906               return 2;
1907             }
1908         }
1909
1910       if (code == XOR)
1911         break;
1912
1913       if (set_sign_bit_copies > 8
1914           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1915         {
1916           if (generate)
1917             {
1918               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1919               rtx shift = GEN_INT (set_sign_bit_copies);
1920
1921               emit_constant_insn
1922                 (cond,
1923                  gen_rtx_SET (VOIDmode, sub,
1924                               gen_rtx_NOT (mode,
1925                                            gen_rtx_ASHIFT (mode,
1926                                                            source,
1927                                                            shift))));
1928               emit_constant_insn
1929                 (cond,
1930                  gen_rtx_SET (VOIDmode, target,
1931                               gen_rtx_NOT (mode,
1932                                            gen_rtx_LSHIFTRT (mode, sub,
1933                                                              shift))));
1934             }
1935           return 2;
1936         }
1937
1938       if (set_zero_bit_copies > 8
1939           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1940         {
1941           if (generate)
1942             {
1943               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1944               rtx shift = GEN_INT (set_zero_bit_copies);
1945
1946               emit_constant_insn
1947                 (cond,
1948                  gen_rtx_SET (VOIDmode, sub,
1949                               gen_rtx_NOT (mode,
1950                                            gen_rtx_LSHIFTRT (mode,
1951                                                              source,
1952                                                              shift))));
1953               emit_constant_insn
1954                 (cond,
1955                  gen_rtx_SET (VOIDmode, target,
1956                               gen_rtx_NOT (mode,
1957                                            gen_rtx_ASHIFT (mode, sub,
1958                                                            shift))));
1959             }
1960           return 2;
1961         }
1962
1963       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1964         {
1965           if (generate)
1966             {
1967               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1968               emit_constant_insn (cond,
1969                                   gen_rtx_SET (VOIDmode, sub,
1970                                                gen_rtx_NOT (mode, source)));
1971               source = sub;
1972               if (subtargets)
1973                 sub = gen_reg_rtx (mode);
1974               emit_constant_insn (cond,
1975                                   gen_rtx_SET (VOIDmode, sub,
1976                                                gen_rtx_AND (mode, source,
1977                                                             GEN_INT (temp1))));
1978               emit_constant_insn (cond,
1979                                   gen_rtx_SET (VOIDmode, target,
1980                                                gen_rtx_NOT (mode, sub)));
1981             }
1982           return 3;
1983         }
1984       break;
1985
1986     case AND:
1987       /* See if two shifts will do 2 or more insn's worth of work.  */
1988       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1989         {
1990           HOST_WIDE_INT shift_mask = ((0xffffffff
1991                                        << (32 - clear_sign_bit_copies))
1992                                       & 0xffffffff);
1993
1994           if ((remainder | shift_mask) != 0xffffffff)
1995             {
1996               if (generate)
1997                 {
1998                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1999                   insns = arm_gen_constant (AND, mode, cond,
2000                                             remainder | shift_mask,
2001                                             new_src, source, subtargets, 1);
2002                   source = new_src;
2003                 }
2004               else
2005                 {
2006                   rtx targ = subtargets ? NULL_RTX : target;
2007                   insns = arm_gen_constant (AND, mode, cond,
2008                                             remainder | shift_mask,
2009                                             targ, source, subtargets, 0);
2010                 }
2011             }
2012
2013           if (generate)
2014             {
2015               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2016               rtx shift = GEN_INT (clear_sign_bit_copies);
2017
2018               emit_insn (gen_ashlsi3 (new_src, source, shift));
2019               emit_insn (gen_lshrsi3 (target, new_src, shift));
2020             }
2021
2022           return insns + 2;
2023         }
2024
2025       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2026         {
2027           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2028
2029           if ((remainder | shift_mask) != 0xffffffff)
2030             {
2031               if (generate)
2032                 {
2033                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2034
2035                   insns = arm_gen_constant (AND, mode, cond,
2036                                             remainder | shift_mask,
2037                                             new_src, source, subtargets, 1);
2038                   source = new_src;
2039                 }
2040               else
2041                 {
2042                   rtx targ = subtargets ? NULL_RTX : target;
2043
2044                   insns = arm_gen_constant (AND, mode, cond,
2045                                             remainder | shift_mask,
2046                                             targ, source, subtargets, 0);
2047                 }
2048             }
2049
2050           if (generate)
2051             {
2052               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2053               rtx shift = GEN_INT (clear_zero_bit_copies);
2054
2055               emit_insn (gen_lshrsi3 (new_src, source, shift));
2056               emit_insn (gen_ashlsi3 (target, new_src, shift));
2057             }
2058
2059           return insns + 2;
2060         }
2061
2062       break;
2063
2064     default:
2065       break;
2066     }
2067
2068   for (i = 0; i < 32; i++)
2069     if (remainder & (1 << i))
2070       num_bits_set++;
2071
2072   if (code == AND || (can_invert && num_bits_set > 16))
2073     remainder = (~remainder) & 0xffffffff;
2074   else if (code == PLUS && num_bits_set > 16)
2075     remainder = (-remainder) & 0xffffffff;
2076   else
2077     {
2078       can_invert = 0;
2079       can_negate = 0;
2080     }
2081
2082   /* Now try and find a way of doing the job in either two or three
2083      instructions.
2084      We start by looking for the largest block of zeros that are aligned on
2085      a 2-bit boundary, we then fill up the temps, wrapping around to the
2086      top of the word when we drop off the bottom.
2087      In the worst case this code should produce no more than four insns.  */
2088   {
2089     int best_start = 0;
2090     int best_consecutive_zeros = 0;
2091
2092     for (i = 0; i < 32; i += 2)
2093       {
2094         int consecutive_zeros = 0;
2095
2096         if (!(remainder & (3 << i)))
2097           {
2098             while ((i < 32) && !(remainder & (3 << i)))
2099               {
2100                 consecutive_zeros += 2;
2101                 i += 2;
2102               }
2103             if (consecutive_zeros > best_consecutive_zeros)
2104               {
2105                 best_consecutive_zeros = consecutive_zeros;
2106                 best_start = i - consecutive_zeros;
2107               }
2108             i -= 2;
2109           }
2110       }
2111
2112     /* So long as it won't require any more insns to do so, it's
2113        desirable to emit a small constant (in bits 0...9) in the last
2114        insn.  This way there is more chance that it can be combined with
2115        a later addressing insn to form a pre-indexed load or store
2116        operation.  Consider:
2117
2118                *((volatile int *)0xe0000100) = 1;
2119                *((volatile int *)0xe0000110) = 2;
2120
2121        We want this to wind up as:
2122
2123                 mov rA, #0xe0000000
2124                 mov rB, #1
2125                 str rB, [rA, #0x100]
2126                 mov rB, #2
2127                 str rB, [rA, #0x110]
2128
2129        rather than having to synthesize both large constants from scratch.
2130
2131        Therefore, we calculate how many insns would be required to emit
2132        the constant starting from `best_start', and also starting from
2133        zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2134        yield a shorter sequence, we may as well use zero.  */
2135     if (best_start != 0
2136         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2137         && (count_insns_for_constant (remainder, 0) <=
2138             count_insns_for_constant (remainder, best_start)))
2139       best_start = 0;
2140
2141     /* Now start emitting the insns.  */
2142     i = best_start;
2143     do
2144       {
2145         int end;
2146
2147         if (i <= 0)
2148           i += 32;
2149         if (remainder & (3 << (i - 2)))
2150           {
2151             end = i - 8;
2152             if (end < 0)
2153               end += 32;
2154             temp1 = remainder & ((0x0ff << end)
2155                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2156             remainder &= ~temp1;
2157
2158             if (generate)
2159               {
2160                 rtx new_src, temp1_rtx;
2161
2162                 if (code == SET || code == MINUS)
2163                   {
2164                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2165                     if (can_invert && code != MINUS)
2166                       temp1 = ~temp1;
2167                   }
2168                 else
2169                   {
2170                     if (remainder && subtargets)
2171                       new_src = gen_reg_rtx (mode);
2172                     else
2173                       new_src = target;
2174                     if (can_invert)
2175                       temp1 = ~temp1;
2176                     else if (can_negate)
2177                       temp1 = -temp1;
2178                   }
2179
2180                 temp1 = trunc_int_for_mode (temp1, mode);
2181                 temp1_rtx = GEN_INT (temp1);
2182
2183                 if (code == SET)
2184                   ;
2185                 else if (code == MINUS)
2186                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2187                 else
2188                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2189
2190                 emit_constant_insn (cond,
2191                                     gen_rtx_SET (VOIDmode, new_src,
2192                                                  temp1_rtx));
2193                 source = new_src;
2194               }
2195
2196             if (code == SET)
2197               {
2198                 can_invert = 0;
2199                 code = PLUS;
2200               }
2201             else if (code == MINUS)
2202               code = PLUS;
2203
2204             insns++;
2205             i -= 6;
2206           }
2207         i -= 2;
2208       }
2209     while (remainder);
2210   }
2211
2212   return insns;
2213 }
2214
2215 /* Canonicalize a comparison so that we are more likely to recognize it.
2216    This can be done for a few constant compares, where we can make the
2217    immediate value easier to load.  */
2218
2219 enum rtx_code
2220 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2221 {
2222   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2223
2224   switch (code)
2225     {
2226     case EQ:
2227     case NE:
2228       return code;
2229
2230     case GT:
2231     case LE:
2232       if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2233           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2234         {
2235           *op1 = GEN_INT (i + 1);
2236           return code == GT ? GE : LT;
2237         }
2238       break;
2239
2240     case GE:
2241     case LT:
2242       if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2243           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2244         {
2245           *op1 = GEN_INT (i - 1);
2246           return code == GE ? GT : LE;
2247         }
2248       break;
2249
2250     case GTU:
2251     case LEU:
2252       if (i != ~((unsigned HOST_WIDE_INT) 0)
2253           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2254         {
2255           *op1 = GEN_INT (i + 1);
2256           return code == GTU ? GEU : LTU;
2257         }
2258       break;
2259
2260     case GEU:
2261     case LTU:
2262       if (i != 0
2263           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2264         {
2265           *op1 = GEN_INT (i - 1);
2266           return code == GEU ? GTU : LEU;
2267         }
2268       break;
2269
2270     default:
2271       abort ();
2272     }
2273
2274   return code;
2275 }
2276
2277
2278 /* Define how to find the value returned by a function.  */
2279
2280 rtx
2281 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2282 {
2283   enum machine_mode mode;
2284   int unsignedp ATTRIBUTE_UNUSED;
2285   rtx r ATTRIBUTE_UNUSED;
2286
2287
2288   mode = TYPE_MODE (type);
2289   /* Promote integer types.  */
2290   if (INTEGRAL_TYPE_P (type))
2291     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2292   return LIBCALL_VALUE(mode);
2293 }
2294
2295 /* Determine the amount of memory needed to store the possible return 
2296    registers of an untyped call.  */
2297 int
2298 arm_apply_result_size (void)
2299 {
2300   int size = 16;
2301
2302   if (TARGET_ARM)
2303     {
2304       if (TARGET_HARD_FLOAT_ABI)
2305         {
2306           if (TARGET_FPA)
2307             size += 12;
2308           if (TARGET_MAVERICK)
2309             size += 8;
2310         }
2311       if (TARGET_IWMMXT_ABI)
2312         size += 8;
2313     }
2314
2315   return size;
2316 }
2317
2318 /* Decide whether a type should be returned in memory (true)
2319    or in a register (false).  This is called by the macro
2320    RETURN_IN_MEMORY.  */
2321 int
2322 arm_return_in_memory (tree type)
2323 {
2324   HOST_WIDE_INT size;
2325
2326   if (!AGGREGATE_TYPE_P (type) &&
2327       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2328     /* All simple types are returned in registers.
2329        For AAPCS, complex types are treated the same as aggregates.  */
2330     return 0;
2331
2332   size = int_size_in_bytes (type);
2333
2334   if (arm_abi != ARM_ABI_APCS)
2335     {
2336       /* ATPCS and later return aggregate types in memory only if they are
2337          larger than a word (or are variable size).  */
2338       return (size < 0 || size > UNITS_PER_WORD);
2339     }
2340
2341   /* For the arm-wince targets we choose to be compatible with Microsoft's
2342      ARM and Thumb compilers, which always return aggregates in memory.  */
2343 #ifndef ARM_WINCE
2344   /* All structures/unions bigger than one word are returned in memory.
2345      Also catch the case where int_size_in_bytes returns -1.  In this case
2346      the aggregate is either huge or of variable size, and in either case
2347      we will want to return it via memory and not in a register.  */
2348   if (size < 0 || size > UNITS_PER_WORD)
2349     return 1;
2350
2351   if (TREE_CODE (type) == RECORD_TYPE)
2352     {
2353       tree field;
2354
2355       /* For a struct the APCS says that we only return in a register
2356          if the type is 'integer like' and every addressable element
2357          has an offset of zero.  For practical purposes this means
2358          that the structure can have at most one non bit-field element
2359          and that this element must be the first one in the structure.  */
2360
2361       /* Find the first field, ignoring non FIELD_DECL things which will
2362          have been created by C++.  */
2363       for (field = TYPE_FIELDS (type);
2364            field && TREE_CODE (field) != FIELD_DECL;
2365            field = TREE_CHAIN (field))
2366         continue;
2367
2368       if (field == NULL)
2369         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2370
2371       /* Check that the first field is valid for returning in a register.  */
2372
2373       /* ... Floats are not allowed */
2374       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2375         return 1;
2376
2377       /* ... Aggregates that are not themselves valid for returning in
2378          a register are not allowed.  */
2379       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2380         return 1;
2381
2382       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2383          since they are not addressable.  */
2384       for (field = TREE_CHAIN (field);
2385            field;
2386            field = TREE_CHAIN (field))
2387         {
2388           if (TREE_CODE (field) != FIELD_DECL)
2389             continue;
2390
2391           if (!DECL_BIT_FIELD_TYPE (field))
2392             return 1;
2393         }
2394
2395       return 0;
2396     }
2397
2398   if (TREE_CODE (type) == UNION_TYPE)
2399     {
2400       tree field;
2401
2402       /* Unions can be returned in registers if every element is
2403          integral, or can be returned in an integer register.  */
2404       for (field = TYPE_FIELDS (type);
2405            field;
2406            field = TREE_CHAIN (field))
2407         {
2408           if (TREE_CODE (field) != FIELD_DECL)
2409             continue;
2410
2411           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2412             return 1;
2413
2414           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2415             return 1;
2416         }
2417
2418       return 0;
2419     }
2420 #endif /* not ARM_WINCE */
2421
2422   /* Return all other types in memory.  */
2423   return 1;
2424 }
2425
2426 /* Indicate whether or not words of a double are in big-endian order.  */
2427
2428 int
2429 arm_float_words_big_endian (void)
2430 {
2431   if (TARGET_MAVERICK)
2432     return 0;
2433
2434   /* For FPA, float words are always big-endian.  For VFP, floats words
2435      follow the memory system mode.  */
2436
2437   if (TARGET_FPA)
2438     {
2439       return 1;
2440     }
2441
2442   if (TARGET_VFP)
2443     return (TARGET_BIG_END ? 1 : 0);
2444
2445   return 1;
2446 }
2447
2448 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2449    for a call to a function whose data type is FNTYPE.
2450    For a library call, FNTYPE is NULL.  */
2451 void
2452 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2453                           rtx libname  ATTRIBUTE_UNUSED,
2454                           tree fndecl ATTRIBUTE_UNUSED)
2455 {
2456   /* On the ARM, the offset starts at 0.  */
2457   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2458   pcum->iwmmxt_nregs = 0;
2459   pcum->can_split = true;
2460
2461   pcum->call_cookie = CALL_NORMAL;
2462
2463   if (TARGET_LONG_CALLS)
2464     pcum->call_cookie = CALL_LONG;
2465
2466   /* Check for long call/short call attributes.  The attributes
2467      override any command line option.  */
2468   if (fntype)
2469     {
2470       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2471         pcum->call_cookie = CALL_SHORT;
2472       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2473         pcum->call_cookie = CALL_LONG;
2474     }
2475
2476   /* Varargs vectors are treated the same as long long.
2477      named_count avoids having to change the way arm handles 'named' */
2478   pcum->named_count = 0;
2479   pcum->nargs = 0;
2480
2481   if (TARGET_REALLY_IWMMXT && fntype)
2482     {
2483       tree fn_arg;
2484
2485       for (fn_arg = TYPE_ARG_TYPES (fntype);
2486            fn_arg;
2487            fn_arg = TREE_CHAIN (fn_arg))
2488         pcum->named_count += 1;
2489
2490       if (! pcum->named_count)
2491         pcum->named_count = INT_MAX;
2492     }
2493 }
2494
2495
2496 /* Return true if mode/type need doubleword alignment.  */
2497 bool
2498 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2499 {
2500   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2501           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2502 }
2503
2504
2505 /* Determine where to put an argument to a function.
2506    Value is zero to push the argument on the stack,
2507    or a hard register in which to store the argument.
2508
2509    MODE is the argument's machine mode.
2510    TYPE is the data type of the argument (as a tree).
2511     This is null for libcalls where that information may
2512     not be available.
2513    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2514     the preceding args and about the function being called.
2515    NAMED is nonzero if this argument is a named parameter
2516     (otherwise it is an extra parameter matching an ellipsis).  */
2517
2518 rtx
2519 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2520                   tree type, int named)
2521 {
2522   int nregs;
2523
2524   /* Varargs vectors are treated the same as long long.
2525      named_count avoids having to change the way arm handles 'named' */
2526   if (TARGET_IWMMXT_ABI
2527       && arm_vector_mode_supported_p (mode)
2528       && pcum->named_count > pcum->nargs + 1)
2529     {
2530       if (pcum->iwmmxt_nregs <= 9)
2531         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2532       else
2533         {
2534           pcum->can_split = false;
2535           return NULL_RTX;
2536         }
2537     }
2538
2539   /* Put doubleword aligned quantities in even register pairs.  */
2540   if (pcum->nregs & 1
2541       && ARM_DOUBLEWORD_ALIGN
2542       && arm_needs_doubleword_align (mode, type))
2543     pcum->nregs++;
2544
2545   if (mode == VOIDmode)
2546     /* Compute operand 2 of the call insn.  */
2547     return GEN_INT (pcum->call_cookie);
2548
2549   /* Only allow splitting an arg between regs and memory if all preceding
2550      args were allocated to regs.  For args passed by reference we only count
2551      the reference pointer.  */
2552   if (pcum->can_split)
2553     nregs = 1;
2554   else
2555     nregs = ARM_NUM_REGS2 (mode, type);
2556
2557   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2558     return NULL_RTX;
2559
2560   return gen_rtx_REG (mode, pcum->nregs);
2561 }
2562
2563 static int
2564 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2565                        tree type, bool named ATTRIBUTE_UNUSED)
2566 {
2567   int nregs = pcum->nregs;
2568
2569   if (arm_vector_mode_supported_p (mode))
2570     return 0;
2571
2572   if (NUM_ARG_REGS > nregs
2573       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2574       && pcum->can_split)
2575     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2576
2577   return 0;
2578 }
2579
2580 /* Variable sized types are passed by reference.  This is a GCC
2581    extension to the ARM ABI.  */
2582
2583 static bool
2584 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2585                        enum machine_mode mode ATTRIBUTE_UNUSED,
2586                        tree type, bool named ATTRIBUTE_UNUSED)
2587 {
2588   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2589 }
2590 \f
2591 /* Encode the current state of the #pragma [no_]long_calls.  */
2592 typedef enum
2593 {
2594   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2595   LONG,         /* #pragma long_calls is in effect.  */
2596   SHORT         /* #pragma no_long_calls is in effect.  */
2597 } arm_pragma_enum;
2598
2599 static arm_pragma_enum arm_pragma_long_calls = OFF;
2600
2601 void
2602 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2603 {
2604   arm_pragma_long_calls = LONG;
2605 }
2606
2607 void
2608 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2609 {
2610   arm_pragma_long_calls = SHORT;
2611 }
2612
2613 void
2614 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2615 {
2616   arm_pragma_long_calls = OFF;
2617 }
2618 \f
2619 /* Table of machine attributes.  */
2620 const struct attribute_spec arm_attribute_table[] =
2621 {
2622   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2623   /* Function calls made to this symbol must be done indirectly, because
2624      it may lie outside of the 26 bit addressing range of a normal function
2625      call.  */
2626   { "long_call",    0, 0, false, true,  true,  NULL },
2627   /* Whereas these functions are always known to reside within the 26 bit
2628      addressing range.  */
2629   { "short_call",   0, 0, false, true,  true,  NULL },
2630   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
2631   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2632   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2633   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2634 #ifdef ARM_PE
2635   /* ARM/PE has three new attributes:
2636      interfacearm - ?
2637      dllexport - for exporting a function/variable that will live in a dll
2638      dllimport - for importing a function/variable from a dll
2639
2640      Microsoft allows multiple declspecs in one __declspec, separating
2641      them with spaces.  We do NOT support this.  Instead, use __declspec
2642      multiple times.
2643   */
2644   { "dllimport",    0, 0, true,  false, false, NULL },
2645   { "dllexport",    0, 0, true,  false, false, NULL },
2646   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2647 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2648   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
2649   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
2650   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
2651 #endif
2652   { NULL,           0, 0, false, false, false, NULL }
2653 };
2654
2655 /* Handle an attribute requiring a FUNCTION_DECL;
2656    arguments as in struct attribute_spec.handler.  */
2657 static tree
2658 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2659                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2660 {
2661   if (TREE_CODE (*node) != FUNCTION_DECL)
2662     {
2663       warning ("%qs attribute only applies to functions",
2664                IDENTIFIER_POINTER (name));
2665       *no_add_attrs = true;
2666     }
2667
2668   return NULL_TREE;
2669 }
2670
2671 /* Handle an "interrupt" or "isr" attribute;
2672    arguments as in struct attribute_spec.handler.  */
2673 static tree
2674 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2675                           bool *no_add_attrs)
2676 {
2677   if (DECL_P (*node))
2678     {
2679       if (TREE_CODE (*node) != FUNCTION_DECL)
2680         {
2681           warning ("%qs attribute only applies to functions",
2682                    IDENTIFIER_POINTER (name));
2683           *no_add_attrs = true;
2684         }
2685       /* FIXME: the argument if any is checked for type attributes;
2686          should it be checked for decl ones?  */
2687     }
2688   else
2689     {
2690       if (TREE_CODE (*node) == FUNCTION_TYPE
2691           || TREE_CODE (*node) == METHOD_TYPE)
2692         {
2693           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2694             {
2695               warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
2696               *no_add_attrs = true;
2697             }
2698         }
2699       else if (TREE_CODE (*node) == POINTER_TYPE
2700                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2701                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2702                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2703         {
2704           *node = build_variant_type_copy (*node);
2705           TREE_TYPE (*node) = build_type_attribute_variant
2706             (TREE_TYPE (*node),
2707              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2708           *no_add_attrs = true;
2709         }
2710       else
2711         {
2712           /* Possibly pass this attribute on from the type to a decl.  */
2713           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2714                        | (int) ATTR_FLAG_FUNCTION_NEXT
2715                        | (int) ATTR_FLAG_ARRAY_NEXT))
2716             {
2717               *no_add_attrs = true;
2718               return tree_cons (name, args, NULL_TREE);
2719             }
2720           else
2721             {
2722               warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
2723             }
2724         }
2725     }
2726
2727   return NULL_TREE;
2728 }
2729
2730 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2731 /* Handle the "notshared" attribute.  This attribute is another way of
2732    requesting hidden visibility.  ARM's compiler supports
2733    "__declspec(notshared)"; we support the same thing via an
2734    attribute.  */
2735
2736 static tree
2737 arm_handle_notshared_attribute (tree *node, 
2738                                 tree name ATTRIBUTE_UNUSED, 
2739                                 tree args ATTRIBUTE_UNUSED, 
2740                                 int flags ATTRIBUTE_UNUSED, 
2741                                 bool *no_add_attrs)
2742 {
2743   tree decl = TYPE_NAME (*node);
2744
2745   if (decl)
2746     {
2747       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2748       DECL_VISIBILITY_SPECIFIED (decl) = 1;
2749       *no_add_attrs = false;
2750     }
2751   return NULL_TREE;
2752 }
2753 #endif
2754
2755 /* Return 0 if the attributes for two types are incompatible, 1 if they
2756    are compatible, and 2 if they are nearly compatible (which causes a
2757    warning to be generated).  */
2758 static int
2759 arm_comp_type_attributes (tree type1, tree type2)
2760 {
2761   int l1, l2, s1, s2;
2762
2763   /* Check for mismatch of non-default calling convention.  */
2764   if (TREE_CODE (type1) != FUNCTION_TYPE)
2765     return 1;
2766
2767   /* Check for mismatched call attributes.  */
2768   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2769   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2770   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2771   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2772
2773   /* Only bother to check if an attribute is defined.  */
2774   if (l1 | l2 | s1 | s2)
2775     {
2776       /* If one type has an attribute, the other must have the same attribute.  */
2777       if ((l1 != l2) || (s1 != s2))
2778         return 0;
2779
2780       /* Disallow mixed attributes.  */
2781       if ((l1 & s2) || (l2 & s1))
2782         return 0;
2783     }
2784
2785   /* Check for mismatched ISR attribute.  */
2786   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2787   if (! l1)
2788     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2789   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2790   if (! l2)
2791     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2792   if (l1 != l2)
2793     return 0;
2794
2795   return 1;
2796 }
2797
2798 /*  Encode long_call or short_call attribute by prefixing
2799     symbol name in DECL with a special character FLAG.  */
2800 void
2801 arm_encode_call_attribute (tree decl, int flag)
2802 {
2803   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2804   int          len = strlen (str);
2805   char *       newstr;
2806
2807   /* Do not allow weak functions to be treated as short call.  */
2808   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2809     return;
2810
2811   newstr = alloca (len + 2);
2812   newstr[0] = flag;
2813   strcpy (newstr + 1, str);
2814
2815   newstr = (char *) ggc_alloc_string (newstr, len + 1);
2816   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2817 }
2818
2819 /*  Assigns default attributes to newly defined type.  This is used to
2820     set short_call/long_call attributes for function types of
2821     functions defined inside corresponding #pragma scopes.  */
2822 static void
2823 arm_set_default_type_attributes (tree type)
2824 {
2825   /* Add __attribute__ ((long_call)) to all functions, when
2826      inside #pragma long_calls or __attribute__ ((short_call)),
2827      when inside #pragma no_long_calls.  */
2828   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2829     {
2830       tree type_attr_list, attr_name;
2831       type_attr_list = TYPE_ATTRIBUTES (type);
2832
2833       if (arm_pragma_long_calls == LONG)
2834         attr_name = get_identifier ("long_call");
2835       else if (arm_pragma_long_calls == SHORT)
2836         attr_name = get_identifier ("short_call");
2837       else
2838         return;
2839
2840       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2841       TYPE_ATTRIBUTES (type) = type_attr_list;
2842     }
2843 }
2844 \f
2845 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2846    defined within the current compilation unit.  If this cannot be
2847    determined, then 0 is returned.  */
2848 static int
2849 current_file_function_operand (rtx sym_ref)
2850 {
2851   /* This is a bit of a fib.  A function will have a short call flag
2852      applied to its name if it has the short call attribute, or it has
2853      already been defined within the current compilation unit.  */
2854   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2855     return 1;
2856
2857   /* The current function is always defined within the current compilation
2858      unit.  If it s a weak definition however, then this may not be the real
2859      definition of the function, and so we have to say no.  */
2860   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2861       && !DECL_WEAK (current_function_decl))
2862     return 1;
2863
2864   /* We cannot make the determination - default to returning 0.  */
2865   return 0;
2866 }
2867
2868 /* Return nonzero if a 32 bit "long_call" should be generated for
2869    this call.  We generate a long_call if the function:
2870
2871         a.  has an __attribute__((long call))
2872      or b.  is within the scope of a #pragma long_calls
2873      or c.  the -mlong-calls command line switch has been specified
2874          .  and either:
2875                 1. -ffunction-sections is in effect
2876              or 2. the current function has __attribute__ ((section))
2877              or 3. the target function has __attribute__ ((section))
2878
2879    However we do not generate a long call if the function:
2880
2881         d.  has an __attribute__ ((short_call))
2882      or e.  is inside the scope of a #pragma no_long_calls
2883      or f.  is defined within the current compilation unit.
2884
2885    This function will be called by C fragments contained in the machine
2886    description file.  SYM_REF and CALL_COOKIE correspond to the matched
2887    rtl operands.  CALL_SYMBOL is used to distinguish between
2888    two different callers of the function.  It is set to 1 in the
2889    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2890    and "call_value" patterns.  This is because of the difference in the
2891    SYM_REFs passed by these patterns.  */
2892 int
2893 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2894 {
2895   if (!call_symbol)
2896     {
2897       if (GET_CODE (sym_ref) != MEM)
2898         return 0;
2899
2900       sym_ref = XEXP (sym_ref, 0);
2901     }
2902
2903   if (GET_CODE (sym_ref) != SYMBOL_REF)
2904     return 0;
2905
2906   if (call_cookie & CALL_SHORT)
2907     return 0;
2908
2909   if (TARGET_LONG_CALLS)
2910     {
2911       if (flag_function_sections
2912           || DECL_SECTION_NAME (current_function_decl))
2913         /* c.3 is handled by the definition of the
2914            ARM_DECLARE_FUNCTION_SIZE macro.  */
2915         return 1;
2916     }
2917
2918   if (current_file_function_operand (sym_ref))
2919     return 0;
2920
2921   return (call_cookie & CALL_LONG)
2922     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2923     || TARGET_LONG_CALLS;
2924 }
2925
2926 /* Return nonzero if it is ok to make a tail-call to DECL.  */
2927 static bool
2928 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
2929 {
2930   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2931
2932   if (cfun->machine->sibcall_blocked)
2933     return false;
2934
2935   /* Never tailcall something for which we have no decl, or if we
2936      are in Thumb mode.  */
2937   if (decl == NULL || TARGET_THUMB)
2938     return false;
2939
2940   /* Get the calling method.  */
2941   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2942     call_type = CALL_SHORT;
2943   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2944     call_type = CALL_LONG;
2945
2946   /* Cannot tail-call to long calls, since these are out of range of
2947      a branch instruction.  However, if not compiling PIC, we know
2948      we can reach the symbol if it is in this compilation unit.  */
2949   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2950     return false;
2951
2952   /* If we are interworking and the function is not declared static
2953      then we can't tail-call it unless we know that it exists in this
2954      compilation unit (since it might be a Thumb routine).  */
2955   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2956     return false;
2957
2958   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
2959   if (IS_INTERRUPT (arm_current_func_type ()))
2960     return false;
2961
2962   /* Everything else is ok.  */
2963   return true;
2964 }
2965
2966 \f
2967 /* Addressing mode support functions.  */
2968
2969 /* Return nonzero if X is a legitimate immediate operand when compiling
2970    for PIC.  */
2971 int
2972 legitimate_pic_operand_p (rtx x)
2973 {
2974   if (CONSTANT_P (x)
2975       && flag_pic
2976       && (GET_CODE (x) == SYMBOL_REF
2977           || (GET_CODE (x) == CONST
2978               && GET_CODE (XEXP (x, 0)) == PLUS
2979               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2980     return 0;
2981
2982   return 1;
2983 }
2984
2985 rtx
2986 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
2987 {
2988   if (GET_CODE (orig) == SYMBOL_REF
2989       || GET_CODE (orig) == LABEL_REF)
2990     {
2991 #ifndef AOF_ASSEMBLER
2992       rtx pic_ref, address;
2993 #endif
2994       rtx insn;
2995       int subregs = 0;
2996
2997       if (reg == 0)
2998         {
2999           if (no_new_pseudos)
3000             abort ();
3001           else
3002             reg = gen_reg_rtx (Pmode);
3003
3004           subregs = 1;
3005         }
3006
3007 #ifdef AOF_ASSEMBLER
3008       /* The AOF assembler can generate relocations for these directly, and
3009          understands that the PIC register has to be added into the offset.  */
3010       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3011 #else
3012       if (subregs)
3013         address = gen_reg_rtx (Pmode);
3014       else
3015         address = reg;
3016
3017       if (TARGET_ARM)
3018         emit_insn (gen_pic_load_addr_arm (address, orig));
3019       else
3020         emit_insn (gen_pic_load_addr_thumb (address, orig));
3021
3022       if ((GET_CODE (orig) == LABEL_REF
3023            || (GET_CODE (orig) == SYMBOL_REF &&
3024                SYMBOL_REF_LOCAL_P (orig)))
3025           && NEED_GOT_RELOC)
3026         pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
3027       else
3028         {
3029           pic_ref = gen_const_mem (Pmode,
3030                                    gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3031                                                  address));
3032         }
3033
3034       insn = emit_move_insn (reg, pic_ref);
3035 #endif
3036       current_function_uses_pic_offset_table = 1;
3037       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3038          by loop.  */
3039       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3040                                             REG_NOTES (insn));
3041       return reg;
3042     }
3043   else if (GET_CODE (orig) == CONST)
3044     {
3045       rtx base, offset;
3046
3047       if (GET_CODE (XEXP (orig, 0)) == PLUS
3048           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3049         return orig;
3050
3051       if (reg == 0)
3052         {
3053           if (no_new_pseudos)
3054             abort ();
3055           else
3056             reg = gen_reg_rtx (Pmode);
3057         }
3058
3059       if (GET_CODE (XEXP (orig, 0)) == PLUS)
3060         {
3061           base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3062           offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3063                                            base == reg ? 0 : reg);
3064         }
3065       else
3066         abort ();
3067
3068       if (GET_CODE (offset) == CONST_INT)
3069         {
3070           /* The base register doesn't really matter, we only want to
3071              test the index for the appropriate mode.  */
3072           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3073             {
3074               if (!no_new_pseudos)
3075                 offset = force_reg (Pmode, offset);
3076               else
3077                 abort ();
3078             }
3079
3080           if (GET_CODE (offset) == CONST_INT)
3081             return plus_constant (base, INTVAL (offset));
3082         }
3083
3084       if (GET_MODE_SIZE (mode) > 4
3085           && (GET_MODE_CLASS (mode) == MODE_INT
3086               || TARGET_SOFT_FLOAT))
3087         {
3088           emit_insn (gen_addsi3 (reg, base, offset));
3089           return reg;
3090         }
3091
3092       return gen_rtx_PLUS (Pmode, base, offset);
3093     }
3094
3095   return orig;
3096 }
3097
3098
3099 /* Find a spare low register to use during the prolog of a function.  */
3100
3101 static int
3102 thumb_find_work_register (unsigned long pushed_regs_mask)
3103 {
3104   int reg;
3105
3106   /* Check the argument registers first as these are call-used.  The
3107      register allocation order means that sometimes r3 might be used
3108      but earlier argument registers might not, so check them all.  */
3109   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3110     if (!regs_ever_live[reg])
3111       return reg;
3112
3113   /* Before going on to check the call-saved registers we can try a couple
3114      more ways of deducing that r3 is available.  The first is when we are
3115      pushing anonymous arguments onto the stack and we have less than 4
3116      registers worth of fixed arguments(*).  In this case r3 will be part of
3117      the variable argument list and so we can be sure that it will be
3118      pushed right at the start of the function.  Hence it will be available
3119      for the rest of the prologue.
3120      (*): ie current_function_pretend_args_size is greater than 0.  */
3121   if (cfun->machine->uses_anonymous_args
3122       && current_function_pretend_args_size > 0)
3123     return LAST_ARG_REGNUM;
3124
3125   /* The other case is when we have fixed arguments but less than 4 registers
3126      worth.  In this case r3 might be used in the body of the function, but
3127      it is not being used to convey an argument into the function.  In theory
3128      we could just check current_function_args_size to see how many bytes are
3129      being passed in argument registers, but it seems that it is unreliable.
3130      Sometimes it will have the value 0 when in fact arguments are being
3131      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3132      check the args_info.nregs field as well.  The problem with this field is
3133      that it makes no allowances for arguments that are passed to the
3134      function but which are not used.  Hence we could miss an opportunity
3135      when a function has an unused argument in r3.  But it is better to be
3136      safe than to be sorry.  */
3137   if (! cfun->machine->uses_anonymous_args
3138       && current_function_args_size >= 0
3139       && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3140       && cfun->args_info.nregs < 4)
3141     return LAST_ARG_REGNUM;
3142   
3143   /* Otherwise look for a call-saved register that is going to be pushed.  */
3144   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3145     if (pushed_regs_mask & (1 << reg))
3146       return reg;
3147
3148   /* Something went wrong - thumb_compute_save_reg_mask()
3149      should have arranged for a suitable register to be pushed.  */
3150   abort ();
3151 }
3152
3153
3154 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3155    low register.  */
3156
3157 void
3158 arm_load_pic_register (unsigned int scratch)
3159 {
3160 #ifndef AOF_ASSEMBLER
3161   rtx l1, pic_tmp, pic_tmp2, pic_rtx;
3162   rtx global_offset_table;
3163
3164   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3165     return;
3166
3167   if (!flag_pic)
3168     abort ();
3169
3170   l1 = gen_label_rtx ();
3171
3172   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3173   /* On the ARM the PC register contains 'dot + 8' at the time of the
3174      addition, on the Thumb it is 'dot + 4'.  */
3175   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
3176   if (GOT_PCREL)
3177     pic_tmp2 = gen_rtx_CONST (VOIDmode,
3178                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3179   else
3180     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3181
3182   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3183
3184   if (TARGET_ARM)
3185     {
3186       emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3187       emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
3188     }
3189   else
3190     {
3191       if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3192         {
3193           /* We will have pushed the pic register, so should always be
3194              able to find a work register.  */
3195           pic_tmp = gen_rtx_REG (SImode, scratch);
3196           emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3197           emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3198         }
3199       else
3200         emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3201       emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
3202     }
3203
3204   /* Need to emit this whether or not we obey regdecls,
3205      since setjmp/longjmp can cause life info to screw up.  */
3206   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3207 #endif /* AOF_ASSEMBLER */
3208 }
3209
3210
3211 /* Return nonzero if X is valid as an ARM state addressing register.  */
3212 static int
3213 arm_address_register_rtx_p (rtx x, int strict_p)
3214 {
3215   int regno;
3216
3217   if (GET_CODE (x) != REG)
3218     return 0;
3219
3220   regno = REGNO (x);
3221
3222   if (strict_p)
3223     return ARM_REGNO_OK_FOR_BASE_P (regno);
3224
3225   return (regno <= LAST_ARM_REGNUM
3226           || regno >= FIRST_PSEUDO_REGISTER
3227           || regno == FRAME_POINTER_REGNUM
3228           || regno == ARG_POINTER_REGNUM);
3229 }
3230
3231 /* Return nonzero if X is a valid ARM state address operand.  */
3232 int
3233 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3234                           int strict_p)
3235 {
3236   bool use_ldrd;
3237   enum rtx_code code = GET_CODE (x);
3238
3239   if (arm_address_register_rtx_p (x, strict_p))
3240     return 1;
3241
3242   use_ldrd = (TARGET_LDRD
3243               && (mode == DImode
3244                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3245
3246   if (code == POST_INC || code == PRE_DEC
3247       || ((code == PRE_INC || code == POST_DEC)
3248           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3249     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3250
3251   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3252            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3253            && GET_CODE (XEXP (x, 1)) == PLUS
3254            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3255     {
3256       rtx addend = XEXP (XEXP (x, 1), 1);
3257
3258       /* Don't allow ldrd post increment by register because it's hard
3259          to fixup invalid register choices.  */
3260       if (use_ldrd
3261           && GET_CODE (x) == POST_MODIFY
3262           && GET_CODE (addend) == REG)
3263         return 0;
3264
3265       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3266               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3267     }
3268
3269   /* After reload constants split into minipools will have addresses
3270      from a LABEL_REF.  */
3271   else if (reload_completed
3272            && (code == LABEL_REF
3273                || (code == CONST
3274                    && GET_CODE (XEXP (x, 0)) == PLUS
3275                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3276                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3277     return 1;
3278
3279   else if (mode == TImode)
3280     return 0;
3281
3282   else if (code == PLUS)
3283     {
3284       rtx xop0 = XEXP (x, 0);
3285       rtx xop1 = XEXP (x, 1);
3286
3287       return ((arm_address_register_rtx_p (xop0, strict_p)
3288                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3289               || (arm_address_register_rtx_p (xop1, strict_p)
3290                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3291     }
3292
3293 #if 0
3294   /* Reload currently can't handle MINUS, so disable this for now */
3295   else if (GET_CODE (x) == MINUS)
3296     {
3297       rtx xop0 = XEXP (x, 0);
3298       rtx xop1 = XEXP (x, 1);
3299
3300       return (arm_address_register_rtx_p (xop0, strict_p)
3301               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3302     }
3303 #endif
3304
3305   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3306            && code == SYMBOL_REF
3307            && CONSTANT_POOL_ADDRESS_P (x)
3308            && ! (flag_pic
3309                  && symbol_mentioned_p (get_pool_constant (x))))
3310     return 1;
3311
3312   return 0;
3313 }
3314
3315 /* Return nonzero if INDEX is valid for an address index operand in
3316    ARM state.  */
3317 static int
3318 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3319                         int strict_p)
3320 {
3321   HOST_WIDE_INT range;
3322   enum rtx_code code = GET_CODE (index);
3323
3324   /* Standard coprocessor addressing modes.  */
3325   if (TARGET_HARD_FLOAT
3326       && (TARGET_FPA || TARGET_MAVERICK)
3327       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3328           || (TARGET_MAVERICK && mode == DImode)))
3329     return (code == CONST_INT && INTVAL (index) < 1024
3330             && INTVAL (index) > -1024
3331             && (INTVAL (index) & 3) == 0);
3332
3333   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3334     return (code == CONST_INT
3335             && INTVAL (index) < 1024
3336             && INTVAL (index) > -1024
3337             && (INTVAL (index) & 3) == 0);
3338
3339   if (arm_address_register_rtx_p (index, strict_p)
3340       && (GET_MODE_SIZE (mode) <= 4))
3341     return 1;
3342
3343   if (mode == DImode || mode == DFmode)
3344     {
3345       if (code == CONST_INT)
3346         {
3347           HOST_WIDE_INT val = INTVAL (index);
3348
3349           if (TARGET_LDRD)
3350             return val > -256 && val < 256;
3351           else
3352             return val > -4096 && val < 4092;
3353         }
3354
3355       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3356     }
3357
3358   if (GET_MODE_SIZE (mode) <= 4
3359       && ! (arm_arch4
3360             && (mode == HImode
3361                 || (mode == QImode && outer == SIGN_EXTEND))))
3362     {
3363       if (code == MULT)
3364         {
3365           rtx xiop0 = XEXP (index, 0);
3366           rtx xiop1 = XEXP (index, 1);
3367
3368           return ((arm_address_register_rtx_p (xiop0, strict_p)
3369                    && power_of_two_operand (xiop1, SImode))
3370                   || (arm_address_register_rtx_p (xiop1, strict_p)
3371                       && power_of_two_operand (xiop0, SImode)));
3372         }
3373       else if (code == LSHIFTRT || code == ASHIFTRT
3374                || code == ASHIFT || code == ROTATERT)
3375         {
3376           rtx op = XEXP (index, 1);
3377
3378           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3379                   && GET_CODE (op) == CONST_INT
3380                   && INTVAL (op) > 0
3381                   && INTVAL (op) <= 31);
3382         }
3383     }
3384
3385   /* For ARM v4 we may be doing a sign-extend operation during the
3386      load.  */
3387   if (arm_arch4)
3388     {
3389       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3390         range = 256;
3391       else
3392         range = 4096;
3393     }
3394   else
3395     range = (mode == HImode) ? 4095 : 4096;
3396
3397   return (code == CONST_INT
3398           && INTVAL (index) < range
3399           && INTVAL (index) > -range);
3400 }
3401
3402 /* Return nonzero if X is valid as a Thumb state base register.  */
3403 static int
3404 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3405 {
3406   int regno;
3407
3408   if (GET_CODE (x) != REG)
3409     return 0;
3410
3411   regno = REGNO (x);
3412
3413   if (strict_p)
3414     return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3415
3416   return (regno <= LAST_LO_REGNUM
3417           || regno > LAST_VIRTUAL_REGISTER
3418           || regno == FRAME_POINTER_REGNUM
3419           || (GET_MODE_SIZE (mode) >= 4
3420               && (regno == STACK_POINTER_REGNUM
3421                   || regno >= FIRST_PSEUDO_REGISTER
3422                   || x == hard_frame_pointer_rtx
3423                   || x == arg_pointer_rtx)));
3424 }
3425
3426 /* Return nonzero if x is a legitimate index register.  This is the case
3427    for any base register that can access a QImode object.  */
3428 inline static int
3429 thumb_index_register_rtx_p (rtx x, int strict_p)
3430 {
3431   return thumb_base_register_rtx_p (x, QImode, strict_p);
3432 }
3433
3434 /* Return nonzero if x is a legitimate Thumb-state address.
3435
3436    The AP may be eliminated to either the SP or the FP, so we use the
3437    least common denominator, e.g. SImode, and offsets from 0 to 64.
3438
3439    ??? Verify whether the above is the right approach.
3440
3441    ??? Also, the FP may be eliminated to the SP, so perhaps that
3442    needs special handling also.
3443
3444    ??? Look at how the mips16 port solves this problem.  It probably uses
3445    better ways to solve some of these problems.
3446
3447    Although it is not incorrect, we don't accept QImode and HImode
3448    addresses based on the frame pointer or arg pointer until the
3449    reload pass starts.  This is so that eliminating such addresses
3450    into stack based ones won't produce impossible code.  */
3451 int
3452 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3453 {
3454   /* ??? Not clear if this is right.  Experiment.  */
3455   if (GET_MODE_SIZE (mode) < 4
3456       && !(reload_in_progress || reload_completed)
3457       && (reg_mentioned_p (frame_pointer_rtx, x)
3458           || reg_mentioned_p (arg_pointer_rtx, x)
3459           || reg_mentioned_p (virtual_incoming_args_rtx, x)
3460           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3461           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3462           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3463     return 0;
3464
3465   /* Accept any base register.  SP only in SImode or larger.  */
3466   else if (thumb_base_register_rtx_p (x, mode, strict_p))
3467     return 1;
3468
3469   /* This is PC relative data before arm_reorg runs.  */
3470   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3471            && GET_CODE (x) == SYMBOL_REF
3472            && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3473     return 1;
3474
3475   /* This is PC relative data after arm_reorg runs.  */
3476   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3477            && (GET_CODE (x) == LABEL_REF
3478                || (GET_CODE (x) == CONST
3479                    && GET_CODE (XEXP (x, 0)) == PLUS
3480                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3481                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3482     return 1;
3483
3484   /* Post-inc indexing only supported for SImode and larger.  */
3485   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3486            && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3487     return 1;
3488
3489   else if (GET_CODE (x) == PLUS)
3490     {
3491       /* REG+REG address can be any two index registers.  */
3492       /* We disallow FRAME+REG addressing since we know that FRAME
3493          will be replaced with STACK, and SP relative addressing only
3494          permits SP+OFFSET.  */
3495       if (GET_MODE_SIZE (mode) <= 4
3496           && XEXP (x, 0) != frame_pointer_rtx
3497           && XEXP (x, 1) != frame_pointer_rtx
3498           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3499           && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3500         return 1;
3501
3502       /* REG+const has 5-7 bit offset for non-SP registers.  */
3503       else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3504                 || XEXP (x, 0) == arg_pointer_rtx)
3505                && GET_CODE (XEXP (x, 1)) == CONST_INT
3506                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3507         return 1;
3508
3509       /* REG+const has 10 bit offset for SP, but only SImode and
3510          larger is supported.  */
3511       /* ??? Should probably check for DI/DFmode overflow here
3512          just like GO_IF_LEGITIMATE_OFFSET does.  */
3513       else if (GET_CODE (XEXP (x, 0)) == REG
3514                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3515                && GET_MODE_SIZE (mode) >= 4
3516                && GET_CODE (XEXP (x, 1)) == CONST_INT
3517                && INTVAL (XEXP (x, 1)) >= 0
3518                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3519                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3520         return 1;
3521
3522       else if (GET_CODE (XEXP (x, 0)) == REG
3523                && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3524                && GET_MODE_SIZE (mode) >= 4
3525                && GET_CODE (XEXP (x, 1)) == CONST_INT
3526                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3527         return 1;
3528     }
3529
3530   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3531            && GET_MODE_SIZE (mode) == 4
3532            && GET_CODE (x) == SYMBOL_REF
3533            && CONSTANT_POOL_ADDRESS_P (x)
3534            && !(flag_pic
3535                 && symbol_mentioned_p (get_pool_constant (x))))
3536     return 1;
3537
3538   return 0;
3539 }
3540
3541 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3542    instruction of mode MODE.  */
3543 int
3544 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3545 {
3546   switch (GET_MODE_SIZE (mode))
3547     {
3548     case 1:
3549       return val >= 0 && val < 32;
3550
3551     case 2:
3552       return val >= 0 && val < 64 && (val & 1) == 0;
3553
3554     default:
3555       return (val >= 0
3556               && (val + GET_MODE_SIZE (mode)) <= 128
3557               && (val & 3) == 0);
3558     }
3559 }
3560
3561 /* Try machine-dependent ways of modifying an illegitimate address
3562    to be legitimate.  If we find one, return the new, valid address.  */
3563 rtx
3564 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3565 {
3566   if (GET_CODE (x) == PLUS)
3567     {
3568       rtx xop0 = XEXP (x, 0);
3569       rtx xop1 = XEXP (x, 1);
3570
3571       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3572         xop0 = force_reg (SImode, xop0);
3573
3574       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3575         xop1 = force_reg (SImode, xop1);
3576
3577       if (ARM_BASE_REGISTER_RTX_P (xop0)
3578           && GET_CODE (xop1) == CONST_INT)
3579         {
3580           HOST_WIDE_INT n, low_n;
3581           rtx base_reg, val;
3582           n = INTVAL (xop1);
3583
3584           /* VFP addressing modes actually allow greater offsets, but for
3585              now we just stick with the lowest common denominator.  */
3586           if (mode == DImode
3587               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3588             {
3589               low_n = n & 0x0f;
3590               n &= ~0x0f;
3591               if (low_n > 4)
3592                 {
3593                   n += 16;
3594                   low_n -= 16;
3595                 }
3596             }
3597           else
3598             {
3599               low_n = ((mode) == TImode ? 0
3600                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3601               n -= low_n;
3602             }
3603
3604           base_reg = gen_reg_rtx (SImode);
3605           val = force_operand (gen_rtx_PLUS (SImode, xop0,
3606                                              GEN_INT (n)), NULL_RTX);
3607           emit_move_insn (base_reg, val);
3608           x = (low_n == 0 ? base_reg
3609                : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3610         }
3611       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3612         x = gen_rtx_PLUS (SImode, xop0, xop1);
3613     }
3614
3615   /* XXX We don't allow MINUS any more -- see comment in
3616      arm_legitimate_address_p ().  */
3617   else if (GET_CODE (x) == MINUS)
3618     {
3619       rtx xop0 = XEXP (x, 0);
3620       rtx xop1 = XEXP (x, 1);
3621
3622       if (CONSTANT_P (xop0))
3623         xop0 = force_reg (SImode, xop0);
3624
3625       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3626         xop1 = force_reg (SImode, xop1);
3627
3628       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3629         x = gen_rtx_MINUS (SImode, xop0, xop1);
3630     }
3631
3632   if (flag_pic)
3633     {
3634       /* We need to find and carefully transform any SYMBOL and LABEL
3635          references; so go back to the original address expression.  */
3636       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3637
3638       if (new_x != orig_x)
3639         x = new_x;
3640     }
3641
3642   return x;
3643 }
3644
3645
3646 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3647    to be legitimate.  If we find one, return the new, valid address.  */
3648 rtx
3649 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3650 {
3651   if (GET_CODE (x) == PLUS
3652       && GET_CODE (XEXP (x, 1)) == CONST_INT
3653       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3654           || INTVAL (XEXP (x, 1)) < 0))
3655     {
3656       rtx xop0 = XEXP (x, 0);
3657       rtx xop1 = XEXP (x, 1);
3658       HOST_WIDE_INT offset = INTVAL (xop1);
3659
3660       /* Try and fold the offset into a biasing of the base register and
3661          then offsetting that.  Don't do this when optimizing for space
3662          since it can cause too many CSEs.  */
3663       if (optimize_size && offset >= 0
3664           && offset < 256 + 31 * GET_MODE_SIZE (mode))
3665         {
3666           HOST_WIDE_INT delta;
3667
3668           if (offset >= 256)
3669             delta = offset - (256 - GET_MODE_SIZE (mode));
3670           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3671             delta = 31 * GET_MODE_SIZE (mode);
3672           else
3673             delta = offset & (~31 * GET_MODE_SIZE (mode));
3674
3675           xop0 = force_operand (plus_constant (xop0, offset - delta),
3676                                 NULL_RTX);
3677           x = plus_constant (xop0, delta);
3678         }
3679       else if (offset < 0 && offset > -256)
3680         /* Small negative offsets are best done with a subtract before the
3681            dereference, forcing these into a register normally takes two
3682            instructions.  */
3683         x = force_operand (x, NULL_RTX);
3684       else
3685         {
3686           /* For the remaining cases, force the constant into a register.  */
3687           xop1 = force_reg (SImode, xop1);
3688           x = gen_rtx_PLUS (SImode, xop0, xop1);
3689         }
3690     }
3691   else if (GET_CODE (x) == PLUS
3692            && s_register_operand (XEXP (x, 1), SImode)
3693            && !s_register_operand (XEXP (x, 0), SImode))
3694     {
3695       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3696
3697       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3698     }
3699
3700   if (flag_pic)
3701     {
3702       /* We need to find and carefully transform any SYMBOL and LABEL
3703          references; so go back to the original address expression.  */
3704       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3705
3706       if (new_x != orig_x)
3707         x = new_x;
3708     }
3709
3710   return x;
3711 }
3712
3713 \f
3714
3715 #define REG_OR_SUBREG_REG(X)                                            \
3716   (GET_CODE (X) == REG                                                  \
3717    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3718
3719 #define REG_OR_SUBREG_RTX(X)                    \
3720    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3721
3722 #ifndef COSTS_N_INSNS
3723 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3724 #endif
3725 static inline int
3726 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3727 {
3728   enum machine_mode mode = GET_MODE (x);
3729
3730   switch (code)
3731     {
3732     case ASHIFT:
3733     case ASHIFTRT:
3734     case LSHIFTRT:
3735     case ROTATERT:
3736     case PLUS:
3737     case MINUS:
3738     case COMPARE:
3739     case NEG:
3740     case NOT:
3741       return COSTS_N_INSNS (1);
3742
3743     case MULT:
3744       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3745         {
3746           int cycles = 0;
3747           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3748
3749           while (i)
3750             {
3751               i >>= 2;
3752               cycles++;
3753             }
3754           return COSTS_N_INSNS (2) + cycles;
3755         }
3756       return COSTS_N_INSNS (1) + 16;
3757
3758     case SET:
3759       return (COSTS_N_INSNS (1)
3760               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3761                      + GET_CODE (SET_DEST (x)) == MEM));
3762
3763     case CONST_INT:
3764       if (outer == SET)
3765         {
3766           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3767             return 0;
3768           if (thumb_shiftable_const (INTVAL (x)))
3769             return COSTS_N_INSNS (2);
3770           return COSTS_N_INSNS (3);
3771         }
3772       else if ((outer == PLUS || outer == COMPARE)
3773                && INTVAL (x) < 256 && INTVAL (x) > -256)
3774         return 0;
3775       else if (outer == AND
3776                && INTVAL (x) < 256 && INTVAL (x) >= -256)
3777         return COSTS_N_INSNS (1);
3778       else if (outer == ASHIFT || outer == ASHIFTRT
3779                || outer == LSHIFTRT)
3780         return 0;
3781       return COSTS_N_INSNS (2);
3782
3783     case CONST:
3784     case CONST_DOUBLE:
3785     case LABEL_REF:
3786     case SYMBOL_REF:
3787       return COSTS_N_INSNS (3);
3788
3789     case UDIV:
3790     case UMOD:
3791     case DIV:
3792     case MOD:
3793       return 100;
3794
3795     case TRUNCATE:
3796       return 99;
3797
3798     case AND:
3799     case XOR:
3800     case IOR:
3801       /* XXX guess.  */
3802       return 8;
3803
3804     case MEM:
3805       /* XXX another guess.  */
3806       /* Memory costs quite a lot for the first word, but subsequent words
3807          load at the equivalent of a single insn each.  */
3808       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3809               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3810                  ? 4 : 0));
3811
3812     case IF_THEN_ELSE:
3813       /* XXX a guess.  */
3814       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3815         return 14;
3816       return 2;
3817
3818     case ZERO_EXTEND:
3819       /* XXX still guessing.  */
3820       switch (GET_MODE (XEXP (x, 0)))
3821         {
3822         case QImode:
3823           return (1 + (mode == DImode ? 4 : 0)
3824                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3825
3826         case HImode:
3827           return (4 + (mode == DImode ? 4 : 0)
3828                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3829
3830         case SImode:
3831           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3832
3833         default:
3834           return 99;
3835         }
3836
3837     default:
3838       return 99;
3839     }
3840 }
3841
3842
3843 /* Worker routine for arm_rtx_costs.  */
3844 static inline int
3845 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3846 {
3847   enum machine_mode mode = GET_MODE (x);
3848   enum rtx_code subcode;
3849   int extra_cost;
3850
3851   switch (code)
3852     {
3853     case MEM:
3854       /* Memory costs quite a lot for the first word, but subsequent words
3855          load at the equivalent of a single insn each.  */
3856       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3857               + (GET_CODE (x) == SYMBOL_REF
3858                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3859
3860     case DIV:
3861     case MOD:
3862     case UDIV:
3863     case UMOD:
3864       return optimize_size ? COSTS_N_INSNS (2) : 100;
3865
3866     case ROTATE:
3867       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3868         return 4;
3869       /* Fall through */
3870     case ROTATERT:
3871       if (mode != SImode)
3872         return 8;
3873       /* Fall through */
3874     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3875       if (mode == DImode)
3876         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3877                 + ((GET_CODE (XEXP (x, 0)) == REG
3878                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3879                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3880                    ? 0 : 8));
3881       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3882                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3883                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3884                    ? 0 : 4)
3885               + ((GET_CODE (XEXP (x, 1)) == REG
3886                   || (GET_CODE (XEXP (x, 1)) == SUBREG
3887                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3888                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3889                  ? 0 : 4));
3890
3891     case MINUS:
3892       if (mode == DImode)
3893         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3894                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3895                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
3896                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3897                    ? 0 : 8));
3898
3899       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3900         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3901                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3902                           && arm_const_double_rtx (XEXP (x, 1))))
3903                      ? 0 : 8)
3904                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3905                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3906                         && arm_const_double_rtx (XEXP (x, 0))))
3907                    ? 0 : 8));
3908
3909       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3910             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3911             && REG_OR_SUBREG_REG (XEXP (x, 1))))
3912           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3913                || subcode == ASHIFTRT || subcode == LSHIFTRT
3914                || subcode == ROTATE || subcode == ROTATERT
3915                || (subcode == MULT
3916                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3917                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3918                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3919               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3920               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3921                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3922               && REG_OR_SUBREG_REG (XEXP (x, 0))))
3923         return 1;
3924       /* Fall through */
3925
3926     case PLUS:
3927       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3928         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3929                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3930                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3931                         && arm_const_double_rtx (XEXP (x, 1))))
3932                    ? 0 : 8));
3933
3934       /* Fall through */
3935     case AND: case XOR: case IOR:
3936       extra_cost = 0;
3937
3938       /* Normally the frame registers will be spilt into reg+const during
3939          reload, so it is a bad idea to combine them with other instructions,
3940          since then they might not be moved outside of loops.  As a compromise
3941          we allow integration with ops that have a constant as their second
3942          operand.  */
3943       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3944            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3945            && GET_CODE (XEXP (x, 1)) != CONST_INT)
3946           || (REG_OR_SUBREG_REG (XEXP (x, 0))
3947               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3948         extra_cost = 4;
3949
3950       if (mode == DImode)
3951         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3952                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3953                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3954                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3955                    ? 0 : 8));
3956
3957       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3958         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3959                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3960                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3961                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3962                    ? 0 : 4));
3963
3964       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3965         return (1 + extra_cost
3966                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3967                      || subcode == LSHIFTRT || subcode == ASHIFTRT
3968                      || subcode == ROTATE || subcode == ROTATERT
3969                      || (subcode == MULT
3970                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3971                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3972                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3973                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3974                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3975                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3976                    ? 0 : 4));
3977
3978       return 8;
3979
3980     case MULT:
3981       /* This should have been handled by the CPU specific routines.  */
3982       abort ();
3983
3984     case TRUNCATE:
3985       if (arm_arch3m && mode == SImode
3986           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
3987           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
3988           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
3989               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
3990           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
3991               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
3992         return 8;
3993       return 99;
3994
3995     case NEG:
3996       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3997         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
3998       /* Fall through */
3999     case NOT:
4000       if (mode == DImode)
4001         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4002
4003       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4004
4005     case IF_THEN_ELSE:
4006       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4007         return 14;
4008       return 2;
4009
4010     case COMPARE:
4011       return 1;
4012
4013     case ABS:
4014       return 4 + (mode == DImode ? 4 : 0);
4015
4016     case SIGN_EXTEND:
4017       if (GET_MODE (XEXP (x, 0)) == QImode)
4018         return (4 + (mode == DImode ? 4 : 0)
4019                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4020       /* Fall through */
4021     case ZERO_EXTEND:
4022       switch (GET_MODE (XEXP (x, 0)))
4023         {
4024         case QImode:
4025           return (1 + (mode == DImode ? 4 : 0)
4026                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4027
4028         case HImode:
4029           return (4 + (mode == DImode ? 4 : 0)
4030                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4031
4032         case SImode:
4033           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4034
4035         case V8QImode:
4036         case V4HImode:
4037         case V2SImode:
4038         case V4QImode:
4039         case V2HImode:
4040             return 1;
4041
4042         default:
4043           break;
4044         }
4045       abort ();
4046
4047     case CONST_INT:
4048       if (const_ok_for_arm (INTVAL (x)))
4049         return outer == SET ? 2 : -1;
4050       else if (outer == AND
4051                && const_ok_for_arm (~INTVAL (x)))
4052         return -1;
4053       else if ((outer == COMPARE
4054                 || outer == PLUS || outer == MINUS)
4055                && const_ok_for_arm (-INTVAL (x)))
4056         return -1;
4057       else
4058         return 5;
4059
4060     case CONST:
4061     case LABEL_REF:
4062     case SYMBOL_REF:
4063       return 6;
4064
4065     case CONST_DOUBLE:
4066       if (arm_const_double_rtx (x))
4067         return outer == SET ? 2 : -1;
4068       else if ((outer == COMPARE || outer == PLUS)
4069                && neg_const_double_rtx_ok_for_fpa (x))
4070         return -1;
4071       return 7;
4072
4073     default:
4074       return 99;
4075     }
4076 }
4077
4078 /* RTX costs when optimizing for size.  */
4079 static bool
4080 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4081 {
4082   enum machine_mode mode = GET_MODE (x);
4083
4084   if (TARGET_THUMB)
4085     {
4086       /* XXX TBD.  For now, use the standard costs.  */
4087       *total = thumb_rtx_costs (x, code, outer_code);
4088       return true;
4089     }
4090
4091   switch (code)
4092     {
4093     case MEM:
4094       /* A memory access costs 1 insn if the mode is small, or the address is
4095          a single register, otherwise it costs one insn per word.  */
4096       if (REG_P (XEXP (x, 0)))
4097         *total = COSTS_N_INSNS (1);
4098       else
4099         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4100       return true;
4101
4102     case DIV:
4103     case MOD:
4104     case UDIV:
4105     case UMOD:
4106       /* Needs a libcall, so it costs about this.  */
4107       *total = COSTS_N_INSNS (2);
4108       return false;
4109
4110     case ROTATE:
4111       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4112         {
4113           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4114           return true;
4115         }
4116       /* Fall through */
4117     case ROTATERT:
4118     case ASHIFT:
4119     case LSHIFTRT:
4120     case ASHIFTRT:
4121       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4122         {
4123           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4124           return true;
4125         }
4126       else if (mode == SImode)
4127         {
4128           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4129           /* Slightly disparage register shifts, but not by much.  */
4130           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4131             *total += 1 + rtx_cost (XEXP (x, 1), code);
4132           return true;
4133         }
4134
4135       /* Needs a libcall.  */
4136       *total = COSTS_N_INSNS (2);
4137       return false;
4138
4139     case MINUS:
4140       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4141         {
4142           *total = COSTS_N_INSNS (1);
4143           return false;
4144         }
4145
4146       if (mode == SImode)
4147         {
4148           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4149           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4150
4151           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4152               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4153               || subcode1 == ROTATE || subcode1 == ROTATERT
4154               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4155               || subcode1 == ASHIFTRT)
4156             {
4157               /* It's just the cost of the two operands.  */
4158               *total = 0;
4159               return false;
4160             }
4161
4162           *total = COSTS_N_INSNS (1);
4163           return false;
4164         }
4165
4166       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4167       return false;
4168
4169     case PLUS:
4170       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4171         {
4172           *total = COSTS_N_INSNS (1);
4173           return false;
4174         }
4175
4176       /* Fall through */
4177     case AND: case XOR: case IOR:
4178       if (mode == SImode)
4179         {
4180           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4181
4182           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4183               || subcode == LSHIFTRT || subcode == ASHIFTRT
4184               || (code == AND && subcode == NOT))
4185             {
4186               /* It's just the cost of the two operands.  */
4187               *total = 0;
4188               return false;
4189             }
4190         }
4191
4192       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4193       return false;
4194
4195     case MULT:
4196       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4197       return false;
4198
4199     case NEG:
4200       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4201         *total = COSTS_N_INSNS (1);
4202       /* Fall through */
4203     case NOT:
4204       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4205
4206       return false;
4207
4208     case IF_THEN_ELSE:
4209       *total = 0;
4210       return false;
4211
4212     case COMPARE:
4213       if (cc_register (XEXP (x, 0), VOIDmode))
4214         * total = 0;
4215       else
4216         *total = COSTS_N_INSNS (1);
4217       return false;
4218
4219     case ABS:
4220       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4221         *total = COSTS_N_INSNS (1);
4222       else
4223         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4224       return false;
4225
4226     case SIGN_EXTEND:
4227       *total = 0;
4228       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4229         {
4230           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4231             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4232         }
4233       if (mode == DImode)
4234         *total += COSTS_N_INSNS (1);
4235       return false;
4236
4237     case ZERO_EXTEND:
4238       *total = 0;
4239       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4240         {
4241           switch (GET_MODE (XEXP (x, 0)))
4242             {
4243             case QImode:
4244               *total += COSTS_N_INSNS (1);
4245               break;
4246
4247             case HImode:
4248               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4249
4250             case SImode:
4251               break;
4252
4253             default:
4254               *total += COSTS_N_INSNS (2);
4255             }
4256         }
4257
4258       if (mode == DImode)
4259         *total += COSTS_N_INSNS (1);
4260
4261       return false;
4262
4263     case CONST_INT:
4264       if (const_ok_for_arm (INTVAL (x)))
4265         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4266       else if (const_ok_for_arm (~INTVAL (x)))
4267         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4268       else if (const_ok_for_arm (-INTVAL (x)))
4269         {
4270           if (outer_code == COMPARE || outer_code == PLUS
4271               || outer_code == MINUS)
4272             *total = 0;
4273           else
4274             *total = COSTS_N_INSNS (1);
4275         }
4276       else
4277         *total = COSTS_N_INSNS (2);
4278       return true;
4279
4280     case CONST:
4281     case LABEL_REF:
4282     case SYMBOL_REF:
4283       *total = COSTS_N_INSNS (2);
4284       return true;
4285
4286     case CONST_DOUBLE:
4287       *total = COSTS_N_INSNS (4);
4288       return true;
4289
4290     default:
4291       if (mode != VOIDmode)
4292         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4293       else
4294         *total = COSTS_N_INSNS (4); /* How knows?  */
4295       return false;
4296     }
4297 }
4298
4299 /* RTX costs for cores with a slow MUL implementation.  */
4300
4301 static bool
4302 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4303 {
4304   enum machine_mode mode = GET_MODE (x);
4305
4306   if (TARGET_THUMB)
4307     {
4308       *total = thumb_rtx_costs (x, code, outer_code);
4309       return true;
4310     }
4311
4312   switch (code)
4313     {
4314     case MULT:
4315       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4316           || mode == DImode)
4317         {
4318           *total = 30;
4319           return true;
4320         }
4321
4322       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4323         {
4324           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4325                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4326           int cost, const_ok = const_ok_for_arm (i);
4327           int j, booth_unit_size;
4328
4329           /* Tune as appropriate.  */
4330           cost = const_ok ? 4 : 8;
4331           booth_unit_size = 2;
4332           for (j = 0; i && j < 32; j += booth_unit_size)
4333             {
4334               i >>= booth_unit_size;
4335               cost += 2;
4336             }
4337
4338           *total = cost;
4339           return true;
4340         }
4341
4342       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4343                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4344       return true;
4345
4346     default:
4347       *total = arm_rtx_costs_1 (x, code, outer_code);
4348       return true;
4349     }
4350 }
4351
4352
4353 /* RTX cost for cores with a fast multiply unit (M variants).  */
4354
4355 static bool
4356 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4357 {
4358   enum machine_mode mode = GET_MODE (x);
4359
4360   if (TARGET_THUMB)
4361     {
4362       *total = thumb_rtx_costs (x, code, outer_code);
4363       return true;
4364     }
4365
4366   switch (code)
4367     {
4368     case MULT:
4369       /* There is no point basing this on the tuning, since it is always the
4370          fast variant if it exists at all.  */
4371       if (mode == DImode
4372           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4373           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4374               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4375         {
4376           *total = 8;
4377           return true;
4378         }
4379
4380
4381       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4382           || mode == DImode)
4383         {
4384           *total = 30;
4385           return true;
4386         }
4387
4388       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4389         {
4390           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4391                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4392           int cost, const_ok = const_ok_for_arm (i);
4393           int j, booth_unit_size;
4394
4395           /* Tune as appropriate.  */
4396           cost = const_ok ? 4 : 8;
4397           booth_unit_size = 8;
4398           for (j = 0; i && j < 32; j += booth_unit_size)
4399             {
4400               i >>= booth_unit_size;
4401               cost += 2;
4402             }
4403
4404           *total = cost;
4405           return true;
4406         }
4407
4408       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4409                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4410       return true;
4411
4412     default:
4413       *total = arm_rtx_costs_1 (x, code, outer_code);
4414       return true;
4415     }
4416 }
4417
4418
4419 /* RTX cost for XScale CPUs.  */
4420
4421 static bool
4422 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4423 {
4424   enum machine_mode mode = GET_MODE (x);
4425
4426   if (TARGET_THUMB)
4427     {
4428       *total = thumb_rtx_costs (x, code, outer_code);
4429       return true;
4430     }
4431
4432   switch (code)
4433     {
4434     case MULT:
4435       /* There is no point basing this on the tuning, since it is always the
4436          fast variant if it exists at all.  */
4437       if (mode == DImode
4438           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4439           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4440               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4441         {
4442           *total = 8;
4443           return true;
4444         }
4445
4446
4447       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4448           || mode == DImode)
4449         {
4450           *total = 30;
4451           return true;
4452         }
4453
4454       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4455         {
4456           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4457                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4458           int cost, const_ok = const_ok_for_arm (i);
4459           unsigned HOST_WIDE_INT masked_const;
4460
4461           /* The cost will be related to two insns.
4462              First a load of the constant (MOV or LDR), then a multiply.  */
4463           cost = 2;
4464           if (! const_ok)
4465             cost += 1;      /* LDR is probably more expensive because
4466                                of longer result latency.  */
4467           masked_const = i & 0xffff8000;
4468           if (masked_const != 0 && masked_const != 0xffff8000)
4469             {
4470               masked_const = i & 0xf8000000;
4471               if (masked_const == 0 || masked_const == 0xf8000000)
4472                 cost += 1;
4473               else
4474                 cost += 2;
4475             }
4476           *total = cost;
4477           return true;
4478         }
4479
4480       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4481                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4482       return true;
4483
4484     case COMPARE:
4485       /* A COMPARE of a MULT is slow on XScale; the muls instruction
4486          will stall until the multiplication is complete.  */
4487       if (GET_CODE (XEXP (x, 0)) == MULT)
4488         *total = 4 + rtx_cost (XEXP (x, 0), code);
4489       else
4490         *total = arm_rtx_costs_1 (x, code, outer_code);
4491       return true;
4492
4493     default:
4494       *total = arm_rtx_costs_1 (x, code, outer_code);
4495       return true;
4496     }
4497 }
4498
4499
4500 /* RTX costs for 9e (and later) cores.  */
4501
4502 static bool
4503 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4504 {
4505   enum machine_mode mode = GET_MODE (x);
4506   int nonreg_cost;
4507   int cost;
4508
4509   if (TARGET_THUMB)
4510     {
4511       switch (code)
4512         {
4513         case MULT:
4514           *total = COSTS_N_INSNS (3);
4515           return true;
4516
4517         default:
4518           *total = thumb_rtx_costs (x, code, outer_code);
4519           return true;
4520         }
4521     }
4522
4523   switch (code)
4524     {
4525     case MULT:
4526       /* There is no point basing this on the tuning, since it is always the
4527          fast variant if it exists at all.  */
4528       if (mode == DImode
4529           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4530           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4531               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4532         {
4533           *total = 3;
4534           return true;
4535         }
4536
4537
4538       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4539         {
4540           *total = 30;
4541           return true;
4542         }
4543       if (mode == DImode)
4544         {
4545           cost = 7;
4546           nonreg_cost = 8;
4547         }
4548       else
4549         {
4550           cost = 2;
4551           nonreg_cost = 4;
4552         }
4553
4554
4555       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4556                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4557       return true;
4558
4559     default:
4560       *total = arm_rtx_costs_1 (x, code, outer_code);
4561       return true;
4562     }
4563 }
4564 /* All address computations that can be done are free, but rtx cost returns
4565    the same for practically all of them.  So we weight the different types
4566    of address here in the order (most pref first):
4567    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
4568 static inline int
4569 arm_arm_address_cost (rtx x)
4570 {
4571   enum rtx_code c  = GET_CODE (x);
4572
4573   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4574     return 0;
4575   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4576     return 10;
4577
4578   if (c == PLUS || c == MINUS)
4579     {
4580       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4581         return 2;
4582
4583       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4584         return 3;
4585
4586       return 4;
4587     }
4588
4589   return 6;
4590 }
4591
4592 static inline int
4593 arm_thumb_address_cost (rtx x)
4594 {
4595   enum rtx_code c  = GET_CODE (x);
4596
4597   if (c == REG)
4598     return 1;
4599   if (c == PLUS
4600       && GET_CODE (XEXP (x, 0)) == REG
4601       && GET_CODE (XEXP (x, 1)) == CONST_INT)
4602     return 1;
4603
4604   return 2;
4605 }
4606
4607 static int
4608 arm_address_cost (rtx x)
4609 {
4610   return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4611 }
4612
4613 static int
4614 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4615 {
4616   rtx i_pat, d_pat;
4617
4618   /* Some true dependencies can have a higher cost depending
4619      on precisely how certain input operands are used.  */
4620   if (arm_tune_xscale
4621       && REG_NOTE_KIND (link) == 0
4622       && recog_memoized (insn) >= 0
4623       && recog_memoized (dep) >= 0)
4624     {
4625       int shift_opnum = get_attr_shift (insn);
4626       enum attr_type attr_type = get_attr_type (dep);
4627
4628       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4629          operand for INSN.  If we have a shifted input operand and the
4630          instruction we depend on is another ALU instruction, then we may
4631          have to account for an additional stall.  */
4632       if (shift_opnum != 0
4633           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4634         {
4635           rtx shifted_operand;
4636           int opno;
4637
4638           /* Get the shifted operand.  */
4639           extract_insn (insn);
4640           shifted_operand = recog_data.operand[shift_opnum];
4641
4642           /* Iterate over all the operands in DEP.  If we write an operand
4643              that overlaps with SHIFTED_OPERAND, then we have increase the
4644              cost of this dependency.  */
4645           extract_insn (dep);
4646           preprocess_constraints ();
4647           for (opno = 0; opno < recog_data.n_operands; opno++)
4648             {
4649               /* We can ignore strict inputs.  */
4650               if (recog_data.operand_type[opno] == OP_IN)
4651                 continue;
4652
4653               if (reg_overlap_mentioned_p (recog_data.operand[opno],
4654                                            shifted_operand))
4655                 return 2;
4656             }
4657         }
4658     }
4659
4660   /* XXX This is not strictly true for the FPA.  */
4661   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4662       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4663     return 0;
4664
4665   /* Call insns don't incur a stall, even if they follow a load.  */
4666   if (REG_NOTE_KIND (link) == 0
4667       && GET_CODE (insn) == CALL_INSN)
4668     return 1;
4669
4670   if ((i_pat = single_set (insn)) != NULL
4671       && GET_CODE (SET_SRC (i_pat)) == MEM
4672       && (d_pat = single_set (dep)) != NULL
4673       && GET_CODE (SET_DEST (d_pat)) == MEM)
4674     {
4675       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4676       /* This is a load after a store, there is no conflict if the load reads
4677          from a cached area.  Assume that loads from the stack, and from the
4678          constant pool are cached, and that others will miss.  This is a
4679          hack.  */
4680
4681       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4682           || reg_mentioned_p (stack_pointer_rtx, src_mem)
4683           || reg_mentioned_p (frame_pointer_rtx, src_mem)
4684           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4685         return 1;
4686     }
4687
4688   return cost;
4689 }
4690
4691 static int fp_consts_inited = 0;
4692
4693 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
4694 static const char * const strings_fp[8] =
4695 {
4696   "0",   "1",   "2",   "3",
4697   "4",   "5",   "0.5", "10"
4698 };
4699
4700 static REAL_VALUE_TYPE values_fp[8];
4701
4702 static void
4703 init_fp_table (void)
4704 {
4705   int i;
4706   REAL_VALUE_TYPE r;
4707
4708   if (TARGET_VFP)
4709     fp_consts_inited = 1;
4710   else
4711     fp_consts_inited = 8;
4712
4713   for (i = 0; i < fp_consts_inited; i++)
4714     {
4715       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4716       values_fp[i] = r;
4717     }
4718 }
4719
4720 /* Return TRUE if rtx X is a valid immediate FP constant.  */
4721 int
4722 arm_const_double_rtx (rtx x)
4723 {
4724   REAL_VALUE_TYPE r;
4725   int i;
4726
4727   if (!fp_consts_inited)
4728     init_fp_table ();
4729
4730   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4731   if (REAL_VALUE_MINUS_ZERO (r))
4732     return 0;
4733
4734   for (i = 0; i < fp_consts_inited; i++)
4735     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4736       return 1;
4737
4738   return 0;
4739 }
4740
4741 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
4742 int
4743 neg_const_double_rtx_ok_for_fpa (rtx x)
4744 {
4745   REAL_VALUE_TYPE r;
4746   int i;
4747
4748   if (!fp_consts_inited)
4749     init_fp_table ();
4750
4751   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4752   r = REAL_VALUE_NEGATE (r);
4753   if (REAL_VALUE_MINUS_ZERO (r))
4754     return 0;
4755
4756   for (i = 0; i < 8; i++)
4757     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4758       return 1;
4759
4760   return 0;
4761 }
4762 \f
4763 /* Predicates for `match_operand' and `match_operator'.  */
4764
4765 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
4766 int
4767 cirrus_memory_offset (rtx op)
4768 {
4769   /* Reject eliminable registers.  */
4770   if (! (reload_in_progress || reload_completed)
4771       && (   reg_mentioned_p (frame_pointer_rtx, op)
4772           || reg_mentioned_p (arg_pointer_rtx, op)
4773           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4774           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4775           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4776           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4777     return 0;
4778
4779   if (GET_CODE (op) == MEM)
4780     {
4781       rtx ind;
4782
4783       ind = XEXP (op, 0);
4784
4785       /* Match: (mem (reg)).  */
4786       if (GET_CODE (ind) == REG)
4787         return 1;
4788
4789       /* Match:
4790          (mem (plus (reg)
4791                     (const))).  */
4792       if (GET_CODE (ind) == PLUS
4793           && GET_CODE (XEXP (ind, 0)) == REG
4794           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4795           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4796         return 1;
4797     }
4798
4799   return 0;
4800 }
4801
4802 /* Return TRUE if OP is a valid VFP memory address pattern.
4803    WB if true if writeback address modes are allowed.  */
4804
4805 int
4806 arm_coproc_mem_operand (rtx op, bool wb)
4807 {
4808   rtx ind;
4809
4810   /* Reject eliminable registers.  */
4811   if (! (reload_in_progress || reload_completed)
4812       && (   reg_mentioned_p (frame_pointer_rtx, op)
4813           || reg_mentioned_p (arg_pointer_rtx, op)
4814           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4815           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4816           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4817           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4818     return FALSE;
4819
4820   /* Constants are converted into offsets from labels.  */
4821   if (GET_CODE (op) != MEM)
4822     return FALSE;
4823
4824   ind = XEXP (op, 0);
4825
4826   if (reload_completed
4827       && (GET_CODE (ind) == LABEL_REF
4828           || (GET_CODE (ind) == CONST
4829               && GET_CODE (XEXP (ind, 0)) == PLUS
4830               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4831               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4832     return TRUE;
4833
4834   /* Match: (mem (reg)).  */
4835   if (GET_CODE (ind) == REG)
4836     return arm_address_register_rtx_p (ind, 0);
4837
4838   /* Autoincremment addressing modes.  */
4839   if (wb
4840       && (GET_CODE (ind) == PRE_INC
4841           || GET_CODE (ind) == POST_INC
4842           || GET_CODE (ind) == PRE_DEC
4843           || GET_CODE (ind) == POST_DEC))
4844     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
4845
4846   if (wb
4847       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
4848       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
4849       && GET_CODE (XEXP (ind, 1)) == PLUS
4850       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
4851     ind = XEXP (ind, 1);
4852
4853   /* Match:
4854      (plus (reg)
4855            (const)).  */
4856   if (GET_CODE (ind) == PLUS
4857       && GET_CODE (XEXP (ind, 0)) == REG
4858       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4859       && GET_CODE (XEXP (ind, 1)) == CONST_INT
4860       && INTVAL (XEXP (ind, 1)) > -1024
4861       && INTVAL (XEXP (ind, 1)) <  1024
4862       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
4863     return TRUE;
4864
4865   return FALSE;
4866 }
4867
4868 /* Return true if X is a register that will be eliminated later on.  */
4869 int
4870 arm_eliminable_register (rtx x)
4871 {
4872   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
4873                        || REGNO (x) == ARG_POINTER_REGNUM
4874                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
4875                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
4876 }
4877
4878 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4879    VFP registers.  Otherwise return NO_REGS.  */
4880
4881 enum reg_class
4882 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4883 {
4884   if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
4885     return NO_REGS;
4886
4887   return GENERAL_REGS;
4888 }
4889
4890
4891 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4892    Use by the Cirrus Maverick code which has to workaround
4893    a hardware bug triggered by such instructions.  */
4894 static bool
4895 arm_memory_load_p (rtx insn)
4896 {
4897   rtx body, lhs, rhs;;
4898
4899   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4900     return false;
4901
4902   body = PATTERN (insn);
4903
4904   if (GET_CODE (body) != SET)
4905     return false;
4906
4907   lhs = XEXP (body, 0);
4908   rhs = XEXP (body, 1);
4909
4910   lhs = REG_OR_SUBREG_RTX (lhs);
4911
4912   /* If the destination is not a general purpose
4913      register we do not have to worry.  */
4914   if (GET_CODE (lhs) != REG
4915       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4916     return false;
4917
4918   /* As well as loads from memory we also have to react
4919      to loads of invalid constants which will be turned
4920      into loads from the minipool.  */
4921   return (GET_CODE (rhs) == MEM
4922           || GET_CODE (rhs) == SYMBOL_REF
4923           || note_invalid_constants (insn, -1, false));
4924 }
4925
4926 /* Return TRUE if INSN is a Cirrus instruction.  */
4927 static bool
4928 arm_cirrus_insn_p (rtx insn)
4929 {
4930   enum attr_cirrus attr;
4931
4932   /* get_attr aborts on USE and CLOBBER.  */
4933   if (!insn
4934       || GET_CODE (insn) != INSN
4935       || GET_CODE (PATTERN (insn)) == USE
4936       || GET_CODE (PATTERN (insn)) == CLOBBER)
4937     return 0;
4938
4939   attr = get_attr_cirrus (insn);
4940
4941   return attr != CIRRUS_NOT;
4942 }
4943
4944 /* Cirrus reorg for invalid instruction combinations.  */
4945 static void
4946 cirrus_reorg (rtx first)
4947 {
4948   enum attr_cirrus attr;
4949   rtx body = PATTERN (first);
4950   rtx t;
4951   int nops;
4952
4953   /* Any branch must be followed by 2 non Cirrus instructions.  */
4954   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4955     {
4956       nops = 0;
4957       t = next_nonnote_insn (first);
4958
4959       if (arm_cirrus_insn_p (t))
4960         ++ nops;
4961
4962       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4963         ++ nops;
4964
4965       while (nops --)
4966         emit_insn_after (gen_nop (), first);
4967
4968       return;
4969     }
4970
4971   /* (float (blah)) is in parallel with a clobber.  */
4972   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4973     body = XVECEXP (body, 0, 0);
4974
4975   if (GET_CODE (body) == SET)
4976     {
4977       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4978
4979       /* cfldrd, cfldr64, cfstrd, cfstr64 must
4980          be followed by a non Cirrus insn.  */
4981       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
4982         {
4983           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
4984             emit_insn_after (gen_nop (), first);
4985
4986           return;
4987         }
4988       else if (arm_memory_load_p (first))
4989         {
4990           unsigned int arm_regno;
4991
4992           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
4993              ldr/cfmv64hr combination where the Rd field is the same
4994              in both instructions must be split with a non Cirrus
4995              insn.  Example:
4996
4997              ldr r0, blah
4998              nop
4999              cfmvsr mvf0, r0.  */
5000
5001           /* Get Arm register number for ldr insn.  */
5002           if (GET_CODE (lhs) == REG)
5003             arm_regno = REGNO (lhs);
5004           else if (GET_CODE (rhs) == REG)
5005             arm_regno = REGNO (rhs);
5006           else
5007             abort ();
5008
5009           /* Next insn.  */
5010           first = next_nonnote_insn (first);
5011
5012           if (! arm_cirrus_insn_p (first))
5013             return;
5014
5015           body = PATTERN (first);
5016
5017           /* (float (blah)) is in parallel with a clobber.  */
5018           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5019             body = XVECEXP (body, 0, 0);
5020
5021           if (GET_CODE (body) == FLOAT)
5022             body = XEXP (body, 0);
5023
5024           if (get_attr_cirrus (first) == CIRRUS_MOVE
5025               && GET_CODE (XEXP (body, 1)) == REG
5026               && arm_regno == REGNO (XEXP (body, 1)))
5027             emit_insn_after (gen_nop (), first);
5028
5029           return;
5030         }
5031     }
5032
5033   /* get_attr aborts on USE and CLOBBER.  */
5034   if (!first
5035       || GET_CODE (first) != INSN
5036       || GET_CODE (PATTERN (first)) == USE
5037       || GET_CODE (PATTERN (first)) == CLOBBER)
5038     return;
5039
5040   attr = get_attr_cirrus (first);
5041
5042   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5043      must be followed by a non-coprocessor instruction.  */
5044   if (attr == CIRRUS_COMPARE)
5045     {
5046       nops = 0;
5047
5048       t = next_nonnote_insn (first);
5049
5050       if (arm_cirrus_insn_p (t))
5051         ++ nops;
5052
5053       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5054         ++ nops;
5055
5056       while (nops --)
5057         emit_insn_after (gen_nop (), first);
5058
5059       return;
5060     }
5061 }
5062
5063 /* Return TRUE if X references a SYMBOL_REF.  */
5064 int
5065 symbol_mentioned_p (rtx x)
5066 {
5067   const char * fmt;
5068   int i;
5069
5070   if (GET_CODE (x) == SYMBOL_REF)
5071     return 1;
5072
5073   fmt = GET_RTX_FORMAT (GET_CODE (x));
5074
5075   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5076     {
5077       if (fmt[i] == 'E')
5078         {
5079           int j;
5080
5081           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5082             if (symbol_mentioned_p (XVECEXP (x, i, j)))
5083               return 1;
5084         }
5085       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5086         return 1;
5087     }
5088
5089   return 0;
5090 }
5091
5092 /* Return TRUE if X references a LABEL_REF.  */
5093 int
5094 label_mentioned_p (rtx x)
5095 {
5096   const char * fmt;
5097   int i;
5098
5099   if (GET_CODE (x) == LABEL_REF)
5100     return 1;
5101
5102   fmt = GET_RTX_FORMAT (GET_CODE (x));
5103   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5104     {
5105       if (fmt[i] == 'E')
5106         {
5107           int j;
5108
5109           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5110             if (label_mentioned_p (XVECEXP (x, i, j)))
5111               return 1;
5112         }
5113       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5114         return 1;
5115     }
5116
5117   return 0;
5118 }
5119
5120 enum rtx_code
5121 minmax_code (rtx x)
5122 {
5123   enum rtx_code code = GET_CODE (x);
5124
5125   if (code == SMAX)
5126     return GE;
5127   else if (code == SMIN)
5128     return LE;
5129   else if (code == UMIN)
5130     return LEU;
5131   else if (code == UMAX)
5132     return GEU;
5133
5134   abort ();
5135 }
5136
5137 /* Return 1 if memory locations are adjacent.  */
5138 int
5139 adjacent_mem_locations (rtx a, rtx b)
5140 {
5141   /* We don't guarantee to preserve the order of these memory refs.  */
5142   if (volatile_refs_p (a) || volatile_refs_p (b))
5143     return 0;
5144
5145   if ((GET_CODE (XEXP (a, 0)) == REG
5146        || (GET_CODE (XEXP (a, 0)) == PLUS
5147            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5148       && (GET_CODE (XEXP (b, 0)) == REG
5149           || (GET_CODE (XEXP (b, 0)) == PLUS
5150               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5151     {
5152       HOST_WIDE_INT val0 = 0, val1 = 0;
5153       rtx reg0, reg1;
5154       int val_diff;
5155
5156       if (GET_CODE (XEXP (a, 0)) == PLUS)
5157         {
5158           reg0 = XEXP (XEXP (a, 0), 0);
5159           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5160         }
5161       else
5162         reg0 = XEXP (a, 0);
5163
5164       if (GET_CODE (XEXP (b, 0)) == PLUS)
5165         {
5166           reg1 = XEXP (XEXP (b, 0), 0);
5167           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5168         }
5169       else
5170         reg1 = XEXP (b, 0);
5171
5172       /* Don't accept any offset that will require multiple
5173          instructions to handle, since this would cause the
5174          arith_adjacentmem pattern to output an overlong sequence.  */
5175       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5176         return 0;
5177
5178       /* Don't allow an eliminable register: register elimination can make
5179          the offset too large.  */
5180       if (arm_eliminable_register (reg0))
5181         return 0;
5182
5183       val_diff = val1 - val0;
5184
5185       if (arm_ld_sched)
5186         {
5187           /* If the target has load delay slots, then there's no benefit
5188              to using an ldm instruction unless the offset is zero and
5189              we are optimizing for size.  */
5190           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
5191                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
5192                   && (val_diff == 4 || val_diff == -4));
5193         }
5194
5195       return ((REGNO (reg0) == REGNO (reg1))
5196               && (val_diff == 4 || val_diff == -4));
5197     }
5198
5199   return 0;
5200 }
5201
5202 int
5203 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5204                         HOST_WIDE_INT *load_offset)
5205 {
5206   int unsorted_regs[4];
5207   HOST_WIDE_INT unsorted_offsets[4];
5208   int order[4];
5209   int base_reg = -1;
5210   int i;
5211
5212   /* Can only handle 2, 3, or 4 insns at present,
5213      though could be easily extended if required.  */
5214   if (nops < 2 || nops > 4)
5215     abort ();
5216
5217   /* Loop over the operands and check that the memory references are
5218      suitable (i.e. immediate offsets from the same base register).  At
5219      the same time, extract the target register, and the memory
5220      offsets.  */
5221   for (i = 0; i < nops; i++)
5222     {
5223       rtx reg;
5224       rtx offset;
5225
5226       /* Convert a subreg of a mem into the mem itself.  */
5227       if (GET_CODE (operands[nops + i]) == SUBREG)
5228         operands[nops + i] = alter_subreg (operands + (nops + i));
5229
5230       if (GET_CODE (operands[nops + i]) != MEM)
5231         abort ();
5232
5233       /* Don't reorder volatile memory references; it doesn't seem worth
5234          looking for the case where the order is ok anyway.  */
5235       if (MEM_VOLATILE_P (operands[nops + i]))
5236         return 0;
5237
5238       offset = const0_rtx;
5239
5240       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5241            || (GET_CODE (reg) == SUBREG
5242                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5243           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5244               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5245                    == REG)
5246                   || (GET_CODE (reg) == SUBREG
5247                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5248               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5249                   == CONST_INT)))
5250         {
5251           if (i == 0)
5252             {
5253               base_reg = REGNO (reg);
5254               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5255                                   ? REGNO (operands[i])
5256                                   : REGNO (SUBREG_REG (operands[i])));
5257               order[0] = 0;
5258             }
5259           else
5260             {
5261               if (base_reg != (int) REGNO (reg))
5262                 /* Not addressed from the same base register.  */
5263                 return 0;
5264
5265               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5266                                   ? REGNO (operands[i])
5267                                   : REGNO (SUBREG_REG (operands[i])));
5268               if (unsorted_regs[i] < unsorted_regs[order[0]])
5269                 order[0] = i;
5270             }
5271
5272           /* If it isn't an integer register, or if it overwrites the
5273              base register but isn't the last insn in the list, then
5274              we can't do this.  */
5275           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5276               || (i != nops - 1 && unsorted_regs[i] == base_reg))
5277             return 0;
5278
5279           unsorted_offsets[i] = INTVAL (offset);
5280         }
5281       else
5282         /* Not a suitable memory address.  */
5283         return 0;
5284     }
5285
5286   /* All the useful information has now been extracted from the
5287      operands into unsorted_regs and unsorted_offsets; additionally,
5288      order[0] has been set to the lowest numbered register in the
5289      list.  Sort the registers into order, and check that the memory
5290      offsets are ascending and adjacent.  */
5291
5292   for (i = 1; i < nops; i++)
5293     {
5294       int j;
5295
5296       order[i] = order[i - 1];
5297       for (j = 0; j < nops; j++)
5298         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5299             && (order[i] == order[i - 1]
5300                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5301           order[i] = j;
5302
5303       /* Have we found a suitable register? if not, one must be used more
5304          than once.  */
5305       if (order[i] == order[i - 1])
5306         return 0;
5307
5308       /* Is the memory address adjacent and ascending? */
5309       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5310         return 0;
5311     }
5312
5313   if (base)
5314     {
5315       *base = base_reg;
5316
5317       for (i = 0; i < nops; i++)
5318         regs[i] = unsorted_regs[order[i]];
5319
5320       *load_offset = unsorted_offsets[order[0]];
5321     }
5322
5323   if (unsorted_offsets[order[0]] == 0)
5324     return 1; /* ldmia */
5325
5326   if (unsorted_offsets[order[0]] == 4)
5327     return 2; /* ldmib */
5328
5329   if (unsorted_offsets[order[nops - 1]] == 0)
5330     return 3; /* ldmda */
5331
5332   if (unsorted_offsets[order[nops - 1]] == -4)
5333     return 4; /* ldmdb */
5334
5335   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5336      if the offset isn't small enough.  The reason 2 ldrs are faster
5337      is because these ARMs are able to do more than one cache access
5338      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
5339      whilst the ARM8 has a double bandwidth cache.  This means that
5340      these cores can do both an instruction fetch and a data fetch in
5341      a single cycle, so the trick of calculating the address into a
5342      scratch register (one of the result regs) and then doing a load
5343      multiple actually becomes slower (and no smaller in code size).
5344      That is the transformation
5345
5346         ldr     rd1, [rbase + offset]
5347         ldr     rd2, [rbase + offset + 4]
5348
5349      to
5350
5351         add     rd1, rbase, offset
5352         ldmia   rd1, {rd1, rd2}
5353
5354      produces worse code -- '3 cycles + any stalls on rd2' instead of
5355      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
5356      access per cycle, the first sequence could never complete in less
5357      than 6 cycles, whereas the ldm sequence would only take 5 and
5358      would make better use of sequential accesses if not hitting the
5359      cache.
5360
5361      We cheat here and test 'arm_ld_sched' which we currently know to
5362      only be true for the ARM8, ARM9 and StrongARM.  If this ever
5363      changes, then the test below needs to be reworked.  */
5364   if (nops == 2 && arm_ld_sched)
5365     return 0;
5366
5367   /* Can't do it without setting up the offset, only do this if it takes
5368      no more than one insn.  */
5369   return (const_ok_for_arm (unsorted_offsets[order[0]])
5370           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5371 }
5372
5373 const char *
5374 emit_ldm_seq (rtx *operands, int nops)
5375 {
5376   int regs[4];
5377   int base_reg;
5378   HOST_WIDE_INT offset;
5379   char buf[100];
5380   int i;
5381
5382   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5383     {
5384     case 1:
5385       strcpy (buf, "ldm%?ia\t");
5386       break;
5387
5388     case 2:
5389       strcpy (buf, "ldm%?ib\t");
5390       break;
5391
5392     case 3:
5393       strcpy (buf, "ldm%?da\t");
5394       break;
5395
5396     case 4:
5397       strcpy (buf, "ldm%?db\t");
5398       break;
5399
5400     case 5:
5401       if (offset >= 0)
5402         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5403                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5404                  (long) offset);
5405       else
5406         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5407                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5408                  (long) -offset);
5409       output_asm_insn (buf, operands);
5410       base_reg = regs[0];
5411       strcpy (buf, "ldm%?ia\t");
5412       break;
5413
5414     default:
5415       abort ();
5416     }
5417
5418   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5419            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5420
5421   for (i = 1; i < nops; i++)
5422     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5423              reg_names[regs[i]]);
5424
5425   strcat (buf, "}\t%@ phole ldm");
5426
5427   output_asm_insn (buf, operands);
5428   return "";
5429 }
5430
5431 int
5432 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5433                          HOST_WIDE_INT * load_offset)
5434 {
5435   int unsorted_regs[4];
5436   HOST_WIDE_INT unsorted_offsets[4];
5437   int order[4];
5438   int base_reg = -1;
5439   int i;
5440
5441   /* Can only handle 2, 3, or 4 insns at present, though could be easily
5442      extended if required.  */
5443   if (nops < 2 || nops > 4)
5444     abort ();
5445
5446   /* Loop over the operands and check that the memory references are
5447      suitable (i.e. immediate offsets from the same base register).  At
5448      the same time, extract the target register, and the memory
5449      offsets.  */
5450   for (i = 0; i < nops; i++)
5451     {
5452       rtx reg;
5453       rtx offset;
5454
5455       /* Convert a subreg of a mem into the mem itself.  */
5456       if (GET_CODE (operands[nops + i]) == SUBREG)
5457         operands[nops + i] = alter_subreg (operands + (nops + i));
5458
5459       if (GET_CODE (operands[nops + i]) != MEM)
5460         abort ();
5461
5462       /* Don't reorder volatile memory references; it doesn't seem worth
5463          looking for the case where the order is ok anyway.  */
5464       if (MEM_VOLATILE_P (operands[nops + i]))
5465         return 0;
5466
5467       offset = const0_rtx;
5468
5469       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5470            || (GET_CODE (reg) == SUBREG
5471                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5472           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5473               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5474                    == REG)
5475                   || (GET_CODE (reg) == SUBREG
5476                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5477               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5478                   == CONST_INT)))
5479         {
5480           if (i == 0)
5481             {
5482               base_reg = REGNO (reg);
5483               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5484                                   ? REGNO (operands[i])
5485                                   : REGNO (SUBREG_REG (operands[i])));
5486               order[0] = 0;
5487             }
5488           else
5489             {
5490               if (base_reg != (int) REGNO (reg))
5491                 /* Not addressed from the same base register.  */
5492                 return 0;
5493
5494               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5495                                   ? REGNO (operands[i])
5496                                   : REGNO (SUBREG_REG (operands[i])));
5497               if (unsorted_regs[i] < unsorted_regs[order[0]])
5498                 order[0] = i;
5499             }
5500
5501           /* If it isn't an integer register, then we can't do this.  */
5502           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5503             return 0;
5504
5505           unsorted_offsets[i] = INTVAL (offset);
5506         }
5507       else
5508         /* Not a suitable memory address.  */
5509         return 0;
5510     }
5511
5512   /* All the useful information has now been extracted from the
5513      operands into unsorted_regs and unsorted_offsets; additionally,
5514      order[0] has been set to the lowest numbered register in the
5515      list.  Sort the registers into order, and check that the memory
5516      offsets are ascending and adjacent.  */
5517
5518   for (i = 1; i < nops; i++)
5519     {
5520       int j;
5521
5522       order[i] = order[i - 1];
5523       for (j = 0; j < nops; j++)
5524         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5525             && (order[i] == order[i - 1]
5526                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5527           order[i] = j;
5528
5529       /* Have we found a suitable register? if not, one must be used more
5530          than once.  */
5531       if (order[i] == order[i - 1])
5532         return 0;
5533
5534       /* Is the memory address adjacent and ascending? */
5535       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5536         return 0;
5537     }
5538
5539   if (base)
5540     {
5541       *base = base_reg;
5542
5543       for (i = 0; i < nops; i++)
5544         regs[i] = unsorted_regs[order[i]];
5545
5546       *load_offset = unsorted_offsets[order[0]];
5547     }
5548
5549   if (unsorted_offsets[order[0]] == 0)
5550     return 1; /* stmia */
5551
5552   if (unsorted_offsets[order[0]] == 4)
5553     return 2; /* stmib */
5554
5555   if (unsorted_offsets[order[nops - 1]] == 0)
5556     return 3; /* stmda */
5557
5558   if (unsorted_offsets[order[nops - 1]] == -4)
5559     return 4; /* stmdb */
5560
5561   return 0;
5562 }
5563
5564 const char *
5565 emit_stm_seq (rtx *operands, int nops)
5566 {
5567   int regs[4];
5568   int base_reg;
5569   HOST_WIDE_INT offset;
5570   char buf[100];
5571   int i;
5572
5573   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5574     {
5575     case 1:
5576       strcpy (buf, "stm%?ia\t");
5577       break;
5578
5579     case 2:
5580       strcpy (buf, "stm%?ib\t");
5581       break;
5582
5583     case 3:
5584       strcpy (buf, "stm%?da\t");
5585       break;
5586
5587     case 4:
5588       strcpy (buf, "stm%?db\t");
5589       break;
5590
5591     default:
5592       abort ();
5593     }
5594
5595   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5596            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5597
5598   for (i = 1; i < nops; i++)
5599     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5600              reg_names[regs[i]]);
5601
5602   strcat (buf, "}\t%@ phole stm");
5603
5604   output_asm_insn (buf, operands);
5605   return "";
5606 }
5607
5608 \f
5609 /* Routines for use in generating RTL.  */
5610
5611 rtx
5612 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5613                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5614 {
5615   HOST_WIDE_INT offset = *offsetp;
5616   int i = 0, j;
5617   rtx result;
5618   int sign = up ? 1 : -1;
5619   rtx mem, addr;
5620
5621   /* XScale has load-store double instructions, but they have stricter
5622      alignment requirements than load-store multiple, so we cannot
5623      use them.
5624
5625      For XScale ldm requires 2 + NREGS cycles to complete and blocks
5626      the pipeline until completion.
5627
5628         NREGS           CYCLES
5629           1               3
5630           2               4
5631           3               5
5632           4               6
5633
5634      An ldr instruction takes 1-3 cycles, but does not block the
5635      pipeline.
5636
5637         NREGS           CYCLES
5638           1              1-3
5639           2              2-6
5640           3              3-9
5641           4              4-12
5642
5643      Best case ldr will always win.  However, the more ldr instructions
5644      we issue, the less likely we are to be able to schedule them well.
5645      Using ldr instructions also increases code size.
5646
5647      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5648      for counts of 3 or 4 regs.  */
5649   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5650     {
5651       rtx seq;
5652
5653       start_sequence ();
5654
5655       for (i = 0; i < count; i++)
5656         {
5657           addr = plus_constant (from, i * 4 * sign);
5658           mem = adjust_automodify_address (basemem, SImode, addr, offset);
5659           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5660           offset += 4 * sign;
5661         }
5662
5663       if (write_back)
5664         {
5665           emit_move_insn (from, plus_constant (from, count * 4 * sign));
5666           *offsetp = offset;
5667         }
5668
5669       seq = get_insns ();
5670       end_sequence ();
5671
5672       return seq;
5673     }
5674
5675   result = gen_rtx_PARALLEL (VOIDmode,
5676                              rtvec_alloc (count + (write_back ? 1 : 0)));
5677   if (write_back)
5678     {
5679       XVECEXP (result, 0, 0)
5680         = gen_rtx_SET (GET_MODE (from), from,
5681                        plus_constant (from, count * 4 * sign));
5682       i = 1;
5683       count++;
5684     }
5685
5686   for (j = 0; i < count; i++, j++)
5687     {
5688       addr = plus_constant (from, j * 4 * sign);
5689       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5690       XVECEXP (result, 0, i)
5691         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5692       offset += 4 * sign;
5693     }
5694
5695   if (write_back)
5696     *offsetp = offset;
5697
5698   return result;
5699 }
5700
5701 rtx
5702 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5703                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5704 {
5705   HOST_WIDE_INT offset = *offsetp;
5706   int i = 0, j;
5707   rtx result;
5708   int sign = up ? 1 : -1;
5709   rtx mem, addr;
5710
5711   /* See arm_gen_load_multiple for discussion of
5712      the pros/cons of ldm/stm usage for XScale.  */
5713   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5714     {
5715       rtx seq;
5716
5717       start_sequence ();
5718
5719       for (i = 0; i < count; i++)
5720         {
5721           addr = plus_constant (to, i * 4 * sign);
5722           mem = adjust_automodify_address (basemem, SImode, addr, offset);
5723           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5724           offset += 4 * sign;
5725         }
5726
5727       if (write_back)
5728         {
5729           emit_move_insn (to, plus_constant (to, count * 4 * sign));
5730           *offsetp = offset;
5731         }
5732
5733       seq = get_insns ();
5734       end_sequence ();
5735
5736       return seq;
5737     }
5738
5739   result = gen_rtx_PARALLEL (VOIDmode,
5740                              rtvec_alloc (count + (write_back ? 1 : 0)));
5741   if (write_back)
5742     {
5743       XVECEXP (result, 0, 0)
5744         = gen_rtx_SET (GET_MODE (to), to,
5745                        plus_constant (to, count * 4 * sign));
5746       i = 1;
5747       count++;
5748     }
5749
5750   for (j = 0; i < count; i++, j++)
5751     {
5752       addr = plus_constant (to, j * 4 * sign);
5753       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5754       XVECEXP (result, 0, i)
5755         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5756       offset += 4 * sign;
5757     }
5758
5759   if (write_back)
5760     *offsetp = offset;
5761
5762   return result;
5763 }
5764
5765 int
5766 arm_gen_movmemqi (rtx *operands)
5767 {
5768   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5769   HOST_WIDE_INT srcoffset, dstoffset;
5770   int i;
5771   rtx src, dst, srcbase, dstbase;
5772   rtx part_bytes_reg = NULL;
5773   rtx mem;
5774
5775   if (GET_CODE (operands[2]) != CONST_INT
5776       || GET_CODE (operands[3]) != CONST_INT
5777       || INTVAL (operands[2]) > 64
5778       || INTVAL (operands[3]) & 3)
5779     return 0;
5780
5781   dstbase = operands[0];
5782   srcbase = operands[1];
5783
5784   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
5785   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
5786
5787   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5788   out_words_to_go = INTVAL (operands[2]) / 4;
5789   last_bytes = INTVAL (operands[2]) & 3;
5790   dstoffset = srcoffset = 0;
5791
5792   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5793     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5794
5795   for (i = 0; in_words_to_go >= 2; i+=4)
5796     {
5797       if (in_words_to_go > 4)
5798         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5799                                           srcbase, &srcoffset));
5800       else
5801         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
5802                                           FALSE, srcbase, &srcoffset));
5803
5804       if (out_words_to_go)
5805         {
5806           if (out_words_to_go > 4)
5807             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5808                                                dstbase, &dstoffset));
5809           else if (out_words_to_go != 1)
5810             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5811                                                dst, TRUE,
5812                                                (last_bytes == 0
5813                                                 ? FALSE : TRUE),
5814                                                dstbase, &dstoffset));
5815           else
5816             {
5817               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5818               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5819               if (last_bytes != 0)
5820                 {
5821                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5822                   dstoffset += 4;
5823                 }
5824             }
5825         }
5826
5827       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5828       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
5829     }
5830
5831   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
5832   if (out_words_to_go)
5833     {
5834       rtx sreg;
5835
5836       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5837       sreg = copy_to_reg (mem);
5838
5839       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5840       emit_move_insn (mem, sreg);
5841       in_words_to_go--;
5842
5843       if (in_words_to_go)       /* Sanity check */
5844         abort ();
5845     }
5846
5847   if (in_words_to_go)
5848     {
5849       if (in_words_to_go < 0)
5850         abort ();
5851
5852       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5853       part_bytes_reg = copy_to_mode_reg (SImode, mem);
5854     }
5855
5856   if (last_bytes && part_bytes_reg == NULL)
5857     abort ();
5858
5859   if (BYTES_BIG_ENDIAN && last_bytes)
5860     {
5861       rtx tmp = gen_reg_rtx (SImode);
5862
5863       /* The bytes we want are in the top end of the word.  */
5864       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
5865                               GEN_INT (8 * (4 - last_bytes))));
5866       part_bytes_reg = tmp;
5867
5868       while (last_bytes)
5869         {
5870           mem = adjust_automodify_address (dstbase, QImode,
5871                                            plus_constant (dst, last_bytes - 1),
5872                                            dstoffset + last_bytes - 1);
5873           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5874
5875           if (--last_bytes)
5876             {
5877               tmp = gen_reg_rtx (SImode);
5878               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
5879               part_bytes_reg = tmp;
5880             }
5881         }
5882
5883     }
5884   else
5885     {
5886       if (last_bytes > 1)
5887         {
5888           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
5889           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
5890           last_bytes -= 2;
5891           if (last_bytes)
5892             {
5893               rtx tmp = gen_reg_rtx (SImode);
5894               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
5895               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
5896               part_bytes_reg = tmp;
5897               dstoffset += 2;
5898             }
5899         }
5900
5901       if (last_bytes)
5902         {
5903           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
5904           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5905         }
5906     }
5907
5908   return 1;
5909 }
5910
5911 /* Generate a memory reference for a half word, such that it will be loaded
5912    into the top 16 bits of the word.  We can assume that the address is
5913    known to be alignable and of the form reg, or plus (reg, const).  */
5914
5915 rtx
5916 arm_gen_rotated_half_load (rtx memref)
5917 {
5918   HOST_WIDE_INT offset = 0;
5919   rtx base = XEXP (memref, 0);
5920
5921   if (GET_CODE (base) == PLUS)
5922     {
5923       offset = INTVAL (XEXP (base, 1));
5924       base = XEXP (base, 0);
5925     }
5926
5927   /* If we aren't allowed to generate unaligned addresses, then fail.  */
5928   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0))
5929     return NULL;
5930
5931   base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
5932
5933   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
5934     return base;
5935
5936   return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
5937 }
5938
5939 /* Select a dominance comparison mode if possible for a test of the general
5940    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
5941    COND_OR == DOM_CC_X_AND_Y => (X && Y)
5942    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
5943    COND_OR == DOM_CC_X_OR_Y => (X || Y)
5944    In all cases OP will be either EQ or NE, but we don't need to know which
5945    here.  If we are unable to support a dominance comparison we return
5946    CC mode.  This will then fail to match for the RTL expressions that
5947    generate this call.  */
5948 enum machine_mode
5949 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
5950 {
5951   enum rtx_code cond1, cond2;
5952   int swapped = 0;
5953
5954   /* Currently we will probably get the wrong result if the individual
5955      comparisons are not simple.  This also ensures that it is safe to
5956      reverse a comparison if necessary.  */
5957   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
5958        != CCmode)
5959       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
5960           != CCmode))
5961     return CCmode;
5962
5963   /* The if_then_else variant of this tests the second condition if the
5964      first passes, but is true if the first fails.  Reverse the first
5965      condition to get a true "inclusive-or" expression.  */
5966   if (cond_or == DOM_CC_NX_OR_Y)
5967     cond1 = reverse_condition (cond1);
5968
5969   /* If the comparisons are not equal, and one doesn't dominate the other,
5970      then we can't do this.  */
5971   if (cond1 != cond2
5972       && !comparison_dominates_p (cond1, cond2)
5973       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
5974     return CCmode;
5975
5976   if (swapped)
5977     {
5978       enum rtx_code temp = cond1;
5979       cond1 = cond2;
5980       cond2 = temp;
5981     }
5982
5983   switch (cond1)
5984     {
5985     case EQ:
5986       if (cond2 == EQ || cond_or == DOM_CC_X_AND_Y)
5987         return CC_DEQmode;
5988
5989       switch (cond2)
5990         {
5991         case LE: return CC_DLEmode;
5992         case LEU: return CC_DLEUmode;
5993         case GE: return CC_DGEmode;
5994         case GEU: return CC_DGEUmode;
5995         default: break;
5996         }
5997
5998       break;
5999
6000     case LT:
6001       if (cond2 == LT || cond_or == DOM_CC_X_AND_Y)
6002         return CC_DLTmode;
6003       if (cond2 == LE)
6004         return CC_DLEmode;
6005       if (cond2 == NE)
6006         return CC_DNEmode;
6007       break;
6008
6009     case GT:
6010       if (cond2 == GT || cond_or == DOM_CC_X_AND_Y)
6011         return CC_DGTmode;
6012       if (cond2 == GE)
6013         return CC_DGEmode;
6014       if (cond2 == NE)
6015         return CC_DNEmode;
6016       break;
6017
6018     case LTU:
6019       if (cond2 == LTU || cond_or == DOM_CC_X_AND_Y)
6020         return CC_DLTUmode;
6021       if (cond2 == LEU)
6022         return CC_DLEUmode;
6023       if (cond2 == NE)
6024         return CC_DNEmode;
6025       break;
6026
6027     case GTU:
6028       if (cond2 == GTU || cond_or == DOM_CC_X_AND_Y)
6029         return CC_DGTUmode;
6030       if (cond2 == GEU)
6031         return CC_DGEUmode;
6032       if (cond2 == NE)
6033         return CC_DNEmode;
6034       break;
6035
6036     /* The remaining cases only occur when both comparisons are the
6037        same.  */
6038     case NE:
6039       return CC_DNEmode;
6040
6041     case LE:
6042       return CC_DLEmode;
6043
6044     case GE:
6045       return CC_DGEmode;
6046
6047     case LEU:
6048       return CC_DLEUmode;
6049
6050     case GEU:
6051       return CC_DGEUmode;
6052
6053     default:
6054       break;
6055     }
6056
6057   abort ();
6058 }
6059
6060 enum machine_mode
6061 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6062 {
6063   /* All floating point compares return CCFP if it is an equality
6064      comparison, and CCFPE otherwise.  */
6065   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6066     {
6067       switch (op)
6068         {
6069         case EQ:
6070         case NE:
6071         case UNORDERED:
6072         case ORDERED:
6073         case UNLT:
6074         case UNLE:
6075         case UNGT:
6076         case UNGE:
6077         case UNEQ:
6078         case LTGT:
6079           return CCFPmode;
6080
6081         case LT:
6082         case LE:
6083         case GT:
6084         case GE:
6085           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6086             return CCFPmode;
6087           return CCFPEmode;
6088
6089         default:
6090           abort ();
6091         }
6092     }
6093
6094   /* A compare with a shifted operand.  Because of canonicalization, the
6095      comparison will have to be swapped when we emit the assembler.  */
6096   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6097       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6098           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6099           || GET_CODE (x) == ROTATERT))
6100     return CC_SWPmode;
6101
6102   /* This operation is performed swapped, but since we only rely on the Z
6103      flag we don't need an additional mode.  */
6104   if (GET_MODE (y) == SImode && REG_P (y)
6105       && GET_CODE (x) == NEG
6106       && (op == EQ || op == NE))
6107     return CC_Zmode;
6108
6109   /* This is a special case that is used by combine to allow a
6110      comparison of a shifted byte load to be split into a zero-extend
6111      followed by a comparison of the shifted integer (only valid for
6112      equalities and unsigned inequalities).  */
6113   if (GET_MODE (x) == SImode
6114       && GET_CODE (x) == ASHIFT
6115       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6116       && GET_CODE (XEXP (x, 0)) == SUBREG
6117       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6118       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6119       && (op == EQ || op == NE
6120           || op == GEU || op == GTU || op == LTU || op == LEU)
6121       && GET_CODE (y) == CONST_INT)
6122     return CC_Zmode;
6123
6124   /* A construct for a conditional compare, if the false arm contains
6125      0, then both conditions must be true, otherwise either condition
6126      must be true.  Not all conditions are possible, so CCmode is
6127      returned if it can't be done.  */
6128   if (GET_CODE (x) == IF_THEN_ELSE
6129       && (XEXP (x, 2) == const0_rtx
6130           || XEXP (x, 2) == const1_rtx)
6131       && COMPARISON_P (XEXP (x, 0))
6132       && COMPARISON_P (XEXP (x, 1)))
6133     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6134                                          INTVAL (XEXP (x, 2)));
6135
6136   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
6137   if (GET_CODE (x) == AND
6138       && COMPARISON_P (XEXP (x, 0))
6139       && COMPARISON_P (XEXP (x, 1)))
6140     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6141                                          DOM_CC_X_AND_Y);
6142
6143   if (GET_CODE (x) == IOR
6144       && COMPARISON_P (XEXP (x, 0))
6145       && COMPARISON_P (XEXP (x, 1)))
6146     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6147                                          DOM_CC_X_OR_Y);
6148
6149   /* An operation (on Thumb) where we want to test for a single bit.
6150      This is done by shifting that bit up into the top bit of a
6151      scratch register; we can then branch on the sign bit.  */
6152   if (TARGET_THUMB
6153       && GET_MODE (x) == SImode
6154       && (op == EQ || op == NE)
6155       && (GET_CODE (x) == ZERO_EXTRACT))
6156     return CC_Nmode;
6157
6158   /* An operation that sets the condition codes as a side-effect, the
6159      V flag is not set correctly, so we can only use comparisons where
6160      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
6161      instead.)  */
6162   if (GET_MODE (x) == SImode
6163       && y == const0_rtx
6164       && (op == EQ || op == NE || op == LT || op == GE)
6165       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6166           || GET_CODE (x) == AND || GET_CODE (x) == IOR
6167           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6168           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6169           || GET_CODE (x) == LSHIFTRT
6170           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6171           || GET_CODE (x) == ROTATERT
6172           || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6173     return CC_NOOVmode;
6174
6175   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6176     return CC_Zmode;
6177
6178   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6179       && GET_CODE (x) == PLUS
6180       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6181     return CC_Cmode;
6182
6183   return CCmode;
6184 }
6185
6186 /* X and Y are two things to compare using CODE.  Emit the compare insn and
6187    return the rtx for register 0 in the proper mode.  FP means this is a
6188    floating point compare: I don't think that it is needed on the arm.  */
6189 rtx
6190 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6191 {
6192   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6193   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6194
6195   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6196                           gen_rtx_COMPARE (mode, x, y)));
6197
6198   return cc_reg;
6199 }
6200
6201 /* Generate a sequence of insns that will generate the correct return
6202    address mask depending on the physical architecture that the program
6203    is running on.  */
6204 rtx
6205 arm_gen_return_addr_mask (void)
6206 {
6207   rtx reg = gen_reg_rtx (Pmode);
6208
6209   emit_insn (gen_return_addr_mask (reg));
6210   return reg;
6211 }
6212
6213 void
6214 arm_reload_in_hi (rtx *operands)
6215 {
6216   rtx ref = operands[1];
6217   rtx base, scratch;
6218   HOST_WIDE_INT offset = 0;
6219
6220   if (GET_CODE (ref) == SUBREG)
6221     {
6222       offset = SUBREG_BYTE (ref);
6223       ref = SUBREG_REG (ref);
6224     }
6225
6226   if (GET_CODE (ref) == REG)
6227     {
6228       /* We have a pseudo which has been spilt onto the stack; there
6229          are two cases here: the first where there is a simple
6230          stack-slot replacement and a second where the stack-slot is
6231          out of range, or is used as a subreg.  */
6232       if (reg_equiv_mem[REGNO (ref)])
6233         {
6234           ref = reg_equiv_mem[REGNO (ref)];
6235           base = find_replacement (&XEXP (ref, 0));
6236         }
6237       else
6238         /* The slot is out of range, or was dressed up in a SUBREG.  */
6239         base = reg_equiv_address[REGNO (ref)];
6240     }
6241   else
6242     base = find_replacement (&XEXP (ref, 0));
6243
6244   /* Handle the case where the address is too complex to be offset by 1.  */
6245   if (GET_CODE (base) == MINUS
6246       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6247     {
6248       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6249
6250       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6251       base = base_plus;
6252     }
6253   else if (GET_CODE (base) == PLUS)
6254     {
6255       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6256       HOST_WIDE_INT hi, lo;
6257
6258       offset += INTVAL (XEXP (base, 1));
6259       base = XEXP (base, 0);
6260
6261       /* Rework the address into a legal sequence of insns.  */
6262       /* Valid range for lo is -4095 -> 4095 */
6263       lo = (offset >= 0
6264             ? (offset & 0xfff)
6265             : -((-offset) & 0xfff));
6266
6267       /* Corner case, if lo is the max offset then we would be out of range
6268          once we have added the additional 1 below, so bump the msb into the
6269          pre-loading insn(s).  */
6270       if (lo == 4095)
6271         lo &= 0x7ff;
6272
6273       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6274              ^ (HOST_WIDE_INT) 0x80000000)
6275             - (HOST_WIDE_INT) 0x80000000);
6276
6277       if (hi + lo != offset)
6278         abort ();
6279
6280       if (hi != 0)
6281         {
6282           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6283
6284           /* Get the base address; addsi3 knows how to handle constants
6285              that require more than one insn.  */
6286           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6287           base = base_plus;
6288           offset = lo;
6289         }
6290     }
6291
6292   /* Operands[2] may overlap operands[0] (though it won't overlap
6293      operands[1]), that's why we asked for a DImode reg -- so we can
6294      use the bit that does not overlap.  */
6295   if (REGNO (operands[2]) == REGNO (operands[0]))
6296     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6297   else
6298     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6299
6300   emit_insn (gen_zero_extendqisi2 (scratch,
6301                                    gen_rtx_MEM (QImode,
6302                                                 plus_constant (base,
6303                                                                offset))));
6304   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6305                                    gen_rtx_MEM (QImode,
6306                                                 plus_constant (base,
6307                                                                offset + 1))));
6308   if (!BYTES_BIG_ENDIAN)
6309     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6310                         gen_rtx_IOR (SImode,
6311                                      gen_rtx_ASHIFT
6312                                      (SImode,
6313                                       gen_rtx_SUBREG (SImode, operands[0], 0),
6314                                       GEN_INT (8)),
6315                                      scratch)));
6316   else
6317     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6318                             gen_rtx_IOR (SImode,
6319                                          gen_rtx_ASHIFT (SImode, scratch,
6320                                                          GEN_INT (8)),
6321                                          gen_rtx_SUBREG (SImode, operands[0],
6322                                                          0))));
6323 }
6324
6325 /* Handle storing a half-word to memory during reload by synthesizing as two
6326    byte stores.  Take care not to clobber the input values until after we
6327    have moved them somewhere safe.  This code assumes that if the DImode
6328    scratch in operands[2] overlaps either the input value or output address
6329    in some way, then that value must die in this insn (we absolutely need
6330    two scratch registers for some corner cases).  */
6331 void
6332 arm_reload_out_hi (rtx *operands)
6333 {
6334   rtx ref = operands[0];
6335   rtx outval = operands[1];
6336   rtx base, scratch;
6337   HOST_WIDE_INT offset = 0;
6338
6339   if (GET_CODE (ref) == SUBREG)
6340     {
6341       offset = SUBREG_BYTE (ref);
6342       ref = SUBREG_REG (ref);
6343     }
6344
6345   if (GET_CODE (ref) == REG)
6346     {
6347       /* We have a pseudo which has been spilt onto the stack; there
6348          are two cases here: the first where there is a simple
6349          stack-slot replacement and a second where the stack-slot is
6350          out of range, or is used as a subreg.  */
6351       if (reg_equiv_mem[REGNO (ref)])
6352         {
6353           ref = reg_equiv_mem[REGNO (ref)];
6354           base = find_replacement (&XEXP (ref, 0));
6355         }
6356       else
6357         /* The slot is out of range, or was dressed up in a SUBREG.  */
6358         base = reg_equiv_address[REGNO (ref)];
6359     }
6360   else
6361     base = find_replacement (&XEXP (ref, 0));
6362
6363   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6364
6365   /* Handle the case where the address is too complex to be offset by 1.  */
6366   if (GET_CODE (base) == MINUS
6367       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6368     {
6369       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6370
6371       /* Be careful not to destroy OUTVAL.  */
6372       if (reg_overlap_mentioned_p (base_plus, outval))
6373         {
6374           /* Updating base_plus might destroy outval, see if we can
6375              swap the scratch and base_plus.  */
6376           if (!reg_overlap_mentioned_p (scratch, outval))
6377             {
6378               rtx tmp = scratch;
6379               scratch = base_plus;
6380               base_plus = tmp;
6381             }
6382           else
6383             {
6384               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6385
6386               /* Be conservative and copy OUTVAL into the scratch now,
6387                  this should only be necessary if outval is a subreg
6388                  of something larger than a word.  */
6389               /* XXX Might this clobber base?  I can't see how it can,
6390                  since scratch is known to overlap with OUTVAL, and
6391                  must be wider than a word.  */
6392               emit_insn (gen_movhi (scratch_hi, outval));
6393               outval = scratch_hi;
6394             }
6395         }
6396
6397       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6398       base = base_plus;
6399     }
6400   else if (GET_CODE (base) == PLUS)
6401     {
6402       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6403       HOST_WIDE_INT hi, lo;
6404
6405       offset += INTVAL (XEXP (base, 1));
6406       base = XEXP (base, 0);
6407
6408       /* Rework the address into a legal sequence of insns.  */
6409       /* Valid range for lo is -4095 -> 4095 */
6410       lo = (offset >= 0
6411             ? (offset & 0xfff)
6412             : -((-offset) & 0xfff));
6413
6414       /* Corner case, if lo is the max offset then we would be out of range
6415          once we have added the additional 1 below, so bump the msb into the
6416          pre-loading insn(s).  */
6417       if (lo == 4095)
6418         lo &= 0x7ff;
6419
6420       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6421              ^ (HOST_WIDE_INT) 0x80000000)
6422             - (HOST_WIDE_INT) 0x80000000);
6423
6424       if (hi + lo != offset)
6425         abort ();
6426
6427       if (hi != 0)
6428         {
6429           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6430
6431           /* Be careful not to destroy OUTVAL.  */
6432           if (reg_overlap_mentioned_p (base_plus, outval))
6433             {
6434               /* Updating base_plus might destroy outval, see if we
6435                  can swap the scratch and base_plus.  */
6436               if (!reg_overlap_mentioned_p (scratch, outval))
6437                 {
6438                   rtx tmp = scratch;
6439                   scratch = base_plus;
6440                   base_plus = tmp;
6441                 }
6442               else
6443                 {
6444                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6445
6446                   /* Be conservative and copy outval into scratch now,
6447                      this should only be necessary if outval is a
6448                      subreg of something larger than a word.  */
6449                   /* XXX Might this clobber base?  I can't see how it
6450                      can, since scratch is known to overlap with
6451                      outval.  */
6452                   emit_insn (gen_movhi (scratch_hi, outval));
6453                   outval = scratch_hi;
6454                 }
6455             }
6456
6457           /* Get the base address; addsi3 knows how to handle constants
6458              that require more than one insn.  */
6459           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6460           base = base_plus;
6461           offset = lo;
6462         }
6463     }
6464
6465   if (BYTES_BIG_ENDIAN)
6466     {
6467       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6468                                          plus_constant (base, offset + 1)),
6469                             gen_lowpart (QImode, outval)));
6470       emit_insn (gen_lshrsi3 (scratch,
6471                               gen_rtx_SUBREG (SImode, outval, 0),
6472                               GEN_INT (8)));
6473       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6474                             gen_lowpart (QImode, scratch)));
6475     }
6476   else
6477     {
6478       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6479                             gen_lowpart (QImode, outval)));
6480       emit_insn (gen_lshrsi3 (scratch,
6481                               gen_rtx_SUBREG (SImode, outval, 0),
6482                               GEN_INT (8)));
6483       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6484                                          plus_constant (base, offset + 1)),
6485                             gen_lowpart (QImode, scratch)));
6486     }
6487 }
6488 \f
6489 /* Print a symbolic form of X to the debug file, F.  */
6490 static void
6491 arm_print_value (FILE *f, rtx x)
6492 {
6493   switch (GET_CODE (x))
6494     {
6495     case CONST_INT:
6496       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6497       return;
6498
6499     case CONST_DOUBLE:
6500       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6501       return;
6502
6503     case CONST_VECTOR:
6504       {
6505         int i;
6506
6507         fprintf (f, "<");
6508         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6509           {
6510             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6511             if (i < (CONST_VECTOR_NUNITS (x) - 1))
6512               fputc (',', f);
6513           }
6514         fprintf (f, ">");
6515       }
6516       return;
6517
6518     case CONST_STRING:
6519       fprintf (f, "\"%s\"", XSTR (x, 0));
6520       return;
6521
6522     case SYMBOL_REF:
6523       fprintf (f, "`%s'", XSTR (x, 0));
6524       return;
6525
6526     case LABEL_REF:
6527       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6528       return;
6529
6530     case CONST:
6531       arm_print_value (f, XEXP (x, 0));
6532       return;
6533
6534     case PLUS:
6535       arm_print_value (f, XEXP (x, 0));
6536       fprintf (f, "+");
6537       arm_print_value (f, XEXP (x, 1));
6538       return;
6539
6540     case PC:
6541       fprintf (f, "pc");
6542       return;
6543
6544     default:
6545       fprintf (f, "????");
6546       return;
6547     }
6548 }
6549 \f
6550 /* Routines for manipulation of the constant pool.  */
6551
6552 /* Arm instructions cannot load a large constant directly into a
6553    register; they have to come from a pc relative load.  The constant
6554    must therefore be placed in the addressable range of the pc
6555    relative load.  Depending on the precise pc relative load
6556    instruction the range is somewhere between 256 bytes and 4k.  This
6557    means that we often have to dump a constant inside a function, and
6558    generate code to branch around it.
6559
6560    It is important to minimize this, since the branches will slow
6561    things down and make the code larger.
6562
6563    Normally we can hide the table after an existing unconditional
6564    branch so that there is no interruption of the flow, but in the
6565    worst case the code looks like this:
6566
6567         ldr     rn, L1
6568         ...
6569         b       L2
6570         align
6571         L1:     .long value
6572         L2:
6573         ...
6574
6575         ldr     rn, L3
6576         ...
6577         b       L4
6578         align
6579         L3:     .long value
6580         L4:
6581         ...
6582
6583    We fix this by performing a scan after scheduling, which notices
6584    which instructions need to have their operands fetched from the
6585    constant table and builds the table.
6586
6587    The algorithm starts by building a table of all the constants that
6588    need fixing up and all the natural barriers in the function (places
6589    where a constant table can be dropped without breaking the flow).
6590    For each fixup we note how far the pc-relative replacement will be
6591    able to reach and the offset of the instruction into the function.
6592
6593    Having built the table we then group the fixes together to form
6594    tables that are as large as possible (subject to addressing
6595    constraints) and emit each table of constants after the last
6596    barrier that is within range of all the instructions in the group.
6597    If a group does not contain a barrier, then we forcibly create one
6598    by inserting a jump instruction into the flow.  Once the table has
6599    been inserted, the insns are then modified to reference the
6600    relevant entry in the pool.
6601
6602    Possible enhancements to the algorithm (not implemented) are:
6603
6604    1) For some processors and object formats, there may be benefit in
6605    aligning the pools to the start of cache lines; this alignment
6606    would need to be taken into account when calculating addressability
6607    of a pool.  */
6608
6609 /* These typedefs are located at the start of this file, so that
6610    they can be used in the prototypes there.  This comment is to
6611    remind readers of that fact so that the following structures
6612    can be understood more easily.
6613
6614      typedef struct minipool_node    Mnode;
6615      typedef struct minipool_fixup   Mfix;  */
6616
6617 struct minipool_node
6618 {
6619   /* Doubly linked chain of entries.  */
6620   Mnode * next;
6621   Mnode * prev;
6622   /* The maximum offset into the code that this entry can be placed.  While
6623      pushing fixes for forward references, all entries are sorted in order
6624      of increasing max_address.  */
6625   HOST_WIDE_INT max_address;
6626   /* Similarly for an entry inserted for a backwards ref.  */
6627   HOST_WIDE_INT min_address;
6628   /* The number of fixes referencing this entry.  This can become zero
6629      if we "unpush" an entry.  In this case we ignore the entry when we
6630      come to emit the code.  */
6631   int refcount;
6632   /* The offset from the start of the minipool.  */
6633   HOST_WIDE_INT offset;
6634   /* The value in table.  */
6635   rtx value;
6636   /* The mode of value.  */
6637   enum machine_mode mode;
6638   /* The size of the value.  With iWMMXt enabled
6639      sizes > 4 also imply an alignment of 8-bytes.  */
6640   int fix_size;
6641 };
6642
6643 struct minipool_fixup
6644 {
6645   Mfix *            next;
6646   rtx               insn;
6647   HOST_WIDE_INT     address;
6648   rtx *             loc;
6649   enum machine_mode mode;
6650   int               fix_size;
6651   rtx               value;
6652   Mnode *           minipool;
6653   HOST_WIDE_INT     forwards;
6654   HOST_WIDE_INT     backwards;
6655 };
6656
6657 /* Fixes less than a word need padding out to a word boundary.  */
6658 #define MINIPOOL_FIX_SIZE(mode) \
6659   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6660
6661 static Mnode *  minipool_vector_head;
6662 static Mnode *  minipool_vector_tail;
6663 static rtx      minipool_vector_label;
6664
6665 /* The linked list of all minipool fixes required for this function.  */
6666 Mfix *          minipool_fix_head;
6667 Mfix *          minipool_fix_tail;
6668 /* The fix entry for the current minipool, once it has been placed.  */
6669 Mfix *          minipool_barrier;
6670
6671 /* Determines if INSN is the start of a jump table.  Returns the end
6672    of the TABLE or NULL_RTX.  */
6673 static rtx
6674 is_jump_table (rtx insn)
6675 {
6676   rtx table;
6677
6678   if (GET_CODE (insn) == JUMP_INSN
6679       && JUMP_LABEL (insn) != NULL
6680       && ((table = next_real_insn (JUMP_LABEL (insn)))
6681           == next_real_insn (insn))
6682       && table != NULL
6683       && GET_CODE (table) == JUMP_INSN
6684       && (GET_CODE (PATTERN (table)) == ADDR_VEC
6685           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6686     return table;
6687
6688   return NULL_RTX;
6689 }
6690
6691 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6692 #define JUMP_TABLES_IN_TEXT_SECTION 0
6693 #endif
6694
6695 static HOST_WIDE_INT
6696 get_jump_table_size (rtx insn)
6697 {
6698   /* ADDR_VECs only take room if read-only data does into the text
6699      section.  */
6700   if (JUMP_TABLES_IN_TEXT_SECTION
6701 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6702       || 1
6703 #endif
6704       )
6705     {
6706       rtx body = PATTERN (insn);
6707       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6708
6709       return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6710     }
6711
6712   return 0;
6713 }
6714
6715 /* Move a minipool fix MP from its current location to before MAX_MP.
6716    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6717    constraints may need updating.  */
6718 static Mnode *
6719 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6720                                HOST_WIDE_INT max_address)
6721 {
6722   /* This should never be true and the code below assumes these are
6723      different.  */
6724   if (mp == max_mp)
6725     abort ();
6726
6727   if (max_mp == NULL)
6728     {
6729       if (max_address < mp->max_address)
6730         mp->max_address = max_address;
6731     }
6732   else
6733     {
6734       if (max_address > max_mp->max_address - mp->fix_size)
6735         mp->max_address = max_mp->max_address - mp->fix_size;
6736       else
6737         mp->max_address = max_address;
6738
6739       /* Unlink MP from its current position.  Since max_mp is non-null,
6740        mp->prev must be non-null.  */
6741       mp->prev->next = mp->next;
6742       if (mp->next != NULL)
6743         mp->next->prev = mp->prev;
6744       else
6745         minipool_vector_tail = mp->prev;
6746
6747       /* Re-insert it before MAX_MP.  */
6748       mp->next = max_mp;
6749       mp->prev = max_mp->prev;
6750       max_mp->prev = mp;
6751
6752       if (mp->prev != NULL)
6753         mp->prev->next = mp;
6754       else
6755         minipool_vector_head = mp;
6756     }
6757
6758   /* Save the new entry.  */
6759   max_mp = mp;
6760
6761   /* Scan over the preceding entries and adjust their addresses as
6762      required.  */
6763   while (mp->prev != NULL
6764          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6765     {
6766       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6767       mp = mp->prev;
6768     }
6769
6770   return max_mp;
6771 }
6772
6773 /* Add a constant to the minipool for a forward reference.  Returns the
6774    node added or NULL if the constant will not fit in this pool.  */
6775 static Mnode *
6776 add_minipool_forward_ref (Mfix *fix)
6777 {
6778   /* If set, max_mp is the first pool_entry that has a lower
6779      constraint than the one we are trying to add.  */
6780   Mnode *       max_mp = NULL;
6781   HOST_WIDE_INT max_address = fix->address + fix->forwards;
6782   Mnode *       mp;
6783
6784   /* If this fix's address is greater than the address of the first
6785      entry, then we can't put the fix in this pool.  We subtract the
6786      size of the current fix to ensure that if the table is fully
6787      packed we still have enough room to insert this value by suffling
6788      the other fixes forwards.  */
6789   if (minipool_vector_head &&
6790       fix->address >= minipool_vector_head->max_address - fix->fix_size)
6791     return NULL;
6792
6793   /* Scan the pool to see if a constant with the same value has
6794      already been added.  While we are doing this, also note the
6795      location where we must insert the constant if it doesn't already
6796      exist.  */
6797   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6798     {
6799       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6800           && fix->mode == mp->mode
6801           && (GET_CODE (fix->value) != CODE_LABEL
6802               || (CODE_LABEL_NUMBER (fix->value)
6803                   == CODE_LABEL_NUMBER (mp->value)))
6804           && rtx_equal_p (fix->value, mp->value))
6805         {
6806           /* More than one fix references this entry.  */
6807           mp->refcount++;
6808           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6809         }
6810
6811       /* Note the insertion point if necessary.  */
6812       if (max_mp == NULL
6813           && mp->max_address > max_address)
6814         max_mp = mp;
6815
6816       /* If we are inserting an 8-bytes aligned quantity and
6817          we have not already found an insertion point, then
6818          make sure that all such 8-byte aligned quantities are
6819          placed at the start of the pool.  */
6820       if (ARM_DOUBLEWORD_ALIGN
6821           && max_mp == NULL
6822           && fix->fix_size == 8
6823           && mp->fix_size != 8)
6824         {
6825           max_mp = mp;
6826           max_address = mp->max_address;
6827         }
6828     }
6829
6830   /* The value is not currently in the minipool, so we need to create
6831      a new entry for it.  If MAX_MP is NULL, the entry will be put on
6832      the end of the list since the placement is less constrained than
6833      any existing entry.  Otherwise, we insert the new fix before
6834      MAX_MP and, if necessary, adjust the constraints on the other
6835      entries.  */
6836   mp = xmalloc (sizeof (* mp));
6837   mp->fix_size = fix->fix_size;
6838   mp->mode = fix->mode;
6839   mp->value = fix->value;
6840   mp->refcount = 1;
6841   /* Not yet required for a backwards ref.  */
6842   mp->min_address = -65536;
6843
6844   if (max_mp == NULL)
6845     {
6846       mp->max_address = max_address;
6847       mp->next = NULL;
6848       mp->prev = minipool_vector_tail;
6849
6850       if (mp->prev == NULL)
6851         {
6852           minipool_vector_head = mp;
6853           minipool_vector_label = gen_label_rtx ();
6854         }
6855       else
6856         mp->prev->next = mp;
6857
6858       minipool_vector_tail = mp;
6859     }
6860   else
6861     {
6862       if (max_address > max_mp->max_address - mp->fix_size)
6863         mp->max_address = max_mp->max_address - mp->fix_size;
6864       else
6865         mp->max_address = max_address;
6866
6867       mp->next = max_mp;
6868       mp->prev = max_mp->prev;
6869       max_mp->prev = mp;
6870       if (mp->prev != NULL)
6871         mp->prev->next = mp;
6872       else
6873         minipool_vector_head = mp;
6874     }
6875
6876   /* Save the new entry.  */
6877   max_mp = mp;
6878
6879   /* Scan over the preceding entries and adjust their addresses as
6880      required.  */
6881   while (mp->prev != NULL
6882          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6883     {
6884       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6885       mp = mp->prev;
6886     }
6887
6888   return max_mp;
6889 }
6890
6891 static Mnode *
6892 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
6893                                 HOST_WIDE_INT  min_address)
6894 {
6895   HOST_WIDE_INT offset;
6896
6897   /* This should never be true, and the code below assumes these are
6898      different.  */
6899   if (mp == min_mp)
6900     abort ();
6901
6902   if (min_mp == NULL)
6903     {
6904       if (min_address > mp->min_address)
6905         mp->min_address = min_address;
6906     }
6907   else
6908     {
6909       /* We will adjust this below if it is too loose.  */
6910       mp->min_address = min_address;
6911
6912       /* Unlink MP from its current position.  Since min_mp is non-null,
6913          mp->next must be non-null.  */
6914       mp->next->prev = mp->prev;
6915       if (mp->prev != NULL)
6916         mp->prev->next = mp->next;
6917       else
6918         minipool_vector_head = mp->next;
6919
6920       /* Reinsert it after MIN_MP.  */
6921       mp->prev = min_mp;
6922       mp->next = min_mp->next;
6923       min_mp->next = mp;
6924       if (mp->next != NULL)
6925         mp->next->prev = mp;
6926       else
6927         minipool_vector_tail = mp;
6928     }
6929
6930   min_mp = mp;
6931
6932   offset = 0;
6933   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6934     {
6935       mp->offset = offset;
6936       if (mp->refcount > 0)
6937         offset += mp->fix_size;
6938
6939       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
6940         mp->next->min_address = mp->min_address + mp->fix_size;
6941     }
6942
6943   return min_mp;
6944 }
6945
6946 /* Add a constant to the minipool for a backward reference.  Returns the
6947    node added or NULL if the constant will not fit in this pool.
6948
6949    Note that the code for insertion for a backwards reference can be
6950    somewhat confusing because the calculated offsets for each fix do
6951    not take into account the size of the pool (which is still under
6952    construction.  */
6953 static Mnode *
6954 add_minipool_backward_ref (Mfix *fix)
6955 {
6956   /* If set, min_mp is the last pool_entry that has a lower constraint
6957      than the one we are trying to add.  */
6958   Mnode *min_mp = NULL;
6959   /* This can be negative, since it is only a constraint.  */
6960   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
6961   Mnode *mp;
6962
6963   /* If we can't reach the current pool from this insn, or if we can't
6964      insert this entry at the end of the pool without pushing other
6965      fixes out of range, then we don't try.  This ensures that we
6966      can't fail later on.  */
6967   if (min_address >= minipool_barrier->address
6968       || (minipool_vector_tail->min_address + fix->fix_size
6969           >= minipool_barrier->address))
6970     return NULL;
6971
6972   /* Scan the pool to see if a constant with the same value has
6973      already been added.  While we are doing this, also note the
6974      location where we must insert the constant if it doesn't already
6975      exist.  */
6976   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
6977     {
6978       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6979           && fix->mode == mp->mode
6980           && (GET_CODE (fix->value) != CODE_LABEL
6981               || (CODE_LABEL_NUMBER (fix->value)
6982                   == CODE_LABEL_NUMBER (mp->value)))
6983           && rtx_equal_p (fix->value, mp->value)
6984           /* Check that there is enough slack to move this entry to the
6985              end of the table (this is conservative).  */
6986           && (mp->max_address
6987               > (minipool_barrier->address
6988                  + minipool_vector_tail->offset
6989                  + minipool_vector_tail->fix_size)))
6990         {
6991           mp->refcount++;
6992           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
6993         }
6994
6995       if (min_mp != NULL)
6996         mp->min_address += fix->fix_size;
6997       else
6998         {
6999           /* Note the insertion point if necessary.  */
7000           if (mp->min_address < min_address)
7001             {
7002               /* For now, we do not allow the insertion of 8-byte alignment
7003                  requiring nodes anywhere but at the start of the pool.  */
7004               if (ARM_DOUBLEWORD_ALIGN
7005                   && fix->fix_size == 8 && mp->fix_size != 8)
7006                 return NULL;
7007               else
7008                 min_mp = mp;
7009             }
7010           else if (mp->max_address
7011                    < minipool_barrier->address + mp->offset + fix->fix_size)
7012             {
7013               /* Inserting before this entry would push the fix beyond
7014                  its maximum address (which can happen if we have
7015                  re-located a forwards fix); force the new fix to come
7016                  after it.  */
7017               min_mp = mp;
7018               min_address = mp->min_address + fix->fix_size;
7019             }
7020           /* If we are inserting an 8-bytes aligned quantity and
7021              we have not already found an insertion point, then
7022              make sure that all such 8-byte aligned quantities are
7023              placed at the start of the pool.  */
7024           else if (ARM_DOUBLEWORD_ALIGN
7025                    && min_mp == NULL
7026                    && fix->fix_size == 8
7027                    && mp->fix_size < 8)
7028             {
7029               min_mp = mp;
7030               min_address = mp->min_address + fix->fix_size;
7031             }
7032         }
7033     }
7034
7035   /* We need to create a new entry.  */
7036   mp = xmalloc (sizeof (* mp));
7037   mp->fix_size = fix->fix_size;
7038   mp->mode = fix->mode;
7039   mp->value = fix->value;
7040   mp->refcount = 1;
7041   mp->max_address = minipool_barrier->address + 65536;
7042
7043   mp->min_address = min_address;
7044
7045   if (min_mp == NULL)
7046     {
7047       mp->prev = NULL;
7048       mp->next = minipool_vector_head;
7049
7050       if (mp->next == NULL)
7051         {
7052           minipool_vector_tail = mp;
7053           minipool_vector_label = gen_label_rtx ();
7054         }
7055       else
7056         mp->next->prev = mp;
7057
7058       minipool_vector_head = mp;
7059     }
7060   else
7061     {
7062       mp->next = min_mp->next;
7063       mp->prev = min_mp;
7064       min_mp->next = mp;
7065
7066       if (mp->next != NULL)
7067         mp->next->prev = mp;
7068       else
7069         minipool_vector_tail = mp;
7070     }
7071
7072   /* Save the new entry.  */
7073   min_mp = mp;
7074
7075   if (mp->prev)
7076     mp = mp->prev;
7077   else
7078     mp->offset = 0;
7079
7080   /* Scan over the following entries and adjust their offsets.  */
7081   while (mp->next != NULL)
7082     {
7083       if (mp->next->min_address < mp->min_address + mp->fix_size)
7084         mp->next->min_address = mp->min_address + mp->fix_size;
7085
7086       if (mp->refcount)
7087         mp->next->offset = mp->offset + mp->fix_size;
7088       else
7089         mp->next->offset = mp->offset;
7090
7091       mp = mp->next;
7092     }
7093
7094   return min_mp;
7095 }
7096
7097 static void
7098 assign_minipool_offsets (Mfix *barrier)
7099 {
7100   HOST_WIDE_INT offset = 0;
7101   Mnode *mp;
7102
7103   minipool_barrier = barrier;
7104
7105   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7106     {
7107       mp->offset = offset;
7108
7109       if (mp->refcount > 0)
7110         offset += mp->fix_size;
7111     }
7112 }
7113
7114 /* Output the literal table */
7115 static void
7116 dump_minipool (rtx scan)
7117 {
7118   Mnode * mp;
7119   Mnode * nmp;
7120   int align64 = 0;
7121
7122   if (ARM_DOUBLEWORD_ALIGN)
7123     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7124       if (mp->refcount > 0 && mp->fix_size == 8)
7125         {
7126           align64 = 1;
7127           break;
7128         }
7129
7130   if (dump_file)
7131     fprintf (dump_file,
7132              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7133              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7134
7135   scan = emit_label_after (gen_label_rtx (), scan);
7136   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7137   scan = emit_label_after (minipool_vector_label, scan);
7138
7139   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7140     {
7141       if (mp->refcount > 0)
7142         {
7143           if (dump_file)
7144             {
7145               fprintf (dump_file,
7146                        ";;  Offset %u, min %ld, max %ld ",
7147                        (unsigned) mp->offset, (unsigned long) mp->min_address,
7148                        (unsigned long) mp->max_address);
7149               arm_print_value (dump_file, mp->value);
7150               fputc ('\n', dump_file);
7151             }
7152
7153           switch (mp->fix_size)
7154             {
7155 #ifdef HAVE_consttable_1
7156             case 1:
7157               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7158               break;
7159
7160 #endif
7161 #ifdef HAVE_consttable_2
7162             case 2:
7163               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7164               break;
7165
7166 #endif
7167 #ifdef HAVE_consttable_4
7168             case 4:
7169               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7170               break;
7171
7172 #endif
7173 #ifdef HAVE_consttable_8
7174             case 8:
7175               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7176               break;
7177
7178 #endif
7179             default:
7180               abort ();
7181               break;
7182             }
7183         }
7184
7185       nmp = mp->next;
7186       free (mp);
7187     }
7188
7189   minipool_vector_head = minipool_vector_tail = NULL;
7190   scan = emit_insn_after (gen_consttable_end (), scan);
7191   scan = emit_barrier_after (scan);
7192 }
7193
7194 /* Return the cost of forcibly inserting a barrier after INSN.  */
7195 static int
7196 arm_barrier_cost (rtx insn)
7197 {
7198   /* Basing the location of the pool on the loop depth is preferable,
7199      but at the moment, the basic block information seems to be
7200      corrupt by this stage of the compilation.  */
7201   int base_cost = 50;
7202   rtx next = next_nonnote_insn (insn);
7203
7204   if (next != NULL && GET_CODE (next) == CODE_LABEL)
7205     base_cost -= 20;
7206
7207   switch (GET_CODE (insn))
7208     {
7209     case CODE_LABEL:
7210       /* It will always be better to place the table before the label, rather
7211          than after it.  */
7212       return 50;
7213
7214     case INSN:
7215     case CALL_INSN:
7216       return base_cost;
7217
7218     case JUMP_INSN:
7219       return base_cost - 10;
7220
7221     default:
7222       return base_cost + 10;
7223     }
7224 }
7225
7226 /* Find the best place in the insn stream in the range
7227    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7228    Create the barrier by inserting a jump and add a new fix entry for
7229    it.  */
7230 static Mfix *
7231 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7232 {
7233   HOST_WIDE_INT count = 0;
7234   rtx barrier;
7235   rtx from = fix->insn;
7236   rtx selected = from;
7237   int selected_cost;
7238   HOST_WIDE_INT selected_address;
7239   Mfix * new_fix;
7240   HOST_WIDE_INT max_count = max_address - fix->address;
7241   rtx label = gen_label_rtx ();
7242
7243   selected_cost = arm_barrier_cost (from);
7244   selected_address = fix->address;
7245
7246   while (from && count < max_count)
7247     {
7248       rtx tmp;
7249       int new_cost;
7250
7251       /* This code shouldn't have been called if there was a natural barrier
7252          within range.  */
7253       if (GET_CODE (from) == BARRIER)
7254         abort ();
7255
7256       /* Count the length of this insn.  */
7257       count += get_attr_length (from);
7258
7259       /* If there is a jump table, add its length.  */
7260       tmp = is_jump_table (from);
7261       if (tmp != NULL)
7262         {
7263           count += get_jump_table_size (tmp);
7264
7265           /* Jump tables aren't in a basic block, so base the cost on
7266              the dispatch insn.  If we select this location, we will
7267              still put the pool after the table.  */
7268           new_cost = arm_barrier_cost (from);
7269
7270           if (count < max_count && new_cost <= selected_cost)
7271             {
7272               selected = tmp;
7273               selected_cost = new_cost;
7274               selected_address = fix->address + count;
7275             }
7276
7277           /* Continue after the dispatch table.  */
7278           from = NEXT_INSN (tmp);
7279           continue;
7280         }
7281
7282       new_cost = arm_barrier_cost (from);
7283
7284       if (count < max_count && new_cost <= selected_cost)
7285         {
7286           selected = from;
7287           selected_cost = new_cost;
7288           selected_address = fix->address + count;
7289         }
7290
7291       from = NEXT_INSN (from);
7292     }
7293
7294   /* Create a new JUMP_INSN that branches around a barrier.  */
7295   from = emit_jump_insn_after (gen_jump (label), selected);
7296   JUMP_LABEL (from) = label;
7297   barrier = emit_barrier_after (from);
7298   emit_label_after (label, barrier);
7299
7300   /* Create a minipool barrier entry for the new barrier.  */
7301   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7302   new_fix->insn = barrier;
7303   new_fix->address = selected_address;
7304   new_fix->next = fix->next;
7305   fix->next = new_fix;
7306
7307   return new_fix;
7308 }
7309
7310 /* Record that there is a natural barrier in the insn stream at
7311    ADDRESS.  */
7312 static void
7313 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7314 {
7315   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7316
7317   fix->insn = insn;
7318   fix->address = address;
7319
7320   fix->next = NULL;
7321   if (minipool_fix_head != NULL)
7322     minipool_fix_tail->next = fix;
7323   else
7324     minipool_fix_head = fix;
7325
7326   minipool_fix_tail = fix;
7327 }
7328
7329 /* Record INSN, which will need fixing up to load a value from the
7330    minipool.  ADDRESS is the offset of the insn since the start of the
7331    function; LOC is a pointer to the part of the insn which requires
7332    fixing; VALUE is the constant that must be loaded, which is of type
7333    MODE.  */
7334 static void
7335 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7336                    enum machine_mode mode, rtx value)
7337 {
7338   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7339
7340 #ifdef AOF_ASSEMBLER
7341   /* PIC symbol references need to be converted into offsets into the
7342      based area.  */
7343   /* XXX This shouldn't be done here.  */
7344   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7345     value = aof_pic_entry (value);
7346 #endif /* AOF_ASSEMBLER */
7347
7348   fix->insn = insn;
7349   fix->address = address;
7350   fix->loc = loc;
7351   fix->mode = mode;
7352   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7353   fix->value = value;
7354   fix->forwards = get_attr_pool_range (insn);
7355   fix->backwards = get_attr_neg_pool_range (insn);
7356   fix->minipool = NULL;
7357
7358   /* If an insn doesn't have a range defined for it, then it isn't
7359      expecting to be reworked by this code.  Better to abort now than
7360      to generate duff assembly code.  */
7361   if (fix->forwards == 0 && fix->backwards == 0)
7362     abort ();
7363
7364   /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7365      So there might be an empty word before the start of the pool.
7366      Hence we reduce the forward range by 4 to allow for this
7367      possibility.  */
7368   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7369     fix->forwards -= 4;
7370
7371   if (dump_file)
7372     {
7373       fprintf (dump_file,
7374                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7375                GET_MODE_NAME (mode),
7376                INSN_UID (insn), (unsigned long) address,
7377                -1 * (long)fix->backwards, (long)fix->forwards);
7378       arm_print_value (dump_file, fix->value);
7379       fprintf (dump_file, "\n");
7380     }
7381
7382   /* Add it to the chain of fixes.  */
7383   fix->next = NULL;
7384
7385   if (minipool_fix_head != NULL)
7386     minipool_fix_tail->next = fix;
7387   else
7388     minipool_fix_head = fix;
7389
7390   minipool_fix_tail = fix;
7391 }
7392
7393 /* Return the cost of synthesizing a 64-bit constant VAL inline.
7394    Returns the number of insns needed, or 99 if we don't know how to
7395    do it.  */
7396 int
7397 arm_const_double_inline_cost (rtx val)
7398 {
7399   rtx lowpart, highpart;
7400   enum machine_mode mode;
7401   
7402   mode = GET_MODE (val);
7403
7404   if (mode == VOIDmode)
7405     mode = DImode;
7406
7407   gcc_assert (GET_MODE_SIZE (mode) == 8);
7408   
7409   lowpart = gen_lowpart (SImode, val);
7410   highpart = gen_highpart_mode (SImode, mode, val);
7411   
7412   gcc_assert (GET_CODE (lowpart) == CONST_INT);
7413   gcc_assert (GET_CODE (highpart) == CONST_INT);
7414
7415   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
7416                             NULL_RTX, NULL_RTX, 0, 0)
7417           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
7418                               NULL_RTX, NULL_RTX, 0, 0));
7419 }
7420
7421 /* Scan INSN and note any of its operands that need fixing.
7422    If DO_PUSHES is false we do not actually push any of the fixups
7423    needed.  The function returns TRUE if any fixups were needed/pushed.
7424    This is used by arm_memory_load_p() which needs to know about loads
7425    of constants that will be converted into minipool loads.  */
7426 static bool
7427 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7428 {
7429   bool result = false;
7430   int opno;
7431
7432   extract_insn (insn);
7433
7434   if (!constrain_operands (1))
7435     fatal_insn_not_found (insn);
7436
7437   if (recog_data.n_alternatives == 0)
7438     return false;
7439
7440   /* Fill in recog_op_alt with information about the constraints of
7441      this insn.  */
7442   preprocess_constraints ();
7443
7444   for (opno = 0; opno < recog_data.n_operands; opno++)
7445     {
7446       /* Things we need to fix can only occur in inputs.  */
7447       if (recog_data.operand_type[opno] != OP_IN)
7448         continue;
7449
7450       /* If this alternative is a memory reference, then any mention
7451          of constants in this alternative is really to fool reload
7452          into allowing us to accept one there.  We need to fix them up
7453          now so that we output the right code.  */
7454       if (recog_op_alt[opno][which_alternative].memory_ok)
7455         {
7456           rtx op = recog_data.operand[opno];
7457
7458           if (CONSTANT_P (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 == MEM)
8055         {
8056           switch (GET_CODE (XEXP (operands[1], 0)))
8057             {
8058             case REG:
8059               output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8060               break;
8061
8062             case PRE_INC:
8063               if (!TARGET_LDRD)
8064                 abort (); /* Should never happen now.  */
8065               output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8066               break;
8067
8068             case PRE_DEC:
8069               output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8070               break;
8071
8072             case POST_INC:
8073               output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8074               break;
8075
8076             case POST_DEC:
8077               if (!TARGET_LDRD)
8078                 abort (); /* Should never happen now.  */
8079               output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8080               break;
8081
8082             case PRE_MODIFY:
8083             case POST_MODIFY:
8084               otherops[0] = operands[0];
8085               otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8086               otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8087
8088               if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8089                 {
8090                   if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8091                     {
8092                       /* Registers overlap so split out the increment.  */
8093                       output_asm_insn ("add%?\t%1, %1, %2", otherops);
8094                       output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8095                     }
8096                   else
8097                     output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8098                 }
8099               else
8100                 {
8101                   /* We only allow constant increments, so this is safe.  */
8102                   output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8103                 }
8104               break;
8105
8106             case LABEL_REF:
8107             case CONST:
8108               output_asm_insn ("adr%?\t%0, %1", operands);
8109               output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8110               break;
8111
8112             default:
8113               if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8114                                    GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8115                 {
8116                   otherops[0] = operands[0];
8117                   otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8118                   otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8119
8120                   if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8121                     {
8122                       if (GET_CODE (otherops[2]) == CONST_INT)
8123                         {
8124                           switch ((int) INTVAL (otherops[2]))
8125                             {
8126                             case -8:
8127                               output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8128                               return "";
8129                             case -4:
8130                               output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8131                               return "";
8132                             case 4:
8133                               output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8134                               return "";
8135                             }
8136                         }
8137                       if (TARGET_LDRD
8138                           && (GET_CODE (otherops[2]) == REG
8139                               || (GET_CODE (otherops[2]) == CONST_INT
8140                                   && INTVAL (otherops[2]) > -256
8141                                   && INTVAL (otherops[2]) < 256)))
8142                         {
8143                           if (reg_overlap_mentioned_p (otherops[0],
8144                                                        otherops[2]))
8145                             {
8146                               /* Swap base and index registers over to
8147                                  avoid a conflict.  */
8148                               otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8149                               otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8150
8151                             }
8152                           /* If both registers conflict, it will usually
8153                              have been fixed by a splitter.  */
8154                           if (reg_overlap_mentioned_p (otherops[0],
8155                                                         otherops[2]))
8156                             {
8157                               output_asm_insn ("add%?\t%1, %1, %2", otherops);
8158                               output_asm_insn ("ldr%?d\t%0, [%1]",
8159                                                otherops);
8160                               return "";
8161                             }
8162                           else
8163                             {
8164                               output_asm_insn ("ldr%?d\t%0, [%1, %2]",
8165                                                otherops);
8166                               return "";
8167                             }
8168                         }
8169                       if (GET_CODE (otherops[2]) == CONST_INT)
8170                         {
8171                           if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8172                             output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8173                           else
8174                             output_asm_insn ("add%?\t%0, %1, %2", otherops);
8175                         }
8176                       else
8177                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
8178                     }
8179                   else
8180                     output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8181
8182                   return "ldm%?ia\t%0, %M0";
8183                 }
8184               else
8185                 {
8186                   otherops[1] = adjust_address (operands[1], SImode, 4);
8187                   /* Take care of overlapping base/data reg.  */
8188                   if (reg_mentioned_p (operands[0], operands[1]))
8189                     {
8190                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8191                       output_asm_insn ("ldr%?\t%0, %1", operands);
8192                     }
8193                   else
8194                     {
8195                       output_asm_insn ("ldr%?\t%0, %1", operands);
8196                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8197                     }
8198                 }
8199             }
8200         }
8201       else
8202         abort ();  /* Constraints should prevent this.  */
8203     }
8204   else if (code0 == MEM && code1 == REG)
8205     {
8206       if (REGNO (operands[1]) == IP_REGNUM)
8207         abort ();
8208
8209       switch (GET_CODE (XEXP (operands[0], 0)))
8210         {
8211         case REG:
8212           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8213           break;
8214
8215         case PRE_INC:
8216           if (!TARGET_LDRD)
8217             abort (); /* Should never happen now.  */
8218           output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8219           break;
8220
8221         case PRE_DEC:
8222           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8223           break;
8224
8225         case POST_INC:
8226           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8227           break;
8228
8229         case POST_DEC:
8230           if (!TARGET_LDRD)
8231             abort (); /* Should never happen now.  */
8232           output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8233           break;
8234
8235         case PRE_MODIFY:
8236         case POST_MODIFY:
8237           otherops[0] = operands[1];
8238           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8239           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8240
8241           if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8242             output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8243           else
8244             output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8245           break;
8246
8247         case PLUS:
8248           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8249           if (GET_CODE (otherops[2]) == CONST_INT)
8250             {
8251               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8252                 {
8253                 case -8:
8254                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
8255                   return "";
8256
8257                 case -4:
8258                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
8259                   return "";
8260
8261                 case 4:
8262                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8263                   return "";
8264                 }
8265             }
8266           if (TARGET_LDRD
8267               && (GET_CODE (otherops[2]) == REG
8268                   || (GET_CODE (otherops[2]) == CONST_INT
8269                       && INTVAL (otherops[2]) > -256
8270                       && INTVAL (otherops[2]) < 256)))
8271             {
8272               otherops[0] = operands[1];
8273               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8274               output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8275               return "";
8276             }
8277           /* Fall through */
8278
8279         default:
8280           otherops[0] = adjust_address (operands[0], SImode, 4);
8281           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8282           output_asm_insn ("str%?\t%1, %0", operands);
8283           output_asm_insn ("str%?\t%1, %0", otherops);
8284         }
8285     }
8286   else
8287     /* Constraints should prevent this.  */
8288     abort ();
8289
8290   return "";
8291 }
8292
8293 /* Output an ADD r, s, #n where n may be too big for one instruction.
8294    If adding zero to one register, output nothing.  */
8295 const char *
8296 output_add_immediate (rtx *operands)
8297 {
8298   HOST_WIDE_INT n = INTVAL (operands[2]);
8299
8300   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8301     {
8302       if (n < 0)
8303         output_multi_immediate (operands,
8304                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8305                                 -n);
8306       else
8307         output_multi_immediate (operands,
8308                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8309                                 n);
8310     }
8311
8312   return "";
8313 }
8314
8315 /* Output a multiple immediate operation.
8316    OPERANDS is the vector of operands referred to in the output patterns.
8317    INSTR1 is the output pattern to use for the first constant.
8318    INSTR2 is the output pattern to use for subsequent constants.
8319    IMMED_OP is the index of the constant slot in OPERANDS.
8320    N is the constant value.  */
8321 static const char *
8322 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8323                         int immed_op, HOST_WIDE_INT n)
8324 {
8325 #if HOST_BITS_PER_WIDE_INT > 32
8326   n &= 0xffffffff;
8327 #endif
8328
8329   if (n == 0)
8330     {
8331       /* Quick and easy output.  */
8332       operands[immed_op] = const0_rtx;
8333       output_asm_insn (instr1, operands);
8334     }
8335   else
8336     {
8337       int i;
8338       const char * instr = instr1;
8339
8340       /* Note that n is never zero here (which would give no output).  */
8341       for (i = 0; i < 32; i += 2)
8342         {
8343           if (n & (3 << i))
8344             {
8345               operands[immed_op] = GEN_INT (n & (255 << i));
8346               output_asm_insn (instr, operands);
8347               instr = instr2;
8348               i += 6;
8349             }
8350         }
8351     }
8352
8353   return "";
8354 }
8355
8356 /* Return the appropriate ARM instruction for the operation code.
8357    The returned result should not be overwritten.  OP is the rtx of the
8358    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8359    was shifted.  */
8360 const char *
8361 arithmetic_instr (rtx op, int shift_first_arg)
8362 {
8363   switch (GET_CODE (op))
8364     {
8365     case PLUS:
8366       return "add";
8367
8368     case MINUS:
8369       return shift_first_arg ? "rsb" : "sub";
8370
8371     case IOR:
8372       return "orr";
8373
8374     case XOR:
8375       return "eor";
8376
8377     case AND:
8378       return "and";
8379
8380     default:
8381       abort ();
8382     }
8383 }
8384
8385 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8386    for the operation code.  The returned result should not be overwritten.
8387    OP is the rtx code of the shift.
8388    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8389    shift.  */
8390 static const char *
8391 shift_op (rtx op, HOST_WIDE_INT *amountp)
8392 {
8393   const char * mnem;
8394   enum rtx_code code = GET_CODE (op);
8395
8396   if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
8397     *amountp = -1;
8398   else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
8399     *amountp = INTVAL (XEXP (op, 1));
8400   else
8401     abort ();
8402
8403   switch (code)
8404     {
8405     case ASHIFT:
8406       mnem = "asl";
8407       break;
8408
8409     case ASHIFTRT:
8410       mnem = "asr";
8411       break;
8412
8413     case LSHIFTRT:
8414       mnem = "lsr";
8415       break;
8416
8417     case ROTATE:
8418       if (*amountp == -1)
8419         abort ();
8420       *amountp = 32 - *amountp;
8421
8422       /* Fall through.  */
8423
8424     case ROTATERT:
8425       mnem = "ror";
8426       break;
8427
8428     case MULT:
8429       /* We never have to worry about the amount being other than a
8430          power of 2, since this case can never be reloaded from a reg.  */
8431       if (*amountp != -1)
8432         *amountp = int_log2 (*amountp);
8433       else
8434         abort ();
8435       return "asl";
8436
8437     default:
8438       abort ();
8439     }
8440
8441   if (*amountp != -1)
8442     {
8443       /* This is not 100% correct, but follows from the desire to merge
8444          multiplication by a power of 2 with the recognizer for a
8445          shift.  >=32 is not a valid shift for "asl", so we must try and
8446          output a shift that produces the correct arithmetical result.
8447          Using lsr #32 is identical except for the fact that the carry bit
8448          is not set correctly if we set the flags; but we never use the
8449          carry bit from such an operation, so we can ignore that.  */
8450       if (code == ROTATERT)
8451         /* Rotate is just modulo 32.  */
8452         *amountp &= 31;
8453       else if (*amountp != (*amountp & 31))
8454         {
8455           if (code == ASHIFT)
8456             mnem = "lsr";
8457           *amountp = 32;
8458         }
8459
8460       /* Shifts of 0 are no-ops.  */
8461       if (*amountp == 0)
8462         return NULL;
8463     }
8464
8465   return mnem;
8466 }
8467
8468 /* Obtain the shift from the POWER of two.  */
8469
8470 static HOST_WIDE_INT
8471 int_log2 (HOST_WIDE_INT power)
8472 {
8473   HOST_WIDE_INT shift = 0;
8474
8475   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8476     {
8477       if (shift > 31)
8478         abort ();
8479       shift++;
8480     }
8481
8482   return shift;
8483 }
8484
8485 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
8486    because /bin/as is horribly restrictive.  The judgement about
8487    whether or not each character is 'printable' (and can be output as
8488    is) or not (and must be printed with an octal escape) must be made
8489    with reference to the *host* character set -- the situation is
8490    similar to that discussed in the comments above pp_c_char in
8491    c-pretty-print.c.  */
8492
8493 #define MAX_ASCII_LEN 51
8494
8495 void
8496 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8497 {
8498   int i;
8499   int len_so_far = 0;
8500
8501   fputs ("\t.ascii\t\"", stream);
8502
8503   for (i = 0; i < len; i++)
8504     {
8505       int c = p[i];
8506
8507       if (len_so_far >= MAX_ASCII_LEN)
8508         {
8509           fputs ("\"\n\t.ascii\t\"", stream);
8510           len_so_far = 0;
8511         }
8512
8513       if (ISPRINT (c))
8514         {
8515           if (c == '\\' || c == '\"')
8516             {
8517               putc ('\\', stream);
8518               len_so_far++;
8519             }
8520           putc (c, stream);
8521           len_so_far++;
8522         }
8523       else
8524         {
8525           fprintf (stream, "\\%03o", c);
8526           len_so_far += 4;
8527         }
8528     }
8529
8530   fputs ("\"\n", stream);
8531 }
8532 \f
8533 /* Compute the register save mask for registers 0 through 12
8534    inclusive.  This code is used by arm_compute_save_reg_mask.  */
8535
8536 static unsigned long
8537 arm_compute_save_reg0_reg12_mask (void)
8538 {
8539   unsigned long func_type = arm_current_func_type ();
8540   unsigned long save_reg_mask = 0;
8541   unsigned int reg;
8542
8543   if (IS_INTERRUPT (func_type))
8544     {
8545       unsigned int max_reg;
8546       /* Interrupt functions must not corrupt any registers,
8547          even call clobbered ones.  If this is a leaf function
8548          we can just examine the registers used by the RTL, but
8549          otherwise we have to assume that whatever function is
8550          called might clobber anything, and so we have to save
8551          all the call-clobbered registers as well.  */
8552       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8553         /* FIQ handlers have registers r8 - r12 banked, so
8554            we only need to check r0 - r7, Normal ISRs only
8555            bank r14 and r15, so we must check up to r12.
8556            r13 is the stack pointer which is always preserved,
8557            so we do not need to consider it here.  */
8558         max_reg = 7;
8559       else
8560         max_reg = 12;
8561
8562       for (reg = 0; reg <= max_reg; reg++)
8563         if (regs_ever_live[reg]
8564             || (! current_function_is_leaf && call_used_regs [reg]))
8565           save_reg_mask |= (1 << reg);
8566
8567       /* Also save the pic base register if necessary.  */
8568       if (flag_pic
8569           && !TARGET_SINGLE_PIC_BASE
8570           && current_function_uses_pic_offset_table)
8571         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8572     }
8573   else
8574     {
8575       /* In the normal case we only need to save those registers
8576          which are call saved and which are used by this function.  */
8577       for (reg = 0; reg <= 10; reg++)
8578         if (regs_ever_live[reg] && ! call_used_regs [reg])
8579           save_reg_mask |= (1 << reg);
8580
8581       /* Handle the frame pointer as a special case.  */
8582       if (! TARGET_APCS_FRAME
8583           && ! frame_pointer_needed
8584           && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8585           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8586         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8587
8588       /* If we aren't loading the PIC register,
8589          don't stack it even though it may be live.  */
8590       if (flag_pic
8591           && !TARGET_SINGLE_PIC_BASE 
8592           && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
8593               || current_function_uses_pic_offset_table))
8594         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8595     }
8596
8597   /* Save registers so the exception handler can modify them.  */
8598   if (current_function_calls_eh_return)
8599     {
8600       unsigned int i;
8601
8602       for (i = 0; ; i++)
8603         {
8604           reg = EH_RETURN_DATA_REGNO (i);
8605           if (reg == INVALID_REGNUM)
8606             break;
8607           save_reg_mask |= 1 << reg;
8608         }
8609     }
8610
8611   return save_reg_mask;
8612 }
8613
8614 /* Compute a bit mask of which registers need to be
8615    saved on the stack for the current function.  */
8616
8617 static unsigned long
8618 arm_compute_save_reg_mask (void)
8619 {
8620   unsigned int save_reg_mask = 0;
8621   unsigned long func_type = arm_current_func_type ();
8622
8623   if (IS_NAKED (func_type))
8624     /* This should never really happen.  */
8625     return 0;
8626
8627   /* If we are creating a stack frame, then we must save the frame pointer,
8628      IP (which will hold the old stack pointer), LR and the PC.  */
8629   if (frame_pointer_needed)
8630     save_reg_mask |=
8631       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8632       | (1 << IP_REGNUM)
8633       | (1 << LR_REGNUM)
8634       | (1 << PC_REGNUM);
8635
8636   /* Volatile functions do not return, so there
8637      is no need to save any other registers.  */
8638   if (IS_VOLATILE (func_type))
8639     return save_reg_mask;
8640
8641   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8642
8643   /* Decide if we need to save the link register.
8644      Interrupt routines have their own banked link register,
8645      so they never need to save it.
8646      Otherwise if we do not use the link register we do not need to save
8647      it.  If we are pushing other registers onto the stack however, we
8648      can save an instruction in the epilogue by pushing the link register
8649      now and then popping it back into the PC.  This incurs extra memory
8650      accesses though, so we only do it when optimizing for size, and only
8651      if we know that we will not need a fancy return sequence.  */
8652   if (regs_ever_live [LR_REGNUM]
8653           || (save_reg_mask
8654               && optimize_size
8655               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
8656               && !current_function_calls_eh_return))
8657     save_reg_mask |= 1 << LR_REGNUM;
8658
8659   if (cfun->machine->lr_save_eliminated)
8660     save_reg_mask &= ~ (1 << LR_REGNUM);
8661
8662   if (TARGET_REALLY_IWMMXT
8663       && ((bit_count (save_reg_mask)
8664            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8665     {
8666       unsigned int reg;
8667
8668       /* The total number of registers that are going to be pushed
8669          onto the stack is odd.  We need to ensure that the stack
8670          is 64-bit aligned before we start to save iWMMXt registers,
8671          and also before we start to create locals.  (A local variable
8672          might be a double or long long which we will load/store using
8673          an iWMMXt instruction).  Therefore we need to push another
8674          ARM register, so that the stack will be 64-bit aligned.  We
8675          try to avoid using the arg registers (r0 -r3) as they might be
8676          used to pass values in a tail call.  */
8677       for (reg = 4; reg <= 12; reg++)
8678         if ((save_reg_mask & (1 << reg)) == 0)
8679           break;
8680
8681       if (reg <= 12)
8682         save_reg_mask |= (1 << reg);
8683       else
8684         {
8685           cfun->machine->sibcall_blocked = 1;
8686           save_reg_mask |= (1 << 3);
8687         }
8688     }
8689
8690   return save_reg_mask;
8691 }
8692
8693
8694 /* Compute a bit mask of which registers need to be
8695    saved on the stack for the current function.  */
8696 static unsigned long
8697 thumb_compute_save_reg_mask (void)
8698 {
8699   unsigned long mask;
8700   unsigned reg;
8701
8702   mask = 0;
8703   for (reg = 0; reg < 12; reg ++)
8704     if (regs_ever_live[reg] && !call_used_regs[reg])
8705       mask |= 1 << reg;
8706
8707   if (flag_pic && !TARGET_SINGLE_PIC_BASE)
8708     mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
8709
8710   if (TARGET_SINGLE_PIC_BASE)
8711     mask &= ~(1 << arm_pic_register);
8712
8713   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
8714   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
8715     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
8716
8717   /* LR will also be pushed if any lo regs are pushed.  */
8718   if (mask & 0xff || thumb_force_lr_save ())
8719     mask |= (1 << LR_REGNUM);
8720
8721   /* Make sure we have a low work register if we need one.
8722      We will need one if we are going to push a high register,
8723      but we are not currently intending to push a low register.  */
8724   if ((mask & 0xff) == 0
8725       && ((mask & 0x0f00) || TARGET_BACKTRACE))
8726     {
8727       /* Use thumb_find_work_register to choose which register
8728          we will use.  If the register is live then we will
8729          have to push it.  Use LAST_LO_REGNUM as our fallback
8730          choice for the register to select.  */
8731       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
8732
8733       if (! call_used_regs[reg])
8734         mask |= 1 << reg;
8735     }
8736
8737   return mask;
8738 }
8739
8740
8741 /* Return the number of bytes required to save VFP registers.  */
8742 static int
8743 arm_get_vfp_saved_size (void)
8744 {
8745   unsigned int regno;
8746   int count;
8747   int saved;
8748
8749   saved = 0;
8750   /* Space for saved VFP registers.  */
8751   if (TARGET_HARD_FLOAT && TARGET_VFP)
8752     {
8753       count = 0;
8754       for (regno = FIRST_VFP_REGNUM;
8755            regno < LAST_VFP_REGNUM;
8756            regno += 2)
8757         {
8758           if ((!regs_ever_live[regno] || call_used_regs[regno])
8759               && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8760             {
8761               if (count > 0)
8762                 {
8763                   /* Workaround ARM10 VFPr1 bug.  */
8764                   if (count == 2 && !arm_arch6)
8765                     count++;
8766                   saved += count * 8 + 4;
8767                 }
8768               count = 0;
8769             }
8770           else
8771             count++;
8772         }
8773       if (count > 0)
8774         {
8775           if (count == 2 && !arm_arch6)
8776             count++;
8777           saved += count * 8 + 4;
8778         }
8779     }
8780   return saved;
8781 }
8782
8783
8784 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
8785    everything bar the final return instruction.  */
8786 const char *
8787 output_return_instruction (rtx operand, int really_return, int reverse)
8788 {
8789   char conditional[10];
8790   char instr[100];
8791   unsigned reg;
8792   unsigned long live_regs_mask;
8793   unsigned long func_type;
8794   arm_stack_offsets *offsets;
8795
8796   func_type = arm_current_func_type ();
8797
8798   if (IS_NAKED (func_type))
8799     return "";
8800
8801   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
8802     {
8803       /* If this function was declared non-returning, and we have
8804          found a tail call, then we have to trust that the called
8805          function won't return.  */
8806       if (really_return)
8807         {
8808           rtx ops[2];
8809
8810           /* Otherwise, trap an attempted return by aborting.  */
8811           ops[0] = operand;
8812           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
8813                                        : "abort");
8814           assemble_external_libcall (ops[1]);
8815           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
8816         }
8817
8818       return "";
8819     }
8820
8821   if (current_function_calls_alloca && !really_return)
8822     abort ();
8823
8824   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
8825
8826   return_used_this_function = 1;
8827
8828   live_regs_mask = arm_compute_save_reg_mask ();
8829
8830   if (live_regs_mask)
8831     {
8832       const char * return_reg;
8833
8834       /* If we do not have any special requirements for function exit
8835          (e.g. interworking, or ISR) then we can load the return address
8836          directly into the PC.  Otherwise we must load it into LR.  */
8837       if (really_return
8838           && ! TARGET_INTERWORK)
8839         return_reg = reg_names[PC_REGNUM];
8840       else
8841         return_reg = reg_names[LR_REGNUM];
8842
8843       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
8844         {
8845           /* There are three possible reasons for the IP register
8846              being saved.  1) a stack frame was created, in which case
8847              IP contains the old stack pointer, or 2) an ISR routine
8848              corrupted it, or 3) it was saved to align the stack on
8849              iWMMXt.  In case 1, restore IP into SP, otherwise just
8850              restore IP.  */
8851           if (frame_pointer_needed)
8852             {
8853               live_regs_mask &= ~ (1 << IP_REGNUM);
8854               live_regs_mask |=   (1 << SP_REGNUM);
8855             }
8856           else
8857             {
8858               if (! IS_INTERRUPT (func_type)
8859                   && ! TARGET_REALLY_IWMMXT)
8860                 abort ();
8861             }
8862         }
8863
8864       /* On some ARM architectures it is faster to use LDR rather than
8865          LDM to load a single register.  On other architectures, the
8866          cost is the same.  In 26 bit mode, or for exception handlers,
8867          we have to use LDM to load the PC so that the CPSR is also
8868          restored.  */
8869       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
8870         if (live_regs_mask == (1U << reg))
8871           break;
8872
8873       if (reg <= LAST_ARM_REGNUM
8874           && (reg != LR_REGNUM
8875               || ! really_return
8876               || ! IS_INTERRUPT (func_type)))
8877         {
8878           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
8879                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
8880         }
8881       else
8882         {
8883           char *p;
8884           int first = 1;
8885
8886           /* Generate the load multiple instruction to restore the
8887              registers.  Note we can get here, even if
8888              frame_pointer_needed is true, but only if sp already
8889              points to the base of the saved core registers.  */
8890           if (live_regs_mask & (1 << SP_REGNUM))
8891             {
8892               unsigned HOST_WIDE_INT stack_adjust;
8893
8894               offsets = arm_get_frame_offsets ();
8895               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
8896               if (stack_adjust != 0 && stack_adjust != 4)
8897                 abort ();
8898
8899               if (stack_adjust && arm_arch5)
8900                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
8901               else
8902                 {
8903                   /* If we can't use ldmib (SA110 bug),
8904                      then try to pop r3 instead.  */
8905                   if (stack_adjust)
8906                     live_regs_mask |= 1 << 3;
8907                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
8908                 }
8909             }
8910           else
8911             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
8912
8913           p = instr + strlen (instr);
8914
8915           for (reg = 0; reg <= SP_REGNUM; reg++)
8916             if (live_regs_mask & (1 << reg))
8917               {
8918                 int l = strlen (reg_names[reg]);
8919
8920                 if (first)
8921                   first = 0;
8922                 else
8923                   {
8924                     memcpy (p, ", ", 2);
8925                     p += 2;
8926                   }
8927
8928                 memcpy (p, "%|", 2);
8929                 memcpy (p + 2, reg_names[reg], l);
8930                 p += l + 2;
8931               }
8932
8933           if (live_regs_mask & (1 << LR_REGNUM))
8934             {
8935               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
8936               /* If returning from an interrupt, restore the CPSR.  */
8937               if (IS_INTERRUPT (func_type))
8938                 strcat (p, "^");
8939             }
8940           else
8941             strcpy (p, "}");
8942         }
8943
8944       output_asm_insn (instr, & operand);
8945
8946       /* See if we need to generate an extra instruction to
8947          perform the actual function return.  */
8948       if (really_return
8949           && func_type != ARM_FT_INTERWORKED
8950           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
8951         {
8952           /* The return has already been handled
8953              by loading the LR into the PC.  */
8954           really_return = 0;
8955         }
8956     }
8957
8958   if (really_return)
8959     {
8960       switch ((int) ARM_FUNC_TYPE (func_type))
8961         {
8962         case ARM_FT_ISR:
8963         case ARM_FT_FIQ:
8964           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
8965           break;
8966
8967         case ARM_FT_INTERWORKED:
8968           sprintf (instr, "bx%s\t%%|lr", conditional);
8969           break;
8970
8971         case ARM_FT_EXCEPTION:
8972           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
8973           break;
8974
8975         default:
8976           /* Use bx if it's available.  */
8977           if (arm_arch5 || arm_arch4t)
8978             sprintf (instr, "bx%s\t%%|lr", conditional);
8979           else
8980             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
8981           break;
8982         }
8983
8984       output_asm_insn (instr, & operand);
8985     }
8986
8987   return "";
8988 }
8989
8990 /* Write the function name into the code section, directly preceding
8991    the function prologue.
8992
8993    Code will be output similar to this:
8994      t0
8995          .ascii "arm_poke_function_name", 0
8996          .align
8997      t1
8998          .word 0xff000000 + (t1 - t0)
8999      arm_poke_function_name
9000          mov     ip, sp
9001          stmfd   sp!, {fp, ip, lr, pc}
9002          sub     fp, ip, #4
9003
9004    When performing a stack backtrace, code can inspect the value
9005    of 'pc' stored at 'fp' + 0.  If the trace function then looks
9006    at location pc - 12 and the top 8 bits are set, then we know
9007    that there is a function name embedded immediately preceding this
9008    location and has length ((pc[-3]) & 0xff000000).
9009
9010    We assume that pc is declared as a pointer to an unsigned long.
9011
9012    It is of no benefit to output the function name if we are assembling
9013    a leaf function.  These function types will not contain a stack
9014    backtrace structure, therefore it is not possible to determine the
9015    function name.  */
9016 void
9017 arm_poke_function_name (FILE *stream, const char *name)
9018 {
9019   unsigned long alignlength;
9020   unsigned long length;
9021   rtx           x;
9022
9023   length      = strlen (name) + 1;
9024   alignlength = ROUND_UP_WORD (length);
9025
9026   ASM_OUTPUT_ASCII (stream, name, length);
9027   ASM_OUTPUT_ALIGN (stream, 2);
9028   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9029   assemble_aligned_integer (UNITS_PER_WORD, x);
9030 }
9031
9032 /* Place some comments into the assembler stream
9033    describing the current function.  */
9034 static void
9035 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9036 {
9037   unsigned long func_type;
9038
9039   if (!TARGET_ARM)
9040     {
9041       thumb_output_function_prologue (f, frame_size);
9042       return;
9043     }
9044
9045   /* Sanity check.  */
9046   if (arm_ccfsm_state || arm_target_insn)
9047     abort ();
9048
9049   func_type = arm_current_func_type ();
9050
9051   switch ((int) ARM_FUNC_TYPE (func_type))
9052     {
9053     default:
9054     case ARM_FT_NORMAL:
9055       break;
9056     case ARM_FT_INTERWORKED:
9057       asm_fprintf (f, "\t%@ Function supports interworking.\n");
9058       break;
9059     case ARM_FT_ISR:
9060       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9061       break;
9062     case ARM_FT_FIQ:
9063       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9064       break;
9065     case ARM_FT_EXCEPTION:
9066       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9067       break;
9068     }
9069
9070   if (IS_NAKED (func_type))
9071     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9072
9073   if (IS_VOLATILE (func_type))
9074     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9075
9076   if (IS_NESTED (func_type))
9077     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9078
9079   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9080                current_function_args_size,
9081                current_function_pretend_args_size, frame_size);
9082
9083   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9084                frame_pointer_needed,
9085                cfun->machine->uses_anonymous_args);
9086
9087   if (cfun->machine->lr_save_eliminated)
9088     asm_fprintf (f, "\t%@ link register save eliminated.\n");
9089
9090   if (current_function_calls_eh_return)
9091     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9092
9093 #ifdef AOF_ASSEMBLER
9094   if (flag_pic)
9095     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9096 #endif
9097
9098   return_used_this_function = 0;
9099 }
9100
9101 const char *
9102 arm_output_epilogue (rtx sibling)
9103 {
9104   int reg;
9105   unsigned long saved_regs_mask;
9106   unsigned long func_type;
9107   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
9108      frame that is $fp + 4 for a non-variadic function.  */
9109   int floats_offset = 0;
9110   rtx operands[3];
9111   FILE * f = asm_out_file;
9112   unsigned int lrm_count = 0;
9113   int really_return = (sibling == NULL);
9114   int start_reg;
9115   arm_stack_offsets *offsets;
9116
9117   /* If we have already generated the return instruction
9118      then it is futile to generate anything else.  */
9119   if (use_return_insn (FALSE, sibling) && return_used_this_function)
9120     return "";
9121
9122   func_type = arm_current_func_type ();
9123
9124   if (IS_NAKED (func_type))
9125     /* Naked functions don't have epilogues.  */
9126     return "";
9127
9128   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9129     {
9130       rtx op;
9131
9132       /* A volatile function should never return.  Call abort.  */
9133       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9134       assemble_external_libcall (op);
9135       output_asm_insn ("bl\t%a0", &op);
9136
9137       return "";
9138     }
9139
9140   if (current_function_calls_eh_return
9141       && ! really_return)
9142     /* If we are throwing an exception, then we really must
9143        be doing a return,  so we can't tail-call.  */
9144     abort ();
9145
9146   offsets = arm_get_frame_offsets ();
9147   saved_regs_mask = arm_compute_save_reg_mask ();
9148
9149   if (TARGET_IWMMXT)
9150     lrm_count = bit_count (saved_regs_mask);
9151
9152   floats_offset = offsets->saved_args;
9153   /* Compute how far away the floats will be.  */
9154   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9155     if (saved_regs_mask & (1 << reg))
9156       floats_offset += 4;
9157
9158   if (frame_pointer_needed)
9159     {
9160       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
9161       int vfp_offset = offsets->frame;
9162
9163       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9164         {
9165           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9166             if (regs_ever_live[reg] && !call_used_regs[reg])
9167               {
9168                 floats_offset += 12;
9169                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9170                              reg, FP_REGNUM, floats_offset - vfp_offset);
9171               }
9172         }
9173       else
9174         {
9175           start_reg = LAST_FPA_REGNUM;
9176
9177           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9178             {
9179               if (regs_ever_live[reg] && !call_used_regs[reg])
9180                 {
9181                   floats_offset += 12;
9182
9183                   /* We can't unstack more than four registers at once.  */
9184                   if (start_reg - reg == 3)
9185                     {
9186                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9187                                    reg, FP_REGNUM, floats_offset - vfp_offset);
9188                       start_reg = reg - 1;
9189                     }
9190                 }
9191               else
9192                 {
9193                   if (reg != start_reg)
9194                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9195                                  reg + 1, start_reg - reg,
9196                                  FP_REGNUM, floats_offset - vfp_offset);
9197                   start_reg = reg - 1;
9198                 }
9199             }
9200
9201           /* Just in case the last register checked also needs unstacking.  */
9202           if (reg != start_reg)
9203             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9204                          reg + 1, start_reg - reg,
9205                          FP_REGNUM, floats_offset - vfp_offset);
9206         }
9207
9208       if (TARGET_HARD_FLOAT && TARGET_VFP)
9209         {
9210           int saved_size;
9211
9212           /* The fldmx insn does not have base+offset addressing modes,
9213              so we use IP to hold the address.  */
9214           saved_size = arm_get_vfp_saved_size ();
9215
9216           if (saved_size > 0)
9217             {
9218               floats_offset += saved_size;
9219               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9220                            FP_REGNUM, floats_offset - vfp_offset);
9221             }
9222           start_reg = FIRST_VFP_REGNUM;
9223           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9224             {
9225               if ((!regs_ever_live[reg] || call_used_regs[reg])
9226                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9227                 {
9228                   if (start_reg != reg)
9229                     arm_output_fldmx (f, IP_REGNUM,
9230                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9231                                       (reg - start_reg) / 2);
9232                   start_reg = reg + 2;
9233                 }
9234             }
9235           if (start_reg != reg)
9236             arm_output_fldmx (f, IP_REGNUM,
9237                               (start_reg - FIRST_VFP_REGNUM) / 2,
9238                               (reg - start_reg) / 2);
9239         }
9240
9241       if (TARGET_IWMMXT)
9242         {
9243           /* The frame pointer is guaranteed to be non-double-word aligned.
9244              This is because it is set to (old_stack_pointer - 4) and the
9245              old_stack_pointer was double word aligned.  Thus the offset to
9246              the iWMMXt registers to be loaded must also be non-double-word
9247              sized, so that the resultant address *is* double-word aligned.
9248              We can ignore floats_offset since that was already included in
9249              the live_regs_mask.  */
9250           lrm_count += (lrm_count % 2 ? 2 : 1);
9251
9252           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9253             if (regs_ever_live[reg] && !call_used_regs[reg])
9254               {
9255                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9256                              reg, FP_REGNUM, lrm_count * 4);
9257                 lrm_count += 2;
9258               }
9259         }
9260
9261       /* saved_regs_mask should contain the IP, which at the time of stack
9262          frame generation actually contains the old stack pointer.  So a
9263          quick way to unwind the stack is just pop the IP register directly
9264          into the stack pointer.  */
9265       if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
9266         abort ();
9267       saved_regs_mask &= ~ (1 << IP_REGNUM);
9268       saved_regs_mask |=   (1 << SP_REGNUM);
9269
9270       /* There are two registers left in saved_regs_mask - LR and PC.  We
9271          only need to restore the LR register (the return address), but to
9272          save time we can load it directly into the PC, unless we need a
9273          special function exit sequence, or we are not really returning.  */
9274       if (really_return
9275           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9276           && !current_function_calls_eh_return)
9277         /* Delete the LR from the register mask, so that the LR on
9278            the stack is loaded into the PC in the register mask.  */
9279         saved_regs_mask &= ~ (1 << LR_REGNUM);
9280       else
9281         saved_regs_mask &= ~ (1 << PC_REGNUM);
9282
9283       /* We must use SP as the base register, because SP is one of the
9284          registers being restored.  If an interrupt or page fault
9285          happens in the ldm instruction, the SP might or might not
9286          have been restored.  That would be bad, as then SP will no
9287          longer indicate the safe area of stack, and we can get stack
9288          corruption.  Using SP as the base register means that it will
9289          be reset correctly to the original value, should an interrupt
9290          occur.  If the stack pointer already points at the right
9291          place, then omit the subtraction.  */
9292       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9293           || current_function_calls_alloca)
9294         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9295                      4 * bit_count (saved_regs_mask));
9296       print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9297
9298       if (IS_INTERRUPT (func_type))
9299         /* Interrupt handlers will have pushed the
9300            IP onto the stack, so restore it now.  */
9301         print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9302     }
9303   else
9304     {
9305       /* Restore stack pointer if necessary.  */
9306       if (offsets->outgoing_args != offsets->saved_regs)
9307         {
9308           operands[0] = operands[1] = stack_pointer_rtx;
9309           operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9310           output_add_immediate (operands);
9311         }
9312
9313       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9314         {
9315           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9316             if (regs_ever_live[reg] && !call_used_regs[reg])
9317               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9318                            reg, SP_REGNUM);
9319         }
9320       else
9321         {
9322           start_reg = FIRST_FPA_REGNUM;
9323
9324           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9325             {
9326               if (regs_ever_live[reg] && !call_used_regs[reg])
9327                 {
9328                   if (reg - start_reg == 3)
9329                     {
9330                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9331                                    start_reg, SP_REGNUM);
9332                       start_reg = reg + 1;
9333                     }
9334                 }
9335               else
9336                 {
9337                   if (reg != start_reg)
9338                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9339                                  start_reg, reg - start_reg,
9340                                  SP_REGNUM);
9341
9342                   start_reg = reg + 1;
9343                 }
9344             }
9345
9346           /* Just in case the last register checked also needs unstacking.  */
9347           if (reg != start_reg)
9348             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9349                          start_reg, reg - start_reg, SP_REGNUM);
9350         }
9351
9352       if (TARGET_HARD_FLOAT && TARGET_VFP)
9353         {
9354           start_reg = FIRST_VFP_REGNUM;
9355           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9356             {
9357               if ((!regs_ever_live[reg] || call_used_regs[reg])
9358                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9359                 {
9360                   if (start_reg != reg)
9361                     arm_output_fldmx (f, SP_REGNUM,
9362                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9363                                       (reg - start_reg) / 2);
9364                   start_reg = reg + 2;
9365                 }
9366             }
9367           if (start_reg != reg)
9368             arm_output_fldmx (f, SP_REGNUM,
9369                               (start_reg - FIRST_VFP_REGNUM) / 2,
9370                               (reg - start_reg) / 2);
9371         }
9372       if (TARGET_IWMMXT)
9373         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9374           if (regs_ever_live[reg] && !call_used_regs[reg])
9375             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9376
9377       /* If we can, restore the LR into the PC.  */
9378       if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9379           && really_return
9380           && current_function_pretend_args_size == 0
9381           && saved_regs_mask & (1 << LR_REGNUM)
9382           && !current_function_calls_eh_return)
9383         {
9384           saved_regs_mask &= ~ (1 << LR_REGNUM);
9385           saved_regs_mask |=   (1 << PC_REGNUM);
9386         }
9387
9388       /* Load the registers off the stack.  If we only have one register
9389          to load use the LDR instruction - it is faster.  */
9390       if (saved_regs_mask == (1 << LR_REGNUM))
9391         {
9392           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9393         }
9394       else if (saved_regs_mask)
9395         {
9396           if (saved_regs_mask & (1 << SP_REGNUM))
9397             /* Note - write back to the stack register is not enabled
9398                (i.e. "ldmfd sp!...").  We know that the stack pointer is
9399                in the list of registers and if we add writeback the
9400                instruction becomes UNPREDICTABLE.  */
9401             print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9402           else
9403             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9404         }
9405
9406       if (current_function_pretend_args_size)
9407         {
9408           /* Unwind the pre-pushed regs.  */
9409           operands[0] = operands[1] = stack_pointer_rtx;
9410           operands[2] = GEN_INT (current_function_pretend_args_size);
9411           output_add_immediate (operands);
9412         }
9413     }
9414
9415   /* We may have already restored PC directly from the stack.  */
9416   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
9417     return "";
9418
9419   /* Stack adjustment for exception handler.  */
9420   if (current_function_calls_eh_return)
9421     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
9422                  ARM_EH_STACKADJ_REGNUM);
9423
9424   /* Generate the return instruction.  */
9425   switch ((int) ARM_FUNC_TYPE (func_type))
9426     {
9427     case ARM_FT_ISR:
9428     case ARM_FT_FIQ:
9429       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9430       break;
9431
9432     case ARM_FT_EXCEPTION:
9433       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9434       break;
9435
9436     case ARM_FT_INTERWORKED:
9437       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9438       break;
9439
9440     default:
9441       if (arm_arch5 || arm_arch4t)
9442         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9443       else
9444         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9445       break;
9446     }
9447
9448   return "";
9449 }
9450
9451 static void
9452 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9453                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9454 {
9455   arm_stack_offsets *offsets;
9456
9457   if (TARGET_THUMB)
9458     {
9459       int regno;
9460
9461       /* Emit any call-via-reg trampolines that are needed for v4t support
9462          of call_reg and call_value_reg type insns.  */
9463       for (regno = 0; regno < LR_REGNUM; regno++)
9464         {
9465           rtx label = cfun->machine->call_via[regno];
9466
9467           if (label != NULL)
9468             {
9469               function_section (current_function_decl);
9470               targetm.asm_out.internal_label (asm_out_file, "L",
9471                                               CODE_LABEL_NUMBER (label));
9472               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
9473             }
9474         }
9475
9476       /* ??? Probably not safe to set this here, since it assumes that a
9477          function will be emitted as assembly immediately after we generate
9478          RTL for it.  This does not happen for inline functions.  */
9479       return_used_this_function = 0;
9480     }
9481   else
9482     {
9483       /* We need to take into account any stack-frame rounding.  */
9484       offsets = arm_get_frame_offsets ();
9485
9486       if (use_return_insn (FALSE, NULL)
9487           && return_used_this_function
9488           && offsets->saved_regs != offsets->outgoing_args
9489           && !frame_pointer_needed)
9490         abort ();
9491
9492       /* Reset the ARM-specific per-function variables.  */
9493       after_arm_reorg = 0;
9494     }
9495 }
9496
9497 /* Generate and emit an insn that we will recognize as a push_multi.
9498    Unfortunately, since this insn does not reflect very well the actual
9499    semantics of the operation, we need to annotate the insn for the benefit
9500    of DWARF2 frame unwind information.  */
9501 static rtx
9502 emit_multi_reg_push (unsigned long mask)
9503 {
9504   int num_regs = 0;
9505   int num_dwarf_regs;
9506   int i, j;
9507   rtx par;
9508   rtx dwarf;
9509   int dwarf_par_index;
9510   rtx tmp, reg;
9511
9512   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9513     if (mask & (1 << i))
9514       num_regs++;
9515
9516   if (num_regs == 0 || num_regs > 16)
9517     abort ();
9518
9519   /* We don't record the PC in the dwarf frame information.  */
9520   num_dwarf_regs = num_regs;
9521   if (mask & (1 << PC_REGNUM))
9522     num_dwarf_regs--;
9523
9524   /* For the body of the insn we are going to generate an UNSPEC in
9525      parallel with several USEs.  This allows the insn to be recognized
9526      by the push_multi pattern in the arm.md file.  The insn looks
9527      something like this:
9528
9529        (parallel [
9530            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9531                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9532            (use (reg:SI 11 fp))
9533            (use (reg:SI 12 ip))
9534            (use (reg:SI 14 lr))
9535            (use (reg:SI 15 pc))
9536         ])
9537
9538      For the frame note however, we try to be more explicit and actually
9539      show each register being stored into the stack frame, plus a (single)
9540      decrement of the stack pointer.  We do it this way in order to be
9541      friendly to the stack unwinding code, which only wants to see a single
9542      stack decrement per instruction.  The RTL we generate for the note looks
9543      something like this:
9544
9545       (sequence [
9546            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9547            (set (mem:SI (reg:SI sp)) (reg:SI r4))
9548            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9549            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9550            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9551         ])
9552
9553       This sequence is used both by the code to support stack unwinding for
9554       exceptions handlers and the code to generate dwarf2 frame debugging.  */
9555
9556   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9557   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9558   dwarf_par_index = 1;
9559
9560   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9561     {
9562       if (mask & (1 << i))
9563         {
9564           reg = gen_rtx_REG (SImode, i);
9565
9566           XVECEXP (par, 0, 0)
9567             = gen_rtx_SET (VOIDmode,
9568                            gen_rtx_MEM (BLKmode,
9569                                         gen_rtx_PRE_DEC (BLKmode,
9570                                                          stack_pointer_rtx)),
9571                            gen_rtx_UNSPEC (BLKmode,
9572                                            gen_rtvec (1, reg),
9573                                            UNSPEC_PUSH_MULT));
9574
9575           if (i != PC_REGNUM)
9576             {
9577               tmp = gen_rtx_SET (VOIDmode,
9578                                  gen_rtx_MEM (SImode, stack_pointer_rtx),
9579                                  reg);
9580               RTX_FRAME_RELATED_P (tmp) = 1;
9581               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9582               dwarf_par_index++;
9583             }
9584
9585           break;
9586         }
9587     }
9588
9589   for (j = 1, i++; j < num_regs; i++)
9590     {
9591       if (mask & (1 << i))
9592         {
9593           reg = gen_rtx_REG (SImode, i);
9594
9595           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9596
9597           if (i != PC_REGNUM)
9598             {
9599               tmp = gen_rtx_SET (VOIDmode,
9600                                  gen_rtx_MEM (SImode,
9601                                               plus_constant (stack_pointer_rtx,
9602                                                              4 * j)),
9603                                  reg);
9604               RTX_FRAME_RELATED_P (tmp) = 1;
9605               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9606             }
9607
9608           j++;
9609         }
9610     }
9611
9612   par = emit_insn (par);
9613
9614   tmp = gen_rtx_SET (SImode,
9615                      stack_pointer_rtx,
9616                      gen_rtx_PLUS (SImode,
9617                                    stack_pointer_rtx,
9618                                    GEN_INT (-4 * num_regs)));
9619   RTX_FRAME_RELATED_P (tmp) = 1;
9620   XVECEXP (dwarf, 0, 0) = tmp;
9621
9622   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9623                                        REG_NOTES (par));
9624   return par;
9625 }
9626
9627 static rtx
9628 emit_sfm (int base_reg, int count)
9629 {
9630   rtx par;
9631   rtx dwarf;
9632   rtx tmp, reg;
9633   int i;
9634
9635   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9636   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9637
9638   reg = gen_rtx_REG (XFmode, base_reg++);
9639
9640   XVECEXP (par, 0, 0)
9641     = gen_rtx_SET (VOIDmode,
9642                    gen_rtx_MEM (BLKmode,
9643                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9644                    gen_rtx_UNSPEC (BLKmode,
9645                                    gen_rtvec (1, reg),
9646                                    UNSPEC_PUSH_MULT));
9647   tmp = gen_rtx_SET (VOIDmode,
9648                      gen_rtx_MEM (XFmode, stack_pointer_rtx), reg);
9649   RTX_FRAME_RELATED_P (tmp) = 1;
9650   XVECEXP (dwarf, 0, 1) = tmp;
9651
9652   for (i = 1; i < count; i++)
9653     {
9654       reg = gen_rtx_REG (XFmode, base_reg++);
9655       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9656
9657       tmp = gen_rtx_SET (VOIDmode,
9658                          gen_rtx_MEM (XFmode,
9659                                       plus_constant (stack_pointer_rtx,
9660                                                      i * 12)),
9661                          reg);
9662       RTX_FRAME_RELATED_P (tmp) = 1;
9663       XVECEXP (dwarf, 0, i + 1) = tmp;
9664     }
9665
9666   tmp = gen_rtx_SET (VOIDmode,
9667                      stack_pointer_rtx,
9668                      gen_rtx_PLUS (SImode,
9669                                    stack_pointer_rtx,
9670                                    GEN_INT (-12 * count)));
9671   RTX_FRAME_RELATED_P (tmp) = 1;
9672   XVECEXP (dwarf, 0, 0) = tmp;
9673
9674   par = emit_insn (par);
9675   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9676                                        REG_NOTES (par));
9677   return par;
9678 }
9679
9680
9681 /* Return true if the current function needs to save/restore LR.  */
9682
9683 static bool
9684 thumb_force_lr_save (void)
9685 {
9686   return !cfun->machine->lr_save_eliminated
9687          && (!leaf_function_p ()
9688              || thumb_far_jump_used_p ()
9689              || regs_ever_live [LR_REGNUM]);
9690 }
9691
9692
9693 /* Compute the distance from register FROM to register TO.
9694    These can be the arg pointer (26), the soft frame pointer (25),
9695    the stack pointer (13) or the hard frame pointer (11).
9696    In thumb mode r7 is used as the soft frame pointer, if needed.
9697    Typical stack layout looks like this:
9698
9699        old stack pointer -> |    |
9700                              ----
9701                             |    | \
9702                             |    |   saved arguments for
9703                             |    |   vararg functions
9704                             |    | /
9705                               --
9706    hard FP & arg pointer -> |    | \
9707                             |    |   stack
9708                             |    |   frame
9709                             |    | /
9710                               --
9711                             |    | \
9712                             |    |   call saved
9713                             |    |   registers
9714       soft frame pointer -> |    | /
9715                               --
9716                             |    | \
9717                             |    |   local
9718                             |    |   variables
9719                             |    | /
9720                               --
9721                             |    | \
9722                             |    |   outgoing
9723                             |    |   arguments
9724    current stack pointer -> |    | /
9725                               --
9726
9727   For a given function some or all of these stack components
9728   may not be needed, giving rise to the possibility of
9729   eliminating some of the registers.
9730
9731   The values returned by this function must reflect the behavior
9732   of arm_expand_prologue() and arm_compute_save_reg_mask().
9733
9734   The sign of the number returned reflects the direction of stack
9735   growth, so the values are positive for all eliminations except
9736   from the soft frame pointer to the hard frame pointer.
9737
9738   SFP may point just inside the local variables block to ensure correct
9739   alignment.  */
9740
9741
9742 /* Calculate stack offsets.  These are used to calculate register elimination
9743    offsets and in prologue/epilogue code.  */
9744
9745 static arm_stack_offsets *
9746 arm_get_frame_offsets (void)
9747 {
9748   struct arm_stack_offsets *offsets;
9749   unsigned long func_type;
9750   int leaf;
9751   int saved;
9752   HOST_WIDE_INT frame_size;
9753
9754   offsets = &cfun->machine->stack_offsets;
9755
9756   /* We need to know if we are a leaf function.  Unfortunately, it
9757      is possible to be called after start_sequence has been called,
9758      which causes get_insns to return the insns for the sequence,
9759      not the function, which will cause leaf_function_p to return
9760      the incorrect result.
9761
9762      to know about leaf functions once reload has completed, and the
9763      frame size cannot be changed after that time, so we can safely
9764      use the cached value.  */
9765
9766   if (reload_completed)
9767     return offsets;
9768
9769   /* Initially this is the size of the local variables.  It will translated
9770      into an offset once we have determined the size of preceding data.  */
9771   frame_size = ROUND_UP_WORD (get_frame_size ());
9772
9773   leaf = leaf_function_p ();
9774
9775   /* Space for variadic functions.  */
9776   offsets->saved_args = current_function_pretend_args_size;
9777
9778   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
9779
9780   if (TARGET_ARM)
9781     {
9782       unsigned int regno;
9783
9784       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
9785
9786       /* We know that SP will be doubleword aligned on entry, and we must
9787          preserve that condition at any subroutine call.  We also require the
9788          soft frame pointer to be doubleword aligned.  */
9789
9790       if (TARGET_REALLY_IWMMXT)
9791         {
9792           /* Check for the call-saved iWMMXt registers.  */
9793           for (regno = FIRST_IWMMXT_REGNUM;
9794                regno <= LAST_IWMMXT_REGNUM;
9795                regno++)
9796             if (regs_ever_live [regno] && ! call_used_regs [regno])
9797               saved += 8;
9798         }
9799
9800       func_type = arm_current_func_type ();
9801       if (! IS_VOLATILE (func_type))
9802         {
9803           /* Space for saved FPA registers.  */
9804           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
9805           if (regs_ever_live[regno] && ! call_used_regs[regno])
9806             saved += 12;
9807
9808           /* Space for saved VFP registers.  */
9809           if (TARGET_HARD_FLOAT && TARGET_VFP)
9810             saved += arm_get_vfp_saved_size ();
9811         }
9812     }
9813   else /* TARGET_THUMB */
9814     {
9815       saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
9816       if (TARGET_BACKTRACE)
9817         saved += 16;
9818     }
9819
9820   /* Saved registers include the stack frame.  */
9821   offsets->saved_regs = offsets->saved_args + saved;
9822   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
9823   /* A leaf function does not need any stack alignment if it has nothing
9824      on the stack.  */
9825   if (leaf && frame_size == 0)
9826     {
9827       offsets->outgoing_args = offsets->soft_frame;
9828       return offsets;
9829     }
9830
9831   /* Ensure SFP has the correct alignment.  */
9832   if (ARM_DOUBLEWORD_ALIGN
9833       && (offsets->soft_frame & 7))
9834     offsets->soft_frame += 4;
9835
9836   offsets->outgoing_args = offsets->soft_frame + frame_size
9837                            + current_function_outgoing_args_size;
9838
9839   if (ARM_DOUBLEWORD_ALIGN)
9840     {
9841       /* Ensure SP remains doubleword aligned.  */
9842       if (offsets->outgoing_args & 7)
9843         offsets->outgoing_args += 4;
9844       if (offsets->outgoing_args & 7)
9845         abort ();
9846     }
9847
9848   return offsets;
9849 }
9850
9851
9852 /* Calculate the relative offsets for the different stack pointers.  Positive
9853    offsets are in the direction of stack growth.  */
9854
9855 HOST_WIDE_INT
9856 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
9857 {
9858   arm_stack_offsets *offsets;
9859
9860   offsets = arm_get_frame_offsets ();
9861
9862   /* OK, now we have enough information to compute the distances.
9863      There must be an entry in these switch tables for each pair
9864      of registers in ELIMINABLE_REGS, even if some of the entries
9865      seem to be redundant or useless.  */
9866   switch (from)
9867     {
9868     case ARG_POINTER_REGNUM:
9869       switch (to)
9870         {
9871         case THUMB_HARD_FRAME_POINTER_REGNUM:
9872           return 0;
9873
9874         case FRAME_POINTER_REGNUM:
9875           /* This is the reverse of the soft frame pointer
9876              to hard frame pointer elimination below.  */
9877           return offsets->soft_frame - offsets->saved_args;
9878
9879         case ARM_HARD_FRAME_POINTER_REGNUM:
9880           /* If there is no stack frame then the hard
9881              frame pointer and the arg pointer coincide.  */
9882           if (offsets->frame == offsets->saved_regs)
9883             return 0;
9884           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
9885           return (frame_pointer_needed
9886                   && cfun->static_chain_decl != NULL
9887                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
9888
9889         case STACK_POINTER_REGNUM:
9890           /* If nothing has been pushed on the stack at all
9891              then this will return -4.  This *is* correct!  */
9892           return offsets->outgoing_args - (offsets->saved_args + 4);
9893
9894         default:
9895           abort ();
9896         }
9897       break;
9898
9899     case FRAME_POINTER_REGNUM:
9900       switch (to)
9901         {
9902         case THUMB_HARD_FRAME_POINTER_REGNUM:
9903           return 0;
9904
9905         case ARM_HARD_FRAME_POINTER_REGNUM:
9906           /* The hard frame pointer points to the top entry in the
9907              stack frame.  The soft frame pointer to the bottom entry
9908              in the stack frame.  If there is no stack frame at all,
9909              then they are identical.  */
9910
9911           return offsets->frame - offsets->soft_frame;
9912
9913         case STACK_POINTER_REGNUM:
9914           return offsets->outgoing_args - offsets->soft_frame;
9915
9916         default:
9917           abort ();
9918         }
9919       break;
9920
9921     default:
9922       /* You cannot eliminate from the stack pointer.
9923          In theory you could eliminate from the hard frame
9924          pointer to the stack pointer, but this will never
9925          happen, since if a stack frame is not needed the
9926          hard frame pointer will never be used.  */
9927       abort ();
9928     }
9929 }
9930
9931
9932 /* Generate the prologue instructions for entry into an ARM function.  */
9933 void
9934 arm_expand_prologue (void)
9935 {
9936   int reg;
9937   rtx amount;
9938   rtx insn;
9939   rtx ip_rtx;
9940   unsigned long live_regs_mask;
9941   unsigned long func_type;
9942   int fp_offset = 0;
9943   int saved_pretend_args = 0;
9944   int saved_regs = 0;
9945   unsigned HOST_WIDE_INT args_to_push;
9946   arm_stack_offsets *offsets;
9947
9948   func_type = arm_current_func_type ();
9949
9950   /* Naked functions don't have prologues.  */
9951   if (IS_NAKED (func_type))
9952     return;
9953
9954   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
9955   args_to_push = current_function_pretend_args_size;
9956
9957   /* Compute which register we will have to save onto the stack.  */
9958   live_regs_mask = arm_compute_save_reg_mask ();
9959
9960   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
9961
9962   if (frame_pointer_needed)
9963     {
9964       if (IS_INTERRUPT (func_type))
9965         {
9966           /* Interrupt functions must not corrupt any registers.
9967              Creating a frame pointer however, corrupts the IP
9968              register, so we must push it first.  */
9969           insn = emit_multi_reg_push (1 << IP_REGNUM);
9970
9971           /* Do not set RTX_FRAME_RELATED_P on this insn.
9972              The dwarf stack unwinding code only wants to see one
9973              stack decrement per function, and this is not it.  If
9974              this instruction is labeled as being part of the frame
9975              creation sequence then dwarf2out_frame_debug_expr will
9976              abort when it encounters the assignment of IP to FP
9977              later on, since the use of SP here establishes SP as
9978              the CFA register and not IP.
9979
9980              Anyway this instruction is not really part of the stack
9981              frame creation although it is part of the prologue.  */
9982         }
9983       else if (IS_NESTED (func_type))
9984         {
9985           /* The Static chain register is the same as the IP register
9986              used as a scratch register during stack frame creation.
9987              To get around this need to find somewhere to store IP
9988              whilst the frame is being created.  We try the following
9989              places in order:
9990
9991                1. The last argument register.
9992                2. A slot on the stack above the frame.  (This only
9993                   works if the function is not a varargs function).
9994                3. Register r3, after pushing the argument registers
9995                   onto the stack.
9996
9997              Note - we only need to tell the dwarf2 backend about the SP
9998              adjustment in the second variant; the static chain register
9999              doesn't need to be unwound, as it doesn't contain a value
10000              inherited from the caller.  */
10001
10002           if (regs_ever_live[3] == 0)
10003             {
10004               insn = gen_rtx_REG (SImode, 3);
10005               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10006               insn = emit_insn (insn);
10007             }
10008           else if (args_to_push == 0)
10009             {
10010               rtx dwarf;
10011               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10012               insn = gen_rtx_MEM (SImode, insn);
10013               insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10014               insn = emit_insn (insn);
10015
10016               fp_offset = 4;
10017
10018               /* Just tell the dwarf backend that we adjusted SP.  */
10019               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10020                                    gen_rtx_PLUS (SImode, stack_pointer_rtx,
10021                                                  GEN_INT (-fp_offset)));
10022               RTX_FRAME_RELATED_P (insn) = 1;
10023               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10024                                                     dwarf, REG_NOTES (insn));
10025             }
10026           else
10027             {
10028               /* Store the args on the stack.  */
10029               if (cfun->machine->uses_anonymous_args)
10030                 insn = emit_multi_reg_push
10031                   ((0xf0 >> (args_to_push / 4)) & 0xf);
10032               else
10033                 insn = emit_insn
10034                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10035                                GEN_INT (- args_to_push)));
10036
10037               RTX_FRAME_RELATED_P (insn) = 1;
10038
10039               saved_pretend_args = 1;
10040               fp_offset = args_to_push;
10041               args_to_push = 0;
10042
10043               /* Now reuse r3 to preserve IP.  */
10044               insn = gen_rtx_REG (SImode, 3);
10045               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10046               (void) emit_insn (insn);
10047             }
10048         }
10049
10050       if (fp_offset)
10051         {
10052           insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10053           insn = gen_rtx_SET  (SImode, ip_rtx, insn);
10054         }
10055       else
10056         insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10057
10058       insn = emit_insn (insn);
10059       RTX_FRAME_RELATED_P (insn) = 1;
10060     }
10061
10062   if (args_to_push)
10063     {
10064       /* Push the argument registers, or reserve space for them.  */
10065       if (cfun->machine->uses_anonymous_args)
10066         insn = emit_multi_reg_push
10067           ((0xf0 >> (args_to_push / 4)) & 0xf);
10068       else
10069         insn = emit_insn
10070           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10071                        GEN_INT (- args_to_push)));
10072       RTX_FRAME_RELATED_P (insn) = 1;
10073     }
10074
10075   /* If this is an interrupt service routine, and the link register
10076      is going to be pushed, and we are not creating a stack frame,
10077      (which would involve an extra push of IP and a pop in the epilogue)
10078      subtracting four from LR now will mean that the function return
10079      can be done with a single instruction.  */
10080   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10081       && (live_regs_mask & (1 << LR_REGNUM)) != 0
10082       && ! frame_pointer_needed)
10083     emit_insn (gen_rtx_SET (SImode,
10084                             gen_rtx_REG (SImode, LR_REGNUM),
10085                             gen_rtx_PLUS (SImode,
10086                                           gen_rtx_REG (SImode, LR_REGNUM),
10087                                           GEN_INT (-4))));
10088
10089   if (live_regs_mask)
10090     {
10091       insn = emit_multi_reg_push (live_regs_mask);
10092       saved_regs += bit_count (live_regs_mask) * 4;
10093       RTX_FRAME_RELATED_P (insn) = 1;
10094     }
10095
10096   if (TARGET_IWMMXT)
10097     for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10098       if (regs_ever_live[reg] && ! call_used_regs [reg])
10099         {
10100           insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10101           insn = gen_rtx_MEM (V2SImode, insn);
10102           insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10103                                          gen_rtx_REG (V2SImode, reg)));
10104           RTX_FRAME_RELATED_P (insn) = 1;
10105           saved_regs += 8;
10106         }
10107
10108   if (! IS_VOLATILE (func_type))
10109     {
10110       int start_reg;
10111
10112       /* Save any floating point call-saved registers used by this
10113          function.  */
10114       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10115         {
10116           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10117             if (regs_ever_live[reg] && !call_used_regs[reg])
10118               {
10119                 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10120                 insn = gen_rtx_MEM (XFmode, insn);
10121                 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10122                                                gen_rtx_REG (XFmode, reg)));
10123                 RTX_FRAME_RELATED_P (insn) = 1;
10124                 saved_regs += 12;
10125               }
10126         }
10127       else
10128         {
10129           start_reg = LAST_FPA_REGNUM;
10130
10131           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10132             {
10133               if (regs_ever_live[reg] && !call_used_regs[reg])
10134                 {
10135                   if (start_reg - reg == 3)
10136                     {
10137                       insn = emit_sfm (reg, 4);
10138                       RTX_FRAME_RELATED_P (insn) = 1;
10139                       saved_regs += 48;
10140                       start_reg = reg - 1;
10141                     }
10142                 }
10143               else
10144                 {
10145                   if (start_reg != reg)
10146                     {
10147                       insn = emit_sfm (reg + 1, start_reg - reg);
10148                       RTX_FRAME_RELATED_P (insn) = 1;
10149                       saved_regs += (start_reg - reg) * 12;
10150                     }
10151                   start_reg = reg - 1;
10152                 }
10153             }
10154
10155           if (start_reg != reg)
10156             {
10157               insn = emit_sfm (reg + 1, start_reg - reg);
10158               saved_regs += (start_reg - reg) * 12;
10159               RTX_FRAME_RELATED_P (insn) = 1;
10160             }
10161         }
10162       if (TARGET_HARD_FLOAT && TARGET_VFP)
10163         {
10164           start_reg = FIRST_VFP_REGNUM;
10165
10166           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10167             {
10168               if ((!regs_ever_live[reg] || call_used_regs[reg])
10169                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10170                 {
10171                   if (start_reg != reg)
10172                     saved_regs += vfp_emit_fstmx (start_reg,
10173                                                   (reg - start_reg) / 2);
10174                   start_reg = reg + 2;
10175                 }
10176             }
10177           if (start_reg != reg)
10178             saved_regs += vfp_emit_fstmx (start_reg,
10179                                           (reg - start_reg) / 2);
10180         }
10181     }
10182
10183   if (frame_pointer_needed)
10184     {
10185       /* Create the new frame pointer.  */
10186       insn = GEN_INT (-(4 + args_to_push + fp_offset));
10187       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10188       RTX_FRAME_RELATED_P (insn) = 1;
10189
10190       if (IS_NESTED (func_type))
10191         {
10192           /* Recover the static chain register.  */
10193           if (regs_ever_live [3] == 0
10194               || saved_pretend_args)
10195             insn = gen_rtx_REG (SImode, 3);
10196           else /* if (current_function_pretend_args_size == 0) */
10197             {
10198               insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10199                                    GEN_INT (4));
10200               insn = gen_rtx_MEM (SImode, insn);
10201             }
10202
10203           emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10204           /* Add a USE to stop propagate_one_insn() from barfing.  */
10205           emit_insn (gen_prologue_use (ip_rtx));
10206         }
10207     }
10208
10209   offsets = arm_get_frame_offsets ();
10210   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10211     {
10212       /* This add can produce multiple insns for a large constant, so we
10213          need to get tricky.  */
10214       rtx last = get_last_insn ();
10215
10216       amount = GEN_INT (offsets->saved_args + saved_regs
10217                         - offsets->outgoing_args);
10218
10219       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10220                                     amount));
10221       do
10222         {
10223           last = last ? NEXT_INSN (last) : get_insns ();
10224           RTX_FRAME_RELATED_P (last) = 1;
10225         }
10226       while (last != insn);
10227
10228       /* If the frame pointer is needed, emit a special barrier that
10229          will prevent the scheduler from moving stores to the frame
10230          before the stack adjustment.  */
10231       if (frame_pointer_needed)
10232         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10233                                          hard_frame_pointer_rtx));
10234     }
10235
10236
10237   if (flag_pic)
10238     arm_load_pic_register (INVALID_REGNUM);
10239
10240   /* If we are profiling, make sure no instructions are scheduled before
10241      the call to mcount.  Similarly if the user has requested no
10242      scheduling in the prolog.  */
10243   if (current_function_profile || TARGET_NO_SCHED_PRO)
10244     emit_insn (gen_blockage ());
10245
10246   /* If the link register is being kept alive, with the return address in it,
10247      then make sure that it does not get reused by the ce2 pass.  */
10248   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10249     {
10250       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10251       cfun->machine->lr_save_eliminated = 1;
10252     }
10253 }
10254 \f
10255 /* If CODE is 'd', then the X is a condition operand and the instruction
10256    should only be executed if the condition is true.
10257    if CODE is 'D', then the X is a condition operand and the instruction
10258    should only be executed if the condition is false: however, if the mode
10259    of the comparison is CCFPEmode, then always execute the instruction -- we
10260    do this because in these circumstances !GE does not necessarily imply LT;
10261    in these cases the instruction pattern will take care to make sure that
10262    an instruction containing %d will follow, thereby undoing the effects of
10263    doing this instruction unconditionally.
10264    If CODE is 'N' then X is a floating point operand that must be negated
10265    before output.
10266    If CODE is 'B' then output a bitwise inverted value of X (a const int).
10267    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
10268 void
10269 arm_print_operand (FILE *stream, rtx x, int code)
10270 {
10271   switch (code)
10272     {
10273     case '@':
10274       fputs (ASM_COMMENT_START, stream);
10275       return;
10276
10277     case '_':
10278       fputs (user_label_prefix, stream);
10279       return;
10280
10281     case '|':
10282       fputs (REGISTER_PREFIX, stream);
10283       return;
10284
10285     case '?':
10286       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10287         {
10288           if (TARGET_THUMB)
10289             {
10290               output_operand_lossage ("predicated Thumb instruction");
10291               break;
10292             }
10293           if (current_insn_predicate != NULL)
10294             {
10295               output_operand_lossage
10296                 ("predicated instruction in conditional sequence");
10297               break;
10298             }
10299
10300           fputs (arm_condition_codes[arm_current_cc], stream);
10301         }
10302       else if (current_insn_predicate)
10303         {
10304           enum arm_cond_code code;
10305
10306           if (TARGET_THUMB)
10307             {
10308               output_operand_lossage ("predicated Thumb instruction");
10309               break;
10310             }
10311
10312           code = get_arm_condition_code (current_insn_predicate);
10313           fputs (arm_condition_codes[code], stream);
10314         }
10315       return;
10316
10317     case 'N':
10318       {
10319         REAL_VALUE_TYPE r;
10320         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10321         r = REAL_VALUE_NEGATE (r);
10322         fprintf (stream, "%s", fp_const_from_val (&r));
10323       }
10324       return;
10325
10326     case 'B':
10327       if (GET_CODE (x) == CONST_INT)
10328         {
10329           HOST_WIDE_INT val;
10330           val = ARM_SIGN_EXTEND (~INTVAL (x));
10331           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10332         }
10333       else
10334         {
10335           putc ('~', stream);
10336           output_addr_const (stream, x);
10337         }
10338       return;
10339
10340     case 'i':
10341       fprintf (stream, "%s", arithmetic_instr (x, 1));
10342       return;
10343
10344     /* Truncate Cirrus shift counts.  */
10345     case 's':
10346       if (GET_CODE (x) == CONST_INT)
10347         {
10348           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10349           return;
10350         }
10351       arm_print_operand (stream, x, 0);
10352       return;
10353
10354     case 'I':
10355       fprintf (stream, "%s", arithmetic_instr (x, 0));
10356       return;
10357
10358     case 'S':
10359       {
10360         HOST_WIDE_INT val;
10361         const char * shift = shift_op (x, &val);
10362
10363         if (shift)
10364           {
10365             fprintf (stream, ", %s ", shift_op (x, &val));
10366             if (val == -1)
10367               arm_print_operand (stream, XEXP (x, 1), 0);
10368             else
10369               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10370           }
10371       }
10372       return;
10373
10374       /* An explanation of the 'Q', 'R' and 'H' register operands:
10375
10376          In a pair of registers containing a DI or DF value the 'Q'
10377          operand returns the register number of the register containing
10378          the least significant part of the value.  The 'R' operand returns
10379          the register number of the register containing the most
10380          significant part of the value.
10381
10382          The 'H' operand returns the higher of the two register numbers.
10383          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10384          same as the 'Q' operand, since the most significant part of the
10385          value is held in the lower number register.  The reverse is true
10386          on systems where WORDS_BIG_ENDIAN is false.
10387
10388          The purpose of these operands is to distinguish between cases
10389          where the endian-ness of the values is important (for example
10390          when they are added together), and cases where the endian-ness
10391          is irrelevant, but the order of register operations is important.
10392          For example when loading a value from memory into a register
10393          pair, the endian-ness does not matter.  Provided that the value
10394          from the lower memory address is put into the lower numbered
10395          register, and the value from the higher address is put into the
10396          higher numbered register, the load will work regardless of whether
10397          the value being loaded is big-wordian or little-wordian.  The
10398          order of the two register loads can matter however, if the address
10399          of the memory location is actually held in one of the registers
10400          being overwritten by the load.  */
10401     case 'Q':
10402       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10403         {
10404           output_operand_lossage ("invalid operand for code '%c'", code);
10405           return;
10406         }
10407
10408       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10409       return;
10410
10411     case 'R':
10412       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10413         {
10414           output_operand_lossage ("invalid operand for code '%c'", code);
10415           return;
10416         }
10417
10418       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10419       return;
10420
10421     case 'H':
10422       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10423         {
10424           output_operand_lossage ("invalid operand for code '%c'", code);
10425           return;
10426         }
10427
10428       asm_fprintf (stream, "%r", REGNO (x) + 1);
10429       return;
10430
10431     case 'm':
10432       asm_fprintf (stream, "%r",
10433                    GET_CODE (XEXP (x, 0)) == REG
10434                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10435       return;
10436
10437     case 'M':
10438       asm_fprintf (stream, "{%r-%r}",
10439                    REGNO (x),
10440                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10441       return;
10442
10443     case 'd':
10444       /* CONST_TRUE_RTX means always -- that's the default.  */
10445       if (x == const_true_rtx)
10446         return;
10447
10448       if (!COMPARISON_P (x))
10449         {
10450           output_operand_lossage ("invalid operand for code '%c'", code);
10451           return;
10452         }
10453
10454       fputs (arm_condition_codes[get_arm_condition_code (x)],
10455              stream);
10456       return;
10457
10458     case 'D':
10459       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
10460          want to do that.  */
10461       if (x == const_true_rtx)
10462         {
10463           output_operand_lossage ("instruction never exectued");
10464           return;
10465         }
10466       if (!COMPARISON_P (x))
10467         {
10468           output_operand_lossage ("invalid operand for code '%c'", code);
10469           return;
10470         }
10471
10472       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10473                                  (get_arm_condition_code (x))],
10474              stream);
10475       return;
10476
10477     /* Cirrus registers can be accessed in a variety of ways:
10478          single floating point (f)
10479          double floating point (d)
10480          32bit integer         (fx)
10481          64bit integer         (dx).  */
10482     case 'W':                   /* Cirrus register in F mode.  */
10483     case 'X':                   /* Cirrus register in D mode.  */
10484     case 'Y':                   /* Cirrus register in FX mode.  */
10485     case 'Z':                   /* Cirrus register in DX mode.  */
10486       if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10487         abort ();
10488
10489       fprintf (stream, "mv%s%s",
10490                code == 'W' ? "f"
10491                : code == 'X' ? "d"
10492                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10493
10494       return;
10495
10496     /* Print cirrus register in the mode specified by the register's mode.  */
10497     case 'V':
10498       {
10499         int mode = GET_MODE (x);
10500
10501         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10502           {
10503             output_operand_lossage ("invalid operand for code '%c'", code);
10504             return;
10505           }
10506
10507         fprintf (stream, "mv%s%s",
10508                  mode == DFmode ? "d"
10509                  : mode == SImode ? "fx"
10510                  : mode == DImode ? "dx"
10511                  : "f", reg_names[REGNO (x)] + 2);
10512
10513         return;
10514       }
10515
10516     case 'U':
10517       if (GET_CODE (x) != REG
10518           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10519           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10520         /* Bad value for wCG register number.  */
10521         {
10522           output_operand_lossage ("invalid operand for code '%c'", code);
10523           return;
10524         }
10525
10526       else
10527         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10528       return;
10529
10530       /* Print an iWMMXt control register name.  */
10531     case 'w':
10532       if (GET_CODE (x) != CONST_INT
10533           || INTVAL (x) < 0
10534           || INTVAL (x) >= 16)
10535         /* Bad value for wC register number.  */
10536         {
10537           output_operand_lossage ("invalid operand for code '%c'", code);
10538           return;
10539         }
10540
10541       else
10542         {
10543           static const char * wc_reg_names [16] =
10544             {
10545               "wCID",  "wCon",  "wCSSF", "wCASF",
10546               "wC4",   "wC5",   "wC6",   "wC7",
10547               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10548               "wC12",  "wC13",  "wC14",  "wC15"
10549             };
10550
10551           fprintf (stream, wc_reg_names [INTVAL (x)]);
10552         }
10553       return;
10554
10555       /* Print a VFP double precision register name.  */
10556     case 'P':
10557       {
10558         int mode = GET_MODE (x);
10559         int num;
10560
10561         if (mode != DImode && mode != DFmode)
10562           {
10563             output_operand_lossage ("invalid operand for code '%c'", code);
10564             return;
10565           }
10566
10567         if (GET_CODE (x) != REG
10568             || !IS_VFP_REGNUM (REGNO (x)))
10569           {
10570             output_operand_lossage ("invalid operand for code '%c'", code);
10571             return;
10572           }
10573
10574         num = REGNO(x) - FIRST_VFP_REGNUM;
10575         if (num & 1)
10576           {
10577             output_operand_lossage ("invalid operand for code '%c'", code);
10578             return;
10579           }
10580
10581         fprintf (stream, "d%d", num >> 1);
10582       }
10583       return;
10584
10585     default:
10586       if (x == 0)
10587         {
10588           output_operand_lossage ("missing operand");
10589           return;
10590         }
10591
10592       if (GET_CODE (x) == REG)
10593         asm_fprintf (stream, "%r", REGNO (x));
10594       else if (GET_CODE (x) == MEM)
10595         {
10596           output_memory_reference_mode = GET_MODE (x);
10597           output_address (XEXP (x, 0));
10598         }
10599       else if (GET_CODE (x) == CONST_DOUBLE)
10600         fprintf (stream, "#%s", fp_immediate_constant (x));
10601       else if (GET_CODE (x) == NEG)
10602         abort (); /* This should never happen now.  */
10603       else
10604         {
10605           fputc ('#', stream);
10606           output_addr_const (stream, x);
10607         }
10608     }
10609 }
10610 \f
10611 #ifndef AOF_ASSEMBLER
10612 /* Target hook for assembling integer objects.  The ARM version needs to
10613    handle word-sized values specially.  */
10614 static bool
10615 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10616 {
10617   if (size == UNITS_PER_WORD && aligned_p)
10618     {
10619       fputs ("\t.word\t", asm_out_file);
10620       output_addr_const (asm_out_file, x);
10621
10622       /* Mark symbols as position independent.  We only do this in the
10623          .text segment, not in the .data segment.  */
10624       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10625           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10626         {
10627           if (GET_CODE (x) == SYMBOL_REF
10628               && (CONSTANT_POOL_ADDRESS_P (x)
10629                   || SYMBOL_REF_LOCAL_P (x)))
10630             fputs ("(GOTOFF)", asm_out_file);
10631           else if (GET_CODE (x) == LABEL_REF)
10632             fputs ("(GOTOFF)", asm_out_file);
10633           else
10634             fputs ("(GOT)", asm_out_file);
10635         }
10636       fputc ('\n', asm_out_file);
10637       return true;
10638     }
10639
10640   if (arm_vector_mode_supported_p (GET_MODE (x)))
10641     {
10642       int i, units;
10643
10644       if (GET_CODE (x) != CONST_VECTOR)
10645         abort ();
10646
10647       units = CONST_VECTOR_NUNITS (x);
10648
10649       switch (GET_MODE (x))
10650         {
10651         case V2SImode: size = 4; break;
10652         case V4HImode: size = 2; break;
10653         case V8QImode: size = 1; break;
10654         default:
10655           abort ();
10656         }
10657
10658       for (i = 0; i < units; i++)
10659         {
10660           rtx elt;
10661
10662           elt = CONST_VECTOR_ELT (x, i);
10663           assemble_integer
10664             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10665         }
10666
10667       return true;
10668     }
10669
10670   return default_assemble_integer (x, size, aligned_p);
10671 }
10672 #endif
10673 \f
10674 /* A finite state machine takes care of noticing whether or not instructions
10675    can be conditionally executed, and thus decrease execution time and code
10676    size by deleting branch instructions.  The fsm is controlled by
10677    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
10678
10679 /* The state of the fsm controlling condition codes are:
10680    0: normal, do nothing special
10681    1: make ASM_OUTPUT_OPCODE not output this instruction
10682    2: make ASM_OUTPUT_OPCODE not output this instruction
10683    3: make instructions conditional
10684    4: make instructions conditional
10685
10686    State transitions (state->state by whom under condition):
10687    0 -> 1 final_prescan_insn if the `target' is a label
10688    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10689    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10690    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10691    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10692           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10693    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10694           (the target insn is arm_target_insn).
10695
10696    If the jump clobbers the conditions then we use states 2 and 4.
10697
10698    A similar thing can be done with conditional return insns.
10699
10700    XXX In case the `target' is an unconditional branch, this conditionalising
10701    of the instructions always reduces code size, but not always execution
10702    time.  But then, I want to reduce the code size to somewhere near what
10703    /bin/cc produces.  */
10704
10705 /* Returns the index of the ARM condition code string in
10706    `arm_condition_codes'.  COMPARISON should be an rtx like
10707    `(eq (...) (...))'.  */
10708 static enum arm_cond_code
10709 get_arm_condition_code (rtx comparison)
10710 {
10711   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10712   int code;
10713   enum rtx_code comp_code = GET_CODE (comparison);
10714
10715   if (GET_MODE_CLASS (mode) != MODE_CC)
10716     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10717                            XEXP (comparison, 1));
10718
10719   switch (mode)
10720     {
10721     case CC_DNEmode: code = ARM_NE; goto dominance;
10722     case CC_DEQmode: code = ARM_EQ; goto dominance;
10723     case CC_DGEmode: code = ARM_GE; goto dominance;
10724     case CC_DGTmode: code = ARM_GT; goto dominance;
10725     case CC_DLEmode: code = ARM_LE; goto dominance;
10726     case CC_DLTmode: code = ARM_LT; goto dominance;
10727     case CC_DGEUmode: code = ARM_CS; goto dominance;
10728     case CC_DGTUmode: code = ARM_HI; goto dominance;
10729     case CC_DLEUmode: code = ARM_LS; goto dominance;
10730     case CC_DLTUmode: code = ARM_CC;
10731
10732     dominance:
10733       if (comp_code != EQ && comp_code != NE)
10734         abort ();
10735
10736       if (comp_code == EQ)
10737         return ARM_INVERSE_CONDITION_CODE (code);
10738       return code;
10739
10740     case CC_NOOVmode:
10741       switch (comp_code)
10742         {
10743         case NE: return ARM_NE;
10744         case EQ: return ARM_EQ;
10745         case GE: return ARM_PL;
10746         case LT: return ARM_MI;
10747         default: abort ();
10748         }
10749
10750     case CC_Zmode:
10751       switch (comp_code)
10752         {
10753         case NE: return ARM_NE;
10754         case EQ: return ARM_EQ;
10755         default: abort ();
10756         }
10757
10758     case CC_Nmode:
10759       switch (comp_code)
10760         {
10761         case NE: return ARM_MI;
10762         case EQ: return ARM_PL;
10763         default: abort ();
10764         }
10765
10766     case CCFPEmode:
10767     case CCFPmode:
10768       /* These encodings assume that AC=1 in the FPA system control
10769          byte.  This allows us to handle all cases except UNEQ and
10770          LTGT.  */
10771       switch (comp_code)
10772         {
10773         case GE: return ARM_GE;
10774         case GT: return ARM_GT;
10775         case LE: return ARM_LS;
10776         case LT: return ARM_MI;
10777         case NE: return ARM_NE;
10778         case EQ: return ARM_EQ;
10779         case ORDERED: return ARM_VC;
10780         case UNORDERED: return ARM_VS;
10781         case UNLT: return ARM_LT;
10782         case UNLE: return ARM_LE;
10783         case UNGT: return ARM_HI;
10784         case UNGE: return ARM_PL;
10785           /* UNEQ and LTGT do not have a representation.  */
10786         case UNEQ: /* Fall through.  */
10787         case LTGT: /* Fall through.  */
10788         default: abort ();
10789         }
10790
10791     case CC_SWPmode:
10792       switch (comp_code)
10793         {
10794         case NE: return ARM_NE;
10795         case EQ: return ARM_EQ;
10796         case GE: return ARM_LE;
10797         case GT: return ARM_LT;
10798         case LE: return ARM_GE;
10799         case LT: return ARM_GT;
10800         case GEU: return ARM_LS;
10801         case GTU: return ARM_CC;
10802         case LEU: return ARM_CS;
10803         case LTU: return ARM_HI;
10804         default: abort ();
10805         }
10806
10807     case CC_Cmode:
10808       switch (comp_code)
10809       {
10810       case LTU: return ARM_CS;
10811       case GEU: return ARM_CC;
10812       default: abort ();
10813       }
10814
10815     case CCmode:
10816       switch (comp_code)
10817         {
10818         case NE: return ARM_NE;
10819         case EQ: return ARM_EQ;
10820         case GE: return ARM_GE;
10821         case GT: return ARM_GT;
10822         case LE: return ARM_LE;
10823         case LT: return ARM_LT;
10824         case GEU: return ARM_CS;
10825         case GTU: return ARM_HI;
10826         case LEU: return ARM_LS;
10827         case LTU: return ARM_CC;
10828         default: abort ();
10829         }
10830
10831     default: abort ();
10832     }
10833
10834   abort ();
10835 }
10836
10837 void
10838 arm_final_prescan_insn (rtx insn)
10839 {
10840   /* BODY will hold the body of INSN.  */
10841   rtx body = PATTERN (insn);
10842
10843   /* This will be 1 if trying to repeat the trick, and things need to be
10844      reversed if it appears to fail.  */
10845   int reverse = 0;
10846
10847   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
10848      taken are clobbered, even if the rtl suggests otherwise.  It also
10849      means that we have to grub around within the jump expression to find
10850      out what the conditions are when the jump isn't taken.  */
10851   int jump_clobbers = 0;
10852
10853   /* If we start with a return insn, we only succeed if we find another one.  */
10854   int seeking_return = 0;
10855
10856   /* START_INSN will hold the insn from where we start looking.  This is the
10857      first insn after the following code_label if REVERSE is true.  */
10858   rtx start_insn = insn;
10859
10860   /* If in state 4, check if the target branch is reached, in order to
10861      change back to state 0.  */
10862   if (arm_ccfsm_state == 4)
10863     {
10864       if (insn == arm_target_insn)
10865         {
10866           arm_target_insn = NULL;
10867           arm_ccfsm_state = 0;
10868         }
10869       return;
10870     }
10871
10872   /* If in state 3, it is possible to repeat the trick, if this insn is an
10873      unconditional branch to a label, and immediately following this branch
10874      is the previous target label which is only used once, and the label this
10875      branch jumps to is not too far off.  */
10876   if (arm_ccfsm_state == 3)
10877     {
10878       if (simplejump_p (insn))
10879         {
10880           start_insn = next_nonnote_insn (start_insn);
10881           if (GET_CODE (start_insn) == BARRIER)
10882             {
10883               /* XXX Isn't this always a barrier?  */
10884               start_insn = next_nonnote_insn (start_insn);
10885             }
10886           if (GET_CODE (start_insn) == CODE_LABEL
10887               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
10888               && LABEL_NUSES (start_insn) == 1)
10889             reverse = TRUE;
10890           else
10891             return;
10892         }
10893       else if (GET_CODE (body) == RETURN)
10894         {
10895           start_insn = next_nonnote_insn (start_insn);
10896           if (GET_CODE (start_insn) == BARRIER)
10897             start_insn = next_nonnote_insn (start_insn);
10898           if (GET_CODE (start_insn) == CODE_LABEL
10899               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
10900               && LABEL_NUSES (start_insn) == 1)
10901             {
10902               reverse = TRUE;
10903               seeking_return = 1;
10904             }
10905           else
10906             return;
10907         }
10908       else
10909         return;
10910     }
10911
10912   if (arm_ccfsm_state != 0 && !reverse)
10913     abort ();
10914   if (GET_CODE (insn) != JUMP_INSN)
10915     return;
10916
10917   /* This jump might be paralleled with a clobber of the condition codes
10918      the jump should always come first */
10919   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
10920     body = XVECEXP (body, 0, 0);
10921
10922   if (reverse
10923       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
10924           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
10925     {
10926       int insns_skipped;
10927       int fail = FALSE, succeed = FALSE;
10928       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
10929       int then_not_else = TRUE;
10930       rtx this_insn = start_insn, label = 0;
10931
10932       /* If the jump cannot be done with one instruction, we cannot
10933          conditionally execute the instruction in the inverse case.  */
10934       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
10935         {
10936           jump_clobbers = 1;
10937           return;
10938         }
10939
10940       /* Register the insn jumped to.  */
10941       if (reverse)
10942         {
10943           if (!seeking_return)
10944             label = XEXP (SET_SRC (body), 0);
10945         }
10946       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
10947         label = XEXP (XEXP (SET_SRC (body), 1), 0);
10948       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
10949         {
10950           label = XEXP (XEXP (SET_SRC (body), 2), 0);
10951           then_not_else = FALSE;
10952         }
10953       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
10954         seeking_return = 1;
10955       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
10956         {
10957           seeking_return = 1;
10958           then_not_else = FALSE;
10959         }
10960       else
10961         abort ();
10962
10963       /* See how many insns this branch skips, and what kind of insns.  If all
10964          insns are okay, and the label or unconditional branch to the same
10965          label is not too far away, succeed.  */
10966       for (insns_skipped = 0;
10967            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
10968         {
10969           rtx scanbody;
10970
10971           this_insn = next_nonnote_insn (this_insn);
10972           if (!this_insn)
10973             break;
10974
10975           switch (GET_CODE (this_insn))
10976             {
10977             case CODE_LABEL:
10978               /* Succeed if it is the target label, otherwise fail since
10979                  control falls in from somewhere else.  */
10980               if (this_insn == label)
10981                 {
10982                   if (jump_clobbers)
10983                     {
10984                       arm_ccfsm_state = 2;
10985                       this_insn = next_nonnote_insn (this_insn);
10986                     }
10987                   else
10988                     arm_ccfsm_state = 1;
10989                   succeed = TRUE;
10990                 }
10991               else
10992                 fail = TRUE;
10993               break;
10994
10995             case BARRIER:
10996               /* Succeed if the following insn is the target label.
10997                  Otherwise fail.
10998                  If return insns are used then the last insn in a function
10999                  will be a barrier.  */
11000               this_insn = next_nonnote_insn (this_insn);
11001               if (this_insn && this_insn == label)
11002                 {
11003                   if (jump_clobbers)
11004                     {
11005                       arm_ccfsm_state = 2;
11006                       this_insn = next_nonnote_insn (this_insn);
11007                     }
11008                   else
11009                     arm_ccfsm_state = 1;
11010                   succeed = TRUE;
11011                 }
11012               else
11013                 fail = TRUE;
11014               break;
11015
11016             case CALL_INSN:
11017               /* The AAPCS says that conditional calls should not be
11018                  used since they make interworking inefficient (the
11019                  linker can't transform BL<cond> into BLX).  That's
11020                  only a problem if the machine has BLX.  */
11021               if (arm_arch5)
11022                 {
11023                   fail = TRUE;
11024                   break;
11025                 }
11026
11027               /* Succeed if the following insn is the target label, or
11028                  if the following two insns are a barrier and the
11029                  target label.  */
11030               this_insn = next_nonnote_insn (this_insn);
11031               if (this_insn && GET_CODE (this_insn) == BARRIER)
11032                 this_insn = next_nonnote_insn (this_insn);
11033
11034               if (this_insn && this_insn == label
11035                   && insns_skipped < max_insns_skipped)
11036                 {
11037                   if (jump_clobbers)
11038                     {
11039                       arm_ccfsm_state = 2;
11040                       this_insn = next_nonnote_insn (this_insn);
11041                     }
11042                   else
11043                     arm_ccfsm_state = 1;
11044                   succeed = TRUE;
11045                 }
11046               else
11047                 fail = TRUE;
11048               break;
11049
11050             case JUMP_INSN:
11051               /* If this is an unconditional branch to the same label, succeed.
11052                  If it is to another label, do nothing.  If it is conditional,
11053                  fail.  */
11054               /* XXX Probably, the tests for SET and the PC are
11055                  unnecessary.  */
11056
11057               scanbody = PATTERN (this_insn);
11058               if (GET_CODE (scanbody) == SET
11059                   && GET_CODE (SET_DEST (scanbody)) == PC)
11060                 {
11061                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11062                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11063                     {
11064                       arm_ccfsm_state = 2;
11065                       succeed = TRUE;
11066                     }
11067                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11068                     fail = TRUE;
11069                 }
11070               /* Fail if a conditional return is undesirable (e.g. on a
11071                  StrongARM), but still allow this if optimizing for size.  */
11072               else if (GET_CODE (scanbody) == RETURN
11073                        && !use_return_insn (TRUE, NULL)
11074                        && !optimize_size)
11075                 fail = TRUE;
11076               else if (GET_CODE (scanbody) == RETURN
11077                        && seeking_return)
11078                 {
11079                   arm_ccfsm_state = 2;
11080                   succeed = TRUE;
11081                 }
11082               else if (GET_CODE (scanbody) == PARALLEL)
11083                 {
11084                   switch (get_attr_conds (this_insn))
11085                     {
11086                     case CONDS_NOCOND:
11087                       break;
11088                     default:
11089                       fail = TRUE;
11090                       break;
11091                     }
11092                 }
11093               else
11094                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
11095
11096               break;
11097
11098             case INSN:
11099               /* Instructions using or affecting the condition codes make it
11100                  fail.  */
11101               scanbody = PATTERN (this_insn);
11102               if (!(GET_CODE (scanbody) == SET
11103                     || GET_CODE (scanbody) == PARALLEL)
11104                   || get_attr_conds (this_insn) != CONDS_NOCOND)
11105                 fail = TRUE;
11106
11107               /* A conditional cirrus instruction must be followed by
11108                  a non Cirrus instruction.  However, since we
11109                  conditionalize instructions in this function and by
11110                  the time we get here we can't add instructions
11111                  (nops), because shorten_branches() has already been
11112                  called, we will disable conditionalizing Cirrus
11113                  instructions to be safe.  */
11114               if (GET_CODE (scanbody) != USE
11115                   && GET_CODE (scanbody) != CLOBBER
11116                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11117                 fail = TRUE;
11118               break;
11119
11120             default:
11121               break;
11122             }
11123         }
11124       if (succeed)
11125         {
11126           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11127             arm_target_label = CODE_LABEL_NUMBER (label);
11128           else if (seeking_return || arm_ccfsm_state == 2)
11129             {
11130               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11131                 {
11132                   this_insn = next_nonnote_insn (this_insn);
11133                   if (this_insn && (GET_CODE (this_insn) == BARRIER
11134                                     || GET_CODE (this_insn) == CODE_LABEL))
11135                     abort ();
11136                 }
11137               if (!this_insn)
11138                 {
11139                   /* Oh, dear! we ran off the end.. give up.  */
11140                   recog (PATTERN (insn), insn, NULL);
11141                   arm_ccfsm_state = 0;
11142                   arm_target_insn = NULL;
11143                   return;
11144                 }
11145               arm_target_insn = this_insn;
11146             }
11147           else
11148             abort ();
11149           if (jump_clobbers)
11150             {
11151               if (reverse)
11152                 abort ();
11153               arm_current_cc =
11154                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11155                                                             0), 0), 1));
11156               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11157                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11158               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11159                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11160             }
11161           else
11162             {
11163               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11164                  what it was.  */
11165               if (!reverse)
11166                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11167                                                                0));
11168             }
11169
11170           if (reverse || then_not_else)
11171             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11172         }
11173
11174       /* Restore recog_data (getting the attributes of other insns can
11175          destroy this array, but final.c assumes that it remains intact
11176          across this call; since the insn has been recognized already we
11177          call recog direct).  */
11178       recog (PATTERN (insn), insn, NULL);
11179     }
11180 }
11181
11182 /* Returns true if REGNO is a valid register
11183    for holding a quantity of type MODE.  */
11184 int
11185 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11186 {
11187   if (GET_MODE_CLASS (mode) == MODE_CC)
11188     return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11189
11190   if (TARGET_THUMB)
11191     /* For the Thumb we only allow values bigger than SImode in
11192        registers 0 - 6, so that there is always a second low
11193        register available to hold the upper part of the value.
11194        We probably we ought to ensure that the register is the
11195        start of an even numbered register pair.  */
11196     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11197
11198   if (IS_CIRRUS_REGNUM (regno))
11199     /* We have outlawed SI values in Cirrus registers because they
11200        reside in the lower 32 bits, but SF values reside in the
11201        upper 32 bits.  This causes gcc all sorts of grief.  We can't
11202        even split the registers into pairs because Cirrus SI values
11203        get sign extended to 64bits-- aldyh.  */
11204     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11205
11206   if (IS_VFP_REGNUM (regno))
11207     {
11208       if (mode == SFmode || mode == SImode)
11209         return TRUE;
11210
11211       /* DFmode values are only valid in even register pairs.  */
11212       if (mode == DFmode)
11213         return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11214       return FALSE;
11215     }
11216
11217   if (IS_IWMMXT_GR_REGNUM (regno))
11218     return mode == SImode;
11219
11220   if (IS_IWMMXT_REGNUM (regno))
11221     return VALID_IWMMXT_REG_MODE (mode);
11222
11223   /* We allow any value to be stored in the general registers.
11224      Restrict doubleword quantities to even register pairs so that we can
11225      use ldrd.  */
11226   if (regno <= LAST_ARM_REGNUM)
11227     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11228
11229   if (   regno == FRAME_POINTER_REGNUM
11230       || regno == ARG_POINTER_REGNUM)
11231     /* We only allow integers in the fake hard registers.  */
11232     return GET_MODE_CLASS (mode) == MODE_INT;
11233
11234   /* The only registers left are the FPA registers
11235      which we only allow to hold FP values.  */
11236   return GET_MODE_CLASS (mode) == MODE_FLOAT
11237     && regno >= FIRST_FPA_REGNUM
11238     && regno <= LAST_FPA_REGNUM;
11239 }
11240
11241 int
11242 arm_regno_class (int regno)
11243 {
11244   if (TARGET_THUMB)
11245     {
11246       if (regno == STACK_POINTER_REGNUM)
11247         return STACK_REG;
11248       if (regno == CC_REGNUM)
11249         return CC_REG;
11250       if (regno < 8)
11251         return LO_REGS;
11252       return HI_REGS;
11253     }
11254
11255   if (   regno <= LAST_ARM_REGNUM
11256       || regno == FRAME_POINTER_REGNUM
11257       || regno == ARG_POINTER_REGNUM)
11258     return GENERAL_REGS;
11259
11260   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11261     return NO_REGS;
11262
11263   if (IS_CIRRUS_REGNUM (regno))
11264     return CIRRUS_REGS;
11265
11266   if (IS_VFP_REGNUM (regno))
11267     return VFP_REGS;
11268
11269   if (IS_IWMMXT_REGNUM (regno))
11270     return IWMMXT_REGS;
11271
11272   if (IS_IWMMXT_GR_REGNUM (regno))
11273     return IWMMXT_GR_REGS;
11274
11275   return FPA_REGS;
11276 }
11277
11278 /* Handle a special case when computing the offset
11279    of an argument from the frame pointer.  */
11280 int
11281 arm_debugger_arg_offset (int value, rtx addr)
11282 {
11283   rtx insn;
11284
11285   /* We are only interested if dbxout_parms() failed to compute the offset.  */
11286   if (value != 0)
11287     return 0;
11288
11289   /* We can only cope with the case where the address is held in a register.  */
11290   if (GET_CODE (addr) != REG)
11291     return 0;
11292
11293   /* If we are using the frame pointer to point at the argument, then
11294      an offset of 0 is correct.  */
11295   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11296     return 0;
11297
11298   /* If we are using the stack pointer to point at the
11299      argument, then an offset of 0 is correct.  */
11300   if ((TARGET_THUMB || !frame_pointer_needed)
11301       && REGNO (addr) == SP_REGNUM)
11302     return 0;
11303
11304   /* Oh dear.  The argument is pointed to by a register rather
11305      than being held in a register, or being stored at a known
11306      offset from the frame pointer.  Since GDB only understands
11307      those two kinds of argument we must translate the address
11308      held in the register into an offset from the frame pointer.
11309      We do this by searching through the insns for the function
11310      looking to see where this register gets its value.  If the
11311      register is initialized from the frame pointer plus an offset
11312      then we are in luck and we can continue, otherwise we give up.
11313
11314      This code is exercised by producing debugging information
11315      for a function with arguments like this:
11316
11317            double func (double a, double b, int c, double d) {return d;}
11318
11319      Without this code the stab for parameter 'd' will be set to
11320      an offset of 0 from the frame pointer, rather than 8.  */
11321
11322   /* The if() statement says:
11323
11324      If the insn is a normal instruction
11325      and if the insn is setting the value in a register
11326      and if the register being set is the register holding the address of the argument
11327      and if the address is computing by an addition
11328      that involves adding to a register
11329      which is the frame pointer
11330      a constant integer
11331
11332      then...  */
11333
11334   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11335     {
11336       if (   GET_CODE (insn) == INSN
11337           && GET_CODE (PATTERN (insn)) == SET
11338           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11339           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11340           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11341           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11342           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11343              )
11344         {
11345           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11346
11347           break;
11348         }
11349     }
11350
11351   if (value == 0)
11352     {
11353       debug_rtx (addr);
11354       warning ("unable to compute real location of stacked parameter");
11355       value = 8; /* XXX magic hack */
11356     }
11357
11358   return value;
11359 }
11360 \f
11361 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
11362   do                                                                    \
11363     {                                                                   \
11364       if ((MASK) & insn_flags)                                          \
11365         lang_hooks.builtin_function ((NAME), (TYPE), (CODE),            \
11366                                      BUILT_IN_MD, NULL, NULL_TREE);     \
11367     }                                                                   \
11368   while (0)
11369
11370 struct builtin_description
11371 {
11372   const unsigned int       mask;
11373   const enum insn_code     icode;
11374   const char * const       name;
11375   const enum arm_builtins  code;
11376   const enum rtx_code      comparison;
11377   const unsigned int       flag;
11378 };
11379
11380 static const struct builtin_description bdesc_2arg[] =
11381 {
11382 #define IWMMXT_BUILTIN(code, string, builtin) \
11383   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11384     ARM_BUILTIN_##builtin, 0, 0 },
11385
11386   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11387   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11388   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11389   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11390   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11391   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11392   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11393   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11394   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11395   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11396   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11397   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11398   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11399   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11400   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11401   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11402   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11403   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11404   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11405   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11406   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11407   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11408   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11409   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11410   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11411   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11412   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11413   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11414   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11415   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11416   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11417   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11418   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11419   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11420   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11421   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11422   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11423   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11424   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11425   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11426   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11427   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11428   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11429   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11430   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11431   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11432   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11433   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11434   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11435   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11436   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11437   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11438   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11439   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11440   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11441   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11442   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11443   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11444
11445 #define IWMMXT_BUILTIN2(code, builtin) \
11446   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11447
11448   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11449   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11450   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11451   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11452   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11453   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11454   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
11455   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
11456   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
11457   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
11458   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
11459   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
11460   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
11461   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
11462   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
11463   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
11464   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
11465   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
11466   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
11467   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
11468   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
11469   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
11470   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
11471   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
11472   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
11473   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
11474   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
11475   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
11476   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
11477   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
11478   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
11479   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
11480 };
11481
11482 static const struct builtin_description bdesc_1arg[] =
11483 {
11484   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11485   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11486   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11487   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11488   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11489   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11490   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11491   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11492   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11493   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11494   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11495   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11496   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11497   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11498   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11499   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11500   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11501   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11502 };
11503
11504 /* Set up all the iWMMXt builtins.  This is
11505    not called if TARGET_IWMMXT is zero.  */
11506
11507 static void
11508 arm_init_iwmmxt_builtins (void)
11509 {
11510   const struct builtin_description * d;
11511   size_t i;
11512   tree endlink = void_list_node;
11513
11514   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11515   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11516   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11517
11518   tree int_ftype_int
11519     = build_function_type (integer_type_node,
11520                            tree_cons (NULL_TREE, integer_type_node, endlink));
11521   tree v8qi_ftype_v8qi_v8qi_int
11522     = build_function_type (V8QI_type_node,
11523                            tree_cons (NULL_TREE, V8QI_type_node,
11524                                       tree_cons (NULL_TREE, V8QI_type_node,
11525                                                  tree_cons (NULL_TREE,
11526                                                             integer_type_node,
11527                                                             endlink))));
11528   tree v4hi_ftype_v4hi_int
11529     = build_function_type (V4HI_type_node,
11530                            tree_cons (NULL_TREE, V4HI_type_node,
11531                                       tree_cons (NULL_TREE, integer_type_node,
11532                                                  endlink)));
11533   tree v2si_ftype_v2si_int
11534     = build_function_type (V2SI_type_node,
11535                            tree_cons (NULL_TREE, V2SI_type_node,
11536                                       tree_cons (NULL_TREE, integer_type_node,
11537                                                  endlink)));
11538   tree v2si_ftype_di_di
11539     = build_function_type (V2SI_type_node,
11540                            tree_cons (NULL_TREE, long_long_integer_type_node,
11541                                       tree_cons (NULL_TREE, long_long_integer_type_node,
11542                                                  endlink)));
11543   tree di_ftype_di_int
11544     = build_function_type (long_long_integer_type_node,
11545                            tree_cons (NULL_TREE, long_long_integer_type_node,
11546                                       tree_cons (NULL_TREE, integer_type_node,
11547                                                  endlink)));
11548   tree di_ftype_di_int_int
11549     = build_function_type (long_long_integer_type_node,
11550                            tree_cons (NULL_TREE, long_long_integer_type_node,
11551                                       tree_cons (NULL_TREE, integer_type_node,
11552                                                  tree_cons (NULL_TREE,
11553                                                             integer_type_node,
11554                                                             endlink))));
11555   tree int_ftype_v8qi
11556     = build_function_type (integer_type_node,
11557                            tree_cons (NULL_TREE, V8QI_type_node,
11558                                       endlink));
11559   tree int_ftype_v4hi
11560     = build_function_type (integer_type_node,
11561                            tree_cons (NULL_TREE, V4HI_type_node,
11562                                       endlink));
11563   tree int_ftype_v2si
11564     = build_function_type (integer_type_node,
11565                            tree_cons (NULL_TREE, V2SI_type_node,
11566                                       endlink));
11567   tree int_ftype_v8qi_int
11568     = build_function_type (integer_type_node,
11569                            tree_cons (NULL_TREE, V8QI_type_node,
11570                                       tree_cons (NULL_TREE, integer_type_node,
11571                                                  endlink)));
11572   tree int_ftype_v4hi_int
11573     = build_function_type (integer_type_node,
11574                            tree_cons (NULL_TREE, V4HI_type_node,
11575                                       tree_cons (NULL_TREE, integer_type_node,
11576                                                  endlink)));
11577   tree int_ftype_v2si_int
11578     = build_function_type (integer_type_node,
11579                            tree_cons (NULL_TREE, V2SI_type_node,
11580                                       tree_cons (NULL_TREE, integer_type_node,
11581                                                  endlink)));
11582   tree v8qi_ftype_v8qi_int_int
11583     = build_function_type (V8QI_type_node,
11584                            tree_cons (NULL_TREE, V8QI_type_node,
11585                                       tree_cons (NULL_TREE, integer_type_node,
11586                                                  tree_cons (NULL_TREE,
11587                                                             integer_type_node,
11588                                                             endlink))));
11589   tree v4hi_ftype_v4hi_int_int
11590     = build_function_type (V4HI_type_node,
11591                            tree_cons (NULL_TREE, V4HI_type_node,
11592                                       tree_cons (NULL_TREE, integer_type_node,
11593                                                  tree_cons (NULL_TREE,
11594                                                             integer_type_node,
11595                                                             endlink))));
11596   tree v2si_ftype_v2si_int_int
11597     = build_function_type (V2SI_type_node,
11598                            tree_cons (NULL_TREE, V2SI_type_node,
11599                                       tree_cons (NULL_TREE, integer_type_node,
11600                                                  tree_cons (NULL_TREE,
11601                                                             integer_type_node,
11602                                                             endlink))));
11603   /* Miscellaneous.  */
11604   tree v8qi_ftype_v4hi_v4hi
11605     = build_function_type (V8QI_type_node,
11606                            tree_cons (NULL_TREE, V4HI_type_node,
11607                                       tree_cons (NULL_TREE, V4HI_type_node,
11608                                                  endlink)));
11609   tree v4hi_ftype_v2si_v2si
11610     = build_function_type (V4HI_type_node,
11611                            tree_cons (NULL_TREE, V2SI_type_node,
11612                                       tree_cons (NULL_TREE, V2SI_type_node,
11613                                                  endlink)));
11614   tree v2si_ftype_v4hi_v4hi
11615     = build_function_type (V2SI_type_node,
11616                            tree_cons (NULL_TREE, V4HI_type_node,
11617                                       tree_cons (NULL_TREE, V4HI_type_node,
11618                                                  endlink)));
11619   tree v2si_ftype_v8qi_v8qi
11620     = build_function_type (V2SI_type_node,
11621                            tree_cons (NULL_TREE, V8QI_type_node,
11622                                       tree_cons (NULL_TREE, V8QI_type_node,
11623                                                  endlink)));
11624   tree v4hi_ftype_v4hi_di
11625     = build_function_type (V4HI_type_node,
11626                            tree_cons (NULL_TREE, V4HI_type_node,
11627                                       tree_cons (NULL_TREE,
11628                                                  long_long_integer_type_node,
11629                                                  endlink)));
11630   tree v2si_ftype_v2si_di
11631     = build_function_type (V2SI_type_node,
11632                            tree_cons (NULL_TREE, V2SI_type_node,
11633                                       tree_cons (NULL_TREE,
11634                                                  long_long_integer_type_node,
11635                                                  endlink)));
11636   tree void_ftype_int_int
11637     = build_function_type (void_type_node,
11638                            tree_cons (NULL_TREE, integer_type_node,
11639                                       tree_cons (NULL_TREE, integer_type_node,
11640                                                  endlink)));
11641   tree di_ftype_void
11642     = build_function_type (long_long_unsigned_type_node, endlink);
11643   tree di_ftype_v8qi
11644     = build_function_type (long_long_integer_type_node,
11645                            tree_cons (NULL_TREE, V8QI_type_node,
11646                                       endlink));
11647   tree di_ftype_v4hi
11648     = build_function_type (long_long_integer_type_node,
11649                            tree_cons (NULL_TREE, V4HI_type_node,
11650                                       endlink));
11651   tree di_ftype_v2si
11652     = build_function_type (long_long_integer_type_node,
11653                            tree_cons (NULL_TREE, V2SI_type_node,
11654                                       endlink));
11655   tree v2si_ftype_v4hi
11656     = build_function_type (V2SI_type_node,
11657                            tree_cons (NULL_TREE, V4HI_type_node,
11658                                       endlink));
11659   tree v4hi_ftype_v8qi
11660     = build_function_type (V4HI_type_node,
11661                            tree_cons (NULL_TREE, V8QI_type_node,
11662                                       endlink));
11663
11664   tree di_ftype_di_v4hi_v4hi
11665     = build_function_type (long_long_unsigned_type_node,
11666                            tree_cons (NULL_TREE,
11667                                       long_long_unsigned_type_node,
11668                                       tree_cons (NULL_TREE, V4HI_type_node,
11669                                                  tree_cons (NULL_TREE,
11670                                                             V4HI_type_node,
11671                                                             endlink))));
11672
11673   tree di_ftype_v4hi_v4hi
11674     = build_function_type (long_long_unsigned_type_node,
11675                            tree_cons (NULL_TREE, V4HI_type_node,
11676                                       tree_cons (NULL_TREE, V4HI_type_node,
11677                                                  endlink)));
11678
11679   /* Normal vector binops.  */
11680   tree v8qi_ftype_v8qi_v8qi
11681     = build_function_type (V8QI_type_node,
11682                            tree_cons (NULL_TREE, V8QI_type_node,
11683                                       tree_cons (NULL_TREE, V8QI_type_node,
11684                                                  endlink)));
11685   tree v4hi_ftype_v4hi_v4hi
11686     = build_function_type (V4HI_type_node,
11687                            tree_cons (NULL_TREE, V4HI_type_node,
11688                                       tree_cons (NULL_TREE, V4HI_type_node,
11689                                                  endlink)));
11690   tree v2si_ftype_v2si_v2si
11691     = build_function_type (V2SI_type_node,
11692                            tree_cons (NULL_TREE, V2SI_type_node,
11693                                       tree_cons (NULL_TREE, V2SI_type_node,
11694                                                  endlink)));
11695   tree di_ftype_di_di
11696     = build_function_type (long_long_unsigned_type_node,
11697                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
11698                                       tree_cons (NULL_TREE,
11699                                                  long_long_unsigned_type_node,
11700                                                  endlink)));
11701
11702   /* Add all builtins that are more or less simple operations on two
11703      operands.  */
11704   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11705     {
11706       /* Use one of the operands; the target can have a different mode for
11707          mask-generating compares.  */
11708       enum machine_mode mode;
11709       tree type;
11710
11711       if (d->name == 0)
11712         continue;
11713
11714       mode = insn_data[d->icode].operand[1].mode;
11715
11716       switch (mode)
11717         {
11718         case V8QImode:
11719           type = v8qi_ftype_v8qi_v8qi;
11720           break;
11721         case V4HImode:
11722           type = v4hi_ftype_v4hi_v4hi;
11723           break;
11724         case V2SImode:
11725           type = v2si_ftype_v2si_v2si;
11726           break;
11727         case DImode:
11728           type = di_ftype_di_di;
11729           break;
11730
11731         default:
11732           abort ();
11733         }
11734
11735       def_mbuiltin (d->mask, d->name, type, d->code);
11736     }
11737
11738   /* Add the remaining MMX insns with somewhat more complicated types.  */
11739   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11740   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11741   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11742
11743   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11744   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11745   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11746   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11747   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11748   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11749
11750   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11751   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11752   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11753   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11754   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11755   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11756
11757   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11758   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11759   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11760   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11761   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11762   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11763
11764   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11765   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11766   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11767   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11768   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11769   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11770
11771   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11772
11773   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11774   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11775   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11776   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11777
11778   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11779   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11780   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11781   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11782   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11783   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11784   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11785   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11786   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11787
11788   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11789   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11790   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11791
11792   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11793   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11794   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
11795
11796   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
11797   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
11798   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
11799   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
11800   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
11801   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
11802
11803   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
11804   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
11805   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
11806   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
11807   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
11808   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
11809   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
11810   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
11811   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
11812   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
11813   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
11814   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
11815
11816   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
11817   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
11818   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
11819   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
11820
11821   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
11822   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
11823   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
11824   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
11825   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
11826   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
11827   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
11828 }
11829
11830 static void
11831 arm_init_builtins (void)
11832 {
11833   if (TARGET_REALLY_IWMMXT)
11834     arm_init_iwmmxt_builtins ();
11835 }
11836
11837 /* Errors in the source file can cause expand_expr to return const0_rtx
11838    where we expect a vector.  To avoid crashing, use one of the vector
11839    clear instructions.  */
11840
11841 static rtx
11842 safe_vector_operand (rtx x, enum machine_mode mode)
11843 {
11844   if (x != const0_rtx)
11845     return x;
11846   x = gen_reg_rtx (mode);
11847
11848   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
11849                                : gen_rtx_SUBREG (DImode, x, 0)));
11850   return x;
11851 }
11852
11853 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
11854
11855 static rtx
11856 arm_expand_binop_builtin (enum insn_code icode,
11857                           tree arglist, rtx target)
11858 {
11859   rtx pat;
11860   tree arg0 = TREE_VALUE (arglist);
11861   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11862   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11863   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11864   enum machine_mode tmode = insn_data[icode].operand[0].mode;
11865   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11866   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11867
11868   if (VECTOR_MODE_P (mode0))
11869     op0 = safe_vector_operand (op0, mode0);
11870   if (VECTOR_MODE_P (mode1))
11871     op1 = safe_vector_operand (op1, mode1);
11872
11873   if (! target
11874       || GET_MODE (target) != tmode
11875       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11876     target = gen_reg_rtx (tmode);
11877
11878   /* In case the insn wants input operands in modes different from
11879      the result, abort.  */
11880   if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
11881     abort ();
11882
11883   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11884     op0 = copy_to_mode_reg (mode0, op0);
11885   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11886     op1 = copy_to_mode_reg (mode1, op1);
11887
11888   pat = GEN_FCN (icode) (target, op0, op1);
11889   if (! pat)
11890     return 0;
11891   emit_insn (pat);
11892   return target;
11893 }
11894
11895 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
11896
11897 static rtx
11898 arm_expand_unop_builtin (enum insn_code icode,
11899                          tree arglist, rtx target, int do_load)
11900 {
11901   rtx pat;
11902   tree arg0 = TREE_VALUE (arglist);
11903   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11904   enum machine_mode tmode = insn_data[icode].operand[0].mode;
11905   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11906
11907   if (! target
11908       || GET_MODE (target) != tmode
11909       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11910     target = gen_reg_rtx (tmode);
11911   if (do_load)
11912     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11913   else
11914     {
11915       if (VECTOR_MODE_P (mode0))
11916         op0 = safe_vector_operand (op0, mode0);
11917
11918       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11919         op0 = copy_to_mode_reg (mode0, op0);
11920     }
11921
11922   pat = GEN_FCN (icode) (target, op0);
11923   if (! pat)
11924     return 0;
11925   emit_insn (pat);
11926   return target;
11927 }
11928
11929 /* Expand an expression EXP that calls a built-in function,
11930    with result going to TARGET if that's convenient
11931    (and in mode MODE if that's convenient).
11932    SUBTARGET may be used as the target for computing one of EXP's operands.
11933    IGNORE is nonzero if the value is to be ignored.  */
11934
11935 static rtx
11936 arm_expand_builtin (tree exp,
11937                     rtx target,
11938                     rtx subtarget ATTRIBUTE_UNUSED,
11939                     enum machine_mode mode ATTRIBUTE_UNUSED,
11940                     int ignore ATTRIBUTE_UNUSED)
11941 {
11942   const struct builtin_description * d;
11943   enum insn_code    icode;
11944   tree              fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
11945   tree              arglist = TREE_OPERAND (exp, 1);
11946   tree              arg0;
11947   tree              arg1;
11948   tree              arg2;
11949   rtx               op0;
11950   rtx               op1;
11951   rtx               op2;
11952   rtx               pat;
11953   int               fcode = DECL_FUNCTION_CODE (fndecl);
11954   size_t            i;
11955   enum machine_mode tmode;
11956   enum machine_mode mode0;
11957   enum machine_mode mode1;
11958   enum machine_mode mode2;
11959
11960   switch (fcode)
11961     {
11962     case ARM_BUILTIN_TEXTRMSB:
11963     case ARM_BUILTIN_TEXTRMUB:
11964     case ARM_BUILTIN_TEXTRMSH:
11965     case ARM_BUILTIN_TEXTRMUH:
11966     case ARM_BUILTIN_TEXTRMSW:
11967     case ARM_BUILTIN_TEXTRMUW:
11968       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
11969                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
11970                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
11971                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
11972                : CODE_FOR_iwmmxt_textrmw);
11973
11974       arg0 = TREE_VALUE (arglist);
11975       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11976       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11977       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11978       tmode = insn_data[icode].operand[0].mode;
11979       mode0 = insn_data[icode].operand[1].mode;
11980       mode1 = insn_data[icode].operand[2].mode;
11981
11982       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11983         op0 = copy_to_mode_reg (mode0, op0);
11984       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11985         {
11986           /* @@@ better error message */
11987           error ("selector must be an immediate");
11988           return gen_reg_rtx (tmode);
11989         }
11990       if (target == 0
11991           || GET_MODE (target) != tmode
11992           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11993         target = gen_reg_rtx (tmode);
11994       pat = GEN_FCN (icode) (target, op0, op1);
11995       if (! pat)
11996         return 0;
11997       emit_insn (pat);
11998       return target;
11999
12000     case ARM_BUILTIN_TINSRB:
12001     case ARM_BUILTIN_TINSRH:
12002     case ARM_BUILTIN_TINSRW:
12003       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12004                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12005                : CODE_FOR_iwmmxt_tinsrw);
12006       arg0 = TREE_VALUE (arglist);
12007       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12008       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12009       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12010       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12011       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12012       tmode = insn_data[icode].operand[0].mode;
12013       mode0 = insn_data[icode].operand[1].mode;
12014       mode1 = insn_data[icode].operand[2].mode;
12015       mode2 = insn_data[icode].operand[3].mode;
12016
12017       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12018         op0 = copy_to_mode_reg (mode0, op0);
12019       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12020         op1 = copy_to_mode_reg (mode1, op1);
12021       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12022         {
12023           /* @@@ better error message */
12024           error ("selector must be an immediate");
12025           return const0_rtx;
12026         }
12027       if (target == 0
12028           || GET_MODE (target) != tmode
12029           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12030         target = gen_reg_rtx (tmode);
12031       pat = GEN_FCN (icode) (target, op0, op1, op2);
12032       if (! pat)
12033         return 0;
12034       emit_insn (pat);
12035       return target;
12036
12037     case ARM_BUILTIN_SETWCX:
12038       arg0 = TREE_VALUE (arglist);
12039       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12040       op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12041       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12042       emit_insn (gen_iwmmxt_tmcr (op1, op0));
12043       return 0;
12044
12045     case ARM_BUILTIN_GETWCX:
12046       arg0 = TREE_VALUE (arglist);
12047       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12048       target = gen_reg_rtx (SImode);
12049       emit_insn (gen_iwmmxt_tmrc (target, op0));
12050       return target;
12051
12052     case ARM_BUILTIN_WSHUFH:
12053       icode = CODE_FOR_iwmmxt_wshufh;
12054       arg0 = TREE_VALUE (arglist);
12055       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12056       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12057       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12058       tmode = insn_data[icode].operand[0].mode;
12059       mode1 = insn_data[icode].operand[1].mode;
12060       mode2 = insn_data[icode].operand[2].mode;
12061
12062       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12063         op0 = copy_to_mode_reg (mode1, op0);
12064       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12065         {
12066           /* @@@ better error message */
12067           error ("mask must be an immediate");
12068           return const0_rtx;
12069         }
12070       if (target == 0
12071           || GET_MODE (target) != tmode
12072           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12073         target = gen_reg_rtx (tmode);
12074       pat = GEN_FCN (icode) (target, op0, op1);
12075       if (! pat)
12076         return 0;
12077       emit_insn (pat);
12078       return target;
12079
12080     case ARM_BUILTIN_WSADB:
12081       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12082     case ARM_BUILTIN_WSADH:
12083       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12084     case ARM_BUILTIN_WSADBZ:
12085       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12086     case ARM_BUILTIN_WSADHZ:
12087       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12088
12089       /* Several three-argument builtins.  */
12090     case ARM_BUILTIN_WMACS:
12091     case ARM_BUILTIN_WMACU:
12092     case ARM_BUILTIN_WALIGN:
12093     case ARM_BUILTIN_TMIA:
12094     case ARM_BUILTIN_TMIAPH:
12095     case ARM_BUILTIN_TMIATT:
12096     case ARM_BUILTIN_TMIATB:
12097     case ARM_BUILTIN_TMIABT:
12098     case ARM_BUILTIN_TMIABB:
12099       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12100                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12101                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12102                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12103                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12104                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12105                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12106                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12107                : CODE_FOR_iwmmxt_walign);
12108       arg0 = TREE_VALUE (arglist);
12109       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12110       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12111       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12112       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12113       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12114       tmode = insn_data[icode].operand[0].mode;
12115       mode0 = insn_data[icode].operand[1].mode;
12116       mode1 = insn_data[icode].operand[2].mode;
12117       mode2 = insn_data[icode].operand[3].mode;
12118
12119       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12120         op0 = copy_to_mode_reg (mode0, op0);
12121       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12122         op1 = copy_to_mode_reg (mode1, op1);
12123       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12124         op2 = copy_to_mode_reg (mode2, op2);
12125       if (target == 0
12126           || GET_MODE (target) != tmode
12127           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12128         target = gen_reg_rtx (tmode);
12129       pat = GEN_FCN (icode) (target, op0, op1, op2);
12130       if (! pat)
12131         return 0;
12132       emit_insn (pat);
12133       return target;
12134
12135     case ARM_BUILTIN_WZERO:
12136       target = gen_reg_rtx (DImode);
12137       emit_insn (gen_iwmmxt_clrdi (target));
12138       return target;
12139
12140     default:
12141       break;
12142     }
12143
12144   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12145     if (d->code == (const enum arm_builtins) fcode)
12146       return arm_expand_binop_builtin (d->icode, arglist, target);
12147
12148   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12149     if (d->code == (const enum arm_builtins) fcode)
12150       return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12151
12152   /* @@@ Should really do something sensible here.  */
12153   return NULL_RTX;
12154 }
12155 \f
12156 /* Return the number (counting from 0) of
12157    the least significant set bit in MASK.  */
12158
12159 inline static int
12160 number_of_first_bit_set (unsigned mask)
12161 {
12162   int bit;
12163
12164   for (bit = 0;
12165        (mask & (1 << bit)) == 0;
12166        ++bit)
12167     continue;
12168
12169   return bit;
12170 }
12171
12172 /* Emit code to push or pop registers to or from the stack.  F is the
12173    assembly file.  MASK is the registers to push or pop.  PUSH is
12174    nonzero if we should push, and zero if we should pop.  For debugging
12175    output, if pushing, adjust CFA_OFFSET by the amount of space added
12176    to the stack.  REAL_REGS should have the same number of bits set as
12177    MASK, and will be used instead (in the same order) to describe which
12178    registers were saved - this is used to mark the save slots when we
12179    push high registers after moving them to low registers.  */
12180 static void
12181 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
12182                unsigned long real_regs)
12183 {
12184   int regno;
12185   int lo_mask = mask & 0xFF;
12186   int pushed_words = 0;
12187
12188   if (mask == 0)
12189     abort ();
12190
12191   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12192     {
12193       /* Special case.  Do not generate a POP PC statement here, do it in
12194          thumb_exit() */
12195       thumb_exit (f, -1);
12196       return;
12197     }
12198
12199   fprintf (f, "\t%s\t{", push ? "push" : "pop");
12200
12201   /* Look at the low registers first.  */
12202   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12203     {
12204       if (lo_mask & 1)
12205         {
12206           asm_fprintf (f, "%r", regno);
12207
12208           if ((lo_mask & ~1) != 0)
12209             fprintf (f, ", ");
12210
12211           pushed_words++;
12212         }
12213     }
12214
12215   if (push && (mask & (1 << LR_REGNUM)))
12216     {
12217       /* Catch pushing the LR.  */
12218       if (mask & 0xFF)
12219         fprintf (f, ", ");
12220
12221       asm_fprintf (f, "%r", LR_REGNUM);
12222
12223       pushed_words++;
12224     }
12225   else if (!push && (mask & (1 << PC_REGNUM)))
12226     {
12227       /* Catch popping the PC.  */
12228       if (TARGET_INTERWORK || TARGET_BACKTRACE
12229           || current_function_calls_eh_return)
12230         {
12231           /* The PC is never poped directly, instead
12232              it is popped into r3 and then BX is used.  */
12233           fprintf (f, "}\n");
12234
12235           thumb_exit (f, -1);
12236
12237           return;
12238         }
12239       else
12240         {
12241           if (mask & 0xFF)
12242             fprintf (f, ", ");
12243
12244           asm_fprintf (f, "%r", PC_REGNUM);
12245         }
12246     }
12247
12248   fprintf (f, "}\n");
12249
12250   if (push && pushed_words && dwarf2out_do_frame ())
12251     {
12252       char *l = dwarf2out_cfi_label ();
12253       int pushed_mask = real_regs;
12254
12255       *cfa_offset += pushed_words * 4;
12256       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12257
12258       pushed_words = 0;
12259       pushed_mask = real_regs;
12260       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12261         {
12262           if (pushed_mask & 1)
12263             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12264         }
12265     }
12266 }
12267
12268 /* Generate code to return from a thumb function.
12269    If 'reg_containing_return_addr' is -1, then the return address is
12270    actually on the stack, at the stack pointer.  */
12271 static void
12272 thumb_exit (FILE *f, int reg_containing_return_addr)
12273 {
12274   unsigned regs_available_for_popping;
12275   unsigned regs_to_pop;
12276   int pops_needed;
12277   unsigned available;
12278   unsigned required;
12279   int mode;
12280   int size;
12281   int restore_a4 = FALSE;
12282
12283   /* Compute the registers we need to pop.  */
12284   regs_to_pop = 0;
12285   pops_needed = 0;
12286
12287   if (reg_containing_return_addr == -1)
12288     {
12289       regs_to_pop |= 1 << LR_REGNUM;
12290       ++pops_needed;
12291     }
12292
12293   if (TARGET_BACKTRACE)
12294     {
12295       /* Restore the (ARM) frame pointer and stack pointer.  */
12296       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12297       pops_needed += 2;
12298     }
12299
12300   /* If there is nothing to pop then just emit the BX instruction and
12301      return.  */
12302   if (pops_needed == 0)
12303     {
12304       if (current_function_calls_eh_return)
12305         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12306
12307       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12308       return;
12309     }
12310   /* Otherwise if we are not supporting interworking and we have not created
12311      a backtrace structure and the function was not entered in ARM mode then
12312      just pop the return address straight into the PC.  */
12313   else if (!TARGET_INTERWORK
12314            && !TARGET_BACKTRACE
12315            && !is_called_in_ARM_mode (current_function_decl)
12316            && !current_function_calls_eh_return)
12317     {
12318       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12319       return;
12320     }
12321
12322   /* Find out how many of the (return) argument registers we can corrupt.  */
12323   regs_available_for_popping = 0;
12324
12325   /* If returning via __builtin_eh_return, the bottom three registers
12326      all contain information needed for the return.  */
12327   if (current_function_calls_eh_return)
12328     size = 12;
12329   else
12330     {
12331       /* If we can deduce the registers used from the function's
12332          return value.  This is more reliable that examining
12333          regs_ever_live[] because that will be set if the register is
12334          ever used in the function, not just if the register is used
12335          to hold a return value.  */
12336
12337       if (current_function_return_rtx != 0)
12338         mode = GET_MODE (current_function_return_rtx);
12339       else
12340         mode = DECL_MODE (DECL_RESULT (current_function_decl));
12341
12342       size = GET_MODE_SIZE (mode);
12343
12344       if (size == 0)
12345         {
12346           /* In a void function we can use any argument register.
12347              In a function that returns a structure on the stack
12348              we can use the second and third argument registers.  */
12349           if (mode == VOIDmode)
12350             regs_available_for_popping =
12351               (1 << ARG_REGISTER (1))
12352               | (1 << ARG_REGISTER (2))
12353               | (1 << ARG_REGISTER (3));
12354           else
12355             regs_available_for_popping =
12356               (1 << ARG_REGISTER (2))
12357               | (1 << ARG_REGISTER (3));
12358         }
12359       else if (size <= 4)
12360         regs_available_for_popping =
12361           (1 << ARG_REGISTER (2))
12362           | (1 << ARG_REGISTER (3));
12363       else if (size <= 8)
12364         regs_available_for_popping =
12365           (1 << ARG_REGISTER (3));
12366     }
12367
12368   /* Match registers to be popped with registers into which we pop them.  */
12369   for (available = regs_available_for_popping,
12370        required  = regs_to_pop;
12371        required != 0 && available != 0;
12372        available &= ~(available & - available),
12373        required  &= ~(required  & - required))
12374     -- pops_needed;
12375
12376   /* If we have any popping registers left over, remove them.  */
12377   if (available > 0)
12378     regs_available_for_popping &= ~available;
12379
12380   /* Otherwise if we need another popping register we can use
12381      the fourth argument register.  */
12382   else if (pops_needed)
12383     {
12384       /* If we have not found any free argument registers and
12385          reg a4 contains the return address, we must move it.  */
12386       if (regs_available_for_popping == 0
12387           && reg_containing_return_addr == LAST_ARG_REGNUM)
12388         {
12389           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12390           reg_containing_return_addr = LR_REGNUM;
12391         }
12392       else if (size > 12)
12393         {
12394           /* Register a4 is being used to hold part of the return value,
12395              but we have dire need of a free, low register.  */
12396           restore_a4 = TRUE;
12397
12398           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12399         }
12400
12401       if (reg_containing_return_addr != LAST_ARG_REGNUM)
12402         {
12403           /* The fourth argument register is available.  */
12404           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12405
12406           --pops_needed;
12407         }
12408     }
12409
12410   /* Pop as many registers as we can.  */
12411   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12412                  regs_available_for_popping);
12413
12414   /* Process the registers we popped.  */
12415   if (reg_containing_return_addr == -1)
12416     {
12417       /* The return address was popped into the lowest numbered register.  */
12418       regs_to_pop &= ~(1 << LR_REGNUM);
12419
12420       reg_containing_return_addr =
12421         number_of_first_bit_set (regs_available_for_popping);
12422
12423       /* Remove this register for the mask of available registers, so that
12424          the return address will not be corrupted by further pops.  */
12425       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12426     }
12427
12428   /* If we popped other registers then handle them here.  */
12429   if (regs_available_for_popping)
12430     {
12431       int frame_pointer;
12432
12433       /* Work out which register currently contains the frame pointer.  */
12434       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12435
12436       /* Move it into the correct place.  */
12437       asm_fprintf (f, "\tmov\t%r, %r\n",
12438                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12439
12440       /* (Temporarily) remove it from the mask of popped registers.  */
12441       regs_available_for_popping &= ~(1 << frame_pointer);
12442       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12443
12444       if (regs_available_for_popping)
12445         {
12446           int stack_pointer;
12447
12448           /* We popped the stack pointer as well,
12449              find the register that contains it.  */
12450           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12451
12452           /* Move it into the stack register.  */
12453           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12454
12455           /* At this point we have popped all necessary registers, so
12456              do not worry about restoring regs_available_for_popping
12457              to its correct value:
12458
12459              assert (pops_needed == 0)
12460              assert (regs_available_for_popping == (1 << frame_pointer))
12461              assert (regs_to_pop == (1 << STACK_POINTER))  */
12462         }
12463       else
12464         {
12465           /* Since we have just move the popped value into the frame
12466              pointer, the popping register is available for reuse, and
12467              we know that we still have the stack pointer left to pop.  */
12468           regs_available_for_popping |= (1 << frame_pointer);
12469         }
12470     }
12471
12472   /* If we still have registers left on the stack, but we no longer have
12473      any registers into which we can pop them, then we must move the return
12474      address into the link register and make available the register that
12475      contained it.  */
12476   if (regs_available_for_popping == 0 && pops_needed > 0)
12477     {
12478       regs_available_for_popping |= 1 << reg_containing_return_addr;
12479
12480       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12481                    reg_containing_return_addr);
12482
12483       reg_containing_return_addr = LR_REGNUM;
12484     }
12485
12486   /* If we have registers left on the stack then pop some more.
12487      We know that at most we will want to pop FP and SP.  */
12488   if (pops_needed > 0)
12489     {
12490       int  popped_into;
12491       int  move_to;
12492
12493       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12494                      regs_available_for_popping);
12495
12496       /* We have popped either FP or SP.
12497          Move whichever one it is into the correct register.  */
12498       popped_into = number_of_first_bit_set (regs_available_for_popping);
12499       move_to     = number_of_first_bit_set (regs_to_pop);
12500
12501       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12502
12503       regs_to_pop &= ~(1 << move_to);
12504
12505       --pops_needed;
12506     }
12507
12508   /* If we still have not popped everything then we must have only
12509      had one register available to us and we are now popping the SP.  */
12510   if (pops_needed > 0)
12511     {
12512       int  popped_into;
12513
12514       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12515                      regs_available_for_popping);
12516
12517       popped_into = number_of_first_bit_set (regs_available_for_popping);
12518
12519       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12520       /*
12521         assert (regs_to_pop == (1 << STACK_POINTER))
12522         assert (pops_needed == 1)
12523       */
12524     }
12525
12526   /* If necessary restore the a4 register.  */
12527   if (restore_a4)
12528     {
12529       if (reg_containing_return_addr != LR_REGNUM)
12530         {
12531           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12532           reg_containing_return_addr = LR_REGNUM;
12533         }
12534
12535       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12536     }
12537
12538   if (current_function_calls_eh_return)
12539     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12540
12541   /* Return to caller.  */
12542   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12543 }
12544
12545 \f
12546 void
12547 thumb_final_prescan_insn (rtx insn)
12548 {
12549   if (flag_print_asm_name)
12550     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12551                  INSN_ADDRESSES (INSN_UID (insn)));
12552 }
12553
12554 int
12555 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12556 {
12557   unsigned HOST_WIDE_INT mask = 0xff;
12558   int i;
12559
12560   if (val == 0) /* XXX */
12561     return 0;
12562
12563   for (i = 0; i < 25; i++)
12564     if ((val & (mask << i)) == val)
12565       return 1;
12566
12567   return 0;
12568 }
12569
12570 /* Returns nonzero if the current function contains,
12571    or might contain a far jump.  */
12572 static int
12573 thumb_far_jump_used_p (void)
12574 {
12575   rtx insn;
12576
12577   /* This test is only important for leaf functions.  */
12578   /* assert (!leaf_function_p ()); */
12579
12580   /* If we have already decided that far jumps may be used,
12581      do not bother checking again, and always return true even if
12582      it turns out that they are not being used.  Once we have made
12583      the decision that far jumps are present (and that hence the link
12584      register will be pushed onto the stack) we cannot go back on it.  */
12585   if (cfun->machine->far_jump_used)
12586     return 1;
12587
12588   /* If this function is not being called from the prologue/epilogue
12589      generation code then it must be being called from the
12590      INITIAL_ELIMINATION_OFFSET macro.  */
12591   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12592     {
12593       /* In this case we know that we are being asked about the elimination
12594          of the arg pointer register.  If that register is not being used,
12595          then there are no arguments on the stack, and we do not have to
12596          worry that a far jump might force the prologue to push the link
12597          register, changing the stack offsets.  In this case we can just
12598          return false, since the presence of far jumps in the function will
12599          not affect stack offsets.
12600
12601          If the arg pointer is live (or if it was live, but has now been
12602          eliminated and so set to dead) then we do have to test to see if
12603          the function might contain a far jump.  This test can lead to some
12604          false negatives, since before reload is completed, then length of
12605          branch instructions is not known, so gcc defaults to returning their
12606          longest length, which in turn sets the far jump attribute to true.
12607
12608          A false negative will not result in bad code being generated, but it
12609          will result in a needless push and pop of the link register.  We
12610          hope that this does not occur too often.
12611
12612          If we need doubleword stack alignment this could affect the other
12613          elimination offsets so we can't risk getting it wrong.  */
12614       if (regs_ever_live [ARG_POINTER_REGNUM])
12615         cfun->machine->arg_pointer_live = 1;
12616       else if (!cfun->machine->arg_pointer_live)
12617         return 0;
12618     }
12619
12620   /* Check to see if the function contains a branch
12621      insn with the far jump attribute set.  */
12622   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12623     {
12624       if (GET_CODE (insn) == JUMP_INSN
12625           /* Ignore tablejump patterns.  */
12626           && GET_CODE (PATTERN (insn)) != ADDR_VEC
12627           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12628           && get_attr_far_jump (insn) == FAR_JUMP_YES
12629           )
12630         {
12631           /* Record the fact that we have decided that
12632              the function does use far jumps.  */
12633           cfun->machine->far_jump_used = 1;
12634           return 1;
12635         }
12636     }
12637
12638   return 0;
12639 }
12640
12641 /* Return nonzero if FUNC must be entered in ARM mode.  */
12642 int
12643 is_called_in_ARM_mode (tree func)
12644 {
12645   if (TREE_CODE (func) != FUNCTION_DECL)
12646     abort ();
12647
12648   /* Ignore the problem about functions whoes address is taken.  */
12649   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12650     return TRUE;
12651
12652 #ifdef ARM_PE
12653   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12654 #else
12655   return FALSE;
12656 #endif
12657 }
12658
12659 /* The bits which aren't usefully expanded as rtl.  */
12660 const char *
12661 thumb_unexpanded_epilogue (void)
12662 {
12663   int regno;
12664   unsigned long live_regs_mask = 0;
12665   int high_regs_pushed = 0;
12666   int had_to_push_lr;
12667   int size;
12668   int mode;
12669
12670   if (return_used_this_function)
12671     return "";
12672
12673   if (IS_NAKED (arm_current_func_type ()))
12674     return "";
12675
12676   live_regs_mask = thumb_compute_save_reg_mask ();
12677   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
12678
12679   /* If we can deduce the registers used from the function's return value.
12680      This is more reliable that examining regs_ever_live[] because that
12681      will be set if the register is ever used in the function, not just if
12682      the register is used to hold a return value.  */
12683
12684   if (current_function_return_rtx != 0)
12685     mode = GET_MODE (current_function_return_rtx);
12686   else
12687     mode = DECL_MODE (DECL_RESULT (current_function_decl));
12688
12689   size = GET_MODE_SIZE (mode);
12690
12691   /* The prolog may have pushed some high registers to use as
12692      work registers.  e.g. the testsuite file:
12693      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12694      compiles to produce:
12695         push    {r4, r5, r6, r7, lr}
12696         mov     r7, r9
12697         mov     r6, r8
12698         push    {r6, r7}
12699      as part of the prolog.  We have to undo that pushing here.  */
12700
12701   if (high_regs_pushed)
12702     {
12703       unsigned long mask = live_regs_mask & 0xff;
12704       int next_hi_reg;
12705
12706       /* The available low registers depend on the size of the value we are
12707          returning.  */
12708       if (size <= 12)
12709         mask |=  1 << 3;
12710       if (size <= 8)
12711         mask |= 1 << 2;
12712
12713       if (mask == 0)
12714         /* Oh dear!  We have no low registers into which we can pop
12715            high registers!  */
12716         internal_error
12717           ("no low registers available for popping high registers");
12718
12719       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12720         if (live_regs_mask & (1 << next_hi_reg))
12721           break;
12722
12723       while (high_regs_pushed)
12724         {
12725           /* Find lo register(s) into which the high register(s) can
12726              be popped.  */
12727           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12728             {
12729               if (mask & (1 << regno))
12730                 high_regs_pushed--;
12731               if (high_regs_pushed == 0)
12732                 break;
12733             }
12734
12735           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
12736
12737           /* Pop the values into the low register(s).  */
12738           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
12739
12740           /* Move the value(s) into the high registers.  */
12741           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12742             {
12743               if (mask & (1 << regno))
12744                 {
12745                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12746                                regno);
12747
12748                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
12749                     if (live_regs_mask & (1 << next_hi_reg))
12750                       break;
12751                 }
12752             }
12753         }
12754       live_regs_mask &= ~0x0f00;
12755     }
12756
12757   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
12758   live_regs_mask &= 0xff;
12759
12760   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12761     {
12762       /* Pop the return address into the PC.  */
12763       if (had_to_push_lr)
12764         live_regs_mask |= 1 << PC_REGNUM;
12765
12766       /* Either no argument registers were pushed or a backtrace
12767          structure was created which includes an adjusted stack
12768          pointer, so just pop everything.  */
12769       if (live_regs_mask)
12770         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12771                        live_regs_mask);
12772
12773       /* We have either just popped the return address into the
12774          PC or it is was kept in LR for the entire function.  */
12775       if (!had_to_push_lr)
12776         thumb_exit (asm_out_file, LR_REGNUM);
12777     }
12778   else
12779     {
12780       /* Pop everything but the return address.  */
12781       if (live_regs_mask)
12782         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12783                        live_regs_mask);
12784
12785       if (had_to_push_lr)
12786         {
12787           if (size > 12)
12788             {
12789               /* We have no free low regs, so save one.  */
12790               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
12791                            LAST_ARG_REGNUM);
12792             }
12793
12794           /* Get the return address into a temporary register.  */
12795           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
12796                          1 << LAST_ARG_REGNUM);
12797
12798           if (size > 12)
12799             {
12800               /* Move the return address to lr.  */
12801               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
12802                            LAST_ARG_REGNUM);
12803               /* Restore the low register.  */
12804               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
12805                            IP_REGNUM);
12806               regno = LR_REGNUM;
12807             }
12808           else
12809             regno = LAST_ARG_REGNUM;
12810         }
12811       else
12812         regno = LR_REGNUM;
12813
12814       /* Remove the argument registers that were pushed onto the stack.  */
12815       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
12816                    SP_REGNUM, SP_REGNUM,
12817                    current_function_pretend_args_size);
12818
12819       thumb_exit (asm_out_file, regno);
12820     }
12821
12822   return "";
12823 }
12824
12825 /* Functions to save and restore machine-specific function data.  */
12826 static struct machine_function *
12827 arm_init_machine_status (void)
12828 {
12829   struct machine_function *machine;
12830   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
12831
12832 #if ARM_FT_UNKNOWN != 0
12833   machine->func_type = ARM_FT_UNKNOWN;
12834 #endif
12835   return machine;
12836 }
12837
12838 /* Return an RTX indicating where the return address to the
12839    calling function can be found.  */
12840 rtx
12841 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
12842 {
12843   if (count != 0)
12844     return NULL_RTX;
12845
12846   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
12847 }
12848
12849 /* Do anything needed before RTL is emitted for each function.  */
12850 void
12851 arm_init_expanders (void)
12852 {
12853   /* Arrange to initialize and mark the machine per-function status.  */
12854   init_machine_status = arm_init_machine_status;
12855
12856   /* This is to stop the combine pass optimizing away the alignment
12857      adjustment of va_arg.  */
12858   /* ??? It is claimed that this should not be necessary.  */
12859   if (cfun)
12860     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
12861 }
12862
12863
12864 /* Like arm_compute_initial_elimination offset.  Simpler because
12865    THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer.  */
12866
12867 HOST_WIDE_INT
12868 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
12869 {
12870   arm_stack_offsets *offsets;
12871
12872   offsets = arm_get_frame_offsets ();
12873
12874   switch (from)
12875     {
12876     case ARG_POINTER_REGNUM:
12877       switch (to)
12878         {
12879         case STACK_POINTER_REGNUM:
12880           return offsets->outgoing_args - offsets->saved_args;
12881
12882         case FRAME_POINTER_REGNUM:
12883           return offsets->soft_frame - offsets->saved_args;
12884
12885         case THUMB_HARD_FRAME_POINTER_REGNUM:
12886         case ARM_HARD_FRAME_POINTER_REGNUM:
12887           return offsets->saved_regs - offsets->saved_args;
12888
12889         default:
12890           abort();
12891         }
12892       break;
12893
12894     case FRAME_POINTER_REGNUM:
12895       switch (to)
12896         {
12897         case STACK_POINTER_REGNUM:
12898           return offsets->outgoing_args - offsets->soft_frame;
12899
12900         case THUMB_HARD_FRAME_POINTER_REGNUM:
12901         case ARM_HARD_FRAME_POINTER_REGNUM:
12902           return offsets->saved_regs - offsets->soft_frame;
12903
12904         default:
12905           abort();
12906         }
12907       break;
12908
12909     default:
12910       abort ();
12911     }
12912 }
12913
12914
12915 /* Generate the rest of a function's prologue.  */
12916 void
12917 thumb_expand_prologue (void)
12918 {
12919   rtx insn, dwarf;
12920
12921   HOST_WIDE_INT amount;
12922   arm_stack_offsets *offsets;
12923   unsigned long func_type;
12924   int regno;
12925   unsigned long live_regs_mask;
12926
12927   func_type = arm_current_func_type ();
12928
12929   /* Naked functions don't have prologues.  */
12930   if (IS_NAKED (func_type))
12931     return;
12932
12933   if (IS_INTERRUPT (func_type))
12934     {
12935       error ("interrupt Service Routines cannot be coded in Thumb mode");
12936       return;
12937     }
12938
12939   live_regs_mask = thumb_compute_save_reg_mask ();
12940   /* Load the pic register before setting the frame pointer,
12941      so we can use r7 as a temporary work register.  */
12942   if (flag_pic)
12943     arm_load_pic_register (thumb_find_work_register (live_regs_mask));
12944
12945   offsets = arm_get_frame_offsets ();
12946
12947   if (frame_pointer_needed)
12948     {
12949       insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
12950                                    stack_pointer_rtx));
12951       RTX_FRAME_RELATED_P (insn) = 1;
12952     }
12953   else if (CALLER_INTERWORKING_SLOT_SIZE > 0)
12954     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
12955                     stack_pointer_rtx);
12956
12957   amount = offsets->outgoing_args - offsets->saved_regs;
12958   if (amount)
12959     {
12960       if (amount < 512)
12961         {
12962           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
12963                                         GEN_INT (- amount)));
12964           RTX_FRAME_RELATED_P (insn) = 1;
12965         }
12966       else
12967         {
12968           rtx reg;
12969
12970           /* The stack decrement is too big for an immediate value in a single
12971              insn.  In theory we could issue multiple subtracts, but after
12972              three of them it becomes more space efficient to place the full
12973              value in the constant pool and load into a register.  (Also the
12974              ARM debugger really likes to see only one stack decrement per
12975              function).  So instead we look for a scratch register into which
12976              we can load the decrement, and then we subtract this from the
12977              stack pointer.  Unfortunately on the thumb the only available
12978              scratch registers are the argument registers, and we cannot use
12979              these as they may hold arguments to the function.  Instead we
12980              attempt to locate a call preserved register which is used by this
12981              function.  If we can find one, then we know that it will have
12982              been pushed at the start of the prologue and so we can corrupt
12983              it now.  */
12984           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
12985             if (live_regs_mask & (1 << regno)
12986                 && !(frame_pointer_needed
12987                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
12988               break;
12989
12990           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
12991             {
12992               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
12993
12994               /* Choose an arbitrary, non-argument low register.  */
12995               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
12996
12997               /* Save it by copying it into a high, scratch register.  */
12998               emit_insn (gen_movsi (spare, reg));
12999               /* Add a USE to stop propagate_one_insn() from barfing.  */
13000               emit_insn (gen_prologue_use (spare));
13001
13002               /* Decrement the stack.  */
13003               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13004               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13005                                             stack_pointer_rtx, reg));
13006               RTX_FRAME_RELATED_P (insn) = 1;
13007               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13008                                    plus_constant (stack_pointer_rtx,
13009                                                   -amount));
13010               RTX_FRAME_RELATED_P (dwarf) = 1;
13011               REG_NOTES (insn)
13012                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13013                                      REG_NOTES (insn));
13014
13015               /* Restore the low register's original value.  */
13016               emit_insn (gen_movsi (reg, spare));
13017
13018               /* Emit a USE of the restored scratch register, so that flow
13019                  analysis will not consider the restore redundant.  The
13020                  register won't be used again in this function and isn't
13021                  restored by the epilogue.  */
13022               emit_insn (gen_prologue_use (reg));
13023             }
13024           else
13025             {
13026               reg = gen_rtx_REG (SImode, regno);
13027
13028               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13029
13030               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13031                                             stack_pointer_rtx, reg));
13032               RTX_FRAME_RELATED_P (insn) = 1;
13033               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13034                                    plus_constant (stack_pointer_rtx,
13035                                                   -amount));
13036               RTX_FRAME_RELATED_P (dwarf) = 1;
13037               REG_NOTES (insn)
13038                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13039                                      REG_NOTES (insn));
13040             }
13041         }
13042       /* If the frame pointer is needed, emit a special barrier that
13043          will prevent the scheduler from moving stores to the frame
13044          before the stack adjustment.  */
13045       if (frame_pointer_needed)
13046         emit_insn (gen_stack_tie (stack_pointer_rtx,
13047                                   hard_frame_pointer_rtx));
13048     }
13049
13050   if (current_function_profile || TARGET_NO_SCHED_PRO)
13051     emit_insn (gen_blockage ());
13052
13053   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13054   if (live_regs_mask & 0xff)
13055     cfun->machine->lr_save_eliminated = 0;
13056
13057   /* If the link register is being kept alive, with the return address in it,
13058      then make sure that it does not get reused by the ce2 pass.  */
13059   if (cfun->machine->lr_save_eliminated)
13060     emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13061 }
13062
13063
13064 void
13065 thumb_expand_epilogue (void)
13066 {
13067   HOST_WIDE_INT amount;
13068   arm_stack_offsets *offsets;
13069   int regno;
13070
13071   /* Naked functions don't have prologues.  */
13072   if (IS_NAKED (arm_current_func_type ()))
13073     return;
13074
13075   offsets = arm_get_frame_offsets ();
13076   amount = offsets->outgoing_args - offsets->saved_regs;
13077
13078   if (frame_pointer_needed)
13079     emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13080   else if (amount)
13081     {
13082       if (amount < 512)
13083         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13084                                GEN_INT (amount)));
13085       else
13086         {
13087           /* r3 is always free in the epilogue.  */
13088           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13089
13090           emit_insn (gen_movsi (reg, GEN_INT (amount)));
13091           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13092         }
13093     }
13094
13095   /* Emit a USE (stack_pointer_rtx), so that
13096      the stack adjustment will not be deleted.  */
13097   emit_insn (gen_prologue_use (stack_pointer_rtx));
13098
13099   if (current_function_profile || TARGET_NO_SCHED_PRO)
13100     emit_insn (gen_blockage ());
13101
13102   /* Emit a clobber for each insn that will be restored in the epilogue,
13103      so that flow2 will get register lifetimes correct.  */
13104   for (regno = 0; regno < 13; regno++)
13105     if (regs_ever_live[regno] && !call_used_regs[regno])
13106       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13107
13108   if (! regs_ever_live[LR_REGNUM])
13109     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13110 }
13111
13112 static void
13113 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13114 {
13115   unsigned long live_regs_mask = 0;
13116   unsigned long l_mask;
13117   unsigned high_regs_pushed = 0;
13118   int cfa_offset = 0;
13119   int regno;
13120
13121   if (IS_NAKED (arm_current_func_type ()))
13122     return;
13123
13124   if (is_called_in_ARM_mode (current_function_decl))
13125     {
13126       const char * name;
13127
13128       if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
13129         abort ();
13130       if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
13131         abort ();
13132       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
13133
13134       /* Generate code sequence to switch us into Thumb mode.  */
13135       /* The .code 32 directive has already been emitted by
13136          ASM_DECLARE_FUNCTION_NAME.  */
13137       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13138       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13139
13140       /* Generate a label, so that the debugger will notice the
13141          change in instruction sets.  This label is also used by
13142          the assembler to bypass the ARM code when this function
13143          is called from a Thumb encoded function elsewhere in the
13144          same file.  Hence the definition of STUB_NAME here must
13145          agree with the definition in gas/config/tc-arm.c.  */
13146
13147 #define STUB_NAME ".real_start_of"
13148
13149       fprintf (f, "\t.code\t16\n");
13150 #ifdef ARM_PE
13151       if (arm_dllexport_name_p (name))
13152         name = arm_strip_name_encoding (name);
13153 #endif
13154       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13155       fprintf (f, "\t.thumb_func\n");
13156       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13157     }
13158
13159   if (current_function_pretend_args_size)
13160     {
13161       if (cfun->machine->uses_anonymous_args)
13162         {
13163           int num_pushes;
13164
13165           fprintf (f, "\tpush\t{");
13166
13167           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13168
13169           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13170                regno <= LAST_ARG_REGNUM;
13171                regno++)
13172             asm_fprintf (f, "%r%s", regno,
13173                          regno == LAST_ARG_REGNUM ? "" : ", ");
13174
13175           fprintf (f, "}\n");
13176         }
13177       else
13178         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13179                      SP_REGNUM, SP_REGNUM,
13180                      current_function_pretend_args_size);
13181
13182       /* We don't need to record the stores for unwinding (would it
13183          help the debugger any if we did?), but record the change in
13184          the stack pointer.  */
13185       if (dwarf2out_do_frame ())
13186         {
13187           char *l = dwarf2out_cfi_label ();
13188
13189           cfa_offset = cfa_offset + current_function_pretend_args_size;
13190           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13191         }
13192     }
13193
13194   /* Get the registers we are going to push.  */
13195   live_regs_mask = thumb_compute_save_reg_mask ();
13196   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
13197   l_mask = live_regs_mask & 0x40ff;
13198   /* Then count how many other high registers will need to be pushed.  */
13199   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13200
13201   if (TARGET_BACKTRACE)
13202     {
13203       unsigned offset;
13204       unsigned work_register;
13205
13206       /* We have been asked to create a stack backtrace structure.
13207          The code looks like this:
13208
13209          0   .align 2
13210          0   func:
13211          0     sub   SP, #16         Reserve space for 4 registers.
13212          2     push  {R7}            Push low registers.
13213          4     add   R7, SP, #20     Get the stack pointer before the push.
13214          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
13215          8     mov   R7, PC          Get hold of the start of this code plus 12.
13216         10     str   R7, [SP, #16]   Store it.
13217         12     mov   R7, FP          Get hold of the current frame pointer.
13218         14     str   R7, [SP, #4]    Store it.
13219         16     mov   R7, LR          Get hold of the current return address.
13220         18     str   R7, [SP, #12]   Store it.
13221         20     add   R7, SP, #16     Point at the start of the backtrace structure.
13222         22     mov   FP, R7          Put this value into the frame pointer.  */
13223
13224       work_register = thumb_find_work_register (live_regs_mask);
13225
13226       asm_fprintf
13227         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13228          SP_REGNUM, SP_REGNUM);
13229
13230       if (dwarf2out_do_frame ())
13231         {
13232           char *l = dwarf2out_cfi_label ();
13233
13234           cfa_offset = cfa_offset + 16;
13235           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13236         }
13237
13238       if (l_mask)
13239         {
13240           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13241           offset = bit_count (l_mask);
13242         }
13243       else
13244         offset = 0;
13245
13246       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13247                    offset + 16 + current_function_pretend_args_size);
13248
13249       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13250                    offset + 4);
13251
13252       /* Make sure that the instruction fetching the PC is in the right place
13253          to calculate "start of backtrace creation code + 12".  */
13254       if (l_mask)
13255         {
13256           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13257           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13258                        offset + 12);
13259           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13260                        ARM_HARD_FRAME_POINTER_REGNUM);
13261           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13262                        offset);
13263         }
13264       else
13265         {
13266           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13267                        ARM_HARD_FRAME_POINTER_REGNUM);
13268           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13269                        offset);
13270           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13271           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13272                        offset + 12);
13273         }
13274
13275       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13276       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13277                    offset + 8);
13278       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13279                    offset + 12);
13280       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13281                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13282     }
13283   /* Optimisation:  If we are not pushing any low registers but we are going
13284      to push some high registers then delay our first push.  This will just
13285      be a push of LR and we can combine it with the push of the first high
13286      register.  */
13287   else if ((l_mask & 0xff) != 0
13288            || (high_regs_pushed == 0 && l_mask))
13289     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13290
13291   if (high_regs_pushed)
13292     {
13293       unsigned pushable_regs;
13294       unsigned next_hi_reg;
13295
13296       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13297         if (live_regs_mask & (1 << next_hi_reg))
13298           break;
13299
13300       pushable_regs = l_mask & 0xff;
13301
13302       if (pushable_regs == 0)
13303         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
13304
13305       while (high_regs_pushed > 0)
13306         {
13307           unsigned long real_regs_mask = 0;
13308
13309           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
13310             {
13311               if (pushable_regs & (1 << regno))
13312                 {
13313                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13314
13315                   high_regs_pushed --;
13316                   real_regs_mask |= (1 << next_hi_reg);
13317
13318                   if (high_regs_pushed)
13319                     {
13320                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
13321                            next_hi_reg --)
13322                         if (live_regs_mask & (1 << next_hi_reg))
13323                           break;
13324                     }
13325                   else
13326                     {
13327                       pushable_regs &= ~((1 << regno) - 1);
13328                       break;
13329                     }
13330                 }
13331             }
13332
13333           /* If we had to find a work register and we have not yet
13334              saved the LR then add it to the list of regs to push.  */
13335           if (l_mask == (1 << LR_REGNUM))
13336             {
13337               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
13338                              1, &cfa_offset,
13339                              real_regs_mask | (1 << LR_REGNUM));
13340               l_mask = 0;
13341             }
13342           else
13343             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
13344         }
13345     }
13346 }
13347
13348 /* Handle the case of a double word load into a low register from
13349    a computed memory address.  The computed address may involve a
13350    register which is overwritten by the load.  */
13351 const char *
13352 thumb_load_double_from_address (rtx *operands)
13353 {
13354   rtx addr;
13355   rtx base;
13356   rtx offset;
13357   rtx arg1;
13358   rtx arg2;
13359
13360   if (GET_CODE (operands[0]) != REG)
13361     abort ();
13362
13363   if (GET_CODE (operands[1]) != MEM)
13364     abort ();
13365
13366   /* Get the memory address.  */
13367   addr = XEXP (operands[1], 0);
13368
13369   /* Work out how the memory address is computed.  */
13370   switch (GET_CODE (addr))
13371     {
13372     case REG:
13373       operands[2] = gen_rtx_MEM (SImode,
13374                                  plus_constant (XEXP (operands[1], 0), 4));
13375
13376       if (REGNO (operands[0]) == REGNO (addr))
13377         {
13378           output_asm_insn ("ldr\t%H0, %2", operands);
13379           output_asm_insn ("ldr\t%0, %1", operands);
13380         }
13381       else
13382         {
13383           output_asm_insn ("ldr\t%0, %1", operands);
13384           output_asm_insn ("ldr\t%H0, %2", operands);
13385         }
13386       break;
13387
13388     case CONST:
13389       /* Compute <address> + 4 for the high order load.  */
13390       operands[2] = gen_rtx_MEM (SImode,
13391                                  plus_constant (XEXP (operands[1], 0), 4));
13392
13393       output_asm_insn ("ldr\t%0, %1", operands);
13394       output_asm_insn ("ldr\t%H0, %2", operands);
13395       break;
13396
13397     case PLUS:
13398       arg1   = XEXP (addr, 0);
13399       arg2   = XEXP (addr, 1);
13400
13401       if (CONSTANT_P (arg1))
13402         base = arg2, offset = arg1;
13403       else
13404         base = arg1, offset = arg2;
13405
13406       if (GET_CODE (base) != REG)
13407         abort ();
13408
13409       /* Catch the case of <address> = <reg> + <reg> */
13410       if (GET_CODE (offset) == REG)
13411         {
13412           int reg_offset = REGNO (offset);
13413           int reg_base   = REGNO (base);
13414           int reg_dest   = REGNO (operands[0]);
13415
13416           /* Add the base and offset registers together into the
13417              higher destination register.  */
13418           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13419                        reg_dest + 1, reg_base, reg_offset);
13420
13421           /* Load the lower destination register from the address in
13422              the higher destination register.  */
13423           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13424                        reg_dest, reg_dest + 1);
13425
13426           /* Load the higher destination register from its own address
13427              plus 4.  */
13428           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13429                        reg_dest + 1, reg_dest + 1);
13430         }
13431       else
13432         {
13433           /* Compute <address> + 4 for the high order load.  */
13434           operands[2] = gen_rtx_MEM (SImode,
13435                                      plus_constant (XEXP (operands[1], 0), 4));
13436
13437           /* If the computed address is held in the low order register
13438              then load the high order register first, otherwise always
13439              load the low order register first.  */
13440           if (REGNO (operands[0]) == REGNO (base))
13441             {
13442               output_asm_insn ("ldr\t%H0, %2", operands);
13443               output_asm_insn ("ldr\t%0, %1", operands);
13444             }
13445           else
13446             {
13447               output_asm_insn ("ldr\t%0, %1", operands);
13448               output_asm_insn ("ldr\t%H0, %2", operands);
13449             }
13450         }
13451       break;
13452
13453     case LABEL_REF:
13454       /* With no registers to worry about we can just load the value
13455          directly.  */
13456       operands[2] = gen_rtx_MEM (SImode,
13457                                  plus_constant (XEXP (operands[1], 0), 4));
13458
13459       output_asm_insn ("ldr\t%H0, %2", operands);
13460       output_asm_insn ("ldr\t%0, %1", operands);
13461       break;
13462
13463     default:
13464       abort ();
13465       break;
13466     }
13467
13468   return "";
13469 }
13470
13471 const char *
13472 thumb_output_move_mem_multiple (int n, rtx *operands)
13473 {
13474   rtx tmp;
13475
13476   switch (n)
13477     {
13478     case 2:
13479       if (REGNO (operands[4]) > REGNO (operands[5]))
13480         {
13481           tmp = operands[4];
13482           operands[4] = operands[5];
13483           operands[5] = tmp;
13484         }
13485       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13486       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13487       break;
13488
13489     case 3:
13490       if (REGNO (operands[4]) > REGNO (operands[5]))
13491         {
13492           tmp = operands[4];
13493           operands[4] = operands[5];
13494           operands[5] = tmp;
13495         }
13496       if (REGNO (operands[5]) > REGNO (operands[6]))
13497         {
13498           tmp = operands[5];
13499           operands[5] = operands[6];
13500           operands[6] = tmp;
13501         }
13502       if (REGNO (operands[4]) > REGNO (operands[5]))
13503         {
13504           tmp = operands[4];
13505           operands[4] = operands[5];
13506           operands[5] = tmp;
13507         }
13508
13509       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13510       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13511       break;
13512
13513     default:
13514       abort ();
13515     }
13516
13517   return "";
13518 }
13519
13520 /* Output a call-via instruction for thumb state.  */
13521 const char *
13522 thumb_call_via_reg (rtx reg)
13523 {
13524   int regno = REGNO (reg);
13525   rtx *labelp;
13526
13527   gcc_assert (regno < LR_REGNUM);
13528
13529   /* If we are in the normal text section we can use a single instance
13530      per compilation unit.  If we are doing function sections, then we need
13531      an entry per section, since we can't rely on reachability.  */
13532   if (in_text_section ())
13533     {
13534       thumb_call_reg_needed = 1;
13535
13536       if (thumb_call_via_label[regno] == NULL)
13537         thumb_call_via_label[regno] = gen_label_rtx ();
13538       labelp = thumb_call_via_label + regno;
13539     }
13540   else
13541     {
13542       if (cfun->machine->call_via[regno] == NULL)
13543         cfun->machine->call_via[regno] = gen_label_rtx ();
13544       labelp = cfun->machine->call_via + regno;
13545     }
13546
13547   output_asm_insn ("bl\t%a0", labelp);
13548   return "";
13549 }
13550
13551 /* Routines for generating rtl.  */
13552 void
13553 thumb_expand_movmemqi (rtx *operands)
13554 {
13555   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13556   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13557   HOST_WIDE_INT len = INTVAL (operands[2]);
13558   HOST_WIDE_INT offset = 0;
13559
13560   while (len >= 12)
13561     {
13562       emit_insn (gen_movmem12b (out, in, out, in));
13563       len -= 12;
13564     }
13565
13566   if (len >= 8)
13567     {
13568       emit_insn (gen_movmem8b (out, in, out, in));
13569       len -= 8;
13570     }
13571
13572   if (len >= 4)
13573     {
13574       rtx reg = gen_reg_rtx (SImode);
13575       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13576       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13577       len -= 4;
13578       offset += 4;
13579     }
13580
13581   if (len >= 2)
13582     {
13583       rtx reg = gen_reg_rtx (HImode);
13584       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13585                                               plus_constant (in, offset))));
13586       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13587                             reg));
13588       len -= 2;
13589       offset += 2;
13590     }
13591
13592   if (len)
13593     {
13594       rtx reg = gen_reg_rtx (QImode);
13595       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13596                                               plus_constant (in, offset))));
13597       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13598                             reg));
13599     }
13600 }
13601
13602 void
13603 thumb_reload_out_hi (rtx *operands)
13604 {
13605   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13606 }
13607
13608 /* Handle reading a half-word from memory during reload.  */
13609 void
13610 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13611 {
13612   abort ();
13613 }
13614
13615 /* Return the length of a function name prefix
13616     that starts with the character 'c'.  */
13617 static int
13618 arm_get_strip_length (int c)
13619 {
13620   switch (c)
13621     {
13622     ARM_NAME_ENCODING_LENGTHS
13623       default: return 0;
13624     }
13625 }
13626
13627 /* Return a pointer to a function's name with any
13628    and all prefix encodings stripped from it.  */
13629 const char *
13630 arm_strip_name_encoding (const char *name)
13631 {
13632   int skip;
13633
13634   while ((skip = arm_get_strip_length (* name)))
13635     name += skip;
13636
13637   return name;
13638 }
13639
13640 /* If there is a '*' anywhere in the name's prefix, then
13641    emit the stripped name verbatim, otherwise prepend an
13642    underscore if leading underscores are being used.  */
13643 void
13644 arm_asm_output_labelref (FILE *stream, const char *name)
13645 {
13646   int skip;
13647   int verbatim = 0;
13648
13649   while ((skip = arm_get_strip_length (* name)))
13650     {
13651       verbatim |= (*name == '*');
13652       name += skip;
13653     }
13654
13655   if (verbatim)
13656     fputs (name, stream);
13657   else
13658     asm_fprintf (stream, "%U%s", name);
13659 }
13660
13661 static void
13662 arm_file_end (void)
13663 {
13664   int regno;
13665
13666   if (! thumb_call_reg_needed)
13667     return;
13668
13669   text_section ();
13670   asm_fprintf (asm_out_file, "\t.code 16\n");
13671   ASM_OUTPUT_ALIGN (asm_out_file, 1);
13672
13673   for (regno = 0; regno < LR_REGNUM; regno++)
13674     {
13675       rtx label = thumb_call_via_label[regno];
13676
13677       if (label != 0)
13678         {
13679           targetm.asm_out.internal_label (asm_out_file, "L",
13680                                           CODE_LABEL_NUMBER (label));
13681           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
13682         }
13683     }
13684 }
13685
13686 rtx aof_pic_label;
13687
13688 #ifdef AOF_ASSEMBLER
13689 /* Special functions only needed when producing AOF syntax assembler.  */
13690
13691 struct pic_chain
13692 {
13693   struct pic_chain * next;
13694   const char * symname;
13695 };
13696
13697 static struct pic_chain * aof_pic_chain = NULL;
13698
13699 rtx
13700 aof_pic_entry (rtx x)
13701 {
13702   struct pic_chain ** chainp;
13703   int offset;
13704
13705   if (aof_pic_label == NULL_RTX)
13706     {
13707       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13708     }
13709
13710   for (offset = 0, chainp = &aof_pic_chain; *chainp;
13711        offset += 4, chainp = &(*chainp)->next)
13712     if ((*chainp)->symname == XSTR (x, 0))
13713       return plus_constant (aof_pic_label, offset);
13714
13715   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13716   (*chainp)->next = NULL;
13717   (*chainp)->symname = XSTR (x, 0);
13718   return plus_constant (aof_pic_label, offset);
13719 }
13720
13721 void
13722 aof_dump_pic_table (FILE *f)
13723 {
13724   struct pic_chain * chain;
13725
13726   if (aof_pic_chain == NULL)
13727     return;
13728
13729   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13730                PIC_OFFSET_TABLE_REGNUM,
13731                PIC_OFFSET_TABLE_REGNUM);
13732   fputs ("|x$adcons|\n", f);
13733
13734   for (chain = aof_pic_chain; chain; chain = chain->next)
13735     {
13736       fputs ("\tDCD\t", f);
13737       assemble_name (f, chain->symname);
13738       fputs ("\n", f);
13739     }
13740 }
13741
13742 int arm_text_section_count = 1;
13743
13744 char *
13745 aof_text_section (void )
13746 {
13747   static char buf[100];
13748   sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13749            arm_text_section_count++);
13750   if (flag_pic)
13751     strcat (buf, ", PIC, REENTRANT");
13752   return buf;
13753 }
13754
13755 static int arm_data_section_count = 1;
13756
13757 char *
13758 aof_data_section (void)
13759 {
13760   static char buf[100];
13761   sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13762   return buf;
13763 }
13764
13765 /* The AOF assembler is religiously strict about declarations of
13766    imported and exported symbols, so that it is impossible to declare
13767    a function as imported near the beginning of the file, and then to
13768    export it later on.  It is, however, possible to delay the decision
13769    until all the functions in the file have been compiled.  To get
13770    around this, we maintain a list of the imports and exports, and
13771    delete from it any that are subsequently defined.  At the end of
13772    compilation we spit the remainder of the list out before the END
13773    directive.  */
13774
13775 struct import
13776 {
13777   struct import * next;
13778   const char * name;
13779 };
13780
13781 static struct import * imports_list = NULL;
13782
13783 void
13784 aof_add_import (const char *name)
13785 {
13786   struct import * new;
13787
13788   for (new = imports_list; new; new = new->next)
13789     if (new->name == name)
13790       return;
13791
13792   new = (struct import *) xmalloc (sizeof (struct import));
13793   new->next = imports_list;
13794   imports_list = new;
13795   new->name = name;
13796 }
13797
13798 void
13799 aof_delete_import (const char *name)
13800 {
13801   struct import ** old;
13802
13803   for (old = &imports_list; *old; old = & (*old)->next)
13804     {
13805       if ((*old)->name == name)
13806         {
13807           *old = (*old)->next;
13808           return;
13809         }
13810     }
13811 }
13812
13813 int arm_main_function = 0;
13814
13815 static void
13816 aof_dump_imports (FILE *f)
13817 {
13818   /* The AOF assembler needs this to cause the startup code to be extracted
13819      from the library.  Brining in __main causes the whole thing to work
13820      automagically.  */
13821   if (arm_main_function)
13822     {
13823       text_section ();
13824       fputs ("\tIMPORT __main\n", f);
13825       fputs ("\tDCD __main\n", f);
13826     }
13827
13828   /* Now dump the remaining imports.  */
13829   while (imports_list)
13830     {
13831       fprintf (f, "\tIMPORT\t");
13832       assemble_name (f, imports_list->name);
13833       fputc ('\n', f);
13834       imports_list = imports_list->next;
13835     }
13836 }
13837
13838 static void
13839 aof_globalize_label (FILE *stream, const char *name)
13840 {
13841   default_globalize_label (stream, name);
13842   if (! strcmp (name, "main"))
13843     arm_main_function = 1;
13844 }
13845
13846 static void
13847 aof_file_start (void)
13848 {
13849   fputs ("__r0\tRN\t0\n", asm_out_file);
13850   fputs ("__a1\tRN\t0\n", asm_out_file);
13851   fputs ("__a2\tRN\t1\n", asm_out_file);
13852   fputs ("__a3\tRN\t2\n", asm_out_file);
13853   fputs ("__a4\tRN\t3\n", asm_out_file);
13854   fputs ("__v1\tRN\t4\n", asm_out_file);
13855   fputs ("__v2\tRN\t5\n", asm_out_file);
13856   fputs ("__v3\tRN\t6\n", asm_out_file);
13857   fputs ("__v4\tRN\t7\n", asm_out_file);
13858   fputs ("__v5\tRN\t8\n", asm_out_file);
13859   fputs ("__v6\tRN\t9\n", asm_out_file);
13860   fputs ("__sl\tRN\t10\n", asm_out_file);
13861   fputs ("__fp\tRN\t11\n", asm_out_file);
13862   fputs ("__ip\tRN\t12\n", asm_out_file);
13863   fputs ("__sp\tRN\t13\n", asm_out_file);
13864   fputs ("__lr\tRN\t14\n", asm_out_file);
13865   fputs ("__pc\tRN\t15\n", asm_out_file);
13866   fputs ("__f0\tFN\t0\n", asm_out_file);
13867   fputs ("__f1\tFN\t1\n", asm_out_file);
13868   fputs ("__f2\tFN\t2\n", asm_out_file);
13869   fputs ("__f3\tFN\t3\n", asm_out_file);
13870   fputs ("__f4\tFN\t4\n", asm_out_file);
13871   fputs ("__f5\tFN\t5\n", asm_out_file);
13872   fputs ("__f6\tFN\t6\n", asm_out_file);
13873   fputs ("__f7\tFN\t7\n", asm_out_file);
13874   text_section ();
13875 }
13876
13877 static void
13878 aof_file_end (void)
13879 {
13880   if (flag_pic)
13881     aof_dump_pic_table (asm_out_file);
13882   arm_file_end ();
13883   aof_dump_imports (asm_out_file);
13884   fputs ("\tEND\n", asm_out_file);
13885 }
13886 #endif /* AOF_ASSEMBLER */
13887
13888 #ifndef ARM_PE
13889 /* Symbols in the text segment can be accessed without indirecting via the
13890    constant pool; it may take an extra binary operation, but this is still
13891    faster than indirecting via memory.  Don't do this when not optimizing,
13892    since we won't be calculating al of the offsets necessary to do this
13893    simplification.  */
13894
13895 static void
13896 arm_encode_section_info (tree decl, rtx rtl, int first)
13897 {
13898   /* This doesn't work with AOF syntax, since the string table may be in
13899      a different AREA.  */
13900 #ifndef AOF_ASSEMBLER
13901   if (optimize > 0 && TREE_CONSTANT (decl))
13902     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
13903 #endif
13904
13905   /* If we are referencing a function that is weak then encode a long call
13906      flag in the function name, otherwise if the function is static or
13907      or known to be defined in this file then encode a short call flag.  */
13908   if (first && DECL_P (decl))
13909     {
13910       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
13911         arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
13912       else if (! TREE_PUBLIC (decl))
13913         arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
13914     }
13915 }
13916 #endif /* !ARM_PE */
13917
13918 static void
13919 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
13920 {
13921   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
13922       && !strcmp (prefix, "L"))
13923     {
13924       arm_ccfsm_state = 0;
13925       arm_target_insn = NULL;
13926     }
13927   default_internal_label (stream, prefix, labelno);
13928 }
13929
13930 /* Output code to add DELTA to the first argument, and then jump
13931    to FUNCTION.  Used for C++ multiple inheritance.  */
13932 static void
13933 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
13934                      HOST_WIDE_INT delta,
13935                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
13936                      tree function)
13937 {
13938   static int thunk_label = 0;
13939   char label[256];
13940   int mi_delta = delta;
13941   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
13942   int shift = 0;
13943   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
13944                     ? 1 : 0);
13945   if (mi_delta < 0)
13946     mi_delta = - mi_delta;
13947   if (TARGET_THUMB)
13948     {
13949       int labelno = thunk_label++;
13950       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
13951       fputs ("\tldr\tr12, ", file);
13952       assemble_name (file, label);
13953       fputc ('\n', file);
13954     }
13955   while (mi_delta != 0)
13956     {
13957       if ((mi_delta & (3 << shift)) == 0)
13958         shift += 2;
13959       else
13960         {
13961           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
13962                        mi_op, this_regno, this_regno,
13963                        mi_delta & (0xff << shift));
13964           mi_delta &= ~(0xff << shift);
13965           shift += 8;
13966         }
13967     }
13968   if (TARGET_THUMB)
13969     {
13970       fprintf (file, "\tbx\tr12\n");
13971       ASM_OUTPUT_ALIGN (file, 2);
13972       assemble_name (file, label);
13973       fputs (":\n", file);
13974       assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
13975     }
13976   else
13977     {
13978       fputs ("\tb\t", file);
13979       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
13980       if (NEED_PLT_RELOC)
13981         fputs ("(PLT)", file);
13982       fputc ('\n', file);
13983     }
13984 }
13985
13986 int
13987 arm_emit_vector_const (FILE *file, rtx x)
13988 {
13989   int i;
13990   const char * pattern;
13991
13992   if (GET_CODE (x) != CONST_VECTOR)
13993     abort ();
13994
13995   switch (GET_MODE (x))
13996     {
13997     case V2SImode: pattern = "%08x"; break;
13998     case V4HImode: pattern = "%04x"; break;
13999     case V8QImode: pattern = "%02x"; break;
14000     default:       abort ();
14001     }
14002
14003   fprintf (file, "0x");
14004   for (i = CONST_VECTOR_NUNITS (x); i--;)
14005     {
14006       rtx element;
14007
14008       element = CONST_VECTOR_ELT (x, i);
14009       fprintf (file, pattern, INTVAL (element));
14010     }
14011
14012   return 1;
14013 }
14014
14015 const char *
14016 arm_output_load_gr (rtx *operands)
14017 {
14018   rtx reg;
14019   rtx offset;
14020   rtx wcgr;
14021   rtx sum;
14022
14023   if (GET_CODE (operands [1]) != MEM
14024       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14025       || GET_CODE (reg = XEXP (sum, 0)) != REG
14026       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14027       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14028     return "wldrw%?\t%0, %1";
14029
14030   /* Fix up an out-of-range load of a GR register.  */
14031   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14032   wcgr = operands[0];
14033   operands[0] = reg;
14034   output_asm_insn ("ldr%?\t%0, %1", operands);
14035
14036   operands[0] = wcgr;
14037   operands[1] = reg;
14038   output_asm_insn ("tmcr%?\t%0, %1", operands);
14039   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14040
14041   return "";
14042 }
14043
14044 static rtx
14045 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14046                       int incoming ATTRIBUTE_UNUSED)
14047 {
14048 #if 0
14049   /* FIXME: The ARM backend has special code to handle structure
14050          returns, and will reserve its own hidden first argument.  So
14051          if this macro is enabled a *second* hidden argument will be
14052          reserved, which will break binary compatibility with old
14053          toolchains and also thunk handling.  One day this should be
14054          fixed.  */
14055   return 0;
14056 #else
14057   /* Register in which address to store a structure value
14058      is passed to a function.  */
14059   return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14060 #endif
14061 }
14062
14063 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14064
14065    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14066    named arg and all anonymous args onto the stack.
14067    XXX I know the prologue shouldn't be pushing registers, but it is faster
14068    that way.  */
14069
14070 static void
14071 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14072                             enum machine_mode mode ATTRIBUTE_UNUSED,
14073                             tree type ATTRIBUTE_UNUSED,
14074                             int *pretend_size,
14075                             int second_time ATTRIBUTE_UNUSED)
14076 {
14077   cfun->machine->uses_anonymous_args = 1;
14078   if (cum->nregs < NUM_ARG_REGS)
14079     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14080 }
14081
14082 /* Return nonzero if the CONSUMER instruction (a store) does not need
14083    PRODUCER's value to calculate the address.  */
14084
14085 int
14086 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14087 {
14088   rtx value = PATTERN (producer);
14089   rtx addr = PATTERN (consumer);
14090
14091   if (GET_CODE (value) == COND_EXEC)
14092     value = COND_EXEC_CODE (value);
14093   if (GET_CODE (value) == PARALLEL)
14094     value = XVECEXP (value, 0, 0);
14095   value = XEXP (value, 0);
14096   if (GET_CODE (addr) == COND_EXEC)
14097     addr = COND_EXEC_CODE (addr);
14098   if (GET_CODE (addr) == PARALLEL)
14099     addr = XVECEXP (addr, 0, 0);
14100   addr = XEXP (addr, 0);
14101
14102   return !reg_overlap_mentioned_p (value, addr);
14103 }
14104
14105 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14106    have an early register shift value or amount dependency on the
14107    result of PRODUCER.  */
14108
14109 int
14110 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14111 {
14112   rtx value = PATTERN (producer);
14113   rtx op = PATTERN (consumer);
14114   rtx early_op;
14115
14116   if (GET_CODE (value) == COND_EXEC)
14117     value = COND_EXEC_CODE (value);
14118   if (GET_CODE (value) == PARALLEL)
14119     value = XVECEXP (value, 0, 0);
14120   value = XEXP (value, 0);
14121   if (GET_CODE (op) == COND_EXEC)
14122     op = COND_EXEC_CODE (op);
14123   if (GET_CODE (op) == PARALLEL)
14124     op = XVECEXP (op, 0, 0);
14125   op = XEXP (op, 1);
14126
14127   early_op = XEXP (op, 0);
14128   /* This is either an actual independent shift, or a shift applied to
14129      the first operand of another operation.  We want the whole shift
14130      operation.  */
14131   if (GET_CODE (early_op) == REG)
14132     early_op = op;
14133
14134   return !reg_overlap_mentioned_p (value, early_op);
14135 }
14136
14137 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14138    have an early register shift value dependency on the result of
14139    PRODUCER.  */
14140
14141 int
14142 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14143 {
14144   rtx value = PATTERN (producer);
14145   rtx op = PATTERN (consumer);
14146   rtx early_op;
14147
14148   if (GET_CODE (value) == COND_EXEC)
14149     value = COND_EXEC_CODE (value);
14150   if (GET_CODE (value) == PARALLEL)
14151     value = XVECEXP (value, 0, 0);
14152   value = XEXP (value, 0);
14153   if (GET_CODE (op) == COND_EXEC)
14154     op = COND_EXEC_CODE (op);
14155   if (GET_CODE (op) == PARALLEL)
14156     op = XVECEXP (op, 0, 0);
14157   op = XEXP (op, 1);
14158
14159   early_op = XEXP (op, 0);
14160
14161   /* This is either an actual independent shift, or a shift applied to
14162      the first operand of another operation.  We want the value being
14163      shifted, in either case.  */
14164   if (GET_CODE (early_op) != REG)
14165     early_op = XEXP (early_op, 0);
14166
14167   return !reg_overlap_mentioned_p (value, early_op);
14168 }
14169
14170 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14171    have an early register mult dependency on the result of
14172    PRODUCER.  */
14173
14174 int
14175 arm_no_early_mul_dep (rtx producer, rtx consumer)
14176 {
14177   rtx value = PATTERN (producer);
14178   rtx op = PATTERN (consumer);
14179
14180   if (GET_CODE (value) == COND_EXEC)
14181     value = COND_EXEC_CODE (value);
14182   if (GET_CODE (value) == PARALLEL)
14183     value = XVECEXP (value, 0, 0);
14184   value = XEXP (value, 0);
14185   if (GET_CODE (op) == COND_EXEC)
14186     op = COND_EXEC_CODE (op);
14187   if (GET_CODE (op) == PARALLEL)
14188     op = XVECEXP (op, 0, 0);
14189   op = XEXP (op, 1);
14190
14191   return (GET_CODE (op) == PLUS
14192           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14193 }
14194
14195
14196 /* We can't rely on the caller doing the proper promotion when
14197    using APCS or ATPCS.  */
14198
14199 static bool
14200 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14201 {
14202     return !TARGET_AAPCS_BASED;
14203 }
14204
14205
14206 /* AAPCS based ABIs use short enums by default.  */
14207
14208 static bool
14209 arm_default_short_enums (void)
14210 {
14211   return TARGET_AAPCS_BASED;
14212 }
14213
14214
14215 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
14216
14217 static bool
14218 arm_align_anon_bitfield (void)
14219 {
14220   return TARGET_AAPCS_BASED;
14221 }
14222
14223
14224 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
14225
14226 static tree
14227 arm_cxx_guard_type (void)
14228 {
14229   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14230 }
14231
14232
14233 /* The EABI says test the least significan bit of a guard variable.  */
14234
14235 static bool
14236 arm_cxx_guard_mask_bit (void)
14237 {
14238   return TARGET_AAPCS_BASED;
14239 }
14240
14241
14242 /* The EABI specifies that all array cookies are 8 bytes long.  */
14243
14244 static tree
14245 arm_get_cookie_size (tree type)
14246 {
14247   tree size;
14248
14249   if (!TARGET_AAPCS_BASED)
14250     return default_cxx_get_cookie_size (type);
14251
14252   size = build_int_cst (sizetype, 8);
14253   return size;
14254 }
14255
14256
14257 /* The EABI says that array cookies should also contain the element size.  */
14258
14259 static bool
14260 arm_cookie_has_size (void)
14261 {
14262   return TARGET_AAPCS_BASED;
14263 }
14264
14265
14266 /* The EABI says constructors and destructors should return a pointer to
14267    the object constructed/destroyed.  */
14268
14269 static bool
14270 arm_cxx_cdtor_returns_this (void)
14271 {
14272   return TARGET_AAPCS_BASED;
14273 }
14274
14275 /* The EABI says that an inline function may never be the key
14276    method.  */
14277
14278 static bool
14279 arm_cxx_key_method_may_be_inline (void)
14280 {
14281   return !TARGET_AAPCS_BASED;
14282 }
14283
14284 /* The EABI says that the virtual table, etc., for a class must be
14285    exported if it has a key method.  The EABI does not specific the
14286    behavior if there is no key method, but there is no harm in
14287    exporting the class data in that case too.  */
14288
14289 static bool
14290 arm_cxx_export_class_data (void)
14291 {
14292   return TARGET_AAPCS_BASED;
14293 }
14294
14295 void
14296 arm_set_return_address (rtx source, rtx scratch)
14297 {
14298   arm_stack_offsets *offsets;
14299   HOST_WIDE_INT delta;
14300   rtx addr;
14301   unsigned long saved_regs;
14302
14303   saved_regs = arm_compute_save_reg_mask ();
14304
14305   if ((saved_regs & (1 << LR_REGNUM)) == 0)
14306     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14307   else
14308     {
14309       if (frame_pointer_needed)
14310         addr = plus_constant(hard_frame_pointer_rtx, -4);
14311       else
14312         {
14313           /* LR will be the first saved register.  */
14314           offsets = arm_get_frame_offsets ();
14315           delta = offsets->outgoing_args - (offsets->frame + 4);
14316
14317
14318           if (delta >= 4096)
14319             {
14320               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
14321                                      GEN_INT (delta & ~4095)));
14322               addr = scratch;
14323               delta &= 4095;
14324             }
14325           else
14326             addr = stack_pointer_rtx;
14327
14328           addr = plus_constant (addr, delta);
14329         }
14330       emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14331     }
14332 }
14333
14334
14335 void
14336 thumb_set_return_address (rtx source, rtx scratch)
14337 {
14338   arm_stack_offsets *offsets;
14339   HOST_WIDE_INT delta;
14340   int reg;
14341   rtx addr;
14342   unsigned long mask;
14343
14344   emit_insn (gen_rtx_USE (VOIDmode, source));
14345
14346   mask = thumb_compute_save_reg_mask ();
14347   if (mask & (1 << LR_REGNUM))
14348     {
14349       offsets = arm_get_frame_offsets ();
14350
14351       /* Find the saved regs.  */
14352       if (frame_pointer_needed)
14353         {
14354           delta = offsets->soft_frame - offsets->saved_args;
14355           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
14356         }
14357       else
14358         {
14359           delta = offsets->outgoing_args - offsets->saved_args;
14360           reg = SP_REGNUM;
14361         }
14362       /* Allow for the stack frame.  */
14363       if (TARGET_BACKTRACE)
14364         delta -= 16;
14365       /* The link register is always the first saved register.  */
14366       delta -= 4;
14367
14368       /* Construct the address.  */
14369       addr = gen_rtx_REG (SImode, reg);
14370       if ((reg != SP_REGNUM && delta >= 128)
14371           || delta >= 1024)
14372         {
14373           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
14374           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
14375           addr = scratch;
14376         }
14377       else
14378         addr = plus_constant (addr, delta);
14379
14380       emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14381     }
14382   else
14383     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14384 }
14385
14386 /* Implements target hook vector_mode_supported_p.  */
14387 bool
14388 arm_vector_mode_supported_p (enum machine_mode mode)
14389 {
14390   if ((mode == V2SImode)
14391       || (mode == V4HImode)
14392       || (mode == V8QImode))
14393     return true;
14394
14395   return false;
14396 }
14397
14398 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
14399    ARM insns and therefore guarantee that the shift count is modulo 256.
14400    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
14401    guarantee no particular behavior for out-of-range counts.  */
14402
14403 static unsigned HOST_WIDE_INT
14404 arm_shift_truncation_mask (enum machine_mode mode)
14405 {
14406   return mode == SImode ? 255 : 0;
14407 }
14408
14409
14410 /* Map internal gcc register numbers to DWARF2 register numbers.  */
14411
14412 unsigned int
14413 arm_dbx_register_number (unsigned int regno)
14414 {
14415   if (regno < 16)
14416     return regno;
14417
14418   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
14419      compatibility.  The EABI defines them as registers 96-103.  */
14420   if (IS_FPA_REGNUM (regno))
14421     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
14422
14423   if (IS_VFP_REGNUM (regno))
14424     return 64 + regno - FIRST_VFP_REGNUM;
14425
14426   if (IS_IWMMXT_GR_REGNUM (regno))
14427     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
14428
14429   if (IS_IWMMXT_REGNUM (regno))
14430     return 112 + regno - FIRST_IWMMXT_REGNUM;
14431
14432   abort ();
14433 }
14434