OSDN Git Service

* config/darwin.c, config/alpha/alpha.h, config/arm/arm.c,
[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  Free Software Foundation, Inc.
4    Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5    and Martin Simmons (@harleqn.co.uk).
6    More major hacks by Richard Earnshaw (rearnsha@arm.com).
7
8    This file is part of GCC.
9
10    GCC is free software; you can redistribute it and/or modify it
11    under the terms of the GNU General Public License as published
12    by the Free Software Foundation; either version 2, or (at your
13    option) any later version.
14
15    GCC is distributed in the hope that it will be useful, but WITHOUT
16    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18    License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with GCC; see the file COPYING.  If not, write to
22    the Free Software Foundation, 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, USA.  */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "obstack.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "output.h"
38 #include "insn-attr.h"
39 #include "flags.h"
40 #include "reload.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "optabs.h"
44 #include "toplev.h"
45 #include "recog.h"
46 #include "ggc.h"
47 #include "except.h"
48 #include "c-pragma.h"
49 #include "integrate.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "debug.h"
54 #include "langhooks.h"
55
56 /* Forward definitions of types.  */
57 typedef struct minipool_node    Mnode;
58 typedef struct minipool_fixup   Mfix;
59
60 const struct attribute_spec arm_attribute_table[];
61
62 /* Forward function declarations.  */
63 static arm_stack_offsets *arm_get_frame_offsets (void);
64 static void arm_add_gc_roots (void);
65 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
66                              HOST_WIDE_INT, rtx, rtx, int, int);
67 static unsigned bit_count (unsigned long);
68 static int arm_address_register_rtx_p (rtx, int);
69 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
70 static int thumb_base_register_rtx_p (rtx, enum machine_mode, int);
71 inline static int thumb_index_register_rtx_p (rtx, int);
72 static int thumb_far_jump_used_p (void);
73 static bool thumb_force_lr_save (void);
74 static unsigned long thumb_compute_save_reg_mask (void);
75 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
76 static rtx emit_multi_reg_push (int);
77 static rtx emit_sfm (int, int);
78 #ifndef AOF_ASSEMBLER
79 static bool arm_assemble_integer (rtx, unsigned int, int);
80 #endif
81 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
82 static arm_cc get_arm_condition_code (rtx);
83 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
84 static rtx is_jump_table (rtx);
85 static const char *output_multi_immediate (rtx *, const char *, const char *,
86                                            int, HOST_WIDE_INT);
87 static void print_multi_reg (FILE *, const char *, int, int);
88 static const char *shift_op (rtx, HOST_WIDE_INT *);
89 static struct machine_function *arm_init_machine_status (void);
90 static int number_of_first_bit_set (int);
91 static void replace_symbols_in_block (tree, rtx, rtx);
92 static void thumb_exit (FILE *, int);
93 static void thumb_pushpop (FILE *, int, int, int *, int);
94 static rtx is_jump_table (rtx);
95 static HOST_WIDE_INT get_jump_table_size (rtx);
96 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
97 static Mnode *add_minipool_forward_ref (Mfix *);
98 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
99 static Mnode *add_minipool_backward_ref (Mfix *);
100 static void assign_minipool_offsets (Mfix *);
101 static void arm_print_value (FILE *, rtx);
102 static void dump_minipool (rtx);
103 static int arm_barrier_cost (rtx);
104 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
105 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
106 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
107                                rtx);
108 static void arm_reorg (void);
109 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
110 static int current_file_function_operand (rtx);
111 static unsigned long arm_compute_save_reg0_reg12_mask (void);
112 static unsigned long arm_compute_save_reg_mask (void);
113 static unsigned long arm_isr_value (tree);
114 static unsigned long arm_compute_func_type (void);
115 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
116 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
117 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
118 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
119 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
120 static int arm_comp_type_attributes (tree, tree);
121 static void arm_set_default_type_attributes (tree);
122 static int arm_adjust_cost (rtx, rtx, rtx, int);
123 static int count_insns_for_constant (HOST_WIDE_INT, int);
124 static int arm_get_strip_length (int);
125 static bool arm_function_ok_for_sibcall (tree, tree);
126 static void arm_internal_label (FILE *, const char *, unsigned long);
127 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
128                                  tree);
129 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
130 static bool arm_size_rtx_costs (rtx, int, int, int *);
131 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
132 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
133 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
134 static bool arm_9e_rtx_costs (rtx, int, int, int *);
135 static int arm_address_cost (rtx);
136 static bool arm_memory_load_p (rtx);
137 static bool arm_cirrus_insn_p (rtx);
138 static void cirrus_reorg (rtx);
139 static void arm_init_builtins (void);
140 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
141 static void arm_init_iwmmxt_builtins (void);
142 static rtx safe_vector_operand (rtx, enum machine_mode);
143 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
144 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
145 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
146 static void emit_constant_insn (rtx cond, rtx pattern);
147
148 #ifdef OBJECT_FORMAT_ELF
149 static void arm_elf_asm_named_section (const char *, unsigned int);
150 #endif
151 #ifndef ARM_PE
152 static void arm_encode_section_info (tree, rtx, int);
153 #endif
154 #ifdef AOF_ASSEMBLER
155 static void aof_globalize_label (FILE *, const char *);
156 static void aof_dump_imports (FILE *);
157 static void aof_dump_pic_table (FILE *);
158 static void aof_file_start (void);
159 static void aof_file_end (void);
160 #endif
161 static rtx arm_struct_value_rtx (tree, int);
162 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
163                                         tree, int *, int);
164 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
165                                    enum machine_mode, tree, bool);
166 static bool arm_promote_prototypes (tree);
167 static bool arm_default_short_enums (void);
168 static bool arm_align_anon_bitfield (void);
169
170 static tree arm_cxx_guard_type (void);
171 static bool arm_cxx_guard_mask_bit (void);
172 static tree arm_get_cookie_size (tree);
173 static bool arm_cookie_has_size (void);
174 static bool arm_cxx_cdtor_returns_this (void);
175 static bool arm_cxx_key_method_may_be_inline (void);
176 static bool arm_cxx_export_class_data (void);
177 static void arm_init_libfuncs (void);
178 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
179 \f
180 /* Initialize the GCC target structure.  */
181 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
182 #undef  TARGET_MERGE_DECL_ATTRIBUTES
183 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
184 #endif
185
186 #undef  TARGET_ATTRIBUTE_TABLE
187 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
188
189 #ifdef AOF_ASSEMBLER
190 #undef  TARGET_ASM_BYTE_OP
191 #define TARGET_ASM_BYTE_OP "\tDCB\t"
192 #undef  TARGET_ASM_ALIGNED_HI_OP
193 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
194 #undef  TARGET_ASM_ALIGNED_SI_OP
195 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
196 #undef TARGET_ASM_GLOBALIZE_LABEL
197 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
198 #undef TARGET_ASM_FILE_START
199 #define TARGET_ASM_FILE_START aof_file_start
200 #undef TARGET_ASM_FILE_END
201 #define TARGET_ASM_FILE_END aof_file_end
202 #else
203 #undef  TARGET_ASM_ALIGNED_SI_OP
204 #define TARGET_ASM_ALIGNED_SI_OP NULL
205 #undef  TARGET_ASM_INTEGER
206 #define TARGET_ASM_INTEGER arm_assemble_integer
207 #endif
208
209 #undef  TARGET_ASM_FUNCTION_PROLOGUE
210 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
211
212 #undef  TARGET_ASM_FUNCTION_EPILOGUE
213 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
214
215 #undef  TARGET_COMP_TYPE_ATTRIBUTES
216 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
217
218 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
219 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
220
221 #undef  TARGET_SCHED_ADJUST_COST
222 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
223
224 #undef TARGET_ENCODE_SECTION_INFO
225 #ifdef ARM_PE
226 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
227 #else
228 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
229 #endif
230
231 #undef  TARGET_STRIP_NAME_ENCODING
232 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
233
234 #undef  TARGET_ASM_INTERNAL_LABEL
235 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
236
237 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
238 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
239
240 #undef  TARGET_ASM_OUTPUT_MI_THUNK
241 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
242 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
243 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
244
245 /* This will be overridden in arm_override_options.  */
246 #undef  TARGET_RTX_COSTS
247 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
248 #undef  TARGET_ADDRESS_COST
249 #define TARGET_ADDRESS_COST arm_address_cost
250
251 #undef TARGET_SHIFT_TRUNCATION_MASK
252 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
253 #undef TARGET_VECTOR_MODE_SUPPORTED_P
254 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
255
256 #undef  TARGET_MACHINE_DEPENDENT_REORG
257 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
258
259 #undef  TARGET_INIT_BUILTINS
260 #define TARGET_INIT_BUILTINS  arm_init_builtins
261 #undef  TARGET_EXPAND_BUILTIN
262 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
263
264 #undef TARGET_INIT_LIBFUNCS
265 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
266
267 #undef TARGET_PROMOTE_FUNCTION_ARGS
268 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
269 #undef TARGET_PROMOTE_FUNCTION_RETURN
270 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
271 #undef TARGET_PROMOTE_PROTOTYPES
272 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
273 #undef TARGET_PASS_BY_REFERENCE
274 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
275
276 #undef TARGET_STRUCT_VALUE_RTX
277 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
278
279 #undef  TARGET_SETUP_INCOMING_VARARGS
280 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
281
282 #undef TARGET_DEFAULT_SHORT_ENUMS
283 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
284
285 #undef TARGET_ALIGN_ANON_BITFIELD
286 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
287
288 #undef TARGET_CXX_GUARD_TYPE
289 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
290
291 #undef TARGET_CXX_GUARD_MASK_BIT
292 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
293
294 #undef TARGET_CXX_GET_COOKIE_SIZE
295 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
296
297 #undef TARGET_CXX_COOKIE_HAS_SIZE
298 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
299
300 #undef TARGET_CXX_CDTOR_RETURNS_THIS
301 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
302
303 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
304 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
305
306 #undef TARGET_CXX_EXPORT_CLASS_DATA
307 #define TARGET_CXX_EXPORT_CLASS_DATA arm_cxx_export_class_data
308
309 struct gcc_target targetm = TARGET_INITIALIZER;
310 \f
311 /* Obstack for minipool constant handling.  */
312 static struct obstack minipool_obstack;
313 static char *         minipool_startobj;
314
315 /* The maximum number of insns skipped which
316    will be conditionalised if possible.  */
317 static int max_insns_skipped = 5;
318
319 extern FILE * asm_out_file;
320
321 /* True if we are currently building a constant table.  */
322 int making_const_table;
323
324 /* Define the information needed to generate branch insns.  This is
325    stored from the compare operation.  */
326 rtx arm_compare_op0, arm_compare_op1;
327
328 /* The processor for which instructions should be scheduled.  */
329 enum processor_type arm_tune = arm_none;
330
331 /* Which floating point model to use.  */
332 enum arm_fp_model arm_fp_model;
333
334 /* Which floating point hardware is available.  */
335 enum fputype arm_fpu_arch;
336
337 /* Which floating point hardware to schedule for.  */
338 enum fputype arm_fpu_tune;
339
340 /* Whether to use floating point hardware.  */
341 enum float_abi_type arm_float_abi;
342
343 /* Which ABI to use.  */
344 enum arm_abi_type arm_abi;
345
346 /* Set by the -mfpu=... option.  */
347 const char * target_fpu_name = NULL;
348
349 /* Set by the -mfpe=... option.  */
350 const char * target_fpe_name = NULL;
351
352 /* Set by the -mfloat-abi=... option.  */
353 const char * target_float_abi_name = NULL;
354
355 /* Set by the -mabi=... option.  */
356 const char * target_abi_name = NULL;
357
358 /* Used to parse -mstructure_size_boundary command line option.  */
359 const char * structure_size_string = NULL;
360 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
361
362 /* Bit values used to identify processor capabilities.  */
363 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
364 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
365 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
366 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
367 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
368 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
369 #define FL_THUMB      (1 << 6)        /* Thumb aware */
370 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
371 #define FL_STRONG     (1 << 8)        /* StrongARM */
372 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
373 #define FL_XSCALE     (1 << 10)       /* XScale */
374 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
375 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
376                                          media instructions.  */
377 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
378
379 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
380
381 #define FL_FOR_ARCH2    0
382 #define FL_FOR_ARCH3    FL_MODE32
383 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
384 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
385 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
386 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
387 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
388 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
389 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
390 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
391 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
392 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
393
394 /* The bits in this mask specify which
395    instructions we are allowed to generate.  */
396 static unsigned long insn_flags = 0;
397
398 /* The bits in this mask specify which instruction scheduling options should
399    be used.  */
400 static unsigned long tune_flags = 0;
401
402 /* The following are used in the arm.md file as equivalents to bits
403    in the above two flag variables.  */
404
405 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
406 int arm_arch3m = 0;
407
408 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
409 int arm_arch4 = 0;
410
411 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
412 int arm_arch4t = 0;
413
414 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
415 int arm_arch5 = 0;
416
417 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
418 int arm_arch5e = 0;
419
420 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
421 int arm_arch6 = 0;
422
423 /* Nonzero if this chip can benefit from load scheduling.  */
424 int arm_ld_sched = 0;
425
426 /* Nonzero if this chip is a StrongARM.  */
427 int arm_is_strong = 0;
428
429 /* Nonzero if this chip is a Cirrus variant.  */
430 int arm_arch_cirrus = 0;
431
432 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
433 int arm_arch_iwmmxt = 0;
434
435 /* Nonzero if this chip is an XScale.  */
436 int arm_arch_xscale = 0;
437
438 /* Nonzero if tuning for XScale  */
439 int arm_tune_xscale = 0;
440
441 /* Nonzero if this chip is an ARM6 or an ARM7.  */
442 int arm_is_6_or_7 = 0;
443
444 /* Nonzero if generating Thumb instructions.  */
445 int thumb_code = 0;
446
447 /* Nonzero if we should define __THUMB_INTERWORK__ in the
448    preprocessor.
449    XXX This is a bit of a hack, it's intended to help work around
450    problems in GLD which doesn't understand that armv5t code is
451    interworking clean.  */
452 int arm_cpp_interwork = 0;
453
454 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
455    must report the mode of the memory reference from PRINT_OPERAND to
456    PRINT_OPERAND_ADDRESS.  */
457 enum machine_mode output_memory_reference_mode;
458
459 /* The register number to be used for the PIC offset register.  */
460 const char * arm_pic_register_string = NULL;
461 int arm_pic_register = INVALID_REGNUM;
462
463 /* Set to 1 when a return insn is output, this means that the epilogue
464    is not needed.  */
465 int return_used_this_function;
466
467 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
468    the next function.  */
469 static int after_arm_reorg = 0;
470
471 /* The maximum number of insns to be used when loading a constant.  */
472 static int arm_constant_limit = 3;
473
474 /* For an explanation of these variables, see final_prescan_insn below.  */
475 int arm_ccfsm_state;
476 enum arm_cond_code arm_current_cc;
477 rtx arm_target_insn;
478 int arm_target_label;
479
480 /* The condition codes of the ARM, and the inverse function.  */
481 static const char * const arm_condition_codes[] =
482 {
483   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
484   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
485 };
486
487 #define streq(string1, string2) (strcmp (string1, string2) == 0)
488 \f
489 /* Initialization code.  */
490
491 struct processors
492 {
493   const char *const name;
494   enum processor_type core;
495   const char *arch;
496   const unsigned long flags;
497   bool (* rtx_costs) (rtx, int, int, int *);
498 };
499
500 /* Not all of these give usefully different compilation alternatives,
501    but there is no simple way of generalizing them.  */
502 static const struct processors all_cores[] =
503 {
504   /* ARM Cores */
505 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
506   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
507 #include "arm-cores.def"
508 #undef ARM_CORE
509   {NULL, arm_none, NULL, 0, NULL}
510 };
511
512 static const struct processors all_architectures[] =
513 {
514   /* ARM Architectures */
515   /* We don't specify rtx_costs here as it will be figured out
516      from the core.  */
517
518   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
519   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
520   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
521   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
522   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
523   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
524      implementations that support it, so we will leave it out for now.  */
525   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
526   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
527   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
528   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
529   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
530   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
531   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
532   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
533   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
534   {NULL, arm_none, NULL, 0 , NULL}
535 };
536
537 /* This is a magic structure.  The 'string' field is magically filled in
538    with a pointer to the value specified by the user on the command line
539    assuming that the user has specified such a value.  */
540
541 struct arm_cpu_select arm_select[] =
542 {
543   /* string       name            processors  */
544   { NULL,       "-mcpu=",       all_cores  },
545   { NULL,       "-march=",      all_architectures },
546   { NULL,       "-mtune=",      all_cores }
547 };
548
549
550 /* The name of the proprocessor macro to define for this architecture.  */
551
552 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
553
554 struct fpu_desc
555 {
556   const char * name;
557   enum fputype fpu;
558 };
559
560
561 /* Available values for for -mfpu=.  */
562
563 static const struct fpu_desc all_fpus[] =
564 {
565   {"fpa",       FPUTYPE_FPA},
566   {"fpe2",      FPUTYPE_FPA_EMU2},
567   {"fpe3",      FPUTYPE_FPA_EMU2},
568   {"maverick",  FPUTYPE_MAVERICK},
569   {"vfp",       FPUTYPE_VFP}
570 };
571
572
573 /* Floating point models used by the different hardware.
574    See fputype in arm.h.  */
575
576 static const enum fputype fp_model_for_fpu[] =
577 {
578   /* No FP hardware.  */
579   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
580   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
581   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
582   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
583   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
584   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
585 };
586
587
588 struct float_abi
589 {
590   const char * name;
591   enum float_abi_type abi_type;
592 };
593
594
595 /* Available values for -mfloat-abi=.  */
596
597 static const struct float_abi all_float_abis[] =
598 {
599   {"soft",      ARM_FLOAT_ABI_SOFT},
600   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
601   {"hard",      ARM_FLOAT_ABI_HARD}
602 };
603
604
605 struct abi_name
606 {
607   const char *name;
608   enum arm_abi_type abi_type;
609 };
610
611
612 /* Available values for -mabi=.  */
613
614 static const struct abi_name arm_all_abis[] =
615 {
616   {"apcs-gnu",    ARM_ABI_APCS},
617   {"atpcs",   ARM_ABI_ATPCS},
618   {"aapcs",   ARM_ABI_AAPCS},
619   {"iwmmxt",  ARM_ABI_IWMMXT}
620 };
621
622 /* Return the number of bits set in VALUE.  */
623 static unsigned
624 bit_count (unsigned long value)
625 {
626   unsigned long count = 0;
627
628   while (value)
629     {
630       count++;
631       value &= value - 1;  /* Clear the least-significant set bit.  */
632     }
633
634   return count;
635 }
636
637 /* Set up library functions unique to ARM.  */
638
639 static void
640 arm_init_libfuncs (void)
641 {
642   /* There are no special library functions unless we are using the
643      ARM BPABI.  */
644   if (!TARGET_BPABI)
645     return;
646
647   /* The functions below are described in Section 4 of the "Run-Time
648      ABI for the ARM architecture", Version 1.0.  */
649
650   /* Double-precision floating-point arithmetic.  Table 2.  */
651   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
652   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
653   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
654   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
655   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
656
657   /* Double-precision comparisons.  Table 3.  */
658   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
659   set_optab_libfunc (ne_optab, DFmode, NULL);
660   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
661   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
662   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
663   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
664   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
665
666   /* Single-precision floating-point arithmetic.  Table 4.  */
667   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
668   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
669   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
670   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
671   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
672
673   /* Single-precision comparisons.  Table 5.  */
674   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
675   set_optab_libfunc (ne_optab, SFmode, NULL);
676   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
677   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
678   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
679   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
680   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
681
682   /* Floating-point to integer conversions.  Table 6.  */
683   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
684   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
685   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
686   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
687   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
688   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
689   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
690   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
691
692   /* Conversions between floating types.  Table 7.  */
693   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
694   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
695
696   /* Integer to floating-point conversions.  Table 8.  */
697   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
698   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
699   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
700   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
701   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
702   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
703   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
704   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
705
706   /* Long long.  Table 9.  */
707   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
708   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
709   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
710   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
711   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
712   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
713   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
714   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
715
716   /* Integer (32/32->32) division.  \S 4.3.1.  */
717   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
718   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
719
720   /* The divmod functions are designed so that they can be used for
721      plain division, even though they return both the quotient and the
722      remainder.  The quotient is returned in the usual location (i.e.,
723      r0 for SImode, {r0, r1} for DImode), just as would be expected
724      for an ordinary division routine.  Because the AAPCS calling
725      conventions specify that all of { r0, r1, r2, r3 } are
726      callee-saved registers, there is no need to tell the compiler
727      explicitly that those registers are clobbered by these
728      routines.  */
729   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
730   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
731   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idivmod");
732   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidivmod");
733 }
734
735 /* Fix up any incompatible options that the user has specified.
736    This has now turned into a maze.  */
737 void
738 arm_override_options (void)
739 {
740   unsigned i;
741
742   /* Set up the flags based on the cpu/architecture selected by the user.  */
743   for (i = ARRAY_SIZE (arm_select); i--;)
744     {
745       struct arm_cpu_select * ptr = arm_select + i;
746
747       if (ptr->string != NULL && ptr->string[0] != '\0')
748         {
749           const struct processors * sel;
750
751           for (sel = ptr->processors; sel->name != NULL; sel++)
752             if (streq (ptr->string, sel->name))
753               {
754                 /* Set the architecture define.  */
755                 if (i != 2)
756                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
757
758                 /* Determine the processor core for which we should
759                    tune code-generation.  */
760                 if (/* -mcpu= is a sensible default.  */
761                     i == 0
762                     /* If -march= is used, and -mcpu= has not been used,
763                        assume that we should tune for a representative
764                        CPU from that architecture.  */
765                     || i == 1
766                     /* -mtune= overrides -mcpu= and -march=.  */
767                     || i == 2)
768                   arm_tune = (enum processor_type) (sel - ptr->processors);
769
770                 if (i != 2)
771                   {
772                     /* If we have been given an architecture and a processor
773                        make sure that they are compatible.  We only generate
774                        a warning though, and we prefer the CPU over the
775                        architecture.  */
776                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
777                       warning ("switch -mcpu=%s conflicts with -march= switch",
778                                ptr->string);
779
780                     insn_flags = sel->flags;
781                   }
782
783                 break;
784               }
785
786           if (sel->name == NULL)
787             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
788         }
789     }
790
791   /* If the user did not specify a processor, choose one for them.  */
792   if (insn_flags == 0)
793     {
794       const struct processors * sel;
795       unsigned int        sought;
796       enum processor_type cpu;
797
798       cpu = TARGET_CPU_DEFAULT;
799       if (cpu == arm_none)
800         {
801 #ifdef SUBTARGET_CPU_DEFAULT
802           /* Use the subtarget default CPU if none was specified by
803              configure.  */
804           cpu = SUBTARGET_CPU_DEFAULT;
805 #endif
806           /* Default to ARM6.  */
807           if (cpu == arm_none)
808             cpu = arm6;
809         }
810       sel = &all_cores[cpu];
811
812       insn_flags = sel->flags;
813
814       /* Now check to see if the user has specified some command line
815          switch that require certain abilities from the cpu.  */
816       sought = 0;
817
818       if (TARGET_INTERWORK || TARGET_THUMB)
819         {
820           sought |= (FL_THUMB | FL_MODE32);
821
822           /* There are no ARM processors that support both APCS-26 and
823              interworking.  Therefore we force FL_MODE26 to be removed
824              from insn_flags here (if it was set), so that the search
825              below will always be able to find a compatible processor.  */
826           insn_flags &= ~FL_MODE26;
827         }
828
829       if (sought != 0 && ((sought & insn_flags) != sought))
830         {
831           /* Try to locate a CPU type that supports all of the abilities
832              of the default CPU, plus the extra abilities requested by
833              the user.  */
834           for (sel = all_cores; sel->name != NULL; sel++)
835             if ((sel->flags & sought) == (sought | insn_flags))
836               break;
837
838           if (sel->name == NULL)
839             {
840               unsigned current_bit_count = 0;
841               const struct processors * best_fit = NULL;
842
843               /* Ideally we would like to issue an error message here
844                  saying that it was not possible to find a CPU compatible
845                  with the default CPU, but which also supports the command
846                  line options specified by the programmer, and so they
847                  ought to use the -mcpu=<name> command line option to
848                  override the default CPU type.
849
850                  If we cannot find a cpu that has both the
851                  characteristics of the default cpu and the given
852                  command line options we scan the array again looking
853                  for a best match.  */
854               for (sel = all_cores; sel->name != NULL; sel++)
855                 if ((sel->flags & sought) == sought)
856                   {
857                     unsigned count;
858
859                     count = bit_count (sel->flags & insn_flags);
860
861                     if (count >= current_bit_count)
862                       {
863                         best_fit = sel;
864                         current_bit_count = count;
865                       }
866                   }
867
868               if (best_fit == NULL)
869                 abort ();
870               else
871                 sel = best_fit;
872             }
873
874           insn_flags = sel->flags;
875         }
876       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
877       if (arm_tune == arm_none)
878         arm_tune = (enum processor_type) (sel - all_cores);
879     }
880
881   /* The processor for which we should tune should now have been
882      chosen.  */
883   if (arm_tune == arm_none)
884     abort ();
885
886   tune_flags = all_cores[(int)arm_tune].flags;
887   if (optimize_size)
888     targetm.rtx_costs = arm_size_rtx_costs;
889   else
890     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
891
892   /* Make sure that the processor choice does not conflict with any of the
893      other command line choices.  */
894   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
895     {
896       warning ("target CPU does not support interworking" );
897       target_flags &= ~ARM_FLAG_INTERWORK;
898     }
899
900   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
901     {
902       warning ("target CPU does not support THUMB instructions");
903       target_flags &= ~ARM_FLAG_THUMB;
904     }
905
906   if (TARGET_APCS_FRAME && TARGET_THUMB)
907     {
908       /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
909       target_flags &= ~ARM_FLAG_APCS_FRAME;
910     }
911
912   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
913      from here where no function is being compiled currently.  */
914   if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
915       && TARGET_ARM)
916     warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
917
918   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
919     warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
920
921   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
922     warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
923
924   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
925     {
926       warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
927       target_flags |= ARM_FLAG_APCS_FRAME;
928     }
929
930   if (TARGET_POKE_FUNCTION_NAME)
931     target_flags |= ARM_FLAG_APCS_FRAME;
932
933   if (TARGET_APCS_REENT && flag_pic)
934     error ("-fpic and -mapcs-reent are incompatible");
935
936   if (TARGET_APCS_REENT)
937     warning ("APCS reentrant code not supported.  Ignored");
938
939   /* If this target is normally configured to use APCS frames, warn if they
940      are turned off and debugging is turned on.  */
941   if (TARGET_ARM
942       && write_symbols != NO_DEBUG
943       && !TARGET_APCS_FRAME
944       && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
945     warning ("-g with -mno-apcs-frame may not give sensible debugging");
946
947   /* If stack checking is disabled, we can use r10 as the PIC register,
948      which keeps r9 available.  */
949   if (flag_pic)
950     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
951
952   if (TARGET_APCS_FLOAT)
953     warning ("passing floating point arguments in fp regs not yet supported");
954
955   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
956   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
957   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
958   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
959   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
960   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
961   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
962   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
963   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
964
965   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
966   arm_is_strong = (tune_flags & FL_STRONG) != 0;
967   thumb_code = (TARGET_ARM == 0);
968   arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
969                     && !(tune_flags & FL_ARCH4))) != 0;
970   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
971   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
972
973   /* V5 code we generate is completely interworking capable, so we turn off
974      TARGET_INTERWORK here to avoid many tests later on.  */
975
976   /* XXX However, we must pass the right pre-processor defines to CPP
977      or GLD can get confused.  This is a hack.  */
978   if (TARGET_INTERWORK)
979     arm_cpp_interwork = 1;
980
981   if (arm_arch5)
982     target_flags &= ~ARM_FLAG_INTERWORK;
983
984   if (target_abi_name)
985     {
986       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
987         {
988           if (streq (arm_all_abis[i].name, target_abi_name))
989             {
990               arm_abi = arm_all_abis[i].abi_type;
991               break;
992             }
993         }
994       if (i == ARRAY_SIZE (arm_all_abis))
995         error ("invalid ABI option: -mabi=%s", target_abi_name);
996     }
997   else
998     arm_abi = ARM_DEFAULT_ABI;
999
1000   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1001     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1002
1003   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1004     error ("iwmmxt abi requires an iwmmxt capable cpu");
1005
1006   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1007   if (target_fpu_name == NULL && target_fpe_name != NULL)
1008     {
1009       if (streq (target_fpe_name, "2"))
1010         target_fpu_name = "fpe2";
1011       else if (streq (target_fpe_name, "3"))
1012         target_fpu_name = "fpe3";
1013       else
1014         error ("invalid floating point emulation option: -mfpe=%s",
1015                target_fpe_name);
1016     }
1017   if (target_fpu_name != NULL)
1018     {
1019       /* The user specified a FPU.  */
1020       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1021         {
1022           if (streq (all_fpus[i].name, target_fpu_name))
1023             {
1024               arm_fpu_arch = all_fpus[i].fpu;
1025               arm_fpu_tune = arm_fpu_arch;
1026               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1027               break;
1028             }
1029         }
1030       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1031         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1032     }
1033   else
1034     {
1035 #ifdef FPUTYPE_DEFAULT
1036       /* Use the default if it is specified for this platform.  */
1037       arm_fpu_arch = FPUTYPE_DEFAULT;
1038       arm_fpu_tune = FPUTYPE_DEFAULT;
1039 #else
1040       /* Pick one based on CPU type.  */
1041       /* ??? Some targets assume FPA is the default.
1042       if ((insn_flags & FL_VFP) != 0)
1043         arm_fpu_arch = FPUTYPE_VFP;
1044       else
1045       */
1046       if (arm_arch_cirrus)
1047         arm_fpu_arch = FPUTYPE_MAVERICK;
1048       else
1049         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1050 #endif
1051       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1052         arm_fpu_tune = FPUTYPE_FPA;
1053       else
1054         arm_fpu_tune = arm_fpu_arch;
1055       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1056       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1057         abort ();
1058     }
1059
1060   if (target_float_abi_name != NULL)
1061     {
1062       /* The user specified a FP ABI.  */
1063       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1064         {
1065           if (streq (all_float_abis[i].name, target_float_abi_name))
1066             {
1067               arm_float_abi = all_float_abis[i].abi_type;
1068               break;
1069             }
1070         }
1071       if (i == ARRAY_SIZE (all_float_abis))
1072         error ("invalid floating point abi: -mfloat-abi=%s",
1073                target_float_abi_name);
1074     }
1075   else
1076     {
1077       /* Use soft-float target flag.  */
1078       if (target_flags & ARM_FLAG_SOFT_FLOAT)
1079         arm_float_abi = ARM_FLOAT_ABI_SOFT;
1080       else
1081         arm_float_abi = ARM_FLOAT_ABI_HARD;
1082     }
1083
1084   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1085     sorry ("-mfloat-abi=hard and VFP");
1086
1087   /* If soft-float is specified then don't use FPU.  */
1088   if (TARGET_SOFT_FLOAT)
1089     arm_fpu_arch = FPUTYPE_NONE;
1090
1091   /* For arm2/3 there is no need to do any scheduling if there is only
1092      a floating point emulator, or we are doing software floating-point.  */
1093   if ((TARGET_SOFT_FLOAT
1094        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1095        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1096       && (tune_flags & FL_MODE32) == 0)
1097     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1098
1099   /* Override the default structure alignment for AAPCS ABI.  */
1100   if (arm_abi == ARM_ABI_AAPCS)
1101     arm_structure_size_boundary = 8;
1102
1103   if (structure_size_string != NULL)
1104     {
1105       int size = strtol (structure_size_string, NULL, 0);
1106
1107       if (size == 8 || size == 32
1108           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1109         arm_structure_size_boundary = size;
1110       else
1111         warning ("structure size boundary can only be set to %s",
1112                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1113     }
1114
1115   if (arm_pic_register_string != NULL)
1116     {
1117       int pic_register = decode_reg_name (arm_pic_register_string);
1118
1119       if (!flag_pic)
1120         warning ("-mpic-register= is useless without -fpic");
1121
1122       /* Prevent the user from choosing an obviously stupid PIC register.  */
1123       else if (pic_register < 0 || call_used_regs[pic_register]
1124                || pic_register == HARD_FRAME_POINTER_REGNUM
1125                || pic_register == STACK_POINTER_REGNUM
1126                || pic_register >= PC_REGNUM)
1127         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1128       else
1129         arm_pic_register = pic_register;
1130     }
1131
1132   if (TARGET_THUMB && flag_schedule_insns)
1133     {
1134       /* Don't warn since it's on by default in -O2.  */
1135       flag_schedule_insns = 0;
1136     }
1137
1138   if (optimize_size)
1139     {
1140       /* There's some dispute as to whether this should be 1 or 2.  However,
1141          experiments seem to show that in pathological cases a setting of
1142          1 degrades less severely than a setting of 2.  This could change if
1143          other parts of the compiler change their behavior.  */
1144       arm_constant_limit = 1;
1145
1146       /* If optimizing for size, bump the number of instructions that we
1147          are prepared to conditionally execute (even on a StrongARM).  */
1148       max_insns_skipped = 6;
1149     }
1150   else
1151     {
1152       /* For processors with load scheduling, it never costs more than
1153          2 cycles to load a constant, and the load scheduler may well
1154          reduce that to 1.  */
1155       if (tune_flags & FL_LDSCHED)
1156         arm_constant_limit = 1;
1157
1158       /* On XScale the longer latency of a load makes it more difficult
1159          to achieve a good schedule, so it's faster to synthesize
1160          constants that can be done in two insns.  */
1161       if (arm_tune_xscale)
1162         arm_constant_limit = 2;
1163
1164       /* StrongARM has early execution of branches, so a sequence
1165          that is worth skipping is shorter.  */
1166       if (arm_is_strong)
1167         max_insns_skipped = 3;
1168     }
1169
1170   /* Register global variables with the garbage collector.  */
1171   arm_add_gc_roots ();
1172 }
1173
1174 static void
1175 arm_add_gc_roots (void)
1176 {
1177   gcc_obstack_init(&minipool_obstack);
1178   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1179 }
1180 \f
1181 /* A table of known ARM exception types.
1182    For use with the interrupt function attribute.  */
1183
1184 typedef struct
1185 {
1186   const char *const arg;
1187   const unsigned long return_value;
1188 }
1189 isr_attribute_arg;
1190
1191 static const isr_attribute_arg isr_attribute_args [] =
1192 {
1193   { "IRQ",   ARM_FT_ISR },
1194   { "irq",   ARM_FT_ISR },
1195   { "FIQ",   ARM_FT_FIQ },
1196   { "fiq",   ARM_FT_FIQ },
1197   { "ABORT", ARM_FT_ISR },
1198   { "abort", ARM_FT_ISR },
1199   { "ABORT", ARM_FT_ISR },
1200   { "abort", ARM_FT_ISR },
1201   { "UNDEF", ARM_FT_EXCEPTION },
1202   { "undef", ARM_FT_EXCEPTION },
1203   { "SWI",   ARM_FT_EXCEPTION },
1204   { "swi",   ARM_FT_EXCEPTION },
1205   { NULL,    ARM_FT_NORMAL }
1206 };
1207
1208 /* Returns the (interrupt) function type of the current
1209    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1210
1211 static unsigned long
1212 arm_isr_value (tree argument)
1213 {
1214   const isr_attribute_arg * ptr;
1215   const char *              arg;
1216
1217   /* No argument - default to IRQ.  */
1218   if (argument == NULL_TREE)
1219     return ARM_FT_ISR;
1220
1221   /* Get the value of the argument.  */
1222   if (TREE_VALUE (argument) == NULL_TREE
1223       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1224     return ARM_FT_UNKNOWN;
1225
1226   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1227
1228   /* Check it against the list of known arguments.  */
1229   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1230     if (streq (arg, ptr->arg))
1231       return ptr->return_value;
1232
1233   /* An unrecognized interrupt type.  */
1234   return ARM_FT_UNKNOWN;
1235 }
1236
1237 /* Computes the type of the current function.  */
1238
1239 static unsigned long
1240 arm_compute_func_type (void)
1241 {
1242   unsigned long type = ARM_FT_UNKNOWN;
1243   tree a;
1244   tree attr;
1245
1246   if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
1247     abort ();
1248
1249   /* Decide if the current function is volatile.  Such functions
1250      never return, and many memory cycles can be saved by not storing
1251      register values that will never be needed again.  This optimization
1252      was added to speed up context switching in a kernel application.  */
1253   if (optimize > 0
1254       && TREE_NOTHROW (current_function_decl)
1255       && TREE_THIS_VOLATILE (current_function_decl))
1256     type |= ARM_FT_VOLATILE;
1257
1258   if (cfun->static_chain_decl != NULL)
1259     type |= ARM_FT_NESTED;
1260
1261   attr = DECL_ATTRIBUTES (current_function_decl);
1262
1263   a = lookup_attribute ("naked", attr);
1264   if (a != NULL_TREE)
1265     type |= ARM_FT_NAKED;
1266
1267   a = lookup_attribute ("isr", attr);
1268   if (a == NULL_TREE)
1269     a = lookup_attribute ("interrupt", attr);
1270
1271   if (a == NULL_TREE)
1272     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1273   else
1274     type |= arm_isr_value (TREE_VALUE (a));
1275
1276   return type;
1277 }
1278
1279 /* Returns the type of the current function.  */
1280
1281 unsigned long
1282 arm_current_func_type (void)
1283 {
1284   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1285     cfun->machine->func_type = arm_compute_func_type ();
1286
1287   return cfun->machine->func_type;
1288 }
1289 \f
1290 /* Return 1 if it is possible to return using a single instruction.
1291    If SIBLING is non-null, this is a test for a return before a sibling
1292    call.  SIBLING is the call insn, so we can examine its register usage.  */
1293
1294 int
1295 use_return_insn (int iscond, rtx sibling)
1296 {
1297   int regno;
1298   unsigned int func_type;
1299   unsigned long saved_int_regs;
1300   unsigned HOST_WIDE_INT stack_adjust;
1301   arm_stack_offsets *offsets;
1302
1303   /* Never use a return instruction before reload has run.  */
1304   if (!reload_completed)
1305     return 0;
1306
1307   func_type = arm_current_func_type ();
1308
1309   /* Naked functions and volatile functions need special
1310      consideration.  */
1311   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1312     return 0;
1313
1314   /* So do interrupt functions that use the frame pointer.  */
1315   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1316     return 0;
1317
1318   offsets = arm_get_frame_offsets ();
1319   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1320
1321   /* As do variadic functions.  */
1322   if (current_function_pretend_args_size
1323       || cfun->machine->uses_anonymous_args
1324       /* Or if the function calls __builtin_eh_return () */
1325       || current_function_calls_eh_return
1326       /* Or if the function calls alloca */
1327       || current_function_calls_alloca
1328       /* Or if there is a stack adjustment.  However, if the stack pointer
1329          is saved on the stack, we can use a pre-incrementing stack load.  */
1330       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1331     return 0;
1332
1333   saved_int_regs = arm_compute_save_reg_mask ();
1334
1335   /* Unfortunately, the insn
1336
1337        ldmib sp, {..., sp, ...}
1338
1339      triggers a bug on most SA-110 based devices, such that the stack
1340      pointer won't be correctly restored if the instruction takes a
1341      page fault.  We work around this problem by popping r3 along with
1342      the other registers, since that is never slower than executing
1343      another instruction.
1344
1345      We test for !arm_arch5 here, because code for any architecture
1346      less than this could potentially be run on one of the buggy
1347      chips.  */
1348   if (stack_adjust == 4 && !arm_arch5)
1349     {
1350       /* Validate that r3 is a call-clobbered register (always true in
1351          the default abi) ...  */
1352       if (!call_used_regs[3])
1353         return 0;
1354
1355       /* ... that it isn't being used for a return value (always true
1356          until we implement return-in-regs), or for a tail-call
1357          argument ...  */
1358       if (sibling)
1359         {
1360           if (GET_CODE (sibling) != CALL_INSN)
1361             abort ();
1362
1363           if (find_regno_fusage (sibling, USE, 3))
1364             return 0;
1365         }
1366
1367       /* ... and that there are no call-saved registers in r0-r2
1368          (always true in the default ABI).  */
1369       if (saved_int_regs & 0x7)
1370         return 0;
1371     }
1372
1373   /* Can't be done if interworking with Thumb, and any registers have been
1374      stacked.  */
1375   if (TARGET_INTERWORK && saved_int_regs != 0)
1376     return 0;
1377
1378   /* On StrongARM, conditional returns are expensive if they aren't
1379      taken and multiple registers have been stacked.  */
1380   if (iscond && arm_is_strong)
1381     {
1382       /* Conditional return when just the LR is stored is a simple
1383          conditional-load instruction, that's not expensive.  */
1384       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1385         return 0;
1386
1387       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1388         return 0;
1389     }
1390
1391   /* If there are saved registers but the LR isn't saved, then we need
1392      two instructions for the return.  */
1393   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1394     return 0;
1395
1396   /* Can't be done if any of the FPA regs are pushed,
1397      since this also requires an insn.  */
1398   if (TARGET_HARD_FLOAT && TARGET_FPA)
1399     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1400       if (regs_ever_live[regno] && !call_used_regs[regno])
1401         return 0;
1402
1403   /* Likewise VFP regs.  */
1404   if (TARGET_HARD_FLOAT && TARGET_VFP)
1405     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1406       if (regs_ever_live[regno] && !call_used_regs[regno])
1407         return 0;
1408
1409   if (TARGET_REALLY_IWMMXT)
1410     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1411       if (regs_ever_live[regno] && ! call_used_regs [regno])
1412         return 0;
1413
1414   return 1;
1415 }
1416
1417 /* Return TRUE if int I is a valid immediate ARM constant.  */
1418
1419 int
1420 const_ok_for_arm (HOST_WIDE_INT i)
1421 {
1422   unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1423
1424   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1425      be all zero, or all one.  */
1426   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1427       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1428           != ((~(unsigned HOST_WIDE_INT) 0)
1429               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1430     return FALSE;
1431
1432   /* Fast return for 0 and powers of 2 */
1433   if ((i & (i - 1)) == 0)
1434     return TRUE;
1435
1436   do
1437     {
1438       if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1439         return TRUE;
1440       mask =
1441           (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1442                           >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1443     }
1444   while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1445
1446   return FALSE;
1447 }
1448
1449 /* Return true if I is a valid constant for the operation CODE.  */
1450 static int
1451 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1452 {
1453   if (const_ok_for_arm (i))
1454     return 1;
1455
1456   switch (code)
1457     {
1458     case PLUS:
1459       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1460
1461     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1462     case XOR:
1463     case IOR:
1464       return 0;
1465
1466     case AND:
1467       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1468
1469     default:
1470       abort ();
1471     }
1472 }
1473
1474 /* Emit a sequence of insns to handle a large constant.
1475    CODE is the code of the operation required, it can be any of SET, PLUS,
1476    IOR, AND, XOR, MINUS;
1477    MODE is the mode in which the operation is being performed;
1478    VAL is the integer to operate on;
1479    SOURCE is the other operand (a register, or a null-pointer for SET);
1480    SUBTARGETS means it is safe to create scratch registers if that will
1481    either produce a simpler sequence, or we will want to cse the values.
1482    Return value is the number of insns emitted.  */
1483
1484 int
1485 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1486                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1487 {
1488   rtx cond;
1489
1490   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1491     cond = COND_EXEC_TEST (PATTERN (insn));
1492   else
1493     cond = NULL_RTX;
1494
1495   if (subtargets || code == SET
1496       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1497           && REGNO (target) != REGNO (source)))
1498     {
1499       /* After arm_reorg has been called, we can't fix up expensive
1500          constants by pushing them into memory so we must synthesize
1501          them in-line, regardless of the cost.  This is only likely to
1502          be more costly on chips that have load delay slots and we are
1503          compiling without running the scheduler (so no splitting
1504          occurred before the final instruction emission).
1505
1506          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1507       */
1508       if (!after_arm_reorg
1509           && !cond
1510           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1511                                 1, 0)
1512               > arm_constant_limit + (code != SET)))
1513         {
1514           if (code == SET)
1515             {
1516               /* Currently SET is the only monadic value for CODE, all
1517                  the rest are diadic.  */
1518               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1519               return 1;
1520             }
1521           else
1522             {
1523               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1524
1525               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1526               /* For MINUS, the value is subtracted from, since we never
1527                  have subtraction of a constant.  */
1528               if (code == MINUS)
1529                 emit_insn (gen_rtx_SET (VOIDmode, target,
1530                                         gen_rtx_MINUS (mode, temp, source)));
1531               else
1532                 emit_insn (gen_rtx_SET (VOIDmode, target,
1533                                         gen_rtx_fmt_ee (code, mode, source, temp)));
1534               return 2;
1535             }
1536         }
1537     }
1538
1539   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1540                            1);
1541 }
1542
1543 static int
1544 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1545 {
1546   HOST_WIDE_INT temp1;
1547   int num_insns = 0;
1548   do
1549     {
1550       int end;
1551
1552       if (i <= 0)
1553         i += 32;
1554       if (remainder & (3 << (i - 2)))
1555         {
1556           end = i - 8;
1557           if (end < 0)
1558             end += 32;
1559           temp1 = remainder & ((0x0ff << end)
1560                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1561           remainder &= ~temp1;
1562           num_insns++;
1563           i -= 6;
1564         }
1565       i -= 2;
1566     } while (remainder);
1567   return num_insns;
1568 }
1569
1570 /* Emit an instruction with the indicated PATTERN.  If COND is
1571    non-NULL, conditionalize the execution of the instruction on COND
1572    being true.  */
1573
1574 static void
1575 emit_constant_insn (rtx cond, rtx pattern)
1576 {
1577   if (cond)
1578     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1579   emit_insn (pattern);
1580 }
1581
1582 /* As above, but extra parameter GENERATE which, if clear, suppresses
1583    RTL generation.  */
1584
1585 static int
1586 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1587                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1588                   int generate)
1589 {
1590   int can_invert = 0;
1591   int can_negate = 0;
1592   int can_negate_initial = 0;
1593   int can_shift = 0;
1594   int i;
1595   int num_bits_set = 0;
1596   int set_sign_bit_copies = 0;
1597   int clear_sign_bit_copies = 0;
1598   int clear_zero_bit_copies = 0;
1599   int set_zero_bit_copies = 0;
1600   int insns = 0;
1601   unsigned HOST_WIDE_INT temp1, temp2;
1602   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1603
1604   /* Find out which operations are safe for a given CODE.  Also do a quick
1605      check for degenerate cases; these can occur when DImode operations
1606      are split.  */
1607   switch (code)
1608     {
1609     case SET:
1610       can_invert = 1;
1611       can_shift = 1;
1612       can_negate = 1;
1613       break;
1614
1615     case PLUS:
1616       can_negate = 1;
1617       can_negate_initial = 1;
1618       break;
1619
1620     case IOR:
1621       if (remainder == 0xffffffff)
1622         {
1623           if (generate)
1624             emit_constant_insn (cond,
1625                                 gen_rtx_SET (VOIDmode, target,
1626                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1627           return 1;
1628         }
1629       if (remainder == 0)
1630         {
1631           if (reload_completed && rtx_equal_p (target, source))
1632             return 0;
1633           if (generate)
1634             emit_constant_insn (cond,
1635                                 gen_rtx_SET (VOIDmode, target, source));
1636           return 1;
1637         }
1638       break;
1639
1640     case AND:
1641       if (remainder == 0)
1642         {
1643           if (generate)
1644             emit_constant_insn (cond,
1645                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1646           return 1;
1647         }
1648       if (remainder == 0xffffffff)
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       can_invert = 1;
1658       break;
1659
1660     case XOR:
1661       if (remainder == 0)
1662         {
1663           if (reload_completed && rtx_equal_p (target, source))
1664             return 0;
1665           if (generate)
1666             emit_constant_insn (cond,
1667                                 gen_rtx_SET (VOIDmode, target, source));
1668           return 1;
1669         }
1670       if (remainder == 0xffffffff)
1671         {
1672           if (generate)
1673             emit_constant_insn (cond,
1674                                 gen_rtx_SET (VOIDmode, target,
1675                                              gen_rtx_NOT (mode, source)));
1676           return 1;
1677         }
1678
1679       /* We don't know how to handle this yet below.  */
1680       abort ();
1681
1682     case MINUS:
1683       /* We treat MINUS as (val - source), since (source - val) is always
1684          passed as (source + (-val)).  */
1685       if (remainder == 0)
1686         {
1687           if (generate)
1688             emit_constant_insn (cond,
1689                                 gen_rtx_SET (VOIDmode, target,
1690                                              gen_rtx_NEG (mode, source)));
1691           return 1;
1692         }
1693       if (const_ok_for_arm (val))
1694         {
1695           if (generate)
1696             emit_constant_insn (cond,
1697                                 gen_rtx_SET (VOIDmode, target,
1698                                              gen_rtx_MINUS (mode, GEN_INT (val),
1699                                                             source)));
1700           return 1;
1701         }
1702       can_negate = 1;
1703
1704       break;
1705
1706     default:
1707       abort ();
1708     }
1709
1710   /* If we can do it in one insn get out quickly.  */
1711   if (const_ok_for_arm (val)
1712       || (can_negate_initial && const_ok_for_arm (-val))
1713       || (can_invert && const_ok_for_arm (~val)))
1714     {
1715       if (generate)
1716         emit_constant_insn (cond,
1717                             gen_rtx_SET (VOIDmode, target,
1718                                          (source
1719                                           ? gen_rtx_fmt_ee (code, mode, source,
1720                                                             GEN_INT (val))
1721                                           : GEN_INT (val))));
1722       return 1;
1723     }
1724
1725   /* Calculate a few attributes that may be useful for specific
1726      optimizations.  */
1727   for (i = 31; i >= 0; i--)
1728     {
1729       if ((remainder & (1 << i)) == 0)
1730         clear_sign_bit_copies++;
1731       else
1732         break;
1733     }
1734
1735   for (i = 31; i >= 0; i--)
1736     {
1737       if ((remainder & (1 << i)) != 0)
1738         set_sign_bit_copies++;
1739       else
1740         break;
1741     }
1742
1743   for (i = 0; i <= 31; i++)
1744     {
1745       if ((remainder & (1 << i)) == 0)
1746         clear_zero_bit_copies++;
1747       else
1748         break;
1749     }
1750
1751   for (i = 0; i <= 31; i++)
1752     {
1753       if ((remainder & (1 << i)) != 0)
1754         set_zero_bit_copies++;
1755       else
1756         break;
1757     }
1758
1759   switch (code)
1760     {
1761     case SET:
1762       /* See if we can do this by sign_extending a constant that is known
1763          to be negative.  This is a good, way of doing it, since the shift
1764          may well merge into a subsequent insn.  */
1765       if (set_sign_bit_copies > 1)
1766         {
1767           if (const_ok_for_arm
1768               (temp1 = ARM_SIGN_EXTEND (remainder
1769                                         << (set_sign_bit_copies - 1))))
1770             {
1771               if (generate)
1772                 {
1773                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1774                   emit_constant_insn (cond,
1775                                       gen_rtx_SET (VOIDmode, new_src,
1776                                                    GEN_INT (temp1)));
1777                   emit_constant_insn (cond,
1778                                       gen_ashrsi3 (target, new_src,
1779                                                    GEN_INT (set_sign_bit_copies - 1)));
1780                 }
1781               return 2;
1782             }
1783           /* For an inverted constant, we will need to set the low bits,
1784              these will be shifted out of harm's way.  */
1785           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1786           if (const_ok_for_arm (~temp1))
1787             {
1788               if (generate)
1789                 {
1790                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1791                   emit_constant_insn (cond,
1792                                       gen_rtx_SET (VOIDmode, new_src,
1793                                                    GEN_INT (temp1)));
1794                   emit_constant_insn (cond,
1795                                       gen_ashrsi3 (target, new_src,
1796                                                    GEN_INT (set_sign_bit_copies - 1)));
1797                 }
1798               return 2;
1799             }
1800         }
1801
1802       /* See if we can generate this by setting the bottom (or the top)
1803          16 bits, and then shifting these into the other half of the
1804          word.  We only look for the simplest cases, to do more would cost
1805          too much.  Be careful, however, not to generate this when the
1806          alternative would take fewer insns.  */
1807       if (val & 0xffff0000)
1808         {
1809           temp1 = remainder & 0xffff0000;
1810           temp2 = remainder & 0x0000ffff;
1811
1812           /* Overlaps outside this range are best done using other methods.  */
1813           for (i = 9; i < 24; i++)
1814             {
1815               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1816                   && !const_ok_for_arm (temp2))
1817                 {
1818                   rtx new_src = (subtargets
1819                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1820                                  : target);
1821                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1822                                             source, subtargets, generate);
1823                   source = new_src;
1824                   if (generate)
1825                     emit_constant_insn
1826                       (cond,
1827                        gen_rtx_SET
1828                        (VOIDmode, target,
1829                         gen_rtx_IOR (mode,
1830                                      gen_rtx_ASHIFT (mode, source,
1831                                                      GEN_INT (i)),
1832                                      source)));
1833                   return insns + 1;
1834                 }
1835             }
1836
1837           /* Don't duplicate cases already considered.  */
1838           for (i = 17; i < 24; i++)
1839             {
1840               if (((temp1 | (temp1 >> i)) == remainder)
1841                   && !const_ok_for_arm (temp1))
1842                 {
1843                   rtx new_src = (subtargets
1844                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1845                                  : target);
1846                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1847                                             source, subtargets, generate);
1848                   source = new_src;
1849                   if (generate)
1850                     emit_constant_insn
1851                       (cond,
1852                        gen_rtx_SET (VOIDmode, target,
1853                                     gen_rtx_IOR
1854                                     (mode,
1855                                      gen_rtx_LSHIFTRT (mode, source,
1856                                                        GEN_INT (i)),
1857                                      source)));
1858                   return insns + 1;
1859                 }
1860             }
1861         }
1862       break;
1863
1864     case IOR:
1865     case XOR:
1866       /* If we have IOR or XOR, and the constant can be loaded in a
1867          single instruction, and we can find a temporary to put it in,
1868          then this can be done in two instructions instead of 3-4.  */
1869       if (subtargets
1870           /* TARGET can't be NULL if SUBTARGETS is 0 */
1871           || (reload_completed && !reg_mentioned_p (target, source)))
1872         {
1873           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1874             {
1875               if (generate)
1876                 {
1877                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1878
1879                   emit_constant_insn (cond,
1880                                       gen_rtx_SET (VOIDmode, sub,
1881                                                    GEN_INT (val)));
1882                   emit_constant_insn (cond,
1883                                       gen_rtx_SET (VOIDmode, target,
1884                                                    gen_rtx_fmt_ee (code, mode,
1885                                                                    source, sub)));
1886                 }
1887               return 2;
1888             }
1889         }
1890
1891       if (code == XOR)
1892         break;
1893
1894       if (set_sign_bit_copies > 8
1895           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1896         {
1897           if (generate)
1898             {
1899               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1900               rtx shift = GEN_INT (set_sign_bit_copies);
1901
1902               emit_constant_insn
1903                 (cond,
1904                  gen_rtx_SET (VOIDmode, sub,
1905                               gen_rtx_NOT (mode,
1906                                            gen_rtx_ASHIFT (mode,
1907                                                            source,
1908                                                            shift))));
1909               emit_constant_insn
1910                 (cond,
1911                  gen_rtx_SET (VOIDmode, target,
1912                               gen_rtx_NOT (mode,
1913                                            gen_rtx_LSHIFTRT (mode, sub,
1914                                                              shift))));
1915             }
1916           return 2;
1917         }
1918
1919       if (set_zero_bit_copies > 8
1920           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1921         {
1922           if (generate)
1923             {
1924               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1925               rtx shift = GEN_INT (set_zero_bit_copies);
1926
1927               emit_constant_insn
1928                 (cond,
1929                  gen_rtx_SET (VOIDmode, sub,
1930                               gen_rtx_NOT (mode,
1931                                            gen_rtx_LSHIFTRT (mode,
1932                                                              source,
1933                                                              shift))));
1934               emit_constant_insn
1935                 (cond,
1936                  gen_rtx_SET (VOIDmode, target,
1937                               gen_rtx_NOT (mode,
1938                                            gen_rtx_ASHIFT (mode, sub,
1939                                                            shift))));
1940             }
1941           return 2;
1942         }
1943
1944       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1945         {
1946           if (generate)
1947             {
1948               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1949               emit_constant_insn (cond,
1950                                   gen_rtx_SET (VOIDmode, sub,
1951                                                gen_rtx_NOT (mode, source)));
1952               source = sub;
1953               if (subtargets)
1954                 sub = gen_reg_rtx (mode);
1955               emit_constant_insn (cond,
1956                                   gen_rtx_SET (VOIDmode, sub,
1957                                                gen_rtx_AND (mode, source,
1958                                                             GEN_INT (temp1))));
1959               emit_constant_insn (cond,
1960                                   gen_rtx_SET (VOIDmode, target,
1961                                                gen_rtx_NOT (mode, sub)));
1962             }
1963           return 3;
1964         }
1965       break;
1966
1967     case AND:
1968       /* See if two shifts will do 2 or more insn's worth of work.  */
1969       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1970         {
1971           HOST_WIDE_INT shift_mask = ((0xffffffff
1972                                        << (32 - clear_sign_bit_copies))
1973                                       & 0xffffffff);
1974
1975           if ((remainder | shift_mask) != 0xffffffff)
1976             {
1977               if (generate)
1978                 {
1979                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1980                   insns = arm_gen_constant (AND, mode, cond,
1981                                             remainder | shift_mask,
1982                                             new_src, source, subtargets, 1);
1983                   source = new_src;
1984                 }
1985               else
1986                 {
1987                   rtx targ = subtargets ? NULL_RTX : target;
1988                   insns = arm_gen_constant (AND, mode, cond,
1989                                             remainder | shift_mask,
1990                                             targ, source, subtargets, 0);
1991                 }
1992             }
1993
1994           if (generate)
1995             {
1996               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1997               rtx shift = GEN_INT (clear_sign_bit_copies);
1998
1999               emit_insn (gen_ashlsi3 (new_src, source, shift));
2000               emit_insn (gen_lshrsi3 (target, new_src, shift));
2001             }
2002
2003           return insns + 2;
2004         }
2005
2006       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2007         {
2008           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2009
2010           if ((remainder | shift_mask) != 0xffffffff)
2011             {
2012               if (generate)
2013                 {
2014                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2015
2016                   insns = arm_gen_constant (AND, mode, cond,
2017                                             remainder | shift_mask,
2018                                             new_src, source, subtargets, 1);
2019                   source = new_src;
2020                 }
2021               else
2022                 {
2023                   rtx targ = subtargets ? NULL_RTX : target;
2024
2025                   insns = arm_gen_constant (AND, mode, cond,
2026                                             remainder | shift_mask,
2027                                             targ, source, subtargets, 0);
2028                 }
2029             }
2030
2031           if (generate)
2032             {
2033               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2034               rtx shift = GEN_INT (clear_zero_bit_copies);
2035
2036               emit_insn (gen_lshrsi3 (new_src, source, shift));
2037               emit_insn (gen_ashlsi3 (target, new_src, shift));
2038             }
2039
2040           return insns + 2;
2041         }
2042
2043       break;
2044
2045     default:
2046       break;
2047     }
2048
2049   for (i = 0; i < 32; i++)
2050     if (remainder & (1 << i))
2051       num_bits_set++;
2052
2053   if (code == AND || (can_invert && num_bits_set > 16))
2054     remainder = (~remainder) & 0xffffffff;
2055   else if (code == PLUS && num_bits_set > 16)
2056     remainder = (-remainder) & 0xffffffff;
2057   else
2058     {
2059       can_invert = 0;
2060       can_negate = 0;
2061     }
2062
2063   /* Now try and find a way of doing the job in either two or three
2064      instructions.
2065      We start by looking for the largest block of zeros that are aligned on
2066      a 2-bit boundary, we then fill up the temps, wrapping around to the
2067      top of the word when we drop off the bottom.
2068      In the worst case this code should produce no more than four insns.  */
2069   {
2070     int best_start = 0;
2071     int best_consecutive_zeros = 0;
2072
2073     for (i = 0; i < 32; i += 2)
2074       {
2075         int consecutive_zeros = 0;
2076
2077         if (!(remainder & (3 << i)))
2078           {
2079             while ((i < 32) && !(remainder & (3 << i)))
2080               {
2081                 consecutive_zeros += 2;
2082                 i += 2;
2083               }
2084             if (consecutive_zeros > best_consecutive_zeros)
2085               {
2086                 best_consecutive_zeros = consecutive_zeros;
2087                 best_start = i - consecutive_zeros;
2088               }
2089             i -= 2;
2090           }
2091       }
2092
2093     /* So long as it won't require any more insns to do so, it's
2094        desirable to emit a small constant (in bits 0...9) in the last
2095        insn.  This way there is more chance that it can be combined with
2096        a later addressing insn to form a pre-indexed load or store
2097        operation.  Consider:
2098
2099                *((volatile int *)0xe0000100) = 1;
2100                *((volatile int *)0xe0000110) = 2;
2101
2102        We want this to wind up as:
2103
2104                 mov rA, #0xe0000000
2105                 mov rB, #1
2106                 str rB, [rA, #0x100]
2107                 mov rB, #2
2108                 str rB, [rA, #0x110]
2109
2110        rather than having to synthesize both large constants from scratch.
2111
2112        Therefore, we calculate how many insns would be required to emit
2113        the constant starting from `best_start', and also starting from
2114        zero (ie with bit 31 first to be output).  If `best_start' doesn't
2115        yield a shorter sequence, we may as well use zero.  */
2116     if (best_start != 0
2117         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2118         && (count_insns_for_constant (remainder, 0) <=
2119             count_insns_for_constant (remainder, best_start)))
2120       best_start = 0;
2121
2122     /* Now start emitting the insns.  */
2123     i = best_start;
2124     do
2125       {
2126         int end;
2127
2128         if (i <= 0)
2129           i += 32;
2130         if (remainder & (3 << (i - 2)))
2131           {
2132             end = i - 8;
2133             if (end < 0)
2134               end += 32;
2135             temp1 = remainder & ((0x0ff << end)
2136                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2137             remainder &= ~temp1;
2138
2139             if (generate)
2140               {
2141                 rtx new_src, temp1_rtx;
2142
2143                 if (code == SET || code == MINUS)
2144                   {
2145                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2146                     if (can_invert && code != MINUS)
2147                       temp1 = ~temp1;
2148                   }
2149                 else
2150                   {
2151                     if (remainder && subtargets)
2152                       new_src = gen_reg_rtx (mode);
2153                     else
2154                       new_src = target;
2155                     if (can_invert)
2156                       temp1 = ~temp1;
2157                     else if (can_negate)
2158                       temp1 = -temp1;
2159                   }
2160
2161                 temp1 = trunc_int_for_mode (temp1, mode);
2162                 temp1_rtx = GEN_INT (temp1);
2163
2164                 if (code == SET)
2165                   ;
2166                 else if (code == MINUS)
2167                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2168                 else
2169                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2170
2171                 emit_constant_insn (cond,
2172                                     gen_rtx_SET (VOIDmode, new_src,
2173                                                  temp1_rtx));
2174                 source = new_src;
2175               }
2176
2177             if (code == SET)
2178               {
2179                 can_invert = 0;
2180                 code = PLUS;
2181               }
2182             else if (code == MINUS)
2183               code = PLUS;
2184
2185             insns++;
2186             i -= 6;
2187           }
2188         i -= 2;
2189       }
2190     while (remainder);
2191   }
2192
2193   return insns;
2194 }
2195
2196 /* Canonicalize a comparison so that we are more likely to recognize it.
2197    This can be done for a few constant compares, where we can make the
2198    immediate value easier to load.  */
2199
2200 enum rtx_code
2201 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2202 {
2203   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2204
2205   switch (code)
2206     {
2207     case EQ:
2208     case NE:
2209       return code;
2210
2211     case GT:
2212     case LE:
2213       if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2214           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2215         {
2216           *op1 = GEN_INT (i + 1);
2217           return code == GT ? GE : LT;
2218         }
2219       break;
2220
2221     case GE:
2222     case LT:
2223       if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2224           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2225         {
2226           *op1 = GEN_INT (i - 1);
2227           return code == GE ? GT : LE;
2228         }
2229       break;
2230
2231     case GTU:
2232     case LEU:
2233       if (i != ~((unsigned HOST_WIDE_INT) 0)
2234           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2235         {
2236           *op1 = GEN_INT (i + 1);
2237           return code == GTU ? GEU : LTU;
2238         }
2239       break;
2240
2241     case GEU:
2242     case LTU:
2243       if (i != 0
2244           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2245         {
2246           *op1 = GEN_INT (i - 1);
2247           return code == GEU ? GTU : LEU;
2248         }
2249       break;
2250
2251     default:
2252       abort ();
2253     }
2254
2255   return code;
2256 }
2257
2258
2259 /* Define how to find the value returned by a function.  */
2260
2261 rtx arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2262 {
2263   enum machine_mode mode;
2264   int unsignedp ATTRIBUTE_UNUSED;
2265   rtx r ATTRIBUTE_UNUSED;
2266
2267
2268   mode = TYPE_MODE (type);
2269   /* Promote integer types.  */
2270   if (INTEGRAL_TYPE_P (type))
2271     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2272   return LIBCALL_VALUE(mode);
2273 }
2274
2275
2276 /* Decide whether a type should be returned in memory (true)
2277    or in a register (false).  This is called by the macro
2278    RETURN_IN_MEMORY.  */
2279 int
2280 arm_return_in_memory (tree type)
2281 {
2282   HOST_WIDE_INT size;
2283
2284   if (!AGGREGATE_TYPE_P (type))
2285     /* All simple types are returned in registers.  */
2286     return 0;
2287
2288   size = int_size_in_bytes (type);
2289
2290   if (arm_abi != ARM_ABI_APCS)
2291     {
2292       /* ATPCS and later return aggregate types in memory only if they are
2293          larger than a word (or are variable size).  */
2294       return (size < 0 || size > UNITS_PER_WORD);
2295     }
2296
2297   /* For the arm-wince targets we choose to be compatible with Microsoft's
2298      ARM and Thumb compilers, which always return aggregates in memory.  */
2299 #ifndef ARM_WINCE
2300   /* All structures/unions bigger than one word are returned in memory.
2301      Also catch the case where int_size_in_bytes returns -1.  In this case
2302      the aggregate is either huge or of variable size, and in either case
2303      we will want to return it via memory and not in a register.  */
2304   if (size < 0 || size > UNITS_PER_WORD)
2305     return 1;
2306
2307   if (TREE_CODE (type) == RECORD_TYPE)
2308     {
2309       tree field;
2310
2311       /* For a struct the APCS says that we only return in a register
2312          if the type is 'integer like' and every addressable element
2313          has an offset of zero.  For practical purposes this means
2314          that the structure can have at most one non bit-field element
2315          and that this element must be the first one in the structure.  */
2316
2317       /* Find the first field, ignoring non FIELD_DECL things which will
2318          have been created by C++.  */
2319       for (field = TYPE_FIELDS (type);
2320            field && TREE_CODE (field) != FIELD_DECL;
2321            field = TREE_CHAIN (field))
2322         continue;
2323
2324       if (field == NULL)
2325         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2326
2327       /* Check that the first field is valid for returning in a register.  */
2328
2329       /* ... Floats are not allowed */
2330       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2331         return 1;
2332
2333       /* ... Aggregates that are not themselves valid for returning in
2334          a register are not allowed.  */
2335       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2336         return 1;
2337
2338       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2339          since they are not addressable.  */
2340       for (field = TREE_CHAIN (field);
2341            field;
2342            field = TREE_CHAIN (field))
2343         {
2344           if (TREE_CODE (field) != FIELD_DECL)
2345             continue;
2346
2347           if (!DECL_BIT_FIELD_TYPE (field))
2348             return 1;
2349         }
2350
2351       return 0;
2352     }
2353
2354   if (TREE_CODE (type) == UNION_TYPE)
2355     {
2356       tree field;
2357
2358       /* Unions can be returned in registers if every element is
2359          integral, or can be returned in an integer register.  */
2360       for (field = TYPE_FIELDS (type);
2361            field;
2362            field = TREE_CHAIN (field))
2363         {
2364           if (TREE_CODE (field) != FIELD_DECL)
2365             continue;
2366
2367           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2368             return 1;
2369
2370           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2371             return 1;
2372         }
2373
2374       return 0;
2375     }
2376 #endif /* not ARM_WINCE */
2377
2378   /* Return all other types in memory.  */
2379   return 1;
2380 }
2381
2382 /* Indicate whether or not words of a double are in big-endian order.  */
2383
2384 int
2385 arm_float_words_big_endian (void)
2386 {
2387   if (TARGET_MAVERICK)
2388     return 0;
2389
2390   /* For FPA, float words are always big-endian.  For VFP, floats words
2391      follow the memory system mode.  */
2392
2393   if (TARGET_FPA)
2394     {
2395       return 1;
2396     }
2397
2398   if (TARGET_VFP)
2399     return (TARGET_BIG_END ? 1 : 0);
2400
2401   return 1;
2402 }
2403
2404 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2405    for a call to a function whose data type is FNTYPE.
2406    For a library call, FNTYPE is NULL.  */
2407 void
2408 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2409                           rtx libname  ATTRIBUTE_UNUSED,
2410                           tree fndecl ATTRIBUTE_UNUSED)
2411 {
2412   /* On the ARM, the offset starts at 0.  */
2413   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2414   pcum->iwmmxt_nregs = 0;
2415   pcum->can_split = true;
2416
2417   pcum->call_cookie = CALL_NORMAL;
2418
2419   if (TARGET_LONG_CALLS)
2420     pcum->call_cookie = CALL_LONG;
2421
2422   /* Check for long call/short call attributes.  The attributes
2423      override any command line option.  */
2424   if (fntype)
2425     {
2426       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2427         pcum->call_cookie = CALL_SHORT;
2428       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2429         pcum->call_cookie = CALL_LONG;
2430     }
2431
2432   /* Varargs vectors are treated the same as long long.
2433      named_count avoids having to change the way arm handles 'named' */
2434   pcum->named_count = 0;
2435   pcum->nargs = 0;
2436
2437   if (TARGET_REALLY_IWMMXT && fntype)
2438     {
2439       tree fn_arg;
2440
2441       for (fn_arg = TYPE_ARG_TYPES (fntype);
2442            fn_arg;
2443            fn_arg = TREE_CHAIN (fn_arg))
2444         pcum->named_count += 1;
2445
2446       if (! pcum->named_count)
2447         pcum->named_count = INT_MAX;
2448     }
2449 }
2450
2451
2452 /* Return true if mode/type need doubleword alignment.  */
2453 bool
2454 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2455 {
2456   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2457           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2458 }
2459
2460
2461 /* Determine where to put an argument to a function.
2462    Value is zero to push the argument on the stack,
2463    or a hard register in which to store the argument.
2464
2465    MODE is the argument's machine mode.
2466    TYPE is the data type of the argument (as a tree).
2467     This is null for libcalls where that information may
2468     not be available.
2469    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2470     the preceding args and about the function being called.
2471    NAMED is nonzero if this argument is a named parameter
2472     (otherwise it is an extra parameter matching an ellipsis).  */
2473
2474 rtx
2475 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2476                   tree type, int named)
2477 {
2478   int nregs;
2479
2480   /* Varargs vectors are treated the same as long long.
2481      named_count avoids having to change the way arm handles 'named' */
2482   if (TARGET_IWMMXT_ABI
2483       && arm_vector_mode_supported_p (mode)
2484       && pcum->named_count > pcum->nargs + 1)
2485     {
2486       if (pcum->iwmmxt_nregs <= 9)
2487         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2488       else
2489         {
2490           pcum->can_split = false;
2491           return NULL_RTX;
2492         }
2493     }
2494
2495   /* Put doubleword aligned quantities in even register pairs.  */
2496   if (pcum->nregs & 1
2497       && ARM_DOUBLEWORD_ALIGN
2498       && arm_needs_doubleword_align (mode, type))
2499     pcum->nregs++;
2500
2501   if (mode == VOIDmode)
2502     /* Compute operand 2 of the call insn.  */
2503     return GEN_INT (pcum->call_cookie);
2504
2505   /* Only allow splitting an arg between regs and memory if all preceding
2506      args were allocated to regs.  For args passed by reference we only count
2507      the reference pointer.  */
2508   if (pcum->can_split)
2509     nregs = 1;
2510   else
2511     nregs = ARM_NUM_REGS2 (mode, type);
2512
2513   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2514     return NULL_RTX;
2515
2516   return gen_rtx_REG (mode, pcum->nregs);
2517 }
2518
2519 /* Variable sized types are passed by reference.  This is a GCC
2520    extension to the ARM ABI.  */
2521
2522 static bool
2523 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2524                        enum machine_mode mode ATTRIBUTE_UNUSED,
2525                        tree type, bool named ATTRIBUTE_UNUSED)
2526 {
2527   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2528 }
2529 \f
2530 /* Encode the current state of the #pragma [no_]long_calls.  */
2531 typedef enum
2532 {
2533   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2534   LONG,         /* #pragma long_calls is in effect.  */
2535   SHORT         /* #pragma no_long_calls is in effect.  */
2536 } arm_pragma_enum;
2537
2538 static arm_pragma_enum arm_pragma_long_calls = OFF;
2539
2540 void
2541 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2542 {
2543   arm_pragma_long_calls = LONG;
2544 }
2545
2546 void
2547 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2548 {
2549   arm_pragma_long_calls = SHORT;
2550 }
2551
2552 void
2553 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2554 {
2555   arm_pragma_long_calls = OFF;
2556 }
2557 \f
2558 /* Table of machine attributes.  */
2559 const struct attribute_spec arm_attribute_table[] =
2560 {
2561   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2562   /* Function calls made to this symbol must be done indirectly, because
2563      it may lie outside of the 26 bit addressing range of a normal function
2564      call.  */
2565   { "long_call",    0, 0, false, true,  true,  NULL },
2566   /* Whereas these functions are always known to reside within the 26 bit
2567      addressing range.  */
2568   { "short_call",   0, 0, false, true,  true,  NULL },
2569   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
2570   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2571   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2572   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2573 #ifdef ARM_PE
2574   /* ARM/PE has three new attributes:
2575      interfacearm - ?
2576      dllexport - for exporting a function/variable that will live in a dll
2577      dllimport - for importing a function/variable from a dll
2578
2579      Microsoft allows multiple declspecs in one __declspec, separating
2580      them with spaces.  We do NOT support this.  Instead, use __declspec
2581      multiple times.
2582   */
2583   { "dllimport",    0, 0, true,  false, false, NULL },
2584   { "dllexport",    0, 0, true,  false, false, NULL },
2585   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2586 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2587   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
2588   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
2589 #endif
2590   { NULL,           0, 0, false, false, false, NULL }
2591 };
2592
2593 /* Handle an attribute requiring a FUNCTION_DECL;
2594    arguments as in struct attribute_spec.handler.  */
2595 static tree
2596 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2597                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2598 {
2599   if (TREE_CODE (*node) != FUNCTION_DECL)
2600     {
2601       warning ("`%s' attribute only applies to functions",
2602                IDENTIFIER_POINTER (name));
2603       *no_add_attrs = true;
2604     }
2605
2606   return NULL_TREE;
2607 }
2608
2609 /* Handle an "interrupt" or "isr" attribute;
2610    arguments as in struct attribute_spec.handler.  */
2611 static tree
2612 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2613                           bool *no_add_attrs)
2614 {
2615   if (DECL_P (*node))
2616     {
2617       if (TREE_CODE (*node) != FUNCTION_DECL)
2618         {
2619           warning ("`%s' attribute only applies to functions",
2620                    IDENTIFIER_POINTER (name));
2621           *no_add_attrs = true;
2622         }
2623       /* FIXME: the argument if any is checked for type attributes;
2624          should it be checked for decl ones?  */
2625     }
2626   else
2627     {
2628       if (TREE_CODE (*node) == FUNCTION_TYPE
2629           || TREE_CODE (*node) == METHOD_TYPE)
2630         {
2631           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2632             {
2633               warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2634               *no_add_attrs = true;
2635             }
2636         }
2637       else if (TREE_CODE (*node) == POINTER_TYPE
2638                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2639                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2640                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2641         {
2642           *node = build_variant_type_copy (*node);
2643           TREE_TYPE (*node) = build_type_attribute_variant
2644             (TREE_TYPE (*node),
2645              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2646           *no_add_attrs = true;
2647         }
2648       else
2649         {
2650           /* Possibly pass this attribute on from the type to a decl.  */
2651           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2652                        | (int) ATTR_FLAG_FUNCTION_NEXT
2653                        | (int) ATTR_FLAG_ARRAY_NEXT))
2654             {
2655               *no_add_attrs = true;
2656               return tree_cons (name, args, NULL_TREE);
2657             }
2658           else
2659             {
2660               warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2661             }
2662         }
2663     }
2664
2665   return NULL_TREE;
2666 }
2667
2668 /* Return 0 if the attributes for two types are incompatible, 1 if they
2669    are compatible, and 2 if they are nearly compatible (which causes a
2670    warning to be generated).  */
2671 static int
2672 arm_comp_type_attributes (tree type1, tree type2)
2673 {
2674   int l1, l2, s1, s2;
2675
2676   /* Check for mismatch of non-default calling convention.  */
2677   if (TREE_CODE (type1) != FUNCTION_TYPE)
2678     return 1;
2679
2680   /* Check for mismatched call attributes.  */
2681   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2682   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2683   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2684   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2685
2686   /* Only bother to check if an attribute is defined.  */
2687   if (l1 | l2 | s1 | s2)
2688     {
2689       /* If one type has an attribute, the other must have the same attribute.  */
2690       if ((l1 != l2) || (s1 != s2))
2691         return 0;
2692
2693       /* Disallow mixed attributes.  */
2694       if ((l1 & s2) || (l2 & s1))
2695         return 0;
2696     }
2697
2698   /* Check for mismatched ISR attribute.  */
2699   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2700   if (! l1)
2701     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2702   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2703   if (! l2)
2704     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2705   if (l1 != l2)
2706     return 0;
2707
2708   return 1;
2709 }
2710
2711 /*  Encode long_call or short_call attribute by prefixing
2712     symbol name in DECL with a special character FLAG.  */
2713 void
2714 arm_encode_call_attribute (tree decl, int flag)
2715 {
2716   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2717   int          len = strlen (str);
2718   char *       newstr;
2719
2720   /* Do not allow weak functions to be treated as short call.  */
2721   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2722     return;
2723
2724   newstr = alloca (len + 2);
2725   newstr[0] = flag;
2726   strcpy (newstr + 1, str);
2727
2728   newstr = (char *) ggc_alloc_string (newstr, len + 1);
2729   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2730 }
2731
2732 /*  Assigns default attributes to newly defined type.  This is used to
2733     set short_call/long_call attributes for function types of
2734     functions defined inside corresponding #pragma scopes.  */
2735 static void
2736 arm_set_default_type_attributes (tree type)
2737 {
2738   /* Add __attribute__ ((long_call)) to all functions, when
2739      inside #pragma long_calls or __attribute__ ((short_call)),
2740      when inside #pragma no_long_calls.  */
2741   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2742     {
2743       tree type_attr_list, attr_name;
2744       type_attr_list = TYPE_ATTRIBUTES (type);
2745
2746       if (arm_pragma_long_calls == LONG)
2747         attr_name = get_identifier ("long_call");
2748       else if (arm_pragma_long_calls == SHORT)
2749         attr_name = get_identifier ("short_call");
2750       else
2751         return;
2752
2753       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2754       TYPE_ATTRIBUTES (type) = type_attr_list;
2755     }
2756 }
2757 \f
2758 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2759    defined within the current compilation unit.  If this cannot be
2760    determined, then 0 is returned.  */
2761 static int
2762 current_file_function_operand (rtx sym_ref)
2763 {
2764   /* This is a bit of a fib.  A function will have a short call flag
2765      applied to its name if it has the short call attribute, or it has
2766      already been defined within the current compilation unit.  */
2767   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2768     return 1;
2769
2770   /* The current function is always defined within the current compilation
2771      unit.  If it s a weak definition however, then this may not be the real
2772      definition of the function, and so we have to say no.  */
2773   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2774       && !DECL_WEAK (current_function_decl))
2775     return 1;
2776
2777   /* We cannot make the determination - default to returning 0.  */
2778   return 0;
2779 }
2780
2781 /* Return nonzero if a 32 bit "long_call" should be generated for
2782    this call.  We generate a long_call if the function:
2783
2784         a.  has an __attribute__((long call))
2785      or b.  is within the scope of a #pragma long_calls
2786      or c.  the -mlong-calls command line switch has been specified
2787          .  and either:
2788                 1. -ffunction-sections is in effect
2789              or 2. the current function has __attribute__ ((section))
2790              or 3. the target function has __attribute__ ((section))
2791
2792    However we do not generate a long call if the function:
2793
2794         d.  has an __attribute__ ((short_call))
2795      or e.  is inside the scope of a #pragma no_long_calls
2796      or f.  is defined within the current compilation unit.
2797
2798    This function will be called by C fragments contained in the machine
2799    description file.  SYM_REF and CALL_COOKIE correspond to the matched
2800    rtl operands.  CALL_SYMBOL is used to distinguish between
2801    two different callers of the function.  It is set to 1 in the
2802    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2803    and "call_value" patterns.  This is because of the difference in the
2804    SYM_REFs passed by these patterns.  */
2805 int
2806 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2807 {
2808   if (!call_symbol)
2809     {
2810       if (GET_CODE (sym_ref) != MEM)
2811         return 0;
2812
2813       sym_ref = XEXP (sym_ref, 0);
2814     }
2815
2816   if (GET_CODE (sym_ref) != SYMBOL_REF)
2817     return 0;
2818
2819   if (call_cookie & CALL_SHORT)
2820     return 0;
2821
2822   if (TARGET_LONG_CALLS)
2823     {
2824       if (flag_function_sections
2825           || DECL_SECTION_NAME (current_function_decl))
2826         /* c.3 is handled by the definition of the
2827            ARM_DECLARE_FUNCTION_SIZE macro.  */
2828         return 1;
2829     }
2830
2831   if (current_file_function_operand (sym_ref))
2832     return 0;
2833
2834   return (call_cookie & CALL_LONG)
2835     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2836     || TARGET_LONG_CALLS;
2837 }
2838
2839 /* Return nonzero if it is ok to make a tail-call to DECL.  */
2840 static bool
2841 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
2842 {
2843   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2844
2845   if (cfun->machine->sibcall_blocked)
2846     return false;
2847
2848   /* Never tailcall something for which we have no decl, or if we
2849      are in Thumb mode.  */
2850   if (decl == NULL || TARGET_THUMB)
2851     return false;
2852
2853   /* Get the calling method.  */
2854   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2855     call_type = CALL_SHORT;
2856   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2857     call_type = CALL_LONG;
2858
2859   /* Cannot tail-call to long calls, since these are out of range of
2860      a branch instruction.  However, if not compiling PIC, we know
2861      we can reach the symbol if it is in this compilation unit.  */
2862   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2863     return false;
2864
2865   /* If we are interworking and the function is not declared static
2866      then we can't tail-call it unless we know that it exists in this
2867      compilation unit (since it might be a Thumb routine).  */
2868   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2869     return false;
2870
2871   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
2872   if (IS_INTERRUPT (arm_current_func_type ()))
2873     return false;
2874
2875   /* Everything else is ok.  */
2876   return true;
2877 }
2878
2879 \f
2880 /* Addressing mode support functions.  */
2881
2882 /* Return nonzero if X is a legitimate immediate operand when compiling
2883    for PIC.  */
2884 int
2885 legitimate_pic_operand_p (rtx x)
2886 {
2887   if (CONSTANT_P (x)
2888       && flag_pic
2889       && (GET_CODE (x) == SYMBOL_REF
2890           || (GET_CODE (x) == CONST
2891               && GET_CODE (XEXP (x, 0)) == PLUS
2892               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2893     return 0;
2894
2895   return 1;
2896 }
2897
2898 rtx
2899 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
2900 {
2901   if (GET_CODE (orig) == SYMBOL_REF
2902       || GET_CODE (orig) == LABEL_REF)
2903     {
2904 #ifndef AOF_ASSEMBLER
2905       rtx pic_ref, address;
2906 #endif
2907       rtx insn;
2908       int subregs = 0;
2909
2910       if (reg == 0)
2911         {
2912           if (no_new_pseudos)
2913             abort ();
2914           else
2915             reg = gen_reg_rtx (Pmode);
2916
2917           subregs = 1;
2918         }
2919
2920 #ifdef AOF_ASSEMBLER
2921       /* The AOF assembler can generate relocations for these directly, and
2922          understands that the PIC register has to be added into the offset.  */
2923       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
2924 #else
2925       if (subregs)
2926         address = gen_reg_rtx (Pmode);
2927       else
2928         address = reg;
2929
2930       if (TARGET_ARM)
2931         emit_insn (gen_pic_load_addr_arm (address, orig));
2932       else
2933         emit_insn (gen_pic_load_addr_thumb (address, orig));
2934
2935       if ((GET_CODE (orig) == LABEL_REF
2936            || (GET_CODE (orig) == SYMBOL_REF &&
2937                SYMBOL_REF_LOCAL_P (orig)))
2938           && NEED_GOT_RELOC)
2939         pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
2940       else
2941         {
2942           pic_ref = gen_const_mem (Pmode,
2943                                    gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
2944                                                  address));
2945         }
2946
2947       insn = emit_move_insn (reg, pic_ref);
2948 #endif
2949       current_function_uses_pic_offset_table = 1;
2950       /* Put a REG_EQUAL note on this insn, so that it can be optimized
2951          by loop.  */
2952       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
2953                                             REG_NOTES (insn));
2954       return reg;
2955     }
2956   else if (GET_CODE (orig) == CONST)
2957     {
2958       rtx base, offset;
2959
2960       if (GET_CODE (XEXP (orig, 0)) == PLUS
2961           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
2962         return orig;
2963
2964       if (reg == 0)
2965         {
2966           if (no_new_pseudos)
2967             abort ();
2968           else
2969             reg = gen_reg_rtx (Pmode);
2970         }
2971
2972       if (GET_CODE (XEXP (orig, 0)) == PLUS)
2973         {
2974           base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2975           offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2976                                            base == reg ? 0 : reg);
2977         }
2978       else
2979         abort ();
2980
2981       if (GET_CODE (offset) == CONST_INT)
2982         {
2983           /* The base register doesn't really matter, we only want to
2984              test the index for the appropriate mode.  */
2985           if (!arm_legitimate_index_p (mode, offset, SET, 0))
2986             {
2987               if (!no_new_pseudos)
2988                 offset = force_reg (Pmode, offset);
2989               else
2990                 abort ();
2991             }
2992
2993           if (GET_CODE (offset) == CONST_INT)
2994             return plus_constant (base, INTVAL (offset));
2995         }
2996
2997       if (GET_MODE_SIZE (mode) > 4
2998           && (GET_MODE_CLASS (mode) == MODE_INT
2999               || TARGET_SOFT_FLOAT))
3000         {
3001           emit_insn (gen_addsi3 (reg, base, offset));
3002           return reg;
3003         }
3004
3005       return gen_rtx_PLUS (Pmode, base, offset);
3006     }
3007
3008   return orig;
3009 }
3010
3011
3012 /* Find a spare low register.  */
3013
3014 static int
3015 thumb_find_work_register (int live_regs_mask)
3016 {
3017   int reg;
3018
3019   /* Use a spare arg register.  */
3020   if (!regs_ever_live[LAST_ARG_REGNUM])
3021     return LAST_ARG_REGNUM;
3022
3023   /* Look for a pushed register.  */
3024   for (reg = 0; reg < LAST_LO_REGNUM; reg++)
3025     if (live_regs_mask & (1 << reg))
3026       return reg;
3027
3028   /* Something went wrong.  */
3029   abort ();
3030 }
3031
3032
3033 /* Generate code to load the PIC register.  */
3034
3035 void
3036 arm_load_pic_register (void)
3037 {
3038 #ifndef AOF_ASSEMBLER
3039   rtx l1, pic_tmp, pic_tmp2, pic_rtx;
3040   rtx global_offset_table;
3041
3042   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3043     return;
3044
3045   if (!flag_pic)
3046     abort ();
3047
3048   l1 = gen_label_rtx ();
3049
3050   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3051   /* On the ARM the PC register contains 'dot + 8' at the time of the
3052      addition, on the Thumb it is 'dot + 4'.  */
3053   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
3054   if (GOT_PCREL)
3055     pic_tmp2 = gen_rtx_CONST (VOIDmode,
3056                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3057   else
3058     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3059
3060   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3061
3062   if (TARGET_ARM)
3063     {
3064       emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3065       emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
3066     }
3067   else
3068     {
3069       if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3070         {
3071           int reg;
3072
3073           /* We will have pushed the pic register, so should always be
3074              able to find a work register.  */
3075           reg = thumb_find_work_register (thumb_compute_save_reg_mask ());
3076           pic_tmp = gen_rtx_REG (SImode, reg);
3077           emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3078           emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3079         }
3080       else
3081         emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3082       emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
3083     }
3084
3085   /* Need to emit this whether or not we obey regdecls,
3086      since setjmp/longjmp can cause life info to screw up.  */
3087   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3088 #endif /* AOF_ASSEMBLER */
3089 }
3090
3091
3092 /* Return nonzero if X is valid as an ARM state addressing register.  */
3093 static int
3094 arm_address_register_rtx_p (rtx x, int strict_p)
3095 {
3096   int regno;
3097
3098   if (GET_CODE (x) != REG)
3099     return 0;
3100
3101   regno = REGNO (x);
3102
3103   if (strict_p)
3104     return ARM_REGNO_OK_FOR_BASE_P (regno);
3105
3106   return (regno <= LAST_ARM_REGNUM
3107           || regno >= FIRST_PSEUDO_REGISTER
3108           || regno == FRAME_POINTER_REGNUM
3109           || regno == ARG_POINTER_REGNUM);
3110 }
3111
3112 /* Return nonzero if X is a valid ARM state address operand.  */
3113 int
3114 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3115                           int strict_p)
3116 {
3117   bool use_ldrd;
3118   enum rtx_code code = GET_CODE (x);
3119
3120   if (arm_address_register_rtx_p (x, strict_p))
3121     return 1;
3122
3123   use_ldrd = (TARGET_LDRD
3124               && (mode == DImode
3125                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3126
3127   if (code == POST_INC || code == PRE_DEC
3128       || ((code == PRE_INC || code == POST_DEC)
3129           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3130     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3131
3132   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3133            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3134            && GET_CODE (XEXP (x, 1)) == PLUS
3135            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3136     {
3137       rtx addend = XEXP (XEXP (x, 1), 1);
3138
3139       /* Don't allow ldrd post increment by register becuase it's hard
3140          to fixup invalid register choices.  */
3141       if (use_ldrd
3142           && GET_CODE (x) == POST_MODIFY
3143           && GET_CODE (addend) == REG)
3144         return 0;
3145
3146       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3147               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3148     }
3149
3150   /* After reload constants split into minipools will have addresses
3151      from a LABEL_REF.  */
3152   else if (reload_completed
3153            && (code == LABEL_REF
3154                || (code == CONST
3155                    && GET_CODE (XEXP (x, 0)) == PLUS
3156                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3157                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3158     return 1;
3159
3160   else if (mode == TImode)
3161     return 0;
3162
3163   else if (code == PLUS)
3164     {
3165       rtx xop0 = XEXP (x, 0);
3166       rtx xop1 = XEXP (x, 1);
3167
3168       return ((arm_address_register_rtx_p (xop0, strict_p)
3169                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3170               || (arm_address_register_rtx_p (xop1, strict_p)
3171                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3172     }
3173
3174 #if 0
3175   /* Reload currently can't handle MINUS, so disable this for now */
3176   else if (GET_CODE (x) == MINUS)
3177     {
3178       rtx xop0 = XEXP (x, 0);
3179       rtx xop1 = XEXP (x, 1);
3180
3181       return (arm_address_register_rtx_p (xop0, strict_p)
3182               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3183     }
3184 #endif
3185
3186   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3187            && code == SYMBOL_REF
3188            && CONSTANT_POOL_ADDRESS_P (x)
3189            && ! (flag_pic
3190                  && symbol_mentioned_p (get_pool_constant (x))))
3191     return 1;
3192
3193   return 0;
3194 }
3195
3196 /* Return nonzero if INDEX is valid for an address index operand in
3197    ARM state.  */
3198 static int
3199 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3200                         int strict_p)
3201 {
3202   HOST_WIDE_INT range;
3203   enum rtx_code code = GET_CODE (index);
3204
3205   /* Standard coprocessor addressing modes.  */
3206   if (TARGET_HARD_FLOAT
3207       && (TARGET_FPA || TARGET_MAVERICK)
3208       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3209           || (TARGET_MAVERICK && mode == DImode)))
3210     return (code == CONST_INT && INTVAL (index) < 1024
3211             && INTVAL (index) > -1024
3212             && (INTVAL (index) & 3) == 0);
3213
3214   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3215     return (code == CONST_INT
3216             && INTVAL (index) < 1024
3217             && INTVAL (index) > -1024
3218             && (INTVAL (index) & 3) == 0);
3219
3220   if (arm_address_register_rtx_p (index, strict_p)
3221       && (GET_MODE_SIZE (mode) <= 4))
3222     return 1;
3223
3224   if (mode == DImode || mode == DFmode)
3225     {
3226       if (code == CONST_INT)
3227         {
3228           HOST_WIDE_INT val = INTVAL (index);
3229
3230           if (TARGET_LDRD)
3231             return val > -256 && val < 256;
3232           else
3233             return val > -4096 && val < 4092;
3234         }
3235
3236       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3237     }
3238
3239   if (GET_MODE_SIZE (mode) <= 4
3240       && ! (arm_arch4
3241             && (mode == HImode
3242                 || (mode == QImode && outer == SIGN_EXTEND))))
3243     {
3244       if (code == MULT)
3245         {
3246           rtx xiop0 = XEXP (index, 0);
3247           rtx xiop1 = XEXP (index, 1);
3248
3249           return ((arm_address_register_rtx_p (xiop0, strict_p)
3250                    && power_of_two_operand (xiop1, SImode))
3251                   || (arm_address_register_rtx_p (xiop1, strict_p)
3252                       && power_of_two_operand (xiop0, SImode)));
3253         }
3254       else if (code == LSHIFTRT || code == ASHIFTRT
3255                || code == ASHIFT || code == ROTATERT)
3256         {
3257           rtx op = XEXP (index, 1);
3258
3259           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3260                   && GET_CODE (op) == CONST_INT
3261                   && INTVAL (op) > 0
3262                   && INTVAL (op) <= 31);
3263         }
3264     }
3265
3266   /* For ARM v4 we may be doing a sign-extend operation during the
3267      load.  */
3268   if (arm_arch4)
3269     {
3270       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3271         range = 256;
3272       else
3273         range = 4096;
3274     }
3275   else
3276     range = (mode == HImode) ? 4095 : 4096;
3277
3278   return (code == CONST_INT
3279           && INTVAL (index) < range
3280           && INTVAL (index) > -range);
3281 }
3282
3283 /* Return nonzero if X is valid as a Thumb state base register.  */
3284 static int
3285 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3286 {
3287   int regno;
3288
3289   if (GET_CODE (x) != REG)
3290     return 0;
3291
3292   regno = REGNO (x);
3293
3294   if (strict_p)
3295     return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3296
3297   return (regno <= LAST_LO_REGNUM
3298           || regno > LAST_VIRTUAL_REGISTER
3299           || regno == FRAME_POINTER_REGNUM
3300           || (GET_MODE_SIZE (mode) >= 4
3301               && (regno == STACK_POINTER_REGNUM
3302                   || regno >= FIRST_PSEUDO_REGISTER
3303                   || x == hard_frame_pointer_rtx
3304                   || x == arg_pointer_rtx)));
3305 }
3306
3307 /* Return nonzero if x is a legitimate index register.  This is the case
3308    for any base register that can access a QImode object.  */
3309 inline static int
3310 thumb_index_register_rtx_p (rtx x, int strict_p)
3311 {
3312   return thumb_base_register_rtx_p (x, QImode, strict_p);
3313 }
3314
3315 /* Return nonzero if x is a legitimate Thumb-state address.
3316
3317    The AP may be eliminated to either the SP or the FP, so we use the
3318    least common denominator, e.g. SImode, and offsets from 0 to 64.
3319
3320    ??? Verify whether the above is the right approach.
3321
3322    ??? Also, the FP may be eliminated to the SP, so perhaps that
3323    needs special handling also.
3324
3325    ??? Look at how the mips16 port solves this problem.  It probably uses
3326    better ways to solve some of these problems.
3327
3328    Although it is not incorrect, we don't accept QImode and HImode
3329    addresses based on the frame pointer or arg pointer until the
3330    reload pass starts.  This is so that eliminating such addresses
3331    into stack based ones won't produce impossible code.  */
3332 int
3333 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3334 {
3335   /* ??? Not clear if this is right.  Experiment.  */
3336   if (GET_MODE_SIZE (mode) < 4
3337       && !(reload_in_progress || reload_completed)
3338       && (reg_mentioned_p (frame_pointer_rtx, x)
3339           || reg_mentioned_p (arg_pointer_rtx, x)
3340           || reg_mentioned_p (virtual_incoming_args_rtx, x)
3341           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3342           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3343           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3344     return 0;
3345
3346   /* Accept any base register.  SP only in SImode or larger.  */
3347   else if (thumb_base_register_rtx_p (x, mode, strict_p))
3348     return 1;
3349
3350   /* This is PC relative data before arm_reorg runs.  */
3351   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3352            && GET_CODE (x) == SYMBOL_REF
3353            && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3354     return 1;
3355
3356   /* This is PC relative data after arm_reorg runs.  */
3357   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3358            && (GET_CODE (x) == LABEL_REF
3359                || (GET_CODE (x) == CONST
3360                    && GET_CODE (XEXP (x, 0)) == PLUS
3361                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3362                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3363     return 1;
3364
3365   /* Post-inc indexing only supported for SImode and larger.  */
3366   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3367            && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3368     return 1;
3369
3370   else if (GET_CODE (x) == PLUS)
3371     {
3372       /* REG+REG address can be any two index registers.  */
3373       /* We disallow FRAME+REG addressing since we know that FRAME
3374          will be replaced with STACK, and SP relative addressing only
3375          permits SP+OFFSET.  */
3376       if (GET_MODE_SIZE (mode) <= 4
3377           && XEXP (x, 0) != frame_pointer_rtx
3378           && XEXP (x, 1) != frame_pointer_rtx
3379           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3380           && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3381         return 1;
3382
3383       /* REG+const has 5-7 bit offset for non-SP registers.  */
3384       else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3385                 || XEXP (x, 0) == arg_pointer_rtx)
3386                && GET_CODE (XEXP (x, 1)) == CONST_INT
3387                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3388         return 1;
3389
3390       /* REG+const has 10 bit offset for SP, but only SImode and
3391          larger is supported.  */
3392       /* ??? Should probably check for DI/DFmode overflow here
3393          just like GO_IF_LEGITIMATE_OFFSET does.  */
3394       else if (GET_CODE (XEXP (x, 0)) == REG
3395                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3396                && GET_MODE_SIZE (mode) >= 4
3397                && GET_CODE (XEXP (x, 1)) == CONST_INT
3398                && INTVAL (XEXP (x, 1)) >= 0
3399                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3400                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3401         return 1;
3402
3403       else if (GET_CODE (XEXP (x, 0)) == REG
3404                && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3405                && GET_MODE_SIZE (mode) >= 4
3406                && GET_CODE (XEXP (x, 1)) == CONST_INT
3407                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3408         return 1;
3409     }
3410
3411   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3412            && GET_MODE_SIZE (mode) == 4
3413            && GET_CODE (x) == SYMBOL_REF
3414            && CONSTANT_POOL_ADDRESS_P (x)
3415            && !(flag_pic
3416                 && symbol_mentioned_p (get_pool_constant (x))))
3417     return 1;
3418
3419   return 0;
3420 }
3421
3422 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3423    instruction of mode MODE.  */
3424 int
3425 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3426 {
3427   switch (GET_MODE_SIZE (mode))
3428     {
3429     case 1:
3430       return val >= 0 && val < 32;
3431
3432     case 2:
3433       return val >= 0 && val < 64 && (val & 1) == 0;
3434
3435     default:
3436       return (val >= 0
3437               && (val + GET_MODE_SIZE (mode)) <= 128
3438               && (val & 3) == 0);
3439     }
3440 }
3441
3442 /* Try machine-dependent ways of modifying an illegitimate address
3443    to be legitimate.  If we find one, return the new, valid address.  */
3444 rtx
3445 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3446 {
3447   if (GET_CODE (x) == PLUS)
3448     {
3449       rtx xop0 = XEXP (x, 0);
3450       rtx xop1 = XEXP (x, 1);
3451
3452       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3453         xop0 = force_reg (SImode, xop0);
3454
3455       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3456         xop1 = force_reg (SImode, xop1);
3457
3458       if (ARM_BASE_REGISTER_RTX_P (xop0)
3459           && GET_CODE (xop1) == CONST_INT)
3460         {
3461           HOST_WIDE_INT n, low_n;
3462           rtx base_reg, val;
3463           n = INTVAL (xop1);
3464
3465           /* VFP addressing modes actually allow greater offsets, but for
3466              now we just stick with the lowest common denominator.  */
3467           if (mode == DImode
3468               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3469             {
3470               low_n = n & 0x0f;
3471               n &= ~0x0f;
3472               if (low_n > 4)
3473                 {
3474                   n += 16;
3475                   low_n -= 16;
3476                 }
3477             }
3478           else
3479             {
3480               low_n = ((mode) == TImode ? 0
3481                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3482               n -= low_n;
3483             }
3484
3485           base_reg = gen_reg_rtx (SImode);
3486           val = force_operand (gen_rtx_PLUS (SImode, xop0,
3487                                              GEN_INT (n)), NULL_RTX);
3488           emit_move_insn (base_reg, val);
3489           x = (low_n == 0 ? base_reg
3490                : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3491         }
3492       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3493         x = gen_rtx_PLUS (SImode, xop0, xop1);
3494     }
3495
3496   /* XXX We don't allow MINUS any more -- see comment in
3497      arm_legitimate_address_p ().  */
3498   else if (GET_CODE (x) == MINUS)
3499     {
3500       rtx xop0 = XEXP (x, 0);
3501       rtx xop1 = XEXP (x, 1);
3502
3503       if (CONSTANT_P (xop0))
3504         xop0 = force_reg (SImode, xop0);
3505
3506       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3507         xop1 = force_reg (SImode, xop1);
3508
3509       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3510         x = gen_rtx_MINUS (SImode, xop0, xop1);
3511     }
3512
3513   if (flag_pic)
3514     {
3515       /* We need to find and carefully transform any SYMBOL and LABEL
3516          references; so go back to the original address expression.  */
3517       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3518
3519       if (new_x != orig_x)
3520         x = new_x;
3521     }
3522
3523   return x;
3524 }
3525
3526
3527 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3528    to be legitimate.  If we find one, return the new, valid address.  */
3529 rtx
3530 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3531 {
3532   if (GET_CODE (x) == PLUS
3533       && GET_CODE (XEXP (x, 1)) == CONST_INT
3534       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3535           || INTVAL (XEXP (x, 1)) < 0))
3536     {
3537       rtx xop0 = XEXP (x, 0);
3538       rtx xop1 = XEXP (x, 1);
3539       HOST_WIDE_INT offset = INTVAL (xop1);
3540
3541       /* Try and fold the offset into a biasing of the base register and
3542          then offsetting that.  Don't do this when optimizing for space
3543          since it can cause too many CSEs.  */
3544       if (optimize_size && offset >= 0
3545           && offset < 256 + 31 * GET_MODE_SIZE (mode))
3546         {
3547           HOST_WIDE_INT delta;
3548
3549           if (offset >= 256)
3550             delta = offset - (256 - GET_MODE_SIZE (mode));
3551           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3552             delta = 31 * GET_MODE_SIZE (mode);
3553           else
3554             delta = offset & (~31 * GET_MODE_SIZE (mode));
3555
3556           xop0 = force_operand (plus_constant (xop0, offset - delta),
3557                                 NULL_RTX);
3558           x = plus_constant (xop0, delta);
3559         }
3560       else if (offset < 0 && offset > -256)
3561         /* Small negative offsets are best done with a subtract before the
3562            dereference, forcing these into a register normally takes two
3563            instructions.  */
3564         x = force_operand (x, NULL_RTX);
3565       else
3566         {
3567           /* For the remaining cases, force the constant into a register.  */
3568           xop1 = force_reg (SImode, xop1);
3569           x = gen_rtx_PLUS (SImode, xop0, xop1);
3570         }
3571     }
3572   else if (GET_CODE (x) == PLUS
3573            && s_register_operand (XEXP (x, 1), SImode)
3574            && !s_register_operand (XEXP (x, 0), SImode))
3575     {
3576       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3577
3578       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3579     }
3580
3581   if (flag_pic)
3582     {
3583       /* We need to find and carefully transform any SYMBOL and LABEL
3584          references; so go back to the original address expression.  */
3585       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3586
3587       if (new_x != orig_x)
3588         x = new_x;
3589     }
3590
3591   return x;
3592 }
3593
3594 \f
3595
3596 #define REG_OR_SUBREG_REG(X)                                            \
3597   (GET_CODE (X) == REG                                                  \
3598    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3599
3600 #define REG_OR_SUBREG_RTX(X)                    \
3601    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3602
3603 #ifndef COSTS_N_INSNS
3604 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3605 #endif
3606 static inline int
3607 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3608 {
3609   enum machine_mode mode = GET_MODE (x);
3610
3611   switch (code)
3612     {
3613     case ASHIFT:
3614     case ASHIFTRT:
3615     case LSHIFTRT:
3616     case ROTATERT:
3617     case PLUS:
3618     case MINUS:
3619     case COMPARE:
3620     case NEG:
3621     case NOT:
3622       return COSTS_N_INSNS (1);
3623
3624     case MULT:
3625       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3626         {
3627           int cycles = 0;
3628           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3629
3630           while (i)
3631             {
3632               i >>= 2;
3633               cycles++;
3634             }
3635           return COSTS_N_INSNS (2) + cycles;
3636         }
3637       return COSTS_N_INSNS (1) + 16;
3638
3639     case SET:
3640       return (COSTS_N_INSNS (1)
3641               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3642                      + GET_CODE (SET_DEST (x)) == MEM));
3643
3644     case CONST_INT:
3645       if (outer == SET)
3646         {
3647           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3648             return 0;
3649           if (thumb_shiftable_const (INTVAL (x)))
3650             return COSTS_N_INSNS (2);
3651           return COSTS_N_INSNS (3);
3652         }
3653       else if ((outer == PLUS || outer == COMPARE)
3654                && INTVAL (x) < 256 && INTVAL (x) > -256)
3655         return 0;
3656       else if (outer == AND
3657                && INTVAL (x) < 256 && INTVAL (x) >= -256)
3658         return COSTS_N_INSNS (1);
3659       else if (outer == ASHIFT || outer == ASHIFTRT
3660                || outer == LSHIFTRT)
3661         return 0;
3662       return COSTS_N_INSNS (2);
3663
3664     case CONST:
3665     case CONST_DOUBLE:
3666     case LABEL_REF:
3667     case SYMBOL_REF:
3668       return COSTS_N_INSNS (3);
3669
3670     case UDIV:
3671     case UMOD:
3672     case DIV:
3673     case MOD:
3674       return 100;
3675
3676     case TRUNCATE:
3677       return 99;
3678
3679     case AND:
3680     case XOR:
3681     case IOR:
3682       /* XXX guess.  */
3683       return 8;
3684
3685     case MEM:
3686       /* XXX another guess.  */
3687       /* Memory costs quite a lot for the first word, but subsequent words
3688          load at the equivalent of a single insn each.  */
3689       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3690               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3691                  ? 4 : 0));
3692
3693     case IF_THEN_ELSE:
3694       /* XXX a guess.  */
3695       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3696         return 14;
3697       return 2;
3698
3699     case ZERO_EXTEND:
3700       /* XXX still guessing.  */
3701       switch (GET_MODE (XEXP (x, 0)))
3702         {
3703         case QImode:
3704           return (1 + (mode == DImode ? 4 : 0)
3705                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3706
3707         case HImode:
3708           return (4 + (mode == DImode ? 4 : 0)
3709                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3710
3711         case SImode:
3712           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3713
3714         default:
3715           return 99;
3716         }
3717
3718     default:
3719       return 99;
3720     }
3721 }
3722
3723
3724 /* Worker routine for arm_rtx_costs.  */
3725 static inline int
3726 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3727 {
3728   enum machine_mode mode = GET_MODE (x);
3729   enum rtx_code subcode;
3730   int extra_cost;
3731
3732   switch (code)
3733     {
3734     case MEM:
3735       /* Memory costs quite a lot for the first word, but subsequent words
3736          load at the equivalent of a single insn each.  */
3737       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3738               + (GET_CODE (x) == SYMBOL_REF
3739                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3740
3741     case DIV:
3742     case MOD:
3743     case UDIV:
3744     case UMOD:
3745       return optimize_size ? COSTS_N_INSNS (2) : 100;
3746
3747     case ROTATE:
3748       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3749         return 4;
3750       /* Fall through */
3751     case ROTATERT:
3752       if (mode != SImode)
3753         return 8;
3754       /* Fall through */
3755     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3756       if (mode == DImode)
3757         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3758                 + ((GET_CODE (XEXP (x, 0)) == REG
3759                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3760                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3761                    ? 0 : 8));
3762       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3763                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3764                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3765                    ? 0 : 4)
3766               + ((GET_CODE (XEXP (x, 1)) == REG
3767                   || (GET_CODE (XEXP (x, 1)) == SUBREG
3768                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3769                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3770                  ? 0 : 4));
3771
3772     case MINUS:
3773       if (mode == DImode)
3774         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3775                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3776                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
3777                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3778                    ? 0 : 8));
3779
3780       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3781         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3782                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3783                           && arm_const_double_rtx (XEXP (x, 1))))
3784                      ? 0 : 8)
3785                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3786                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3787                         && arm_const_double_rtx (XEXP (x, 0))))
3788                    ? 0 : 8));
3789
3790       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3791             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3792             && REG_OR_SUBREG_REG (XEXP (x, 1))))
3793           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3794                || subcode == ASHIFTRT || subcode == LSHIFTRT
3795                || subcode == ROTATE || subcode == ROTATERT
3796                || (subcode == MULT
3797                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3798                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3799                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3800               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3801               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3802                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3803               && REG_OR_SUBREG_REG (XEXP (x, 0))))
3804         return 1;
3805       /* Fall through */
3806
3807     case PLUS:
3808       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3809         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3810                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3811                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3812                         && arm_const_double_rtx (XEXP (x, 1))))
3813                    ? 0 : 8));
3814
3815       /* Fall through */
3816     case AND: case XOR: case IOR:
3817       extra_cost = 0;
3818
3819       /* Normally the frame registers will be spilt into reg+const during
3820          reload, so it is a bad idea to combine them with other instructions,
3821          since then they might not be moved outside of loops.  As a compromise
3822          we allow integration with ops that have a constant as their second
3823          operand.  */
3824       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3825            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3826            && GET_CODE (XEXP (x, 1)) != CONST_INT)
3827           || (REG_OR_SUBREG_REG (XEXP (x, 0))
3828               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3829         extra_cost = 4;
3830
3831       if (mode == DImode)
3832         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3833                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3834                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3835                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3836                    ? 0 : 8));
3837
3838       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3839         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3840                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3841                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3842                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3843                    ? 0 : 4));
3844
3845       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3846         return (1 + extra_cost
3847                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3848                      || subcode == LSHIFTRT || subcode == ASHIFTRT
3849                      || subcode == ROTATE || subcode == ROTATERT
3850                      || (subcode == MULT
3851                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3852                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3853                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3854                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3855                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3856                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3857                    ? 0 : 4));
3858
3859       return 8;
3860
3861     case MULT:
3862       /* This should have been handled by the CPU specific routines.  */
3863       abort ();
3864
3865     case TRUNCATE:
3866       if (arm_arch3m && mode == SImode
3867           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
3868           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
3869           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
3870               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
3871           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
3872               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
3873         return 8;
3874       return 99;
3875
3876     case NEG:
3877       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3878         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
3879       /* Fall through */
3880     case NOT:
3881       if (mode == DImode)
3882         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3883
3884       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3885
3886     case IF_THEN_ELSE:
3887       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3888         return 14;
3889       return 2;
3890
3891     case COMPARE:
3892       return 1;
3893
3894     case ABS:
3895       return 4 + (mode == DImode ? 4 : 0);
3896
3897     case SIGN_EXTEND:
3898       if (GET_MODE (XEXP (x, 0)) == QImode)
3899         return (4 + (mode == DImode ? 4 : 0)
3900                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3901       /* Fall through */
3902     case ZERO_EXTEND:
3903       switch (GET_MODE (XEXP (x, 0)))
3904         {
3905         case QImode:
3906           return (1 + (mode == DImode ? 4 : 0)
3907                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3908
3909         case HImode:
3910           return (4 + (mode == DImode ? 4 : 0)
3911                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3912
3913         case SImode:
3914           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3915
3916         case V8QImode:
3917         case V4HImode:
3918         case V2SImode:
3919         case V4QImode:
3920         case V2HImode:
3921             return 1;
3922
3923         default:
3924           break;
3925         }
3926       abort ();
3927
3928     case CONST_INT:
3929       if (const_ok_for_arm (INTVAL (x)))
3930         return outer == SET ? 2 : -1;
3931       else if (outer == AND
3932                && const_ok_for_arm (~INTVAL (x)))
3933         return -1;
3934       else if ((outer == COMPARE
3935                 || outer == PLUS || outer == MINUS)
3936                && const_ok_for_arm (-INTVAL (x)))
3937         return -1;
3938       else
3939         return 5;
3940
3941     case CONST:
3942     case LABEL_REF:
3943     case SYMBOL_REF:
3944       return 6;
3945
3946     case CONST_DOUBLE:
3947       if (arm_const_double_rtx (x))
3948         return outer == SET ? 2 : -1;
3949       else if ((outer == COMPARE || outer == PLUS)
3950                && neg_const_double_rtx_ok_for_fpa (x))
3951         return -1;
3952       return 7;
3953
3954     default:
3955       return 99;
3956     }
3957 }
3958
3959 /* RTX costs when optimizing for size.  */
3960 static bool
3961 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
3962 {
3963   enum machine_mode mode = GET_MODE (x);
3964
3965   if (TARGET_THUMB)
3966     {
3967       /* XXX TBD.  For now, use the standard costs.  */
3968       *total = thumb_rtx_costs (x, code, outer_code);
3969       return true;
3970     }
3971
3972   switch (code)
3973     {
3974     case MEM:
3975       /* A memory access costs 1 insn if the mode is small, or the address is
3976          a single register, otherwise it costs one insn per word.  */
3977       if (REG_P (XEXP (x, 0)))
3978         *total = COSTS_N_INSNS (1);
3979       else
3980         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
3981       return true;
3982
3983     case DIV:
3984     case MOD:
3985     case UDIV:
3986     case UMOD:
3987       /* Needs a libcall, so it costs about this.  */
3988       *total = COSTS_N_INSNS (2);
3989       return false;
3990
3991     case ROTATE:
3992       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3993         {
3994           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
3995           return true;
3996         }
3997       /* Fall through */
3998     case ROTATERT:
3999     case ASHIFT:
4000     case LSHIFTRT:
4001     case ASHIFTRT:
4002       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4003         {
4004           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4005           return true;
4006         }
4007       else if (mode == SImode)
4008         {
4009           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4010           /* Slightly disparage register shifts, but not by much.  */
4011           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4012             *total += 1 + rtx_cost (XEXP (x, 1), code);
4013           return true;
4014         }
4015
4016       /* Needs a libcall.  */
4017       *total = COSTS_N_INSNS (2);
4018       return false;
4019
4020     case MINUS:
4021       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4022         {
4023           *total = COSTS_N_INSNS (1);
4024           return false;
4025         }
4026
4027       if (mode == SImode)
4028         {
4029           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4030           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4031
4032           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4033               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4034               || subcode1 == ROTATE || subcode1 == ROTATERT
4035               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4036               || subcode1 == ASHIFTRT)
4037             {
4038               /* It's just the cost of the two operands.  */
4039               *total = 0;
4040               return false;
4041             }
4042
4043           *total = COSTS_N_INSNS (1);
4044           return false;
4045         }
4046
4047       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4048       return false;
4049
4050     case PLUS:
4051       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4052         {
4053           *total = COSTS_N_INSNS (1);
4054           return false;
4055         }
4056
4057       /* Fall through */
4058     case AND: case XOR: case IOR:
4059       if (mode == SImode)
4060         {
4061           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4062
4063           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4064               || subcode == LSHIFTRT || subcode == ASHIFTRT
4065               || (code == AND && subcode == NOT))
4066             {
4067               /* It's just the cost of the two operands.  */
4068               *total = 0;
4069               return false;
4070             }
4071         }
4072
4073       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4074       return false;
4075
4076     case MULT:
4077       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4078       return false;
4079
4080     case NEG:
4081       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4082         *total = COSTS_N_INSNS (1);
4083       /* Fall through */
4084     case NOT:
4085       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4086
4087       return false;
4088
4089     case IF_THEN_ELSE:
4090       *total = 0;
4091       return false;
4092
4093     case COMPARE:
4094       if (cc_register (XEXP (x, 0), VOIDmode))
4095         * total = 0;
4096       else
4097         *total = COSTS_N_INSNS (1);
4098       return false;
4099
4100     case ABS:
4101       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4102         *total = COSTS_N_INSNS (1);
4103       else
4104         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4105       return false;
4106
4107     case SIGN_EXTEND:
4108       *total = 0;
4109       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4110         {
4111           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4112             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4113         }
4114       if (mode == DImode)
4115         *total += COSTS_N_INSNS (1);
4116       return false;
4117
4118     case ZERO_EXTEND:
4119       *total = 0;
4120       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4121         {
4122           switch (GET_MODE (XEXP (x, 0)))
4123             {
4124             case QImode:
4125               *total += COSTS_N_INSNS (1);
4126               break;
4127
4128             case HImode:
4129               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4130
4131             case SImode:
4132               break;
4133
4134             default:
4135               *total += COSTS_N_INSNS (2);
4136             }
4137         }
4138
4139       if (mode == DImode)
4140         *total += COSTS_N_INSNS (1);
4141
4142       return false;
4143
4144     case CONST_INT:
4145       if (const_ok_for_arm (INTVAL (x)))
4146         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4147       else if (const_ok_for_arm (~INTVAL (x)))
4148         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4149       else if (const_ok_for_arm (-INTVAL (x)))
4150         {
4151           if (outer_code == COMPARE || outer_code == PLUS
4152               || outer_code == MINUS)
4153             *total = 0;
4154           else
4155             *total = COSTS_N_INSNS (1);
4156         }
4157       else
4158         *total = COSTS_N_INSNS (2);
4159       return true;
4160
4161     case CONST:
4162     case LABEL_REF:
4163     case SYMBOL_REF:
4164       *total = COSTS_N_INSNS (2);
4165       return true;
4166
4167     case CONST_DOUBLE:
4168       *total = COSTS_N_INSNS (4);
4169       return true;
4170
4171     default:
4172       if (mode != VOIDmode)
4173         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4174       else
4175         *total = COSTS_N_INSNS (4); /* How knows?  */
4176       return false;
4177     }
4178 }
4179
4180 /* RTX costs for cores with a slow MUL implementation.  */
4181
4182 static bool
4183 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4184 {
4185   enum machine_mode mode = GET_MODE (x);
4186
4187   if (TARGET_THUMB)
4188     {
4189       *total = thumb_rtx_costs (x, code, outer_code);
4190       return true;
4191     }
4192
4193   switch (code)
4194     {
4195     case MULT:
4196       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4197           || mode == DImode)
4198         {
4199           *total = 30;
4200           return true;
4201         }
4202
4203       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4204         {
4205           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4206                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4207           int cost, const_ok = const_ok_for_arm (i);
4208           int j, booth_unit_size;
4209
4210           /* Tune as appropriate.  */
4211           cost = const_ok ? 4 : 8;
4212           booth_unit_size = 2;
4213           for (j = 0; i && j < 32; j += booth_unit_size)
4214             {
4215               i >>= booth_unit_size;
4216               cost += 2;
4217             }
4218
4219           *total = cost;
4220           return true;
4221         }
4222
4223       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4224                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4225       return true;
4226
4227     default:
4228       *total = arm_rtx_costs_1 (x, code, outer_code);
4229       return true;
4230     }
4231 }
4232
4233
4234 /* RTX cost for cores with a fast multiply unit (M variants).  */
4235
4236 static bool
4237 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4238 {
4239   enum machine_mode mode = GET_MODE (x);
4240
4241   if (TARGET_THUMB)
4242     {
4243       *total = thumb_rtx_costs (x, code, outer_code);
4244       return true;
4245     }
4246
4247   switch (code)
4248     {
4249     case MULT:
4250       /* There is no point basing this on the tuning, since it is always the
4251          fast variant if it exists at all.  */
4252       if (mode == DImode
4253           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4254           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4255               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4256         {
4257           *total = 8;
4258           return true;
4259         }
4260
4261
4262       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4263           || mode == DImode)
4264         {
4265           *total = 30;
4266           return true;
4267         }
4268
4269       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4270         {
4271           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4272                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4273           int cost, const_ok = const_ok_for_arm (i);
4274           int j, booth_unit_size;
4275
4276           /* Tune as appropriate.  */
4277           cost = const_ok ? 4 : 8;
4278           booth_unit_size = 8;
4279           for (j = 0; i && j < 32; j += booth_unit_size)
4280             {
4281               i >>= booth_unit_size;
4282               cost += 2;
4283             }
4284
4285           *total = cost;
4286           return true;
4287         }
4288
4289       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4290                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4291       return true;
4292
4293     default:
4294       *total = arm_rtx_costs_1 (x, code, outer_code);
4295       return true;
4296     }
4297 }
4298
4299
4300 /* RTX cost for XScale CPUs.  */
4301
4302 static bool
4303 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4304 {
4305   enum machine_mode mode = GET_MODE (x);
4306
4307   if (TARGET_THUMB)
4308     {
4309       *total = thumb_rtx_costs (x, code, outer_code);
4310       return true;
4311     }
4312
4313   switch (code)
4314     {
4315     case MULT:
4316       /* There is no point basing this on the tuning, since it is always the
4317          fast variant if it exists at all.  */
4318       if (mode == DImode
4319           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4320           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4321               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4322         {
4323           *total = 8;
4324           return true;
4325         }
4326
4327
4328       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4329           || mode == DImode)
4330         {
4331           *total = 30;
4332           return true;
4333         }
4334
4335       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4336         {
4337           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4338                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4339           int cost, const_ok = const_ok_for_arm (i);
4340           unsigned HOST_WIDE_INT masked_const;
4341
4342           /* The cost will be related to two insns.
4343              First a load of the constant (MOV or LDR), then a multiply.  */
4344           cost = 2;
4345           if (! const_ok)
4346             cost += 1;      /* LDR is probably more expensive because
4347                                of longer result latency.  */
4348           masked_const = i & 0xffff8000;
4349           if (masked_const != 0 && masked_const != 0xffff8000)
4350             {
4351               masked_const = i & 0xf8000000;
4352               if (masked_const == 0 || masked_const == 0xf8000000)
4353                 cost += 1;
4354               else
4355                 cost += 2;
4356             }
4357           *total = cost;
4358           return true;
4359         }
4360
4361       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4362                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4363       return true;
4364
4365     default:
4366       *total = arm_rtx_costs_1 (x, code, outer_code);
4367       return true;
4368     }
4369 }
4370
4371
4372 /* RTX costs for 9e (and later) cores.  */
4373
4374 static bool
4375 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4376 {
4377   enum machine_mode mode = GET_MODE (x);
4378   int nonreg_cost;
4379   int cost;
4380
4381   if (TARGET_THUMB)
4382     {
4383       switch (code)
4384         {
4385         case MULT:
4386           *total = COSTS_N_INSNS (3);
4387           return true;
4388
4389         default:
4390           *total = thumb_rtx_costs (x, code, outer_code);
4391           return true;
4392         }
4393     }
4394
4395   switch (code)
4396     {
4397     case MULT:
4398       /* There is no point basing this on the tuning, since it is always the
4399          fast variant if it exists at all.  */
4400       if (mode == DImode
4401           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4402           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4403               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4404         {
4405           *total = 3;
4406           return true;
4407         }
4408
4409
4410       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4411         {
4412           *total = 30;
4413           return true;
4414         }
4415       if (mode == DImode)
4416         {
4417           cost = 7;
4418           nonreg_cost = 8;
4419         }
4420       else
4421         {
4422           cost = 2;
4423           nonreg_cost = 4;
4424         }
4425
4426
4427       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4428                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4429       return true;
4430
4431     default:
4432       *total = arm_rtx_costs_1 (x, code, outer_code);
4433       return true;
4434     }
4435 }
4436 /* All address computations that can be done are free, but rtx cost returns
4437    the same for practically all of them.  So we weight the different types
4438    of address here in the order (most pref first):
4439    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
4440 static inline int
4441 arm_arm_address_cost (rtx x)
4442 {
4443   enum rtx_code c  = GET_CODE (x);
4444
4445   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4446     return 0;
4447   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4448     return 10;
4449
4450   if (c == PLUS || c == MINUS)
4451     {
4452       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4453         return 2;
4454
4455       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4456         return 3;
4457
4458       return 4;
4459     }
4460
4461   return 6;
4462 }
4463
4464 static inline int
4465 arm_thumb_address_cost (rtx x)
4466 {
4467   enum rtx_code c  = GET_CODE (x);
4468
4469   if (c == REG)
4470     return 1;
4471   if (c == PLUS
4472       && GET_CODE (XEXP (x, 0)) == REG
4473       && GET_CODE (XEXP (x, 1)) == CONST_INT)
4474     return 1;
4475
4476   return 2;
4477 }
4478
4479 static int
4480 arm_address_cost (rtx x)
4481 {
4482   return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4483 }
4484
4485 static int
4486 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4487 {
4488   rtx i_pat, d_pat;
4489
4490   /* Some true dependencies can have a higher cost depending
4491      on precisely how certain input operands are used.  */
4492   if (arm_tune_xscale
4493       && REG_NOTE_KIND (link) == 0
4494       && recog_memoized (insn) >= 0
4495       && recog_memoized (dep) >= 0)
4496     {
4497       int shift_opnum = get_attr_shift (insn);
4498       enum attr_type attr_type = get_attr_type (dep);
4499
4500       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4501          operand for INSN.  If we have a shifted input operand and the
4502          instruction we depend on is another ALU instruction, then we may
4503          have to account for an additional stall.  */
4504       if (shift_opnum != 0
4505           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4506         {
4507           rtx shifted_operand;
4508           int opno;
4509
4510           /* Get the shifted operand.  */
4511           extract_insn (insn);
4512           shifted_operand = recog_data.operand[shift_opnum];
4513
4514           /* Iterate over all the operands in DEP.  If we write an operand
4515              that overlaps with SHIFTED_OPERAND, then we have increase the
4516              cost of this dependency.  */
4517           extract_insn (dep);
4518           preprocess_constraints ();
4519           for (opno = 0; opno < recog_data.n_operands; opno++)
4520             {
4521               /* We can ignore strict inputs.  */
4522               if (recog_data.operand_type[opno] == OP_IN)
4523                 continue;
4524
4525               if (reg_overlap_mentioned_p (recog_data.operand[opno],
4526                                            shifted_operand))
4527                 return 2;
4528             }
4529         }
4530     }
4531
4532   /* XXX This is not strictly true for the FPA.  */
4533   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4534       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4535     return 0;
4536
4537   /* Call insns don't incur a stall, even if they follow a load.  */
4538   if (REG_NOTE_KIND (link) == 0
4539       && GET_CODE (insn) == CALL_INSN)
4540     return 1;
4541
4542   if ((i_pat = single_set (insn)) != NULL
4543       && GET_CODE (SET_SRC (i_pat)) == MEM
4544       && (d_pat = single_set (dep)) != NULL
4545       && GET_CODE (SET_DEST (d_pat)) == MEM)
4546     {
4547       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4548       /* This is a load after a store, there is no conflict if the load reads
4549          from a cached area.  Assume that loads from the stack, and from the
4550          constant pool are cached, and that others will miss.  This is a
4551          hack.  */
4552
4553       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4554           || reg_mentioned_p (stack_pointer_rtx, src_mem)
4555           || reg_mentioned_p (frame_pointer_rtx, src_mem)
4556           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4557         return 1;
4558     }
4559
4560   return cost;
4561 }
4562
4563 static int fp_consts_inited = 0;
4564
4565 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
4566 static const char * const strings_fp[8] =
4567 {
4568   "0",   "1",   "2",   "3",
4569   "4",   "5",   "0.5", "10"
4570 };
4571
4572 static REAL_VALUE_TYPE values_fp[8];
4573
4574 static void
4575 init_fp_table (void)
4576 {
4577   int i;
4578   REAL_VALUE_TYPE r;
4579
4580   if (TARGET_VFP)
4581     fp_consts_inited = 1;
4582   else
4583     fp_consts_inited = 8;
4584
4585   for (i = 0; i < fp_consts_inited; i++)
4586     {
4587       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4588       values_fp[i] = r;
4589     }
4590 }
4591
4592 /* Return TRUE if rtx X is a valid immediate FP constant.  */
4593 int
4594 arm_const_double_rtx (rtx x)
4595 {
4596   REAL_VALUE_TYPE r;
4597   int i;
4598
4599   if (!fp_consts_inited)
4600     init_fp_table ();
4601
4602   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4603   if (REAL_VALUE_MINUS_ZERO (r))
4604     return 0;
4605
4606   for (i = 0; i < fp_consts_inited; i++)
4607     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4608       return 1;
4609
4610   return 0;
4611 }
4612
4613 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
4614 int
4615 neg_const_double_rtx_ok_for_fpa (rtx x)
4616 {
4617   REAL_VALUE_TYPE r;
4618   int i;
4619
4620   if (!fp_consts_inited)
4621     init_fp_table ();
4622
4623   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4624   r = REAL_VALUE_NEGATE (r);
4625   if (REAL_VALUE_MINUS_ZERO (r))
4626     return 0;
4627
4628   for (i = 0; i < 8; i++)
4629     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4630       return 1;
4631
4632   return 0;
4633 }
4634 \f
4635 /* Predicates for `match_operand' and `match_operator'.  */
4636
4637 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
4638 int
4639 cirrus_memory_offset (rtx op)
4640 {
4641   /* Reject eliminable registers.  */
4642   if (! (reload_in_progress || reload_completed)
4643       && (   reg_mentioned_p (frame_pointer_rtx, op)
4644           || reg_mentioned_p (arg_pointer_rtx, op)
4645           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4646           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4647           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4648           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4649     return 0;
4650
4651   if (GET_CODE (op) == MEM)
4652     {
4653       rtx ind;
4654
4655       ind = XEXP (op, 0);
4656
4657       /* Match: (mem (reg)).  */
4658       if (GET_CODE (ind) == REG)
4659         return 1;
4660
4661       /* Match:
4662          (mem (plus (reg)
4663                     (const))).  */
4664       if (GET_CODE (ind) == PLUS
4665           && GET_CODE (XEXP (ind, 0)) == REG
4666           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4667           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4668         return 1;
4669     }
4670
4671   return 0;
4672 }
4673
4674 /* Return TRUE if OP is a valid VFP memory address pattern.
4675    WB if true if writeback address modes are allowed.  */
4676
4677 int
4678 arm_coproc_mem_operand (rtx op, bool wb)
4679 {
4680   rtx ind;
4681
4682   /* Reject eliminable registers.  */
4683   if (! (reload_in_progress || reload_completed)
4684       && (   reg_mentioned_p (frame_pointer_rtx, op)
4685           || reg_mentioned_p (arg_pointer_rtx, op)
4686           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4687           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4688           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4689           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4690     return FALSE;
4691
4692   /* Constants are converted into offsets from labels.  */
4693   if (GET_CODE (op) != MEM)
4694     return FALSE;
4695
4696   ind = XEXP (op, 0);
4697
4698   if (reload_completed
4699       && (GET_CODE (ind) == LABEL_REF
4700           || (GET_CODE (ind) == CONST
4701               && GET_CODE (XEXP (ind, 0)) == PLUS
4702               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4703               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4704     return TRUE;
4705
4706   /* Match: (mem (reg)).  */
4707   if (GET_CODE (ind) == REG)
4708     return arm_address_register_rtx_p (ind, 0);
4709
4710   /* Autoincremment addressing modes.  */
4711   if (wb
4712       && (GET_CODE (ind) == PRE_INC
4713           || GET_CODE (ind) == POST_INC
4714           || GET_CODE (ind) == PRE_DEC
4715           || GET_CODE (ind) == POST_DEC))
4716     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
4717
4718   if (wb
4719       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
4720       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
4721       && GET_CODE (XEXP (ind, 1)) == PLUS
4722       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
4723     ind = XEXP (ind, 1);
4724
4725   /* Match:
4726      (plus (reg)
4727            (const)).  */
4728   if (GET_CODE (ind) == PLUS
4729       && GET_CODE (XEXP (ind, 0)) == REG
4730       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4731       && GET_CODE (XEXP (ind, 1)) == CONST_INT
4732       && INTVAL (XEXP (ind, 1)) > -1024
4733       && INTVAL (XEXP (ind, 1)) <  1024
4734       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
4735     return TRUE;
4736
4737   return FALSE;
4738 }
4739
4740
4741 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4742    VFP registers.  Otherwise return NO_REGS.  */
4743
4744 enum reg_class
4745 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4746 {
4747   if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
4748     return NO_REGS;
4749
4750   return GENERAL_REGS;
4751 }
4752
4753
4754 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4755    Use by the Cirrus Maverick code which has to workaround
4756    a hardware bug triggered by such instructions.  */
4757 static bool
4758 arm_memory_load_p (rtx insn)
4759 {
4760   rtx body, lhs, rhs;;
4761
4762   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4763     return false;
4764
4765   body = PATTERN (insn);
4766
4767   if (GET_CODE (body) != SET)
4768     return false;
4769
4770   lhs = XEXP (body, 0);
4771   rhs = XEXP (body, 1);
4772
4773   lhs = REG_OR_SUBREG_RTX (lhs);
4774
4775   /* If the destination is not a general purpose
4776      register we do not have to worry.  */
4777   if (GET_CODE (lhs) != REG
4778       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4779     return false;
4780
4781   /* As well as loads from memory we also have to react
4782      to loads of invalid constants which will be turned
4783      into loads from the minipool.  */
4784   return (GET_CODE (rhs) == MEM
4785           || GET_CODE (rhs) == SYMBOL_REF
4786           || note_invalid_constants (insn, -1, false));
4787 }
4788
4789 /* Return TRUE if INSN is a Cirrus instruction.  */
4790 static bool
4791 arm_cirrus_insn_p (rtx insn)
4792 {
4793   enum attr_cirrus attr;
4794
4795   /* get_attr aborts on USE and CLOBBER.  */
4796   if (!insn
4797       || GET_CODE (insn) != INSN
4798       || GET_CODE (PATTERN (insn)) == USE
4799       || GET_CODE (PATTERN (insn)) == CLOBBER)
4800     return 0;
4801
4802   attr = get_attr_cirrus (insn);
4803
4804   return attr != CIRRUS_NOT;
4805 }
4806
4807 /* Cirrus reorg for invalid instruction combinations.  */
4808 static void
4809 cirrus_reorg (rtx first)
4810 {
4811   enum attr_cirrus attr;
4812   rtx body = PATTERN (first);
4813   rtx t;
4814   int nops;
4815
4816   /* Any branch must be followed by 2 non Cirrus instructions.  */
4817   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4818     {
4819       nops = 0;
4820       t = next_nonnote_insn (first);
4821
4822       if (arm_cirrus_insn_p (t))
4823         ++ nops;
4824
4825       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4826         ++ nops;
4827
4828       while (nops --)
4829         emit_insn_after (gen_nop (), first);
4830
4831       return;
4832     }
4833
4834   /* (float (blah)) is in parallel with a clobber.  */
4835   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4836     body = XVECEXP (body, 0, 0);
4837
4838   if (GET_CODE (body) == SET)
4839     {
4840       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4841
4842       /* cfldrd, cfldr64, cfstrd, cfstr64 must
4843          be followed by a non Cirrus insn.  */
4844       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
4845         {
4846           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
4847             emit_insn_after (gen_nop (), first);
4848
4849           return;
4850         }
4851       else if (arm_memory_load_p (first))
4852         {
4853           unsigned int arm_regno;
4854
4855           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
4856              ldr/cfmv64hr combination where the Rd field is the same
4857              in both instructions must be split with a non Cirrus
4858              insn.  Example:
4859
4860              ldr r0, blah
4861              nop
4862              cfmvsr mvf0, r0.  */
4863
4864           /* Get Arm register number for ldr insn.  */
4865           if (GET_CODE (lhs) == REG)
4866             arm_regno = REGNO (lhs);
4867           else if (GET_CODE (rhs) == REG)
4868             arm_regno = REGNO (rhs);
4869           else
4870             abort ();
4871
4872           /* Next insn.  */
4873           first = next_nonnote_insn (first);
4874
4875           if (! arm_cirrus_insn_p (first))
4876             return;
4877
4878           body = PATTERN (first);
4879
4880           /* (float (blah)) is in parallel with a clobber.  */
4881           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
4882             body = XVECEXP (body, 0, 0);
4883
4884           if (GET_CODE (body) == FLOAT)
4885             body = XEXP (body, 0);
4886
4887           if (get_attr_cirrus (first) == CIRRUS_MOVE
4888               && GET_CODE (XEXP (body, 1)) == REG
4889               && arm_regno == REGNO (XEXP (body, 1)))
4890             emit_insn_after (gen_nop (), first);
4891
4892           return;
4893         }
4894     }
4895
4896   /* get_attr aborts on USE and CLOBBER.  */
4897   if (!first
4898       || GET_CODE (first) != INSN
4899       || GET_CODE (PATTERN (first)) == USE
4900       || GET_CODE (PATTERN (first)) == CLOBBER)
4901     return;
4902
4903   attr = get_attr_cirrus (first);
4904
4905   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
4906      must be followed by a non-coprocessor instruction.  */
4907   if (attr == CIRRUS_COMPARE)
4908     {
4909       nops = 0;
4910
4911       t = next_nonnote_insn (first);
4912
4913       if (arm_cirrus_insn_p (t))
4914         ++ nops;
4915
4916       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4917         ++ nops;
4918
4919       while (nops --)
4920         emit_insn_after (gen_nop (), first);
4921
4922       return;
4923     }
4924 }
4925
4926 /* Return TRUE if X references a SYMBOL_REF.  */
4927 int
4928 symbol_mentioned_p (rtx x)
4929 {
4930   const char * fmt;
4931   int i;
4932
4933   if (GET_CODE (x) == SYMBOL_REF)
4934     return 1;
4935
4936   fmt = GET_RTX_FORMAT (GET_CODE (x));
4937
4938   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4939     {
4940       if (fmt[i] == 'E')
4941         {
4942           int j;
4943
4944           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4945             if (symbol_mentioned_p (XVECEXP (x, i, j)))
4946               return 1;
4947         }
4948       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
4949         return 1;
4950     }
4951
4952   return 0;
4953 }
4954
4955 /* Return TRUE if X references a LABEL_REF.  */
4956 int
4957 label_mentioned_p (rtx x)
4958 {
4959   const char * fmt;
4960   int i;
4961
4962   if (GET_CODE (x) == LABEL_REF)
4963     return 1;
4964
4965   fmt = GET_RTX_FORMAT (GET_CODE (x));
4966   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4967     {
4968       if (fmt[i] == 'E')
4969         {
4970           int j;
4971
4972           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4973             if (label_mentioned_p (XVECEXP (x, i, j)))
4974               return 1;
4975         }
4976       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
4977         return 1;
4978     }
4979
4980   return 0;
4981 }
4982
4983 enum rtx_code
4984 minmax_code (rtx x)
4985 {
4986   enum rtx_code code = GET_CODE (x);
4987
4988   if (code == SMAX)
4989     return GE;
4990   else if (code == SMIN)
4991     return LE;
4992   else if (code == UMIN)
4993     return LEU;
4994   else if (code == UMAX)
4995     return GEU;
4996
4997   abort ();
4998 }
4999
5000 /* Return 1 if memory locations are adjacent.  */
5001 int
5002 adjacent_mem_locations (rtx a, rtx b)
5003 {
5004   if ((GET_CODE (XEXP (a, 0)) == REG
5005        || (GET_CODE (XEXP (a, 0)) == PLUS
5006            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5007       && (GET_CODE (XEXP (b, 0)) == REG
5008           || (GET_CODE (XEXP (b, 0)) == PLUS
5009               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5010     {
5011       int val0 = 0, val1 = 0;
5012       int reg0, reg1;
5013
5014       if (GET_CODE (XEXP (a, 0)) == PLUS)
5015         {
5016           reg0 = REGNO  (XEXP (XEXP (a, 0), 0));
5017           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5018         }
5019       else
5020         reg0 = REGNO (XEXP (a, 0));
5021
5022       if (GET_CODE (XEXP (b, 0)) == PLUS)
5023         {
5024           reg1 = REGNO  (XEXP (XEXP (b, 0), 0));
5025           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5026         }
5027       else
5028         reg1 = REGNO (XEXP (b, 0));
5029
5030       /* Don't accept any offset that will require multiple
5031          instructions to handle, since this would cause the
5032          arith_adjacentmem pattern to output an overlong sequence.  */
5033       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5034         return 0;
5035
5036       return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
5037     }
5038   return 0;
5039 }
5040
5041 int
5042 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5043                         HOST_WIDE_INT *load_offset)
5044 {
5045   int unsorted_regs[4];
5046   HOST_WIDE_INT unsorted_offsets[4];
5047   int order[4];
5048   int base_reg = -1;
5049   int i;
5050
5051   /* Can only handle 2, 3, or 4 insns at present,
5052      though could be easily extended if required.  */
5053   if (nops < 2 || nops > 4)
5054     abort ();
5055
5056   /* Loop over the operands and check that the memory references are
5057      suitable (ie immediate offsets from the same base register).  At
5058      the same time, extract the target register, and the memory
5059      offsets.  */
5060   for (i = 0; i < nops; i++)
5061     {
5062       rtx reg;
5063       rtx offset;
5064
5065       /* Convert a subreg of a mem into the mem itself.  */
5066       if (GET_CODE (operands[nops + i]) == SUBREG)
5067         operands[nops + i] = alter_subreg (operands + (nops + i));
5068
5069       if (GET_CODE (operands[nops + i]) != MEM)
5070         abort ();
5071
5072       /* Don't reorder volatile memory references; it doesn't seem worth
5073          looking for the case where the order is ok anyway.  */
5074       if (MEM_VOLATILE_P (operands[nops + i]))
5075         return 0;
5076
5077       offset = const0_rtx;
5078
5079       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5080            || (GET_CODE (reg) == SUBREG
5081                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5082           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5083               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5084                    == REG)
5085                   || (GET_CODE (reg) == SUBREG
5086                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5087               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5088                   == CONST_INT)))
5089         {
5090           if (i == 0)
5091             {
5092               base_reg = REGNO (reg);
5093               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5094                                   ? REGNO (operands[i])
5095                                   : REGNO (SUBREG_REG (operands[i])));
5096               order[0] = 0;
5097             }
5098           else
5099             {
5100               if (base_reg != (int) REGNO (reg))
5101                 /* Not addressed from the same base register.  */
5102                 return 0;
5103
5104               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5105                                   ? REGNO (operands[i])
5106                                   : REGNO (SUBREG_REG (operands[i])));
5107               if (unsorted_regs[i] < unsorted_regs[order[0]])
5108                 order[0] = i;
5109             }
5110
5111           /* If it isn't an integer register, or if it overwrites the
5112              base register but isn't the last insn in the list, then
5113              we can't do this.  */
5114           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5115               || (i != nops - 1 && unsorted_regs[i] == base_reg))
5116             return 0;
5117
5118           unsorted_offsets[i] = INTVAL (offset);
5119         }
5120       else
5121         /* Not a suitable memory address.  */
5122         return 0;
5123     }
5124
5125   /* All the useful information has now been extracted from the
5126      operands into unsorted_regs and unsorted_offsets; additionally,
5127      order[0] has been set to the lowest numbered register in the
5128      list.  Sort the registers into order, and check that the memory
5129      offsets are ascending and adjacent.  */
5130
5131   for (i = 1; i < nops; i++)
5132     {
5133       int j;
5134
5135       order[i] = order[i - 1];
5136       for (j = 0; j < nops; j++)
5137         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5138             && (order[i] == order[i - 1]
5139                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5140           order[i] = j;
5141
5142       /* Have we found a suitable register? if not, one must be used more
5143          than once.  */
5144       if (order[i] == order[i - 1])
5145         return 0;
5146
5147       /* Is the memory address adjacent and ascending? */
5148       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5149         return 0;
5150     }
5151
5152   if (base)
5153     {
5154       *base = base_reg;
5155
5156       for (i = 0; i < nops; i++)
5157         regs[i] = unsorted_regs[order[i]];
5158
5159       *load_offset = unsorted_offsets[order[0]];
5160     }
5161
5162   if (unsorted_offsets[order[0]] == 0)
5163     return 1; /* ldmia */
5164
5165   if (unsorted_offsets[order[0]] == 4)
5166     return 2; /* ldmib */
5167
5168   if (unsorted_offsets[order[nops - 1]] == 0)
5169     return 3; /* ldmda */
5170
5171   if (unsorted_offsets[order[nops - 1]] == -4)
5172     return 4; /* ldmdb */
5173
5174   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5175      if the offset isn't small enough.  The reason 2 ldrs are faster
5176      is because these ARMs are able to do more than one cache access
5177      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
5178      whilst the ARM8 has a double bandwidth cache.  This means that
5179      these cores can do both an instruction fetch and a data fetch in
5180      a single cycle, so the trick of calculating the address into a
5181      scratch register (one of the result regs) and then doing a load
5182      multiple actually becomes slower (and no smaller in code size).
5183      That is the transformation
5184
5185         ldr     rd1, [rbase + offset]
5186         ldr     rd2, [rbase + offset + 4]
5187
5188      to
5189
5190         add     rd1, rbase, offset
5191         ldmia   rd1, {rd1, rd2}
5192
5193      produces worse code -- '3 cycles + any stalls on rd2' instead of
5194      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
5195      access per cycle, the first sequence could never complete in less
5196      than 6 cycles, whereas the ldm sequence would only take 5 and
5197      would make better use of sequential accesses if not hitting the
5198      cache.
5199
5200      We cheat here and test 'arm_ld_sched' which we currently know to
5201      only be true for the ARM8, ARM9 and StrongARM.  If this ever
5202      changes, then the test below needs to be reworked.  */
5203   if (nops == 2 && arm_ld_sched)
5204     return 0;
5205
5206   /* Can't do it without setting up the offset, only do this if it takes
5207      no more than one insn.  */
5208   return (const_ok_for_arm (unsorted_offsets[order[0]])
5209           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5210 }
5211
5212 const char *
5213 emit_ldm_seq (rtx *operands, int nops)
5214 {
5215   int regs[4];
5216   int base_reg;
5217   HOST_WIDE_INT offset;
5218   char buf[100];
5219   int i;
5220
5221   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5222     {
5223     case 1:
5224       strcpy (buf, "ldm%?ia\t");
5225       break;
5226
5227     case 2:
5228       strcpy (buf, "ldm%?ib\t");
5229       break;
5230
5231     case 3:
5232       strcpy (buf, "ldm%?da\t");
5233       break;
5234
5235     case 4:
5236       strcpy (buf, "ldm%?db\t");
5237       break;
5238
5239     case 5:
5240       if (offset >= 0)
5241         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5242                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5243                  (long) offset);
5244       else
5245         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5246                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5247                  (long) -offset);
5248       output_asm_insn (buf, operands);
5249       base_reg = regs[0];
5250       strcpy (buf, "ldm%?ia\t");
5251       break;
5252
5253     default:
5254       abort ();
5255     }
5256
5257   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5258            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5259
5260   for (i = 1; i < nops; i++)
5261     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5262              reg_names[regs[i]]);
5263
5264   strcat (buf, "}\t%@ phole ldm");
5265
5266   output_asm_insn (buf, operands);
5267   return "";
5268 }
5269
5270 int
5271 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5272                          HOST_WIDE_INT * load_offset)
5273 {
5274   int unsorted_regs[4];
5275   HOST_WIDE_INT unsorted_offsets[4];
5276   int order[4];
5277   int base_reg = -1;
5278   int i;
5279
5280   /* Can only handle 2, 3, or 4 insns at present, though could be easily
5281      extended if required.  */
5282   if (nops < 2 || nops > 4)
5283     abort ();
5284
5285   /* Loop over the operands and check that the memory references are
5286      suitable (ie immediate offsets from the same base register).  At
5287      the same time, extract the target register, and the memory
5288      offsets.  */
5289   for (i = 0; i < nops; i++)
5290     {
5291       rtx reg;
5292       rtx offset;
5293
5294       /* Convert a subreg of a mem into the mem itself.  */
5295       if (GET_CODE (operands[nops + i]) == SUBREG)
5296         operands[nops + i] = alter_subreg (operands + (nops + i));
5297
5298       if (GET_CODE (operands[nops + i]) != MEM)
5299         abort ();
5300
5301       /* Don't reorder volatile memory references; it doesn't seem worth
5302          looking for the case where the order is ok anyway.  */
5303       if (MEM_VOLATILE_P (operands[nops + i]))
5304         return 0;
5305
5306       offset = const0_rtx;
5307
5308       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5309            || (GET_CODE (reg) == SUBREG
5310                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5311           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5312               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5313                    == REG)
5314                   || (GET_CODE (reg) == SUBREG
5315                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5316               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5317                   == CONST_INT)))
5318         {
5319           if (i == 0)
5320             {
5321               base_reg = REGNO (reg);
5322               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5323                                   ? REGNO (operands[i])
5324                                   : REGNO (SUBREG_REG (operands[i])));
5325               order[0] = 0;
5326             }
5327           else
5328             {
5329               if (base_reg != (int) REGNO (reg))
5330                 /* Not addressed from the same base register.  */
5331                 return 0;
5332
5333               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5334                                   ? REGNO (operands[i])
5335                                   : REGNO (SUBREG_REG (operands[i])));
5336               if (unsorted_regs[i] < unsorted_regs[order[0]])
5337                 order[0] = i;
5338             }
5339
5340           /* If it isn't an integer register, then we can't do this.  */
5341           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5342             return 0;
5343
5344           unsorted_offsets[i] = INTVAL (offset);
5345         }
5346       else
5347         /* Not a suitable memory address.  */
5348         return 0;
5349     }
5350
5351   /* All the useful information has now been extracted from the
5352      operands into unsorted_regs and unsorted_offsets; additionally,
5353      order[0] has been set to the lowest numbered register in the
5354      list.  Sort the registers into order, and check that the memory
5355      offsets are ascending and adjacent.  */
5356
5357   for (i = 1; i < nops; i++)
5358     {
5359       int j;
5360
5361       order[i] = order[i - 1];
5362       for (j = 0; j < nops; j++)
5363         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5364             && (order[i] == order[i - 1]
5365                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5366           order[i] = j;
5367
5368       /* Have we found a suitable register? if not, one must be used more
5369          than once.  */
5370       if (order[i] == order[i - 1])
5371         return 0;
5372
5373       /* Is the memory address adjacent and ascending? */
5374       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5375         return 0;
5376     }
5377
5378   if (base)
5379     {
5380       *base = base_reg;
5381
5382       for (i = 0; i < nops; i++)
5383         regs[i] = unsorted_regs[order[i]];
5384
5385       *load_offset = unsorted_offsets[order[0]];
5386     }
5387
5388   if (unsorted_offsets[order[0]] == 0)
5389     return 1; /* stmia */
5390
5391   if (unsorted_offsets[order[0]] == 4)
5392     return 2; /* stmib */
5393
5394   if (unsorted_offsets[order[nops - 1]] == 0)
5395     return 3; /* stmda */
5396
5397   if (unsorted_offsets[order[nops - 1]] == -4)
5398     return 4; /* stmdb */
5399
5400   return 0;
5401 }
5402
5403 const char *
5404 emit_stm_seq (rtx *operands, int nops)
5405 {
5406   int regs[4];
5407   int base_reg;
5408   HOST_WIDE_INT offset;
5409   char buf[100];
5410   int i;
5411
5412   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5413     {
5414     case 1:
5415       strcpy (buf, "stm%?ia\t");
5416       break;
5417
5418     case 2:
5419       strcpy (buf, "stm%?ib\t");
5420       break;
5421
5422     case 3:
5423       strcpy (buf, "stm%?da\t");
5424       break;
5425
5426     case 4:
5427       strcpy (buf, "stm%?db\t");
5428       break;
5429
5430     default:
5431       abort ();
5432     }
5433
5434   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5435            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5436
5437   for (i = 1; i < nops; i++)
5438     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5439              reg_names[regs[i]]);
5440
5441   strcat (buf, "}\t%@ phole stm");
5442
5443   output_asm_insn (buf, operands);
5444   return "";
5445 }
5446
5447 \f
5448 /* Routines for use in generating RTL.  */
5449
5450 rtx
5451 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5452                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5453 {
5454   HOST_WIDE_INT offset = *offsetp;
5455   int i = 0, j;
5456   rtx result;
5457   int sign = up ? 1 : -1;
5458   rtx mem, addr;
5459
5460   /* XScale has load-store double instructions, but they have stricter
5461      alignment requirements than load-store multiple, so we cannot
5462      use them.
5463
5464      For XScale ldm requires 2 + NREGS cycles to complete and blocks
5465      the pipeline until completion.
5466
5467         NREGS           CYCLES
5468           1               3
5469           2               4
5470           3               5
5471           4               6
5472
5473      An ldr instruction takes 1-3 cycles, but does not block the
5474      pipeline.
5475
5476         NREGS           CYCLES
5477           1              1-3
5478           2              2-6
5479           3              3-9
5480           4              4-12
5481
5482      Best case ldr will always win.  However, the more ldr instructions
5483      we issue, the less likely we are to be able to schedule them well.
5484      Using ldr instructions also increases code size.
5485
5486      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5487      for counts of 3 or 4 regs.  */
5488   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5489     {
5490       rtx seq;
5491
5492       start_sequence ();
5493
5494       for (i = 0; i < count; i++)
5495         {
5496           addr = plus_constant (from, i * 4 * sign);
5497           mem = adjust_automodify_address (basemem, SImode, addr, offset);
5498           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5499           offset += 4 * sign;
5500         }
5501
5502       if (write_back)
5503         {
5504           emit_move_insn (from, plus_constant (from, count * 4 * sign));
5505           *offsetp = offset;
5506         }
5507
5508       seq = get_insns ();
5509       end_sequence ();
5510
5511       return seq;
5512     }
5513
5514   result = gen_rtx_PARALLEL (VOIDmode,
5515                              rtvec_alloc (count + (write_back ? 1 : 0)));
5516   if (write_back)
5517     {
5518       XVECEXP (result, 0, 0)
5519         = gen_rtx_SET (GET_MODE (from), from,
5520                        plus_constant (from, count * 4 * sign));
5521       i = 1;
5522       count++;
5523     }
5524
5525   for (j = 0; i < count; i++, j++)
5526     {
5527       addr = plus_constant (from, j * 4 * sign);
5528       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5529       XVECEXP (result, 0, i)
5530         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5531       offset += 4 * sign;
5532     }
5533
5534   if (write_back)
5535     *offsetp = offset;
5536
5537   return result;
5538 }
5539
5540 rtx
5541 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5542                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5543 {
5544   HOST_WIDE_INT offset = *offsetp;
5545   int i = 0, j;
5546   rtx result;
5547   int sign = up ? 1 : -1;
5548   rtx mem, addr;
5549
5550   /* See arm_gen_load_multiple for discussion of
5551      the pros/cons of ldm/stm usage for XScale.  */
5552   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5553     {
5554       rtx seq;
5555
5556       start_sequence ();
5557
5558       for (i = 0; i < count; i++)
5559         {
5560           addr = plus_constant (to, i * 4 * sign);
5561           mem = adjust_automodify_address (basemem, SImode, addr, offset);
5562           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5563           offset += 4 * sign;
5564         }
5565
5566       if (write_back)
5567         {
5568           emit_move_insn (to, plus_constant (to, count * 4 * sign));
5569           *offsetp = offset;
5570         }
5571
5572       seq = get_insns ();
5573       end_sequence ();
5574
5575       return seq;
5576     }
5577
5578   result = gen_rtx_PARALLEL (VOIDmode,
5579                              rtvec_alloc (count + (write_back ? 1 : 0)));
5580   if (write_back)
5581     {
5582       XVECEXP (result, 0, 0)
5583         = gen_rtx_SET (GET_MODE (to), to,
5584                        plus_constant (to, count * 4 * sign));
5585       i = 1;
5586       count++;
5587     }
5588
5589   for (j = 0; i < count; i++, j++)
5590     {
5591       addr = plus_constant (to, j * 4 * sign);
5592       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5593       XVECEXP (result, 0, i)
5594         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5595       offset += 4 * sign;
5596     }
5597
5598   if (write_back)
5599     *offsetp = offset;
5600
5601   return result;
5602 }
5603
5604 int
5605 arm_gen_movmemqi (rtx *operands)
5606 {
5607   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5608   HOST_WIDE_INT srcoffset, dstoffset;
5609   int i;
5610   rtx src, dst, srcbase, dstbase;
5611   rtx part_bytes_reg = NULL;
5612   rtx mem;
5613
5614   if (GET_CODE (operands[2]) != CONST_INT
5615       || GET_CODE (operands[3]) != CONST_INT
5616       || INTVAL (operands[2]) > 64
5617       || INTVAL (operands[3]) & 3)
5618     return 0;
5619
5620   dstbase = operands[0];
5621   srcbase = operands[1];
5622
5623   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
5624   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
5625
5626   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5627   out_words_to_go = INTVAL (operands[2]) / 4;
5628   last_bytes = INTVAL (operands[2]) & 3;
5629   dstoffset = srcoffset = 0;
5630
5631   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5632     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5633
5634   for (i = 0; in_words_to_go >= 2; i+=4)
5635     {
5636       if (in_words_to_go > 4)
5637         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5638                                           srcbase, &srcoffset));
5639       else
5640         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
5641                                           FALSE, srcbase, &srcoffset));
5642
5643       if (out_words_to_go)
5644         {
5645           if (out_words_to_go > 4)
5646             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5647                                                dstbase, &dstoffset));
5648           else if (out_words_to_go != 1)
5649             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5650                                                dst, TRUE,
5651                                                (last_bytes == 0
5652                                                 ? FALSE : TRUE),
5653                                                dstbase, &dstoffset));
5654           else
5655             {
5656               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5657               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5658               if (last_bytes != 0)
5659                 {
5660                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5661                   dstoffset += 4;
5662                 }
5663             }
5664         }
5665
5666       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5667       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
5668     }
5669
5670   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
5671   if (out_words_to_go)
5672     {
5673       rtx sreg;
5674
5675       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5676       sreg = copy_to_reg (mem);
5677
5678       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5679       emit_move_insn (mem, sreg);
5680       in_words_to_go--;
5681
5682       if (in_words_to_go)       /* Sanity check */
5683         abort ();
5684     }
5685
5686   if (in_words_to_go)
5687     {
5688       if (in_words_to_go < 0)
5689         abort ();
5690
5691       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5692       part_bytes_reg = copy_to_mode_reg (SImode, mem);
5693     }
5694
5695   if (last_bytes && part_bytes_reg == NULL)
5696     abort ();
5697
5698   if (BYTES_BIG_ENDIAN && last_bytes)
5699     {
5700       rtx tmp = gen_reg_rtx (SImode);
5701
5702       /* The bytes we want are in the top end of the word.  */
5703       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
5704                               GEN_INT (8 * (4 - last_bytes))));
5705       part_bytes_reg = tmp;
5706
5707       while (last_bytes)
5708         {
5709           mem = adjust_automodify_address (dstbase, QImode,
5710                                            plus_constant (dst, last_bytes - 1),
5711                                            dstoffset + last_bytes - 1);
5712           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5713
5714           if (--last_bytes)
5715             {
5716               tmp = gen_reg_rtx (SImode);
5717               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
5718               part_bytes_reg = tmp;
5719             }
5720         }
5721
5722     }
5723   else
5724     {
5725       if (last_bytes > 1)
5726         {
5727           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
5728           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
5729           last_bytes -= 2;
5730           if (last_bytes)
5731             {
5732               rtx tmp = gen_reg_rtx (SImode);
5733               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
5734               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
5735               part_bytes_reg = tmp;
5736               dstoffset += 2;
5737             }
5738         }
5739
5740       if (last_bytes)
5741         {
5742           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
5743           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5744         }
5745     }
5746
5747   return 1;
5748 }
5749
5750 /* Generate a memory reference for a half word, such that it will be loaded
5751    into the top 16 bits of the word.  We can assume that the address is
5752    known to be alignable and of the form reg, or plus (reg, const).  */
5753
5754 rtx
5755 arm_gen_rotated_half_load (rtx memref)
5756 {
5757   HOST_WIDE_INT offset = 0;
5758   rtx base = XEXP (memref, 0);
5759
5760   if (GET_CODE (base) == PLUS)
5761     {
5762       offset = INTVAL (XEXP (base, 1));
5763       base = XEXP (base, 0);
5764     }
5765
5766   /* If we aren't allowed to generate unaligned addresses, then fail.  */
5767   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0))
5768     return NULL;
5769
5770   base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
5771
5772   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
5773     return base;
5774
5775   return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
5776 }
5777
5778 /* Select a dominance comparison mode if possible for a test of the general
5779    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
5780    COND_OR == DOM_CC_X_AND_Y => (X && Y)
5781    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
5782    COND_OR == DOM_CC_X_OR_Y => (X || Y)
5783    In all cases OP will be either EQ or NE, but we don't need to know which
5784    here.  If we are unable to support a dominance comparison we return
5785    CC mode.  This will then fail to match for the RTL expressions that
5786    generate this call.  */
5787 enum machine_mode
5788 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
5789 {
5790   enum rtx_code cond1, cond2;
5791   int swapped = 0;
5792
5793   /* Currently we will probably get the wrong result if the individual
5794      comparisons are not simple.  This also ensures that it is safe to
5795      reverse a comparison if necessary.  */
5796   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
5797        != CCmode)
5798       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
5799           != CCmode))
5800     return CCmode;
5801
5802   /* The if_then_else variant of this tests the second condition if the
5803      first passes, but is true if the first fails.  Reverse the first
5804      condition to get a true "inclusive-or" expression.  */
5805   if (cond_or == DOM_CC_NX_OR_Y)
5806     cond1 = reverse_condition (cond1);
5807
5808   /* If the comparisons are not equal, and one doesn't dominate the other,
5809      then we can't do this.  */
5810   if (cond1 != cond2
5811       && !comparison_dominates_p (cond1, cond2)
5812       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
5813     return CCmode;
5814
5815   if (swapped)
5816     {
5817       enum rtx_code temp = cond1;
5818       cond1 = cond2;
5819       cond2 = temp;
5820     }
5821
5822   switch (cond1)
5823     {
5824     case EQ:
5825       if (cond2 == EQ || cond_or == DOM_CC_X_AND_Y)
5826         return CC_DEQmode;
5827
5828       switch (cond2)
5829         {
5830         case LE: return CC_DLEmode;
5831         case LEU: return CC_DLEUmode;
5832         case GE: return CC_DGEmode;
5833         case GEU: return CC_DGEUmode;
5834         default: break;
5835         }
5836
5837       break;
5838
5839     case LT:
5840       if (cond2 == LT || cond_or == DOM_CC_X_AND_Y)
5841         return CC_DLTmode;
5842       if (cond2 == LE)
5843         return CC_DLEmode;
5844       if (cond2 == NE)
5845         return CC_DNEmode;
5846       break;
5847
5848     case GT:
5849       if (cond2 == GT || cond_or == DOM_CC_X_AND_Y)
5850         return CC_DGTmode;
5851       if (cond2 == GE)
5852         return CC_DGEmode;
5853       if (cond2 == NE)
5854         return CC_DNEmode;
5855       break;
5856
5857     case LTU:
5858       if (cond2 == LTU || cond_or == DOM_CC_X_AND_Y)
5859         return CC_DLTUmode;
5860       if (cond2 == LEU)
5861         return CC_DLEUmode;
5862       if (cond2 == NE)
5863         return CC_DNEmode;
5864       break;
5865
5866     case GTU:
5867       if (cond2 == GTU || cond_or == DOM_CC_X_AND_Y)
5868         return CC_DGTUmode;
5869       if (cond2 == GEU)
5870         return CC_DGEUmode;
5871       if (cond2 == NE)
5872         return CC_DNEmode;
5873       break;
5874
5875     /* The remaining cases only occur when both comparisons are the
5876        same.  */
5877     case NE:
5878       return CC_DNEmode;
5879
5880     case LE:
5881       return CC_DLEmode;
5882
5883     case GE:
5884       return CC_DGEmode;
5885
5886     case LEU:
5887       return CC_DLEUmode;
5888
5889     case GEU:
5890       return CC_DGEUmode;
5891
5892     default:
5893       break;
5894     }
5895
5896   abort ();
5897 }
5898
5899 enum machine_mode
5900 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
5901 {
5902   /* All floating point compares return CCFP if it is an equality
5903      comparison, and CCFPE otherwise.  */
5904   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
5905     {
5906       switch (op)
5907         {
5908         case EQ:
5909         case NE:
5910         case UNORDERED:
5911         case ORDERED:
5912         case UNLT:
5913         case UNLE:
5914         case UNGT:
5915         case UNGE:
5916         case UNEQ:
5917         case LTGT:
5918           return CCFPmode;
5919
5920         case LT:
5921         case LE:
5922         case GT:
5923         case GE:
5924           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
5925             return CCFPmode;
5926           return CCFPEmode;
5927
5928         default:
5929           abort ();
5930         }
5931     }
5932
5933   /* A compare with a shifted operand.  Because of canonicalization, the
5934      comparison will have to be swapped when we emit the assembler.  */
5935   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
5936       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
5937           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
5938           || GET_CODE (x) == ROTATERT))
5939     return CC_SWPmode;
5940
5941   /* This is a special case that is used by combine to allow a
5942      comparison of a shifted byte load to be split into a zero-extend
5943      followed by a comparison of the shifted integer (only valid for
5944      equalities and unsigned inequalities).  */
5945   if (GET_MODE (x) == SImode
5946       && GET_CODE (x) == ASHIFT
5947       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
5948       && GET_CODE (XEXP (x, 0)) == SUBREG
5949       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
5950       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
5951       && (op == EQ || op == NE
5952           || op == GEU || op == GTU || op == LTU || op == LEU)
5953       && GET_CODE (y) == CONST_INT)
5954     return CC_Zmode;
5955
5956   /* A construct for a conditional compare, if the false arm contains
5957      0, then both conditions must be true, otherwise either condition
5958      must be true.  Not all conditions are possible, so CCmode is
5959      returned if it can't be done.  */
5960   if (GET_CODE (x) == IF_THEN_ELSE
5961       && (XEXP (x, 2) == const0_rtx
5962           || XEXP (x, 2) == const1_rtx)
5963       && COMPARISON_P (XEXP (x, 0))
5964       && COMPARISON_P (XEXP (x, 1)))
5965     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
5966                                          INTVAL (XEXP (x, 2)));
5967
5968   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
5969   if (GET_CODE (x) == AND
5970       && COMPARISON_P (XEXP (x, 0))
5971       && COMPARISON_P (XEXP (x, 1)))
5972     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
5973                                          DOM_CC_X_AND_Y);
5974
5975   if (GET_CODE (x) == IOR
5976       && COMPARISON_P (XEXP (x, 0))
5977       && COMPARISON_P (XEXP (x, 1)))
5978     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
5979                                          DOM_CC_X_OR_Y);
5980
5981   /* An operation (on Thumb) where we want to test for a single bit.
5982      This is done by shifting that bit up into the top bit of a
5983      scratch register; we can then branch on the sign bit.  */
5984   if (TARGET_THUMB
5985       && GET_MODE (x) == SImode
5986       && (op == EQ || op == NE)
5987       && (GET_CODE (x) == ZERO_EXTRACT))
5988     return CC_Nmode;
5989
5990   /* An operation that sets the condition codes as a side-effect, the
5991      V flag is not set correctly, so we can only use comparisons where
5992      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
5993      instead.)  */
5994   if (GET_MODE (x) == SImode
5995       && y == const0_rtx
5996       && (op == EQ || op == NE || op == LT || op == GE)
5997       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
5998           || GET_CODE (x) == AND || GET_CODE (x) == IOR
5999           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6000           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6001           || GET_CODE (x) == LSHIFTRT
6002           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6003           || GET_CODE (x) == ROTATERT
6004           || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6005     return CC_NOOVmode;
6006
6007   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6008     return CC_Zmode;
6009
6010   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6011       && GET_CODE (x) == PLUS
6012       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6013     return CC_Cmode;
6014
6015   return CCmode;
6016 }
6017
6018 /* X and Y are two things to compare using CODE.  Emit the compare insn and
6019    return the rtx for register 0 in the proper mode.  FP means this is a
6020    floating point compare: I don't think that it is needed on the arm.  */
6021 rtx
6022 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6023 {
6024   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6025   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6026
6027   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6028                           gen_rtx_COMPARE (mode, x, y)));
6029
6030   return cc_reg;
6031 }
6032
6033 /* Generate a sequence of insns that will generate the correct return
6034    address mask depending on the physical architecture that the program
6035    is running on.  */
6036 rtx
6037 arm_gen_return_addr_mask (void)
6038 {
6039   rtx reg = gen_reg_rtx (Pmode);
6040
6041   emit_insn (gen_return_addr_mask (reg));
6042   return reg;
6043 }
6044
6045 void
6046 arm_reload_in_hi (rtx *operands)
6047 {
6048   rtx ref = operands[1];
6049   rtx base, scratch;
6050   HOST_WIDE_INT offset = 0;
6051
6052   if (GET_CODE (ref) == SUBREG)
6053     {
6054       offset = SUBREG_BYTE (ref);
6055       ref = SUBREG_REG (ref);
6056     }
6057
6058   if (GET_CODE (ref) == REG)
6059     {
6060       /* We have a pseudo which has been spilt onto the stack; there
6061          are two cases here: the first where there is a simple
6062          stack-slot replacement and a second where the stack-slot is
6063          out of range, or is used as a subreg.  */
6064       if (reg_equiv_mem[REGNO (ref)])
6065         {
6066           ref = reg_equiv_mem[REGNO (ref)];
6067           base = find_replacement (&XEXP (ref, 0));
6068         }
6069       else
6070         /* The slot is out of range, or was dressed up in a SUBREG.  */
6071         base = reg_equiv_address[REGNO (ref)];
6072     }
6073   else
6074     base = find_replacement (&XEXP (ref, 0));
6075
6076   /* Handle the case where the address is too complex to be offset by 1.  */
6077   if (GET_CODE (base) == MINUS
6078       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6079     {
6080       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6081
6082       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6083       base = base_plus;
6084     }
6085   else if (GET_CODE (base) == PLUS)
6086     {
6087       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6088       HOST_WIDE_INT hi, lo;
6089
6090       offset += INTVAL (XEXP (base, 1));
6091       base = XEXP (base, 0);
6092
6093       /* Rework the address into a legal sequence of insns.  */
6094       /* Valid range for lo is -4095 -> 4095 */
6095       lo = (offset >= 0
6096             ? (offset & 0xfff)
6097             : -((-offset) & 0xfff));
6098
6099       /* Corner case, if lo is the max offset then we would be out of range
6100          once we have added the additional 1 below, so bump the msb into the
6101          pre-loading insn(s).  */
6102       if (lo == 4095)
6103         lo &= 0x7ff;
6104
6105       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6106              ^ (HOST_WIDE_INT) 0x80000000)
6107             - (HOST_WIDE_INT) 0x80000000);
6108
6109       if (hi + lo != offset)
6110         abort ();
6111
6112       if (hi != 0)
6113         {
6114           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6115
6116           /* Get the base address; addsi3 knows how to handle constants
6117              that require more than one insn.  */
6118           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6119           base = base_plus;
6120           offset = lo;
6121         }
6122     }
6123
6124   /* Operands[2] may overlap operands[0] (though it won't overlap
6125      operands[1]), that's why we asked for a DImode reg -- so we can
6126      use the bit that does not overlap.  */
6127   if (REGNO (operands[2]) == REGNO (operands[0]))
6128     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6129   else
6130     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6131
6132   emit_insn (gen_zero_extendqisi2 (scratch,
6133                                    gen_rtx_MEM (QImode,
6134                                                 plus_constant (base,
6135                                                                offset))));
6136   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6137                                    gen_rtx_MEM (QImode,
6138                                                 plus_constant (base,
6139                                                                offset + 1))));
6140   if (!BYTES_BIG_ENDIAN)
6141     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6142                         gen_rtx_IOR (SImode,
6143                                      gen_rtx_ASHIFT
6144                                      (SImode,
6145                                       gen_rtx_SUBREG (SImode, operands[0], 0),
6146                                       GEN_INT (8)),
6147                                      scratch)));
6148   else
6149     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6150                             gen_rtx_IOR (SImode,
6151                                          gen_rtx_ASHIFT (SImode, scratch,
6152                                                          GEN_INT (8)),
6153                                          gen_rtx_SUBREG (SImode, operands[0],
6154                                                          0))));
6155 }
6156
6157 /* Handle storing a half-word to memory during reload by synthesizing as two
6158    byte stores.  Take care not to clobber the input values until after we
6159    have moved them somewhere safe.  This code assumes that if the DImode
6160    scratch in operands[2] overlaps either the input value or output address
6161    in some way, then that value must die in this insn (we absolutely need
6162    two scratch registers for some corner cases).  */
6163 void
6164 arm_reload_out_hi (rtx *operands)
6165 {
6166   rtx ref = operands[0];
6167   rtx outval = operands[1];
6168   rtx base, scratch;
6169   HOST_WIDE_INT offset = 0;
6170
6171   if (GET_CODE (ref) == SUBREG)
6172     {
6173       offset = SUBREG_BYTE (ref);
6174       ref = SUBREG_REG (ref);
6175     }
6176
6177   if (GET_CODE (ref) == REG)
6178     {
6179       /* We have a pseudo which has been spilt onto the stack; there
6180          are two cases here: the first where there is a simple
6181          stack-slot replacement and a second where the stack-slot is
6182          out of range, or is used as a subreg.  */
6183       if (reg_equiv_mem[REGNO (ref)])
6184         {
6185           ref = reg_equiv_mem[REGNO (ref)];
6186           base = find_replacement (&XEXP (ref, 0));
6187         }
6188       else
6189         /* The slot is out of range, or was dressed up in a SUBREG.  */
6190         base = reg_equiv_address[REGNO (ref)];
6191     }
6192   else
6193     base = find_replacement (&XEXP (ref, 0));
6194
6195   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6196
6197   /* Handle the case where the address is too complex to be offset by 1.  */
6198   if (GET_CODE (base) == MINUS
6199       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6200     {
6201       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6202
6203       /* Be careful not to destroy OUTVAL.  */
6204       if (reg_overlap_mentioned_p (base_plus, outval))
6205         {
6206           /* Updating base_plus might destroy outval, see if we can
6207              swap the scratch and base_plus.  */
6208           if (!reg_overlap_mentioned_p (scratch, outval))
6209             {
6210               rtx tmp = scratch;
6211               scratch = base_plus;
6212               base_plus = tmp;
6213             }
6214           else
6215             {
6216               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6217
6218               /* Be conservative and copy OUTVAL into the scratch now,
6219                  this should only be necessary if outval is a subreg
6220                  of something larger than a word.  */
6221               /* XXX Might this clobber base?  I can't see how it can,
6222                  since scratch is known to overlap with OUTVAL, and
6223                  must be wider than a word.  */
6224               emit_insn (gen_movhi (scratch_hi, outval));
6225               outval = scratch_hi;
6226             }
6227         }
6228
6229       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6230       base = base_plus;
6231     }
6232   else if (GET_CODE (base) == PLUS)
6233     {
6234       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6235       HOST_WIDE_INT hi, lo;
6236
6237       offset += INTVAL (XEXP (base, 1));
6238       base = XEXP (base, 0);
6239
6240       /* Rework the address into a legal sequence of insns.  */
6241       /* Valid range for lo is -4095 -> 4095 */
6242       lo = (offset >= 0
6243             ? (offset & 0xfff)
6244             : -((-offset) & 0xfff));
6245
6246       /* Corner case, if lo is the max offset then we would be out of range
6247          once we have added the additional 1 below, so bump the msb into the
6248          pre-loading insn(s).  */
6249       if (lo == 4095)
6250         lo &= 0x7ff;
6251
6252       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6253              ^ (HOST_WIDE_INT) 0x80000000)
6254             - (HOST_WIDE_INT) 0x80000000);
6255
6256       if (hi + lo != offset)
6257         abort ();
6258
6259       if (hi != 0)
6260         {
6261           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6262
6263           /* Be careful not to destroy OUTVAL.  */
6264           if (reg_overlap_mentioned_p (base_plus, outval))
6265             {
6266               /* Updating base_plus might destroy outval, see if we
6267                  can swap the scratch and base_plus.  */
6268               if (!reg_overlap_mentioned_p (scratch, outval))
6269                 {
6270                   rtx tmp = scratch;
6271                   scratch = base_plus;
6272                   base_plus = tmp;
6273                 }
6274               else
6275                 {
6276                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6277
6278                   /* Be conservative and copy outval into scratch now,
6279                      this should only be necessary if outval is a
6280                      subreg of something larger than a word.  */
6281                   /* XXX Might this clobber base?  I can't see how it
6282                      can, since scratch is known to overlap with
6283                      outval.  */
6284                   emit_insn (gen_movhi (scratch_hi, outval));
6285                   outval = scratch_hi;
6286                 }
6287             }
6288
6289           /* Get the base address; addsi3 knows how to handle constants
6290              that require more than one insn.  */
6291           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6292           base = base_plus;
6293           offset = lo;
6294         }
6295     }
6296
6297   if (BYTES_BIG_ENDIAN)
6298     {
6299       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6300                                          plus_constant (base, offset + 1)),
6301                             gen_lowpart (QImode, outval)));
6302       emit_insn (gen_lshrsi3 (scratch,
6303                               gen_rtx_SUBREG (SImode, outval, 0),
6304                               GEN_INT (8)));
6305       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6306                             gen_lowpart (QImode, scratch)));
6307     }
6308   else
6309     {
6310       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6311                             gen_lowpart (QImode, outval)));
6312       emit_insn (gen_lshrsi3 (scratch,
6313                               gen_rtx_SUBREG (SImode, outval, 0),
6314                               GEN_INT (8)));
6315       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6316                                          plus_constant (base, offset + 1)),
6317                             gen_lowpart (QImode, scratch)));
6318     }
6319 }
6320 \f
6321 /* Print a symbolic form of X to the debug file, F.  */
6322 static void
6323 arm_print_value (FILE *f, rtx x)
6324 {
6325   switch (GET_CODE (x))
6326     {
6327     case CONST_INT:
6328       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6329       return;
6330
6331     case CONST_DOUBLE:
6332       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6333       return;
6334
6335     case CONST_VECTOR:
6336       {
6337         int i;
6338
6339         fprintf (f, "<");
6340         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6341           {
6342             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6343             if (i < (CONST_VECTOR_NUNITS (x) - 1))
6344               fputc (',', f);
6345           }
6346         fprintf (f, ">");
6347       }
6348       return;
6349
6350     case CONST_STRING:
6351       fprintf (f, "\"%s\"", XSTR (x, 0));
6352       return;
6353
6354     case SYMBOL_REF:
6355       fprintf (f, "`%s'", XSTR (x, 0));
6356       return;
6357
6358     case LABEL_REF:
6359       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6360       return;
6361
6362     case CONST:
6363       arm_print_value (f, XEXP (x, 0));
6364       return;
6365
6366     case PLUS:
6367       arm_print_value (f, XEXP (x, 0));
6368       fprintf (f, "+");
6369       arm_print_value (f, XEXP (x, 1));
6370       return;
6371
6372     case PC:
6373       fprintf (f, "pc");
6374       return;
6375
6376     default:
6377       fprintf (f, "????");
6378       return;
6379     }
6380 }
6381 \f
6382 /* Routines for manipulation of the constant pool.  */
6383
6384 /* Arm instructions cannot load a large constant directly into a
6385    register; they have to come from a pc relative load.  The constant
6386    must therefore be placed in the addressable range of the pc
6387    relative load.  Depending on the precise pc relative load
6388    instruction the range is somewhere between 256 bytes and 4k.  This
6389    means that we often have to dump a constant inside a function, and
6390    generate code to branch around it.
6391
6392    It is important to minimize this, since the branches will slow
6393    things down and make the code larger.
6394
6395    Normally we can hide the table after an existing unconditional
6396    branch so that there is no interruption of the flow, but in the
6397    worst case the code looks like this:
6398
6399         ldr     rn, L1
6400         ...
6401         b       L2
6402         align
6403         L1:     .long value
6404         L2:
6405         ...
6406
6407         ldr     rn, L3
6408         ...
6409         b       L4
6410         align
6411         L3:     .long value
6412         L4:
6413         ...
6414
6415    We fix this by performing a scan after scheduling, which notices
6416    which instructions need to have their operands fetched from the
6417    constant table and builds the table.
6418
6419    The algorithm starts by building a table of all the constants that
6420    need fixing up and all the natural barriers in the function (places
6421    where a constant table can be dropped without breaking the flow).
6422    For each fixup we note how far the pc-relative replacement will be
6423    able to reach and the offset of the instruction into the function.
6424
6425    Having built the table we then group the fixes together to form
6426    tables that are as large as possible (subject to addressing
6427    constraints) and emit each table of constants after the last
6428    barrier that is within range of all the instructions in the group.
6429    If a group does not contain a barrier, then we forcibly create one
6430    by inserting a jump instruction into the flow.  Once the table has
6431    been inserted, the insns are then modified to reference the
6432    relevant entry in the pool.
6433
6434    Possible enhancements to the algorithm (not implemented) are:
6435
6436    1) For some processors and object formats, there may be benefit in
6437    aligning the pools to the start of cache lines; this alignment
6438    would need to be taken into account when calculating addressability
6439    of a pool.  */
6440
6441 /* These typedefs are located at the start of this file, so that
6442    they can be used in the prototypes there.  This comment is to
6443    remind readers of that fact so that the following structures
6444    can be understood more easily.
6445
6446      typedef struct minipool_node    Mnode;
6447      typedef struct minipool_fixup   Mfix;  */
6448
6449 struct minipool_node
6450 {
6451   /* Doubly linked chain of entries.  */
6452   Mnode * next;
6453   Mnode * prev;
6454   /* The maximum offset into the code that this entry can be placed.  While
6455      pushing fixes for forward references, all entries are sorted in order
6456      of increasing max_address.  */
6457   HOST_WIDE_INT max_address;
6458   /* Similarly for an entry inserted for a backwards ref.  */
6459   HOST_WIDE_INT min_address;
6460   /* The number of fixes referencing this entry.  This can become zero
6461      if we "unpush" an entry.  In this case we ignore the entry when we
6462      come to emit the code.  */
6463   int refcount;
6464   /* The offset from the start of the minipool.  */
6465   HOST_WIDE_INT offset;
6466   /* The value in table.  */
6467   rtx value;
6468   /* The mode of value.  */
6469   enum machine_mode mode;
6470   /* The size of the value.  With iWMMXt enabled
6471      sizes > 4 also imply an alignment of 8-bytes.  */
6472   int fix_size;
6473 };
6474
6475 struct minipool_fixup
6476 {
6477   Mfix *            next;
6478   rtx               insn;
6479   HOST_WIDE_INT     address;
6480   rtx *             loc;
6481   enum machine_mode mode;
6482   int               fix_size;
6483   rtx               value;
6484   Mnode *           minipool;
6485   HOST_WIDE_INT     forwards;
6486   HOST_WIDE_INT     backwards;
6487 };
6488
6489 /* Fixes less than a word need padding out to a word boundary.  */
6490 #define MINIPOOL_FIX_SIZE(mode) \
6491   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6492
6493 static Mnode *  minipool_vector_head;
6494 static Mnode *  minipool_vector_tail;
6495 static rtx      minipool_vector_label;
6496
6497 /* The linked list of all minipool fixes required for this function.  */
6498 Mfix *          minipool_fix_head;
6499 Mfix *          minipool_fix_tail;
6500 /* The fix entry for the current minipool, once it has been placed.  */
6501 Mfix *          minipool_barrier;
6502
6503 /* Determines if INSN is the start of a jump table.  Returns the end
6504    of the TABLE or NULL_RTX.  */
6505 static rtx
6506 is_jump_table (rtx insn)
6507 {
6508   rtx table;
6509
6510   if (GET_CODE (insn) == JUMP_INSN
6511       && JUMP_LABEL (insn) != NULL
6512       && ((table = next_real_insn (JUMP_LABEL (insn)))
6513           == next_real_insn (insn))
6514       && table != NULL
6515       && GET_CODE (table) == JUMP_INSN
6516       && (GET_CODE (PATTERN (table)) == ADDR_VEC
6517           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6518     return table;
6519
6520   return NULL_RTX;
6521 }
6522
6523 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6524 #define JUMP_TABLES_IN_TEXT_SECTION 0
6525 #endif
6526
6527 static HOST_WIDE_INT
6528 get_jump_table_size (rtx insn)
6529 {
6530   /* ADDR_VECs only take room if read-only data does into the text
6531      section.  */
6532   if (JUMP_TABLES_IN_TEXT_SECTION
6533 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6534       || 1
6535 #endif
6536       )
6537     {
6538       rtx body = PATTERN (insn);
6539       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6540
6541       return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6542     }
6543
6544   return 0;
6545 }
6546
6547 /* Move a minipool fix MP from its current location to before MAX_MP.
6548    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6549    constraints may need updating.  */
6550 static Mnode *
6551 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6552                                HOST_WIDE_INT max_address)
6553 {
6554   /* This should never be true and the code below assumes these are
6555      different.  */
6556   if (mp == max_mp)
6557     abort ();
6558
6559   if (max_mp == NULL)
6560     {
6561       if (max_address < mp->max_address)
6562         mp->max_address = max_address;
6563     }
6564   else
6565     {
6566       if (max_address > max_mp->max_address - mp->fix_size)
6567         mp->max_address = max_mp->max_address - mp->fix_size;
6568       else
6569         mp->max_address = max_address;
6570
6571       /* Unlink MP from its current position.  Since max_mp is non-null,
6572        mp->prev must be non-null.  */
6573       mp->prev->next = mp->next;
6574       if (mp->next != NULL)
6575         mp->next->prev = mp->prev;
6576       else
6577         minipool_vector_tail = mp->prev;
6578
6579       /* Re-insert it before MAX_MP.  */
6580       mp->next = max_mp;
6581       mp->prev = max_mp->prev;
6582       max_mp->prev = mp;
6583
6584       if (mp->prev != NULL)
6585         mp->prev->next = mp;
6586       else
6587         minipool_vector_head = mp;
6588     }
6589
6590   /* Save the new entry.  */
6591   max_mp = mp;
6592
6593   /* Scan over the preceding entries and adjust their addresses as
6594      required.  */
6595   while (mp->prev != NULL
6596          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6597     {
6598       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6599       mp = mp->prev;
6600     }
6601
6602   return max_mp;
6603 }
6604
6605 /* Add a constant to the minipool for a forward reference.  Returns the
6606    node added or NULL if the constant will not fit in this pool.  */
6607 static Mnode *
6608 add_minipool_forward_ref (Mfix *fix)
6609 {
6610   /* If set, max_mp is the first pool_entry that has a lower
6611      constraint than the one we are trying to add.  */
6612   Mnode *       max_mp = NULL;
6613   HOST_WIDE_INT max_address = fix->address + fix->forwards;
6614   Mnode *       mp;
6615
6616   /* If this fix's address is greater than the address of the first
6617      entry, then we can't put the fix in this pool.  We subtract the
6618      size of the current fix to ensure that if the table is fully
6619      packed we still have enough room to insert this value by suffling
6620      the other fixes forwards.  */
6621   if (minipool_vector_head &&
6622       fix->address >= minipool_vector_head->max_address - fix->fix_size)
6623     return NULL;
6624
6625   /* Scan the pool to see if a constant with the same value has
6626      already been added.  While we are doing this, also note the
6627      location where we must insert the constant if it doesn't already
6628      exist.  */
6629   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6630     {
6631       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6632           && fix->mode == mp->mode
6633           && (GET_CODE (fix->value) != CODE_LABEL
6634               || (CODE_LABEL_NUMBER (fix->value)
6635                   == CODE_LABEL_NUMBER (mp->value)))
6636           && rtx_equal_p (fix->value, mp->value))
6637         {
6638           /* More than one fix references this entry.  */
6639           mp->refcount++;
6640           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6641         }
6642
6643       /* Note the insertion point if necessary.  */
6644       if (max_mp == NULL
6645           && mp->max_address > max_address)
6646         max_mp = mp;
6647
6648       /* If we are inserting an 8-bytes aligned quantity and
6649          we have not already found an insertion point, then
6650          make sure that all such 8-byte aligned quantities are
6651          placed at the start of the pool.  */
6652       if (ARM_DOUBLEWORD_ALIGN
6653           && max_mp == NULL
6654           && fix->fix_size == 8
6655           && mp->fix_size != 8)
6656         {
6657           max_mp = mp;
6658           max_address = mp->max_address;
6659         }
6660     }
6661
6662   /* The value is not currently in the minipool, so we need to create
6663      a new entry for it.  If MAX_MP is NULL, the entry will be put on
6664      the end of the list since the placement is less constrained than
6665      any existing entry.  Otherwise, we insert the new fix before
6666      MAX_MP and, if necessary, adjust the constraints on the other
6667      entries.  */
6668   mp = xmalloc (sizeof (* mp));
6669   mp->fix_size = fix->fix_size;
6670   mp->mode = fix->mode;
6671   mp->value = fix->value;
6672   mp->refcount = 1;
6673   /* Not yet required for a backwards ref.  */
6674   mp->min_address = -65536;
6675
6676   if (max_mp == NULL)
6677     {
6678       mp->max_address = max_address;
6679       mp->next = NULL;
6680       mp->prev = minipool_vector_tail;
6681
6682       if (mp->prev == NULL)
6683         {
6684           minipool_vector_head = mp;
6685           minipool_vector_label = gen_label_rtx ();
6686         }
6687       else
6688         mp->prev->next = mp;
6689
6690       minipool_vector_tail = mp;
6691     }
6692   else
6693     {
6694       if (max_address > max_mp->max_address - mp->fix_size)
6695         mp->max_address = max_mp->max_address - mp->fix_size;
6696       else
6697         mp->max_address = max_address;
6698
6699       mp->next = max_mp;
6700       mp->prev = max_mp->prev;
6701       max_mp->prev = mp;
6702       if (mp->prev != NULL)
6703         mp->prev->next = mp;
6704       else
6705         minipool_vector_head = mp;
6706     }
6707
6708   /* Save the new entry.  */
6709   max_mp = mp;
6710
6711   /* Scan over the preceding entries and adjust their addresses as
6712      required.  */
6713   while (mp->prev != NULL
6714          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6715     {
6716       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6717       mp = mp->prev;
6718     }
6719
6720   return max_mp;
6721 }
6722
6723 static Mnode *
6724 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
6725                                 HOST_WIDE_INT  min_address)
6726 {
6727   HOST_WIDE_INT offset;
6728
6729   /* This should never be true, and the code below assumes these are
6730      different.  */
6731   if (mp == min_mp)
6732     abort ();
6733
6734   if (min_mp == NULL)
6735     {
6736       if (min_address > mp->min_address)
6737         mp->min_address = min_address;
6738     }
6739   else
6740     {
6741       /* We will adjust this below if it is too loose.  */
6742       mp->min_address = min_address;
6743
6744       /* Unlink MP from its current position.  Since min_mp is non-null,
6745          mp->next must be non-null.  */
6746       mp->next->prev = mp->prev;
6747       if (mp->prev != NULL)
6748         mp->prev->next = mp->next;
6749       else
6750         minipool_vector_head = mp->next;
6751
6752       /* Reinsert it after MIN_MP.  */
6753       mp->prev = min_mp;
6754       mp->next = min_mp->next;
6755       min_mp->next = mp;
6756       if (mp->next != NULL)
6757         mp->next->prev = mp;
6758       else
6759         minipool_vector_tail = mp;
6760     }
6761
6762   min_mp = mp;
6763
6764   offset = 0;
6765   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6766     {
6767       mp->offset = offset;
6768       if (mp->refcount > 0)
6769         offset += mp->fix_size;
6770
6771       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
6772         mp->next->min_address = mp->min_address + mp->fix_size;
6773     }
6774
6775   return min_mp;
6776 }
6777
6778 /* Add a constant to the minipool for a backward reference.  Returns the
6779    node added or NULL if the constant will not fit in this pool.
6780
6781    Note that the code for insertion for a backwards reference can be
6782    somewhat confusing because the calculated offsets for each fix do
6783    not take into account the size of the pool (which is still under
6784    construction.  */
6785 static Mnode *
6786 add_minipool_backward_ref (Mfix *fix)
6787 {
6788   /* If set, min_mp is the last pool_entry that has a lower constraint
6789      than the one we are trying to add.  */
6790   Mnode *min_mp = NULL;
6791   /* This can be negative, since it is only a constraint.  */
6792   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
6793   Mnode *mp;
6794
6795   /* If we can't reach the current pool from this insn, or if we can't
6796      insert this entry at the end of the pool without pushing other
6797      fixes out of range, then we don't try.  This ensures that we
6798      can't fail later on.  */
6799   if (min_address >= minipool_barrier->address
6800       || (minipool_vector_tail->min_address + fix->fix_size
6801           >= minipool_barrier->address))
6802     return NULL;
6803
6804   /* Scan the pool to see if a constant with the same value has
6805      already been added.  While we are doing this, also note the
6806      location where we must insert the constant if it doesn't already
6807      exist.  */
6808   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
6809     {
6810       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6811           && fix->mode == mp->mode
6812           && (GET_CODE (fix->value) != CODE_LABEL
6813               || (CODE_LABEL_NUMBER (fix->value)
6814                   == CODE_LABEL_NUMBER (mp->value)))
6815           && rtx_equal_p (fix->value, mp->value)
6816           /* Check that there is enough slack to move this entry to the
6817              end of the table (this is conservative).  */
6818           && (mp->max_address
6819               > (minipool_barrier->address
6820                  + minipool_vector_tail->offset
6821                  + minipool_vector_tail->fix_size)))
6822         {
6823           mp->refcount++;
6824           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
6825         }
6826
6827       if (min_mp != NULL)
6828         mp->min_address += fix->fix_size;
6829       else
6830         {
6831           /* Note the insertion point if necessary.  */
6832           if (mp->min_address < min_address)
6833             {
6834               /* For now, we do not allow the insertion of 8-byte alignment
6835                  requiring nodes anywhere but at the start of the pool.  */
6836               if (ARM_DOUBLEWORD_ALIGN
6837                   && fix->fix_size == 8 && mp->fix_size != 8)
6838                 return NULL;
6839               else
6840                 min_mp = mp;
6841             }
6842           else if (mp->max_address
6843                    < minipool_barrier->address + mp->offset + fix->fix_size)
6844             {
6845               /* Inserting before this entry would push the fix beyond
6846                  its maximum address (which can happen if we have
6847                  re-located a forwards fix); force the new fix to come
6848                  after it.  */
6849               min_mp = mp;
6850               min_address = mp->min_address + fix->fix_size;
6851             }
6852           /* If we are inserting an 8-bytes aligned quantity and
6853              we have not already found an insertion point, then
6854              make sure that all such 8-byte aligned quantities are
6855              placed at the start of the pool.  */
6856           else if (ARM_DOUBLEWORD_ALIGN
6857                    && min_mp == NULL
6858                    && fix->fix_size == 8
6859                    && mp->fix_size < 8)
6860             {
6861               min_mp = mp;
6862               min_address = mp->min_address + fix->fix_size;
6863             }
6864         }
6865     }
6866
6867   /* We need to create a new entry.  */
6868   mp = xmalloc (sizeof (* mp));
6869   mp->fix_size = fix->fix_size;
6870   mp->mode = fix->mode;
6871   mp->value = fix->value;
6872   mp->refcount = 1;
6873   mp->max_address = minipool_barrier->address + 65536;
6874
6875   mp->min_address = min_address;
6876
6877   if (min_mp == NULL)
6878     {
6879       mp->prev = NULL;
6880       mp->next = minipool_vector_head;
6881
6882       if (mp->next == NULL)
6883         {
6884           minipool_vector_tail = mp;
6885           minipool_vector_label = gen_label_rtx ();
6886         }
6887       else
6888         mp->next->prev = mp;
6889
6890       minipool_vector_head = mp;
6891     }
6892   else
6893     {
6894       mp->next = min_mp->next;
6895       mp->prev = min_mp;
6896       min_mp->next = mp;
6897
6898       if (mp->next != NULL)
6899         mp->next->prev = mp;
6900       else
6901         minipool_vector_tail = mp;
6902     }
6903
6904   /* Save the new entry.  */
6905   min_mp = mp;
6906
6907   if (mp->prev)
6908     mp = mp->prev;
6909   else
6910     mp->offset = 0;
6911
6912   /* Scan over the following entries and adjust their offsets.  */
6913   while (mp->next != NULL)
6914     {
6915       if (mp->next->min_address < mp->min_address + mp->fix_size)
6916         mp->next->min_address = mp->min_address + mp->fix_size;
6917
6918       if (mp->refcount)
6919         mp->next->offset = mp->offset + mp->fix_size;
6920       else
6921         mp->next->offset = mp->offset;
6922
6923       mp = mp->next;
6924     }
6925
6926   return min_mp;
6927 }
6928
6929 static void
6930 assign_minipool_offsets (Mfix *barrier)
6931 {
6932   HOST_WIDE_INT offset = 0;
6933   Mnode *mp;
6934
6935   minipool_barrier = barrier;
6936
6937   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6938     {
6939       mp->offset = offset;
6940
6941       if (mp->refcount > 0)
6942         offset += mp->fix_size;
6943     }
6944 }
6945
6946 /* Output the literal table */
6947 static void
6948 dump_minipool (rtx scan)
6949 {
6950   Mnode * mp;
6951   Mnode * nmp;
6952   int align64 = 0;
6953
6954   if (ARM_DOUBLEWORD_ALIGN)
6955     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6956       if (mp->refcount > 0 && mp->fix_size == 8)
6957         {
6958           align64 = 1;
6959           break;
6960         }
6961
6962   if (dump_file)
6963     fprintf (dump_file,
6964              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
6965              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
6966
6967   scan = emit_label_after (gen_label_rtx (), scan);
6968   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
6969   scan = emit_label_after (minipool_vector_label, scan);
6970
6971   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
6972     {
6973       if (mp->refcount > 0)
6974         {
6975           if (dump_file)
6976             {
6977               fprintf (dump_file,
6978                        ";;  Offset %u, min %ld, max %ld ",
6979                        (unsigned) mp->offset, (unsigned long) mp->min_address,
6980                        (unsigned long) mp->max_address);
6981               arm_print_value (dump_file, mp->value);
6982               fputc ('\n', dump_file);
6983             }
6984
6985           switch (mp->fix_size)
6986             {
6987 #ifdef HAVE_consttable_1
6988             case 1:
6989               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
6990               break;
6991
6992 #endif
6993 #ifdef HAVE_consttable_2
6994             case 2:
6995               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
6996               break;
6997
6998 #endif
6999 #ifdef HAVE_consttable_4
7000             case 4:
7001               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7002               break;
7003
7004 #endif
7005 #ifdef HAVE_consttable_8
7006             case 8:
7007               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7008               break;
7009
7010 #endif
7011             default:
7012               abort ();
7013               break;
7014             }
7015         }
7016
7017       nmp = mp->next;
7018       free (mp);
7019     }
7020
7021   minipool_vector_head = minipool_vector_tail = NULL;
7022   scan = emit_insn_after (gen_consttable_end (), scan);
7023   scan = emit_barrier_after (scan);
7024 }
7025
7026 /* Return the cost of forcibly inserting a barrier after INSN.  */
7027 static int
7028 arm_barrier_cost (rtx insn)
7029 {
7030   /* Basing the location of the pool on the loop depth is preferable,
7031      but at the moment, the basic block information seems to be
7032      corrupt by this stage of the compilation.  */
7033   int base_cost = 50;
7034   rtx next = next_nonnote_insn (insn);
7035
7036   if (next != NULL && GET_CODE (next) == CODE_LABEL)
7037     base_cost -= 20;
7038
7039   switch (GET_CODE (insn))
7040     {
7041     case CODE_LABEL:
7042       /* It will always be better to place the table before the label, rather
7043          than after it.  */
7044       return 50;
7045
7046     case INSN:
7047     case CALL_INSN:
7048       return base_cost;
7049
7050     case JUMP_INSN:
7051       return base_cost - 10;
7052
7053     default:
7054       return base_cost + 10;
7055     }
7056 }
7057
7058 /* Find the best place in the insn stream in the range
7059    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7060    Create the barrier by inserting a jump and add a new fix entry for
7061    it.  */
7062 static Mfix *
7063 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7064 {
7065   HOST_WIDE_INT count = 0;
7066   rtx barrier;
7067   rtx from = fix->insn;
7068   rtx selected = from;
7069   int selected_cost;
7070   HOST_WIDE_INT selected_address;
7071   Mfix * new_fix;
7072   HOST_WIDE_INT max_count = max_address - fix->address;
7073   rtx label = gen_label_rtx ();
7074
7075   selected_cost = arm_barrier_cost (from);
7076   selected_address = fix->address;
7077
7078   while (from && count < max_count)
7079     {
7080       rtx tmp;
7081       int new_cost;
7082
7083       /* This code shouldn't have been called if there was a natural barrier
7084          within range.  */
7085       if (GET_CODE (from) == BARRIER)
7086         abort ();
7087
7088       /* Count the length of this insn.  */
7089       count += get_attr_length (from);
7090
7091       /* If there is a jump table, add its length.  */
7092       tmp = is_jump_table (from);
7093       if (tmp != NULL)
7094         {
7095           count += get_jump_table_size (tmp);
7096
7097           /* Jump tables aren't in a basic block, so base the cost on
7098              the dispatch insn.  If we select this location, we will
7099              still put the pool after the table.  */
7100           new_cost = arm_barrier_cost (from);
7101
7102           if (count < max_count && new_cost <= selected_cost)
7103             {
7104               selected = tmp;
7105               selected_cost = new_cost;
7106               selected_address = fix->address + count;
7107             }
7108
7109           /* Continue after the dispatch table.  */
7110           from = NEXT_INSN (tmp);
7111           continue;
7112         }
7113
7114       new_cost = arm_barrier_cost (from);
7115
7116       if (count < max_count && new_cost <= selected_cost)
7117         {
7118           selected = from;
7119           selected_cost = new_cost;
7120           selected_address = fix->address + count;
7121         }
7122
7123       from = NEXT_INSN (from);
7124     }
7125
7126   /* Create a new JUMP_INSN that branches around a barrier.  */
7127   from = emit_jump_insn_after (gen_jump (label), selected);
7128   JUMP_LABEL (from) = label;
7129   barrier = emit_barrier_after (from);
7130   emit_label_after (label, barrier);
7131
7132   /* Create a minipool barrier entry for the new barrier.  */
7133   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7134   new_fix->insn = barrier;
7135   new_fix->address = selected_address;
7136   new_fix->next = fix->next;
7137   fix->next = new_fix;
7138
7139   return new_fix;
7140 }
7141
7142 /* Record that there is a natural barrier in the insn stream at
7143    ADDRESS.  */
7144 static void
7145 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7146 {
7147   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7148
7149   fix->insn = insn;
7150   fix->address = address;
7151
7152   fix->next = NULL;
7153   if (minipool_fix_head != NULL)
7154     minipool_fix_tail->next = fix;
7155   else
7156     minipool_fix_head = fix;
7157
7158   minipool_fix_tail = fix;
7159 }
7160
7161 /* Record INSN, which will need fixing up to load a value from the
7162    minipool.  ADDRESS is the offset of the insn since the start of the
7163    function; LOC is a pointer to the part of the insn which requires
7164    fixing; VALUE is the constant that must be loaded, which is of type
7165    MODE.  */
7166 static void
7167 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7168                    enum machine_mode mode, rtx value)
7169 {
7170   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7171
7172 #ifdef AOF_ASSEMBLER
7173   /* PIC symbol references need to be converted into offsets into the
7174      based area.  */
7175   /* XXX This shouldn't be done here.  */
7176   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7177     value = aof_pic_entry (value);
7178 #endif /* AOF_ASSEMBLER */
7179
7180   fix->insn = insn;
7181   fix->address = address;
7182   fix->loc = loc;
7183   fix->mode = mode;
7184   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7185   fix->value = value;
7186   fix->forwards = get_attr_pool_range (insn);
7187   fix->backwards = get_attr_neg_pool_range (insn);
7188   fix->minipool = NULL;
7189
7190   /* If an insn doesn't have a range defined for it, then it isn't
7191      expecting to be reworked by this code.  Better to abort now than
7192      to generate duff assembly code.  */
7193   if (fix->forwards == 0 && fix->backwards == 0)
7194     abort ();
7195
7196   /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7197      So there might be an empty word before the start of the pool.
7198      Hence we reduce the forward range by 4 to allow for this
7199      possibility.  */
7200   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7201     fix->forwards -= 4;
7202
7203   if (dump_file)
7204     {
7205       fprintf (dump_file,
7206                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7207                GET_MODE_NAME (mode),
7208                INSN_UID (insn), (unsigned long) address,
7209                -1 * (long)fix->backwards, (long)fix->forwards);
7210       arm_print_value (dump_file, fix->value);
7211       fprintf (dump_file, "\n");
7212     }
7213
7214   /* Add it to the chain of fixes.  */
7215   fix->next = NULL;
7216
7217   if (minipool_fix_head != NULL)
7218     minipool_fix_tail->next = fix;
7219   else
7220     minipool_fix_head = fix;
7221
7222   minipool_fix_tail = fix;
7223 }
7224
7225 /* Scan INSN and note any of its operands that need fixing.
7226    If DO_PUSHES is false we do not actually push any of the fixups
7227    needed.  The function returns TRUE is any fixups were needed/pushed.
7228    This is used by arm_memory_load_p() which needs to know about loads
7229    of constants that will be converted into minipool loads.  */
7230 static bool
7231 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7232 {
7233   bool result = false;
7234   int opno;
7235
7236   extract_insn (insn);
7237
7238   if (!constrain_operands (1))
7239     fatal_insn_not_found (insn);
7240
7241   if (recog_data.n_alternatives == 0)
7242     return false;
7243
7244   /* Fill in recog_op_alt with information about the constraints of this insn.  */
7245   preprocess_constraints ();
7246
7247   for (opno = 0; opno < recog_data.n_operands; opno++)
7248     {
7249       /* Things we need to fix can only occur in inputs.  */
7250       if (recog_data.operand_type[opno] != OP_IN)
7251         continue;
7252
7253       /* If this alternative is a memory reference, then any mention
7254          of constants in this alternative is really to fool reload
7255          into allowing us to accept one there.  We need to fix them up
7256          now so that we output the right code.  */
7257       if (recog_op_alt[opno][which_alternative].memory_ok)
7258         {
7259           rtx op = recog_data.operand[opno];
7260
7261           if (CONSTANT_P (op))
7262             {
7263               if (do_pushes)
7264                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7265                                    recog_data.operand_mode[opno], op);
7266               result = true;
7267             }
7268           else if (GET_CODE (op) == MEM
7269                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7270                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7271             {
7272               if (do_pushes)
7273                 {
7274                   rtx cop = avoid_constant_pool_reference (op);
7275
7276                   /* Casting the address of something to a mode narrower
7277                      than a word can cause avoid_constant_pool_reference()
7278                      to return the pool reference itself.  That's no good to
7279                      us here.  Lets just hope that we can use the
7280                      constant pool value directly.  */
7281                   if (op == cop)
7282                     cop = get_pool_constant (XEXP (op, 0));
7283
7284                   push_minipool_fix (insn, address,
7285                                      recog_data.operand_loc[opno],
7286                                      recog_data.operand_mode[opno], cop);
7287                 }
7288
7289               result = true;
7290             }
7291         }
7292     }
7293
7294   return result;
7295 }
7296
7297 /* Gcc puts the pool in the wrong place for ARM, since we can only
7298    load addresses a limited distance around the pc.  We do some
7299    special munging to move the constant pool values to the correct
7300    point in the code.  */
7301 static void
7302 arm_reorg (void)
7303 {
7304   rtx insn;
7305   HOST_WIDE_INT address = 0;
7306   Mfix * fix;
7307
7308   minipool_fix_head = minipool_fix_tail = NULL;
7309
7310   /* The first insn must always be a note, or the code below won't
7311      scan it properly.  */
7312   insn = get_insns ();
7313   if (GET_CODE (insn) != NOTE)
7314     abort ();
7315
7316   /* Scan all the insns and record the operands that will need fixing.  */
7317   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7318     {
7319       if (TARGET_CIRRUS_FIX_INVALID_INSNS
7320           && (arm_cirrus_insn_p (insn)
7321               || GET_CODE (insn) == JUMP_INSN
7322               || arm_memory_load_p (insn)))
7323         cirrus_reorg (insn);
7324
7325       if (GET_CODE (insn) == BARRIER)
7326         push_minipool_barrier (insn, address);
7327       else if (INSN_P (insn))
7328         {
7329           rtx table;
7330
7331           note_invalid_constants (insn, address, true);
7332           address += get_attr_length (insn);
7333
7334           /* If the insn is a vector jump, add the size of the table
7335              and skip the table.  */
7336           if ((table = is_jump_table (insn)) != NULL)
7337             {
7338               address += get_jump_table_size (table);
7339               insn = table;
7340             }
7341         }
7342     }
7343
7344   fix = minipool_fix_head;
7345
7346   /* Now scan the fixups and perform the required changes.  */
7347   while (fix)
7348     {
7349       Mfix * ftmp;
7350       Mfix * fdel;
7351       Mfix *  last_added_fix;
7352       Mfix * last_barrier = NULL;
7353       Mfix * this_fix;
7354
7355       /* Skip any further barriers before the next fix.  */
7356       while (fix && GET_CODE (fix->insn) == BARRIER)
7357         fix = fix->next;
7358
7359       /* No more fixes.  */
7360       if (fix == NULL)
7361         break;
7362
7363       last_added_fix = NULL;
7364
7365       for (ftmp = fix; ftmp; ftmp = ftmp->next)
7366         {
7367           if (GET_CODE (ftmp->insn) == BARRIER)
7368             {
7369               if (ftmp->address >= minipool_vector_head->max_address)
7370                 break;
7371
7372               last_barrier = ftmp;
7373             }
7374           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7375             break;
7376
7377           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
7378         }
7379
7380       /* If we found a barrier, drop back to that; any fixes that we
7381          could have reached but come after the barrier will now go in
7382          the next mini-pool.  */
7383       if (last_barrier != NULL)
7384         {
7385           /* Reduce the refcount for those fixes that won't go into this
7386              pool after all.  */
7387           for (fdel = last_barrier->next;
7388                fdel && fdel != ftmp;
7389                fdel = fdel->next)
7390             {
7391               fdel->minipool->refcount--;
7392               fdel->minipool = NULL;
7393             }
7394
7395           ftmp = last_barrier;
7396         }
7397       else
7398         {
7399           /* ftmp is first fix that we can't fit into this pool and
7400              there no natural barriers that we could use.  Insert a
7401              new barrier in the code somewhere between the previous
7402              fix and this one, and arrange to jump around it.  */
7403           HOST_WIDE_INT max_address;
7404
7405           /* The last item on the list of fixes must be a barrier, so
7406              we can never run off the end of the list of fixes without
7407              last_barrier being set.  */
7408           if (ftmp == NULL)
7409             abort ();
7410
7411           max_address = minipool_vector_head->max_address;
7412           /* Check that there isn't another fix that is in range that
7413              we couldn't fit into this pool because the pool was
7414              already too large: we need to put the pool before such an
7415              instruction.  */
7416           if (ftmp->address < max_address)
7417             max_address = ftmp->address;
7418
7419           last_barrier = create_fix_barrier (last_added_fix, max_address);
7420         }
7421
7422       assign_minipool_offsets (last_barrier);
7423
7424       while (ftmp)
7425         {
7426           if (GET_CODE (ftmp->insn) != BARRIER
7427               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7428                   == NULL))
7429             break;
7430
7431           ftmp = ftmp->next;
7432         }
7433
7434       /* Scan over the fixes we have identified for this pool, fixing them
7435          up and adding the constants to the pool itself.  */
7436       for (this_fix = fix; this_fix && ftmp != this_fix;
7437            this_fix = this_fix->next)
7438         if (GET_CODE (this_fix->insn) != BARRIER)
7439           {
7440             rtx addr
7441               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
7442                                                   minipool_vector_label),
7443                                this_fix->minipool->offset);
7444             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7445           }
7446
7447       dump_minipool (last_barrier->insn);
7448       fix = ftmp;
7449     }
7450
7451   /* From now on we must synthesize any constants that we can't handle
7452      directly.  This can happen if the RTL gets split during final
7453      instruction generation.  */
7454   after_arm_reorg = 1;
7455
7456   /* Free the minipool memory.  */
7457   obstack_free (&minipool_obstack, minipool_startobj);
7458 }
7459 \f
7460 /* Routines to output assembly language.  */
7461
7462 /* If the rtx is the correct value then return the string of the number.
7463    In this way we can ensure that valid double constants are generated even
7464    when cross compiling.  */
7465 const char *
7466 fp_immediate_constant (rtx x)
7467 {
7468   REAL_VALUE_TYPE r;
7469   int i;
7470
7471   if (!fp_consts_inited)
7472     init_fp_table ();
7473
7474   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7475   for (i = 0; i < 8; i++)
7476     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7477       return strings_fp[i];
7478
7479   abort ();
7480 }
7481
7482 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
7483 static const char *
7484 fp_const_from_val (REAL_VALUE_TYPE *r)
7485 {
7486   int i;
7487
7488   if (!fp_consts_inited)
7489     init_fp_table ();
7490
7491   for (i = 0; i < 8; i++)
7492     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7493       return strings_fp[i];
7494
7495   abort ();
7496 }
7497
7498 /* Output the operands of a LDM/STM instruction to STREAM.
7499    MASK is the ARM register set mask of which only bits 0-15 are important.
7500    REG is the base register, either the frame pointer or the stack pointer,
7501    INSTR is the possibly suffixed load or store instruction.  */
7502 static void
7503 print_multi_reg (FILE *stream, const char *instr, int reg, int mask)
7504 {
7505   int i;
7506   int not_first = FALSE;
7507
7508   fputc ('\t', stream);
7509   asm_fprintf (stream, instr, reg);
7510   fputs (", {", stream);
7511
7512   for (i = 0; i <= LAST_ARM_REGNUM; i++)
7513     if (mask & (1 << i))
7514       {
7515         if (not_first)
7516           fprintf (stream, ", ");
7517
7518         asm_fprintf (stream, "%r", i);
7519         not_first = TRUE;
7520       }
7521
7522   fprintf (stream, "}\n");
7523 }
7524
7525
7526 /* Output a FLDMX instruction to STREAM.
7527    BASE if the register containing the address.
7528    REG and COUNT specify the register range.
7529    Extra registers may be added to avoid hardware bugs.  */
7530
7531 static void
7532 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7533 {
7534   int i;
7535
7536   /* Workaround ARM10 VFPr1 bug.  */
7537   if (count == 2 && !arm_arch6)
7538     {
7539       if (reg == 15)
7540         reg--;
7541       count++;
7542     }
7543
7544   fputc ('\t', stream);
7545   asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7546
7547   for (i = reg; i < reg + count; i++)
7548     {
7549       if (i > reg)
7550         fputs (", ", stream);
7551       asm_fprintf (stream, "d%d", i);
7552     }
7553   fputs ("}\n", stream);
7554
7555 }
7556
7557
7558 /* Output the assembly for a store multiple.  */
7559
7560 const char *
7561 vfp_output_fstmx (rtx * operands)
7562 {
7563   char pattern[100];
7564   int p;
7565   int base;
7566   int i;
7567
7568   strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7569   p = strlen (pattern);
7570
7571   if (GET_CODE (operands[1]) != REG)
7572     abort ();
7573
7574   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7575   for (i = 1; i < XVECLEN (operands[2], 0); i++)
7576     {
7577       p += sprintf (&pattern[p], ", d%d", base + i);
7578     }
7579   strcpy (&pattern[p], "}");
7580
7581   output_asm_insn (pattern, operands);
7582   return "";
7583 }
7584
7585
7586 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
7587    number of bytes pushed.  */
7588
7589 static int
7590 vfp_emit_fstmx (int base_reg, int count)
7591 {
7592   rtx par;
7593   rtx dwarf;
7594   rtx tmp, reg;
7595   int i;
7596
7597   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
7598      register pairs are stored by a store multiple insn.  We avoid this
7599      by pushing an extra pair.  */
7600   if (count == 2 && !arm_arch6)
7601     {
7602       if (base_reg == LAST_VFP_REGNUM - 3)
7603         base_reg -= 2;
7604       count++;
7605     }
7606
7607   /* ??? The frame layout is implementation defined.  We describe
7608      standard format 1 (equivalent to a FSTMD insn and unused pad word).
7609      We really need some way of representing the whole block so that the
7610      unwinder can figure it out at runtime.  */
7611   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7612   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
7613
7614   reg = gen_rtx_REG (DFmode, base_reg);
7615   base_reg += 2;
7616
7617   XVECEXP (par, 0, 0)
7618     = gen_rtx_SET (VOIDmode,
7619                    gen_rtx_MEM (BLKmode,
7620                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7621                    gen_rtx_UNSPEC (BLKmode,
7622                                    gen_rtvec (1, reg),
7623                                    UNSPEC_PUSH_MULT));
7624
7625   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7626                      gen_rtx_PLUS (SImode, stack_pointer_rtx,
7627                                    GEN_INT (-(count * 8 + 4))));
7628   RTX_FRAME_RELATED_P (tmp) = 1;
7629   XVECEXP (dwarf, 0, 0) = tmp;
7630
7631   tmp = gen_rtx_SET (VOIDmode,
7632                      gen_rtx_MEM (DFmode, stack_pointer_rtx),
7633                      reg);
7634   RTX_FRAME_RELATED_P (tmp) = 1;
7635   XVECEXP (dwarf, 0, 1) = tmp;
7636
7637   for (i = 1; i < count; i++)
7638     {
7639       reg = gen_rtx_REG (DFmode, base_reg);
7640       base_reg += 2;
7641       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
7642
7643       tmp = gen_rtx_SET (VOIDmode,
7644                          gen_rtx_MEM (DFmode,
7645                                       gen_rtx_PLUS (SImode,
7646                                                     stack_pointer_rtx,
7647                                                     GEN_INT (i * 8))),
7648                          reg);
7649       RTX_FRAME_RELATED_P (tmp) = 1;
7650       XVECEXP (dwarf, 0, i + 1) = tmp;
7651     }
7652
7653   par = emit_insn (par);
7654   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7655                                        REG_NOTES (par));
7656   RTX_FRAME_RELATED_P (par) = 1;
7657
7658   return count * 8 + 4;
7659 }
7660
7661
7662 /* Output a 'call' insn.  */
7663 const char *
7664 output_call (rtx *operands)
7665 {
7666   if (arm_arch5)
7667     abort ();           /* Patterns should call blx <reg> directly.  */
7668
7669   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
7670   if (REGNO (operands[0]) == LR_REGNUM)
7671     {
7672       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
7673       output_asm_insn ("mov%?\t%0, %|lr", operands);
7674     }
7675
7676   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7677
7678   if (TARGET_INTERWORK || arm_arch4t)
7679     output_asm_insn ("bx%?\t%0", operands);
7680   else
7681     output_asm_insn ("mov%?\t%|pc, %0", operands);
7682
7683   return "";
7684 }
7685
7686 /* Output a 'call' insn that is a reference in memory.  */
7687 const char *
7688 output_call_mem (rtx *operands)
7689 {
7690   if (TARGET_INTERWORK && !arm_arch5)
7691     {
7692       output_asm_insn ("ldr%?\t%|ip, %0", operands);
7693       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7694       output_asm_insn ("bx%?\t%|ip", operands);
7695     }
7696   else if (regno_use_in (LR_REGNUM, operands[0]))
7697     {
7698       /* LR is used in the memory address.  We load the address in the
7699          first instruction.  It's safe to use IP as the target of the
7700          load since the call will kill it anyway.  */
7701       output_asm_insn ("ldr%?\t%|ip, %0", operands);
7702       if (arm_arch5)
7703         output_asm_insn ("blx%?%|ip", operands);
7704       else
7705         {
7706           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7707           if (arm_arch4t)
7708             output_asm_insn ("bx%?\t%|ip", operands);
7709           else
7710             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
7711         }
7712     }
7713   else
7714     {
7715       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7716       output_asm_insn ("ldr%?\t%|pc, %0", operands);
7717     }
7718
7719   return "";
7720 }
7721
7722
7723 /* Output a move from arm registers to an fpa registers.
7724    OPERANDS[0] is an fpa register.
7725    OPERANDS[1] is the first registers of an arm register pair.  */
7726 const char *
7727 output_mov_long_double_fpa_from_arm (rtx *operands)
7728 {
7729   int arm_reg0 = REGNO (operands[1]);
7730   rtx ops[3];
7731
7732   if (arm_reg0 == IP_REGNUM)
7733     abort ();
7734
7735   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7736   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7737   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
7738
7739   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
7740   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
7741
7742   return "";
7743 }
7744
7745 /* Output a move from an fpa register to arm registers.
7746    OPERANDS[0] is the first registers of an arm register pair.
7747    OPERANDS[1] is an fpa register.  */
7748 const char *
7749 output_mov_long_double_arm_from_fpa (rtx *operands)
7750 {
7751   int arm_reg0 = REGNO (operands[0]);
7752   rtx ops[3];
7753
7754   if (arm_reg0 == IP_REGNUM)
7755     abort ();
7756
7757   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7758   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7759   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
7760
7761   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
7762   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
7763   return "";
7764 }
7765
7766 /* Output a move from arm registers to arm registers of a long double
7767    OPERANDS[0] is the destination.
7768    OPERANDS[1] is the source.  */
7769 const char *
7770 output_mov_long_double_arm_from_arm (rtx *operands)
7771 {
7772   /* We have to be careful here because the two might overlap.  */
7773   int dest_start = REGNO (operands[0]);
7774   int src_start = REGNO (operands[1]);
7775   rtx ops[2];
7776   int i;
7777
7778   if (dest_start < src_start)
7779     {
7780       for (i = 0; i < 3; i++)
7781         {
7782           ops[0] = gen_rtx_REG (SImode, dest_start + i);
7783           ops[1] = gen_rtx_REG (SImode, src_start + i);
7784           output_asm_insn ("mov%?\t%0, %1", ops);
7785         }
7786     }
7787   else
7788     {
7789       for (i = 2; i >= 0; i--)
7790         {
7791           ops[0] = gen_rtx_REG (SImode, dest_start + i);
7792           ops[1] = gen_rtx_REG (SImode, src_start + i);
7793           output_asm_insn ("mov%?\t%0, %1", ops);
7794         }
7795     }
7796
7797   return "";
7798 }
7799
7800
7801 /* Output a move from arm registers to an fpa registers.
7802    OPERANDS[0] is an fpa register.
7803    OPERANDS[1] is the first registers of an arm register pair.  */
7804 const char *
7805 output_mov_double_fpa_from_arm (rtx *operands)
7806 {
7807   int arm_reg0 = REGNO (operands[1]);
7808   rtx ops[2];
7809
7810   if (arm_reg0 == IP_REGNUM)
7811     abort ();
7812
7813   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7814   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7815   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
7816   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
7817   return "";
7818 }
7819
7820 /* Output a move from an fpa register to arm registers.
7821    OPERANDS[0] is the first registers of an arm register pair.
7822    OPERANDS[1] is an fpa register.  */
7823 const char *
7824 output_mov_double_arm_from_fpa (rtx *operands)
7825 {
7826   int arm_reg0 = REGNO (operands[0]);
7827   rtx ops[2];
7828
7829   if (arm_reg0 == IP_REGNUM)
7830     abort ();
7831
7832   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7833   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7834   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
7835   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
7836   return "";
7837 }
7838
7839 /* Output a move between double words.
7840    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
7841    or MEM<-REG and all MEMs must be offsettable addresses.  */
7842 const char *
7843 output_move_double (rtx *operands)
7844 {
7845   enum rtx_code code0 = GET_CODE (operands[0]);
7846   enum rtx_code code1 = GET_CODE (operands[1]);
7847   rtx otherops[3];
7848
7849   if (code0 == REG)
7850     {
7851       int reg0 = REGNO (operands[0]);
7852
7853       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
7854
7855       if (code1 == REG)
7856         {
7857           int reg1 = REGNO (operands[1]);
7858           if (reg1 == IP_REGNUM)
7859             abort ();
7860
7861           /* Ensure the second source is not overwritten.  */
7862           if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
7863             output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
7864           else
7865             output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
7866         }
7867       else if (code1 == CONST_VECTOR)
7868         {
7869           HOST_WIDE_INT hint = 0;
7870
7871           switch (GET_MODE (operands[1]))
7872             {
7873             case V2SImode:
7874               otherops[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 1)));
7875               operands[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 0)));
7876               break;
7877
7878             case V4HImode:
7879               if (BYTES_BIG_ENDIAN)
7880                 {
7881                   hint = INTVAL (CONST_VECTOR_ELT (operands[1], 2));
7882                   hint <<= 16;
7883                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
7884                 }
7885               else
7886                 {
7887                   hint = INTVAL (CONST_VECTOR_ELT (operands[1], 3));
7888                   hint <<= 16;
7889                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
7890                 }
7891
7892               otherops[1] = GEN_INT (hint);
7893               hint = 0;
7894
7895               if (BYTES_BIG_ENDIAN)
7896                 {
7897                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
7898                   hint <<= 16;
7899                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
7900                 }
7901               else
7902                 {
7903                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
7904                   hint <<= 16;
7905                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
7906                 }
7907
7908               operands[1] = GEN_INT (hint);
7909               break;
7910
7911             case V8QImode:
7912               if (BYTES_BIG_ENDIAN)
7913                 {
7914                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
7915                   hint <<= 8;
7916                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
7917                   hint <<= 8;
7918                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
7919                   hint <<= 8;
7920                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
7921                 }
7922               else
7923                 {
7924                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
7925                   hint <<= 8;
7926                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
7927                   hint <<= 8;
7928                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
7929                   hint <<= 8;
7930                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
7931                 }
7932
7933               otherops[1] = GEN_INT (hint);
7934               hint = 0;
7935
7936               if (BYTES_BIG_ENDIAN)
7937                 {
7938                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
7939                   hint <<= 8;
7940                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
7941                   hint <<= 8;
7942                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
7943                   hint <<= 8;
7944                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
7945                 }
7946               else
7947                 {
7948                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
7949                   hint <<= 8;
7950                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
7951                   hint <<= 8;
7952                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
7953                   hint <<= 8;
7954                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
7955                 }
7956
7957               operands[1] = GEN_INT (hint);
7958               break;
7959
7960             default:
7961               abort ();
7962             }
7963           output_mov_immediate (operands);
7964           output_mov_immediate (otherops);
7965         }
7966       else if (code1 == CONST_DOUBLE)
7967         {
7968           if (GET_MODE (operands[1]) == DFmode)
7969             {
7970               REAL_VALUE_TYPE r;
7971               long l[2];
7972
7973               REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
7974               REAL_VALUE_TO_TARGET_DOUBLE (r, l);
7975               otherops[1] = GEN_INT (l[1]);
7976               operands[1] = GEN_INT (l[0]);
7977             }
7978           else if (GET_MODE (operands[1]) != VOIDmode)
7979             abort ();
7980           else if (WORDS_BIG_ENDIAN)
7981             {
7982               otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
7983               operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
7984             }
7985           else
7986             {
7987               otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
7988               operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
7989             }
7990
7991           output_mov_immediate (operands);
7992           output_mov_immediate (otherops);
7993         }
7994       else if (code1 == CONST_INT)
7995         {
7996 #if HOST_BITS_PER_WIDE_INT > 32
7997           /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
7998              what the upper word is.  */
7999           if (WORDS_BIG_ENDIAN)
8000             {
8001               otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8002               operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8003             }
8004           else
8005             {
8006               otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8007               operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8008             }
8009 #else
8010           /* Sign extend the intval into the high-order word.  */
8011           if (WORDS_BIG_ENDIAN)
8012             {
8013               otherops[1] = operands[1];
8014               operands[1] = (INTVAL (operands[1]) < 0
8015                              ? constm1_rtx : const0_rtx);
8016             }
8017           else
8018             otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
8019 #endif
8020           output_mov_immediate (otherops);
8021           output_mov_immediate (operands);
8022         }
8023       else if (code1 == MEM)
8024         {
8025           switch (GET_CODE (XEXP (operands[1], 0)))
8026             {
8027             case REG:
8028               output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8029               break;
8030
8031             case PRE_INC:
8032               if (!TARGET_LDRD)
8033                 abort (); /* Should never happen now.  */
8034               output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8035               break;
8036
8037             case PRE_DEC:
8038               output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8039               break;
8040
8041             case POST_INC:
8042               output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8043               break;
8044
8045             case POST_DEC:
8046               if (!TARGET_LDRD)
8047                 abort (); /* Should never happen now.  */
8048               output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8049               break;
8050
8051             case PRE_MODIFY:
8052             case POST_MODIFY:
8053               otherops[0] = operands[0];
8054               otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8055               otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8056
8057               if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8058                 {
8059                   if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8060                     {
8061                       /* Registers overlap so split out the increment.  */
8062                       output_asm_insn ("add%?\t%1, %1, %2", otherops);
8063                       output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8064                     }
8065                   else
8066                     output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8067                 }
8068               else
8069                 {
8070                   /* We only allow constant increments, so this is safe.  */
8071                   output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8072                 }
8073               break;
8074
8075             case LABEL_REF:
8076             case CONST:
8077               output_asm_insn ("adr%?\t%0, %1", operands);
8078               output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8079               break;
8080
8081             default:
8082               if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8083                                    GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8084                 {
8085                   otherops[0] = operands[0];
8086                   otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8087                   otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8088
8089                   if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8090                     {
8091                       if (GET_CODE (otherops[2]) == CONST_INT)
8092                         {
8093                           switch ((int) INTVAL (otherops[2]))
8094                             {
8095                             case -8:
8096                               output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8097                               return "";
8098                             case -4:
8099                               output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8100                               return "";
8101                             case 4:
8102                               output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8103                               return "";
8104                             }
8105                         }
8106                       if (TARGET_LDRD
8107                           && (GET_CODE (otherops[2]) == REG
8108                               || (GET_CODE (otherops[2]) == CONST_INT
8109                                   && INTVAL (otherops[2]) > -256
8110                                   && INTVAL (otherops[2]) < 256)))
8111                         {
8112                           if (reg_overlap_mentioned_p (otherops[0],
8113                                                        otherops[2]))
8114                             {
8115                               /* Swap base and index registers over to
8116                                  avoid a conflict.  */
8117                               otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8118                               otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8119
8120                             }
8121                           /* If both registers conflict, it will usually
8122                              have been fixed by a splitter.  */
8123                           if (reg_overlap_mentioned_p (otherops[0],
8124                                                         otherops[2]))
8125                             {
8126                               output_asm_insn ("add%?\t%1, %1, %2", otherops);
8127                               output_asm_insn ("ldr%?d\t%0, [%1]",
8128                                                otherops);
8129                               return "";
8130                             }
8131                           else
8132                             {
8133                               output_asm_insn ("ldr%?d\t%0, [%1, %2]",
8134                                                otherops);
8135                               return "";
8136                             }
8137                         }
8138                       if (GET_CODE (otherops[2]) == CONST_INT)
8139                         {
8140                           if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8141                             output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8142                           else
8143                             output_asm_insn ("add%?\t%0, %1, %2", otherops);
8144                         }
8145                       else
8146                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
8147                     }
8148                   else
8149                     output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8150
8151                   return "ldm%?ia\t%0, %M0";
8152                 }
8153               else
8154                 {
8155                   otherops[1] = adjust_address (operands[1], SImode, 4);
8156                   /* Take care of overlapping base/data reg.  */
8157                   if (reg_mentioned_p (operands[0], operands[1]))
8158                     {
8159                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8160                       output_asm_insn ("ldr%?\t%0, %1", operands);
8161                     }
8162                   else
8163                     {
8164                       output_asm_insn ("ldr%?\t%0, %1", operands);
8165                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8166                     }
8167                 }
8168             }
8169         }
8170       else
8171         abort ();  /* Constraints should prevent this.  */
8172     }
8173   else if (code0 == MEM && code1 == REG)
8174     {
8175       if (REGNO (operands[1]) == IP_REGNUM)
8176         abort ();
8177
8178       switch (GET_CODE (XEXP (operands[0], 0)))
8179         {
8180         case REG:
8181           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8182           break;
8183
8184         case PRE_INC:
8185           if (!TARGET_LDRD)
8186             abort (); /* Should never happen now.  */
8187           output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8188           break;
8189
8190         case PRE_DEC:
8191           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8192           break;
8193
8194         case POST_INC:
8195           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8196           break;
8197
8198         case POST_DEC:
8199           if (!TARGET_LDRD)
8200             abort (); /* Should never happen now.  */
8201           output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8202           break;
8203
8204         case PRE_MODIFY:
8205         case POST_MODIFY:
8206           otherops[0] = operands[1];
8207           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8208           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8209
8210           if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8211             output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8212           else
8213             output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8214           break;
8215
8216         case PLUS:
8217           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8218           if (GET_CODE (otherops[2]) == CONST_INT)
8219             {
8220               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8221                 {
8222                 case -8:
8223                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
8224                   return "";
8225
8226                 case -4:
8227                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
8228                   return "";
8229
8230                 case 4:
8231                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8232                   return "";
8233                 }
8234             }
8235           if (TARGET_LDRD
8236               && (GET_CODE (otherops[2]) == REG
8237                   || (GET_CODE (otherops[2]) == CONST_INT
8238                       && INTVAL (otherops[2]) > -256
8239                       && INTVAL (otherops[2]) < 256)))
8240             {
8241               otherops[0] = operands[1];
8242               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8243               output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8244               return "";
8245             }
8246           /* Fall through */
8247
8248         default:
8249           otherops[0] = adjust_address (operands[0], SImode, 4);
8250           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8251           output_asm_insn ("str%?\t%1, %0", operands);
8252           output_asm_insn ("str%?\t%1, %0", otherops);
8253         }
8254     }
8255   else
8256     /* Constraints should prevent this.  */
8257     abort ();
8258
8259   return "";
8260 }
8261
8262
8263 /* Output an arbitrary MOV reg, #n.
8264    OPERANDS[0] is a register.  OPERANDS[1] is a const_int.  */
8265 const char *
8266 output_mov_immediate (rtx *operands)
8267 {
8268   HOST_WIDE_INT n = INTVAL (operands[1]);
8269
8270   /* Try to use one MOV.  */
8271   if (const_ok_for_arm (n))
8272     output_asm_insn ("mov%?\t%0, %1", operands);
8273
8274   /* Try to use one MVN.  */
8275   else if (const_ok_for_arm (~n))
8276     {
8277       operands[1] = GEN_INT (~n);
8278       output_asm_insn ("mvn%?\t%0, %1", operands);
8279     }
8280   else
8281     {
8282       int n_ones = 0;
8283       int i;
8284
8285       /* If all else fails, make it out of ORRs or BICs as appropriate.  */
8286       for (i = 0; i < 32; i++)
8287         if (n & 1 << i)
8288           n_ones++;
8289
8290       if (n_ones > 16)  /* Shorter to use MVN with BIC in this case.  */
8291         output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1, ~ n);
8292       else
8293         output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
8294     }
8295
8296   return "";
8297 }
8298
8299 /* Output an ADD r, s, #n where n may be too big for one instruction.
8300    If adding zero to one register, output nothing.  */
8301 const char *
8302 output_add_immediate (rtx *operands)
8303 {
8304   HOST_WIDE_INT n = INTVAL (operands[2]);
8305
8306   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8307     {
8308       if (n < 0)
8309         output_multi_immediate (operands,
8310                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8311                                 -n);
8312       else
8313         output_multi_immediate (operands,
8314                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8315                                 n);
8316     }
8317
8318   return "";
8319 }
8320
8321 /* Output a multiple immediate operation.
8322    OPERANDS is the vector of operands referred to in the output patterns.
8323    INSTR1 is the output pattern to use for the first constant.
8324    INSTR2 is the output pattern to use for subsequent constants.
8325    IMMED_OP is the index of the constant slot in OPERANDS.
8326    N is the constant value.  */
8327 static const char *
8328 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8329                         int immed_op, HOST_WIDE_INT n)
8330 {
8331 #if HOST_BITS_PER_WIDE_INT > 32
8332   n &= 0xffffffff;
8333 #endif
8334
8335   if (n == 0)
8336     {
8337       /* Quick and easy output.  */
8338       operands[immed_op] = const0_rtx;
8339       output_asm_insn (instr1, operands);
8340     }
8341   else
8342     {
8343       int i;
8344       const char * instr = instr1;
8345
8346       /* Note that n is never zero here (which would give no output).  */
8347       for (i = 0; i < 32; i += 2)
8348         {
8349           if (n & (3 << i))
8350             {
8351               operands[immed_op] = GEN_INT (n & (255 << i));
8352               output_asm_insn (instr, operands);
8353               instr = instr2;
8354               i += 6;
8355             }
8356         }
8357     }
8358
8359   return "";
8360 }
8361
8362 /* Return the appropriate ARM instruction for the operation code.
8363    The returned result should not be overwritten.  OP is the rtx of the
8364    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8365    was shifted.  */
8366 const char *
8367 arithmetic_instr (rtx op, int shift_first_arg)
8368 {
8369   switch (GET_CODE (op))
8370     {
8371     case PLUS:
8372       return "add";
8373
8374     case MINUS:
8375       return shift_first_arg ? "rsb" : "sub";
8376
8377     case IOR:
8378       return "orr";
8379
8380     case XOR:
8381       return "eor";
8382
8383     case AND:
8384       return "and";
8385
8386     default:
8387       abort ();
8388     }
8389 }
8390
8391 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8392    for the operation code.  The returned result should not be overwritten.
8393    OP is the rtx code of the shift.
8394    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8395    shift.  */
8396 static const char *
8397 shift_op (rtx op, HOST_WIDE_INT *amountp)
8398 {
8399   const char * mnem;
8400   enum rtx_code code = GET_CODE (op);
8401
8402   if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
8403     *amountp = -1;
8404   else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
8405     *amountp = INTVAL (XEXP (op, 1));
8406   else
8407     abort ();
8408
8409   switch (code)
8410     {
8411     case ASHIFT:
8412       mnem = "asl";
8413       break;
8414
8415     case ASHIFTRT:
8416       mnem = "asr";
8417       break;
8418
8419     case LSHIFTRT:
8420       mnem = "lsr";
8421       break;
8422
8423     case ROTATE:
8424       if (*amountp == -1)
8425         abort ();
8426       *amountp = 32 - *amountp;
8427
8428       /* Fall through.  */
8429
8430     case ROTATERT:
8431       mnem = "ror";
8432       break;
8433
8434     case MULT:
8435       /* We never have to worry about the amount being other than a
8436          power of 2, since this case can never be reloaded from a reg.  */
8437       if (*amountp != -1)
8438         *amountp = int_log2 (*amountp);
8439       else
8440         abort ();
8441       return "asl";
8442
8443     default:
8444       abort ();
8445     }
8446
8447   if (*amountp != -1)
8448     {
8449       /* This is not 100% correct, but follows from the desire to merge
8450          multiplication by a power of 2 with the recognizer for a
8451          shift.  >=32 is not a valid shift for "asl", so we must try and
8452          output a shift that produces the correct arithmetical result.
8453          Using lsr #32 is identical except for the fact that the carry bit
8454          is not set correctly if we set the flags; but we never use the
8455          carry bit from such an operation, so we can ignore that.  */
8456       if (code == ROTATERT)
8457         /* Rotate is just modulo 32.  */
8458         *amountp &= 31;
8459       else if (*amountp != (*amountp & 31))
8460         {
8461           if (code == ASHIFT)
8462             mnem = "lsr";
8463           *amountp = 32;
8464         }
8465
8466       /* Shifts of 0 are no-ops.  */
8467       if (*amountp == 0)
8468         return NULL;
8469     }
8470
8471   return mnem;
8472 }
8473
8474 /* Obtain the shift from the POWER of two.  */
8475
8476 static HOST_WIDE_INT
8477 int_log2 (HOST_WIDE_INT power)
8478 {
8479   HOST_WIDE_INT shift = 0;
8480
8481   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8482     {
8483       if (shift > 31)
8484         abort ();
8485       shift++;
8486     }
8487
8488   return shift;
8489 }
8490
8491 /* Output a .ascii pseudo-op, keeping track of lengths.  This is because
8492    /bin/as is horribly restrictive.  */
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       switch (c)
8514         {
8515         case TARGET_TAB:
8516           fputs ("\\t", stream);
8517           len_so_far += 2;
8518           break;
8519
8520         case TARGET_FF:
8521           fputs ("\\f", stream);
8522           len_so_far += 2;
8523           break;
8524
8525         case TARGET_BS:
8526           fputs ("\\b", stream);
8527           len_so_far += 2;
8528           break;
8529
8530         case TARGET_CR:
8531           fputs ("\\r", stream);
8532           len_so_far += 2;
8533           break;
8534
8535         case TARGET_NEWLINE:
8536           fputs ("\\n", stream);
8537           c = p [i + 1];
8538           if ((c >= ' ' && c <= '~')
8539               || c == TARGET_TAB)
8540             /* This is a good place for a line break.  */
8541             len_so_far = MAX_ASCII_LEN;
8542           else
8543             len_so_far += 2;
8544           break;
8545
8546         case '\"':
8547         case '\\':
8548           putc ('\\', stream);
8549           len_so_far++;
8550           /* Drop through.  */
8551
8552         default:
8553           if (c >= ' ' && c <= '~')
8554             {
8555               putc (c, stream);
8556               len_so_far++;
8557             }
8558           else
8559             {
8560               fprintf (stream, "\\%03o", c);
8561               len_so_far += 4;
8562             }
8563           break;
8564         }
8565     }
8566
8567   fputs ("\"\n", stream);
8568 }
8569 \f
8570 /* Compute the register save mask for registers 0 through 12
8571    inclusive.  This code is used by arm_compute_save_reg_mask.  */
8572 static unsigned long
8573 arm_compute_save_reg0_reg12_mask (void)
8574 {
8575   unsigned long func_type = arm_current_func_type ();
8576   unsigned int save_reg_mask = 0;
8577   unsigned int reg;
8578
8579   if (IS_INTERRUPT (func_type))
8580     {
8581       unsigned int max_reg;
8582       /* Interrupt functions must not corrupt any registers,
8583          even call clobbered ones.  If this is a leaf function
8584          we can just examine the registers used by the RTL, but
8585          otherwise we have to assume that whatever function is
8586          called might clobber anything, and so we have to save
8587          all the call-clobbered registers as well.  */
8588       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8589         /* FIQ handlers have registers r8 - r12 banked, so
8590            we only need to check r0 - r7, Normal ISRs only
8591            bank r14 and r15, so we must check up to r12.
8592            r13 is the stack pointer which is always preserved,
8593            so we do not need to consider it here.  */
8594         max_reg = 7;
8595       else
8596         max_reg = 12;
8597
8598       for (reg = 0; reg <= max_reg; reg++)
8599         if (regs_ever_live[reg]
8600             || (! current_function_is_leaf && call_used_regs [reg]))
8601           save_reg_mask |= (1 << reg);
8602     }
8603   else
8604     {
8605       /* In the normal case we only need to save those registers
8606          which are call saved and which are used by this function.  */
8607       for (reg = 0; reg <= 10; reg++)
8608         if (regs_ever_live[reg] && ! call_used_regs [reg])
8609           save_reg_mask |= (1 << reg);
8610
8611       /* Handle the frame pointer as a special case.  */
8612       if (! TARGET_APCS_FRAME
8613           && ! frame_pointer_needed
8614           && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8615           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8616         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8617
8618       /* If we aren't loading the PIC register,
8619          don't stack it even though it may be live.  */
8620       if (flag_pic
8621           && ! TARGET_SINGLE_PIC_BASE
8622           && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
8623         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8624     }
8625
8626   /* Save registers so the exception handler can modify them.  */
8627   if (current_function_calls_eh_return)
8628     {
8629       unsigned int i;
8630
8631       for (i = 0; ; i++)
8632         {
8633           reg = EH_RETURN_DATA_REGNO (i);
8634           if (reg == INVALID_REGNUM)
8635             break;
8636           save_reg_mask |= 1 << reg;
8637         }
8638     }
8639
8640   return save_reg_mask;
8641 }
8642
8643 /* Compute a bit mask of which registers need to be
8644    saved on the stack for the current function.  */
8645
8646 static unsigned long
8647 arm_compute_save_reg_mask (void)
8648 {
8649   unsigned int save_reg_mask = 0;
8650   unsigned long func_type = arm_current_func_type ();
8651
8652   if (IS_NAKED (func_type))
8653     /* This should never really happen.  */
8654     return 0;
8655
8656   /* If we are creating a stack frame, then we must save the frame pointer,
8657      IP (which will hold the old stack pointer), LR and the PC.  */
8658   if (frame_pointer_needed)
8659     save_reg_mask |=
8660       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8661       | (1 << IP_REGNUM)
8662       | (1 << LR_REGNUM)
8663       | (1 << PC_REGNUM);
8664
8665   /* Volatile functions do not return, so there
8666      is no need to save any other registers.  */
8667   if (IS_VOLATILE (func_type))
8668     return save_reg_mask;
8669
8670   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8671
8672   /* Decide if we need to save the link register.
8673      Interrupt routines have their own banked link register,
8674      so they never need to save it.
8675      Otherwise if we do not use the link register we do not need to save
8676      it.  If we are pushing other registers onto the stack however, we
8677      can save an instruction in the epilogue by pushing the link register
8678      now and then popping it back into the PC.  This incurs extra memory
8679      accesses though, so we only do it when optimizing for size, and only
8680      if we know that we will not need a fancy return sequence.  */
8681   if (regs_ever_live [LR_REGNUM]
8682           || (save_reg_mask
8683               && optimize_size
8684               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
8685               && !current_function_calls_eh_return))
8686     save_reg_mask |= 1 << LR_REGNUM;
8687
8688   if (cfun->machine->lr_save_eliminated)
8689     save_reg_mask &= ~ (1 << LR_REGNUM);
8690
8691   if (TARGET_REALLY_IWMMXT
8692       && ((bit_count (save_reg_mask)
8693            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8694     {
8695       unsigned int reg;
8696
8697       /* The total number of registers that are going to be pushed
8698          onto the stack is odd.  We need to ensure that the stack
8699          is 64-bit aligned before we start to save iWMMXt registers,
8700          and also before we start to create locals.  (A local variable
8701          might be a double or long long which we will load/store using
8702          an iWMMXt instruction).  Therefore we need to push another
8703          ARM register, so that the stack will be 64-bit aligned.  We
8704          try to avoid using the arg registers (r0 -r3) as they might be
8705          used to pass values in a tail call.  */
8706       for (reg = 4; reg <= 12; reg++)
8707         if ((save_reg_mask & (1 << reg)) == 0)
8708           break;
8709
8710       if (reg <= 12)
8711         save_reg_mask |= (1 << reg);
8712       else
8713         {
8714           cfun->machine->sibcall_blocked = 1;
8715           save_reg_mask |= (1 << 3);
8716         }
8717     }
8718
8719   return save_reg_mask;
8720 }
8721
8722
8723 /* Compute a bit mask of which registers need to be
8724    saved on the stack for the current function.  */
8725 static unsigned long
8726 thumb_compute_save_reg_mask (void)
8727 {
8728   unsigned long mask;
8729   int reg;
8730
8731   mask = 0;
8732   for (reg = 0; reg < 12; reg ++)
8733     {
8734       if (regs_ever_live[reg] && !call_used_regs[reg])
8735         mask |= 1 << reg;
8736     }
8737
8738   if (flag_pic && !TARGET_SINGLE_PIC_BASE)
8739     mask |= PIC_OFFSET_TABLE_REGNUM;
8740   if (TARGET_SINGLE_PIC_BASE)
8741     mask &= ~(1 << arm_pic_register);
8742
8743   /* lr will also be pushed if any lo regs are pushed.  */
8744   if (mask & 0xff || thumb_force_lr_save ())
8745     mask |= (1 << LR_REGNUM);
8746
8747   /* Make sure we have a low work register if we need one.  */
8748   if (((mask & 0xff) == 0 && regs_ever_live[LAST_ARG_REGNUM])
8749       && ((mask & 0x0f00) || TARGET_BACKTRACE))
8750     mask |= 1 << LAST_LO_REGNUM;
8751
8752   return mask;
8753 }
8754
8755
8756 /* Return the number of bytes required to save VFP registers.  */
8757 static int
8758 arm_get_vfp_saved_size (void)
8759 {
8760   unsigned int regno;
8761   int count;
8762   int saved;
8763
8764   saved = 0;
8765   /* Space for saved VFP registers.  */
8766   if (TARGET_HARD_FLOAT && TARGET_VFP)
8767     {
8768       count = 0;
8769       for (regno = FIRST_VFP_REGNUM;
8770            regno < LAST_VFP_REGNUM;
8771            regno += 2)
8772         {
8773           if ((!regs_ever_live[regno] || call_used_regs[regno])
8774               && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8775             {
8776               if (count > 0)
8777                 {
8778                   /* Workaround ARM10 VFPr1 bug.  */
8779                   if (count == 2 && !arm_arch6)
8780                     count++;
8781                   saved += count * 8 + 4;
8782                 }
8783               count = 0;
8784             }
8785           else
8786             count++;
8787         }
8788       if (count > 0)
8789         {
8790           if (count == 2 && !arm_arch6)
8791             count++;
8792           saved += count * 8 + 4;
8793         }
8794     }
8795   return saved;
8796 }
8797
8798
8799 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
8800    everything bar the final return instruction.  */
8801 const char *
8802 output_return_instruction (rtx operand, int really_return, int reverse)
8803 {
8804   char conditional[10];
8805   char instr[100];
8806   int reg;
8807   unsigned long live_regs_mask;
8808   unsigned long func_type;
8809   arm_stack_offsets *offsets;
8810
8811   func_type = arm_current_func_type ();
8812
8813   if (IS_NAKED (func_type))
8814     return "";
8815
8816   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
8817     {
8818       /* If this function was declared non-returning, and we have
8819          found a tail call, then we have to trust that the called
8820          function won't return.  */
8821       if (really_return)
8822         {
8823           rtx ops[2];
8824
8825           /* Otherwise, trap an attempted return by aborting.  */
8826           ops[0] = operand;
8827           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
8828                                        : "abort");
8829           assemble_external_libcall (ops[1]);
8830           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
8831         }
8832
8833       return "";
8834     }
8835
8836   if (current_function_calls_alloca && !really_return)
8837     abort ();
8838
8839   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
8840
8841   return_used_this_function = 1;
8842
8843   live_regs_mask = arm_compute_save_reg_mask ();
8844
8845   if (live_regs_mask)
8846     {
8847       const char * return_reg;
8848
8849       /* If we do not have any special requirements for function exit
8850          (eg interworking, or ISR) then we can load the return address
8851          directly into the PC.  Otherwise we must load it into LR.  */
8852       if (really_return
8853           && ! TARGET_INTERWORK)
8854         return_reg = reg_names[PC_REGNUM];
8855       else
8856         return_reg = reg_names[LR_REGNUM];
8857
8858       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
8859         {
8860           /* There are three possible reasons for the IP register
8861              being saved.  1) a stack frame was created, in which case
8862              IP contains the old stack pointer, or 2) an ISR routine
8863              corrupted it, or 3) it was saved to align the stack on
8864              iWMMXt.  In case 1, restore IP into SP, otherwise just
8865              restore IP.  */
8866           if (frame_pointer_needed)
8867             {
8868               live_regs_mask &= ~ (1 << IP_REGNUM);
8869               live_regs_mask |=   (1 << SP_REGNUM);
8870             }
8871           else
8872             {
8873               if (! IS_INTERRUPT (func_type)
8874                   && ! TARGET_REALLY_IWMMXT)
8875                 abort ();
8876             }
8877         }
8878
8879       /* On some ARM architectures it is faster to use LDR rather than
8880          LDM to load a single register.  On other architectures, the
8881          cost is the same.  In 26 bit mode, or for exception handlers,
8882          we have to use LDM to load the PC so that the CPSR is also
8883          restored.  */
8884       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
8885         {
8886           if (live_regs_mask == (unsigned int)(1 << reg))
8887             break;
8888         }
8889       if (reg <= LAST_ARM_REGNUM
8890           && (reg != LR_REGNUM
8891               || ! really_return
8892               || ! IS_INTERRUPT (func_type)))
8893         {
8894           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
8895                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
8896         }
8897       else
8898         {
8899           char *p;
8900           int first = 1;
8901
8902           /* Generate the load multiple instruction to restore the
8903              registers.  Note we can get here, even if
8904              frame_pointer_needed is true, but only if sp already
8905              points to the base of the saved core registers.  */
8906           if (live_regs_mask & (1 << SP_REGNUM))
8907             {
8908               unsigned HOST_WIDE_INT stack_adjust;
8909
8910               offsets = arm_get_frame_offsets ();
8911               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
8912               if (stack_adjust != 0 && stack_adjust != 4)
8913                 abort ();
8914
8915               if (stack_adjust && arm_arch5)
8916                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
8917               else
8918                 {
8919                   /* If we can't use ldmib (SA110 bug), then try to pop r3
8920                      instead.  */
8921                   if (stack_adjust)
8922                     live_regs_mask |= 1 << 3;
8923                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
8924                 }
8925             }
8926           else
8927             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
8928
8929           p = instr + strlen (instr);
8930
8931           for (reg = 0; reg <= SP_REGNUM; reg++)
8932             if (live_regs_mask & (1 << reg))
8933               {
8934                 int l = strlen (reg_names[reg]);
8935
8936                 if (first)
8937                   first = 0;
8938                 else
8939                   {
8940                     memcpy (p, ", ", 2);
8941                     p += 2;
8942                   }
8943
8944                 memcpy (p, "%|", 2);
8945                 memcpy (p + 2, reg_names[reg], l);
8946                 p += l + 2;
8947               }
8948
8949           if (live_regs_mask & (1 << LR_REGNUM))
8950             {
8951               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
8952               /* If returning from an interrupt, restore the CPSR.  */
8953               if (IS_INTERRUPT (func_type))
8954                 strcat (p, "^");
8955             }
8956           else
8957             strcpy (p, "}");
8958         }
8959
8960       output_asm_insn (instr, & operand);
8961
8962       /* See if we need to generate an extra instruction to
8963          perform the actual function return.  */
8964       if (really_return
8965           && func_type != ARM_FT_INTERWORKED
8966           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
8967         {
8968           /* The return has already been handled
8969              by loading the LR into the PC.  */
8970           really_return = 0;
8971         }
8972     }
8973
8974   if (really_return)
8975     {
8976       switch ((int) ARM_FUNC_TYPE (func_type))
8977         {
8978         case ARM_FT_ISR:
8979         case ARM_FT_FIQ:
8980           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
8981           break;
8982
8983         case ARM_FT_INTERWORKED:
8984           sprintf (instr, "bx%s\t%%|lr", conditional);
8985           break;
8986
8987         case ARM_FT_EXCEPTION:
8988           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
8989           break;
8990
8991         default:
8992           /* Use bx if it's available.  */
8993           if (arm_arch5 || arm_arch4t)
8994             sprintf (instr, "bx%s\t%%|lr", conditional);
8995           else
8996             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
8997           break;
8998         }
8999
9000       output_asm_insn (instr, & operand);
9001     }
9002
9003   return "";
9004 }
9005
9006 /* Write the function name into the code section, directly preceding
9007    the function prologue.
9008
9009    Code will be output similar to this:
9010      t0
9011          .ascii "arm_poke_function_name", 0
9012          .align
9013      t1
9014          .word 0xff000000 + (t1 - t0)
9015      arm_poke_function_name
9016          mov     ip, sp
9017          stmfd   sp!, {fp, ip, lr, pc}
9018          sub     fp, ip, #4
9019
9020    When performing a stack backtrace, code can inspect the value
9021    of 'pc' stored at 'fp' + 0.  If the trace function then looks
9022    at location pc - 12 and the top 8 bits are set, then we know
9023    that there is a function name embedded immediately preceding this
9024    location and has length ((pc[-3]) & 0xff000000).
9025
9026    We assume that pc is declared as a pointer to an unsigned long.
9027
9028    It is of no benefit to output the function name if we are assembling
9029    a leaf function.  These function types will not contain a stack
9030    backtrace structure, therefore it is not possible to determine the
9031    function name.  */
9032 void
9033 arm_poke_function_name (FILE *stream, const char *name)
9034 {
9035   unsigned long alignlength;
9036   unsigned long length;
9037   rtx           x;
9038
9039   length      = strlen (name) + 1;
9040   alignlength = ROUND_UP_WORD (length);
9041
9042   ASM_OUTPUT_ASCII (stream, name, length);
9043   ASM_OUTPUT_ALIGN (stream, 2);
9044   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9045   assemble_aligned_integer (UNITS_PER_WORD, x);
9046 }
9047
9048 /* Place some comments into the assembler stream
9049    describing the current function.  */
9050 static void
9051 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9052 {
9053   unsigned long func_type;
9054
9055   if (!TARGET_ARM)
9056     {
9057       thumb_output_function_prologue (f, frame_size);
9058       return;
9059     }
9060
9061   /* Sanity check.  */
9062   if (arm_ccfsm_state || arm_target_insn)
9063     abort ();
9064
9065   func_type = arm_current_func_type ();
9066
9067   switch ((int) ARM_FUNC_TYPE (func_type))
9068     {
9069     default:
9070     case ARM_FT_NORMAL:
9071       break;
9072     case ARM_FT_INTERWORKED:
9073       asm_fprintf (f, "\t%@ Function supports interworking.\n");
9074       break;
9075     case ARM_FT_ISR:
9076       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9077       break;
9078     case ARM_FT_FIQ:
9079       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9080       break;
9081     case ARM_FT_EXCEPTION:
9082       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9083       break;
9084     }
9085
9086   if (IS_NAKED (func_type))
9087     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9088
9089   if (IS_VOLATILE (func_type))
9090     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9091
9092   if (IS_NESTED (func_type))
9093     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9094
9095   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9096                current_function_args_size,
9097                current_function_pretend_args_size, frame_size);
9098
9099   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9100                frame_pointer_needed,
9101                cfun->machine->uses_anonymous_args);
9102
9103   if (cfun->machine->lr_save_eliminated)
9104     asm_fprintf (f, "\t%@ link register save eliminated.\n");
9105
9106   if (current_function_calls_eh_return)
9107     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9108
9109 #ifdef AOF_ASSEMBLER
9110   if (flag_pic)
9111     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9112 #endif
9113
9114   return_used_this_function = 0;
9115 }
9116
9117 const char *
9118 arm_output_epilogue (rtx sibling)
9119 {
9120   int reg;
9121   unsigned long saved_regs_mask;
9122   unsigned long func_type;
9123   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
9124      frame that is $fp + 4 for a non-variadic function.  */
9125   int floats_offset = 0;
9126   rtx operands[3];
9127   FILE * f = asm_out_file;
9128   unsigned int lrm_count = 0;
9129   int really_return = (sibling == NULL);
9130   int start_reg;
9131   arm_stack_offsets *offsets;
9132
9133   /* If we have already generated the return instruction
9134      then it is futile to generate anything else.  */
9135   if (use_return_insn (FALSE, sibling) && return_used_this_function)
9136     return "";
9137
9138   func_type = arm_current_func_type ();
9139
9140   if (IS_NAKED (func_type))
9141     /* Naked functions don't have epilogues.  */
9142     return "";
9143
9144   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9145     {
9146       rtx op;
9147
9148       /* A volatile function should never return.  Call abort.  */
9149       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9150       assemble_external_libcall (op);
9151       output_asm_insn ("bl\t%a0", &op);
9152
9153       return "";
9154     }
9155
9156   if (current_function_calls_eh_return
9157       && ! really_return)
9158     /* If we are throwing an exception, then we really must
9159        be doing a return,  so we can't tail-call.  */
9160     abort ();
9161
9162   offsets = arm_get_frame_offsets ();
9163   saved_regs_mask = arm_compute_save_reg_mask ();
9164
9165   if (TARGET_IWMMXT)
9166     lrm_count = bit_count (saved_regs_mask);
9167
9168   floats_offset = offsets->saved_args;
9169   /* Compute how far away the floats will be.  */
9170   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9171     if (saved_regs_mask & (1 << reg))
9172       floats_offset += 4;
9173
9174   if (frame_pointer_needed)
9175     {
9176       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
9177       int vfp_offset = offsets->frame;
9178
9179       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9180         {
9181           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9182             if (regs_ever_live[reg] && !call_used_regs[reg])
9183               {
9184                 floats_offset += 12;
9185                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9186                              reg, FP_REGNUM, floats_offset - vfp_offset);
9187               }
9188         }
9189       else
9190         {
9191           start_reg = LAST_FPA_REGNUM;
9192
9193           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9194             {
9195               if (regs_ever_live[reg] && !call_used_regs[reg])
9196                 {
9197                   floats_offset += 12;
9198
9199                   /* We can't unstack more than four registers at once.  */
9200                   if (start_reg - reg == 3)
9201                     {
9202                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9203                                    reg, FP_REGNUM, floats_offset - vfp_offset);
9204                       start_reg = reg - 1;
9205                     }
9206                 }
9207               else
9208                 {
9209                   if (reg != start_reg)
9210                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9211                                  reg + 1, start_reg - reg,
9212                                  FP_REGNUM, floats_offset - vfp_offset);
9213                   start_reg = reg - 1;
9214                 }
9215             }
9216
9217           /* Just in case the last register checked also needs unstacking.  */
9218           if (reg != start_reg)
9219             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9220                          reg + 1, start_reg - reg,
9221                          FP_REGNUM, floats_offset - vfp_offset);
9222         }
9223
9224       if (TARGET_HARD_FLOAT && TARGET_VFP)
9225         {
9226           int saved_size;
9227
9228           /* The fldmx insn does not have base+offset addressing modes,
9229              so we use IP to hold the address.  */
9230           saved_size = arm_get_vfp_saved_size ();
9231
9232           if (saved_size > 0)
9233             {
9234               floats_offset += saved_size;
9235               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9236                            FP_REGNUM, floats_offset - vfp_offset);
9237             }
9238           start_reg = FIRST_VFP_REGNUM;
9239           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9240             {
9241               if ((!regs_ever_live[reg] || call_used_regs[reg])
9242                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9243                 {
9244                   if (start_reg != reg)
9245                     arm_output_fldmx (f, IP_REGNUM,
9246                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9247                                       (reg - start_reg) / 2);
9248                   start_reg = reg + 2;
9249                 }
9250             }
9251           if (start_reg != reg)
9252             arm_output_fldmx (f, IP_REGNUM,
9253                               (start_reg - FIRST_VFP_REGNUM) / 2,
9254                               (reg - start_reg) / 2);
9255         }
9256
9257       if (TARGET_IWMMXT)
9258         {
9259           /* The frame pointer is guaranteed to be non-double-word aligned.
9260              This is because it is set to (old_stack_pointer - 4) and the
9261              old_stack_pointer was double word aligned.  Thus the offset to
9262              the iWMMXt registers to be loaded must also be non-double-word
9263              sized, so that the resultant address *is* double-word aligned.
9264              We can ignore floats_offset since that was already included in
9265              the live_regs_mask.  */
9266           lrm_count += (lrm_count % 2 ? 2 : 1);
9267
9268           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9269             if (regs_ever_live[reg] && !call_used_regs[reg])
9270               {
9271                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9272                              reg, FP_REGNUM, lrm_count * 4);
9273                 lrm_count += 2;
9274               }
9275         }
9276
9277       /* saved_regs_mask should contain the IP, which at the time of stack
9278          frame generation actually contains the old stack pointer.  So a
9279          quick way to unwind the stack is just pop the IP register directly
9280          into the stack pointer.  */
9281       if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
9282         abort ();
9283       saved_regs_mask &= ~ (1 << IP_REGNUM);
9284       saved_regs_mask |=   (1 << SP_REGNUM);
9285
9286       /* There are two registers left in saved_regs_mask - LR and PC.  We
9287          only need to restore the LR register (the return address), but to
9288          save time we can load it directly into the PC, unless we need a
9289          special function exit sequence, or we are not really returning.  */
9290       if (really_return
9291           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9292           && !current_function_calls_eh_return)
9293         /* Delete the LR from the register mask, so that the LR on
9294            the stack is loaded into the PC in the register mask.  */
9295         saved_regs_mask &= ~ (1 << LR_REGNUM);
9296       else
9297         saved_regs_mask &= ~ (1 << PC_REGNUM);
9298
9299       /* We must use SP as the base register, because SP is one of the
9300          registers being restored.  If an interrupt or page fault
9301          happens in the ldm instruction, the SP might or might not
9302          have been restored.  That would be bad, as then SP will no
9303          longer indicate the safe area of stack, and we can get stack
9304          corruption.  Using SP as the base register means that it will
9305          be reset correctly to the original value, should an interrupt
9306          occur.  If the stack pointer already points at the right
9307          place, then omit the subtraction.  */
9308       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9309           || current_function_calls_alloca)
9310         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9311                      4 * bit_count (saved_regs_mask));
9312       print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9313
9314       if (IS_INTERRUPT (func_type))
9315         /* Interrupt handlers will have pushed the
9316            IP onto the stack, so restore it now.  */
9317         print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9318     }
9319   else
9320     {
9321       /* Restore stack pointer if necessary.  */
9322       if (offsets->outgoing_args != offsets->saved_regs)
9323         {
9324           operands[0] = operands[1] = stack_pointer_rtx;
9325           operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9326           output_add_immediate (operands);
9327         }
9328
9329       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9330         {
9331           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9332             if (regs_ever_live[reg] && !call_used_regs[reg])
9333               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9334                            reg, SP_REGNUM);
9335         }
9336       else
9337         {
9338           start_reg = FIRST_FPA_REGNUM;
9339
9340           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9341             {
9342               if (regs_ever_live[reg] && !call_used_regs[reg])
9343                 {
9344                   if (reg - start_reg == 3)
9345                     {
9346                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9347                                    start_reg, SP_REGNUM);
9348                       start_reg = reg + 1;
9349                     }
9350                 }
9351               else
9352                 {
9353                   if (reg != start_reg)
9354                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9355                                  start_reg, reg - start_reg,
9356                                  SP_REGNUM);
9357
9358                   start_reg = reg + 1;
9359                 }
9360             }
9361
9362           /* Just in case the last register checked also needs unstacking.  */
9363           if (reg != start_reg)
9364             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9365                          start_reg, reg - start_reg, SP_REGNUM);
9366         }
9367
9368       if (TARGET_HARD_FLOAT && TARGET_VFP)
9369         {
9370           start_reg = FIRST_VFP_REGNUM;
9371           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9372             {
9373               if ((!regs_ever_live[reg] || call_used_regs[reg])
9374                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9375                 {
9376                   if (start_reg != reg)
9377                     arm_output_fldmx (f, SP_REGNUM,
9378                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9379                                       (reg - start_reg) / 2);
9380                   start_reg = reg + 2;
9381                 }
9382             }
9383           if (start_reg != reg)
9384             arm_output_fldmx (f, SP_REGNUM,
9385                               (start_reg - FIRST_VFP_REGNUM) / 2,
9386                               (reg - start_reg) / 2);
9387         }
9388       if (TARGET_IWMMXT)
9389         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9390           if (regs_ever_live[reg] && !call_used_regs[reg])
9391             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9392
9393       /* If we can, restore the LR into the PC.  */
9394       if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9395           && really_return
9396           && current_function_pretend_args_size == 0
9397           && saved_regs_mask & (1 << LR_REGNUM)
9398           && !current_function_calls_eh_return)
9399         {
9400           saved_regs_mask &= ~ (1 << LR_REGNUM);
9401           saved_regs_mask |=   (1 << PC_REGNUM);
9402         }
9403
9404       /* Load the registers off the stack.  If we only have one register
9405          to load use the LDR instruction - it is faster.  */
9406       if (saved_regs_mask == (1 << LR_REGNUM))
9407         {
9408           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9409         }
9410       else if (saved_regs_mask)
9411         {
9412           if (saved_regs_mask & (1 << SP_REGNUM))
9413             /* Note - write back to the stack register is not enabled
9414                (ie "ldmfd sp!...").  We know that the stack pointer is
9415                in the list of registers and if we add writeback the
9416                instruction becomes UNPREDICTABLE.  */
9417             print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9418           else
9419             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9420         }
9421
9422       if (current_function_pretend_args_size)
9423         {
9424           /* Unwind the pre-pushed regs.  */
9425           operands[0] = operands[1] = stack_pointer_rtx;
9426           operands[2] = GEN_INT (current_function_pretend_args_size);
9427           output_add_immediate (operands);
9428         }
9429     }
9430
9431   /* We may have already restored PC directly from the stack.  */
9432   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
9433     return "";
9434
9435   /* Stack adjustment for exception handler.  */
9436   if (current_function_calls_eh_return)
9437     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
9438                  ARM_EH_STACKADJ_REGNUM);
9439
9440   /* Generate the return instruction.  */
9441   switch ((int) ARM_FUNC_TYPE (func_type))
9442     {
9443     case ARM_FT_ISR:
9444     case ARM_FT_FIQ:
9445       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9446       break;
9447
9448     case ARM_FT_EXCEPTION:
9449       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9450       break;
9451
9452     case ARM_FT_INTERWORKED:
9453       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9454       break;
9455
9456     default:
9457       if (arm_arch5 || arm_arch4t)
9458         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9459       else
9460         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9461       break;
9462     }
9463
9464   return "";
9465 }
9466
9467 static void
9468 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9469                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9470 {
9471   arm_stack_offsets *offsets;
9472
9473   if (TARGET_THUMB)
9474     {
9475       /* ??? Probably not safe to set this here, since it assumes that a
9476          function will be emitted as assembly immediately after we generate
9477          RTL for it.  This does not happen for inline functions.  */
9478       return_used_this_function = 0;
9479     }
9480   else
9481     {
9482       /* We need to take into account any stack-frame rounding.  */
9483       offsets = arm_get_frame_offsets ();
9484
9485       if (use_return_insn (FALSE, NULL)
9486           && return_used_this_function
9487           && offsets->saved_regs != offsets->outgoing_args
9488           && !frame_pointer_needed)
9489         abort ();
9490
9491       /* Reset the ARM-specific per-function variables.  */
9492       after_arm_reorg = 0;
9493     }
9494 }
9495
9496 /* Generate and emit an insn that we will recognize as a push_multi.
9497    Unfortunately, since this insn does not reflect very well the actual
9498    semantics of the operation, we need to annotate the insn for the benefit
9499    of DWARF2 frame unwind information.  */
9500 static rtx
9501 emit_multi_reg_push (int mask)
9502 {
9503   int num_regs = 0;
9504   int num_dwarf_regs;
9505   int i, j;
9506   rtx par;
9507   rtx dwarf;
9508   int dwarf_par_index;
9509   rtx tmp, reg;
9510
9511   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9512     if (mask & (1 << i))
9513       num_regs++;
9514
9515   if (num_regs == 0 || num_regs > 16)
9516     abort ();
9517
9518   /* We don't record the PC in the dwarf frame information.  */
9519   num_dwarf_regs = num_regs;
9520   if (mask & (1 << PC_REGNUM))
9521     num_dwarf_regs--;
9522
9523   /* For the body of the insn we are going to generate an UNSPEC in
9524      parallel with several USEs.  This allows the insn to be recognized
9525      by the push_multi pattern in the arm.md file.  The insn looks
9526      something like this:
9527
9528        (parallel [
9529            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9530                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9531            (use (reg:SI 11 fp))
9532            (use (reg:SI 12 ip))
9533            (use (reg:SI 14 lr))
9534            (use (reg:SI 15 pc))
9535         ])
9536
9537      For the frame note however, we try to be more explicit and actually
9538      show each register being stored into the stack frame, plus a (single)
9539      decrement of the stack pointer.  We do it this way in order to be
9540      friendly to the stack unwinding code, which only wants to see a single
9541      stack decrement per instruction.  The RTL we generate for the note looks
9542      something like this:
9543
9544       (sequence [
9545            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9546            (set (mem:SI (reg:SI sp)) (reg:SI r4))
9547            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9548            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9549            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9550         ])
9551
9552       This sequence is used both by the code to support stack unwinding for
9553       exceptions handlers and the code to generate dwarf2 frame debugging.  */
9554
9555   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9556   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9557   dwarf_par_index = 1;
9558
9559   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9560     {
9561       if (mask & (1 << i))
9562         {
9563           reg = gen_rtx_REG (SImode, i);
9564
9565           XVECEXP (par, 0, 0)
9566             = gen_rtx_SET (VOIDmode,
9567                            gen_rtx_MEM (BLKmode,
9568                                         gen_rtx_PRE_DEC (BLKmode,
9569                                                          stack_pointer_rtx)),
9570                            gen_rtx_UNSPEC (BLKmode,
9571                                            gen_rtvec (1, reg),
9572                                            UNSPEC_PUSH_MULT));
9573
9574           if (i != PC_REGNUM)
9575             {
9576               tmp = gen_rtx_SET (VOIDmode,
9577                                  gen_rtx_MEM (SImode, stack_pointer_rtx),
9578                                  reg);
9579               RTX_FRAME_RELATED_P (tmp) = 1;
9580               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9581               dwarf_par_index++;
9582             }
9583
9584           break;
9585         }
9586     }
9587
9588   for (j = 1, i++; j < num_regs; i++)
9589     {
9590       if (mask & (1 << i))
9591         {
9592           reg = gen_rtx_REG (SImode, i);
9593
9594           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9595
9596           if (i != PC_REGNUM)
9597             {
9598               tmp = gen_rtx_SET (VOIDmode,
9599                                  gen_rtx_MEM (SImode,
9600                                               plus_constant (stack_pointer_rtx,
9601                                                              4 * j)),
9602                                  reg);
9603               RTX_FRAME_RELATED_P (tmp) = 1;
9604               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9605             }
9606
9607           j++;
9608         }
9609     }
9610
9611   par = emit_insn (par);
9612
9613   tmp = gen_rtx_SET (SImode,
9614                      stack_pointer_rtx,
9615                      gen_rtx_PLUS (SImode,
9616                                    stack_pointer_rtx,
9617                                    GEN_INT (-4 * num_regs)));
9618   RTX_FRAME_RELATED_P (tmp) = 1;
9619   XVECEXP (dwarf, 0, 0) = tmp;
9620
9621   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9622                                        REG_NOTES (par));
9623   return par;
9624 }
9625
9626 static rtx
9627 emit_sfm (int base_reg, int count)
9628 {
9629   rtx par;
9630   rtx dwarf;
9631   rtx tmp, reg;
9632   int i;
9633
9634   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9635   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9636
9637   reg = gen_rtx_REG (XFmode, base_reg++);
9638
9639   XVECEXP (par, 0, 0)
9640     = gen_rtx_SET (VOIDmode,
9641                    gen_rtx_MEM (BLKmode,
9642                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9643                    gen_rtx_UNSPEC (BLKmode,
9644                                    gen_rtvec (1, reg),
9645                                    UNSPEC_PUSH_MULT));
9646   tmp = gen_rtx_SET (VOIDmode,
9647                      gen_rtx_MEM (XFmode, stack_pointer_rtx), reg);
9648   RTX_FRAME_RELATED_P (tmp) = 1;
9649   XVECEXP (dwarf, 0, 1) = tmp;
9650
9651   for (i = 1; i < count; i++)
9652     {
9653       reg = gen_rtx_REG (XFmode, base_reg++);
9654       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9655
9656       tmp = gen_rtx_SET (VOIDmode,
9657                          gen_rtx_MEM (XFmode,
9658                                       plus_constant (stack_pointer_rtx,
9659                                                      i * 12)),
9660                          reg);
9661       RTX_FRAME_RELATED_P (tmp) = 1;
9662       XVECEXP (dwarf, 0, i + 1) = tmp;
9663     }
9664
9665   tmp = gen_rtx_SET (VOIDmode,
9666                      stack_pointer_rtx,
9667                      gen_rtx_PLUS (SImode,
9668                                    stack_pointer_rtx,
9669                                    GEN_INT (-12 * count)));
9670   RTX_FRAME_RELATED_P (tmp) = 1;
9671   XVECEXP (dwarf, 0, 0) = tmp;
9672
9673   par = emit_insn (par);
9674   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9675                                        REG_NOTES (par));
9676   return par;
9677 }
9678
9679
9680 /* Return true if the current function needs to save/restore LR.  */
9681
9682 static bool
9683 thumb_force_lr_save (void)
9684 {
9685   return !cfun->machine->lr_save_eliminated
9686          && (!leaf_function_p ()
9687              || thumb_far_jump_used_p ()
9688              || regs_ever_live [LR_REGNUM]);
9689 }
9690
9691
9692 /* Compute the distance from register FROM to register TO.
9693    These can be the arg pointer (26), the soft frame pointer (25),
9694    the stack pointer (13) or the hard frame pointer (11).
9695    In thumb mode r7 is used as the soft frame pointer, if needed.
9696    Typical stack layout looks like this:
9697
9698        old stack pointer -> |    |
9699                              ----
9700                             |    | \
9701                             |    |   saved arguments for
9702                             |    |   vararg functions
9703                             |    | /
9704                               --
9705    hard FP & arg pointer -> |    | \
9706                             |    |   stack
9707                             |    |   frame
9708                             |    | /
9709                               --
9710                             |    | \
9711                             |    |   call saved
9712                             |    |   registers
9713       soft frame pointer -> |    | /
9714                               --
9715                             |    | \
9716                             |    |   local
9717                             |    |   variables
9718                             |    | /
9719                               --
9720                             |    | \
9721                             |    |   outgoing
9722                             |    |   arguments
9723    current stack pointer -> |    | /
9724                               --
9725
9726   For a given function some or all of these stack components
9727   may not be needed, giving rise to the possibility of
9728   eliminating some of the registers.
9729
9730   The values returned by this function must reflect the behavior
9731   of arm_expand_prologue() and arm_compute_save_reg_mask().
9732
9733   The sign of the number returned reflects the direction of stack
9734   growth, so the values are positive for all eliminations except
9735   from the soft frame pointer to the hard frame pointer.
9736
9737   SFP may point just inside the local variables block to ensure correct
9738   alignment.  */
9739
9740
9741 /* Calculate stack offsets.  These are used to calculate register elimination
9742    offsets and in prologue/epilogue code.  */
9743
9744 static arm_stack_offsets *
9745 arm_get_frame_offsets (void)
9746 {
9747   struct arm_stack_offsets *offsets;
9748   unsigned long func_type;
9749   int leaf;
9750   int saved;
9751   HOST_WIDE_INT frame_size;
9752
9753   offsets = &cfun->machine->stack_offsets;
9754
9755   /* We need to know if we are a leaf function.  Unfortunately, it
9756      is possible to be called after start_sequence has been called,
9757      which causes get_insns to return the insns for the sequence,
9758      not the function, which will cause leaf_function_p to return
9759      the incorrect result.
9760
9761      to know about leaf functions once reload has completed, and the
9762      frame size cannot be changed after that time, so we can safely
9763      use the cached value.  */
9764
9765   if (reload_completed)
9766     return offsets;
9767
9768   /* Initially this is the size of the local variables.  It will translated
9769      into an offset once we have determined the size of preceding data.  */
9770   frame_size = ROUND_UP_WORD (get_frame_size ());
9771
9772   leaf = leaf_function_p ();
9773
9774   /* Space for variadic functions.  */
9775   offsets->saved_args = current_function_pretend_args_size;
9776
9777   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
9778
9779   if (TARGET_ARM)
9780     {
9781       unsigned int regno;
9782
9783       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
9784
9785       /* We know that SP will be doubleword aligned on entry, and we must
9786          preserve that condition at any subroutine call.  We also require the
9787          soft frame pointer to be doubleword aligned.  */
9788
9789       if (TARGET_REALLY_IWMMXT)
9790         {
9791           /* Check for the call-saved iWMMXt registers.  */
9792           for (regno = FIRST_IWMMXT_REGNUM;
9793                regno <= LAST_IWMMXT_REGNUM;
9794                regno++)
9795             if (regs_ever_live [regno] && ! call_used_regs [regno])
9796               saved += 8;
9797         }
9798
9799       func_type = arm_current_func_type ();
9800       if (! IS_VOLATILE (func_type))
9801         {
9802           /* Space for saved FPA registers.  */
9803           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
9804           if (regs_ever_live[regno] && ! call_used_regs[regno])
9805             saved += 12;
9806
9807           /* Space for saved VFP registers.  */
9808           if (TARGET_HARD_FLOAT && TARGET_VFP)
9809             saved += arm_get_vfp_saved_size ();
9810         }
9811     }
9812   else /* TARGET_THUMB */
9813     {
9814       saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
9815       if (TARGET_BACKTRACE)
9816         saved += 16;
9817     }
9818
9819   /* Saved registers include the stack frame.  */
9820   offsets->saved_regs = offsets->saved_args + saved;
9821   offsets->soft_frame = offsets->saved_regs;
9822   /* A leaf function does not need any stack alignment if it has nothing
9823      on the stack.  */
9824   if (leaf && frame_size == 0)
9825     {
9826       offsets->outgoing_args = offsets->soft_frame;
9827       return offsets;
9828     }
9829
9830   /* Ensure SFP has the correct alignment.  */
9831   if (ARM_DOUBLEWORD_ALIGN
9832       && (offsets->soft_frame & 7))
9833     offsets->soft_frame += 4;
9834
9835   offsets->outgoing_args = offsets->soft_frame + frame_size
9836                            + current_function_outgoing_args_size;
9837
9838   if (ARM_DOUBLEWORD_ALIGN)
9839     {
9840       /* Ensure SP remains doubleword aligned.  */
9841       if (offsets->outgoing_args & 7)
9842         offsets->outgoing_args += 4;
9843       if (offsets->outgoing_args & 7)
9844         abort ();
9845     }
9846
9847   return offsets;
9848 }
9849
9850
9851 /* Calculate the relative offsets for the different stack pointers.  Positive
9852    offsets are in the direction of stack growth.  */
9853
9854 HOST_WIDE_INT
9855 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
9856 {
9857   arm_stack_offsets *offsets;
9858
9859   offsets = arm_get_frame_offsets ();
9860
9861   /* OK, now we have enough information to compute the distances.
9862      There must be an entry in these switch tables for each pair
9863      of registers in ELIMINABLE_REGS, even if some of the entries
9864      seem to be redundant or useless.  */
9865   switch (from)
9866     {
9867     case ARG_POINTER_REGNUM:
9868       switch (to)
9869         {
9870         case THUMB_HARD_FRAME_POINTER_REGNUM:
9871           return 0;
9872
9873         case FRAME_POINTER_REGNUM:
9874           /* This is the reverse of the soft frame pointer
9875              to hard frame pointer elimination below.  */
9876           return offsets->soft_frame - offsets->saved_args;
9877
9878         case ARM_HARD_FRAME_POINTER_REGNUM:
9879           /* If there is no stack frame then the hard
9880              frame pointer and the arg pointer coincide.  */
9881           if (offsets->frame == offsets->saved_regs)
9882             return 0;
9883           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
9884           return (frame_pointer_needed
9885                   && cfun->static_chain_decl != NULL
9886                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
9887
9888         case STACK_POINTER_REGNUM:
9889           /* If nothing has been pushed on the stack at all
9890              then this will return -4.  This *is* correct!  */
9891           return offsets->outgoing_args - (offsets->saved_args + 4);
9892
9893         default:
9894           abort ();
9895         }
9896       break;
9897
9898     case FRAME_POINTER_REGNUM:
9899       switch (to)
9900         {
9901         case THUMB_HARD_FRAME_POINTER_REGNUM:
9902           return 0;
9903
9904         case ARM_HARD_FRAME_POINTER_REGNUM:
9905           /* The hard frame pointer points to the top entry in the
9906              stack frame.  The soft frame pointer to the bottom entry
9907              in the stack frame.  If there is no stack frame at all,
9908              then they are identical.  */
9909
9910           return offsets->frame - offsets->soft_frame;
9911
9912         case STACK_POINTER_REGNUM:
9913           return offsets->outgoing_args - offsets->soft_frame;
9914
9915         default:
9916           abort ();
9917         }
9918       break;
9919
9920     default:
9921       /* You cannot eliminate from the stack pointer.
9922          In theory you could eliminate from the hard frame
9923          pointer to the stack pointer, but this will never
9924          happen, since if a stack frame is not needed the
9925          hard frame pointer will never be used.  */
9926       abort ();
9927     }
9928 }
9929
9930
9931 /* Generate the prologue instructions for entry into an ARM function.  */
9932 void
9933 arm_expand_prologue (void)
9934 {
9935   int reg;
9936   rtx amount;
9937   rtx insn;
9938   rtx ip_rtx;
9939   unsigned long live_regs_mask;
9940   unsigned long func_type;
9941   int fp_offset = 0;
9942   int saved_pretend_args = 0;
9943   int saved_regs = 0;
9944   unsigned int args_to_push;
9945   arm_stack_offsets *offsets;
9946
9947   func_type = arm_current_func_type ();
9948
9949   /* Naked functions don't have prologues.  */
9950   if (IS_NAKED (func_type))
9951     return;
9952
9953   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
9954   args_to_push = current_function_pretend_args_size;
9955
9956   /* Compute which register we will have to save onto the stack.  */
9957   live_regs_mask = arm_compute_save_reg_mask ();
9958
9959   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
9960
9961   if (frame_pointer_needed)
9962     {
9963       if (IS_INTERRUPT (func_type))
9964         {
9965           /* Interrupt functions must not corrupt any registers.
9966              Creating a frame pointer however, corrupts the IP
9967              register, so we must push it first.  */
9968           insn = emit_multi_reg_push (1 << IP_REGNUM);
9969
9970           /* Do not set RTX_FRAME_RELATED_P on this insn.
9971              The dwarf stack unwinding code only wants to see one
9972              stack decrement per function, and this is not it.  If
9973              this instruction is labeled as being part of the frame
9974              creation sequence then dwarf2out_frame_debug_expr will
9975              abort when it encounters the assignment of IP to FP
9976              later on, since the use of SP here establishes SP as
9977              the CFA register and not IP.
9978
9979              Anyway this instruction is not really part of the stack
9980              frame creation although it is part of the prologue.  */
9981         }
9982       else if (IS_NESTED (func_type))
9983         {
9984           /* The Static chain register is the same as the IP register
9985              used as a scratch register during stack frame creation.
9986              To get around this need to find somewhere to store IP
9987              whilst the frame is being created.  We try the following
9988              places in order:
9989
9990                1. The last argument register.
9991                2. A slot on the stack above the frame.  (This only
9992                   works if the function is not a varargs function).
9993                3. Register r3, after pushing the argument registers
9994                   onto the stack.
9995
9996              Note - we only need to tell the dwarf2 backend about the SP
9997              adjustment in the second variant; the static chain register
9998              doesn't need to be unwound, as it doesn't contain a value
9999              inherited from the caller.  */
10000
10001           if (regs_ever_live[3] == 0)
10002             {
10003               insn = gen_rtx_REG (SImode, 3);
10004               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10005               insn = emit_insn (insn);
10006             }
10007           else if (args_to_push == 0)
10008             {
10009               rtx dwarf;
10010               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10011               insn = gen_rtx_MEM (SImode, insn);
10012               insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10013               insn = emit_insn (insn);
10014
10015               fp_offset = 4;
10016
10017               /* Just tell the dwarf backend that we adjusted SP.  */
10018               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10019                                    gen_rtx_PLUS (SImode, stack_pointer_rtx,
10020                                                  GEN_INT (-fp_offset)));
10021               RTX_FRAME_RELATED_P (insn) = 1;
10022               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10023                                                     dwarf, REG_NOTES (insn));
10024             }
10025           else
10026             {
10027               /* Store the args on the stack.  */
10028               if (cfun->machine->uses_anonymous_args)
10029                 insn = emit_multi_reg_push
10030                   ((0xf0 >> (args_to_push / 4)) & 0xf);
10031               else
10032                 insn = emit_insn
10033                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10034                                GEN_INT (- args_to_push)));
10035
10036               RTX_FRAME_RELATED_P (insn) = 1;
10037
10038               saved_pretend_args = 1;
10039               fp_offset = args_to_push;
10040               args_to_push = 0;
10041
10042               /* Now reuse r3 to preserve IP.  */
10043               insn = gen_rtx_REG (SImode, 3);
10044               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10045               (void) emit_insn (insn);
10046             }
10047         }
10048
10049       if (fp_offset)
10050         {
10051           insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10052           insn = gen_rtx_SET  (SImode, ip_rtx, insn);
10053         }
10054       else
10055         insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10056
10057       insn = emit_insn (insn);
10058       RTX_FRAME_RELATED_P (insn) = 1;
10059     }
10060
10061   if (args_to_push)
10062     {
10063       /* Push the argument registers, or reserve space for them.  */
10064       if (cfun->machine->uses_anonymous_args)
10065         insn = emit_multi_reg_push
10066           ((0xf0 >> (args_to_push / 4)) & 0xf);
10067       else
10068         insn = emit_insn
10069           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10070                        GEN_INT (- args_to_push)));
10071       RTX_FRAME_RELATED_P (insn) = 1;
10072     }
10073
10074   /* If this is an interrupt service routine, and the link register
10075      is going to be pushed, and we are not creating a stack frame,
10076      (which would involve an extra push of IP and a pop in the epilogue)
10077      subtracting four from LR now will mean that the function return
10078      can be done with a single instruction.  */
10079   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10080       && (live_regs_mask & (1 << LR_REGNUM)) != 0
10081       && ! frame_pointer_needed)
10082     emit_insn (gen_rtx_SET (SImode,
10083                             gen_rtx_REG (SImode, LR_REGNUM),
10084                             gen_rtx_PLUS (SImode,
10085                                           gen_rtx_REG (SImode, LR_REGNUM),
10086                                           GEN_INT (-4))));
10087
10088   if (live_regs_mask)
10089     {
10090       insn = emit_multi_reg_push (live_regs_mask);
10091       saved_regs += bit_count (live_regs_mask) * 4;
10092       RTX_FRAME_RELATED_P (insn) = 1;
10093     }
10094
10095   if (TARGET_IWMMXT)
10096     for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10097       if (regs_ever_live[reg] && ! call_used_regs [reg])
10098         {
10099           insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10100           insn = gen_rtx_MEM (V2SImode, insn);
10101           insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10102                                          gen_rtx_REG (V2SImode, reg)));
10103           RTX_FRAME_RELATED_P (insn) = 1;
10104           saved_regs += 8;
10105         }
10106
10107   if (! IS_VOLATILE (func_type))
10108     {
10109       int start_reg;
10110
10111       /* Save any floating point call-saved registers used by this
10112          function.  */
10113       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10114         {
10115           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10116             if (regs_ever_live[reg] && !call_used_regs[reg])
10117               {
10118                 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10119                 insn = gen_rtx_MEM (XFmode, insn);
10120                 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10121                                                gen_rtx_REG (XFmode, reg)));
10122                 RTX_FRAME_RELATED_P (insn) = 1;
10123                 saved_regs += 12;
10124               }
10125         }
10126       else
10127         {
10128           start_reg = LAST_FPA_REGNUM;
10129
10130           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10131             {
10132               if (regs_ever_live[reg] && !call_used_regs[reg])
10133                 {
10134                   if (start_reg - reg == 3)
10135                     {
10136                       insn = emit_sfm (reg, 4);
10137                       RTX_FRAME_RELATED_P (insn) = 1;
10138                       saved_regs += 48;
10139                       start_reg = reg - 1;
10140                     }
10141                 }
10142               else
10143                 {
10144                   if (start_reg != reg)
10145                     {
10146                       insn = emit_sfm (reg + 1, start_reg - reg);
10147                       RTX_FRAME_RELATED_P (insn) = 1;
10148                       saved_regs += (start_reg - reg) * 12;
10149                     }
10150                   start_reg = reg - 1;
10151                 }
10152             }
10153
10154           if (start_reg != reg)
10155             {
10156               insn = emit_sfm (reg + 1, start_reg - reg);
10157               saved_regs += (start_reg - reg) * 12;
10158               RTX_FRAME_RELATED_P (insn) = 1;
10159             }
10160         }
10161       if (TARGET_HARD_FLOAT && TARGET_VFP)
10162         {
10163           start_reg = FIRST_VFP_REGNUM;
10164
10165           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10166             {
10167               if ((!regs_ever_live[reg] || call_used_regs[reg])
10168                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10169                 {
10170                   if (start_reg != reg)
10171                     saved_regs += vfp_emit_fstmx (start_reg,
10172                                                   (reg - start_reg) / 2);
10173                   start_reg = reg + 2;
10174                 }
10175             }
10176           if (start_reg != reg)
10177             saved_regs += vfp_emit_fstmx (start_reg,
10178                                           (reg - start_reg) / 2);
10179         }
10180     }
10181
10182   if (frame_pointer_needed)
10183     {
10184       /* Create the new frame pointer.  */
10185       insn = GEN_INT (-(4 + args_to_push + fp_offset));
10186       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10187       RTX_FRAME_RELATED_P (insn) = 1;
10188
10189       if (IS_NESTED (func_type))
10190         {
10191           /* Recover the static chain register.  */
10192           if (regs_ever_live [3] == 0
10193               || saved_pretend_args)
10194             insn = gen_rtx_REG (SImode, 3);
10195           else /* if (current_function_pretend_args_size == 0) */
10196             {
10197               insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10198                                    GEN_INT (4));
10199               insn = gen_rtx_MEM (SImode, insn);
10200             }
10201
10202           emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10203           /* Add a USE to stop propagate_one_insn() from barfing.  */
10204           emit_insn (gen_prologue_use (ip_rtx));
10205         }
10206     }
10207
10208   offsets = arm_get_frame_offsets ();
10209   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10210     {
10211       /* This add can produce multiple insns for a large constant, so we
10212          need to get tricky.  */
10213       rtx last = get_last_insn ();
10214
10215       amount = GEN_INT (offsets->saved_args + saved_regs
10216                         - offsets->outgoing_args);
10217
10218       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10219                                     amount));
10220       do
10221         {
10222           last = last ? NEXT_INSN (last) : get_insns ();
10223           RTX_FRAME_RELATED_P (last) = 1;
10224         }
10225       while (last != insn);
10226
10227       /* If the frame pointer is needed, emit a special barrier that
10228          will prevent the scheduler from moving stores to the frame
10229          before the stack adjustment.  */
10230       if (frame_pointer_needed)
10231         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10232                                          hard_frame_pointer_rtx));
10233     }
10234
10235
10236   if (flag_pic)
10237     arm_load_pic_register ();
10238
10239   /* If we are profiling, make sure no instructions are scheduled before
10240      the call to mcount.  Similarly if the user has requested no
10241      scheduling in the prolog.  */
10242   if (current_function_profile || TARGET_NO_SCHED_PRO)
10243     emit_insn (gen_blockage ());
10244
10245   /* If the link register is being kept alive, with the return address in it,
10246      then make sure that it does not get reused by the ce2 pass.  */
10247   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10248     {
10249       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10250       cfun->machine->lr_save_eliminated = 1;
10251     }
10252 }
10253 \f
10254 /* If CODE is 'd', then the X is a condition operand and the instruction
10255    should only be executed if the condition is true.
10256    if CODE is 'D', then the X is a condition operand and the instruction
10257    should only be executed if the condition is false: however, if the mode
10258    of the comparison is CCFPEmode, then always execute the instruction -- we
10259    do this because in these circumstances !GE does not necessarily imply LT;
10260    in these cases the instruction pattern will take care to make sure that
10261    an instruction containing %d will follow, thereby undoing the effects of
10262    doing this instruction unconditionally.
10263    If CODE is 'N' then X is a floating point operand that must be negated
10264    before output.
10265    If CODE is 'B' then output a bitwise inverted value of X (a const int).
10266    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
10267 void
10268 arm_print_operand (FILE *stream, rtx x, int code)
10269 {
10270   switch (code)
10271     {
10272     case '@':
10273       fputs (ASM_COMMENT_START, stream);
10274       return;
10275
10276     case '_':
10277       fputs (user_label_prefix, stream);
10278       return;
10279
10280     case '|':
10281       fputs (REGISTER_PREFIX, stream);
10282       return;
10283
10284     case '?':
10285       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10286         {
10287           if (TARGET_THUMB || current_insn_predicate != NULL)
10288             abort ();
10289
10290           fputs (arm_condition_codes[arm_current_cc], stream);
10291         }
10292       else if (current_insn_predicate)
10293         {
10294           enum arm_cond_code code;
10295
10296           if (TARGET_THUMB)
10297             abort ();
10298
10299           code = get_arm_condition_code (current_insn_predicate);
10300           fputs (arm_condition_codes[code], stream);
10301         }
10302       return;
10303
10304     case 'N':
10305       {
10306         REAL_VALUE_TYPE r;
10307         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10308         r = REAL_VALUE_NEGATE (r);
10309         fprintf (stream, "%s", fp_const_from_val (&r));
10310       }
10311       return;
10312
10313     case 'B':
10314       if (GET_CODE (x) == CONST_INT)
10315         {
10316           HOST_WIDE_INT val;
10317           val = ARM_SIGN_EXTEND (~INTVAL (x));
10318           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10319         }
10320       else
10321         {
10322           putc ('~', stream);
10323           output_addr_const (stream, x);
10324         }
10325       return;
10326
10327     case 'i':
10328       fprintf (stream, "%s", arithmetic_instr (x, 1));
10329       return;
10330
10331     /* Truncate Cirrus shift counts.  */
10332     case 's':
10333       if (GET_CODE (x) == CONST_INT)
10334         {
10335           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10336           return;
10337         }
10338       arm_print_operand (stream, x, 0);
10339       return;
10340
10341     case 'I':
10342       fprintf (stream, "%s", arithmetic_instr (x, 0));
10343       return;
10344
10345     case 'S':
10346       {
10347         HOST_WIDE_INT val;
10348         const char * shift = shift_op (x, &val);
10349
10350         if (shift)
10351           {
10352             fprintf (stream, ", %s ", shift_op (x, &val));
10353             if (val == -1)
10354               arm_print_operand (stream, XEXP (x, 1), 0);
10355             else
10356               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10357           }
10358       }
10359       return;
10360
10361       /* An explanation of the 'Q', 'R' and 'H' register operands:
10362
10363          In a pair of registers containing a DI or DF value the 'Q'
10364          operand returns the register number of the register containing
10365          the least significant part of the value.  The 'R' operand returns
10366          the register number of the register containing the most
10367          significant part of the value.
10368
10369          The 'H' operand returns the higher of the two register numbers.
10370          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10371          same as the 'Q' operand, since the most significant part of the
10372          value is held in the lower number register.  The reverse is true
10373          on systems where WORDS_BIG_ENDIAN is false.
10374
10375          The purpose of these operands is to distinguish between cases
10376          where the endian-ness of the values is important (for example
10377          when they are added together), and cases where the endian-ness
10378          is irrelevant, but the order of register operations is important.
10379          For example when loading a value from memory into a register
10380          pair, the endian-ness does not matter.  Provided that the value
10381          from the lower memory address is put into the lower numbered
10382          register, and the value from the higher address is put into the
10383          higher numbered register, the load will work regardless of whether
10384          the value being loaded is big-wordian or little-wordian.  The
10385          order of the two register loads can matter however, if the address
10386          of the memory location is actually held in one of the registers
10387          being overwritten by the load.  */
10388     case 'Q':
10389       if (REGNO (x) > LAST_ARM_REGNUM)
10390         abort ();
10391       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10392       return;
10393
10394     case 'R':
10395       if (REGNO (x) > LAST_ARM_REGNUM)
10396         abort ();
10397       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10398       return;
10399
10400     case 'H':
10401       if (REGNO (x) > LAST_ARM_REGNUM)
10402         abort ();
10403       asm_fprintf (stream, "%r", REGNO (x) + 1);
10404       return;
10405
10406     case 'm':
10407       asm_fprintf (stream, "%r",
10408                    GET_CODE (XEXP (x, 0)) == REG
10409                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10410       return;
10411
10412     case 'M':
10413       asm_fprintf (stream, "{%r-%r}",
10414                    REGNO (x),
10415                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10416       return;
10417
10418     case 'd':
10419       /* CONST_TRUE_RTX means always -- that's the default.  */
10420       if (x == const_true_rtx)
10421         return;
10422
10423       fputs (arm_condition_codes[get_arm_condition_code (x)],
10424              stream);
10425       return;
10426
10427     case 'D':
10428       /* CONST_TRUE_RTX means not always -- ie never.  We shouldn't ever
10429          want to do that.  */
10430       if (x == const_true_rtx)
10431         abort ();
10432
10433       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10434                                  (get_arm_condition_code (x))],
10435              stream);
10436       return;
10437
10438     /* Cirrus registers can be accessed in a variety of ways:
10439          single floating point (f)
10440          double floating point (d)
10441          32bit integer         (fx)
10442          64bit integer         (dx).  */
10443     case 'W':                   /* Cirrus register in F mode.  */
10444     case 'X':                   /* Cirrus register in D mode.  */
10445     case 'Y':                   /* Cirrus register in FX mode.  */
10446     case 'Z':                   /* Cirrus register in DX mode.  */
10447       if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10448         abort ();
10449
10450       fprintf (stream, "mv%s%s",
10451                code == 'W' ? "f"
10452                : code == 'X' ? "d"
10453                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10454
10455       return;
10456
10457     /* Print cirrus register in the mode specified by the register's mode.  */
10458     case 'V':
10459       {
10460         int mode = GET_MODE (x);
10461
10462         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10463           abort ();
10464
10465         fprintf (stream, "mv%s%s",
10466                  mode == DFmode ? "d"
10467                  : mode == SImode ? "fx"
10468                  : mode == DImode ? "dx"
10469                  : "f", reg_names[REGNO (x)] + 2);
10470
10471         return;
10472       }
10473
10474     case 'U':
10475       if (GET_CODE (x) != REG
10476           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10477           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10478         /* Bad value for wCG register number.  */
10479         abort ();
10480       else
10481         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10482       return;
10483
10484       /* Print an iWMMXt control register name.  */
10485     case 'w':
10486       if (GET_CODE (x) != CONST_INT
10487           || INTVAL (x) < 0
10488           || INTVAL (x) >= 16)
10489         /* Bad value for wC register number.  */
10490         abort ();
10491       else
10492         {
10493           static const char * wc_reg_names [16] =
10494             {
10495               "wCID",  "wCon",  "wCSSF", "wCASF",
10496               "wC4",   "wC5",   "wC6",   "wC7",
10497               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10498               "wC12",  "wC13",  "wC14",  "wC15"
10499             };
10500
10501           fprintf (stream, wc_reg_names [INTVAL (x)]);
10502         }
10503       return;
10504
10505       /* Print a VFP double precision register name.  */
10506     case 'P':
10507       {
10508         int mode = GET_MODE (x);
10509         int num;
10510
10511         if (mode != DImode && mode != DFmode)
10512           abort ();
10513
10514         if (GET_CODE (x) != REG
10515             || !IS_VFP_REGNUM (REGNO (x)))
10516           abort ();
10517
10518         num = REGNO(x) - FIRST_VFP_REGNUM;
10519         if (num & 1)
10520           abort ();
10521
10522         fprintf (stream, "d%d", num >> 1);
10523       }
10524       return;
10525
10526     default:
10527       if (x == 0)
10528         abort ();
10529
10530       if (GET_CODE (x) == REG)
10531         asm_fprintf (stream, "%r", REGNO (x));
10532       else if (GET_CODE (x) == MEM)
10533         {
10534           output_memory_reference_mode = GET_MODE (x);
10535           output_address (XEXP (x, 0));
10536         }
10537       else if (GET_CODE (x) == CONST_DOUBLE)
10538         fprintf (stream, "#%s", fp_immediate_constant (x));
10539       else if (GET_CODE (x) == NEG)
10540         abort (); /* This should never happen now.  */
10541       else
10542         {
10543           fputc ('#', stream);
10544           output_addr_const (stream, x);
10545         }
10546     }
10547 }
10548 \f
10549 #ifndef AOF_ASSEMBLER
10550 /* Target hook for assembling integer objects.  The ARM version needs to
10551    handle word-sized values specially.  */
10552 static bool
10553 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10554 {
10555   if (size == UNITS_PER_WORD && aligned_p)
10556     {
10557       fputs ("\t.word\t", asm_out_file);
10558       output_addr_const (asm_out_file, x);
10559
10560       /* Mark symbols as position independent.  We only do this in the
10561          .text segment, not in the .data segment.  */
10562       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10563           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10564         {
10565           if (GET_CODE (x) == SYMBOL_REF
10566               && (CONSTANT_POOL_ADDRESS_P (x)
10567                   || SYMBOL_REF_LOCAL_P (x)))
10568             fputs ("(GOTOFF)", asm_out_file);
10569           else if (GET_CODE (x) == LABEL_REF)
10570             fputs ("(GOTOFF)", asm_out_file);
10571           else
10572             fputs ("(GOT)", asm_out_file);
10573         }
10574       fputc ('\n', asm_out_file);
10575       return true;
10576     }
10577
10578   if (arm_vector_mode_supported_p (GET_MODE (x)))
10579     {
10580       int i, units;
10581
10582       if (GET_CODE (x) != CONST_VECTOR)
10583         abort ();
10584
10585       units = CONST_VECTOR_NUNITS (x);
10586
10587       switch (GET_MODE (x))
10588         {
10589         case V2SImode: size = 4; break;
10590         case V4HImode: size = 2; break;
10591         case V8QImode: size = 1; break;
10592         default:
10593           abort ();
10594         }
10595
10596       for (i = 0; i < units; i++)
10597         {
10598           rtx elt;
10599
10600           elt = CONST_VECTOR_ELT (x, i);
10601           assemble_integer
10602             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10603         }
10604
10605       return true;
10606     }
10607
10608   return default_assemble_integer (x, size, aligned_p);
10609 }
10610 #endif
10611 \f
10612 /* A finite state machine takes care of noticing whether or not instructions
10613    can be conditionally executed, and thus decrease execution time and code
10614    size by deleting branch instructions.  The fsm is controlled by
10615    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
10616
10617 /* The state of the fsm controlling condition codes are:
10618    0: normal, do nothing special
10619    1: make ASM_OUTPUT_OPCODE not output this instruction
10620    2: make ASM_OUTPUT_OPCODE not output this instruction
10621    3: make instructions conditional
10622    4: make instructions conditional
10623
10624    State transitions (state->state by whom under condition):
10625    0 -> 1 final_prescan_insn if the `target' is a label
10626    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10627    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10628    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10629    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10630           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10631    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10632           (the target insn is arm_target_insn).
10633
10634    If the jump clobbers the conditions then we use states 2 and 4.
10635
10636    A similar thing can be done with conditional return insns.
10637
10638    XXX In case the `target' is an unconditional branch, this conditionalising
10639    of the instructions always reduces code size, but not always execution
10640    time.  But then, I want to reduce the code size to somewhere near what
10641    /bin/cc produces.  */
10642
10643 /* Returns the index of the ARM condition code string in
10644    `arm_condition_codes'.  COMPARISON should be an rtx like
10645    `(eq (...) (...))'.  */
10646 static enum arm_cond_code
10647 get_arm_condition_code (rtx comparison)
10648 {
10649   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10650   int code;
10651   enum rtx_code comp_code = GET_CODE (comparison);
10652
10653   if (GET_MODE_CLASS (mode) != MODE_CC)
10654     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10655                            XEXP (comparison, 1));
10656
10657   switch (mode)
10658     {
10659     case CC_DNEmode: code = ARM_NE; goto dominance;
10660     case CC_DEQmode: code = ARM_EQ; goto dominance;
10661     case CC_DGEmode: code = ARM_GE; goto dominance;
10662     case CC_DGTmode: code = ARM_GT; goto dominance;
10663     case CC_DLEmode: code = ARM_LE; goto dominance;
10664     case CC_DLTmode: code = ARM_LT; goto dominance;
10665     case CC_DGEUmode: code = ARM_CS; goto dominance;
10666     case CC_DGTUmode: code = ARM_HI; goto dominance;
10667     case CC_DLEUmode: code = ARM_LS; goto dominance;
10668     case CC_DLTUmode: code = ARM_CC;
10669
10670     dominance:
10671       if (comp_code != EQ && comp_code != NE)
10672         abort ();
10673
10674       if (comp_code == EQ)
10675         return ARM_INVERSE_CONDITION_CODE (code);
10676       return code;
10677
10678     case CC_NOOVmode:
10679       switch (comp_code)
10680         {
10681         case NE: return ARM_NE;
10682         case EQ: return ARM_EQ;
10683         case GE: return ARM_PL;
10684         case LT: return ARM_MI;
10685         default: abort ();
10686         }
10687
10688     case CC_Zmode:
10689       switch (comp_code)
10690         {
10691         case NE: return ARM_NE;
10692         case EQ: return ARM_EQ;
10693         default: abort ();
10694         }
10695
10696     case CC_Nmode:
10697       switch (comp_code)
10698         {
10699         case NE: return ARM_MI;
10700         case EQ: return ARM_PL;
10701         default: abort ();
10702         }
10703
10704     case CCFPEmode:
10705     case CCFPmode:
10706       /* These encodings assume that AC=1 in the FPA system control
10707          byte.  This allows us to handle all cases except UNEQ and
10708          LTGT.  */
10709       switch (comp_code)
10710         {
10711         case GE: return ARM_GE;
10712         case GT: return ARM_GT;
10713         case LE: return ARM_LS;
10714         case LT: return ARM_MI;
10715         case NE: return ARM_NE;
10716         case EQ: return ARM_EQ;
10717         case ORDERED: return ARM_VC;
10718         case UNORDERED: return ARM_VS;
10719         case UNLT: return ARM_LT;
10720         case UNLE: return ARM_LE;
10721         case UNGT: return ARM_HI;
10722         case UNGE: return ARM_PL;
10723           /* UNEQ and LTGT do not have a representation.  */
10724         case UNEQ: /* Fall through.  */
10725         case LTGT: /* Fall through.  */
10726         default: abort ();
10727         }
10728
10729     case CC_SWPmode:
10730       switch (comp_code)
10731         {
10732         case NE: return ARM_NE;
10733         case EQ: return ARM_EQ;
10734         case GE: return ARM_LE;
10735         case GT: return ARM_LT;
10736         case LE: return ARM_GE;
10737         case LT: return ARM_GT;
10738         case GEU: return ARM_LS;
10739         case GTU: return ARM_CC;
10740         case LEU: return ARM_CS;
10741         case LTU: return ARM_HI;
10742         default: abort ();
10743         }
10744
10745     case CC_Cmode:
10746       switch (comp_code)
10747       {
10748       case LTU: return ARM_CS;
10749       case GEU: return ARM_CC;
10750       default: abort ();
10751       }
10752
10753     case CCmode:
10754       switch (comp_code)
10755         {
10756         case NE: return ARM_NE;
10757         case EQ: return ARM_EQ;
10758         case GE: return ARM_GE;
10759         case GT: return ARM_GT;
10760         case LE: return ARM_LE;
10761         case LT: return ARM_LT;
10762         case GEU: return ARM_CS;
10763         case GTU: return ARM_HI;
10764         case LEU: return ARM_LS;
10765         case LTU: return ARM_CC;
10766         default: abort ();
10767         }
10768
10769     default: abort ();
10770     }
10771
10772   abort ();
10773 }
10774
10775 void
10776 arm_final_prescan_insn (rtx insn)
10777 {
10778   /* BODY will hold the body of INSN.  */
10779   rtx body = PATTERN (insn);
10780
10781   /* This will be 1 if trying to repeat the trick, and things need to be
10782      reversed if it appears to fail.  */
10783   int reverse = 0;
10784
10785   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
10786      taken are clobbered, even if the rtl suggests otherwise.  It also
10787      means that we have to grub around within the jump expression to find
10788      out what the conditions are when the jump isn't taken.  */
10789   int jump_clobbers = 0;
10790
10791   /* If we start with a return insn, we only succeed if we find another one.  */
10792   int seeking_return = 0;
10793
10794   /* START_INSN will hold the insn from where we start looking.  This is the
10795      first insn after the following code_label if REVERSE is true.  */
10796   rtx start_insn = insn;
10797
10798   /* If in state 4, check if the target branch is reached, in order to
10799      change back to state 0.  */
10800   if (arm_ccfsm_state == 4)
10801     {
10802       if (insn == arm_target_insn)
10803         {
10804           arm_target_insn = NULL;
10805           arm_ccfsm_state = 0;
10806         }
10807       return;
10808     }
10809
10810   /* If in state 3, it is possible to repeat the trick, if this insn is an
10811      unconditional branch to a label, and immediately following this branch
10812      is the previous target label which is only used once, and the label this
10813      branch jumps to is not too far off.  */
10814   if (arm_ccfsm_state == 3)
10815     {
10816       if (simplejump_p (insn))
10817         {
10818           start_insn = next_nonnote_insn (start_insn);
10819           if (GET_CODE (start_insn) == BARRIER)
10820             {
10821               /* XXX Isn't this always a barrier?  */
10822               start_insn = next_nonnote_insn (start_insn);
10823             }
10824           if (GET_CODE (start_insn) == CODE_LABEL
10825               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
10826               && LABEL_NUSES (start_insn) == 1)
10827             reverse = TRUE;
10828           else
10829             return;
10830         }
10831       else if (GET_CODE (body) == RETURN)
10832         {
10833           start_insn = next_nonnote_insn (start_insn);
10834           if (GET_CODE (start_insn) == BARRIER)
10835             start_insn = next_nonnote_insn (start_insn);
10836           if (GET_CODE (start_insn) == CODE_LABEL
10837               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
10838               && LABEL_NUSES (start_insn) == 1)
10839             {
10840               reverse = TRUE;
10841               seeking_return = 1;
10842             }
10843           else
10844             return;
10845         }
10846       else
10847         return;
10848     }
10849
10850   if (arm_ccfsm_state != 0 && !reverse)
10851     abort ();
10852   if (GET_CODE (insn) != JUMP_INSN)
10853     return;
10854
10855   /* This jump might be paralleled with a clobber of the condition codes
10856      the jump should always come first */
10857   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
10858     body = XVECEXP (body, 0, 0);
10859
10860   if (reverse
10861       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
10862           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
10863     {
10864       int insns_skipped;
10865       int fail = FALSE, succeed = FALSE;
10866       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
10867       int then_not_else = TRUE;
10868       rtx this_insn = start_insn, label = 0;
10869
10870       /* If the jump cannot be done with one instruction, we cannot
10871          conditionally execute the instruction in the inverse case.  */
10872       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
10873         {
10874           jump_clobbers = 1;
10875           return;
10876         }
10877
10878       /* Register the insn jumped to.  */
10879       if (reverse)
10880         {
10881           if (!seeking_return)
10882             label = XEXP (SET_SRC (body), 0);
10883         }
10884       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
10885         label = XEXP (XEXP (SET_SRC (body), 1), 0);
10886       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
10887         {
10888           label = XEXP (XEXP (SET_SRC (body), 2), 0);
10889           then_not_else = FALSE;
10890         }
10891       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
10892         seeking_return = 1;
10893       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
10894         {
10895           seeking_return = 1;
10896           then_not_else = FALSE;
10897         }
10898       else
10899         abort ();
10900
10901       /* See how many insns this branch skips, and what kind of insns.  If all
10902          insns are okay, and the label or unconditional branch to the same
10903          label is not too far away, succeed.  */
10904       for (insns_skipped = 0;
10905            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
10906         {
10907           rtx scanbody;
10908
10909           this_insn = next_nonnote_insn (this_insn);
10910           if (!this_insn)
10911             break;
10912
10913           switch (GET_CODE (this_insn))
10914             {
10915             case CODE_LABEL:
10916               /* Succeed if it is the target label, otherwise fail since
10917                  control falls in from somewhere else.  */
10918               if (this_insn == label)
10919                 {
10920                   if (jump_clobbers)
10921                     {
10922                       arm_ccfsm_state = 2;
10923                       this_insn = next_nonnote_insn (this_insn);
10924                     }
10925                   else
10926                     arm_ccfsm_state = 1;
10927                   succeed = TRUE;
10928                 }
10929               else
10930                 fail = TRUE;
10931               break;
10932
10933             case BARRIER:
10934               /* Succeed if the following insn is the target label.
10935                  Otherwise fail.
10936                  If return insns are used then the last insn in a function
10937                  will be a barrier.  */
10938               this_insn = next_nonnote_insn (this_insn);
10939               if (this_insn && this_insn == label)
10940                 {
10941                   if (jump_clobbers)
10942                     {
10943                       arm_ccfsm_state = 2;
10944                       this_insn = next_nonnote_insn (this_insn);
10945                     }
10946                   else
10947                     arm_ccfsm_state = 1;
10948                   succeed = TRUE;
10949                 }
10950               else
10951                 fail = TRUE;
10952               break;
10953
10954             case CALL_INSN:
10955               /* The AAPCS says that conditional calls should not be
10956                  used since they make interworking inefficient (the
10957                  linker can't transform BL<cond> into BLX).  That's
10958                  only a problem if the machine has BLX.  */
10959               if (arm_arch5)
10960                 {
10961                   fail = TRUE;
10962                   break;
10963                 }
10964
10965               /* Succeed if the following insn is the target label, or
10966                  if the following two insns are a barrier and the
10967                  target label.  */
10968               this_insn = next_nonnote_insn (this_insn);
10969               if (this_insn && GET_CODE (this_insn) == BARRIER)
10970                 this_insn = next_nonnote_insn (this_insn);
10971
10972               if (this_insn && this_insn == label
10973                   && insns_skipped < max_insns_skipped)
10974                 {
10975                   if (jump_clobbers)
10976                     {
10977                       arm_ccfsm_state = 2;
10978                       this_insn = next_nonnote_insn (this_insn);
10979                     }
10980                   else
10981                     arm_ccfsm_state = 1;
10982                   succeed = TRUE;
10983                 }
10984               else
10985                 fail = TRUE;
10986               break;
10987
10988             case JUMP_INSN:
10989               /* If this is an unconditional branch to the same label, succeed.
10990                  If it is to another label, do nothing.  If it is conditional,
10991                  fail.  */
10992               /* XXX Probably, the tests for SET and the PC are
10993                  unnecessary.  */
10994
10995               scanbody = PATTERN (this_insn);
10996               if (GET_CODE (scanbody) == SET
10997                   && GET_CODE (SET_DEST (scanbody)) == PC)
10998                 {
10999                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11000                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11001                     {
11002                       arm_ccfsm_state = 2;
11003                       succeed = TRUE;
11004                     }
11005                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11006                     fail = TRUE;
11007                 }
11008               /* Fail if a conditional return is undesirable (eg on a
11009                  StrongARM), but still allow this if optimizing for size.  */
11010               else if (GET_CODE (scanbody) == RETURN
11011                        && !use_return_insn (TRUE, NULL)
11012                        && !optimize_size)
11013                 fail = TRUE;
11014               else if (GET_CODE (scanbody) == RETURN
11015                        && seeking_return)
11016                 {
11017                   arm_ccfsm_state = 2;
11018                   succeed = TRUE;
11019                 }
11020               else if (GET_CODE (scanbody) == PARALLEL)
11021                 {
11022                   switch (get_attr_conds (this_insn))
11023                     {
11024                     case CONDS_NOCOND:
11025                       break;
11026                     default:
11027                       fail = TRUE;
11028                       break;
11029                     }
11030                 }
11031               else
11032                 fail = TRUE;    /* Unrecognized jump (eg epilogue).  */
11033
11034               break;
11035
11036             case INSN:
11037               /* Instructions using or affecting the condition codes make it
11038                  fail.  */
11039               scanbody = PATTERN (this_insn);
11040               if (!(GET_CODE (scanbody) == SET
11041                     || GET_CODE (scanbody) == PARALLEL)
11042                   || get_attr_conds (this_insn) != CONDS_NOCOND)
11043                 fail = TRUE;
11044
11045               /* A conditional cirrus instruction must be followed by
11046                  a non Cirrus instruction.  However, since we
11047                  conditionalize instructions in this function and by
11048                  the time we get here we can't add instructions
11049                  (nops), because shorten_branches() has already been
11050                  called, we will disable conditionalizing Cirrus
11051                  instructions to be safe.  */
11052               if (GET_CODE (scanbody) != USE
11053                   && GET_CODE (scanbody) != CLOBBER
11054                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11055                 fail = TRUE;
11056               break;
11057
11058             default:
11059               break;
11060             }
11061         }
11062       if (succeed)
11063         {
11064           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11065             arm_target_label = CODE_LABEL_NUMBER (label);
11066           else if (seeking_return || arm_ccfsm_state == 2)
11067             {
11068               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11069                 {
11070                   this_insn = next_nonnote_insn (this_insn);
11071                   if (this_insn && (GET_CODE (this_insn) == BARRIER
11072                                     || GET_CODE (this_insn) == CODE_LABEL))
11073                     abort ();
11074                 }
11075               if (!this_insn)
11076                 {
11077                   /* Oh, dear! we ran off the end.. give up.  */
11078                   recog (PATTERN (insn), insn, NULL);
11079                   arm_ccfsm_state = 0;
11080                   arm_target_insn = NULL;
11081                   return;
11082                 }
11083               arm_target_insn = this_insn;
11084             }
11085           else
11086             abort ();
11087           if (jump_clobbers)
11088             {
11089               if (reverse)
11090                 abort ();
11091               arm_current_cc =
11092                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11093                                                             0), 0), 1));
11094               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11095                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11096               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11097                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11098             }
11099           else
11100             {
11101               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11102                  what it was.  */
11103               if (!reverse)
11104                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11105                                                                0));
11106             }
11107
11108           if (reverse || then_not_else)
11109             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11110         }
11111
11112       /* Restore recog_data (getting the attributes of other insns can
11113          destroy this array, but final.c assumes that it remains intact
11114          across this call; since the insn has been recognized already we
11115          call recog direct).  */
11116       recog (PATTERN (insn), insn, NULL);
11117     }
11118 }
11119
11120 /* Returns true if REGNO is a valid register
11121    for holding a quantity of type MODE.  */
11122 int
11123 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11124 {
11125   if (GET_MODE_CLASS (mode) == MODE_CC)
11126     return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11127
11128   if (TARGET_THUMB)
11129     /* For the Thumb we only allow values bigger than SImode in
11130        registers 0 - 6, so that there is always a second low
11131        register available to hold the upper part of the value.
11132        We probably we ought to ensure that the register is the
11133        start of an even numbered register pair.  */
11134     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11135
11136   if (IS_CIRRUS_REGNUM (regno))
11137     /* We have outlawed SI values in Cirrus registers because they
11138        reside in the lower 32 bits, but SF values reside in the
11139        upper 32 bits.  This causes gcc all sorts of grief.  We can't
11140        even split the registers into pairs because Cirrus SI values
11141        get sign extended to 64bits-- aldyh.  */
11142     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11143
11144   if (IS_VFP_REGNUM (regno))
11145     {
11146       if (mode == SFmode || mode == SImode)
11147         return TRUE;
11148
11149       /* DFmode values are only valid in even register pairs.  */
11150       if (mode == DFmode)
11151         return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11152       return FALSE;
11153     }
11154
11155   if (IS_IWMMXT_GR_REGNUM (regno))
11156     return mode == SImode;
11157
11158   if (IS_IWMMXT_REGNUM (regno))
11159     return VALID_IWMMXT_REG_MODE (mode);
11160
11161   /* We allow any value to be stored in the general registers.
11162      Restrict doubleword quantities to even register pairs so that we can
11163      use ldrd.  */
11164   if (regno <= LAST_ARM_REGNUM)
11165     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11166
11167   if (   regno == FRAME_POINTER_REGNUM
11168       || regno == ARG_POINTER_REGNUM)
11169     /* We only allow integers in the fake hard registers.  */
11170     return GET_MODE_CLASS (mode) == MODE_INT;
11171
11172   /* The only registers left are the FPA registers
11173      which we only allow to hold FP values.  */
11174   return GET_MODE_CLASS (mode) == MODE_FLOAT
11175     && regno >= FIRST_FPA_REGNUM
11176     && regno <= LAST_FPA_REGNUM;
11177 }
11178
11179 int
11180 arm_regno_class (int regno)
11181 {
11182   if (TARGET_THUMB)
11183     {
11184       if (regno == STACK_POINTER_REGNUM)
11185         return STACK_REG;
11186       if (regno == CC_REGNUM)
11187         return CC_REG;
11188       if (regno < 8)
11189         return LO_REGS;
11190       return HI_REGS;
11191     }
11192
11193   if (   regno <= LAST_ARM_REGNUM
11194       || regno == FRAME_POINTER_REGNUM
11195       || regno == ARG_POINTER_REGNUM)
11196     return GENERAL_REGS;
11197
11198   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11199     return NO_REGS;
11200
11201   if (IS_CIRRUS_REGNUM (regno))
11202     return CIRRUS_REGS;
11203
11204   if (IS_VFP_REGNUM (regno))
11205     return VFP_REGS;
11206
11207   if (IS_IWMMXT_REGNUM (regno))
11208     return IWMMXT_REGS;
11209
11210   if (IS_IWMMXT_GR_REGNUM (regno))
11211     return IWMMXT_GR_REGS;
11212
11213   return FPA_REGS;
11214 }
11215
11216 /* Handle a special case when computing the offset
11217    of an argument from the frame pointer.  */
11218 int
11219 arm_debugger_arg_offset (int value, rtx addr)
11220 {
11221   rtx insn;
11222
11223   /* We are only interested if dbxout_parms() failed to compute the offset.  */
11224   if (value != 0)
11225     return 0;
11226
11227   /* We can only cope with the case where the address is held in a register.  */
11228   if (GET_CODE (addr) != REG)
11229     return 0;
11230
11231   /* If we are using the frame pointer to point at the argument, then
11232      an offset of 0 is correct.  */
11233   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11234     return 0;
11235
11236   /* If we are using the stack pointer to point at the
11237      argument, then an offset of 0 is correct.  */
11238   if ((TARGET_THUMB || !frame_pointer_needed)
11239       && REGNO (addr) == SP_REGNUM)
11240     return 0;
11241
11242   /* Oh dear.  The argument is pointed to by a register rather
11243      than being held in a register, or being stored at a known
11244      offset from the frame pointer.  Since GDB only understands
11245      those two kinds of argument we must translate the address
11246      held in the register into an offset from the frame pointer.
11247      We do this by searching through the insns for the function
11248      looking to see where this register gets its value.  If the
11249      register is initialized from the frame pointer plus an offset
11250      then we are in luck and we can continue, otherwise we give up.
11251
11252      This code is exercised by producing debugging information
11253      for a function with arguments like this:
11254
11255            double func (double a, double b, int c, double d) {return d;}
11256
11257      Without this code the stab for parameter 'd' will be set to
11258      an offset of 0 from the frame pointer, rather than 8.  */
11259
11260   /* The if() statement says:
11261
11262      If the insn is a normal instruction
11263      and if the insn is setting the value in a register
11264      and if the register being set is the register holding the address of the argument
11265      and if the address is computing by an addition
11266      that involves adding to a register
11267      which is the frame pointer
11268      a constant integer
11269
11270      then...  */
11271
11272   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11273     {
11274       if (   GET_CODE (insn) == INSN
11275           && GET_CODE (PATTERN (insn)) == SET
11276           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11277           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11278           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11279           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11280           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11281              )
11282         {
11283           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11284
11285           break;
11286         }
11287     }
11288
11289   if (value == 0)
11290     {
11291       debug_rtx (addr);
11292       warning ("unable to compute real location of stacked parameter");
11293       value = 8; /* XXX magic hack */
11294     }
11295
11296   return value;
11297 }
11298 \f
11299 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
11300   do                                                                    \
11301     {                                                                   \
11302       if ((MASK) & insn_flags)                                          \
11303         lang_hooks.builtin_function ((NAME), (TYPE), (CODE),            \
11304                                      BUILT_IN_MD, NULL, NULL_TREE);     \
11305     }                                                                   \
11306   while (0)
11307
11308 struct builtin_description
11309 {
11310   const unsigned int       mask;
11311   const enum insn_code     icode;
11312   const char * const       name;
11313   const enum arm_builtins  code;
11314   const enum rtx_code      comparison;
11315   const unsigned int       flag;
11316 };
11317
11318 static const struct builtin_description bdesc_2arg[] =
11319 {
11320 #define IWMMXT_BUILTIN(code, string, builtin) \
11321   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11322     ARM_BUILTIN_##builtin, 0, 0 },
11323
11324   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11325   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11326   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11327   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11328   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11329   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11330   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11331   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11332   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11333   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11334   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11335   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11336   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11337   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11338   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11339   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11340   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11341   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11342   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11343   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11344   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11345   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11346   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11347   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11348   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11349   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11350   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11351   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11352   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11353   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11354   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11355   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11356   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11357   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11358   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11359   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11360   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11361   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11362   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11363   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11364   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11365   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11366   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11367   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11368   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11369   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11370   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11371   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11372   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11373   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11374   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11375   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11376   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11377   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11378   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11379   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11380   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11381   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11382
11383 #define IWMMXT_BUILTIN2(code, builtin) \
11384   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11385
11386   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11387   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11388   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11389   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11390   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11391   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11392   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
11393   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
11394   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
11395   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
11396   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
11397   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
11398   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
11399   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
11400   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
11401   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
11402   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
11403   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
11404   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
11405   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
11406   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
11407   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
11408   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
11409   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
11410   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
11411   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
11412   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
11413   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
11414   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
11415   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
11416   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
11417   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
11418 };
11419
11420 static const struct builtin_description bdesc_1arg[] =
11421 {
11422   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11423   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11424   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11425   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11426   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11427   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11428   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11429   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11430   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11431   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11432   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11433   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11434   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11435   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11436   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11437   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11438   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11439   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11440 };
11441
11442 /* Set up all the iWMMXt builtins.  This is
11443    not called if TARGET_IWMMXT is zero.  */
11444
11445 static void
11446 arm_init_iwmmxt_builtins (void)
11447 {
11448   const struct builtin_description * d;
11449   size_t i;
11450   tree endlink = void_list_node;
11451
11452   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11453   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11454   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11455
11456   tree int_ftype_int
11457     = build_function_type (integer_type_node,
11458                            tree_cons (NULL_TREE, integer_type_node, endlink));
11459   tree v8qi_ftype_v8qi_v8qi_int
11460     = build_function_type (V8QI_type_node,
11461                            tree_cons (NULL_TREE, V8QI_type_node,
11462                                       tree_cons (NULL_TREE, V8QI_type_node,
11463                                                  tree_cons (NULL_TREE,
11464                                                             integer_type_node,
11465                                                             endlink))));
11466   tree v4hi_ftype_v4hi_int
11467     = build_function_type (V4HI_type_node,
11468                            tree_cons (NULL_TREE, V4HI_type_node,
11469                                       tree_cons (NULL_TREE, integer_type_node,
11470                                                  endlink)));
11471   tree v2si_ftype_v2si_int
11472     = build_function_type (V2SI_type_node,
11473                            tree_cons (NULL_TREE, V2SI_type_node,
11474                                       tree_cons (NULL_TREE, integer_type_node,
11475                                                  endlink)));
11476   tree v2si_ftype_di_di
11477     = build_function_type (V2SI_type_node,
11478                            tree_cons (NULL_TREE, long_long_integer_type_node,
11479                                       tree_cons (NULL_TREE, long_long_integer_type_node,
11480                                                  endlink)));
11481   tree di_ftype_di_int
11482     = build_function_type (long_long_integer_type_node,
11483                            tree_cons (NULL_TREE, long_long_integer_type_node,
11484                                       tree_cons (NULL_TREE, integer_type_node,
11485                                                  endlink)));
11486   tree di_ftype_di_int_int
11487     = build_function_type (long_long_integer_type_node,
11488                            tree_cons (NULL_TREE, long_long_integer_type_node,
11489                                       tree_cons (NULL_TREE, integer_type_node,
11490                                                  tree_cons (NULL_TREE,
11491                                                             integer_type_node,
11492                                                             endlink))));
11493   tree int_ftype_v8qi
11494     = build_function_type (integer_type_node,
11495                            tree_cons (NULL_TREE, V8QI_type_node,
11496                                       endlink));
11497   tree int_ftype_v4hi
11498     = build_function_type (integer_type_node,
11499                            tree_cons (NULL_TREE, V4HI_type_node,
11500                                       endlink));
11501   tree int_ftype_v2si
11502     = build_function_type (integer_type_node,
11503                            tree_cons (NULL_TREE, V2SI_type_node,
11504                                       endlink));
11505   tree int_ftype_v8qi_int
11506     = build_function_type (integer_type_node,
11507                            tree_cons (NULL_TREE, V8QI_type_node,
11508                                       tree_cons (NULL_TREE, integer_type_node,
11509                                                  endlink)));
11510   tree int_ftype_v4hi_int
11511     = build_function_type (integer_type_node,
11512                            tree_cons (NULL_TREE, V4HI_type_node,
11513                                       tree_cons (NULL_TREE, integer_type_node,
11514                                                  endlink)));
11515   tree int_ftype_v2si_int
11516     = build_function_type (integer_type_node,
11517                            tree_cons (NULL_TREE, V2SI_type_node,
11518                                       tree_cons (NULL_TREE, integer_type_node,
11519                                                  endlink)));
11520   tree v8qi_ftype_v8qi_int_int
11521     = build_function_type (V8QI_type_node,
11522                            tree_cons (NULL_TREE, V8QI_type_node,
11523                                       tree_cons (NULL_TREE, integer_type_node,
11524                                                  tree_cons (NULL_TREE,
11525                                                             integer_type_node,
11526                                                             endlink))));
11527   tree v4hi_ftype_v4hi_int_int
11528     = build_function_type (V4HI_type_node,
11529                            tree_cons (NULL_TREE, V4HI_type_node,
11530                                       tree_cons (NULL_TREE, integer_type_node,
11531                                                  tree_cons (NULL_TREE,
11532                                                             integer_type_node,
11533                                                             endlink))));
11534   tree v2si_ftype_v2si_int_int
11535     = build_function_type (V2SI_type_node,
11536                            tree_cons (NULL_TREE, V2SI_type_node,
11537                                       tree_cons (NULL_TREE, integer_type_node,
11538                                                  tree_cons (NULL_TREE,
11539                                                             integer_type_node,
11540                                                             endlink))));
11541   /* Miscellaneous.  */
11542   tree v8qi_ftype_v4hi_v4hi
11543     = build_function_type (V8QI_type_node,
11544                            tree_cons (NULL_TREE, V4HI_type_node,
11545                                       tree_cons (NULL_TREE, V4HI_type_node,
11546                                                  endlink)));
11547   tree v4hi_ftype_v2si_v2si
11548     = build_function_type (V4HI_type_node,
11549                            tree_cons (NULL_TREE, V2SI_type_node,
11550                                       tree_cons (NULL_TREE, V2SI_type_node,
11551                                                  endlink)));
11552   tree v2si_ftype_v4hi_v4hi
11553     = build_function_type (V2SI_type_node,
11554                            tree_cons (NULL_TREE, V4HI_type_node,
11555                                       tree_cons (NULL_TREE, V4HI_type_node,
11556                                                  endlink)));
11557   tree v2si_ftype_v8qi_v8qi
11558     = build_function_type (V2SI_type_node,
11559                            tree_cons (NULL_TREE, V8QI_type_node,
11560                                       tree_cons (NULL_TREE, V8QI_type_node,
11561                                                  endlink)));
11562   tree v4hi_ftype_v4hi_di
11563     = build_function_type (V4HI_type_node,
11564                            tree_cons (NULL_TREE, V4HI_type_node,
11565                                       tree_cons (NULL_TREE,
11566                                                  long_long_integer_type_node,
11567                                                  endlink)));
11568   tree v2si_ftype_v2si_di
11569     = build_function_type (V2SI_type_node,
11570                            tree_cons (NULL_TREE, V2SI_type_node,
11571                                       tree_cons (NULL_TREE,
11572                                                  long_long_integer_type_node,
11573                                                  endlink)));
11574   tree void_ftype_int_int
11575     = build_function_type (void_type_node,
11576                            tree_cons (NULL_TREE, integer_type_node,
11577                                       tree_cons (NULL_TREE, integer_type_node,
11578                                                  endlink)));
11579   tree di_ftype_void
11580     = build_function_type (long_long_unsigned_type_node, endlink);
11581   tree di_ftype_v8qi
11582     = build_function_type (long_long_integer_type_node,
11583                            tree_cons (NULL_TREE, V8QI_type_node,
11584                                       endlink));
11585   tree di_ftype_v4hi
11586     = build_function_type (long_long_integer_type_node,
11587                            tree_cons (NULL_TREE, V4HI_type_node,
11588                                       endlink));
11589   tree di_ftype_v2si
11590     = build_function_type (long_long_integer_type_node,
11591                            tree_cons (NULL_TREE, V2SI_type_node,
11592                                       endlink));
11593   tree v2si_ftype_v4hi
11594     = build_function_type (V2SI_type_node,
11595                            tree_cons (NULL_TREE, V4HI_type_node,
11596                                       endlink));
11597   tree v4hi_ftype_v8qi
11598     = build_function_type (V4HI_type_node,
11599                            tree_cons (NULL_TREE, V8QI_type_node,
11600                                       endlink));
11601
11602   tree di_ftype_di_v4hi_v4hi
11603     = build_function_type (long_long_unsigned_type_node,
11604                            tree_cons (NULL_TREE,
11605                                       long_long_unsigned_type_node,
11606                                       tree_cons (NULL_TREE, V4HI_type_node,
11607                                                  tree_cons (NULL_TREE,
11608                                                             V4HI_type_node,
11609                                                             endlink))));
11610
11611   tree di_ftype_v4hi_v4hi
11612     = build_function_type (long_long_unsigned_type_node,
11613                            tree_cons (NULL_TREE, V4HI_type_node,
11614                                       tree_cons (NULL_TREE, V4HI_type_node,
11615                                                  endlink)));
11616
11617   /* Normal vector binops.  */
11618   tree v8qi_ftype_v8qi_v8qi
11619     = build_function_type (V8QI_type_node,
11620                            tree_cons (NULL_TREE, V8QI_type_node,
11621                                       tree_cons (NULL_TREE, V8QI_type_node,
11622                                                  endlink)));
11623   tree v4hi_ftype_v4hi_v4hi
11624     = build_function_type (V4HI_type_node,
11625                            tree_cons (NULL_TREE, V4HI_type_node,
11626                                       tree_cons (NULL_TREE, V4HI_type_node,
11627                                                  endlink)));
11628   tree v2si_ftype_v2si_v2si
11629     = build_function_type (V2SI_type_node,
11630                            tree_cons (NULL_TREE, V2SI_type_node,
11631                                       tree_cons (NULL_TREE, V2SI_type_node,
11632                                                  endlink)));
11633   tree di_ftype_di_di
11634     = build_function_type (long_long_unsigned_type_node,
11635                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
11636                                       tree_cons (NULL_TREE,
11637                                                  long_long_unsigned_type_node,
11638                                                  endlink)));
11639
11640   /* Add all builtins that are more or less simple operations on two
11641      operands.  */
11642   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11643     {
11644       /* Use one of the operands; the target can have a different mode for
11645          mask-generating compares.  */
11646       enum machine_mode mode;
11647       tree type;
11648
11649       if (d->name == 0)
11650         continue;
11651
11652       mode = insn_data[d->icode].operand[1].mode;
11653
11654       switch (mode)
11655         {
11656         case V8QImode:
11657           type = v8qi_ftype_v8qi_v8qi;
11658           break;
11659         case V4HImode:
11660           type = v4hi_ftype_v4hi_v4hi;
11661           break;
11662         case V2SImode:
11663           type = v2si_ftype_v2si_v2si;
11664           break;
11665         case DImode:
11666           type = di_ftype_di_di;
11667           break;
11668
11669         default:
11670           abort ();
11671         }
11672
11673       def_mbuiltin (d->mask, d->name, type, d->code);
11674     }
11675
11676   /* Add the remaining MMX insns with somewhat more complicated types.  */
11677   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11678   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11679   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11680
11681   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11682   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11683   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11684   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11685   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11686   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11687
11688   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11689   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11690   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11691   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11692   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11693   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11694
11695   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11696   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11697   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11698   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11699   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11700   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11701
11702   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11703   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11704   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11705   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11706   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11707   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11708
11709   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11710
11711   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11712   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11713   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11714   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11715
11716   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11717   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11718   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11719   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11720   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11721   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11722   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11723   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11724   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11725
11726   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11727   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11728   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11729
11730   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11731   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11732   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
11733
11734   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
11735   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
11736   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
11737   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
11738   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
11739   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
11740
11741   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
11742   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
11743   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
11744   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
11745   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
11746   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
11747   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
11748   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
11749   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
11750   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
11751   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
11752   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
11753
11754   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
11755   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
11756   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
11757   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
11758
11759   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
11760   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
11761   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
11762   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
11763   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
11764   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
11765   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
11766 }
11767
11768 static void
11769 arm_init_builtins (void)
11770 {
11771   if (TARGET_REALLY_IWMMXT)
11772     arm_init_iwmmxt_builtins ();
11773 }
11774
11775 /* Errors in the source file can cause expand_expr to return const0_rtx
11776    where we expect a vector.  To avoid crashing, use one of the vector
11777    clear instructions.  */
11778
11779 static rtx
11780 safe_vector_operand (rtx x, enum machine_mode mode)
11781 {
11782   if (x != const0_rtx)
11783     return x;
11784   x = gen_reg_rtx (mode);
11785
11786   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
11787                                : gen_rtx_SUBREG (DImode, x, 0)));
11788   return x;
11789 }
11790
11791 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
11792
11793 static rtx
11794 arm_expand_binop_builtin (enum insn_code icode,
11795                           tree arglist, rtx target)
11796 {
11797   rtx pat;
11798   tree arg0 = TREE_VALUE (arglist);
11799   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11800   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11801   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11802   enum machine_mode tmode = insn_data[icode].operand[0].mode;
11803   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11804   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11805
11806   if (VECTOR_MODE_P (mode0))
11807     op0 = safe_vector_operand (op0, mode0);
11808   if (VECTOR_MODE_P (mode1))
11809     op1 = safe_vector_operand (op1, mode1);
11810
11811   if (! target
11812       || GET_MODE (target) != tmode
11813       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11814     target = gen_reg_rtx (tmode);
11815
11816   /* In case the insn wants input operands in modes different from
11817      the result, abort.  */
11818   if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
11819     abort ();
11820
11821   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11822     op0 = copy_to_mode_reg (mode0, op0);
11823   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11824     op1 = copy_to_mode_reg (mode1, op1);
11825
11826   pat = GEN_FCN (icode) (target, op0, op1);
11827   if (! pat)
11828     return 0;
11829   emit_insn (pat);
11830   return target;
11831 }
11832
11833 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
11834
11835 static rtx
11836 arm_expand_unop_builtin (enum insn_code icode,
11837                          tree arglist, rtx target, int do_load)
11838 {
11839   rtx pat;
11840   tree arg0 = TREE_VALUE (arglist);
11841   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11842   enum machine_mode tmode = insn_data[icode].operand[0].mode;
11843   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11844
11845   if (! target
11846       || GET_MODE (target) != tmode
11847       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11848     target = gen_reg_rtx (tmode);
11849   if (do_load)
11850     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11851   else
11852     {
11853       if (VECTOR_MODE_P (mode0))
11854         op0 = safe_vector_operand (op0, mode0);
11855
11856       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11857         op0 = copy_to_mode_reg (mode0, op0);
11858     }
11859
11860   pat = GEN_FCN (icode) (target, op0);
11861   if (! pat)
11862     return 0;
11863   emit_insn (pat);
11864   return target;
11865 }
11866
11867 /* Expand an expression EXP that calls a built-in function,
11868    with result going to TARGET if that's convenient
11869    (and in mode MODE if that's convenient).
11870    SUBTARGET may be used as the target for computing one of EXP's operands.
11871    IGNORE is nonzero if the value is to be ignored.  */
11872
11873 static rtx
11874 arm_expand_builtin (tree exp,
11875                     rtx target,
11876                     rtx subtarget ATTRIBUTE_UNUSED,
11877                     enum machine_mode mode ATTRIBUTE_UNUSED,
11878                     int ignore ATTRIBUTE_UNUSED)
11879 {
11880   const struct builtin_description * d;
11881   enum insn_code    icode;
11882   tree              fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
11883   tree              arglist = TREE_OPERAND (exp, 1);
11884   tree              arg0;
11885   tree              arg1;
11886   tree              arg2;
11887   rtx               op0;
11888   rtx               op1;
11889   rtx               op2;
11890   rtx               pat;
11891   int               fcode = DECL_FUNCTION_CODE (fndecl);
11892   size_t            i;
11893   enum machine_mode tmode;
11894   enum machine_mode mode0;
11895   enum machine_mode mode1;
11896   enum machine_mode mode2;
11897
11898   switch (fcode)
11899     {
11900     case ARM_BUILTIN_TEXTRMSB:
11901     case ARM_BUILTIN_TEXTRMUB:
11902     case ARM_BUILTIN_TEXTRMSH:
11903     case ARM_BUILTIN_TEXTRMUH:
11904     case ARM_BUILTIN_TEXTRMSW:
11905     case ARM_BUILTIN_TEXTRMUW:
11906       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
11907                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
11908                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
11909                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
11910                : CODE_FOR_iwmmxt_textrmw);
11911
11912       arg0 = TREE_VALUE (arglist);
11913       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11914       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11915       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11916       tmode = insn_data[icode].operand[0].mode;
11917       mode0 = insn_data[icode].operand[1].mode;
11918       mode1 = insn_data[icode].operand[2].mode;
11919
11920       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11921         op0 = copy_to_mode_reg (mode0, op0);
11922       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11923         {
11924           /* @@@ better error message */
11925           error ("selector must be an immediate");
11926           return gen_reg_rtx (tmode);
11927         }
11928       if (target == 0
11929           || GET_MODE (target) != tmode
11930           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11931         target = gen_reg_rtx (tmode);
11932       pat = GEN_FCN (icode) (target, op0, op1);
11933       if (! pat)
11934         return 0;
11935       emit_insn (pat);
11936       return target;
11937
11938     case ARM_BUILTIN_TINSRB:
11939     case ARM_BUILTIN_TINSRH:
11940     case ARM_BUILTIN_TINSRW:
11941       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
11942                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
11943                : CODE_FOR_iwmmxt_tinsrw);
11944       arg0 = TREE_VALUE (arglist);
11945       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11946       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
11947       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11948       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11949       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
11950       tmode = insn_data[icode].operand[0].mode;
11951       mode0 = insn_data[icode].operand[1].mode;
11952       mode1 = insn_data[icode].operand[2].mode;
11953       mode2 = insn_data[icode].operand[3].mode;
11954
11955       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11956         op0 = copy_to_mode_reg (mode0, op0);
11957       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11958         op1 = copy_to_mode_reg (mode1, op1);
11959       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
11960         {
11961           /* @@@ better error message */
11962           error ("selector must be an immediate");
11963           return const0_rtx;
11964         }
11965       if (target == 0
11966           || GET_MODE (target) != tmode
11967           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11968         target = gen_reg_rtx (tmode);
11969       pat = GEN_FCN (icode) (target, op0, op1, op2);
11970       if (! pat)
11971         return 0;
11972       emit_insn (pat);
11973       return target;
11974
11975     case ARM_BUILTIN_SETWCX:
11976       arg0 = TREE_VALUE (arglist);
11977       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11978       op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
11979       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11980       emit_insn (gen_iwmmxt_tmcr (op1, op0));
11981       return 0;
11982
11983     case ARM_BUILTIN_GETWCX:
11984       arg0 = TREE_VALUE (arglist);
11985       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11986       target = gen_reg_rtx (SImode);
11987       emit_insn (gen_iwmmxt_tmrc (target, op0));
11988       return target;
11989
11990     case ARM_BUILTIN_WSHUFH:
11991       icode = CODE_FOR_iwmmxt_wshufh;
11992       arg0 = TREE_VALUE (arglist);
11993       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11994       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11995       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11996       tmode = insn_data[icode].operand[0].mode;
11997       mode1 = insn_data[icode].operand[1].mode;
11998       mode2 = insn_data[icode].operand[2].mode;
11999
12000       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12001         op0 = copy_to_mode_reg (mode1, op0);
12002       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12003         {
12004           /* @@@ better error message */
12005           error ("mask must be an immediate");
12006           return const0_rtx;
12007         }
12008       if (target == 0
12009           || GET_MODE (target) != tmode
12010           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12011         target = gen_reg_rtx (tmode);
12012       pat = GEN_FCN (icode) (target, op0, op1);
12013       if (! pat)
12014         return 0;
12015       emit_insn (pat);
12016       return target;
12017
12018     case ARM_BUILTIN_WSADB:
12019       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12020     case ARM_BUILTIN_WSADH:
12021       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12022     case ARM_BUILTIN_WSADBZ:
12023       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12024     case ARM_BUILTIN_WSADHZ:
12025       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12026
12027       /* Several three-argument builtins.  */
12028     case ARM_BUILTIN_WMACS:
12029     case ARM_BUILTIN_WMACU:
12030     case ARM_BUILTIN_WALIGN:
12031     case ARM_BUILTIN_TMIA:
12032     case ARM_BUILTIN_TMIAPH:
12033     case ARM_BUILTIN_TMIATT:
12034     case ARM_BUILTIN_TMIATB:
12035     case ARM_BUILTIN_TMIABT:
12036     case ARM_BUILTIN_TMIABB:
12037       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12038                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12039                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12040                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12041                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12042                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12043                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12044                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12045                : CODE_FOR_iwmmxt_walign);
12046       arg0 = TREE_VALUE (arglist);
12047       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12048       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12049       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12050       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12051       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12052       tmode = insn_data[icode].operand[0].mode;
12053       mode0 = insn_data[icode].operand[1].mode;
12054       mode1 = insn_data[icode].operand[2].mode;
12055       mode2 = insn_data[icode].operand[3].mode;
12056
12057       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12058         op0 = copy_to_mode_reg (mode0, op0);
12059       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12060         op1 = copy_to_mode_reg (mode1, op1);
12061       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12062         op2 = copy_to_mode_reg (mode2, op2);
12063       if (target == 0
12064           || GET_MODE (target) != tmode
12065           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12066         target = gen_reg_rtx (tmode);
12067       pat = GEN_FCN (icode) (target, op0, op1, op2);
12068       if (! pat)
12069         return 0;
12070       emit_insn (pat);
12071       return target;
12072
12073     case ARM_BUILTIN_WZERO:
12074       target = gen_reg_rtx (DImode);
12075       emit_insn (gen_iwmmxt_clrdi (target));
12076       return target;
12077
12078     default:
12079       break;
12080     }
12081
12082   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12083     if (d->code == (const enum arm_builtins) fcode)
12084       return arm_expand_binop_builtin (d->icode, arglist, target);
12085
12086   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12087     if (d->code == (const enum arm_builtins) fcode)
12088       return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12089
12090   /* @@@ Should really do something sensible here.  */
12091   return NULL_RTX;
12092 }
12093 \f
12094 /* Recursively search through all of the blocks in a function
12095    checking to see if any of the variables created in that
12096    function match the RTX called 'orig'.  If they do then
12097    replace them with the RTX called 'new'.  */
12098 static void
12099 replace_symbols_in_block (tree block, rtx orig, rtx new)
12100 {
12101   for (; block; block = BLOCK_CHAIN (block))
12102     {
12103       tree sym;
12104
12105       if (!TREE_USED (block))
12106         continue;
12107
12108       for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
12109         {
12110           if (  (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
12111               || DECL_IGNORED_P (sym)
12112               || TREE_CODE (sym) != VAR_DECL
12113               || DECL_EXTERNAL (sym)
12114               || !rtx_equal_p (DECL_RTL (sym), orig)
12115               )
12116             continue;
12117
12118           SET_DECL_RTL (sym, new);
12119         }
12120
12121       replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
12122     }
12123 }
12124
12125 /* Return the number (counting from 0) of
12126    the least significant set bit in MASK.  */
12127
12128 inline static int
12129 number_of_first_bit_set (int mask)
12130 {
12131   int bit;
12132
12133   for (bit = 0;
12134        (mask & (1 << bit)) == 0;
12135        ++bit)
12136     continue;
12137
12138   return bit;
12139 }
12140
12141 /* Generate code to return from a thumb function.
12142    If 'reg_containing_return_addr' is -1, then the return address is
12143    actually on the stack, at the stack pointer.  */
12144 static void
12145 thumb_exit (FILE *f, int reg_containing_return_addr)
12146 {
12147   unsigned regs_available_for_popping;
12148   unsigned regs_to_pop;
12149   int pops_needed;
12150   unsigned available;
12151   unsigned required;
12152   int mode;
12153   int size;
12154   int restore_a4 = FALSE;
12155
12156   /* Compute the registers we need to pop.  */
12157   regs_to_pop = 0;
12158   pops_needed = 0;
12159
12160   if (reg_containing_return_addr == -1)
12161     {
12162       regs_to_pop |= 1 << LR_REGNUM;
12163       ++pops_needed;
12164     }
12165
12166   if (TARGET_BACKTRACE)
12167     {
12168       /* Restore the (ARM) frame pointer and stack pointer.  */
12169       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12170       pops_needed += 2;
12171     }
12172
12173   /* If there is nothing to pop then just emit the BX instruction and
12174      return.  */
12175   if (pops_needed == 0)
12176     {
12177       if (current_function_calls_eh_return)
12178         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12179
12180       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12181       return;
12182     }
12183   /* Otherwise if we are not supporting interworking and we have not created
12184      a backtrace structure and the function was not entered in ARM mode then
12185      just pop the return address straight into the PC.  */
12186   else if (!TARGET_INTERWORK
12187            && !TARGET_BACKTRACE
12188            && !is_called_in_ARM_mode (current_function_decl)
12189            && !current_function_calls_eh_return)
12190     {
12191       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12192       return;
12193     }
12194
12195   /* Find out how many of the (return) argument registers we can corrupt.  */
12196   regs_available_for_popping = 0;
12197
12198   /* If returning via __builtin_eh_return, the bottom three registers
12199      all contain information needed for the return.  */
12200   if (current_function_calls_eh_return)
12201     size = 12;
12202   else
12203     {
12204       /* If we can deduce the registers used from the function's
12205          return value.  This is more reliable that examining
12206          regs_ever_live[] because that will be set if the register is
12207          ever used in the function, not just if the register is used
12208          to hold a return value.  */
12209
12210       if (current_function_return_rtx != 0)
12211         mode = GET_MODE (current_function_return_rtx);
12212       else
12213         mode = DECL_MODE (DECL_RESULT (current_function_decl));
12214
12215       size = GET_MODE_SIZE (mode);
12216
12217       if (size == 0)
12218         {
12219           /* In a void function we can use any argument register.
12220              In a function that returns a structure on the stack
12221              we can use the second and third argument registers.  */
12222           if (mode == VOIDmode)
12223             regs_available_for_popping =
12224               (1 << ARG_REGISTER (1))
12225               | (1 << ARG_REGISTER (2))
12226               | (1 << ARG_REGISTER (3));
12227           else
12228             regs_available_for_popping =
12229               (1 << ARG_REGISTER (2))
12230               | (1 << ARG_REGISTER (3));
12231         }
12232       else if (size <= 4)
12233         regs_available_for_popping =
12234           (1 << ARG_REGISTER (2))
12235           | (1 << ARG_REGISTER (3));
12236       else if (size <= 8)
12237         regs_available_for_popping =
12238           (1 << ARG_REGISTER (3));
12239     }
12240
12241   /* Match registers to be popped with registers into which we pop them.  */
12242   for (available = regs_available_for_popping,
12243        required  = regs_to_pop;
12244        required != 0 && available != 0;
12245        available &= ~(available & - available),
12246        required  &= ~(required  & - required))
12247     -- pops_needed;
12248
12249   /* If we have any popping registers left over, remove them.  */
12250   if (available > 0)
12251     regs_available_for_popping &= ~available;
12252
12253   /* Otherwise if we need another popping register we can use
12254      the fourth argument register.  */
12255   else if (pops_needed)
12256     {
12257       /* If we have not found any free argument registers and
12258          reg a4 contains the return address, we must move it.  */
12259       if (regs_available_for_popping == 0
12260           && reg_containing_return_addr == LAST_ARG_REGNUM)
12261         {
12262           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12263           reg_containing_return_addr = LR_REGNUM;
12264         }
12265       else if (size > 12)
12266         {
12267           /* Register a4 is being used to hold part of the return value,
12268              but we have dire need of a free, low register.  */
12269           restore_a4 = TRUE;
12270
12271           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12272         }
12273
12274       if (reg_containing_return_addr != LAST_ARG_REGNUM)
12275         {
12276           /* The fourth argument register is available.  */
12277           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12278
12279           --pops_needed;
12280         }
12281     }
12282
12283   /* Pop as many registers as we can.  */
12284   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12285                  regs_available_for_popping);
12286
12287   /* Process the registers we popped.  */
12288   if (reg_containing_return_addr == -1)
12289     {
12290       /* The return address was popped into the lowest numbered register.  */
12291       regs_to_pop &= ~(1 << LR_REGNUM);
12292
12293       reg_containing_return_addr =
12294         number_of_first_bit_set (regs_available_for_popping);
12295
12296       /* Remove this register for the mask of available registers, so that
12297          the return address will not be corrupted by further pops.  */
12298       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12299     }
12300
12301   /* If we popped other registers then handle them here.  */
12302   if (regs_available_for_popping)
12303     {
12304       int frame_pointer;
12305
12306       /* Work out which register currently contains the frame pointer.  */
12307       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12308
12309       /* Move it into the correct place.  */
12310       asm_fprintf (f, "\tmov\t%r, %r\n",
12311                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12312
12313       /* (Temporarily) remove it from the mask of popped registers.  */
12314       regs_available_for_popping &= ~(1 << frame_pointer);
12315       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12316
12317       if (regs_available_for_popping)
12318         {
12319           int stack_pointer;
12320
12321           /* We popped the stack pointer as well,
12322              find the register that contains it.  */
12323           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12324
12325           /* Move it into the stack register.  */
12326           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12327
12328           /* At this point we have popped all necessary registers, so
12329              do not worry about restoring regs_available_for_popping
12330              to its correct value:
12331
12332              assert (pops_needed == 0)
12333              assert (regs_available_for_popping == (1 << frame_pointer))
12334              assert (regs_to_pop == (1 << STACK_POINTER))  */
12335         }
12336       else
12337         {
12338           /* Since we have just move the popped value into the frame
12339              pointer, the popping register is available for reuse, and
12340              we know that we still have the stack pointer left to pop.  */
12341           regs_available_for_popping |= (1 << frame_pointer);
12342         }
12343     }
12344
12345   /* If we still have registers left on the stack, but we no longer have
12346      any registers into which we can pop them, then we must move the return
12347      address into the link register and make available the register that
12348      contained it.  */
12349   if (regs_available_for_popping == 0 && pops_needed > 0)
12350     {
12351       regs_available_for_popping |= 1 << reg_containing_return_addr;
12352
12353       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12354                    reg_containing_return_addr);
12355
12356       reg_containing_return_addr = LR_REGNUM;
12357     }
12358
12359   /* If we have registers left on the stack then pop some more.
12360      We know that at most we will want to pop FP and SP.  */
12361   if (pops_needed > 0)
12362     {
12363       int  popped_into;
12364       int  move_to;
12365
12366       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12367                      regs_available_for_popping);
12368
12369       /* We have popped either FP or SP.
12370          Move whichever one it is into the correct register.  */
12371       popped_into = number_of_first_bit_set (regs_available_for_popping);
12372       move_to     = number_of_first_bit_set (regs_to_pop);
12373
12374       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12375
12376       regs_to_pop &= ~(1 << move_to);
12377
12378       --pops_needed;
12379     }
12380
12381   /* If we still have not popped everything then we must have only
12382      had one register available to us and we are now popping the SP.  */
12383   if (pops_needed > 0)
12384     {
12385       int  popped_into;
12386
12387       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12388                      regs_available_for_popping);
12389
12390       popped_into = number_of_first_bit_set (regs_available_for_popping);
12391
12392       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12393       /*
12394         assert (regs_to_pop == (1 << STACK_POINTER))
12395         assert (pops_needed == 1)
12396       */
12397     }
12398
12399   /* If necessary restore the a4 register.  */
12400   if (restore_a4)
12401     {
12402       if (reg_containing_return_addr != LR_REGNUM)
12403         {
12404           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12405           reg_containing_return_addr = LR_REGNUM;
12406         }
12407
12408       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12409     }
12410
12411   if (current_function_calls_eh_return)
12412     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12413
12414   /* Return to caller.  */
12415   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12416 }
12417
12418 /* Emit code to push or pop registers to or from the stack.  F is the
12419    assembly file.  MASK is the registers to push or pop.  PUSH is
12420    nonzero if we should push, and zero if we should pop.  For debugging
12421    output, if pushing, adjust CFA_OFFSET by the amount of space added
12422    to the stack.  REAL_REGS should have the same number of bits set as
12423    MASK, and will be used instead (in the same order) to describe which
12424    registers were saved - this is used to mark the save slots when we
12425    push high registers after moving them to low registers.  */
12426 static void
12427 thumb_pushpop (FILE *f, int mask, int push, int *cfa_offset, int real_regs)
12428 {
12429   int regno;
12430   int lo_mask = mask & 0xFF;
12431   int pushed_words = 0;
12432
12433   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12434     {
12435       /* Special case.  Do not generate a POP PC statement here, do it in
12436          thumb_exit() */
12437       thumb_exit (f, -1);
12438       return;
12439     }
12440
12441   fprintf (f, "\t%s\t{", push ? "push" : "pop");
12442
12443   /* Look at the low registers first.  */
12444   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12445     {
12446       if (lo_mask & 1)
12447         {
12448           asm_fprintf (f, "%r", regno);
12449
12450           if ((lo_mask & ~1) != 0)
12451             fprintf (f, ", ");
12452
12453           pushed_words++;
12454         }
12455     }
12456
12457   if (push && (mask & (1 << LR_REGNUM)))
12458     {
12459       /* Catch pushing the LR.  */
12460       if (mask & 0xFF)
12461         fprintf (f, ", ");
12462
12463       asm_fprintf (f, "%r", LR_REGNUM);
12464
12465       pushed_words++;
12466     }
12467   else if (!push && (mask & (1 << PC_REGNUM)))
12468     {
12469       /* Catch popping the PC.  */
12470       if (TARGET_INTERWORK || TARGET_BACKTRACE
12471           || current_function_calls_eh_return)
12472         {
12473           /* The PC is never poped directly, instead
12474              it is popped into r3 and then BX is used.  */
12475           fprintf (f, "}\n");
12476
12477           thumb_exit (f, -1);
12478
12479           return;
12480         }
12481       else
12482         {
12483           if (mask & 0xFF)
12484             fprintf (f, ", ");
12485
12486           asm_fprintf (f, "%r", PC_REGNUM);
12487         }
12488     }
12489
12490   fprintf (f, "}\n");
12491
12492   if (push && pushed_words && dwarf2out_do_frame ())
12493     {
12494       char *l = dwarf2out_cfi_label ();
12495       int pushed_mask = real_regs;
12496
12497       *cfa_offset += pushed_words * 4;
12498       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12499
12500       pushed_words = 0;
12501       pushed_mask = real_regs;
12502       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12503         {
12504           if (pushed_mask & 1)
12505             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12506         }
12507     }
12508 }
12509 \f
12510 void
12511 thumb_final_prescan_insn (rtx insn)
12512 {
12513   if (flag_print_asm_name)
12514     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12515                  INSN_ADDRESSES (INSN_UID (insn)));
12516 }
12517
12518 int
12519 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12520 {
12521   unsigned HOST_WIDE_INT mask = 0xff;
12522   int i;
12523
12524   if (val == 0) /* XXX */
12525     return 0;
12526
12527   for (i = 0; i < 25; i++)
12528     if ((val & (mask << i)) == val)
12529       return 1;
12530
12531   return 0;
12532 }
12533
12534 /* Returns nonzero if the current function contains,
12535    or might contain a far jump.  */
12536 static int
12537 thumb_far_jump_used_p (void)
12538 {
12539   rtx insn;
12540
12541   /* This test is only important for leaf functions.  */
12542   /* assert (!leaf_function_p ()); */
12543
12544   /* If we have already decided that far jumps may be used,
12545      do not bother checking again, and always return true even if
12546      it turns out that they are not being used.  Once we have made
12547      the decision that far jumps are present (and that hence the link
12548      register will be pushed onto the stack) we cannot go back on it.  */
12549   if (cfun->machine->far_jump_used)
12550     return 1;
12551
12552   /* If this function is not being called from the prologue/epilogue
12553      generation code then it must be being called from the
12554      INITIAL_ELIMINATION_OFFSET macro.  */
12555   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12556     {
12557       /* In this case we know that we are being asked about the elimination
12558          of the arg pointer register.  If that register is not being used,
12559          then there are no arguments on the stack, and we do not have to
12560          worry that a far jump might force the prologue to push the link
12561          register, changing the stack offsets.  In this case we can just
12562          return false, since the presence of far jumps in the function will
12563          not affect stack offsets.
12564
12565          If the arg pointer is live (or if it was live, but has now been
12566          eliminated and so set to dead) then we do have to test to see if
12567          the function might contain a far jump.  This test can lead to some
12568          false negatives, since before reload is completed, then length of
12569          branch instructions is not known, so gcc defaults to returning their
12570          longest length, which in turn sets the far jump attribute to true.
12571
12572          A false negative will not result in bad code being generated, but it
12573          will result in a needless push and pop of the link register.  We
12574          hope that this does not occur too often.
12575
12576          If we need doubleword stack alignment this could affect the other
12577          elimination offsets so we can't risk getting it wrong.  */
12578       if (regs_ever_live [ARG_POINTER_REGNUM])
12579         cfun->machine->arg_pointer_live = 1;
12580       else if (!cfun->machine->arg_pointer_live)
12581         return 0;
12582     }
12583
12584   /* Check to see if the function contains a branch
12585      insn with the far jump attribute set.  */
12586   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12587     {
12588       if (GET_CODE (insn) == JUMP_INSN
12589           /* Ignore tablejump patterns.  */
12590           && GET_CODE (PATTERN (insn)) != ADDR_VEC
12591           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12592           && get_attr_far_jump (insn) == FAR_JUMP_YES
12593           )
12594         {
12595           /* Record the fact that we have decided that
12596              the function does use far jumps.  */
12597           cfun->machine->far_jump_used = 1;
12598           return 1;
12599         }
12600     }
12601
12602   return 0;
12603 }
12604
12605 /* Return nonzero if FUNC must be entered in ARM mode.  */
12606 int
12607 is_called_in_ARM_mode (tree func)
12608 {
12609   if (TREE_CODE (func) != FUNCTION_DECL)
12610     abort ();
12611
12612   /* Ignore the problem about functions whoes address is taken.  */
12613   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12614     return TRUE;
12615
12616 #ifdef ARM_PE
12617   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12618 #else
12619   return FALSE;
12620 #endif
12621 }
12622
12623 /* The bits which aren't usefully expanded as rtl.  */
12624 const char *
12625 thumb_unexpanded_epilogue (void)
12626 {
12627   int regno;
12628   int live_regs_mask = 0;
12629   int high_regs_pushed = 0;
12630   int had_to_push_lr;
12631   int size;
12632   int mode;
12633
12634   if (return_used_this_function)
12635     return "";
12636
12637   if (IS_NAKED (arm_current_func_type ()))
12638     return "";
12639
12640   live_regs_mask = thumb_compute_save_reg_mask ();
12641   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
12642
12643   /* If we can deduce the registers used from the function's return value.
12644      This is more reliable that examining regs_ever_live[] because that
12645      will be set if the register is ever used in the function, not just if
12646      the register is used to hold a return value.  */
12647
12648   if (current_function_return_rtx != 0)
12649     mode = GET_MODE (current_function_return_rtx);
12650   else
12651     mode = DECL_MODE (DECL_RESULT (current_function_decl));
12652
12653   size = GET_MODE_SIZE (mode);
12654
12655   /* The prolog may have pushed some high registers to use as
12656      work registers.  eg the testsuite file:
12657      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12658      compiles to produce:
12659         push    {r4, r5, r6, r7, lr}
12660         mov     r7, r9
12661         mov     r6, r8
12662         push    {r6, r7}
12663      as part of the prolog.  We have to undo that pushing here.  */
12664
12665   if (high_regs_pushed)
12666     {
12667       int mask = live_regs_mask & 0xff;
12668       int next_hi_reg;
12669
12670       /* The available low registers depend on the size of the value we are
12671          returning.  */
12672       if (size <= 12)
12673         mask |=  1 << 3;
12674       if (size <= 8)
12675         mask |= 1 << 2;
12676
12677       if (mask == 0)
12678         /* Oh dear!  We have no low registers into which we can pop
12679            high registers!  */
12680         internal_error
12681           ("no low registers available for popping high registers");
12682
12683       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12684         if (live_regs_mask & (1 << next_hi_reg))
12685           break;
12686
12687       while (high_regs_pushed)
12688         {
12689           /* Find lo register(s) into which the high register(s) can
12690              be popped.  */
12691           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12692             {
12693               if (mask & (1 << regno))
12694                 high_regs_pushed--;
12695               if (high_regs_pushed == 0)
12696                 break;
12697             }
12698
12699           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
12700
12701           /* Pop the values into the low register(s).  */
12702           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
12703
12704           /* Move the value(s) into the high registers.  */
12705           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12706             {
12707               if (mask & (1 << regno))
12708                 {
12709                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12710                                regno);
12711
12712                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
12713                     if (live_regs_mask & (1 << next_hi_reg))
12714                       break;
12715                 }
12716             }
12717         }
12718       live_regs_mask &= ~0x0f00;
12719     }
12720
12721   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
12722   live_regs_mask &= 0xff;
12723
12724   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12725     {
12726       /* Pop the return address into the PC.  */
12727       if (had_to_push_lr)
12728         live_regs_mask |= 1 << PC_REGNUM;
12729
12730       /* Either no argument registers were pushed or a backtrace
12731          structure was created which includes an adjusted stack
12732          pointer, so just pop everything.  */
12733       if (live_regs_mask)
12734         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12735                        live_regs_mask);
12736
12737       /* We have either just popped the return address into the
12738          PC or it is was kept in LR for the entire function.  */
12739       if (!had_to_push_lr)
12740         thumb_exit (asm_out_file, LR_REGNUM);
12741     }
12742   else
12743     {
12744       /* Pop everything but the return address.  */
12745       if (live_regs_mask)
12746         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12747                        live_regs_mask);
12748
12749       if (had_to_push_lr)
12750         {
12751           if (size > 12)
12752             {
12753               /* We have no free low regs, so save one.  */
12754               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
12755                            LAST_ARG_REGNUM);
12756             }
12757
12758           /* Get the return address into a temporary register.  */
12759           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
12760                          1 << LAST_ARG_REGNUM);
12761
12762           if (size > 12)
12763             {
12764               /* Move the return address to lr.  */
12765               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
12766                            LAST_ARG_REGNUM);
12767               /* Restore the low register.  */
12768               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
12769                            IP_REGNUM);
12770               regno = LR_REGNUM;
12771             }
12772           else
12773             regno = LAST_ARG_REGNUM;
12774         }
12775       else
12776         regno = LR_REGNUM;
12777
12778       /* Remove the argument registers that were pushed onto the stack.  */
12779       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
12780                    SP_REGNUM, SP_REGNUM,
12781                    current_function_pretend_args_size);
12782
12783       thumb_exit (asm_out_file, regno);
12784     }
12785
12786   return "";
12787 }
12788
12789 /* Functions to save and restore machine-specific function data.  */
12790 static struct machine_function *
12791 arm_init_machine_status (void)
12792 {
12793   struct machine_function *machine;
12794   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
12795
12796 #if ARM_FT_UNKNOWN != 0
12797   machine->func_type = ARM_FT_UNKNOWN;
12798 #endif
12799   return machine;
12800 }
12801
12802 /* Return an RTX indicating where the return address to the
12803    calling function can be found.  */
12804 rtx
12805 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
12806 {
12807   if (count != 0)
12808     return NULL_RTX;
12809
12810   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
12811 }
12812
12813 /* Do anything needed before RTL is emitted for each function.  */
12814 void
12815 arm_init_expanders (void)
12816 {
12817   /* Arrange to initialize and mark the machine per-function status.  */
12818   init_machine_status = arm_init_machine_status;
12819
12820   /* This is to stop the combine pass optimizing away the alignment
12821      adjustment of va_arg.  */
12822   /* ??? It is claimed that this should not be necessary.  */
12823   if (cfun)
12824     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
12825 }
12826
12827
12828 /* Like arm_compute_initial_elimination offset.  Simpler because
12829    THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer.  */
12830
12831 HOST_WIDE_INT
12832 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
12833 {
12834   arm_stack_offsets *offsets;
12835
12836   offsets = arm_get_frame_offsets ();
12837
12838   switch (from)
12839     {
12840     case ARG_POINTER_REGNUM:
12841       switch (to)
12842         {
12843         case STACK_POINTER_REGNUM:
12844           return offsets->outgoing_args - offsets->saved_args;
12845
12846         case FRAME_POINTER_REGNUM:
12847           return offsets->soft_frame - offsets->saved_args;
12848
12849         case THUMB_HARD_FRAME_POINTER_REGNUM:
12850         case ARM_HARD_FRAME_POINTER_REGNUM:
12851           return offsets->saved_regs - offsets->saved_args;
12852
12853         default:
12854           abort();
12855         }
12856       break;
12857
12858     case FRAME_POINTER_REGNUM:
12859       switch (to)
12860         {
12861         case STACK_POINTER_REGNUM:
12862           return offsets->outgoing_args - offsets->soft_frame;
12863
12864         case THUMB_HARD_FRAME_POINTER_REGNUM:
12865         case ARM_HARD_FRAME_POINTER_REGNUM:
12866           return offsets->saved_regs - offsets->soft_frame;
12867
12868         default:
12869           abort();
12870         }
12871       break;
12872
12873     default:
12874       abort ();
12875     }
12876 }
12877
12878
12879 /* Generate the rest of a function's prologue.  */
12880 void
12881 thumb_expand_prologue (void)
12882 {
12883   rtx insn, dwarf;
12884
12885   HOST_WIDE_INT amount;
12886   arm_stack_offsets *offsets;
12887   unsigned long func_type;
12888   int regno;
12889   unsigned long live_regs_mask;
12890
12891   func_type = arm_current_func_type ();
12892
12893   /* Naked functions don't have prologues.  */
12894   if (IS_NAKED (func_type))
12895     return;
12896
12897   if (IS_INTERRUPT (func_type))
12898     {
12899       error ("interrupt Service Routines cannot be coded in Thumb mode");
12900       return;
12901     }
12902
12903   /* Load the pic register before setting the frame pointer, so we can use r7
12904      as a temporary work register.  */
12905   if (flag_pic)
12906     arm_load_pic_register ();
12907
12908   offsets = arm_get_frame_offsets ();
12909
12910   if (frame_pointer_needed)
12911     {
12912       insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
12913                                    stack_pointer_rtx));
12914       RTX_FRAME_RELATED_P (insn) = 1;
12915     }
12916
12917   live_regs_mask = thumb_compute_save_reg_mask ();
12918   amount = offsets->outgoing_args - offsets->saved_regs;
12919   if (amount)
12920     {
12921       if (amount < 512)
12922         {
12923           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
12924                                         GEN_INT (- amount)));
12925           RTX_FRAME_RELATED_P (insn) = 1;
12926         }
12927       else
12928         {
12929           rtx reg;
12930
12931           /* The stack decrement is too big for an immediate value in a single
12932              insn.  In theory we could issue multiple subtracts, but after
12933              three of them it becomes more space efficient to place the full
12934              value in the constant pool and load into a register.  (Also the
12935              ARM debugger really likes to see only one stack decrement per
12936              function).  So instead we look for a scratch register into which
12937              we can load the decrement, and then we subtract this from the
12938              stack pointer.  Unfortunately on the thumb the only available
12939              scratch registers are the argument registers, and we cannot use
12940              these as they may hold arguments to the function.  Instead we
12941              attempt to locate a call preserved register which is used by this
12942              function.  If we can find one, then we know that it will have
12943              been pushed at the start of the prologue and so we can corrupt
12944              it now.  */
12945           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
12946             if (live_regs_mask & (1 << regno)
12947                 && !(frame_pointer_needed
12948                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
12949               break;
12950
12951           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
12952             {
12953               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
12954
12955               /* Choose an arbitrary, non-argument low register.  */
12956               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
12957
12958               /* Save it by copying it into a high, scratch register.  */
12959               emit_insn (gen_movsi (spare, reg));
12960               /* Add a USE to stop propagate_one_insn() from barfing.  */
12961               emit_insn (gen_prologue_use (spare));
12962
12963               /* Decrement the stack.  */
12964               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
12965               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
12966                                             stack_pointer_rtx, reg));
12967               RTX_FRAME_RELATED_P (insn) = 1;
12968               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
12969                                    plus_constant (stack_pointer_rtx,
12970                                                   -amount));
12971               RTX_FRAME_RELATED_P (dwarf) = 1;
12972               REG_NOTES (insn)
12973                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
12974                                      REG_NOTES (insn));
12975
12976               /* Restore the low register's original value.  */
12977               emit_insn (gen_movsi (reg, spare));
12978
12979               /* Emit a USE of the restored scratch register, so that flow
12980                  analysis will not consider the restore redundant.  The
12981                  register won't be used again in this function and isn't
12982                  restored by the epilogue.  */
12983               emit_insn (gen_prologue_use (reg));
12984             }
12985           else
12986             {
12987               reg = gen_rtx_REG (SImode, regno);
12988
12989               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
12990
12991               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
12992                                             stack_pointer_rtx, reg));
12993               RTX_FRAME_RELATED_P (insn) = 1;
12994               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
12995                                    plus_constant (stack_pointer_rtx,
12996                                                   -amount));
12997               RTX_FRAME_RELATED_P (dwarf) = 1;
12998               REG_NOTES (insn)
12999                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13000                                      REG_NOTES (insn));
13001             }
13002         }
13003       /* If the frame pointer is needed, emit a special barrier that
13004          will prevent the scheduler from moving stores to the frame
13005          before the stack adjustment.  */
13006       if (frame_pointer_needed)
13007         emit_insn (gen_stack_tie (stack_pointer_rtx,
13008                                   hard_frame_pointer_rtx));
13009     }
13010
13011   if (current_function_profile || TARGET_NO_SCHED_PRO)
13012     emit_insn (gen_blockage ());
13013
13014   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13015   if (live_regs_mask & 0xff)
13016     cfun->machine->lr_save_eliminated = 0;
13017
13018   /* If the link register is being kept alive, with the return address in it,
13019      then make sure that it does not get reused by the ce2 pass.  */
13020   if (cfun->machine->lr_save_eliminated)
13021     emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13022 }
13023
13024
13025 void
13026 thumb_expand_epilogue (void)
13027 {
13028   HOST_WIDE_INT amount;
13029   arm_stack_offsets *offsets;
13030   int regno;
13031
13032   /* Naked functions don't have prologues.  */
13033   if (IS_NAKED (arm_current_func_type ()))
13034     return;
13035
13036   offsets = arm_get_frame_offsets ();
13037   amount = offsets->outgoing_args - offsets->saved_regs;
13038
13039   if (frame_pointer_needed)
13040     emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13041   else if (amount)
13042     {
13043       if (amount < 512)
13044         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13045                                GEN_INT (amount)));
13046       else
13047         {
13048           /* r3 is always free in the epilogue.  */
13049           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13050
13051           emit_insn (gen_movsi (reg, GEN_INT (amount)));
13052           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13053         }
13054     }
13055
13056   /* Emit a USE (stack_pointer_rtx), so that
13057      the stack adjustment will not be deleted.  */
13058   emit_insn (gen_prologue_use (stack_pointer_rtx));
13059
13060   if (current_function_profile || TARGET_NO_SCHED_PRO)
13061     emit_insn (gen_blockage ());
13062
13063   /* Emit a clobber for each insn that will be restored in the epilogue,
13064      so that flow2 will get register lifetimes correct.  */
13065   for (regno = 0; regno < 13; regno++)
13066     if (regs_ever_live[regno] && !call_used_regs[regno])
13067       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13068
13069   if (! regs_ever_live[LR_REGNUM])
13070     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13071 }
13072
13073 static void
13074 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13075 {
13076   int live_regs_mask = 0;
13077   int l_mask;
13078   int high_regs_pushed = 0;
13079   int cfa_offset = 0;
13080   int regno;
13081
13082   if (IS_NAKED (arm_current_func_type ()))
13083     return;
13084
13085   if (is_called_in_ARM_mode (current_function_decl))
13086     {
13087       const char * name;
13088
13089       if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
13090         abort ();
13091       if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
13092         abort ();
13093       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
13094
13095       /* Generate code sequence to switch us into Thumb mode.  */
13096       /* The .code 32 directive has already been emitted by
13097          ASM_DECLARE_FUNCTION_NAME.  */
13098       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13099       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13100
13101       /* Generate a label, so that the debugger will notice the
13102          change in instruction sets.  This label is also used by
13103          the assembler to bypass the ARM code when this function
13104          is called from a Thumb encoded function elsewhere in the
13105          same file.  Hence the definition of STUB_NAME here must
13106          agree with the definition in gas/config/tc-arm.c.  */
13107
13108 #define STUB_NAME ".real_start_of"
13109
13110       fprintf (f, "\t.code\t16\n");
13111 #ifdef ARM_PE
13112       if (arm_dllexport_name_p (name))
13113         name = arm_strip_name_encoding (name);
13114 #endif
13115       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13116       fprintf (f, "\t.thumb_func\n");
13117       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13118     }
13119
13120   if (current_function_pretend_args_size)
13121     {
13122       if (cfun->machine->uses_anonymous_args)
13123         {
13124           int num_pushes;
13125
13126           fprintf (f, "\tpush\t{");
13127
13128           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13129
13130           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13131                regno <= LAST_ARG_REGNUM;
13132                regno++)
13133             asm_fprintf (f, "%r%s", regno,
13134                          regno == LAST_ARG_REGNUM ? "" : ", ");
13135
13136           fprintf (f, "}\n");
13137         }
13138       else
13139         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13140                      SP_REGNUM, SP_REGNUM,
13141                      current_function_pretend_args_size);
13142
13143       /* We don't need to record the stores for unwinding (would it
13144          help the debugger any if we did?), but record the change in
13145          the stack pointer.  */
13146       if (dwarf2out_do_frame ())
13147         {
13148           char *l = dwarf2out_cfi_label ();
13149           cfa_offset = cfa_offset + current_function_pretend_args_size;
13150           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13151         }
13152     }
13153
13154   live_regs_mask = thumb_compute_save_reg_mask ();
13155   /* Just low regs and lr. */
13156   l_mask = live_regs_mask & 0x40ff;
13157
13158   if (TARGET_BACKTRACE)
13159     {
13160       int    offset;
13161       int    work_register;
13162
13163       /* We have been asked to create a stack backtrace structure.
13164          The code looks like this:
13165
13166          0   .align 2
13167          0   func:
13168          0     sub   SP, #16         Reserve space for 4 registers.
13169          2     push  {R7}            Push low registers.
13170          4     add   R7, SP, #20     Get the stack pointer before the push.
13171          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
13172          8     mov   R7, PC          Get hold of the start of this code plus 12.
13173         10     str   R7, [SP, #16]   Store it.
13174         12     mov   R7, FP          Get hold of the current frame pointer.
13175         14     str   R7, [SP, #4]    Store it.
13176         16     mov   R7, LR          Get hold of the current return address.
13177         18     str   R7, [SP, #12]   Store it.
13178         20     add   R7, SP, #16     Point at the start of the backtrace structure.
13179         22     mov   FP, R7          Put this value into the frame pointer.  */
13180
13181       work_register = thumb_find_work_register (live_regs_mask);
13182
13183       asm_fprintf
13184         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13185          SP_REGNUM, SP_REGNUM);
13186
13187       if (dwarf2out_do_frame ())
13188         {
13189           char *l = dwarf2out_cfi_label ();
13190           cfa_offset = cfa_offset + 16;
13191           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13192         }
13193
13194       if (l_mask)
13195         {
13196           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13197           offset = bit_count (l_mask);
13198         }
13199       else
13200         offset = 0;
13201
13202       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13203                    offset + 16 + current_function_pretend_args_size);
13204
13205       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13206                    offset + 4);
13207
13208       /* Make sure that the instruction fetching the PC is in the right place
13209          to calculate "start of backtrace creation code + 12".  */
13210       if (l_mask)
13211         {
13212           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13213           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13214                        offset + 12);
13215           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13216                        ARM_HARD_FRAME_POINTER_REGNUM);
13217           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13218                        offset);
13219         }
13220       else
13221         {
13222           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13223                        ARM_HARD_FRAME_POINTER_REGNUM);
13224           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13225                        offset);
13226           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13227           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13228                        offset + 12);
13229         }
13230
13231       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13232       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13233                    offset + 8);
13234       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13235                    offset + 12);
13236       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13237                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13238     }
13239   else if (l_mask)
13240     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13241
13242   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13243
13244   if (high_regs_pushed)
13245     {
13246       int pushable_regs = 0;
13247       int next_hi_reg;
13248
13249       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13250         if (live_regs_mask & (1 << next_hi_reg))
13251           break;
13252
13253       pushable_regs = l_mask & 0xff;
13254
13255       if (pushable_regs == 0)
13256         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
13257
13258       while (high_regs_pushed > 0)
13259         {
13260           int real_regs_mask = 0;
13261
13262           for (regno = LAST_LO_REGNUM; regno >= 0; regno--)
13263             {
13264               if (pushable_regs & (1 << regno))
13265                 {
13266                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13267
13268                   high_regs_pushed--;
13269                   real_regs_mask |= (1 << next_hi_reg);
13270
13271                   if (high_regs_pushed)
13272                     {
13273                       for (next_hi_reg--; next_hi_reg > LAST_LO_REGNUM;
13274                            next_hi_reg--)
13275                         if (live_regs_mask & (1 << next_hi_reg))
13276                           break;
13277                     }
13278                   else
13279                     {
13280                       pushable_regs &= ~((1 << regno) - 1);
13281                       break;
13282                     }
13283                 }
13284             }
13285
13286           thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
13287         }
13288     }
13289 }
13290
13291 /* Handle the case of a double word load into a low register from
13292    a computed memory address.  The computed address may involve a
13293    register which is overwritten by the load.  */
13294 const char *
13295 thumb_load_double_from_address (rtx *operands)
13296 {
13297   rtx addr;
13298   rtx base;
13299   rtx offset;
13300   rtx arg1;
13301   rtx arg2;
13302
13303   if (GET_CODE (operands[0]) != REG)
13304     abort ();
13305
13306   if (GET_CODE (operands[1]) != MEM)
13307     abort ();
13308
13309   /* Get the memory address.  */
13310   addr = XEXP (operands[1], 0);
13311
13312   /* Work out how the memory address is computed.  */
13313   switch (GET_CODE (addr))
13314     {
13315     case REG:
13316       operands[2] = gen_rtx_MEM (SImode,
13317                                  plus_constant (XEXP (operands[1], 0), 4));
13318
13319       if (REGNO (operands[0]) == REGNO (addr))
13320         {
13321           output_asm_insn ("ldr\t%H0, %2", operands);
13322           output_asm_insn ("ldr\t%0, %1", operands);
13323         }
13324       else
13325         {
13326           output_asm_insn ("ldr\t%0, %1", operands);
13327           output_asm_insn ("ldr\t%H0, %2", operands);
13328         }
13329       break;
13330
13331     case CONST:
13332       /* Compute <address> + 4 for the high order load.  */
13333       operands[2] = gen_rtx_MEM (SImode,
13334                                  plus_constant (XEXP (operands[1], 0), 4));
13335
13336       output_asm_insn ("ldr\t%0, %1", operands);
13337       output_asm_insn ("ldr\t%H0, %2", operands);
13338       break;
13339
13340     case PLUS:
13341       arg1   = XEXP (addr, 0);
13342       arg2   = XEXP (addr, 1);
13343
13344       if (CONSTANT_P (arg1))
13345         base = arg2, offset = arg1;
13346       else
13347         base = arg1, offset = arg2;
13348
13349       if (GET_CODE (base) != REG)
13350         abort ();
13351
13352       /* Catch the case of <address> = <reg> + <reg> */
13353       if (GET_CODE (offset) == REG)
13354         {
13355           int reg_offset = REGNO (offset);
13356           int reg_base   = REGNO (base);
13357           int reg_dest   = REGNO (operands[0]);
13358
13359           /* Add the base and offset registers together into the
13360              higher destination register.  */
13361           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13362                        reg_dest + 1, reg_base, reg_offset);
13363
13364           /* Load the lower destination register from the address in
13365              the higher destination register.  */
13366           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13367                        reg_dest, reg_dest + 1);
13368
13369           /* Load the higher destination register from its own address
13370              plus 4.  */
13371           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13372                        reg_dest + 1, reg_dest + 1);
13373         }
13374       else
13375         {
13376           /* Compute <address> + 4 for the high order load.  */
13377           operands[2] = gen_rtx_MEM (SImode,
13378                                      plus_constant (XEXP (operands[1], 0), 4));
13379
13380           /* If the computed address is held in the low order register
13381              then load the high order register first, otherwise always
13382              load the low order register first.  */
13383           if (REGNO (operands[0]) == REGNO (base))
13384             {
13385               output_asm_insn ("ldr\t%H0, %2", operands);
13386               output_asm_insn ("ldr\t%0, %1", operands);
13387             }
13388           else
13389             {
13390               output_asm_insn ("ldr\t%0, %1", operands);
13391               output_asm_insn ("ldr\t%H0, %2", operands);
13392             }
13393         }
13394       break;
13395
13396     case LABEL_REF:
13397       /* With no registers to worry about we can just load the value
13398          directly.  */
13399       operands[2] = gen_rtx_MEM (SImode,
13400                                  plus_constant (XEXP (operands[1], 0), 4));
13401
13402       output_asm_insn ("ldr\t%H0, %2", operands);
13403       output_asm_insn ("ldr\t%0, %1", operands);
13404       break;
13405
13406     default:
13407       abort ();
13408       break;
13409     }
13410
13411   return "";
13412 }
13413
13414 const char *
13415 thumb_output_move_mem_multiple (int n, rtx *operands)
13416 {
13417   rtx tmp;
13418
13419   switch (n)
13420     {
13421     case 2:
13422       if (REGNO (operands[4]) > REGNO (operands[5]))
13423         {
13424           tmp = operands[4];
13425           operands[4] = operands[5];
13426           operands[5] = tmp;
13427         }
13428       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13429       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13430       break;
13431
13432     case 3:
13433       if (REGNO (operands[4]) > REGNO (operands[5]))
13434         {
13435           tmp = operands[4];
13436           operands[4] = operands[5];
13437           operands[5] = tmp;
13438         }
13439       if (REGNO (operands[5]) > REGNO (operands[6]))
13440         {
13441           tmp = operands[5];
13442           operands[5] = operands[6];
13443           operands[6] = tmp;
13444         }
13445       if (REGNO (operands[4]) > REGNO (operands[5]))
13446         {
13447           tmp = operands[4];
13448           operands[4] = operands[5];
13449           operands[5] = tmp;
13450         }
13451
13452       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13453       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13454       break;
13455
13456     default:
13457       abort ();
13458     }
13459
13460   return "";
13461 }
13462
13463 /* Routines for generating rtl.  */
13464 void
13465 thumb_expand_movmemqi (rtx *operands)
13466 {
13467   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13468   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13469   HOST_WIDE_INT len = INTVAL (operands[2]);
13470   HOST_WIDE_INT offset = 0;
13471
13472   while (len >= 12)
13473     {
13474       emit_insn (gen_movmem12b (out, in, out, in));
13475       len -= 12;
13476     }
13477
13478   if (len >= 8)
13479     {
13480       emit_insn (gen_movmem8b (out, in, out, in));
13481       len -= 8;
13482     }
13483
13484   if (len >= 4)
13485     {
13486       rtx reg = gen_reg_rtx (SImode);
13487       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13488       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13489       len -= 4;
13490       offset += 4;
13491     }
13492
13493   if (len >= 2)
13494     {
13495       rtx reg = gen_reg_rtx (HImode);
13496       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13497                                               plus_constant (in, offset))));
13498       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13499                             reg));
13500       len -= 2;
13501       offset += 2;
13502     }
13503
13504   if (len)
13505     {
13506       rtx reg = gen_reg_rtx (QImode);
13507       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13508                                               plus_constant (in, offset))));
13509       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13510                             reg));
13511     }
13512 }
13513
13514 void
13515 thumb_reload_out_hi (rtx *operands)
13516 {
13517   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13518 }
13519
13520 /* Handle reading a half-word from memory during reload.  */
13521 void
13522 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13523 {
13524   abort ();
13525 }
13526
13527 /* Return the length of a function name prefix
13528     that starts with the character 'c'.  */
13529 static int
13530 arm_get_strip_length (int c)
13531 {
13532   switch (c)
13533     {
13534     ARM_NAME_ENCODING_LENGTHS
13535       default: return 0;
13536     }
13537 }
13538
13539 /* Return a pointer to a function's name with any
13540    and all prefix encodings stripped from it.  */
13541 const char *
13542 arm_strip_name_encoding (const char *name)
13543 {
13544   int skip;
13545
13546   while ((skip = arm_get_strip_length (* name)))
13547     name += skip;
13548
13549   return name;
13550 }
13551
13552 /* If there is a '*' anywhere in the name's prefix, then
13553    emit the stripped name verbatim, otherwise prepend an
13554    underscore if leading underscores are being used.  */
13555 void
13556 arm_asm_output_labelref (FILE *stream, const char *name)
13557 {
13558   int skip;
13559   int verbatim = 0;
13560
13561   while ((skip = arm_get_strip_length (* name)))
13562     {
13563       verbatim |= (*name == '*');
13564       name += skip;
13565     }
13566
13567   if (verbatim)
13568     fputs (name, stream);
13569   else
13570     asm_fprintf (stream, "%U%s", name);
13571 }
13572
13573 rtx aof_pic_label;
13574
13575 #ifdef AOF_ASSEMBLER
13576 /* Special functions only needed when producing AOF syntax assembler.  */
13577
13578 struct pic_chain
13579 {
13580   struct pic_chain * next;
13581   const char * symname;
13582 };
13583
13584 static struct pic_chain * aof_pic_chain = NULL;
13585
13586 rtx
13587 aof_pic_entry (rtx x)
13588 {
13589   struct pic_chain ** chainp;
13590   int offset;
13591
13592   if (aof_pic_label == NULL_RTX)
13593     {
13594       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13595     }
13596
13597   for (offset = 0, chainp = &aof_pic_chain; *chainp;
13598        offset += 4, chainp = &(*chainp)->next)
13599     if ((*chainp)->symname == XSTR (x, 0))
13600       return plus_constant (aof_pic_label, offset);
13601
13602   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13603   (*chainp)->next = NULL;
13604   (*chainp)->symname = XSTR (x, 0);
13605   return plus_constant (aof_pic_label, offset);
13606 }
13607
13608 void
13609 aof_dump_pic_table (FILE *f)
13610 {
13611   struct pic_chain * chain;
13612
13613   if (aof_pic_chain == NULL)
13614     return;
13615
13616   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13617                PIC_OFFSET_TABLE_REGNUM,
13618                PIC_OFFSET_TABLE_REGNUM);
13619   fputs ("|x$adcons|\n", f);
13620
13621   for (chain = aof_pic_chain; chain; chain = chain->next)
13622     {
13623       fputs ("\tDCD\t", f);
13624       assemble_name (f, chain->symname);
13625       fputs ("\n", f);
13626     }
13627 }
13628
13629 int arm_text_section_count = 1;
13630
13631 char *
13632 aof_text_section (void )
13633 {
13634   static char buf[100];
13635   sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13636            arm_text_section_count++);
13637   if (flag_pic)
13638     strcat (buf, ", PIC, REENTRANT");
13639   return buf;
13640 }
13641
13642 static int arm_data_section_count = 1;
13643
13644 char *
13645 aof_data_section (void)
13646 {
13647   static char buf[100];
13648   sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13649   return buf;
13650 }
13651
13652 /* The AOF assembler is religiously strict about declarations of
13653    imported and exported symbols, so that it is impossible to declare
13654    a function as imported near the beginning of the file, and then to
13655    export it later on.  It is, however, possible to delay the decision
13656    until all the functions in the file have been compiled.  To get
13657    around this, we maintain a list of the imports and exports, and
13658    delete from it any that are subsequently defined.  At the end of
13659    compilation we spit the remainder of the list out before the END
13660    directive.  */
13661
13662 struct import
13663 {
13664   struct import * next;
13665   const char * name;
13666 };
13667
13668 static struct import * imports_list = NULL;
13669
13670 void
13671 aof_add_import (const char *name)
13672 {
13673   struct import * new;
13674
13675   for (new = imports_list; new; new = new->next)
13676     if (new->name == name)
13677       return;
13678
13679   new = (struct import *) xmalloc (sizeof (struct import));
13680   new->next = imports_list;
13681   imports_list = new;
13682   new->name = name;
13683 }
13684
13685 void
13686 aof_delete_import (const char *name)
13687 {
13688   struct import ** old;
13689
13690   for (old = &imports_list; *old; old = & (*old)->next)
13691     {
13692       if ((*old)->name == name)
13693         {
13694           *old = (*old)->next;
13695           return;
13696         }
13697     }
13698 }
13699
13700 int arm_main_function = 0;
13701
13702 static void
13703 aof_dump_imports (FILE *f)
13704 {
13705   /* The AOF assembler needs this to cause the startup code to be extracted
13706      from the library.  Brining in __main causes the whole thing to work
13707      automagically.  */
13708   if (arm_main_function)
13709     {
13710       text_section ();
13711       fputs ("\tIMPORT __main\n", f);
13712       fputs ("\tDCD __main\n", f);
13713     }
13714
13715   /* Now dump the remaining imports.  */
13716   while (imports_list)
13717     {
13718       fprintf (f, "\tIMPORT\t");
13719       assemble_name (f, imports_list->name);
13720       fputc ('\n', f);
13721       imports_list = imports_list->next;
13722     }
13723 }
13724
13725 static void
13726 aof_globalize_label (FILE *stream, const char *name)
13727 {
13728   default_globalize_label (stream, name);
13729   if (! strcmp (name, "main"))
13730     arm_main_function = 1;
13731 }
13732
13733 static void
13734 aof_file_start (void)
13735 {
13736   fputs ("__r0\tRN\t0\n", asm_out_file);
13737   fputs ("__a1\tRN\t0\n", asm_out_file);
13738   fputs ("__a2\tRN\t1\n", asm_out_file);
13739   fputs ("__a3\tRN\t2\n", asm_out_file);
13740   fputs ("__a4\tRN\t3\n", asm_out_file);
13741   fputs ("__v1\tRN\t4\n", asm_out_file);
13742   fputs ("__v2\tRN\t5\n", asm_out_file);
13743   fputs ("__v3\tRN\t6\n", asm_out_file);
13744   fputs ("__v4\tRN\t7\n", asm_out_file);
13745   fputs ("__v5\tRN\t8\n", asm_out_file);
13746   fputs ("__v6\tRN\t9\n", asm_out_file);
13747   fputs ("__sl\tRN\t10\n", asm_out_file);
13748   fputs ("__fp\tRN\t11\n", asm_out_file);
13749   fputs ("__ip\tRN\t12\n", asm_out_file);
13750   fputs ("__sp\tRN\t13\n", asm_out_file);
13751   fputs ("__lr\tRN\t14\n", asm_out_file);
13752   fputs ("__pc\tRN\t15\n", asm_out_file);
13753   fputs ("__f0\tFN\t0\n", asm_out_file);
13754   fputs ("__f1\tFN\t1\n", asm_out_file);
13755   fputs ("__f2\tFN\t2\n", asm_out_file);
13756   fputs ("__f3\tFN\t3\n", asm_out_file);
13757   fputs ("__f4\tFN\t4\n", asm_out_file);
13758   fputs ("__f5\tFN\t5\n", asm_out_file);
13759   fputs ("__f6\tFN\t6\n", asm_out_file);
13760   fputs ("__f7\tFN\t7\n", asm_out_file);
13761   text_section ();
13762 }
13763
13764 static void
13765 aof_file_end (void)
13766 {
13767   if (flag_pic)
13768     aof_dump_pic_table (asm_out_file);
13769   aof_dump_imports (asm_out_file);
13770   fputs ("\tEND\n", asm_out_file);
13771 }
13772 #endif /* AOF_ASSEMBLER */
13773
13774 #ifdef OBJECT_FORMAT_ELF
13775 /* Switch to an arbitrary section NAME with attributes as specified
13776    by FLAGS.  ALIGN specifies any known alignment requirements for
13777    the section; 0 if the default should be used.
13778
13779    Differs from the default elf version only in the prefix character
13780    used before the section type.  */
13781
13782 static void
13783 arm_elf_asm_named_section (const char *name, unsigned int flags)
13784 {
13785   char flagchars[10], *f = flagchars;
13786
13787   if (! named_section_first_declaration (name))
13788     {
13789       fprintf (asm_out_file, "\t.section\t%s\n", name);
13790       return;
13791     }
13792
13793   if (!(flags & SECTION_DEBUG))
13794     *f++ = 'a';
13795   if (flags & SECTION_WRITE)
13796     *f++ = 'w';
13797   if (flags & SECTION_CODE)
13798     *f++ = 'x';
13799   if (flags & SECTION_SMALL)
13800     *f++ = 's';
13801   if (flags & SECTION_MERGE)
13802     *f++ = 'M';
13803   if (flags & SECTION_STRINGS)
13804     *f++ = 'S';
13805   if (flags & SECTION_TLS)
13806     *f++ = 'T';
13807   *f = '\0';
13808
13809   fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
13810
13811   if (!(flags & SECTION_NOTYPE))
13812     {
13813       const char *type;
13814
13815       if (flags & SECTION_BSS)
13816         type = "nobits";
13817       else
13818         type = "progbits";
13819
13820       fprintf (asm_out_file, ",%%%s", type);
13821
13822       if (flags & SECTION_ENTSIZE)
13823         fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
13824     }
13825
13826   putc ('\n', asm_out_file);
13827 }
13828 #endif
13829
13830 #ifndef ARM_PE
13831 /* Symbols in the text segment can be accessed without indirecting via the
13832    constant pool; it may take an extra binary operation, but this is still
13833    faster than indirecting via memory.  Don't do this when not optimizing,
13834    since we won't be calculating al of the offsets necessary to do this
13835    simplification.  */
13836
13837 static void
13838 arm_encode_section_info (tree decl, rtx rtl, int first)
13839 {
13840   /* This doesn't work with AOF syntax, since the string table may be in
13841      a different AREA.  */
13842 #ifndef AOF_ASSEMBLER
13843   if (optimize > 0 && TREE_CONSTANT (decl))
13844     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
13845 #endif
13846
13847   /* If we are referencing a function that is weak then encode a long call
13848      flag in the function name, otherwise if the function is static or
13849      or known to be defined in this file then encode a short call flag.  */
13850   if (first && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
13851     {
13852       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
13853         arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
13854       else if (! TREE_PUBLIC (decl))
13855         arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
13856     }
13857 }
13858 #endif /* !ARM_PE */
13859
13860 static void
13861 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
13862 {
13863   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
13864       && !strcmp (prefix, "L"))
13865     {
13866       arm_ccfsm_state = 0;
13867       arm_target_insn = NULL;
13868     }
13869   default_internal_label (stream, prefix, labelno);
13870 }
13871
13872 /* Output code to add DELTA to the first argument, and then jump
13873    to FUNCTION.  Used for C++ multiple inheritance.  */
13874 static void
13875 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
13876                      HOST_WIDE_INT delta,
13877                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
13878                      tree function)
13879 {
13880   static int thunk_label = 0;
13881   char label[256];
13882   int mi_delta = delta;
13883   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
13884   int shift = 0;
13885   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
13886                     ? 1 : 0);
13887   if (mi_delta < 0)
13888     mi_delta = - mi_delta;
13889   if (TARGET_THUMB)
13890     {
13891       int labelno = thunk_label++;
13892       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
13893       fputs ("\tldr\tr12, ", file);
13894       assemble_name (file, label);
13895       fputc ('\n', file);
13896     }
13897   while (mi_delta != 0)
13898     {
13899       if ((mi_delta & (3 << shift)) == 0)
13900         shift += 2;
13901       else
13902         {
13903           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
13904                        mi_op, this_regno, this_regno,
13905                        mi_delta & (0xff << shift));
13906           mi_delta &= ~(0xff << shift);
13907           shift += 8;
13908         }
13909     }
13910   if (TARGET_THUMB)
13911     {
13912       fprintf (file, "\tbx\tr12\n");
13913       ASM_OUTPUT_ALIGN (file, 2);
13914       assemble_name (file, label);
13915       fputs (":\n", file);
13916       assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
13917     }
13918   else
13919     {
13920       fputs ("\tb\t", file);
13921       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
13922       if (NEED_PLT_RELOC)
13923         fputs ("(PLT)", file);
13924       fputc ('\n', file);
13925     }
13926 }
13927
13928 int
13929 arm_emit_vector_const (FILE *file, rtx x)
13930 {
13931   int i;
13932   const char * pattern;
13933
13934   if (GET_CODE (x) != CONST_VECTOR)
13935     abort ();
13936
13937   switch (GET_MODE (x))
13938     {
13939     case V2SImode: pattern = "%08x"; break;
13940     case V4HImode: pattern = "%04x"; break;
13941     case V8QImode: pattern = "%02x"; break;
13942     default:       abort ();
13943     }
13944
13945   fprintf (file, "0x");
13946   for (i = CONST_VECTOR_NUNITS (x); i--;)
13947     {
13948       rtx element;
13949
13950       element = CONST_VECTOR_ELT (x, i);
13951       fprintf (file, pattern, INTVAL (element));
13952     }
13953
13954   return 1;
13955 }
13956
13957 const char *
13958 arm_output_load_gr (rtx *operands)
13959 {
13960   rtx reg;
13961   rtx offset;
13962   rtx wcgr;
13963   rtx sum;
13964
13965   if (GET_CODE (operands [1]) != MEM
13966       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
13967       || GET_CODE (reg = XEXP (sum, 0)) != REG
13968       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
13969       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
13970     return "wldrw%?\t%0, %1";
13971
13972   /* Fix up an out-of-range load of a GR register.  */
13973   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
13974   wcgr = operands[0];
13975   operands[0] = reg;
13976   output_asm_insn ("ldr%?\t%0, %1", operands);
13977
13978   operands[0] = wcgr;
13979   operands[1] = reg;
13980   output_asm_insn ("tmcr%?\t%0, %1", operands);
13981   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
13982
13983   return "";
13984 }
13985
13986 static rtx
13987 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
13988                       int incoming ATTRIBUTE_UNUSED)
13989 {
13990 #if 0
13991   /* FIXME: The ARM backend has special code to handle structure
13992          returns, and will reserve its own hidden first argument.  So
13993          if this macro is enabled a *second* hidden argument will be
13994          reserved, which will break binary compatibility with old
13995          toolchains and also thunk handling.  One day this should be
13996          fixed.  */
13997   return 0;
13998 #else
13999   /* Register in which address to store a structure value
14000      is passed to a function.  */
14001   return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14002 #endif
14003 }
14004
14005 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14006
14007    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14008    named arg and all anonymous args onto the stack.
14009    XXX I know the prologue shouldn't be pushing registers, but it is faster
14010    that way.  */
14011
14012 static void
14013 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14014                             enum machine_mode mode ATTRIBUTE_UNUSED,
14015                             tree type ATTRIBUTE_UNUSED,
14016                             int *pretend_size,
14017                             int second_time ATTRIBUTE_UNUSED)
14018 {
14019   cfun->machine->uses_anonymous_args = 1;
14020   if (cum->nregs < NUM_ARG_REGS)
14021     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14022 }
14023
14024 /* Return nonzero if the CONSUMER instruction (a store) does not need
14025    PRODUCER's value to calculate the address.  */
14026
14027 int
14028 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14029 {
14030   rtx value = PATTERN (producer);
14031   rtx addr = PATTERN (consumer);
14032
14033   if (GET_CODE (value) == COND_EXEC)
14034     value = COND_EXEC_CODE (value);
14035   if (GET_CODE (value) == PARALLEL)
14036     value = XVECEXP (value, 0, 0);
14037   value = XEXP (value, 0);
14038   if (GET_CODE (addr) == COND_EXEC)
14039     addr = COND_EXEC_CODE (addr);
14040   if (GET_CODE (addr) == PARALLEL)
14041     addr = XVECEXP (addr, 0, 0);
14042   addr = XEXP (addr, 0);
14043
14044   return !reg_overlap_mentioned_p (value, addr);
14045 }
14046
14047 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14048    have an early register shift value or amount dependency on the
14049    result of PRODUCER.  */
14050
14051 int
14052 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14053 {
14054   rtx value = PATTERN (producer);
14055   rtx op = PATTERN (consumer);
14056   rtx early_op;
14057
14058   if (GET_CODE (value) == COND_EXEC)
14059     value = COND_EXEC_CODE (value);
14060   if (GET_CODE (value) == PARALLEL)
14061     value = XVECEXP (value, 0, 0);
14062   value = XEXP (value, 0);
14063   if (GET_CODE (op) == COND_EXEC)
14064     op = COND_EXEC_CODE (op);
14065   if (GET_CODE (op) == PARALLEL)
14066     op = XVECEXP (op, 0, 0);
14067   op = XEXP (op, 1);
14068
14069   early_op = XEXP (op, 0);
14070   /* This is either an actual independent shift, or a shift applied to
14071      the first operand of another operation.  We want the whole shift
14072      operation.  */
14073   if (GET_CODE (early_op) == REG)
14074     early_op = op;
14075
14076   return !reg_overlap_mentioned_p (value, early_op);
14077 }
14078
14079 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14080    have an early register shift value dependency on the result of
14081    PRODUCER.  */
14082
14083 int
14084 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14085 {
14086   rtx value = PATTERN (producer);
14087   rtx op = PATTERN (consumer);
14088   rtx early_op;
14089
14090   if (GET_CODE (value) == COND_EXEC)
14091     value = COND_EXEC_CODE (value);
14092   if (GET_CODE (value) == PARALLEL)
14093     value = XVECEXP (value, 0, 0);
14094   value = XEXP (value, 0);
14095   if (GET_CODE (op) == COND_EXEC)
14096     op = COND_EXEC_CODE (op);
14097   if (GET_CODE (op) == PARALLEL)
14098     op = XVECEXP (op, 0, 0);
14099   op = XEXP (op, 1);
14100
14101   early_op = XEXP (op, 0);
14102
14103   /* This is either an actual independent shift, or a shift applied to
14104      the first operand of another operation.  We want the value being
14105      shifted, in either case.  */
14106   if (GET_CODE (early_op) != REG)
14107     early_op = XEXP (early_op, 0);
14108
14109   return !reg_overlap_mentioned_p (value, early_op);
14110 }
14111
14112 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14113    have an early register mult dependency on the result of
14114    PRODUCER.  */
14115
14116 int
14117 arm_no_early_mul_dep (rtx producer, rtx consumer)
14118 {
14119   rtx value = PATTERN (producer);
14120   rtx op = PATTERN (consumer);
14121
14122   if (GET_CODE (value) == COND_EXEC)
14123     value = COND_EXEC_CODE (value);
14124   if (GET_CODE (value) == PARALLEL)
14125     value = XVECEXP (value, 0, 0);
14126   value = XEXP (value, 0);
14127   if (GET_CODE (op) == COND_EXEC)
14128     op = COND_EXEC_CODE (op);
14129   if (GET_CODE (op) == PARALLEL)
14130     op = XVECEXP (op, 0, 0);
14131   op = XEXP (op, 1);
14132
14133   return (GET_CODE (op) == PLUS
14134           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14135 }
14136
14137
14138 /* We can't rely on the caller doing the proper promotion when
14139    using APCS or ATPCS.  */
14140
14141 static bool
14142 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14143 {
14144     return !TARGET_AAPCS_BASED;
14145 }
14146
14147
14148 /* AAPCS based ABIs use short enums by default.  */
14149
14150 static bool
14151 arm_default_short_enums (void)
14152 {
14153   return TARGET_AAPCS_BASED;
14154 }
14155
14156
14157 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
14158
14159 static bool
14160 arm_align_anon_bitfield (void)
14161 {
14162   return TARGET_AAPCS_BASED;
14163 }
14164
14165
14166 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
14167
14168 static tree
14169 arm_cxx_guard_type (void)
14170 {
14171   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14172 }
14173
14174
14175 /* The EABI says test the least significan bit of a guard variable.  */
14176
14177 static bool
14178 arm_cxx_guard_mask_bit (void)
14179 {
14180   return TARGET_AAPCS_BASED;
14181 }
14182
14183
14184 /* The EABI specifies that all array cookies are 8 bytes long.  */
14185
14186 static tree
14187 arm_get_cookie_size (tree type)
14188 {
14189   tree size;
14190
14191   if (!TARGET_AAPCS_BASED)
14192     return default_cxx_get_cookie_size (type);
14193
14194   size = build_int_cst (sizetype, 8);
14195   return size;
14196 }
14197
14198
14199 /* The EABI says that array cookies should also contain the element size.  */
14200
14201 static bool
14202 arm_cookie_has_size (void)
14203 {
14204   return TARGET_AAPCS_BASED;
14205 }
14206
14207
14208 /* The EABI says constructors and destructors should return a pointer to
14209    the object constructed/destroyed.  */
14210
14211 static bool
14212 arm_cxx_cdtor_returns_this (void)
14213 {
14214   return TARGET_AAPCS_BASED;
14215 }
14216
14217 /* The EABI says that an inline function may never be the key
14218    method.  */
14219
14220 static bool
14221 arm_cxx_key_method_may_be_inline (void)
14222 {
14223   return !TARGET_AAPCS_BASED;
14224 }
14225
14226 /* The EABI says that the virtual table, etc., for a class must be
14227    exported if it has a key method.  The EABI does not specific the
14228    behavior if there is no key method, but there is no harm in
14229    exporting the class data in that case too.  */
14230
14231 static bool
14232 arm_cxx_export_class_data (void)
14233 {
14234   return TARGET_AAPCS_BASED;
14235 }
14236
14237 void
14238 arm_set_return_address (rtx source, rtx scratch)
14239 {
14240   arm_stack_offsets *offsets;
14241   HOST_WIDE_INT delta;
14242   rtx addr;
14243   unsigned long saved_regs;
14244
14245   saved_regs = arm_compute_save_reg_mask ();
14246
14247   if ((saved_regs & (1 << LR_REGNUM)) == 0)
14248     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14249   else
14250     {
14251       if (frame_pointer_needed)
14252         addr = plus_constant(hard_frame_pointer_rtx, -4);
14253       else
14254         {
14255           /* LR will be the first saved register.  */
14256           offsets = arm_get_frame_offsets ();
14257           delta = offsets->outgoing_args - (offsets->frame + 4);
14258
14259
14260           if (delta >= 4096)
14261             {
14262               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
14263                                      GEN_INT (delta & ~4095)));
14264               addr = scratch;
14265               delta &= 4095;
14266             }
14267           else
14268             addr = stack_pointer_rtx;
14269
14270           addr = plus_constant (addr, delta);
14271         }
14272       emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14273     }
14274 }
14275
14276
14277 void
14278 thumb_set_return_address (rtx source, rtx scratch)
14279 {
14280   arm_stack_offsets *offsets;
14281   HOST_WIDE_INT delta;
14282   int reg;
14283   rtx addr;
14284   unsigned long mask;
14285
14286   emit_insn (gen_rtx_USE (VOIDmode, source));
14287
14288   mask = thumb_compute_save_reg_mask ();
14289   if (mask & (1 << LR_REGNUM))
14290     {
14291       offsets = arm_get_frame_offsets ();
14292
14293       /* Find the saved regs.  */
14294       if (frame_pointer_needed)
14295         {
14296           delta = offsets->soft_frame - offsets->saved_args;
14297           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
14298         }
14299       else
14300         {
14301           delta = offsets->outgoing_args - offsets->saved_args;
14302           reg = SP_REGNUM;
14303         }
14304       /* Allow for the stack frame.  */
14305       if (TARGET_BACKTRACE)
14306         delta -= 16;
14307       /* The link register is always the first saved register.  */
14308       delta -= 4;
14309
14310       /* Construct the address.  */
14311       addr = gen_rtx_REG (SImode, reg);
14312       if ((reg != SP_REGNUM && delta >= 128)
14313           || delta >= 1024)
14314         {
14315           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
14316           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
14317           addr = scratch;
14318         }
14319       else
14320         addr = plus_constant (addr, delta);
14321
14322       emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14323     }
14324   else
14325     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14326 }
14327
14328 /* Implements target hook vector_mode_supported_p.  */
14329 bool
14330 arm_vector_mode_supported_p (enum machine_mode mode)
14331 {
14332   if ((mode == V2SImode)
14333       || (mode == V4HImode)
14334       || (mode == V8QImode))
14335     return true;
14336
14337   return false;
14338 }
14339
14340 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
14341    ARM insns and therefore guarantee that the shift count is modulo 256.
14342    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
14343    guarantee no particular behavior for out-of-range counts.  */
14344
14345 static unsigned HOST_WIDE_INT
14346 arm_shift_truncation_mask (enum machine_mode mode)
14347 {
14348   return mode == SImode ? 255 : 0;
14349 }