OSDN Git Service

PR target/21397
[pf3gnuchains/gcc-fork.git] / gcc / config / arm / arm.c
1 /* Output routines for GCC for ARM.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005  Free Software Foundation, Inc.
4    Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5    and Martin Simmons (@harleqn.co.uk).
6    More major hacks by Richard Earnshaw (rearnsha@arm.com).
7
8    This file is part of GCC.
9
10    GCC is free software; you can redistribute it and/or modify it
11    under the terms of the GNU General Public License as published
12    by the Free Software Foundation; either version 2, or (at your
13    option) any later version.
14
15    GCC is distributed in the hope that it will be useful, but WITHOUT
16    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18    License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with GCC; see the file COPYING.  If not, write to
22    the Free Software Foundation, 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, USA.  */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "obstack.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "output.h"
38 #include "insn-attr.h"
39 #include "flags.h"
40 #include "reload.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "optabs.h"
44 #include "toplev.h"
45 #include "recog.h"
46 #include "ggc.h"
47 #include "except.h"
48 #include "c-pragma.h"
49 #include "integrate.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "debug.h"
54 #include "langhooks.h"
55
56 /* Forward definitions of types.  */
57 typedef struct minipool_node    Mnode;
58 typedef struct minipool_fixup   Mfix;
59
60 const struct attribute_spec arm_attribute_table[];
61
62 /* Forward function declarations.  */
63 static arm_stack_offsets *arm_get_frame_offsets (void);
64 static void arm_add_gc_roots (void);
65 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
66                              HOST_WIDE_INT, rtx, rtx, int, int);
67 static unsigned bit_count (unsigned long);
68 static int arm_address_register_rtx_p (rtx, int);
69 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
70 static int thumb_base_register_rtx_p (rtx, enum machine_mode, int);
71 inline static int thumb_index_register_rtx_p (rtx, int);
72 static int thumb_far_jump_used_p (void);
73 static bool thumb_force_lr_save (void);
74 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
75 static rtx emit_sfm (int, int);
76 #ifndef AOF_ASSEMBLER
77 static bool arm_assemble_integer (rtx, unsigned int, int);
78 #endif
79 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
80 static arm_cc get_arm_condition_code (rtx);
81 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
82 static rtx is_jump_table (rtx);
83 static const char *output_multi_immediate (rtx *, const char *, const char *,
84                                            int, HOST_WIDE_INT);
85 static const char *shift_op (rtx, HOST_WIDE_INT *);
86 static struct machine_function *arm_init_machine_status (void);
87 static void thumb_exit (FILE *, int);
88 static rtx is_jump_table (rtx);
89 static HOST_WIDE_INT get_jump_table_size (rtx);
90 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
91 static Mnode *add_minipool_forward_ref (Mfix *);
92 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
93 static Mnode *add_minipool_backward_ref (Mfix *);
94 static void assign_minipool_offsets (Mfix *);
95 static void arm_print_value (FILE *, rtx);
96 static void dump_minipool (rtx);
97 static int arm_barrier_cost (rtx);
98 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
99 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
100 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
101                                rtx);
102 static void arm_reorg (void);
103 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
104 static int current_file_function_operand (rtx);
105 static unsigned long arm_compute_save_reg0_reg12_mask (void);
106 static unsigned long arm_compute_save_reg_mask (void);
107 static unsigned long arm_isr_value (tree);
108 static unsigned long arm_compute_func_type (void);
109 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
110 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
111 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
112 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
113 #endif
114 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
115 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
116 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
117 static int arm_comp_type_attributes (tree, tree);
118 static void arm_set_default_type_attributes (tree);
119 static int arm_adjust_cost (rtx, rtx, rtx, int);
120 static int count_insns_for_constant (HOST_WIDE_INT, int);
121 static int arm_get_strip_length (int);
122 static bool arm_function_ok_for_sibcall (tree, tree);
123 static void arm_internal_label (FILE *, const char *, unsigned long);
124 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
125                                  tree);
126 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
127 static bool arm_size_rtx_costs (rtx, int, int, int *);
128 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
129 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
130 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
131 static bool arm_9e_rtx_costs (rtx, int, int, int *);
132 static int arm_address_cost (rtx);
133 static bool arm_memory_load_p (rtx);
134 static bool arm_cirrus_insn_p (rtx);
135 static void cirrus_reorg (rtx);
136 static void arm_init_builtins (void);
137 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
138 static void arm_init_iwmmxt_builtins (void);
139 static rtx safe_vector_operand (rtx, enum machine_mode);
140 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
141 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
142 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
143 static void emit_constant_insn (rtx cond, rtx pattern);
144 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
145                                   tree, bool);
146
147 #ifdef OBJECT_FORMAT_ELF
148 static void arm_elf_asm_constructor (rtx, int);
149 #endif
150 #ifndef ARM_PE
151 static void arm_encode_section_info (tree, rtx, int);
152 #endif
153
154 static void arm_file_end (void);
155
156 #ifdef AOF_ASSEMBLER
157 static void aof_globalize_label (FILE *, const char *);
158 static void aof_dump_imports (FILE *);
159 static void aof_dump_pic_table (FILE *);
160 static void aof_file_start (void);
161 static void aof_file_end (void);
162 #endif
163 static rtx arm_struct_value_rtx (tree, int);
164 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
165                                         tree, int *, int);
166 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
167                                    enum machine_mode, tree, bool);
168 static bool arm_promote_prototypes (tree);
169 static bool arm_default_short_enums (void);
170 static bool arm_align_anon_bitfield (void);
171 static bool arm_return_in_msb (tree);
172 static bool arm_must_pass_in_stack (enum machine_mode, tree);
173
174 static tree arm_cxx_guard_type (void);
175 static bool arm_cxx_guard_mask_bit (void);
176 static tree arm_get_cookie_size (tree);
177 static bool arm_cookie_has_size (void);
178 static bool arm_cxx_cdtor_returns_this (void);
179 static bool arm_cxx_key_method_may_be_inline (void);
180 static void arm_cxx_determine_class_data_visibility (tree);
181 static bool arm_cxx_class_data_always_comdat (void);
182 static bool arm_cxx_use_aeabi_atexit (void);
183 static void arm_init_libfuncs (void);
184 static bool arm_handle_option (size_t, const char *, int);
185 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
186 \f
187 /* Initialize the GCC target structure.  */
188 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
189 #undef  TARGET_MERGE_DECL_ATTRIBUTES
190 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
191 #endif
192
193 #undef  TARGET_ATTRIBUTE_TABLE
194 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
195
196 #undef TARGET_ASM_FILE_END
197 #define TARGET_ASM_FILE_END arm_file_end
198
199 #ifdef AOF_ASSEMBLER
200 #undef  TARGET_ASM_BYTE_OP
201 #define TARGET_ASM_BYTE_OP "\tDCB\t"
202 #undef  TARGET_ASM_ALIGNED_HI_OP
203 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
204 #undef  TARGET_ASM_ALIGNED_SI_OP
205 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
206 #undef TARGET_ASM_GLOBALIZE_LABEL
207 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
208 #undef TARGET_ASM_FILE_START
209 #define TARGET_ASM_FILE_START aof_file_start
210 #undef TARGET_ASM_FILE_END
211 #define TARGET_ASM_FILE_END aof_file_end
212 #else
213 #undef  TARGET_ASM_ALIGNED_SI_OP
214 #define TARGET_ASM_ALIGNED_SI_OP NULL
215 #undef  TARGET_ASM_INTEGER
216 #define TARGET_ASM_INTEGER arm_assemble_integer
217 #endif
218
219 #undef  TARGET_ASM_FUNCTION_PROLOGUE
220 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
221
222 #undef  TARGET_ASM_FUNCTION_EPILOGUE
223 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
224
225 #undef  TARGET_DEFAULT_TARGET_FLAGS
226 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
227 #undef  TARGET_HANDLE_OPTION
228 #define TARGET_HANDLE_OPTION arm_handle_option
229
230 #undef  TARGET_COMP_TYPE_ATTRIBUTES
231 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
232
233 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
234 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
235
236 #undef  TARGET_SCHED_ADJUST_COST
237 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
238
239 #undef TARGET_ENCODE_SECTION_INFO
240 #ifdef ARM_PE
241 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
242 #else
243 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
244 #endif
245
246 #undef  TARGET_STRIP_NAME_ENCODING
247 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
248
249 #undef  TARGET_ASM_INTERNAL_LABEL
250 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
251
252 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
253 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
254
255 #undef  TARGET_ASM_OUTPUT_MI_THUNK
256 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
257 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
258 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
259
260 /* This will be overridden in arm_override_options.  */
261 #undef  TARGET_RTX_COSTS
262 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
263 #undef  TARGET_ADDRESS_COST
264 #define TARGET_ADDRESS_COST arm_address_cost
265
266 #undef TARGET_SHIFT_TRUNCATION_MASK
267 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
268 #undef TARGET_VECTOR_MODE_SUPPORTED_P
269 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
270
271 #undef  TARGET_MACHINE_DEPENDENT_REORG
272 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
273
274 #undef  TARGET_INIT_BUILTINS
275 #define TARGET_INIT_BUILTINS  arm_init_builtins
276 #undef  TARGET_EXPAND_BUILTIN
277 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
278
279 #undef TARGET_INIT_LIBFUNCS
280 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
281
282 #undef TARGET_PROMOTE_FUNCTION_ARGS
283 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
284 #undef TARGET_PROMOTE_FUNCTION_RETURN
285 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
286 #undef TARGET_PROMOTE_PROTOTYPES
287 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
288 #undef TARGET_PASS_BY_REFERENCE
289 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
290 #undef TARGET_ARG_PARTIAL_BYTES
291 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
292
293 #undef TARGET_STRUCT_VALUE_RTX
294 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
295
296 #undef  TARGET_SETUP_INCOMING_VARARGS
297 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
298
299 #undef TARGET_DEFAULT_SHORT_ENUMS
300 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
301
302 #undef TARGET_ALIGN_ANON_BITFIELD
303 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
304
305 #undef TARGET_CXX_GUARD_TYPE
306 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
307
308 #undef TARGET_CXX_GUARD_MASK_BIT
309 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
310
311 #undef TARGET_CXX_GET_COOKIE_SIZE
312 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
313
314 #undef TARGET_CXX_COOKIE_HAS_SIZE
315 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
316
317 #undef TARGET_CXX_CDTOR_RETURNS_THIS
318 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
319
320 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
321 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
322
323 #undef TARGET_CXX_USE_AEABI_ATEXIT
324 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
325
326 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
327 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
328   arm_cxx_determine_class_data_visibility
329
330 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
331 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
332
333 #undef TARGET_RETURN_IN_MSB
334 #define TARGET_RETURN_IN_MSB arm_return_in_msb
335
336 #undef TARGET_MUST_PASS_IN_STACK
337 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
338
339 struct gcc_target targetm = TARGET_INITIALIZER;
340 \f
341 /* Obstack for minipool constant handling.  */
342 static struct obstack minipool_obstack;
343 static char *         minipool_startobj;
344
345 /* The maximum number of insns skipped which
346    will be conditionalised if possible.  */
347 static int max_insns_skipped = 5;
348
349 extern FILE * asm_out_file;
350
351 /* True if we are currently building a constant table.  */
352 int making_const_table;
353
354 /* Define the information needed to generate branch insns.  This is
355    stored from the compare operation.  */
356 rtx arm_compare_op0, arm_compare_op1;
357
358 /* The processor for which instructions should be scheduled.  */
359 enum processor_type arm_tune = arm_none;
360
361 /* Which floating point model to use.  */
362 enum arm_fp_model arm_fp_model;
363
364 /* Which floating point hardware is available.  */
365 enum fputype arm_fpu_arch;
366
367 /* Which floating point hardware to schedule for.  */
368 enum fputype arm_fpu_tune;
369
370 /* Whether to use floating point hardware.  */
371 enum float_abi_type arm_float_abi;
372
373 /* Which ABI to use.  */
374 enum arm_abi_type arm_abi;
375
376 /* Set by the -mfpu=... option.  */
377 static const char * target_fpu_name = NULL;
378
379 /* Set by the -mfpe=... option.  */
380 static const char * target_fpe_name = NULL;
381
382 /* Set by the -mfloat-abi=... option.  */
383 static const char * target_float_abi_name = NULL;
384
385 /* Set by the -mabi=... option.  */
386 static const char * target_abi_name = NULL;
387
388 /* Used to parse -mstructure_size_boundary command line option.  */
389 static const char * structure_size_string = NULL;
390 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
391
392 /* Used for Thumb call_via trampolines.  */
393 rtx thumb_call_via_label[14];
394 static int thumb_call_reg_needed;
395
396 /* Bit values used to identify processor capabilities.  */
397 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
398 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
399 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
400 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
401 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
402 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
403 #define FL_THUMB      (1 << 6)        /* Thumb aware */
404 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
405 #define FL_STRONG     (1 << 8)        /* StrongARM */
406 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
407 #define FL_XSCALE     (1 << 10)       /* XScale */
408 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
409 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
410                                          media instructions.  */
411 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
412 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
413                                          Note: ARM6 & 7 derivatives only.  */
414
415 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
416
417 #define FL_FOR_ARCH2    0
418 #define FL_FOR_ARCH3    FL_MODE32
419 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
420 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
421 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
422 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
423 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
424 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
425 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
426 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
427 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
428 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
429 #define FL_FOR_ARCH6K   FL_FOR_ARCH6
430 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
431 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6
432
433 /* The bits in this mask specify which
434    instructions we are allowed to generate.  */
435 static unsigned long insn_flags = 0;
436
437 /* The bits in this mask specify which instruction scheduling options should
438    be used.  */
439 static unsigned long tune_flags = 0;
440
441 /* The following are used in the arm.md file as equivalents to bits
442    in the above two flag variables.  */
443
444 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
445 int arm_arch3m = 0;
446
447 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
448 int arm_arch4 = 0;
449
450 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
451 int arm_arch4t = 0;
452
453 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
454 int arm_arch5 = 0;
455
456 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
457 int arm_arch5e = 0;
458
459 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
460 int arm_arch6 = 0;
461
462 /* Nonzero if this chip can benefit from load scheduling.  */
463 int arm_ld_sched = 0;
464
465 /* Nonzero if this chip is a StrongARM.  */
466 int arm_tune_strongarm = 0;
467
468 /* Nonzero if this chip is a Cirrus variant.  */
469 int arm_arch_cirrus = 0;
470
471 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
472 int arm_arch_iwmmxt = 0;
473
474 /* Nonzero if this chip is an XScale.  */
475 int arm_arch_xscale = 0;
476
477 /* Nonzero if tuning for XScale  */
478 int arm_tune_xscale = 0;
479
480 /* Nonzero if we want to tune for stores that access the write-buffer. 
481    This typically means an ARM6 or ARM7 with MMU or MPU.  */
482 int arm_tune_wbuf = 0;
483
484 /* Nonzero if generating Thumb instructions.  */
485 int thumb_code = 0;
486
487 /* Nonzero if we should define __THUMB_INTERWORK__ in the
488    preprocessor.
489    XXX This is a bit of a hack, it's intended to help work around
490    problems in GLD which doesn't understand that armv5t code is
491    interworking clean.  */
492 int arm_cpp_interwork = 0;
493
494 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
495    must report the mode of the memory reference from PRINT_OPERAND to
496    PRINT_OPERAND_ADDRESS.  */
497 enum machine_mode output_memory_reference_mode;
498
499 /* The register number to be used for the PIC offset register.  */
500 static const char * arm_pic_register_string = NULL;
501 int arm_pic_register = INVALID_REGNUM;
502
503 /* Set to 1 when a return insn is output, this means that the epilogue
504    is not needed.  */
505 int return_used_this_function;
506
507 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
508    the next function.  */
509 static int after_arm_reorg = 0;
510
511 /* The maximum number of insns to be used when loading a constant.  */
512 static int arm_constant_limit = 3;
513
514 /* For an explanation of these variables, see final_prescan_insn below.  */
515 int arm_ccfsm_state;
516 enum arm_cond_code arm_current_cc;
517 rtx arm_target_insn;
518 int arm_target_label;
519
520 /* The condition codes of the ARM, and the inverse function.  */
521 static const char * const arm_condition_codes[] =
522 {
523   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
524   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
525 };
526
527 #define streq(string1, string2) (strcmp (string1, string2) == 0)
528 \f
529 /* Initialization code.  */
530
531 struct processors
532 {
533   const char *const name;
534   enum processor_type core;
535   const char *arch;
536   const unsigned long flags;
537   bool (* rtx_costs) (rtx, int, int, int *);
538 };
539
540 /* Not all of these give usefully different compilation alternatives,
541    but there is no simple way of generalizing them.  */
542 static const struct processors all_cores[] =
543 {
544   /* ARM Cores */
545 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
546   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
547 #include "arm-cores.def"
548 #undef ARM_CORE
549   {NULL, arm_none, NULL, 0, NULL}
550 };
551
552 static const struct processors all_architectures[] =
553 {
554   /* ARM Architectures */
555   /* We don't specify rtx_costs here as it will be figured out
556      from the core.  */
557
558   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
559   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
560   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
561   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
562   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
563   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
564      implementations that support it, so we will leave it out for now.  */
565   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
566   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
567   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
568   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
569   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
570   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
571   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
572   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
573   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
574   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
575   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
576   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
577   {NULL, arm_none, NULL, 0 , NULL}
578 };
579
580 struct arm_cpu_select
581 {
582   const char *              string;
583   const char *              name;
584   const struct processors * processors;
585 };
586
587 /* This is a magic structure.  The 'string' field is magically filled in
588    with a pointer to the value specified by the user on the command line
589    assuming that the user has specified such a value.  */
590
591 static struct arm_cpu_select arm_select[] =
592 {
593   /* string       name            processors  */
594   { NULL,       "-mcpu=",       all_cores  },
595   { NULL,       "-march=",      all_architectures },
596   { NULL,       "-mtune=",      all_cores }
597 };
598
599 /* Defines representing the indexes into the above table.  */
600 #define ARM_OPT_SET_CPU 0
601 #define ARM_OPT_SET_ARCH 1
602 #define ARM_OPT_SET_TUNE 2
603
604 /* The name of the proprocessor macro to define for this architecture.  */
605
606 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
607
608 struct fpu_desc
609 {
610   const char * name;
611   enum fputype fpu;
612 };
613
614
615 /* Available values for for -mfpu=.  */
616
617 static const struct fpu_desc all_fpus[] =
618 {
619   {"fpa",       FPUTYPE_FPA},
620   {"fpe2",      FPUTYPE_FPA_EMU2},
621   {"fpe3",      FPUTYPE_FPA_EMU2},
622   {"maverick",  FPUTYPE_MAVERICK},
623   {"vfp",       FPUTYPE_VFP}
624 };
625
626
627 /* Floating point models used by the different hardware.
628    See fputype in arm.h.  */
629
630 static const enum fputype fp_model_for_fpu[] =
631 {
632   /* No FP hardware.  */
633   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
634   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
635   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
636   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
637   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
638   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
639 };
640
641
642 struct float_abi
643 {
644   const char * name;
645   enum float_abi_type abi_type;
646 };
647
648
649 /* Available values for -mfloat-abi=.  */
650
651 static const struct float_abi all_float_abis[] =
652 {
653   {"soft",      ARM_FLOAT_ABI_SOFT},
654   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
655   {"hard",      ARM_FLOAT_ABI_HARD}
656 };
657
658
659 struct abi_name
660 {
661   const char *name;
662   enum arm_abi_type abi_type;
663 };
664
665
666 /* Available values for -mabi=.  */
667
668 static const struct abi_name arm_all_abis[] =
669 {
670   {"apcs-gnu",    ARM_ABI_APCS},
671   {"atpcs",   ARM_ABI_ATPCS},
672   {"aapcs",   ARM_ABI_AAPCS},
673   {"iwmmxt",  ARM_ABI_IWMMXT}
674 };
675
676 /* Return the number of bits set in VALUE.  */
677 static unsigned
678 bit_count (unsigned long value)
679 {
680   unsigned long count = 0;
681
682   while (value)
683     {
684       count++;
685       value &= value - 1;  /* Clear the least-significant set bit.  */
686     }
687
688   return count;
689 }
690
691 /* Set up library functions unique to ARM.  */
692
693 static void
694 arm_init_libfuncs (void)
695 {
696   /* There are no special library functions unless we are using the
697      ARM BPABI.  */
698   if (!TARGET_BPABI)
699     return;
700
701   /* The functions below are described in Section 4 of the "Run-Time
702      ABI for the ARM architecture", Version 1.0.  */
703
704   /* Double-precision floating-point arithmetic.  Table 2.  */
705   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
706   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
707   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
708   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
709   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
710
711   /* Double-precision comparisons.  Table 3.  */
712   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
713   set_optab_libfunc (ne_optab, DFmode, NULL);
714   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
715   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
716   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
717   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
718   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
719
720   /* Single-precision floating-point arithmetic.  Table 4.  */
721   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
722   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
723   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
724   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
725   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
726
727   /* Single-precision comparisons.  Table 5.  */
728   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
729   set_optab_libfunc (ne_optab, SFmode, NULL);
730   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
731   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
732   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
733   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
734   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
735
736   /* Floating-point to integer conversions.  Table 6.  */
737   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
738   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
739   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
740   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
741   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
742   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
743   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
744   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
745
746   /* Conversions between floating types.  Table 7.  */
747   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
748   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
749
750   /* Integer to floating-point conversions.  Table 8.  */
751   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
752   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
753   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
754   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
755   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
756   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
757   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
758   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
759
760   /* Long long.  Table 9.  */
761   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
762   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
763   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
764   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
765   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
766   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
767   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
768   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
769
770   /* Integer (32/32->32) division.  \S 4.3.1.  */
771   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
772   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
773
774   /* The divmod functions are designed so that they can be used for
775      plain division, even though they return both the quotient and the
776      remainder.  The quotient is returned in the usual location (i.e.,
777      r0 for SImode, {r0, r1} for DImode), just as would be expected
778      for an ordinary division routine.  Because the AAPCS calling
779      conventions specify that all of { r0, r1, r2, r3 } are
780      callee-saved registers, there is no need to tell the compiler
781      explicitly that those registers are clobbered by these
782      routines.  */
783   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
784   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
785   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idivmod");
786   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidivmod");
787
788   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
789      divmod libcalls instead.  */
790   set_optab_libfunc (smod_optab, DImode, NULL);
791   set_optab_libfunc (umod_optab, DImode, NULL);
792   set_optab_libfunc (smod_optab, SImode, NULL);
793   set_optab_libfunc (umod_optab, SImode, NULL);
794 }
795
796 /* Implement TARGET_HANDLE_OPTION.  */
797
798 static bool
799 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
800 {
801   switch (code)
802     {
803     case OPT_mabi_:
804       target_abi_name = arg;
805       return true;
806
807     case OPT_march_:
808       arm_select[1].string = arg;
809       return true;
810
811     case OPT_mcpu_:
812       arm_select[0].string = arg;
813       return true;
814
815     case OPT_mfloat_abi_:
816       target_float_abi_name = arg;
817       return true;
818
819     case OPT_mfp_:
820     case OPT_mfpe_:
821       target_fpe_name = arg;
822       return true;
823
824     case OPT_mfpu_:
825       target_fpu_name = arg;
826       return true;
827
828     case OPT_mhard_float:
829       target_float_abi_name = "hard";
830       return true;
831
832     case OPT_mpic_register_:
833       arm_pic_register_string = arg;
834       return true;
835
836     case OPT_msoft_float:
837       target_float_abi_name = "soft";
838       return true;
839
840     case OPT_mstructure_size_boundary_:
841       structure_size_string = arg;
842       return true;
843
844     case OPT_mtune_:
845       arm_select[2].string = arg;
846       return true;
847
848     default:
849       return true;
850     }
851 }
852
853 /* Fix up any incompatible options that the user has specified.
854    This has now turned into a maze.  */
855 void
856 arm_override_options (void)
857 {
858   unsigned i;
859   enum processor_type target_arch_cpu = arm_none;
860
861   /* Set up the flags based on the cpu/architecture selected by the user.  */
862   for (i = ARRAY_SIZE (arm_select); i--;)
863     {
864       struct arm_cpu_select * ptr = arm_select + i;
865
866       if (ptr->string != NULL && ptr->string[0] != '\0')
867         {
868           const struct processors * sel;
869
870           for (sel = ptr->processors; sel->name != NULL; sel++)
871             if (streq (ptr->string, sel->name))
872               {
873                 /* Set the architecture define.  */
874                 if (i != ARM_OPT_SET_TUNE)
875                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
876
877                 /* Determine the processor core for which we should
878                    tune code-generation.  */
879                 if (/* -mcpu= is a sensible default.  */
880                     i == ARM_OPT_SET_CPU
881                     /* -mtune= overrides -mcpu= and -march=.  */
882                     || i == ARM_OPT_SET_TUNE)
883                   arm_tune = (enum processor_type) (sel - ptr->processors);
884
885                 /* Remember the CPU associated with this architecture.
886                    If no other option is used to set the CPU type,
887                    we'll use this to guess the most suitable tuning
888                    options.  */
889                 if (i == ARM_OPT_SET_ARCH)
890                   target_arch_cpu = sel->core;
891                 
892                 if (i != ARM_OPT_SET_TUNE)
893                   {
894                     /* If we have been given an architecture and a processor
895                        make sure that they are compatible.  We only generate
896                        a warning though, and we prefer the CPU over the
897                        architecture.  */
898                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
899                       warning (0, "switch -mcpu=%s conflicts with -march= switch",
900                                ptr->string);
901
902                     insn_flags = sel->flags;
903                   }
904
905                 break;
906               }
907
908           if (sel->name == NULL)
909             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
910         }
911     }
912
913   /* Guess the tuning options from the architecture if necessary.  */
914   if (arm_tune == arm_none)
915     arm_tune = target_arch_cpu;
916
917   /* If the user did not specify a processor, choose one for them.  */
918   if (insn_flags == 0)
919     {
920       const struct processors * sel;
921       unsigned int        sought;
922       enum processor_type cpu;
923
924       cpu = TARGET_CPU_DEFAULT;
925       if (cpu == arm_none)
926         {
927 #ifdef SUBTARGET_CPU_DEFAULT
928           /* Use the subtarget default CPU if none was specified by
929              configure.  */
930           cpu = SUBTARGET_CPU_DEFAULT;
931 #endif
932           /* Default to ARM6.  */
933           if (cpu == arm_none)
934             cpu = arm6;
935         }
936       sel = &all_cores[cpu];
937
938       insn_flags = sel->flags;
939
940       /* Now check to see if the user has specified some command line
941          switch that require certain abilities from the cpu.  */
942       sought = 0;
943
944       if (TARGET_INTERWORK || TARGET_THUMB)
945         {
946           sought |= (FL_THUMB | FL_MODE32);
947
948           /* There are no ARM processors that support both APCS-26 and
949              interworking.  Therefore we force FL_MODE26 to be removed
950              from insn_flags here (if it was set), so that the search
951              below will always be able to find a compatible processor.  */
952           insn_flags &= ~FL_MODE26;
953         }
954
955       if (sought != 0 && ((sought & insn_flags) != sought))
956         {
957           /* Try to locate a CPU type that supports all of the abilities
958              of the default CPU, plus the extra abilities requested by
959              the user.  */
960           for (sel = all_cores; sel->name != NULL; sel++)
961             if ((sel->flags & sought) == (sought | insn_flags))
962               break;
963
964           if (sel->name == NULL)
965             {
966               unsigned current_bit_count = 0;
967               const struct processors * best_fit = NULL;
968
969               /* Ideally we would like to issue an error message here
970                  saying that it was not possible to find a CPU compatible
971                  with the default CPU, but which also supports the command
972                  line options specified by the programmer, and so they
973                  ought to use the -mcpu=<name> command line option to
974                  override the default CPU type.
975
976                  If we cannot find a cpu that has both the
977                  characteristics of the default cpu and the given
978                  command line options we scan the array again looking
979                  for a best match.  */
980               for (sel = all_cores; sel->name != NULL; sel++)
981                 if ((sel->flags & sought) == sought)
982                   {
983                     unsigned count;
984
985                     count = bit_count (sel->flags & insn_flags);
986
987                     if (count >= current_bit_count)
988                       {
989                         best_fit = sel;
990                         current_bit_count = count;
991                       }
992                   }
993
994               gcc_assert (best_fit);
995               sel = best_fit;
996             }
997
998           insn_flags = sel->flags;
999         }
1000       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1001       if (arm_tune == arm_none)
1002         arm_tune = (enum processor_type) (sel - all_cores);
1003     }
1004
1005   /* The processor for which we should tune should now have been
1006      chosen.  */
1007   gcc_assert (arm_tune != arm_none);
1008
1009   tune_flags = all_cores[(int)arm_tune].flags;
1010   if (optimize_size)
1011     targetm.rtx_costs = arm_size_rtx_costs;
1012   else
1013     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
1014
1015   /* Make sure that the processor choice does not conflict with any of the
1016      other command line choices.  */
1017   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
1018     {
1019       warning (0, "target CPU does not support interworking" );
1020       target_flags &= ~MASK_INTERWORK;
1021     }
1022
1023   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1024     {
1025       warning (0, "target CPU does not support THUMB instructions");
1026       target_flags &= ~MASK_THUMB;
1027     }
1028
1029   if (TARGET_APCS_FRAME && TARGET_THUMB)
1030     {
1031       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1032       target_flags &= ~MASK_APCS_FRAME;
1033     }
1034
1035   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1036      from here where no function is being compiled currently.  */
1037   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1038     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1039
1040   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1041     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1042
1043   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1044     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1045
1046   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1047     {
1048       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1049       target_flags |= MASK_APCS_FRAME;
1050     }
1051
1052   if (TARGET_POKE_FUNCTION_NAME)
1053     target_flags |= MASK_APCS_FRAME;
1054
1055   if (TARGET_APCS_REENT && flag_pic)
1056     error ("-fpic and -mapcs-reent are incompatible");
1057
1058   if (TARGET_APCS_REENT)
1059     warning (0, "APCS reentrant code not supported.  Ignored");
1060
1061   /* If this target is normally configured to use APCS frames, warn if they
1062      are turned off and debugging is turned on.  */
1063   if (TARGET_ARM
1064       && write_symbols != NO_DEBUG
1065       && !TARGET_APCS_FRAME
1066       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1067     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1068
1069   /* If stack checking is disabled, we can use r10 as the PIC register,
1070      which keeps r9 available.  */
1071   if (flag_pic)
1072     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
1073
1074   if (TARGET_APCS_FLOAT)
1075     warning (0, "passing floating point arguments in fp regs not yet supported");
1076
1077   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1078   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1079   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1080   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1081   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1082   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1083   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1084   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1085   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1086
1087   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1088   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1089   thumb_code = (TARGET_ARM == 0);
1090   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1091   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1092   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1093
1094   /* V5 code we generate is completely interworking capable, so we turn off
1095      TARGET_INTERWORK here to avoid many tests later on.  */
1096
1097   /* XXX However, we must pass the right pre-processor defines to CPP
1098      or GLD can get confused.  This is a hack.  */
1099   if (TARGET_INTERWORK)
1100     arm_cpp_interwork = 1;
1101
1102   if (arm_arch5)
1103     target_flags &= ~MASK_INTERWORK;
1104
1105   if (target_abi_name)
1106     {
1107       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1108         {
1109           if (streq (arm_all_abis[i].name, target_abi_name))
1110             {
1111               arm_abi = arm_all_abis[i].abi_type;
1112               break;
1113             }
1114         }
1115       if (i == ARRAY_SIZE (arm_all_abis))
1116         error ("invalid ABI option: -mabi=%s", target_abi_name);
1117     }
1118   else
1119     arm_abi = ARM_DEFAULT_ABI;
1120
1121   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1122     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1123
1124   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1125     error ("iwmmxt abi requires an iwmmxt capable cpu");
1126
1127   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1128   if (target_fpu_name == NULL && target_fpe_name != NULL)
1129     {
1130       if (streq (target_fpe_name, "2"))
1131         target_fpu_name = "fpe2";
1132       else if (streq (target_fpe_name, "3"))
1133         target_fpu_name = "fpe3";
1134       else
1135         error ("invalid floating point emulation option: -mfpe=%s",
1136                target_fpe_name);
1137     }
1138   if (target_fpu_name != NULL)
1139     {
1140       /* The user specified a FPU.  */
1141       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1142         {
1143           if (streq (all_fpus[i].name, target_fpu_name))
1144             {
1145               arm_fpu_arch = all_fpus[i].fpu;
1146               arm_fpu_tune = arm_fpu_arch;
1147               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1148               break;
1149             }
1150         }
1151       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1152         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1153     }
1154   else
1155     {
1156 #ifdef FPUTYPE_DEFAULT
1157       /* Use the default if it is specified for this platform.  */
1158       arm_fpu_arch = FPUTYPE_DEFAULT;
1159       arm_fpu_tune = FPUTYPE_DEFAULT;
1160 #else
1161       /* Pick one based on CPU type.  */
1162       /* ??? Some targets assume FPA is the default.
1163       if ((insn_flags & FL_VFP) != 0)
1164         arm_fpu_arch = FPUTYPE_VFP;
1165       else
1166       */
1167       if (arm_arch_cirrus)
1168         arm_fpu_arch = FPUTYPE_MAVERICK;
1169       else
1170         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1171 #endif
1172       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1173         arm_fpu_tune = FPUTYPE_FPA;
1174       else
1175         arm_fpu_tune = arm_fpu_arch;
1176       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1177       gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1178     }
1179
1180   if (target_float_abi_name != NULL)
1181     {
1182       /* The user specified a FP ABI.  */
1183       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1184         {
1185           if (streq (all_float_abis[i].name, target_float_abi_name))
1186             {
1187               arm_float_abi = all_float_abis[i].abi_type;
1188               break;
1189             }
1190         }
1191       if (i == ARRAY_SIZE (all_float_abis))
1192         error ("invalid floating point abi: -mfloat-abi=%s",
1193                target_float_abi_name);
1194     }
1195   else
1196     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1197
1198   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1199     sorry ("-mfloat-abi=hard and VFP");
1200
1201   /* If soft-float is specified then don't use FPU.  */
1202   if (TARGET_SOFT_FLOAT)
1203     arm_fpu_arch = FPUTYPE_NONE;
1204
1205   /* For arm2/3 there is no need to do any scheduling if there is only
1206      a floating point emulator, or we are doing software floating-point.  */
1207   if ((TARGET_SOFT_FLOAT
1208        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1209        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1210       && (tune_flags & FL_MODE32) == 0)
1211     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1212
1213   /* Override the default structure alignment for AAPCS ABI.  */
1214   if (arm_abi == ARM_ABI_AAPCS)
1215     arm_structure_size_boundary = 8;
1216
1217   if (structure_size_string != NULL)
1218     {
1219       int size = strtol (structure_size_string, NULL, 0);
1220
1221       if (size == 8 || size == 32
1222           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1223         arm_structure_size_boundary = size;
1224       else
1225         warning (0, "structure size boundary can only be set to %s",
1226                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1227     }
1228
1229   if (arm_pic_register_string != NULL)
1230     {
1231       int pic_register = decode_reg_name (arm_pic_register_string);
1232
1233       if (!flag_pic)
1234         warning (0, "-mpic-register= is useless without -fpic");
1235
1236       /* Prevent the user from choosing an obviously stupid PIC register.  */
1237       else if (pic_register < 0 || call_used_regs[pic_register]
1238                || pic_register == HARD_FRAME_POINTER_REGNUM
1239                || pic_register == STACK_POINTER_REGNUM
1240                || pic_register >= PC_REGNUM)
1241         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1242       else
1243         arm_pic_register = pic_register;
1244     }
1245
1246   if (TARGET_THUMB && flag_schedule_insns)
1247     {
1248       /* Don't warn since it's on by default in -O2.  */
1249       flag_schedule_insns = 0;
1250     }
1251
1252   if (optimize_size)
1253     {
1254       /* There's some dispute as to whether this should be 1 or 2.  However,
1255          experiments seem to show that in pathological cases a setting of
1256          1 degrades less severely than a setting of 2.  This could change if
1257          other parts of the compiler change their behavior.  */
1258       arm_constant_limit = 1;
1259
1260       /* If optimizing for size, bump the number of instructions that we
1261          are prepared to conditionally execute (even on a StrongARM).  */
1262       max_insns_skipped = 6;
1263     }
1264   else
1265     {
1266       /* For processors with load scheduling, it never costs more than
1267          2 cycles to load a constant, and the load scheduler may well
1268          reduce that to 1.  */
1269       if (arm_ld_sched)
1270         arm_constant_limit = 1;
1271
1272       /* On XScale the longer latency of a load makes it more difficult
1273          to achieve a good schedule, so it's faster to synthesize
1274          constants that can be done in two insns.  */
1275       if (arm_tune_xscale)
1276         arm_constant_limit = 2;
1277
1278       /* StrongARM has early execution of branches, so a sequence
1279          that is worth skipping is shorter.  */
1280       if (arm_tune_strongarm)
1281         max_insns_skipped = 3;
1282     }
1283
1284   /* Register global variables with the garbage collector.  */
1285   arm_add_gc_roots ();
1286 }
1287
1288 static void
1289 arm_add_gc_roots (void)
1290 {
1291   gcc_obstack_init(&minipool_obstack);
1292   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1293 }
1294 \f
1295 /* A table of known ARM exception types.
1296    For use with the interrupt function attribute.  */
1297
1298 typedef struct
1299 {
1300   const char *const arg;
1301   const unsigned long return_value;
1302 }
1303 isr_attribute_arg;
1304
1305 static const isr_attribute_arg isr_attribute_args [] =
1306 {
1307   { "IRQ",   ARM_FT_ISR },
1308   { "irq",   ARM_FT_ISR },
1309   { "FIQ",   ARM_FT_FIQ },
1310   { "fiq",   ARM_FT_FIQ },
1311   { "ABORT", ARM_FT_ISR },
1312   { "abort", ARM_FT_ISR },
1313   { "ABORT", ARM_FT_ISR },
1314   { "abort", ARM_FT_ISR },
1315   { "UNDEF", ARM_FT_EXCEPTION },
1316   { "undef", ARM_FT_EXCEPTION },
1317   { "SWI",   ARM_FT_EXCEPTION },
1318   { "swi",   ARM_FT_EXCEPTION },
1319   { NULL,    ARM_FT_NORMAL }
1320 };
1321
1322 /* Returns the (interrupt) function type of the current
1323    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1324
1325 static unsigned long
1326 arm_isr_value (tree argument)
1327 {
1328   const isr_attribute_arg * ptr;
1329   const char *              arg;
1330
1331   /* No argument - default to IRQ.  */
1332   if (argument == NULL_TREE)
1333     return ARM_FT_ISR;
1334
1335   /* Get the value of the argument.  */
1336   if (TREE_VALUE (argument) == NULL_TREE
1337       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1338     return ARM_FT_UNKNOWN;
1339
1340   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1341
1342   /* Check it against the list of known arguments.  */
1343   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1344     if (streq (arg, ptr->arg))
1345       return ptr->return_value;
1346
1347   /* An unrecognized interrupt type.  */
1348   return ARM_FT_UNKNOWN;
1349 }
1350
1351 /* Computes the type of the current function.  */
1352
1353 static unsigned long
1354 arm_compute_func_type (void)
1355 {
1356   unsigned long type = ARM_FT_UNKNOWN;
1357   tree a;
1358   tree attr;
1359
1360   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1361
1362   /* Decide if the current function is volatile.  Such functions
1363      never return, and many memory cycles can be saved by not storing
1364      register values that will never be needed again.  This optimization
1365      was added to speed up context switching in a kernel application.  */
1366   if (optimize > 0
1367       && TREE_NOTHROW (current_function_decl)
1368       && TREE_THIS_VOLATILE (current_function_decl))
1369     type |= ARM_FT_VOLATILE;
1370
1371   if (cfun->static_chain_decl != NULL)
1372     type |= ARM_FT_NESTED;
1373
1374   attr = DECL_ATTRIBUTES (current_function_decl);
1375
1376   a = lookup_attribute ("naked", attr);
1377   if (a != NULL_TREE)
1378     type |= ARM_FT_NAKED;
1379
1380   a = lookup_attribute ("isr", attr);
1381   if (a == NULL_TREE)
1382     a = lookup_attribute ("interrupt", attr);
1383
1384   if (a == NULL_TREE)
1385     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1386   else
1387     type |= arm_isr_value (TREE_VALUE (a));
1388
1389   return type;
1390 }
1391
1392 /* Returns the type of the current function.  */
1393
1394 unsigned long
1395 arm_current_func_type (void)
1396 {
1397   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1398     cfun->machine->func_type = arm_compute_func_type ();
1399
1400   return cfun->machine->func_type;
1401 }
1402 \f
1403 /* Return 1 if it is possible to return using a single instruction.
1404    If SIBLING is non-null, this is a test for a return before a sibling
1405    call.  SIBLING is the call insn, so we can examine its register usage.  */
1406
1407 int
1408 use_return_insn (int iscond, rtx sibling)
1409 {
1410   int regno;
1411   unsigned int func_type;
1412   unsigned long saved_int_regs;
1413   unsigned HOST_WIDE_INT stack_adjust;
1414   arm_stack_offsets *offsets;
1415
1416   /* Never use a return instruction before reload has run.  */
1417   if (!reload_completed)
1418     return 0;
1419
1420   func_type = arm_current_func_type ();
1421
1422   /* Naked functions and volatile functions need special
1423      consideration.  */
1424   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1425     return 0;
1426
1427   /* So do interrupt functions that use the frame pointer.  */
1428   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1429     return 0;
1430
1431   offsets = arm_get_frame_offsets ();
1432   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1433
1434   /* As do variadic functions.  */
1435   if (current_function_pretend_args_size
1436       || cfun->machine->uses_anonymous_args
1437       /* Or if the function calls __builtin_eh_return () */
1438       || current_function_calls_eh_return
1439       /* Or if the function calls alloca */
1440       || current_function_calls_alloca
1441       /* Or if there is a stack adjustment.  However, if the stack pointer
1442          is saved on the stack, we can use a pre-incrementing stack load.  */
1443       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1444     return 0;
1445
1446   saved_int_regs = arm_compute_save_reg_mask ();
1447
1448   /* Unfortunately, the insn
1449
1450        ldmib sp, {..., sp, ...}
1451
1452      triggers a bug on most SA-110 based devices, such that the stack
1453      pointer won't be correctly restored if the instruction takes a
1454      page fault.  We work around this problem by popping r3 along with
1455      the other registers, since that is never slower than executing
1456      another instruction.
1457
1458      We test for !arm_arch5 here, because code for any architecture
1459      less than this could potentially be run on one of the buggy
1460      chips.  */
1461   if (stack_adjust == 4 && !arm_arch5)
1462     {
1463       /* Validate that r3 is a call-clobbered register (always true in
1464          the default abi) ...  */
1465       if (!call_used_regs[3])
1466         return 0;
1467
1468       /* ... that it isn't being used for a return value (always true
1469          until we implement return-in-regs), or for a tail-call
1470          argument ...  */
1471       if (sibling)
1472         {
1473           gcc_assert (GET_CODE (sibling) == CALL_INSN);
1474
1475           if (find_regno_fusage (sibling, USE, 3))
1476             return 0;
1477         }
1478
1479       /* ... and that there are no call-saved registers in r0-r2
1480          (always true in the default ABI).  */
1481       if (saved_int_regs & 0x7)
1482         return 0;
1483     }
1484
1485   /* Can't be done if interworking with Thumb, and any registers have been
1486      stacked.  */
1487   if (TARGET_INTERWORK && saved_int_regs != 0)
1488     return 0;
1489
1490   /* On StrongARM, conditional returns are expensive if they aren't
1491      taken and multiple registers have been stacked.  */
1492   if (iscond && arm_tune_strongarm)
1493     {
1494       /* Conditional return when just the LR is stored is a simple
1495          conditional-load instruction, that's not expensive.  */
1496       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1497         return 0;
1498
1499       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1500         return 0;
1501     }
1502
1503   /* If there are saved registers but the LR isn't saved, then we need
1504      two instructions for the return.  */
1505   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1506     return 0;
1507
1508   /* Can't be done if any of the FPA regs are pushed,
1509      since this also requires an insn.  */
1510   if (TARGET_HARD_FLOAT && TARGET_FPA)
1511     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1512       if (regs_ever_live[regno] && !call_used_regs[regno])
1513         return 0;
1514
1515   /* Likewise VFP regs.  */
1516   if (TARGET_HARD_FLOAT && TARGET_VFP)
1517     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1518       if (regs_ever_live[regno] && !call_used_regs[regno])
1519         return 0;
1520
1521   if (TARGET_REALLY_IWMMXT)
1522     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1523       if (regs_ever_live[regno] && ! call_used_regs [regno])
1524         return 0;
1525
1526   return 1;
1527 }
1528
1529 /* Return TRUE if int I is a valid immediate ARM constant.  */
1530
1531 int
1532 const_ok_for_arm (HOST_WIDE_INT i)
1533 {
1534   unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1535
1536   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1537      be all zero, or all one.  */
1538   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1539       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1540           != ((~(unsigned HOST_WIDE_INT) 0)
1541               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1542     return FALSE;
1543
1544   /* Fast return for 0 and powers of 2 */
1545   if ((i & (i - 1)) == 0)
1546     return TRUE;
1547
1548   do
1549     {
1550       if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1551         return TRUE;
1552       mask =
1553           (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1554                           >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1555     }
1556   while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1557
1558   return FALSE;
1559 }
1560
1561 /* Return true if I is a valid constant for the operation CODE.  */
1562 static int
1563 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1564 {
1565   if (const_ok_for_arm (i))
1566     return 1;
1567
1568   switch (code)
1569     {
1570     case PLUS:
1571       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1572
1573     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1574     case XOR:
1575     case IOR:
1576       return 0;
1577
1578     case AND:
1579       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1580
1581     default:
1582       gcc_unreachable ();
1583     }
1584 }
1585
1586 /* Emit a sequence of insns to handle a large constant.
1587    CODE is the code of the operation required, it can be any of SET, PLUS,
1588    IOR, AND, XOR, MINUS;
1589    MODE is the mode in which the operation is being performed;
1590    VAL is the integer to operate on;
1591    SOURCE is the other operand (a register, or a null-pointer for SET);
1592    SUBTARGETS means it is safe to create scratch registers if that will
1593    either produce a simpler sequence, or we will want to cse the values.
1594    Return value is the number of insns emitted.  */
1595
1596 int
1597 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1598                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1599 {
1600   rtx cond;
1601
1602   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1603     cond = COND_EXEC_TEST (PATTERN (insn));
1604   else
1605     cond = NULL_RTX;
1606
1607   if (subtargets || code == SET
1608       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1609           && REGNO (target) != REGNO (source)))
1610     {
1611       /* After arm_reorg has been called, we can't fix up expensive
1612          constants by pushing them into memory so we must synthesize
1613          them in-line, regardless of the cost.  This is only likely to
1614          be more costly on chips that have load delay slots and we are
1615          compiling without running the scheduler (so no splitting
1616          occurred before the final instruction emission).
1617
1618          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1619       */
1620       if (!after_arm_reorg
1621           && !cond
1622           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1623                                 1, 0)
1624               > arm_constant_limit + (code != SET)))
1625         {
1626           if (code == SET)
1627             {
1628               /* Currently SET is the only monadic value for CODE, all
1629                  the rest are diadic.  */
1630               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1631               return 1;
1632             }
1633           else
1634             {
1635               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1636
1637               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1638               /* For MINUS, the value is subtracted from, since we never
1639                  have subtraction of a constant.  */
1640               if (code == MINUS)
1641                 emit_insn (gen_rtx_SET (VOIDmode, target,
1642                                         gen_rtx_MINUS (mode, temp, source)));
1643               else
1644                 emit_insn (gen_rtx_SET (VOIDmode, target,
1645                                         gen_rtx_fmt_ee (code, mode, source, temp)));
1646               return 2;
1647             }
1648         }
1649     }
1650
1651   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1652                            1);
1653 }
1654
1655 static int
1656 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1657 {
1658   HOST_WIDE_INT temp1;
1659   int num_insns = 0;
1660   do
1661     {
1662       int end;
1663
1664       if (i <= 0)
1665         i += 32;
1666       if (remainder & (3 << (i - 2)))
1667         {
1668           end = i - 8;
1669           if (end < 0)
1670             end += 32;
1671           temp1 = remainder & ((0x0ff << end)
1672                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1673           remainder &= ~temp1;
1674           num_insns++;
1675           i -= 6;
1676         }
1677       i -= 2;
1678     } while (remainder);
1679   return num_insns;
1680 }
1681
1682 /* Emit an instruction with the indicated PATTERN.  If COND is
1683    non-NULL, conditionalize the execution of the instruction on COND
1684    being true.  */
1685
1686 static void
1687 emit_constant_insn (rtx cond, rtx pattern)
1688 {
1689   if (cond)
1690     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1691   emit_insn (pattern);
1692 }
1693
1694 /* As above, but extra parameter GENERATE which, if clear, suppresses
1695    RTL generation.  */
1696
1697 static int
1698 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1699                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1700                   int generate)
1701 {
1702   int can_invert = 0;
1703   int can_negate = 0;
1704   int can_negate_initial = 0;
1705   int can_shift = 0;
1706   int i;
1707   int num_bits_set = 0;
1708   int set_sign_bit_copies = 0;
1709   int clear_sign_bit_copies = 0;
1710   int clear_zero_bit_copies = 0;
1711   int set_zero_bit_copies = 0;
1712   int insns = 0;
1713   unsigned HOST_WIDE_INT temp1, temp2;
1714   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1715
1716   /* Find out which operations are safe for a given CODE.  Also do a quick
1717      check for degenerate cases; these can occur when DImode operations
1718      are split.  */
1719   switch (code)
1720     {
1721     case SET:
1722       can_invert = 1;
1723       can_shift = 1;
1724       can_negate = 1;
1725       break;
1726
1727     case PLUS:
1728       can_negate = 1;
1729       can_negate_initial = 1;
1730       break;
1731
1732     case IOR:
1733       if (remainder == 0xffffffff)
1734         {
1735           if (generate)
1736             emit_constant_insn (cond,
1737                                 gen_rtx_SET (VOIDmode, target,
1738                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1739           return 1;
1740         }
1741       if (remainder == 0)
1742         {
1743           if (reload_completed && rtx_equal_p (target, source))
1744             return 0;
1745           if (generate)
1746             emit_constant_insn (cond,
1747                                 gen_rtx_SET (VOIDmode, target, source));
1748           return 1;
1749         }
1750       break;
1751
1752     case AND:
1753       if (remainder == 0)
1754         {
1755           if (generate)
1756             emit_constant_insn (cond,
1757                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1758           return 1;
1759         }
1760       if (remainder == 0xffffffff)
1761         {
1762           if (reload_completed && rtx_equal_p (target, source))
1763             return 0;
1764           if (generate)
1765             emit_constant_insn (cond,
1766                                 gen_rtx_SET (VOIDmode, target, source));
1767           return 1;
1768         }
1769       can_invert = 1;
1770       break;
1771
1772     case XOR:
1773       if (remainder == 0)
1774         {
1775           if (reload_completed && rtx_equal_p (target, source))
1776             return 0;
1777           if (generate)
1778             emit_constant_insn (cond,
1779                                 gen_rtx_SET (VOIDmode, target, source));
1780           return 1;
1781         }
1782       
1783       /* We don't know how to handle other cases yet.  */
1784       gcc_assert (remainder == 0xffffffff);
1785       
1786       if (generate)
1787         emit_constant_insn (cond,
1788                             gen_rtx_SET (VOIDmode, target,
1789                                          gen_rtx_NOT (mode, source)));
1790       return 1;
1791
1792     case MINUS:
1793       /* We treat MINUS as (val - source), since (source - val) is always
1794          passed as (source + (-val)).  */
1795       if (remainder == 0)
1796         {
1797           if (generate)
1798             emit_constant_insn (cond,
1799                                 gen_rtx_SET (VOIDmode, target,
1800                                              gen_rtx_NEG (mode, source)));
1801           return 1;
1802         }
1803       if (const_ok_for_arm (val))
1804         {
1805           if (generate)
1806             emit_constant_insn (cond,
1807                                 gen_rtx_SET (VOIDmode, target,
1808                                              gen_rtx_MINUS (mode, GEN_INT (val),
1809                                                             source)));
1810           return 1;
1811         }
1812       can_negate = 1;
1813
1814       break;
1815
1816     default:
1817       gcc_unreachable ();
1818     }
1819
1820   /* If we can do it in one insn get out quickly.  */
1821   if (const_ok_for_arm (val)
1822       || (can_negate_initial && const_ok_for_arm (-val))
1823       || (can_invert && const_ok_for_arm (~val)))
1824     {
1825       if (generate)
1826         emit_constant_insn (cond,
1827                             gen_rtx_SET (VOIDmode, target,
1828                                          (source
1829                                           ? gen_rtx_fmt_ee (code, mode, source,
1830                                                             GEN_INT (val))
1831                                           : GEN_INT (val))));
1832       return 1;
1833     }
1834
1835   /* Calculate a few attributes that may be useful for specific
1836      optimizations.  */
1837   for (i = 31; i >= 0; i--)
1838     {
1839       if ((remainder & (1 << i)) == 0)
1840         clear_sign_bit_copies++;
1841       else
1842         break;
1843     }
1844
1845   for (i = 31; i >= 0; i--)
1846     {
1847       if ((remainder & (1 << i)) != 0)
1848         set_sign_bit_copies++;
1849       else
1850         break;
1851     }
1852
1853   for (i = 0; i <= 31; i++)
1854     {
1855       if ((remainder & (1 << i)) == 0)
1856         clear_zero_bit_copies++;
1857       else
1858         break;
1859     }
1860
1861   for (i = 0; i <= 31; i++)
1862     {
1863       if ((remainder & (1 << i)) != 0)
1864         set_zero_bit_copies++;
1865       else
1866         break;
1867     }
1868
1869   switch (code)
1870     {
1871     case SET:
1872       /* See if we can do this by sign_extending a constant that is known
1873          to be negative.  This is a good, way of doing it, since the shift
1874          may well merge into a subsequent insn.  */
1875       if (set_sign_bit_copies > 1)
1876         {
1877           if (const_ok_for_arm
1878               (temp1 = ARM_SIGN_EXTEND (remainder
1879                                         << (set_sign_bit_copies - 1))))
1880             {
1881               if (generate)
1882                 {
1883                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1884                   emit_constant_insn (cond,
1885                                       gen_rtx_SET (VOIDmode, new_src,
1886                                                    GEN_INT (temp1)));
1887                   emit_constant_insn (cond,
1888                                       gen_ashrsi3 (target, new_src,
1889                                                    GEN_INT (set_sign_bit_copies - 1)));
1890                 }
1891               return 2;
1892             }
1893           /* For an inverted constant, we will need to set the low bits,
1894              these will be shifted out of harm's way.  */
1895           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1896           if (const_ok_for_arm (~temp1))
1897             {
1898               if (generate)
1899                 {
1900                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1901                   emit_constant_insn (cond,
1902                                       gen_rtx_SET (VOIDmode, new_src,
1903                                                    GEN_INT (temp1)));
1904                   emit_constant_insn (cond,
1905                                       gen_ashrsi3 (target, new_src,
1906                                                    GEN_INT (set_sign_bit_copies - 1)));
1907                 }
1908               return 2;
1909             }
1910         }
1911
1912       /* See if we can generate this by setting the bottom (or the top)
1913          16 bits, and then shifting these into the other half of the
1914          word.  We only look for the simplest cases, to do more would cost
1915          too much.  Be careful, however, not to generate this when the
1916          alternative would take fewer insns.  */
1917       if (val & 0xffff0000)
1918         {
1919           temp1 = remainder & 0xffff0000;
1920           temp2 = remainder & 0x0000ffff;
1921
1922           /* Overlaps outside this range are best done using other methods.  */
1923           for (i = 9; i < 24; i++)
1924             {
1925               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1926                   && !const_ok_for_arm (temp2))
1927                 {
1928                   rtx new_src = (subtargets
1929                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1930                                  : target);
1931                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1932                                             source, subtargets, generate);
1933                   source = new_src;
1934                   if (generate)
1935                     emit_constant_insn
1936                       (cond,
1937                        gen_rtx_SET
1938                        (VOIDmode, target,
1939                         gen_rtx_IOR (mode,
1940                                      gen_rtx_ASHIFT (mode, source,
1941                                                      GEN_INT (i)),
1942                                      source)));
1943                   return insns + 1;
1944                 }
1945             }
1946
1947           /* Don't duplicate cases already considered.  */
1948           for (i = 17; i < 24; i++)
1949             {
1950               if (((temp1 | (temp1 >> i)) == remainder)
1951                   && !const_ok_for_arm (temp1))
1952                 {
1953                   rtx new_src = (subtargets
1954                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1955                                  : target);
1956                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1957                                             source, subtargets, generate);
1958                   source = new_src;
1959                   if (generate)
1960                     emit_constant_insn
1961                       (cond,
1962                        gen_rtx_SET (VOIDmode, target,
1963                                     gen_rtx_IOR
1964                                     (mode,
1965                                      gen_rtx_LSHIFTRT (mode, source,
1966                                                        GEN_INT (i)),
1967                                      source)));
1968                   return insns + 1;
1969                 }
1970             }
1971         }
1972       break;
1973
1974     case IOR:
1975     case XOR:
1976       /* If we have IOR or XOR, and the constant can be loaded in a
1977          single instruction, and we can find a temporary to put it in,
1978          then this can be done in two instructions instead of 3-4.  */
1979       if (subtargets
1980           /* TARGET can't be NULL if SUBTARGETS is 0 */
1981           || (reload_completed && !reg_mentioned_p (target, source)))
1982         {
1983           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1984             {
1985               if (generate)
1986                 {
1987                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1988
1989                   emit_constant_insn (cond,
1990                                       gen_rtx_SET (VOIDmode, sub,
1991                                                    GEN_INT (val)));
1992                   emit_constant_insn (cond,
1993                                       gen_rtx_SET (VOIDmode, target,
1994                                                    gen_rtx_fmt_ee (code, mode,
1995                                                                    source, sub)));
1996                 }
1997               return 2;
1998             }
1999         }
2000
2001       if (code == XOR)
2002         break;
2003
2004       if (set_sign_bit_copies > 8
2005           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2006         {
2007           if (generate)
2008             {
2009               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2010               rtx shift = GEN_INT (set_sign_bit_copies);
2011
2012               emit_constant_insn
2013                 (cond,
2014                  gen_rtx_SET (VOIDmode, sub,
2015                               gen_rtx_NOT (mode,
2016                                            gen_rtx_ASHIFT (mode,
2017                                                            source,
2018                                                            shift))));
2019               emit_constant_insn
2020                 (cond,
2021                  gen_rtx_SET (VOIDmode, target,
2022                               gen_rtx_NOT (mode,
2023                                            gen_rtx_LSHIFTRT (mode, sub,
2024                                                              shift))));
2025             }
2026           return 2;
2027         }
2028
2029       if (set_zero_bit_copies > 8
2030           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2031         {
2032           if (generate)
2033             {
2034               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2035               rtx shift = GEN_INT (set_zero_bit_copies);
2036
2037               emit_constant_insn
2038                 (cond,
2039                  gen_rtx_SET (VOIDmode, sub,
2040                               gen_rtx_NOT (mode,
2041                                            gen_rtx_LSHIFTRT (mode,
2042                                                              source,
2043                                                              shift))));
2044               emit_constant_insn
2045                 (cond,
2046                  gen_rtx_SET (VOIDmode, target,
2047                               gen_rtx_NOT (mode,
2048                                            gen_rtx_ASHIFT (mode, sub,
2049                                                            shift))));
2050             }
2051           return 2;
2052         }
2053
2054       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2055         {
2056           if (generate)
2057             {
2058               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2059               emit_constant_insn (cond,
2060                                   gen_rtx_SET (VOIDmode, sub,
2061                                                gen_rtx_NOT (mode, source)));
2062               source = sub;
2063               if (subtargets)
2064                 sub = gen_reg_rtx (mode);
2065               emit_constant_insn (cond,
2066                                   gen_rtx_SET (VOIDmode, sub,
2067                                                gen_rtx_AND (mode, source,
2068                                                             GEN_INT (temp1))));
2069               emit_constant_insn (cond,
2070                                   gen_rtx_SET (VOIDmode, target,
2071                                                gen_rtx_NOT (mode, sub)));
2072             }
2073           return 3;
2074         }
2075       break;
2076
2077     case AND:
2078       /* See if two shifts will do 2 or more insn's worth of work.  */
2079       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2080         {
2081           HOST_WIDE_INT shift_mask = ((0xffffffff
2082                                        << (32 - clear_sign_bit_copies))
2083                                       & 0xffffffff);
2084
2085           if ((remainder | shift_mask) != 0xffffffff)
2086             {
2087               if (generate)
2088                 {
2089                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2090                   insns = arm_gen_constant (AND, mode, cond,
2091                                             remainder | shift_mask,
2092                                             new_src, source, subtargets, 1);
2093                   source = new_src;
2094                 }
2095               else
2096                 {
2097                   rtx targ = subtargets ? NULL_RTX : target;
2098                   insns = arm_gen_constant (AND, mode, cond,
2099                                             remainder | shift_mask,
2100                                             targ, source, subtargets, 0);
2101                 }
2102             }
2103
2104           if (generate)
2105             {
2106               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2107               rtx shift = GEN_INT (clear_sign_bit_copies);
2108
2109               emit_insn (gen_ashlsi3 (new_src, source, shift));
2110               emit_insn (gen_lshrsi3 (target, new_src, shift));
2111             }
2112
2113           return insns + 2;
2114         }
2115
2116       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2117         {
2118           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2119
2120           if ((remainder | shift_mask) != 0xffffffff)
2121             {
2122               if (generate)
2123                 {
2124                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2125
2126                   insns = arm_gen_constant (AND, mode, cond,
2127                                             remainder | shift_mask,
2128                                             new_src, source, subtargets, 1);
2129                   source = new_src;
2130                 }
2131               else
2132                 {
2133                   rtx targ = subtargets ? NULL_RTX : target;
2134
2135                   insns = arm_gen_constant (AND, mode, cond,
2136                                             remainder | shift_mask,
2137                                             targ, source, subtargets, 0);
2138                 }
2139             }
2140
2141           if (generate)
2142             {
2143               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2144               rtx shift = GEN_INT (clear_zero_bit_copies);
2145
2146               emit_insn (gen_lshrsi3 (new_src, source, shift));
2147               emit_insn (gen_ashlsi3 (target, new_src, shift));
2148             }
2149
2150           return insns + 2;
2151         }
2152
2153       break;
2154
2155     default:
2156       break;
2157     }
2158
2159   for (i = 0; i < 32; i++)
2160     if (remainder & (1 << i))
2161       num_bits_set++;
2162
2163   if (code == AND || (can_invert && num_bits_set > 16))
2164     remainder = (~remainder) & 0xffffffff;
2165   else if (code == PLUS && num_bits_set > 16)
2166     remainder = (-remainder) & 0xffffffff;
2167   else
2168     {
2169       can_invert = 0;
2170       can_negate = 0;
2171     }
2172
2173   /* Now try and find a way of doing the job in either two or three
2174      instructions.
2175      We start by looking for the largest block of zeros that are aligned on
2176      a 2-bit boundary, we then fill up the temps, wrapping around to the
2177      top of the word when we drop off the bottom.
2178      In the worst case this code should produce no more than four insns.  */
2179   {
2180     int best_start = 0;
2181     int best_consecutive_zeros = 0;
2182
2183     for (i = 0; i < 32; i += 2)
2184       {
2185         int consecutive_zeros = 0;
2186
2187         if (!(remainder & (3 << i)))
2188           {
2189             while ((i < 32) && !(remainder & (3 << i)))
2190               {
2191                 consecutive_zeros += 2;
2192                 i += 2;
2193               }
2194             if (consecutive_zeros > best_consecutive_zeros)
2195               {
2196                 best_consecutive_zeros = consecutive_zeros;
2197                 best_start = i - consecutive_zeros;
2198               }
2199             i -= 2;
2200           }
2201       }
2202
2203     /* So long as it won't require any more insns to do so, it's
2204        desirable to emit a small constant (in bits 0...9) in the last
2205        insn.  This way there is more chance that it can be combined with
2206        a later addressing insn to form a pre-indexed load or store
2207        operation.  Consider:
2208
2209                *((volatile int *)0xe0000100) = 1;
2210                *((volatile int *)0xe0000110) = 2;
2211
2212        We want this to wind up as:
2213
2214                 mov rA, #0xe0000000
2215                 mov rB, #1
2216                 str rB, [rA, #0x100]
2217                 mov rB, #2
2218                 str rB, [rA, #0x110]
2219
2220        rather than having to synthesize both large constants from scratch.
2221
2222        Therefore, we calculate how many insns would be required to emit
2223        the constant starting from `best_start', and also starting from
2224        zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2225        yield a shorter sequence, we may as well use zero.  */
2226     if (best_start != 0
2227         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2228         && (count_insns_for_constant (remainder, 0) <=
2229             count_insns_for_constant (remainder, best_start)))
2230       best_start = 0;
2231
2232     /* Now start emitting the insns.  */
2233     i = best_start;
2234     do
2235       {
2236         int end;
2237
2238         if (i <= 0)
2239           i += 32;
2240         if (remainder & (3 << (i - 2)))
2241           {
2242             end = i - 8;
2243             if (end < 0)
2244               end += 32;
2245             temp1 = remainder & ((0x0ff << end)
2246                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2247             remainder &= ~temp1;
2248
2249             if (generate)
2250               {
2251                 rtx new_src, temp1_rtx;
2252
2253                 if (code == SET || code == MINUS)
2254                   {
2255                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2256                     if (can_invert && code != MINUS)
2257                       temp1 = ~temp1;
2258                   }
2259                 else
2260                   {
2261                     if (remainder && subtargets)
2262                       new_src = gen_reg_rtx (mode);
2263                     else
2264                       new_src = target;
2265                     if (can_invert)
2266                       temp1 = ~temp1;
2267                     else if (can_negate)
2268                       temp1 = -temp1;
2269                   }
2270
2271                 temp1 = trunc_int_for_mode (temp1, mode);
2272                 temp1_rtx = GEN_INT (temp1);
2273
2274                 if (code == SET)
2275                   ;
2276                 else if (code == MINUS)
2277                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2278                 else
2279                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2280
2281                 emit_constant_insn (cond,
2282                                     gen_rtx_SET (VOIDmode, new_src,
2283                                                  temp1_rtx));
2284                 source = new_src;
2285               }
2286
2287             if (code == SET)
2288               {
2289                 can_invert = 0;
2290                 code = PLUS;
2291               }
2292             else if (code == MINUS)
2293               code = PLUS;
2294
2295             insns++;
2296             i -= 6;
2297           }
2298         i -= 2;
2299       }
2300     while (remainder);
2301   }
2302
2303   return insns;
2304 }
2305
2306 /* Canonicalize a comparison so that we are more likely to recognize it.
2307    This can be done for a few constant compares, where we can make the
2308    immediate value easier to load.  */
2309
2310 enum rtx_code
2311 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2312 {
2313   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2314
2315   switch (code)
2316     {
2317     case EQ:
2318     case NE:
2319       return code;
2320
2321     case GT:
2322     case LE:
2323       if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2324           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2325         {
2326           *op1 = GEN_INT (i + 1);
2327           return code == GT ? GE : LT;
2328         }
2329       break;
2330
2331     case GE:
2332     case LT:
2333       if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2334           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2335         {
2336           *op1 = GEN_INT (i - 1);
2337           return code == GE ? GT : LE;
2338         }
2339       break;
2340
2341     case GTU:
2342     case LEU:
2343       if (i != ~((unsigned HOST_WIDE_INT) 0)
2344           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2345         {
2346           *op1 = GEN_INT (i + 1);
2347           return code == GTU ? GEU : LTU;
2348         }
2349       break;
2350
2351     case GEU:
2352     case LTU:
2353       if (i != 0
2354           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2355         {
2356           *op1 = GEN_INT (i - 1);
2357           return code == GEU ? GTU : LEU;
2358         }
2359       break;
2360
2361     default:
2362       gcc_unreachable ();
2363     }
2364
2365   return code;
2366 }
2367
2368
2369 /* Define how to find the value returned by a function.  */
2370
2371 rtx
2372 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2373 {
2374   enum machine_mode mode;
2375   int unsignedp ATTRIBUTE_UNUSED;
2376   rtx r ATTRIBUTE_UNUSED;
2377
2378   mode = TYPE_MODE (type);
2379   /* Promote integer types.  */
2380   if (INTEGRAL_TYPE_P (type))
2381     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2382
2383   /* Promotes small structs returned in a register to full-word size
2384      for big-endian AAPCS.  */
2385   if (arm_return_in_msb (type))
2386     {
2387       HOST_WIDE_INT size = int_size_in_bytes (type);
2388       if (size % UNITS_PER_WORD != 0)
2389         {
2390           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2391           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2392         }
2393     }
2394   
2395   return LIBCALL_VALUE(mode);
2396 }
2397
2398 /* Determine the amount of memory needed to store the possible return 
2399    registers of an untyped call.  */
2400 int
2401 arm_apply_result_size (void)
2402 {
2403   int size = 16;
2404
2405   if (TARGET_ARM)
2406     {
2407       if (TARGET_HARD_FLOAT_ABI)
2408         {
2409           if (TARGET_FPA)
2410             size += 12;
2411           if (TARGET_MAVERICK)
2412             size += 8;
2413         }
2414       if (TARGET_IWMMXT_ABI)
2415         size += 8;
2416     }
2417
2418   return size;
2419 }
2420
2421 /* Decide whether a type should be returned in memory (true)
2422    or in a register (false).  This is called by the macro
2423    RETURN_IN_MEMORY.  */
2424 int
2425 arm_return_in_memory (tree type)
2426 {
2427   HOST_WIDE_INT size;
2428
2429   if (!AGGREGATE_TYPE_P (type) &&
2430       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2431     /* All simple types are returned in registers.
2432        For AAPCS, complex types are treated the same as aggregates.  */
2433     return 0;
2434
2435   size = int_size_in_bytes (type);
2436
2437   if (arm_abi != ARM_ABI_APCS)
2438     {
2439       /* ATPCS and later return aggregate types in memory only if they are
2440          larger than a word (or are variable size).  */
2441       return (size < 0 || size > UNITS_PER_WORD);
2442     }
2443
2444   /* For the arm-wince targets we choose to be compatible with Microsoft's
2445      ARM and Thumb compilers, which always return aggregates in memory.  */
2446 #ifndef ARM_WINCE
2447   /* All structures/unions bigger than one word are returned in memory.
2448      Also catch the case where int_size_in_bytes returns -1.  In this case
2449      the aggregate is either huge or of variable size, and in either case
2450      we will want to return it via memory and not in a register.  */
2451   if (size < 0 || size > UNITS_PER_WORD)
2452     return 1;
2453
2454   if (TREE_CODE (type) == RECORD_TYPE)
2455     {
2456       tree field;
2457
2458       /* For a struct the APCS says that we only return in a register
2459          if the type is 'integer like' and every addressable element
2460          has an offset of zero.  For practical purposes this means
2461          that the structure can have at most one non bit-field element
2462          and that this element must be the first one in the structure.  */
2463
2464       /* Find the first field, ignoring non FIELD_DECL things which will
2465          have been created by C++.  */
2466       for (field = TYPE_FIELDS (type);
2467            field && TREE_CODE (field) != FIELD_DECL;
2468            field = TREE_CHAIN (field))
2469         continue;
2470
2471       if (field == NULL)
2472         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2473
2474       /* Check that the first field is valid for returning in a register.  */
2475
2476       /* ... Floats are not allowed */
2477       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2478         return 1;
2479
2480       /* ... Aggregates that are not themselves valid for returning in
2481          a register are not allowed.  */
2482       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2483         return 1;
2484
2485       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2486          since they are not addressable.  */
2487       for (field = TREE_CHAIN (field);
2488            field;
2489            field = TREE_CHAIN (field))
2490         {
2491           if (TREE_CODE (field) != FIELD_DECL)
2492             continue;
2493
2494           if (!DECL_BIT_FIELD_TYPE (field))
2495             return 1;
2496         }
2497
2498       return 0;
2499     }
2500
2501   if (TREE_CODE (type) == UNION_TYPE)
2502     {
2503       tree field;
2504
2505       /* Unions can be returned in registers if every element is
2506          integral, or can be returned in an integer register.  */
2507       for (field = TYPE_FIELDS (type);
2508            field;
2509            field = TREE_CHAIN (field))
2510         {
2511           if (TREE_CODE (field) != FIELD_DECL)
2512             continue;
2513
2514           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2515             return 1;
2516
2517           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2518             return 1;
2519         }
2520
2521       return 0;
2522     }
2523 #endif /* not ARM_WINCE */
2524
2525   /* Return all other types in memory.  */
2526   return 1;
2527 }
2528
2529 /* Indicate whether or not words of a double are in big-endian order.  */
2530
2531 int
2532 arm_float_words_big_endian (void)
2533 {
2534   if (TARGET_MAVERICK)
2535     return 0;
2536
2537   /* For FPA, float words are always big-endian.  For VFP, floats words
2538      follow the memory system mode.  */
2539
2540   if (TARGET_FPA)
2541     {
2542       return 1;
2543     }
2544
2545   if (TARGET_VFP)
2546     return (TARGET_BIG_END ? 1 : 0);
2547
2548   return 1;
2549 }
2550
2551 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2552    for a call to a function whose data type is FNTYPE.
2553    For a library call, FNTYPE is NULL.  */
2554 void
2555 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2556                           rtx libname  ATTRIBUTE_UNUSED,
2557                           tree fndecl ATTRIBUTE_UNUSED)
2558 {
2559   /* On the ARM, the offset starts at 0.  */
2560   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2561   pcum->iwmmxt_nregs = 0;
2562   pcum->can_split = true;
2563
2564   pcum->call_cookie = CALL_NORMAL;
2565
2566   if (TARGET_LONG_CALLS)
2567     pcum->call_cookie = CALL_LONG;
2568
2569   /* Check for long call/short call attributes.  The attributes
2570      override any command line option.  */
2571   if (fntype)
2572     {
2573       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2574         pcum->call_cookie = CALL_SHORT;
2575       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2576         pcum->call_cookie = CALL_LONG;
2577     }
2578
2579   /* Varargs vectors are treated the same as long long.
2580      named_count avoids having to change the way arm handles 'named' */
2581   pcum->named_count = 0;
2582   pcum->nargs = 0;
2583
2584   if (TARGET_REALLY_IWMMXT && fntype)
2585     {
2586       tree fn_arg;
2587
2588       for (fn_arg = TYPE_ARG_TYPES (fntype);
2589            fn_arg;
2590            fn_arg = TREE_CHAIN (fn_arg))
2591         pcum->named_count += 1;
2592
2593       if (! pcum->named_count)
2594         pcum->named_count = INT_MAX;
2595     }
2596 }
2597
2598
2599 /* Return true if mode/type need doubleword alignment.  */
2600 bool
2601 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2602 {
2603   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2604           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2605 }
2606
2607
2608 /* Determine where to put an argument to a function.
2609    Value is zero to push the argument on the stack,
2610    or a hard register in which to store the argument.
2611
2612    MODE is the argument's machine mode.
2613    TYPE is the data type of the argument (as a tree).
2614     This is null for libcalls where that information may
2615     not be available.
2616    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2617     the preceding args and about the function being called.
2618    NAMED is nonzero if this argument is a named parameter
2619     (otherwise it is an extra parameter matching an ellipsis).  */
2620
2621 rtx
2622 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2623                   tree type, int named)
2624 {
2625   int nregs;
2626
2627   /* Varargs vectors are treated the same as long long.
2628      named_count avoids having to change the way arm handles 'named' */
2629   if (TARGET_IWMMXT_ABI
2630       && arm_vector_mode_supported_p (mode)
2631       && pcum->named_count > pcum->nargs + 1)
2632     {
2633       if (pcum->iwmmxt_nregs <= 9)
2634         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2635       else
2636         {
2637           pcum->can_split = false;
2638           return NULL_RTX;
2639         }
2640     }
2641
2642   /* Put doubleword aligned quantities in even register pairs.  */
2643   if (pcum->nregs & 1
2644       && ARM_DOUBLEWORD_ALIGN
2645       && arm_needs_doubleword_align (mode, type))
2646     pcum->nregs++;
2647
2648   if (mode == VOIDmode)
2649     /* Compute operand 2 of the call insn.  */
2650     return GEN_INT (pcum->call_cookie);
2651
2652   /* Only allow splitting an arg between regs and memory if all preceding
2653      args were allocated to regs.  For args passed by reference we only count
2654      the reference pointer.  */
2655   if (pcum->can_split)
2656     nregs = 1;
2657   else
2658     nregs = ARM_NUM_REGS2 (mode, type);
2659
2660   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2661     return NULL_RTX;
2662
2663   return gen_rtx_REG (mode, pcum->nregs);
2664 }
2665
2666 static int
2667 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2668                        tree type, bool named ATTRIBUTE_UNUSED)
2669 {
2670   int nregs = pcum->nregs;
2671
2672   if (arm_vector_mode_supported_p (mode))
2673     return 0;
2674
2675   if (NUM_ARG_REGS > nregs
2676       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2677       && pcum->can_split)
2678     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2679
2680   return 0;
2681 }
2682
2683 /* Variable sized types are passed by reference.  This is a GCC
2684    extension to the ARM ABI.  */
2685
2686 static bool
2687 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2688                        enum machine_mode mode ATTRIBUTE_UNUSED,
2689                        tree type, bool named ATTRIBUTE_UNUSED)
2690 {
2691   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2692 }
2693 \f
2694 /* Encode the current state of the #pragma [no_]long_calls.  */
2695 typedef enum
2696 {
2697   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2698   LONG,         /* #pragma long_calls is in effect.  */
2699   SHORT         /* #pragma no_long_calls is in effect.  */
2700 } arm_pragma_enum;
2701
2702 static arm_pragma_enum arm_pragma_long_calls = OFF;
2703
2704 void
2705 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2706 {
2707   arm_pragma_long_calls = LONG;
2708 }
2709
2710 void
2711 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2712 {
2713   arm_pragma_long_calls = SHORT;
2714 }
2715
2716 void
2717 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2718 {
2719   arm_pragma_long_calls = OFF;
2720 }
2721 \f
2722 /* Table of machine attributes.  */
2723 const struct attribute_spec arm_attribute_table[] =
2724 {
2725   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2726   /* Function calls made to this symbol must be done indirectly, because
2727      it may lie outside of the 26 bit addressing range of a normal function
2728      call.  */
2729   { "long_call",    0, 0, false, true,  true,  NULL },
2730   /* Whereas these functions are always known to reside within the 26 bit
2731      addressing range.  */
2732   { "short_call",   0, 0, false, true,  true,  NULL },
2733   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
2734   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2735   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2736   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2737 #ifdef ARM_PE
2738   /* ARM/PE has three new attributes:
2739      interfacearm - ?
2740      dllexport - for exporting a function/variable that will live in a dll
2741      dllimport - for importing a function/variable from a dll
2742
2743      Microsoft allows multiple declspecs in one __declspec, separating
2744      them with spaces.  We do NOT support this.  Instead, use __declspec
2745      multiple times.
2746   */
2747   { "dllimport",    0, 0, true,  false, false, NULL },
2748   { "dllexport",    0, 0, true,  false, false, NULL },
2749   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2750 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2751   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
2752   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
2753   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
2754 #endif
2755   { NULL,           0, 0, false, false, false, NULL }
2756 };
2757
2758 /* Handle an attribute requiring a FUNCTION_DECL;
2759    arguments as in struct attribute_spec.handler.  */
2760 static tree
2761 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2762                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2763 {
2764   if (TREE_CODE (*node) != FUNCTION_DECL)
2765     {
2766       warning (0, "%qs attribute only applies to functions",
2767                IDENTIFIER_POINTER (name));
2768       *no_add_attrs = true;
2769     }
2770
2771   return NULL_TREE;
2772 }
2773
2774 /* Handle an "interrupt" or "isr" attribute;
2775    arguments as in struct attribute_spec.handler.  */
2776 static tree
2777 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2778                           bool *no_add_attrs)
2779 {
2780   if (DECL_P (*node))
2781     {
2782       if (TREE_CODE (*node) != FUNCTION_DECL)
2783         {
2784           warning (0, "%qs attribute only applies to functions",
2785                    IDENTIFIER_POINTER (name));
2786           *no_add_attrs = true;
2787         }
2788       /* FIXME: the argument if any is checked for type attributes;
2789          should it be checked for decl ones?  */
2790     }
2791   else
2792     {
2793       if (TREE_CODE (*node) == FUNCTION_TYPE
2794           || TREE_CODE (*node) == METHOD_TYPE)
2795         {
2796           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2797             {
2798               warning (0, "%qs attribute ignored", IDENTIFIER_POINTER (name));
2799               *no_add_attrs = true;
2800             }
2801         }
2802       else if (TREE_CODE (*node) == POINTER_TYPE
2803                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2804                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2805                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2806         {
2807           *node = build_variant_type_copy (*node);
2808           TREE_TYPE (*node) = build_type_attribute_variant
2809             (TREE_TYPE (*node),
2810              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2811           *no_add_attrs = true;
2812         }
2813       else
2814         {
2815           /* Possibly pass this attribute on from the type to a decl.  */
2816           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2817                        | (int) ATTR_FLAG_FUNCTION_NEXT
2818                        | (int) ATTR_FLAG_ARRAY_NEXT))
2819             {
2820               *no_add_attrs = true;
2821               return tree_cons (name, args, NULL_TREE);
2822             }
2823           else
2824             {
2825               warning (0, "%qs attribute ignored", IDENTIFIER_POINTER (name));
2826             }
2827         }
2828     }
2829
2830   return NULL_TREE;
2831 }
2832
2833 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2834 /* Handle the "notshared" attribute.  This attribute is another way of
2835    requesting hidden visibility.  ARM's compiler supports
2836    "__declspec(notshared)"; we support the same thing via an
2837    attribute.  */
2838
2839 static tree
2840 arm_handle_notshared_attribute (tree *node, 
2841                                 tree name ATTRIBUTE_UNUSED, 
2842                                 tree args ATTRIBUTE_UNUSED, 
2843                                 int flags ATTRIBUTE_UNUSED, 
2844                                 bool *no_add_attrs)
2845 {
2846   tree decl = TYPE_NAME (*node);
2847
2848   if (decl)
2849     {
2850       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2851       DECL_VISIBILITY_SPECIFIED (decl) = 1;
2852       *no_add_attrs = false;
2853     }
2854   return NULL_TREE;
2855 }
2856 #endif
2857
2858 /* Return 0 if the attributes for two types are incompatible, 1 if they
2859    are compatible, and 2 if they are nearly compatible (which causes a
2860    warning to be generated).  */
2861 static int
2862 arm_comp_type_attributes (tree type1, tree type2)
2863 {
2864   int l1, l2, s1, s2;
2865
2866   /* Check for mismatch of non-default calling convention.  */
2867   if (TREE_CODE (type1) != FUNCTION_TYPE)
2868     return 1;
2869
2870   /* Check for mismatched call attributes.  */
2871   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2872   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2873   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2874   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2875
2876   /* Only bother to check if an attribute is defined.  */
2877   if (l1 | l2 | s1 | s2)
2878     {
2879       /* If one type has an attribute, the other must have the same attribute.  */
2880       if ((l1 != l2) || (s1 != s2))
2881         return 0;
2882
2883       /* Disallow mixed attributes.  */
2884       if ((l1 & s2) || (l2 & s1))
2885         return 0;
2886     }
2887
2888   /* Check for mismatched ISR attribute.  */
2889   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2890   if (! l1)
2891     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2892   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2893   if (! l2)
2894     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2895   if (l1 != l2)
2896     return 0;
2897
2898   return 1;
2899 }
2900
2901 /*  Encode long_call or short_call attribute by prefixing
2902     symbol name in DECL with a special character FLAG.  */
2903 void
2904 arm_encode_call_attribute (tree decl, int flag)
2905 {
2906   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2907   int          len = strlen (str);
2908   char *       newstr;
2909
2910   /* Do not allow weak functions to be treated as short call.  */
2911   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2912     return;
2913
2914   newstr = alloca (len + 2);
2915   newstr[0] = flag;
2916   strcpy (newstr + 1, str);
2917
2918   newstr = (char *) ggc_alloc_string (newstr, len + 1);
2919   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2920 }
2921
2922 /*  Assigns default attributes to newly defined type.  This is used to
2923     set short_call/long_call attributes for function types of
2924     functions defined inside corresponding #pragma scopes.  */
2925 static void
2926 arm_set_default_type_attributes (tree type)
2927 {
2928   /* Add __attribute__ ((long_call)) to all functions, when
2929      inside #pragma long_calls or __attribute__ ((short_call)),
2930      when inside #pragma no_long_calls.  */
2931   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2932     {
2933       tree type_attr_list, attr_name;
2934       type_attr_list = TYPE_ATTRIBUTES (type);
2935
2936       if (arm_pragma_long_calls == LONG)
2937         attr_name = get_identifier ("long_call");
2938       else if (arm_pragma_long_calls == SHORT)
2939         attr_name = get_identifier ("short_call");
2940       else
2941         return;
2942
2943       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2944       TYPE_ATTRIBUTES (type) = type_attr_list;
2945     }
2946 }
2947 \f
2948 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2949    defined within the current compilation unit.  If this cannot be
2950    determined, then 0 is returned.  */
2951 static int
2952 current_file_function_operand (rtx sym_ref)
2953 {
2954   /* This is a bit of a fib.  A function will have a short call flag
2955      applied to its name if it has the short call attribute, or it has
2956      already been defined within the current compilation unit.  */
2957   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2958     return 1;
2959
2960   /* The current function is always defined within the current compilation
2961      unit.  If it s a weak definition however, then this may not be the real
2962      definition of the function, and so we have to say no.  */
2963   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2964       && !DECL_WEAK (current_function_decl))
2965     return 1;
2966
2967   /* We cannot make the determination - default to returning 0.  */
2968   return 0;
2969 }
2970
2971 /* Return nonzero if a 32 bit "long_call" should be generated for
2972    this call.  We generate a long_call if the function:
2973
2974         a.  has an __attribute__((long call))
2975      or b.  is within the scope of a #pragma long_calls
2976      or c.  the -mlong-calls command line switch has been specified
2977          .  and either:
2978                 1. -ffunction-sections is in effect
2979              or 2. the current function has __attribute__ ((section))
2980              or 3. the target function has __attribute__ ((section))
2981
2982    However we do not generate a long call if the function:
2983
2984         d.  has an __attribute__ ((short_call))
2985      or e.  is inside the scope of a #pragma no_long_calls
2986      or f.  is defined within the current compilation unit.
2987
2988    This function will be called by C fragments contained in the machine
2989    description file.  SYM_REF and CALL_COOKIE correspond to the matched
2990    rtl operands.  CALL_SYMBOL is used to distinguish between
2991    two different callers of the function.  It is set to 1 in the
2992    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2993    and "call_value" patterns.  This is because of the difference in the
2994    SYM_REFs passed by these patterns.  */
2995 int
2996 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2997 {
2998   if (!call_symbol)
2999     {
3000       if (GET_CODE (sym_ref) != MEM)
3001         return 0;
3002
3003       sym_ref = XEXP (sym_ref, 0);
3004     }
3005
3006   if (GET_CODE (sym_ref) != SYMBOL_REF)
3007     return 0;
3008
3009   if (call_cookie & CALL_SHORT)
3010     return 0;
3011
3012   if (TARGET_LONG_CALLS)
3013     {
3014       if (flag_function_sections
3015           || DECL_SECTION_NAME (current_function_decl))
3016         /* c.3 is handled by the definition of the
3017            ARM_DECLARE_FUNCTION_SIZE macro.  */
3018         return 1;
3019     }
3020
3021   if (current_file_function_operand (sym_ref))
3022     return 0;
3023
3024   return (call_cookie & CALL_LONG)
3025     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
3026     || TARGET_LONG_CALLS;
3027 }
3028
3029 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3030 static bool
3031 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3032 {
3033   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
3034
3035   if (cfun->machine->sibcall_blocked)
3036     return false;
3037
3038   /* Never tailcall something for which we have no decl, or if we
3039      are in Thumb mode.  */
3040   if (decl == NULL || TARGET_THUMB)
3041     return false;
3042
3043   /* Get the calling method.  */
3044   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3045     call_type = CALL_SHORT;
3046   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3047     call_type = CALL_LONG;
3048
3049   /* Cannot tail-call to long calls, since these are out of range of
3050      a branch instruction.  However, if not compiling PIC, we know
3051      we can reach the symbol if it is in this compilation unit.  */
3052   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
3053     return false;
3054
3055   /* If we are interworking and the function is not declared static
3056      then we can't tail-call it unless we know that it exists in this
3057      compilation unit (since it might be a Thumb routine).  */
3058   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3059     return false;
3060
3061   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3062   if (IS_INTERRUPT (arm_current_func_type ()))
3063     return false;
3064
3065   /* Everything else is ok.  */
3066   return true;
3067 }
3068
3069 \f
3070 /* Addressing mode support functions.  */
3071
3072 /* Return nonzero if X is a legitimate immediate operand when compiling
3073    for PIC.  */
3074 int
3075 legitimate_pic_operand_p (rtx x)
3076 {
3077   if (CONSTANT_P (x)
3078       && flag_pic
3079       && (GET_CODE (x) == SYMBOL_REF
3080           || (GET_CODE (x) == CONST
3081               && GET_CODE (XEXP (x, 0)) == PLUS
3082               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
3083     return 0;
3084
3085   return 1;
3086 }
3087
3088 rtx
3089 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3090 {
3091   if (GET_CODE (orig) == SYMBOL_REF
3092       || GET_CODE (orig) == LABEL_REF)
3093     {
3094 #ifndef AOF_ASSEMBLER
3095       rtx pic_ref, address;
3096 #endif
3097       rtx insn;
3098       int subregs = 0;
3099
3100       if (reg == 0)
3101         {
3102           gcc_assert (!no_new_pseudos);
3103           reg = gen_reg_rtx (Pmode);
3104
3105           subregs = 1;
3106         }
3107
3108 #ifdef AOF_ASSEMBLER
3109       /* The AOF assembler can generate relocations for these directly, and
3110          understands that the PIC register has to be added into the offset.  */
3111       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3112 #else
3113       if (subregs)
3114         address = gen_reg_rtx (Pmode);
3115       else
3116         address = reg;
3117
3118       if (TARGET_ARM)
3119         emit_insn (gen_pic_load_addr_arm (address, orig));
3120       else
3121         emit_insn (gen_pic_load_addr_thumb (address, orig));
3122
3123       if ((GET_CODE (orig) == LABEL_REF
3124            || (GET_CODE (orig) == SYMBOL_REF &&
3125                SYMBOL_REF_LOCAL_P (orig)))
3126           && NEED_GOT_RELOC)
3127         pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
3128       else
3129         {
3130           pic_ref = gen_const_mem (Pmode,
3131                                    gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3132                                                  address));
3133         }
3134
3135       insn = emit_move_insn (reg, pic_ref);
3136 #endif
3137       current_function_uses_pic_offset_table = 1;
3138       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3139          by loop.  */
3140       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3141                                             REG_NOTES (insn));
3142       return reg;
3143     }
3144   else if (GET_CODE (orig) == CONST)
3145     {
3146       rtx base, offset;
3147
3148       if (GET_CODE (XEXP (orig, 0)) == PLUS
3149           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3150         return orig;
3151
3152       if (reg == 0)
3153         {
3154           gcc_assert (!no_new_pseudos);
3155           reg = gen_reg_rtx (Pmode);
3156         }
3157
3158       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3159       
3160       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3161       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3162                                        base == reg ? 0 : reg);
3163
3164       if (GET_CODE (offset) == CONST_INT)
3165         {
3166           /* The base register doesn't really matter, we only want to
3167              test the index for the appropriate mode.  */
3168           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3169             {
3170               gcc_assert (!no_new_pseudos);
3171               offset = force_reg (Pmode, offset);
3172             }
3173
3174           if (GET_CODE (offset) == CONST_INT)
3175             return plus_constant (base, INTVAL (offset));
3176         }
3177
3178       if (GET_MODE_SIZE (mode) > 4
3179           && (GET_MODE_CLASS (mode) == MODE_INT
3180               || TARGET_SOFT_FLOAT))
3181         {
3182           emit_insn (gen_addsi3 (reg, base, offset));
3183           return reg;
3184         }
3185
3186       return gen_rtx_PLUS (Pmode, base, offset);
3187     }
3188
3189   return orig;
3190 }
3191
3192
3193 /* Find a spare low register to use during the prolog of a function.  */
3194
3195 static int
3196 thumb_find_work_register (unsigned long pushed_regs_mask)
3197 {
3198   int reg;
3199
3200   /* Check the argument registers first as these are call-used.  The
3201      register allocation order means that sometimes r3 might be used
3202      but earlier argument registers might not, so check them all.  */
3203   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3204     if (!regs_ever_live[reg])
3205       return reg;
3206
3207   /* Before going on to check the call-saved registers we can try a couple
3208      more ways of deducing that r3 is available.  The first is when we are
3209      pushing anonymous arguments onto the stack and we have less than 4
3210      registers worth of fixed arguments(*).  In this case r3 will be part of
3211      the variable argument list and so we can be sure that it will be
3212      pushed right at the start of the function.  Hence it will be available
3213      for the rest of the prologue.
3214      (*): ie current_function_pretend_args_size is greater than 0.  */
3215   if (cfun->machine->uses_anonymous_args
3216       && current_function_pretend_args_size > 0)
3217     return LAST_ARG_REGNUM;
3218
3219   /* The other case is when we have fixed arguments but less than 4 registers
3220      worth.  In this case r3 might be used in the body of the function, but
3221      it is not being used to convey an argument into the function.  In theory
3222      we could just check current_function_args_size to see how many bytes are
3223      being passed in argument registers, but it seems that it is unreliable.
3224      Sometimes it will have the value 0 when in fact arguments are being
3225      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3226      check the args_info.nregs field as well.  The problem with this field is
3227      that it makes no allowances for arguments that are passed to the
3228      function but which are not used.  Hence we could miss an opportunity
3229      when a function has an unused argument in r3.  But it is better to be
3230      safe than to be sorry.  */
3231   if (! cfun->machine->uses_anonymous_args
3232       && current_function_args_size >= 0
3233       && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3234       && cfun->args_info.nregs < 4)
3235     return LAST_ARG_REGNUM;
3236   
3237   /* Otherwise look for a call-saved register that is going to be pushed.  */
3238   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3239     if (pushed_regs_mask & (1 << reg))
3240       return reg;
3241
3242   /* Something went wrong - thumb_compute_save_reg_mask()
3243      should have arranged for a suitable register to be pushed.  */
3244   gcc_unreachable ();
3245 }
3246
3247
3248 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3249    low register.  */
3250
3251 void
3252 arm_load_pic_register (unsigned int scratch)
3253 {
3254 #ifndef AOF_ASSEMBLER
3255   rtx l1, pic_tmp, pic_tmp2, pic_rtx;
3256   rtx global_offset_table;
3257
3258   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3259     return;
3260
3261   gcc_assert (flag_pic);
3262
3263   l1 = gen_label_rtx ();
3264
3265   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3266   /* On the ARM the PC register contains 'dot + 8' at the time of the
3267      addition, on the Thumb it is 'dot + 4'.  */
3268   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
3269   if (GOT_PCREL)
3270     pic_tmp2 = gen_rtx_CONST (VOIDmode,
3271                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3272   else
3273     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3274
3275   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3276
3277   if (TARGET_ARM)
3278     {
3279       emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3280       emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
3281     }
3282   else
3283     {
3284       if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3285         {
3286           /* We will have pushed the pic register, so should always be
3287              able to find a work register.  */
3288           pic_tmp = gen_rtx_REG (SImode, scratch);
3289           emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3290           emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3291         }
3292       else
3293         emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3294       emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
3295     }
3296
3297   /* Need to emit this whether or not we obey regdecls,
3298      since setjmp/longjmp can cause life info to screw up.  */
3299   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3300 #endif /* AOF_ASSEMBLER */
3301 }
3302
3303
3304 /* Return nonzero if X is valid as an ARM state addressing register.  */
3305 static int
3306 arm_address_register_rtx_p (rtx x, int strict_p)
3307 {
3308   int regno;
3309
3310   if (GET_CODE (x) != REG)
3311     return 0;
3312
3313   regno = REGNO (x);
3314
3315   if (strict_p)
3316     return ARM_REGNO_OK_FOR_BASE_P (regno);
3317
3318   return (regno <= LAST_ARM_REGNUM
3319           || regno >= FIRST_PSEUDO_REGISTER
3320           || regno == FRAME_POINTER_REGNUM
3321           || regno == ARG_POINTER_REGNUM);
3322 }
3323
3324 /* Return nonzero if X is a valid ARM state address operand.  */
3325 int
3326 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3327                           int strict_p)
3328 {
3329   bool use_ldrd;
3330   enum rtx_code code = GET_CODE (x);
3331
3332   if (arm_address_register_rtx_p (x, strict_p))
3333     return 1;
3334
3335   use_ldrd = (TARGET_LDRD
3336               && (mode == DImode
3337                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3338
3339   if (code == POST_INC || code == PRE_DEC
3340       || ((code == PRE_INC || code == POST_DEC)
3341           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3342     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3343
3344   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3345            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3346            && GET_CODE (XEXP (x, 1)) == PLUS
3347            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3348     {
3349       rtx addend = XEXP (XEXP (x, 1), 1);
3350
3351       /* Don't allow ldrd post increment by register because it's hard
3352          to fixup invalid register choices.  */
3353       if (use_ldrd
3354           && GET_CODE (x) == POST_MODIFY
3355           && GET_CODE (addend) == REG)
3356         return 0;
3357
3358       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3359               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3360     }
3361
3362   /* After reload constants split into minipools will have addresses
3363      from a LABEL_REF.  */
3364   else if (reload_completed
3365            && (code == LABEL_REF
3366                || (code == CONST
3367                    && GET_CODE (XEXP (x, 0)) == PLUS
3368                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3369                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3370     return 1;
3371
3372   else if (mode == TImode)
3373     return 0;
3374
3375   else if (code == PLUS)
3376     {
3377       rtx xop0 = XEXP (x, 0);
3378       rtx xop1 = XEXP (x, 1);
3379
3380       return ((arm_address_register_rtx_p (xop0, strict_p)
3381                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3382               || (arm_address_register_rtx_p (xop1, strict_p)
3383                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3384     }
3385
3386 #if 0
3387   /* Reload currently can't handle MINUS, so disable this for now */
3388   else if (GET_CODE (x) == MINUS)
3389     {
3390       rtx xop0 = XEXP (x, 0);
3391       rtx xop1 = XEXP (x, 1);
3392
3393       return (arm_address_register_rtx_p (xop0, strict_p)
3394               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3395     }
3396 #endif
3397
3398   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3399            && code == SYMBOL_REF
3400            && CONSTANT_POOL_ADDRESS_P (x)
3401            && ! (flag_pic
3402                  && symbol_mentioned_p (get_pool_constant (x))))
3403     return 1;
3404
3405   return 0;
3406 }
3407
3408 /* Return nonzero if INDEX is valid for an address index operand in
3409    ARM state.  */
3410 static int
3411 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3412                         int strict_p)
3413 {
3414   HOST_WIDE_INT range;
3415   enum rtx_code code = GET_CODE (index);
3416
3417   /* Standard coprocessor addressing modes.  */
3418   if (TARGET_HARD_FLOAT
3419       && (TARGET_FPA || TARGET_MAVERICK)
3420       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3421           || (TARGET_MAVERICK && mode == DImode)))
3422     return (code == CONST_INT && INTVAL (index) < 1024
3423             && INTVAL (index) > -1024
3424             && (INTVAL (index) & 3) == 0);
3425
3426   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3427     return (code == CONST_INT
3428             && INTVAL (index) < 1024
3429             && INTVAL (index) > -1024
3430             && (INTVAL (index) & 3) == 0);
3431
3432   if (arm_address_register_rtx_p (index, strict_p)
3433       && (GET_MODE_SIZE (mode) <= 4))
3434     return 1;
3435
3436   if (mode == DImode || mode == DFmode)
3437     {
3438       if (code == CONST_INT)
3439         {
3440           HOST_WIDE_INT val = INTVAL (index);
3441
3442           if (TARGET_LDRD)
3443             return val > -256 && val < 256;
3444           else
3445             return val > -4096 && val < 4092;
3446         }
3447
3448       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3449     }
3450
3451   if (GET_MODE_SIZE (mode) <= 4
3452       && ! (arm_arch4
3453             && (mode == HImode
3454                 || (mode == QImode && outer == SIGN_EXTEND))))
3455     {
3456       if (code == MULT)
3457         {
3458           rtx xiop0 = XEXP (index, 0);
3459           rtx xiop1 = XEXP (index, 1);
3460
3461           return ((arm_address_register_rtx_p (xiop0, strict_p)
3462                    && power_of_two_operand (xiop1, SImode))
3463                   || (arm_address_register_rtx_p (xiop1, strict_p)
3464                       && power_of_two_operand (xiop0, SImode)));
3465         }
3466       else if (code == LSHIFTRT || code == ASHIFTRT
3467                || code == ASHIFT || code == ROTATERT)
3468         {
3469           rtx op = XEXP (index, 1);
3470
3471           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3472                   && GET_CODE (op) == CONST_INT
3473                   && INTVAL (op) > 0
3474                   && INTVAL (op) <= 31);
3475         }
3476     }
3477
3478   /* For ARM v4 we may be doing a sign-extend operation during the
3479      load.  */
3480   if (arm_arch4)
3481     {
3482       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3483         range = 256;
3484       else
3485         range = 4096;
3486     }
3487   else
3488     range = (mode == HImode) ? 4095 : 4096;
3489
3490   return (code == CONST_INT
3491           && INTVAL (index) < range
3492           && INTVAL (index) > -range);
3493 }
3494
3495 /* Return nonzero if X is valid as a Thumb state base register.  */
3496 static int
3497 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3498 {
3499   int regno;
3500
3501   if (GET_CODE (x) != REG)
3502     return 0;
3503
3504   regno = REGNO (x);
3505
3506   if (strict_p)
3507     return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3508
3509   return (regno <= LAST_LO_REGNUM
3510           || regno > LAST_VIRTUAL_REGISTER
3511           || regno == FRAME_POINTER_REGNUM
3512           || (GET_MODE_SIZE (mode) >= 4
3513               && (regno == STACK_POINTER_REGNUM
3514                   || regno >= FIRST_PSEUDO_REGISTER
3515                   || x == hard_frame_pointer_rtx
3516                   || x == arg_pointer_rtx)));
3517 }
3518
3519 /* Return nonzero if x is a legitimate index register.  This is the case
3520    for any base register that can access a QImode object.  */
3521 inline static int
3522 thumb_index_register_rtx_p (rtx x, int strict_p)
3523 {
3524   return thumb_base_register_rtx_p (x, QImode, strict_p);
3525 }
3526
3527 /* Return nonzero if x is a legitimate Thumb-state address.
3528
3529    The AP may be eliminated to either the SP or the FP, so we use the
3530    least common denominator, e.g. SImode, and offsets from 0 to 64.
3531
3532    ??? Verify whether the above is the right approach.
3533
3534    ??? Also, the FP may be eliminated to the SP, so perhaps that
3535    needs special handling also.
3536
3537    ??? Look at how the mips16 port solves this problem.  It probably uses
3538    better ways to solve some of these problems.
3539
3540    Although it is not incorrect, we don't accept QImode and HImode
3541    addresses based on the frame pointer or arg pointer until the
3542    reload pass starts.  This is so that eliminating such addresses
3543    into stack based ones won't produce impossible code.  */
3544 int
3545 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3546 {
3547   /* ??? Not clear if this is right.  Experiment.  */
3548   if (GET_MODE_SIZE (mode) < 4
3549       && !(reload_in_progress || reload_completed)
3550       && (reg_mentioned_p (frame_pointer_rtx, x)
3551           || reg_mentioned_p (arg_pointer_rtx, x)
3552           || reg_mentioned_p (virtual_incoming_args_rtx, x)
3553           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3554           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3555           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3556     return 0;
3557
3558   /* Accept any base register.  SP only in SImode or larger.  */
3559   else if (thumb_base_register_rtx_p (x, mode, strict_p))
3560     return 1;
3561
3562   /* This is PC relative data before arm_reorg runs.  */
3563   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3564            && GET_CODE (x) == SYMBOL_REF
3565            && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3566     return 1;
3567
3568   /* This is PC relative data after arm_reorg runs.  */
3569   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3570            && (GET_CODE (x) == LABEL_REF
3571                || (GET_CODE (x) == CONST
3572                    && GET_CODE (XEXP (x, 0)) == PLUS
3573                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3574                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3575     return 1;
3576
3577   /* Post-inc indexing only supported for SImode and larger.  */
3578   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3579            && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3580     return 1;
3581
3582   else if (GET_CODE (x) == PLUS)
3583     {
3584       /* REG+REG address can be any two index registers.  */
3585       /* We disallow FRAME+REG addressing since we know that FRAME
3586          will be replaced with STACK, and SP relative addressing only
3587          permits SP+OFFSET.  */
3588       if (GET_MODE_SIZE (mode) <= 4
3589           && XEXP (x, 0) != frame_pointer_rtx
3590           && XEXP (x, 1) != frame_pointer_rtx
3591           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3592           && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3593         return 1;
3594
3595       /* REG+const has 5-7 bit offset for non-SP registers.  */
3596       else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3597                 || XEXP (x, 0) == arg_pointer_rtx)
3598                && GET_CODE (XEXP (x, 1)) == CONST_INT
3599                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3600         return 1;
3601
3602       /* REG+const has 10 bit offset for SP, but only SImode and
3603          larger is supported.  */
3604       /* ??? Should probably check for DI/DFmode overflow here
3605          just like GO_IF_LEGITIMATE_OFFSET does.  */
3606       else if (GET_CODE (XEXP (x, 0)) == REG
3607                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3608                && GET_MODE_SIZE (mode) >= 4
3609                && GET_CODE (XEXP (x, 1)) == CONST_INT
3610                && INTVAL (XEXP (x, 1)) >= 0
3611                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3612                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3613         return 1;
3614
3615       else if (GET_CODE (XEXP (x, 0)) == REG
3616                && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3617                && GET_MODE_SIZE (mode) >= 4
3618                && GET_CODE (XEXP (x, 1)) == CONST_INT
3619                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3620         return 1;
3621     }
3622
3623   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3624            && GET_MODE_SIZE (mode) == 4
3625            && GET_CODE (x) == SYMBOL_REF
3626            && CONSTANT_POOL_ADDRESS_P (x)
3627            && !(flag_pic
3628                 && symbol_mentioned_p (get_pool_constant (x))))
3629     return 1;
3630
3631   return 0;
3632 }
3633
3634 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3635    instruction of mode MODE.  */
3636 int
3637 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3638 {
3639   switch (GET_MODE_SIZE (mode))
3640     {
3641     case 1:
3642       return val >= 0 && val < 32;
3643
3644     case 2:
3645       return val >= 0 && val < 64 && (val & 1) == 0;
3646
3647     default:
3648       return (val >= 0
3649               && (val + GET_MODE_SIZE (mode)) <= 128
3650               && (val & 3) == 0);
3651     }
3652 }
3653
3654 /* Try machine-dependent ways of modifying an illegitimate address
3655    to be legitimate.  If we find one, return the new, valid address.  */
3656 rtx
3657 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3658 {
3659   if (GET_CODE (x) == PLUS)
3660     {
3661       rtx xop0 = XEXP (x, 0);
3662       rtx xop1 = XEXP (x, 1);
3663
3664       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3665         xop0 = force_reg (SImode, xop0);
3666
3667       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3668         xop1 = force_reg (SImode, xop1);
3669
3670       if (ARM_BASE_REGISTER_RTX_P (xop0)
3671           && GET_CODE (xop1) == CONST_INT)
3672         {
3673           HOST_WIDE_INT n, low_n;
3674           rtx base_reg, val;
3675           n = INTVAL (xop1);
3676
3677           /* VFP addressing modes actually allow greater offsets, but for
3678              now we just stick with the lowest common denominator.  */
3679           if (mode == DImode
3680               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3681             {
3682               low_n = n & 0x0f;
3683               n &= ~0x0f;
3684               if (low_n > 4)
3685                 {
3686                   n += 16;
3687                   low_n -= 16;
3688                 }
3689             }
3690           else
3691             {
3692               low_n = ((mode) == TImode ? 0
3693                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3694               n -= low_n;
3695             }
3696
3697           base_reg = gen_reg_rtx (SImode);
3698           val = force_operand (gen_rtx_PLUS (SImode, xop0,
3699                                              GEN_INT (n)), NULL_RTX);
3700           emit_move_insn (base_reg, val);
3701           x = (low_n == 0 ? base_reg
3702                : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3703         }
3704       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3705         x = gen_rtx_PLUS (SImode, xop0, xop1);
3706     }
3707
3708   /* XXX We don't allow MINUS any more -- see comment in
3709      arm_legitimate_address_p ().  */
3710   else if (GET_CODE (x) == MINUS)
3711     {
3712       rtx xop0 = XEXP (x, 0);
3713       rtx xop1 = XEXP (x, 1);
3714
3715       if (CONSTANT_P (xop0))
3716         xop0 = force_reg (SImode, xop0);
3717
3718       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3719         xop1 = force_reg (SImode, xop1);
3720
3721       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3722         x = gen_rtx_MINUS (SImode, xop0, xop1);
3723     }
3724
3725   if (flag_pic)
3726     {
3727       /* We need to find and carefully transform any SYMBOL and LABEL
3728          references; so go back to the original address expression.  */
3729       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3730
3731       if (new_x != orig_x)
3732         x = new_x;
3733     }
3734
3735   return x;
3736 }
3737
3738
3739 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3740    to be legitimate.  If we find one, return the new, valid address.  */
3741 rtx
3742 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3743 {
3744   if (GET_CODE (x) == PLUS
3745       && GET_CODE (XEXP (x, 1)) == CONST_INT
3746       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3747           || INTVAL (XEXP (x, 1)) < 0))
3748     {
3749       rtx xop0 = XEXP (x, 0);
3750       rtx xop1 = XEXP (x, 1);
3751       HOST_WIDE_INT offset = INTVAL (xop1);
3752
3753       /* Try and fold the offset into a biasing of the base register and
3754          then offsetting that.  Don't do this when optimizing for space
3755          since it can cause too many CSEs.  */
3756       if (optimize_size && offset >= 0
3757           && offset < 256 + 31 * GET_MODE_SIZE (mode))
3758         {
3759           HOST_WIDE_INT delta;
3760
3761           if (offset >= 256)
3762             delta = offset - (256 - GET_MODE_SIZE (mode));
3763           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3764             delta = 31 * GET_MODE_SIZE (mode);
3765           else
3766             delta = offset & (~31 * GET_MODE_SIZE (mode));
3767
3768           xop0 = force_operand (plus_constant (xop0, offset - delta),
3769                                 NULL_RTX);
3770           x = plus_constant (xop0, delta);
3771         }
3772       else if (offset < 0 && offset > -256)
3773         /* Small negative offsets are best done with a subtract before the
3774            dereference, forcing these into a register normally takes two
3775            instructions.  */
3776         x = force_operand (x, NULL_RTX);
3777       else
3778         {
3779           /* For the remaining cases, force the constant into a register.  */
3780           xop1 = force_reg (SImode, xop1);
3781           x = gen_rtx_PLUS (SImode, xop0, xop1);
3782         }
3783     }
3784   else if (GET_CODE (x) == PLUS
3785            && s_register_operand (XEXP (x, 1), SImode)
3786            && !s_register_operand (XEXP (x, 0), SImode))
3787     {
3788       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3789
3790       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3791     }
3792
3793   if (flag_pic)
3794     {
3795       /* We need to find and carefully transform any SYMBOL and LABEL
3796          references; so go back to the original address expression.  */
3797       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3798
3799       if (new_x != orig_x)
3800         x = new_x;
3801     }
3802
3803   return x;
3804 }
3805
3806 \f
3807
3808 #define REG_OR_SUBREG_REG(X)                                            \
3809   (GET_CODE (X) == REG                                                  \
3810    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3811
3812 #define REG_OR_SUBREG_RTX(X)                    \
3813    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3814
3815 #ifndef COSTS_N_INSNS
3816 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3817 #endif
3818 static inline int
3819 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3820 {
3821   enum machine_mode mode = GET_MODE (x);
3822
3823   switch (code)
3824     {
3825     case ASHIFT:
3826     case ASHIFTRT:
3827     case LSHIFTRT:
3828     case ROTATERT:
3829     case PLUS:
3830     case MINUS:
3831     case COMPARE:
3832     case NEG:
3833     case NOT:
3834       return COSTS_N_INSNS (1);
3835
3836     case MULT:
3837       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3838         {
3839           int cycles = 0;
3840           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3841
3842           while (i)
3843             {
3844               i >>= 2;
3845               cycles++;
3846             }
3847           return COSTS_N_INSNS (2) + cycles;
3848         }
3849       return COSTS_N_INSNS (1) + 16;
3850
3851     case SET:
3852       return (COSTS_N_INSNS (1)
3853               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3854                      + GET_CODE (SET_DEST (x)) == MEM));
3855
3856     case CONST_INT:
3857       if (outer == SET)
3858         {
3859           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3860             return 0;
3861           if (thumb_shiftable_const (INTVAL (x)))
3862             return COSTS_N_INSNS (2);
3863           return COSTS_N_INSNS (3);
3864         }
3865       else if ((outer == PLUS || outer == COMPARE)
3866                && INTVAL (x) < 256 && INTVAL (x) > -256)
3867         return 0;
3868       else if (outer == AND
3869                && INTVAL (x) < 256 && INTVAL (x) >= -256)
3870         return COSTS_N_INSNS (1);
3871       else if (outer == ASHIFT || outer == ASHIFTRT
3872                || outer == LSHIFTRT)
3873         return 0;
3874       return COSTS_N_INSNS (2);
3875
3876     case CONST:
3877     case CONST_DOUBLE:
3878     case LABEL_REF:
3879     case SYMBOL_REF:
3880       return COSTS_N_INSNS (3);
3881
3882     case UDIV:
3883     case UMOD:
3884     case DIV:
3885     case MOD:
3886       return 100;
3887
3888     case TRUNCATE:
3889       return 99;
3890
3891     case AND:
3892     case XOR:
3893     case IOR:
3894       /* XXX guess.  */
3895       return 8;
3896
3897     case MEM:
3898       /* XXX another guess.  */
3899       /* Memory costs quite a lot for the first word, but subsequent words
3900          load at the equivalent of a single insn each.  */
3901       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3902               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3903                  ? 4 : 0));
3904
3905     case IF_THEN_ELSE:
3906       /* XXX a guess.  */
3907       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3908         return 14;
3909       return 2;
3910
3911     case ZERO_EXTEND:
3912       /* XXX still guessing.  */
3913       switch (GET_MODE (XEXP (x, 0)))
3914         {
3915         case QImode:
3916           return (1 + (mode == DImode ? 4 : 0)
3917                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3918
3919         case HImode:
3920           return (4 + (mode == DImode ? 4 : 0)
3921                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3922
3923         case SImode:
3924           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3925
3926         default:
3927           return 99;
3928         }
3929
3930     default:
3931       return 99;
3932     }
3933 }
3934
3935
3936 /* Worker routine for arm_rtx_costs.  */
3937 static inline int
3938 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3939 {
3940   enum machine_mode mode = GET_MODE (x);
3941   enum rtx_code subcode;
3942   int extra_cost;
3943
3944   switch (code)
3945     {
3946     case MEM:
3947       /* Memory costs quite a lot for the first word, but subsequent words
3948          load at the equivalent of a single insn each.  */
3949       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3950               + (GET_CODE (x) == SYMBOL_REF
3951                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3952
3953     case DIV:
3954     case MOD:
3955     case UDIV:
3956     case UMOD:
3957       return optimize_size ? COSTS_N_INSNS (2) : 100;
3958
3959     case ROTATE:
3960       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3961         return 4;
3962       /* Fall through */
3963     case ROTATERT:
3964       if (mode != SImode)
3965         return 8;
3966       /* Fall through */
3967     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3968       if (mode == DImode)
3969         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3970                 + ((GET_CODE (XEXP (x, 0)) == REG
3971                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3972                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3973                    ? 0 : 8));
3974       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3975                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3976                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3977                    ? 0 : 4)
3978               + ((GET_CODE (XEXP (x, 1)) == REG
3979                   || (GET_CODE (XEXP (x, 1)) == SUBREG
3980                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3981                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3982                  ? 0 : 4));
3983
3984     case MINUS:
3985       if (mode == DImode)
3986         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3987                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3988                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
3989                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3990                    ? 0 : 8));
3991
3992       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3993         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3994                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3995                           && arm_const_double_rtx (XEXP (x, 1))))
3996                      ? 0 : 8)
3997                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3998                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3999                         && arm_const_double_rtx (XEXP (x, 0))))
4000                    ? 0 : 8));
4001
4002       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
4003             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
4004             && REG_OR_SUBREG_REG (XEXP (x, 1))))
4005           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
4006                || subcode == ASHIFTRT || subcode == LSHIFTRT
4007                || subcode == ROTATE || subcode == ROTATERT
4008                || (subcode == MULT
4009                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4010                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
4011                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4012               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4013               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4014                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4015               && REG_OR_SUBREG_REG (XEXP (x, 0))))
4016         return 1;
4017       /* Fall through */
4018
4019     case PLUS:
4020       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4021         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4022                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4023                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4024                         && arm_const_double_rtx (XEXP (x, 1))))
4025                    ? 0 : 8));
4026
4027       /* Fall through */
4028     case AND: case XOR: case IOR:
4029       extra_cost = 0;
4030
4031       /* Normally the frame registers will be spilt into reg+const during
4032          reload, so it is a bad idea to combine them with other instructions,
4033          since then they might not be moved outside of loops.  As a compromise
4034          we allow integration with ops that have a constant as their second
4035          operand.  */
4036       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
4037            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
4038            && GET_CODE (XEXP (x, 1)) != CONST_INT)
4039           || (REG_OR_SUBREG_REG (XEXP (x, 0))
4040               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
4041         extra_cost = 4;
4042
4043       if (mode == DImode)
4044         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4045                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4046                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4047                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4048                    ? 0 : 8));
4049
4050       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
4051         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
4052                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4053                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4054                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4055                    ? 0 : 4));
4056
4057       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
4058         return (1 + extra_cost
4059                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
4060                      || subcode == LSHIFTRT || subcode == ASHIFTRT
4061                      || subcode == ROTATE || subcode == ROTATERT
4062                      || (subcode == MULT
4063                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4064                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
4065                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
4066                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
4067                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
4068                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4069                    ? 0 : 4));
4070
4071       return 8;
4072
4073     case MULT:
4074       /* This should have been handled by the CPU specific routines.  */
4075       gcc_unreachable ();
4076
4077     case TRUNCATE:
4078       if (arm_arch3m && mode == SImode
4079           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4080           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4081           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
4082               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4083           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4084               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
4085         return 8;
4086       return 99;
4087
4088     case NEG:
4089       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4090         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4091       /* Fall through */
4092     case NOT:
4093       if (mode == DImode)
4094         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4095
4096       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4097
4098     case IF_THEN_ELSE:
4099       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4100         return 14;
4101       return 2;
4102
4103     case COMPARE:
4104       return 1;
4105
4106     case ABS:
4107       return 4 + (mode == DImode ? 4 : 0);
4108
4109     case SIGN_EXTEND:
4110       if (GET_MODE (XEXP (x, 0)) == QImode)
4111         return (4 + (mode == DImode ? 4 : 0)
4112                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4113       /* Fall through */
4114     case ZERO_EXTEND:
4115       switch (GET_MODE (XEXP (x, 0)))
4116         {
4117         case QImode:
4118           return (1 + (mode == DImode ? 4 : 0)
4119                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4120
4121         case HImode:
4122           return (4 + (mode == DImode ? 4 : 0)
4123                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4124
4125         case SImode:
4126           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4127
4128         case V8QImode:
4129         case V4HImode:
4130         case V2SImode:
4131         case V4QImode:
4132         case V2HImode:
4133             return 1;
4134
4135         default:
4136           gcc_unreachable ();
4137         }
4138       gcc_unreachable ();
4139
4140     case CONST_INT:
4141       if (const_ok_for_arm (INTVAL (x)))
4142         return outer == SET ? 2 : -1;
4143       else if (outer == AND
4144                && const_ok_for_arm (~INTVAL (x)))
4145         return -1;
4146       else if ((outer == COMPARE
4147                 || outer == PLUS || outer == MINUS)
4148                && const_ok_for_arm (-INTVAL (x)))
4149         return -1;
4150       else
4151         return 5;
4152
4153     case CONST:
4154     case LABEL_REF:
4155     case SYMBOL_REF:
4156       return 6;
4157
4158     case CONST_DOUBLE:
4159       if (arm_const_double_rtx (x))
4160         return outer == SET ? 2 : -1;
4161       else if ((outer == COMPARE || outer == PLUS)
4162                && neg_const_double_rtx_ok_for_fpa (x))
4163         return -1;
4164       return 7;
4165
4166     default:
4167       return 99;
4168     }
4169 }
4170
4171 /* RTX costs when optimizing for size.  */
4172 static bool
4173 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4174 {
4175   enum machine_mode mode = GET_MODE (x);
4176
4177   if (TARGET_THUMB)
4178     {
4179       /* XXX TBD.  For now, use the standard costs.  */
4180       *total = thumb_rtx_costs (x, code, outer_code);
4181       return true;
4182     }
4183
4184   switch (code)
4185     {
4186     case MEM:
4187       /* A memory access costs 1 insn if the mode is small, or the address is
4188          a single register, otherwise it costs one insn per word.  */
4189       if (REG_P (XEXP (x, 0)))
4190         *total = COSTS_N_INSNS (1);
4191       else
4192         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4193       return true;
4194
4195     case DIV:
4196     case MOD:
4197     case UDIV:
4198     case UMOD:
4199       /* Needs a libcall, so it costs about this.  */
4200       *total = COSTS_N_INSNS (2);
4201       return false;
4202
4203     case ROTATE:
4204       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4205         {
4206           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4207           return true;
4208         }
4209       /* Fall through */
4210     case ROTATERT:
4211     case ASHIFT:
4212     case LSHIFTRT:
4213     case ASHIFTRT:
4214       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4215         {
4216           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4217           return true;
4218         }
4219       else if (mode == SImode)
4220         {
4221           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4222           /* Slightly disparage register shifts, but not by much.  */
4223           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4224             *total += 1 + rtx_cost (XEXP (x, 1), code);
4225           return true;
4226         }
4227
4228       /* Needs a libcall.  */
4229       *total = COSTS_N_INSNS (2);
4230       return false;
4231
4232     case MINUS:
4233       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4234         {
4235           *total = COSTS_N_INSNS (1);
4236           return false;
4237         }
4238
4239       if (mode == SImode)
4240         {
4241           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4242           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4243
4244           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4245               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4246               || subcode1 == ROTATE || subcode1 == ROTATERT
4247               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4248               || subcode1 == ASHIFTRT)
4249             {
4250               /* It's just the cost of the two operands.  */
4251               *total = 0;
4252               return false;
4253             }
4254
4255           *total = COSTS_N_INSNS (1);
4256           return false;
4257         }
4258
4259       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4260       return false;
4261
4262     case PLUS:
4263       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4264         {
4265           *total = COSTS_N_INSNS (1);
4266           return false;
4267         }
4268
4269       /* Fall through */
4270     case AND: case XOR: case IOR:
4271       if (mode == SImode)
4272         {
4273           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4274
4275           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4276               || subcode == LSHIFTRT || subcode == ASHIFTRT
4277               || (code == AND && subcode == NOT))
4278             {
4279               /* It's just the cost of the two operands.  */
4280               *total = 0;
4281               return false;
4282             }
4283         }
4284
4285       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4286       return false;
4287
4288     case MULT:
4289       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4290       return false;
4291
4292     case NEG:
4293       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4294         *total = COSTS_N_INSNS (1);
4295       /* Fall through */
4296     case NOT:
4297       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4298
4299       return false;
4300
4301     case IF_THEN_ELSE:
4302       *total = 0;
4303       return false;
4304
4305     case COMPARE:
4306       if (cc_register (XEXP (x, 0), VOIDmode))
4307         * total = 0;
4308       else
4309         *total = COSTS_N_INSNS (1);
4310       return false;
4311
4312     case ABS:
4313       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4314         *total = COSTS_N_INSNS (1);
4315       else
4316         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4317       return false;
4318
4319     case SIGN_EXTEND:
4320       *total = 0;
4321       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4322         {
4323           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4324             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4325         }
4326       if (mode == DImode)
4327         *total += COSTS_N_INSNS (1);
4328       return false;
4329
4330     case ZERO_EXTEND:
4331       *total = 0;
4332       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4333         {
4334           switch (GET_MODE (XEXP (x, 0)))
4335             {
4336             case QImode:
4337               *total += COSTS_N_INSNS (1);
4338               break;
4339
4340             case HImode:
4341               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4342
4343             case SImode:
4344               break;
4345
4346             default:
4347               *total += COSTS_N_INSNS (2);
4348             }
4349         }
4350
4351       if (mode == DImode)
4352         *total += COSTS_N_INSNS (1);
4353
4354       return false;
4355
4356     case CONST_INT:
4357       if (const_ok_for_arm (INTVAL (x)))
4358         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4359       else if (const_ok_for_arm (~INTVAL (x)))
4360         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4361       else if (const_ok_for_arm (-INTVAL (x)))
4362         {
4363           if (outer_code == COMPARE || outer_code == PLUS
4364               || outer_code == MINUS)
4365             *total = 0;
4366           else
4367             *total = COSTS_N_INSNS (1);
4368         }
4369       else
4370         *total = COSTS_N_INSNS (2);
4371       return true;
4372
4373     case CONST:
4374     case LABEL_REF:
4375     case SYMBOL_REF:
4376       *total = COSTS_N_INSNS (2);
4377       return true;
4378
4379     case CONST_DOUBLE:
4380       *total = COSTS_N_INSNS (4);
4381       return true;
4382
4383     default:
4384       if (mode != VOIDmode)
4385         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4386       else
4387         *total = COSTS_N_INSNS (4); /* How knows?  */
4388       return false;
4389     }
4390 }
4391
4392 /* RTX costs for cores with a slow MUL implementation.  */
4393
4394 static bool
4395 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4396 {
4397   enum machine_mode mode = GET_MODE (x);
4398
4399   if (TARGET_THUMB)
4400     {
4401       *total = thumb_rtx_costs (x, code, outer_code);
4402       return true;
4403     }
4404
4405   switch (code)
4406     {
4407     case MULT:
4408       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4409           || mode == DImode)
4410         {
4411           *total = 30;
4412           return true;
4413         }
4414
4415       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4416         {
4417           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4418                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4419           int cost, const_ok = const_ok_for_arm (i);
4420           int j, booth_unit_size;
4421
4422           /* Tune as appropriate.  */
4423           cost = const_ok ? 4 : 8;
4424           booth_unit_size = 2;
4425           for (j = 0; i && j < 32; j += booth_unit_size)
4426             {
4427               i >>= booth_unit_size;
4428               cost += 2;
4429             }
4430
4431           *total = cost;
4432           return true;
4433         }
4434
4435       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4436                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4437       return true;
4438
4439     default:
4440       *total = arm_rtx_costs_1 (x, code, outer_code);
4441       return true;
4442     }
4443 }
4444
4445
4446 /* RTX cost for cores with a fast multiply unit (M variants).  */
4447
4448 static bool
4449 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4450 {
4451   enum machine_mode mode = GET_MODE (x);
4452
4453   if (TARGET_THUMB)
4454     {
4455       *total = thumb_rtx_costs (x, code, outer_code);
4456       return true;
4457     }
4458
4459   switch (code)
4460     {
4461     case MULT:
4462       /* There is no point basing this on the tuning, since it is always the
4463          fast variant if it exists at all.  */
4464       if (mode == DImode
4465           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4466           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4467               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4468         {
4469           *total = 8;
4470           return true;
4471         }
4472
4473
4474       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4475           || mode == DImode)
4476         {
4477           *total = 30;
4478           return true;
4479         }
4480
4481       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4482         {
4483           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4484                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4485           int cost, const_ok = const_ok_for_arm (i);
4486           int j, booth_unit_size;
4487
4488           /* Tune as appropriate.  */
4489           cost = const_ok ? 4 : 8;
4490           booth_unit_size = 8;
4491           for (j = 0; i && j < 32; j += booth_unit_size)
4492             {
4493               i >>= booth_unit_size;
4494               cost += 2;
4495             }
4496
4497           *total = cost;
4498           return true;
4499         }
4500
4501       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4502                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4503       return true;
4504
4505     default:
4506       *total = arm_rtx_costs_1 (x, code, outer_code);
4507       return true;
4508     }
4509 }
4510
4511
4512 /* RTX cost for XScale CPUs.  */
4513
4514 static bool
4515 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4516 {
4517   enum machine_mode mode = GET_MODE (x);
4518
4519   if (TARGET_THUMB)
4520     {
4521       *total = thumb_rtx_costs (x, code, outer_code);
4522       return true;
4523     }
4524
4525   switch (code)
4526     {
4527     case MULT:
4528       /* There is no point basing this on the tuning, since it is always the
4529          fast variant if it exists at all.  */
4530       if (mode == DImode
4531           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4532           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4533               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4534         {
4535           *total = 8;
4536           return true;
4537         }
4538
4539
4540       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4541           || mode == DImode)
4542         {
4543           *total = 30;
4544           return true;
4545         }
4546
4547       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4548         {
4549           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4550                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4551           int cost, const_ok = const_ok_for_arm (i);
4552           unsigned HOST_WIDE_INT masked_const;
4553
4554           /* The cost will be related to two insns.
4555              First a load of the constant (MOV or LDR), then a multiply.  */
4556           cost = 2;
4557           if (! const_ok)
4558             cost += 1;      /* LDR is probably more expensive because
4559                                of longer result latency.  */
4560           masked_const = i & 0xffff8000;
4561           if (masked_const != 0 && masked_const != 0xffff8000)
4562             {
4563               masked_const = i & 0xf8000000;
4564               if (masked_const == 0 || masked_const == 0xf8000000)
4565                 cost += 1;
4566               else
4567                 cost += 2;
4568             }
4569           *total = cost;
4570           return true;
4571         }
4572
4573       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4574                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4575       return true;
4576
4577     case COMPARE:
4578       /* A COMPARE of a MULT is slow on XScale; the muls instruction
4579          will stall until the multiplication is complete.  */
4580       if (GET_CODE (XEXP (x, 0)) == MULT)
4581         *total = 4 + rtx_cost (XEXP (x, 0), code);
4582       else
4583         *total = arm_rtx_costs_1 (x, code, outer_code);
4584       return true;
4585
4586     default:
4587       *total = arm_rtx_costs_1 (x, code, outer_code);
4588       return true;
4589     }
4590 }
4591
4592
4593 /* RTX costs for 9e (and later) cores.  */
4594
4595 static bool
4596 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4597 {
4598   enum machine_mode mode = GET_MODE (x);
4599   int nonreg_cost;
4600   int cost;
4601
4602   if (TARGET_THUMB)
4603     {
4604       switch (code)
4605         {
4606         case MULT:
4607           *total = COSTS_N_INSNS (3);
4608           return true;
4609
4610         default:
4611           *total = thumb_rtx_costs (x, code, outer_code);
4612           return true;
4613         }
4614     }
4615
4616   switch (code)
4617     {
4618     case MULT:
4619       /* There is no point basing this on the tuning, since it is always the
4620          fast variant if it exists at all.  */
4621       if (mode == DImode
4622           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4623           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4624               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4625         {
4626           *total = 3;
4627           return true;
4628         }
4629
4630
4631       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4632         {
4633           *total = 30;
4634           return true;
4635         }
4636       if (mode == DImode)
4637         {
4638           cost = 7;
4639           nonreg_cost = 8;
4640         }
4641       else
4642         {
4643           cost = 2;
4644           nonreg_cost = 4;
4645         }
4646
4647
4648       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4649                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4650       return true;
4651
4652     default:
4653       *total = arm_rtx_costs_1 (x, code, outer_code);
4654       return true;
4655     }
4656 }
4657 /* All address computations that can be done are free, but rtx cost returns
4658    the same for practically all of them.  So we weight the different types
4659    of address here in the order (most pref first):
4660    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
4661 static inline int
4662 arm_arm_address_cost (rtx x)
4663 {
4664   enum rtx_code c  = GET_CODE (x);
4665
4666   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4667     return 0;
4668   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4669     return 10;
4670
4671   if (c == PLUS || c == MINUS)
4672     {
4673       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4674         return 2;
4675
4676       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4677         return 3;
4678
4679       return 4;
4680     }
4681
4682   return 6;
4683 }
4684
4685 static inline int
4686 arm_thumb_address_cost (rtx x)
4687 {
4688   enum rtx_code c  = GET_CODE (x);
4689
4690   if (c == REG)
4691     return 1;
4692   if (c == PLUS
4693       && GET_CODE (XEXP (x, 0)) == REG
4694       && GET_CODE (XEXP (x, 1)) == CONST_INT)
4695     return 1;
4696
4697   return 2;
4698 }
4699
4700 static int
4701 arm_address_cost (rtx x)
4702 {
4703   return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4704 }
4705
4706 static int
4707 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4708 {
4709   rtx i_pat, d_pat;
4710
4711   /* Some true dependencies can have a higher cost depending
4712      on precisely how certain input operands are used.  */
4713   if (arm_tune_xscale
4714       && REG_NOTE_KIND (link) == 0
4715       && recog_memoized (insn) >= 0
4716       && recog_memoized (dep) >= 0)
4717     {
4718       int shift_opnum = get_attr_shift (insn);
4719       enum attr_type attr_type = get_attr_type (dep);
4720
4721       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4722          operand for INSN.  If we have a shifted input operand and the
4723          instruction we depend on is another ALU instruction, then we may
4724          have to account for an additional stall.  */
4725       if (shift_opnum != 0
4726           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4727         {
4728           rtx shifted_operand;
4729           int opno;
4730
4731           /* Get the shifted operand.  */
4732           extract_insn (insn);
4733           shifted_operand = recog_data.operand[shift_opnum];
4734
4735           /* Iterate over all the operands in DEP.  If we write an operand
4736              that overlaps with SHIFTED_OPERAND, then we have increase the
4737              cost of this dependency.  */
4738           extract_insn (dep);
4739           preprocess_constraints ();
4740           for (opno = 0; opno < recog_data.n_operands; opno++)
4741             {
4742               /* We can ignore strict inputs.  */
4743               if (recog_data.operand_type[opno] == OP_IN)
4744                 continue;
4745
4746               if (reg_overlap_mentioned_p (recog_data.operand[opno],
4747                                            shifted_operand))
4748                 return 2;
4749             }
4750         }
4751     }
4752
4753   /* XXX This is not strictly true for the FPA.  */
4754   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4755       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4756     return 0;
4757
4758   /* Call insns don't incur a stall, even if they follow a load.  */
4759   if (REG_NOTE_KIND (link) == 0
4760       && GET_CODE (insn) == CALL_INSN)
4761     return 1;
4762
4763   if ((i_pat = single_set (insn)) != NULL
4764       && GET_CODE (SET_SRC (i_pat)) == MEM
4765       && (d_pat = single_set (dep)) != NULL
4766       && GET_CODE (SET_DEST (d_pat)) == MEM)
4767     {
4768       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4769       /* This is a load after a store, there is no conflict if the load reads
4770          from a cached area.  Assume that loads from the stack, and from the
4771          constant pool are cached, and that others will miss.  This is a
4772          hack.  */
4773
4774       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4775           || reg_mentioned_p (stack_pointer_rtx, src_mem)
4776           || reg_mentioned_p (frame_pointer_rtx, src_mem)
4777           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4778         return 1;
4779     }
4780
4781   return cost;
4782 }
4783
4784 static int fp_consts_inited = 0;
4785
4786 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
4787 static const char * const strings_fp[8] =
4788 {
4789   "0",   "1",   "2",   "3",
4790   "4",   "5",   "0.5", "10"
4791 };
4792
4793 static REAL_VALUE_TYPE values_fp[8];
4794
4795 static void
4796 init_fp_table (void)
4797 {
4798   int i;
4799   REAL_VALUE_TYPE r;
4800
4801   if (TARGET_VFP)
4802     fp_consts_inited = 1;
4803   else
4804     fp_consts_inited = 8;
4805
4806   for (i = 0; i < fp_consts_inited; i++)
4807     {
4808       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4809       values_fp[i] = r;
4810     }
4811 }
4812
4813 /* Return TRUE if rtx X is a valid immediate FP constant.  */
4814 int
4815 arm_const_double_rtx (rtx x)
4816 {
4817   REAL_VALUE_TYPE r;
4818   int i;
4819
4820   if (!fp_consts_inited)
4821     init_fp_table ();
4822
4823   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4824   if (REAL_VALUE_MINUS_ZERO (r))
4825     return 0;
4826
4827   for (i = 0; i < fp_consts_inited; i++)
4828     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4829       return 1;
4830
4831   return 0;
4832 }
4833
4834 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
4835 int
4836 neg_const_double_rtx_ok_for_fpa (rtx x)
4837 {
4838   REAL_VALUE_TYPE r;
4839   int i;
4840
4841   if (!fp_consts_inited)
4842     init_fp_table ();
4843
4844   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4845   r = REAL_VALUE_NEGATE (r);
4846   if (REAL_VALUE_MINUS_ZERO (r))
4847     return 0;
4848
4849   for (i = 0; i < 8; i++)
4850     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4851       return 1;
4852
4853   return 0;
4854 }
4855 \f
4856 /* Predicates for `match_operand' and `match_operator'.  */
4857
4858 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
4859 int
4860 cirrus_memory_offset (rtx op)
4861 {
4862   /* Reject eliminable registers.  */
4863   if (! (reload_in_progress || reload_completed)
4864       && (   reg_mentioned_p (frame_pointer_rtx, op)
4865           || reg_mentioned_p (arg_pointer_rtx, op)
4866           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4867           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4868           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4869           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4870     return 0;
4871
4872   if (GET_CODE (op) == MEM)
4873     {
4874       rtx ind;
4875
4876       ind = XEXP (op, 0);
4877
4878       /* Match: (mem (reg)).  */
4879       if (GET_CODE (ind) == REG)
4880         return 1;
4881
4882       /* Match:
4883          (mem (plus (reg)
4884                     (const))).  */
4885       if (GET_CODE (ind) == PLUS
4886           && GET_CODE (XEXP (ind, 0)) == REG
4887           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4888           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4889         return 1;
4890     }
4891
4892   return 0;
4893 }
4894
4895 /* Return TRUE if OP is a valid VFP memory address pattern.
4896    WB if true if writeback address modes are allowed.  */
4897
4898 int
4899 arm_coproc_mem_operand (rtx op, bool wb)
4900 {
4901   rtx ind;
4902
4903   /* Reject eliminable registers.  */
4904   if (! (reload_in_progress || reload_completed)
4905       && (   reg_mentioned_p (frame_pointer_rtx, op)
4906           || reg_mentioned_p (arg_pointer_rtx, op)
4907           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4908           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4909           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4910           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4911     return FALSE;
4912
4913   /* Constants are converted into offsets from labels.  */
4914   if (GET_CODE (op) != MEM)
4915     return FALSE;
4916
4917   ind = XEXP (op, 0);
4918
4919   if (reload_completed
4920       && (GET_CODE (ind) == LABEL_REF
4921           || (GET_CODE (ind) == CONST
4922               && GET_CODE (XEXP (ind, 0)) == PLUS
4923               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4924               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4925     return TRUE;
4926
4927   /* Match: (mem (reg)).  */
4928   if (GET_CODE (ind) == REG)
4929     return arm_address_register_rtx_p (ind, 0);
4930
4931   /* Autoincremment addressing modes.  */
4932   if (wb
4933       && (GET_CODE (ind) == PRE_INC
4934           || GET_CODE (ind) == POST_INC
4935           || GET_CODE (ind) == PRE_DEC
4936           || GET_CODE (ind) == POST_DEC))
4937     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
4938
4939   if (wb
4940       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
4941       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
4942       && GET_CODE (XEXP (ind, 1)) == PLUS
4943       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
4944     ind = XEXP (ind, 1);
4945
4946   /* Match:
4947      (plus (reg)
4948            (const)).  */
4949   if (GET_CODE (ind) == PLUS
4950       && GET_CODE (XEXP (ind, 0)) == REG
4951       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4952       && GET_CODE (XEXP (ind, 1)) == CONST_INT
4953       && INTVAL (XEXP (ind, 1)) > -1024
4954       && INTVAL (XEXP (ind, 1)) <  1024
4955       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
4956     return TRUE;
4957
4958   return FALSE;
4959 }
4960
4961 /* Return true if X is a register that will be eliminated later on.  */
4962 int
4963 arm_eliminable_register (rtx x)
4964 {
4965   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
4966                        || REGNO (x) == ARG_POINTER_REGNUM
4967                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
4968                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
4969 }
4970
4971 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4972    VFP registers.  Otherwise return NO_REGS.  */
4973
4974 enum reg_class
4975 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4976 {
4977   if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
4978     return NO_REGS;
4979
4980   return GENERAL_REGS;
4981 }
4982
4983 /* Values which must be returned in the most-significant end of the return
4984    register.  */
4985
4986 static bool
4987 arm_return_in_msb (tree valtype)
4988 {
4989   return (TARGET_AAPCS_BASED
4990           && BYTES_BIG_ENDIAN
4991           && (AGGREGATE_TYPE_P (valtype)
4992               || TREE_CODE (valtype) == COMPLEX_TYPE));
4993 }
4994
4995 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4996    Use by the Cirrus Maverick code which has to workaround
4997    a hardware bug triggered by such instructions.  */
4998 static bool
4999 arm_memory_load_p (rtx insn)
5000 {
5001   rtx body, lhs, rhs;;
5002
5003   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
5004     return false;
5005
5006   body = PATTERN (insn);
5007
5008   if (GET_CODE (body) != SET)
5009     return false;
5010
5011   lhs = XEXP (body, 0);
5012   rhs = XEXP (body, 1);
5013
5014   lhs = REG_OR_SUBREG_RTX (lhs);
5015
5016   /* If the destination is not a general purpose
5017      register we do not have to worry.  */
5018   if (GET_CODE (lhs) != REG
5019       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
5020     return false;
5021
5022   /* As well as loads from memory we also have to react
5023      to loads of invalid constants which will be turned
5024      into loads from the minipool.  */
5025   return (GET_CODE (rhs) == MEM
5026           || GET_CODE (rhs) == SYMBOL_REF
5027           || note_invalid_constants (insn, -1, false));
5028 }
5029
5030 /* Return TRUE if INSN is a Cirrus instruction.  */
5031 static bool
5032 arm_cirrus_insn_p (rtx insn)
5033 {
5034   enum attr_cirrus attr;
5035
5036   /* get_attr cannot accept USE or CLOBBER.  */
5037   if (!insn
5038       || GET_CODE (insn) != INSN
5039       || GET_CODE (PATTERN (insn)) == USE
5040       || GET_CODE (PATTERN (insn)) == CLOBBER)
5041     return 0;
5042
5043   attr = get_attr_cirrus (insn);
5044
5045   return attr != CIRRUS_NOT;
5046 }
5047
5048 /* Cirrus reorg for invalid instruction combinations.  */
5049 static void
5050 cirrus_reorg (rtx first)
5051 {
5052   enum attr_cirrus attr;
5053   rtx body = PATTERN (first);
5054   rtx t;
5055   int nops;
5056
5057   /* Any branch must be followed by 2 non Cirrus instructions.  */
5058   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
5059     {
5060       nops = 0;
5061       t = next_nonnote_insn (first);
5062
5063       if (arm_cirrus_insn_p (t))
5064         ++ nops;
5065
5066       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5067         ++ nops;
5068
5069       while (nops --)
5070         emit_insn_after (gen_nop (), first);
5071
5072       return;
5073     }
5074
5075   /* (float (blah)) is in parallel with a clobber.  */
5076   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
5077     body = XVECEXP (body, 0, 0);
5078
5079   if (GET_CODE (body) == SET)
5080     {
5081       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
5082
5083       /* cfldrd, cfldr64, cfstrd, cfstr64 must
5084          be followed by a non Cirrus insn.  */
5085       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
5086         {
5087           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
5088             emit_insn_after (gen_nop (), first);
5089
5090           return;
5091         }
5092       else if (arm_memory_load_p (first))
5093         {
5094           unsigned int arm_regno;
5095
5096           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
5097              ldr/cfmv64hr combination where the Rd field is the same
5098              in both instructions must be split with a non Cirrus
5099              insn.  Example:
5100
5101              ldr r0, blah
5102              nop
5103              cfmvsr mvf0, r0.  */
5104
5105           /* Get Arm register number for ldr insn.  */
5106           if (GET_CODE (lhs) == REG)
5107             arm_regno = REGNO (lhs);
5108           else
5109             {
5110               gcc_assert (GET_CODE (rhs) == REG);
5111               arm_regno = REGNO (rhs);
5112             }
5113
5114           /* Next insn.  */
5115           first = next_nonnote_insn (first);
5116
5117           if (! arm_cirrus_insn_p (first))
5118             return;
5119
5120           body = PATTERN (first);
5121
5122           /* (float (blah)) is in parallel with a clobber.  */
5123           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5124             body = XVECEXP (body, 0, 0);
5125
5126           if (GET_CODE (body) == FLOAT)
5127             body = XEXP (body, 0);
5128
5129           if (get_attr_cirrus (first) == CIRRUS_MOVE
5130               && GET_CODE (XEXP (body, 1)) == REG
5131               && arm_regno == REGNO (XEXP (body, 1)))
5132             emit_insn_after (gen_nop (), first);
5133
5134           return;
5135         }
5136     }
5137
5138   /* get_attr cannot accept USE or CLOBBER.  */
5139   if (!first
5140       || GET_CODE (first) != INSN
5141       || GET_CODE (PATTERN (first)) == USE
5142       || GET_CODE (PATTERN (first)) == CLOBBER)
5143     return;
5144
5145   attr = get_attr_cirrus (first);
5146
5147   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5148      must be followed by a non-coprocessor instruction.  */
5149   if (attr == CIRRUS_COMPARE)
5150     {
5151       nops = 0;
5152
5153       t = next_nonnote_insn (first);
5154
5155       if (arm_cirrus_insn_p (t))
5156         ++ nops;
5157
5158       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5159         ++ nops;
5160
5161       while (nops --)
5162         emit_insn_after (gen_nop (), first);
5163
5164       return;
5165     }
5166 }
5167
5168 /* Return TRUE if X references a SYMBOL_REF.  */
5169 int
5170 symbol_mentioned_p (rtx x)
5171 {
5172   const char * fmt;
5173   int i;
5174
5175   if (GET_CODE (x) == SYMBOL_REF)
5176     return 1;
5177
5178   fmt = GET_RTX_FORMAT (GET_CODE (x));
5179
5180   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5181     {
5182       if (fmt[i] == 'E')
5183         {
5184           int j;
5185
5186           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5187             if (symbol_mentioned_p (XVECEXP (x, i, j)))
5188               return 1;
5189         }
5190       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5191         return 1;
5192     }
5193
5194   return 0;
5195 }
5196
5197 /* Return TRUE if X references a LABEL_REF.  */
5198 int
5199 label_mentioned_p (rtx x)
5200 {
5201   const char * fmt;
5202   int i;
5203
5204   if (GET_CODE (x) == LABEL_REF)
5205     return 1;
5206
5207   fmt = GET_RTX_FORMAT (GET_CODE (x));
5208   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5209     {
5210       if (fmt[i] == 'E')
5211         {
5212           int j;
5213
5214           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5215             if (label_mentioned_p (XVECEXP (x, i, j)))
5216               return 1;
5217         }
5218       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5219         return 1;
5220     }
5221
5222   return 0;
5223 }
5224
5225 enum rtx_code
5226 minmax_code (rtx x)
5227 {
5228   enum rtx_code code = GET_CODE (x);
5229
5230   switch (code)
5231     {
5232     case SMAX:
5233       return GE;
5234     case SMIN:
5235       return LE;
5236     case UMIN:
5237       return LEU;
5238     case UMAX:
5239       return GEU;
5240     default:
5241       gcc_unreachable ();
5242     }
5243 }
5244
5245 /* Return 1 if memory locations are adjacent.  */
5246 int
5247 adjacent_mem_locations (rtx a, rtx b)
5248 {
5249   /* We don't guarantee to preserve the order of these memory refs.  */
5250   if (volatile_refs_p (a) || volatile_refs_p (b))
5251     return 0;
5252
5253   if ((GET_CODE (XEXP (a, 0)) == REG
5254        || (GET_CODE (XEXP (a, 0)) == PLUS
5255            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5256       && (GET_CODE (XEXP (b, 0)) == REG
5257           || (GET_CODE (XEXP (b, 0)) == PLUS
5258               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5259     {
5260       HOST_WIDE_INT val0 = 0, val1 = 0;
5261       rtx reg0, reg1;
5262       int val_diff;
5263
5264       if (GET_CODE (XEXP (a, 0)) == PLUS)
5265         {
5266           reg0 = XEXP (XEXP (a, 0), 0);
5267           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5268         }
5269       else
5270         reg0 = XEXP (a, 0);
5271
5272       if (GET_CODE (XEXP (b, 0)) == PLUS)
5273         {
5274           reg1 = XEXP (XEXP (b, 0), 0);
5275           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5276         }
5277       else
5278         reg1 = XEXP (b, 0);
5279
5280       /* Don't accept any offset that will require multiple
5281          instructions to handle, since this would cause the
5282          arith_adjacentmem pattern to output an overlong sequence.  */
5283       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5284         return 0;
5285
5286       /* Don't allow an eliminable register: register elimination can make
5287          the offset too large.  */
5288       if (arm_eliminable_register (reg0))
5289         return 0;
5290
5291       val_diff = val1 - val0;
5292
5293       if (arm_ld_sched)
5294         {
5295           /* If the target has load delay slots, then there's no benefit
5296              to using an ldm instruction unless the offset is zero and
5297              we are optimizing for size.  */
5298           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
5299                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
5300                   && (val_diff == 4 || val_diff == -4));
5301         }
5302
5303       return ((REGNO (reg0) == REGNO (reg1))
5304               && (val_diff == 4 || val_diff == -4));
5305     }
5306
5307   return 0;
5308 }
5309
5310 int
5311 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5312                         HOST_WIDE_INT *load_offset)
5313 {
5314   int unsorted_regs[4];
5315   HOST_WIDE_INT unsorted_offsets[4];
5316   int order[4];
5317   int base_reg = -1;
5318   int i;
5319
5320   /* Can only handle 2, 3, or 4 insns at present,
5321      though could be easily extended if required.  */
5322   gcc_assert (nops >= 2 && nops <= 4);
5323
5324   /* Loop over the operands and check that the memory references are
5325      suitable (i.e. immediate offsets from the same base register).  At
5326      the same time, extract the target register, and the memory
5327      offsets.  */
5328   for (i = 0; i < nops; i++)
5329     {
5330       rtx reg;
5331       rtx offset;
5332
5333       /* Convert a subreg of a mem into the mem itself.  */
5334       if (GET_CODE (operands[nops + i]) == SUBREG)
5335         operands[nops + i] = alter_subreg (operands + (nops + i));
5336
5337       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
5338
5339       /* Don't reorder volatile memory references; it doesn't seem worth
5340          looking for the case where the order is ok anyway.  */
5341       if (MEM_VOLATILE_P (operands[nops + i]))
5342         return 0;
5343
5344       offset = const0_rtx;
5345
5346       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5347            || (GET_CODE (reg) == SUBREG
5348                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5349           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5350               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5351                    == REG)
5352                   || (GET_CODE (reg) == SUBREG
5353                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5354               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5355                   == CONST_INT)))
5356         {
5357           if (i == 0)
5358             {
5359               base_reg = REGNO (reg);
5360               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5361                                   ? REGNO (operands[i])
5362                                   : REGNO (SUBREG_REG (operands[i])));
5363               order[0] = 0;
5364             }
5365           else
5366             {
5367               if (base_reg != (int) REGNO (reg))
5368                 /* Not addressed from the same base register.  */
5369                 return 0;
5370
5371               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5372                                   ? REGNO (operands[i])
5373                                   : REGNO (SUBREG_REG (operands[i])));
5374               if (unsorted_regs[i] < unsorted_regs[order[0]])
5375                 order[0] = i;
5376             }
5377
5378           /* If it isn't an integer register, or if it overwrites the
5379              base register but isn't the last insn in the list, then
5380              we can't do this.  */
5381           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5382               || (i != nops - 1 && unsorted_regs[i] == base_reg))
5383             return 0;
5384
5385           unsorted_offsets[i] = INTVAL (offset);
5386         }
5387       else
5388         /* Not a suitable memory address.  */
5389         return 0;
5390     }
5391
5392   /* All the useful information has now been extracted from the
5393      operands into unsorted_regs and unsorted_offsets; additionally,
5394      order[0] has been set to the lowest numbered register in the
5395      list.  Sort the registers into order, and check that the memory
5396      offsets are ascending and adjacent.  */
5397
5398   for (i = 1; i < nops; i++)
5399     {
5400       int j;
5401
5402       order[i] = order[i - 1];
5403       for (j = 0; j < nops; j++)
5404         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5405             && (order[i] == order[i - 1]
5406                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5407           order[i] = j;
5408
5409       /* Have we found a suitable register? if not, one must be used more
5410          than once.  */
5411       if (order[i] == order[i - 1])
5412         return 0;
5413
5414       /* Is the memory address adjacent and ascending? */
5415       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5416         return 0;
5417     }
5418
5419   if (base)
5420     {
5421       *base = base_reg;
5422
5423       for (i = 0; i < nops; i++)
5424         regs[i] = unsorted_regs[order[i]];
5425
5426       *load_offset = unsorted_offsets[order[0]];
5427     }
5428
5429   if (unsorted_offsets[order[0]] == 0)
5430     return 1; /* ldmia */
5431
5432   if (unsorted_offsets[order[0]] == 4)
5433     return 2; /* ldmib */
5434
5435   if (unsorted_offsets[order[nops - 1]] == 0)
5436     return 3; /* ldmda */
5437
5438   if (unsorted_offsets[order[nops - 1]] == -4)
5439     return 4; /* ldmdb */
5440
5441   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5442      if the offset isn't small enough.  The reason 2 ldrs are faster
5443      is because these ARMs are able to do more than one cache access
5444      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
5445      whilst the ARM8 has a double bandwidth cache.  This means that
5446      these cores can do both an instruction fetch and a data fetch in
5447      a single cycle, so the trick of calculating the address into a
5448      scratch register (one of the result regs) and then doing a load
5449      multiple actually becomes slower (and no smaller in code size).
5450      That is the transformation
5451
5452         ldr     rd1, [rbase + offset]
5453         ldr     rd2, [rbase + offset + 4]
5454
5455      to
5456
5457         add     rd1, rbase, offset
5458         ldmia   rd1, {rd1, rd2}
5459
5460      produces worse code -- '3 cycles + any stalls on rd2' instead of
5461      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
5462      access per cycle, the first sequence could never complete in less
5463      than 6 cycles, whereas the ldm sequence would only take 5 and
5464      would make better use of sequential accesses if not hitting the
5465      cache.
5466
5467      We cheat here and test 'arm_ld_sched' which we currently know to
5468      only be true for the ARM8, ARM9 and StrongARM.  If this ever
5469      changes, then the test below needs to be reworked.  */
5470   if (nops == 2 && arm_ld_sched)
5471     return 0;
5472
5473   /* Can't do it without setting up the offset, only do this if it takes
5474      no more than one insn.  */
5475   return (const_ok_for_arm (unsorted_offsets[order[0]])
5476           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5477 }
5478
5479 const char *
5480 emit_ldm_seq (rtx *operands, int nops)
5481 {
5482   int regs[4];
5483   int base_reg;
5484   HOST_WIDE_INT offset;
5485   char buf[100];
5486   int i;
5487
5488   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5489     {
5490     case 1:
5491       strcpy (buf, "ldm%?ia\t");
5492       break;
5493
5494     case 2:
5495       strcpy (buf, "ldm%?ib\t");
5496       break;
5497
5498     case 3:
5499       strcpy (buf, "ldm%?da\t");
5500       break;
5501
5502     case 4:
5503       strcpy (buf, "ldm%?db\t");
5504       break;
5505
5506     case 5:
5507       if (offset >= 0)
5508         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5509                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5510                  (long) offset);
5511       else
5512         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5513                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5514                  (long) -offset);
5515       output_asm_insn (buf, operands);
5516       base_reg = regs[0];
5517       strcpy (buf, "ldm%?ia\t");
5518       break;
5519
5520     default:
5521       gcc_unreachable ();
5522     }
5523
5524   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5525            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5526
5527   for (i = 1; i < nops; i++)
5528     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5529              reg_names[regs[i]]);
5530
5531   strcat (buf, "}\t%@ phole ldm");
5532
5533   output_asm_insn (buf, operands);
5534   return "";
5535 }
5536
5537 int
5538 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5539                          HOST_WIDE_INT * load_offset)
5540 {
5541   int unsorted_regs[4];
5542   HOST_WIDE_INT unsorted_offsets[4];
5543   int order[4];
5544   int base_reg = -1;
5545   int i;
5546
5547   /* Can only handle 2, 3, or 4 insns at present, though could be easily
5548      extended if required.  */
5549   gcc_assert (nops >= 2 && nops <= 4);
5550
5551   /* Loop over the operands and check that the memory references are
5552      suitable (i.e. immediate offsets from the same base register).  At
5553      the same time, extract the target register, and the memory
5554      offsets.  */
5555   for (i = 0; i < nops; i++)
5556     {
5557       rtx reg;
5558       rtx offset;
5559
5560       /* Convert a subreg of a mem into the mem itself.  */
5561       if (GET_CODE (operands[nops + i]) == SUBREG)
5562         operands[nops + i] = alter_subreg (operands + (nops + i));
5563
5564       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
5565
5566       /* Don't reorder volatile memory references; it doesn't seem worth
5567          looking for the case where the order is ok anyway.  */
5568       if (MEM_VOLATILE_P (operands[nops + i]))
5569         return 0;
5570
5571       offset = const0_rtx;
5572
5573       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5574            || (GET_CODE (reg) == SUBREG
5575                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5576           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5577               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5578                    == REG)
5579                   || (GET_CODE (reg) == SUBREG
5580                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5581               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5582                   == CONST_INT)))
5583         {
5584           if (i == 0)
5585             {
5586               base_reg = REGNO (reg);
5587               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5588                                   ? REGNO (operands[i])
5589                                   : REGNO (SUBREG_REG (operands[i])));
5590               order[0] = 0;
5591             }
5592           else
5593             {
5594               if (base_reg != (int) REGNO (reg))
5595                 /* Not addressed from the same base register.  */
5596                 return 0;
5597
5598               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5599                                   ? REGNO (operands[i])
5600                                   : REGNO (SUBREG_REG (operands[i])));
5601               if (unsorted_regs[i] < unsorted_regs[order[0]])
5602                 order[0] = i;
5603             }
5604
5605           /* If it isn't an integer register, then we can't do this.  */
5606           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5607             return 0;
5608
5609           unsorted_offsets[i] = INTVAL (offset);
5610         }
5611       else
5612         /* Not a suitable memory address.  */
5613         return 0;
5614     }
5615
5616   /* All the useful information has now been extracted from the
5617      operands into unsorted_regs and unsorted_offsets; additionally,
5618      order[0] has been set to the lowest numbered register in the
5619      list.  Sort the registers into order, and check that the memory
5620      offsets are ascending and adjacent.  */
5621
5622   for (i = 1; i < nops; i++)
5623     {
5624       int j;
5625
5626       order[i] = order[i - 1];
5627       for (j = 0; j < nops; j++)
5628         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5629             && (order[i] == order[i - 1]
5630                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5631           order[i] = j;
5632
5633       /* Have we found a suitable register? if not, one must be used more
5634          than once.  */
5635       if (order[i] == order[i - 1])
5636         return 0;
5637
5638       /* Is the memory address adjacent and ascending? */
5639       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5640         return 0;
5641     }
5642
5643   if (base)
5644     {
5645       *base = base_reg;
5646
5647       for (i = 0; i < nops; i++)
5648         regs[i] = unsorted_regs[order[i]];
5649
5650       *load_offset = unsorted_offsets[order[0]];
5651     }
5652
5653   if (unsorted_offsets[order[0]] == 0)
5654     return 1; /* stmia */
5655
5656   if (unsorted_offsets[order[0]] == 4)
5657     return 2; /* stmib */
5658
5659   if (unsorted_offsets[order[nops - 1]] == 0)
5660     return 3; /* stmda */
5661
5662   if (unsorted_offsets[order[nops - 1]] == -4)
5663     return 4; /* stmdb */
5664
5665   return 0;
5666 }
5667
5668 const char *
5669 emit_stm_seq (rtx *operands, int nops)
5670 {
5671   int regs[4];
5672   int base_reg;
5673   HOST_WIDE_INT offset;
5674   char buf[100];
5675   int i;
5676
5677   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5678     {
5679     case 1:
5680       strcpy (buf, "stm%?ia\t");
5681       break;
5682
5683     case 2:
5684       strcpy (buf, "stm%?ib\t");
5685       break;
5686
5687     case 3:
5688       strcpy (buf, "stm%?da\t");
5689       break;
5690
5691     case 4:
5692       strcpy (buf, "stm%?db\t");
5693       break;
5694
5695     default:
5696       gcc_unreachable ();
5697     }
5698
5699   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5700            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5701
5702   for (i = 1; i < nops; i++)
5703     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5704              reg_names[regs[i]]);
5705
5706   strcat (buf, "}\t%@ phole stm");
5707
5708   output_asm_insn (buf, operands);
5709   return "";
5710 }
5711
5712 \f
5713 /* Routines for use in generating RTL.  */
5714
5715 rtx
5716 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5717                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5718 {
5719   HOST_WIDE_INT offset = *offsetp;
5720   int i = 0, j;
5721   rtx result;
5722   int sign = up ? 1 : -1;
5723   rtx mem, addr;
5724
5725   /* XScale has load-store double instructions, but they have stricter
5726      alignment requirements than load-store multiple, so we cannot
5727      use them.
5728
5729      For XScale ldm requires 2 + NREGS cycles to complete and blocks
5730      the pipeline until completion.
5731
5732         NREGS           CYCLES
5733           1               3
5734           2               4
5735           3               5
5736           4               6
5737
5738      An ldr instruction takes 1-3 cycles, but does not block the
5739      pipeline.
5740
5741         NREGS           CYCLES
5742           1              1-3
5743           2              2-6
5744           3              3-9
5745           4              4-12
5746
5747      Best case ldr will always win.  However, the more ldr instructions
5748      we issue, the less likely we are to be able to schedule them well.
5749      Using ldr instructions also increases code size.
5750
5751      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5752      for counts of 3 or 4 regs.  */
5753   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5754     {
5755       rtx seq;
5756
5757       start_sequence ();
5758
5759       for (i = 0; i < count; i++)
5760         {
5761           addr = plus_constant (from, i * 4 * sign);
5762           mem = adjust_automodify_address (basemem, SImode, addr, offset);
5763           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5764           offset += 4 * sign;
5765         }
5766
5767       if (write_back)
5768         {
5769           emit_move_insn (from, plus_constant (from, count * 4 * sign));
5770           *offsetp = offset;
5771         }
5772
5773       seq = get_insns ();
5774       end_sequence ();
5775
5776       return seq;
5777     }
5778
5779   result = gen_rtx_PARALLEL (VOIDmode,
5780                              rtvec_alloc (count + (write_back ? 1 : 0)));
5781   if (write_back)
5782     {
5783       XVECEXP (result, 0, 0)
5784         = gen_rtx_SET (GET_MODE (from), from,
5785                        plus_constant (from, count * 4 * sign));
5786       i = 1;
5787       count++;
5788     }
5789
5790   for (j = 0; i < count; i++, j++)
5791     {
5792       addr = plus_constant (from, j * 4 * sign);
5793       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5794       XVECEXP (result, 0, i)
5795         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5796       offset += 4 * sign;
5797     }
5798
5799   if (write_back)
5800     *offsetp = offset;
5801
5802   return result;
5803 }
5804
5805 rtx
5806 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5807                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5808 {
5809   HOST_WIDE_INT offset = *offsetp;
5810   int i = 0, j;
5811   rtx result;
5812   int sign = up ? 1 : -1;
5813   rtx mem, addr;
5814
5815   /* See arm_gen_load_multiple for discussion of
5816      the pros/cons of ldm/stm usage for XScale.  */
5817   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5818     {
5819       rtx seq;
5820
5821       start_sequence ();
5822
5823       for (i = 0; i < count; i++)
5824         {
5825           addr = plus_constant (to, i * 4 * sign);
5826           mem = adjust_automodify_address (basemem, SImode, addr, offset);
5827           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5828           offset += 4 * sign;
5829         }
5830
5831       if (write_back)
5832         {
5833           emit_move_insn (to, plus_constant (to, count * 4 * sign));
5834           *offsetp = offset;
5835         }
5836
5837       seq = get_insns ();
5838       end_sequence ();
5839
5840       return seq;
5841     }
5842
5843   result = gen_rtx_PARALLEL (VOIDmode,
5844                              rtvec_alloc (count + (write_back ? 1 : 0)));
5845   if (write_back)
5846     {
5847       XVECEXP (result, 0, 0)
5848         = gen_rtx_SET (GET_MODE (to), to,
5849                        plus_constant (to, count * 4 * sign));
5850       i = 1;
5851       count++;
5852     }
5853
5854   for (j = 0; i < count; i++, j++)
5855     {
5856       addr = plus_constant (to, j * 4 * sign);
5857       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5858       XVECEXP (result, 0, i)
5859         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5860       offset += 4 * sign;
5861     }
5862
5863   if (write_back)
5864     *offsetp = offset;
5865
5866   return result;
5867 }
5868
5869 int
5870 arm_gen_movmemqi (rtx *operands)
5871 {
5872   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5873   HOST_WIDE_INT srcoffset, dstoffset;
5874   int i;
5875   rtx src, dst, srcbase, dstbase;
5876   rtx part_bytes_reg = NULL;
5877   rtx mem;
5878
5879   if (GET_CODE (operands[2]) != CONST_INT
5880       || GET_CODE (operands[3]) != CONST_INT
5881       || INTVAL (operands[2]) > 64
5882       || INTVAL (operands[3]) & 3)
5883     return 0;
5884
5885   dstbase = operands[0];
5886   srcbase = operands[1];
5887
5888   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
5889   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
5890
5891   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5892   out_words_to_go = INTVAL (operands[2]) / 4;
5893   last_bytes = INTVAL (operands[2]) & 3;
5894   dstoffset = srcoffset = 0;
5895
5896   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5897     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5898
5899   for (i = 0; in_words_to_go >= 2; i+=4)
5900     {
5901       if (in_words_to_go > 4)
5902         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5903                                           srcbase, &srcoffset));
5904       else
5905         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
5906                                           FALSE, srcbase, &srcoffset));
5907
5908       if (out_words_to_go)
5909         {
5910           if (out_words_to_go > 4)
5911             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5912                                                dstbase, &dstoffset));
5913           else if (out_words_to_go != 1)
5914             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5915                                                dst, TRUE,
5916                                                (last_bytes == 0
5917                                                 ? FALSE : TRUE),
5918                                                dstbase, &dstoffset));
5919           else
5920             {
5921               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5922               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5923               if (last_bytes != 0)
5924                 {
5925                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5926                   dstoffset += 4;
5927                 }
5928             }
5929         }
5930
5931       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5932       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
5933     }
5934
5935   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
5936   if (out_words_to_go)
5937     {
5938       rtx sreg;
5939
5940       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5941       sreg = copy_to_reg (mem);
5942
5943       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5944       emit_move_insn (mem, sreg);
5945       in_words_to_go--;
5946
5947       gcc_assert (!in_words_to_go);     /* Sanity check */
5948     }
5949
5950   if (in_words_to_go)
5951     {
5952       gcc_assert (in_words_to_go > 0);
5953
5954       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5955       part_bytes_reg = copy_to_mode_reg (SImode, mem);
5956     }
5957
5958   gcc_assert (!last_bytes || part_bytes_reg);
5959
5960   if (BYTES_BIG_ENDIAN && last_bytes)
5961     {
5962       rtx tmp = gen_reg_rtx (SImode);
5963
5964       /* The bytes we want are in the top end of the word.  */
5965       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
5966                               GEN_INT (8 * (4 - last_bytes))));
5967       part_bytes_reg = tmp;
5968
5969       while (last_bytes)
5970         {
5971           mem = adjust_automodify_address (dstbase, QImode,
5972                                            plus_constant (dst, last_bytes - 1),
5973                                            dstoffset + last_bytes - 1);
5974           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5975
5976           if (--last_bytes)
5977             {
5978               tmp = gen_reg_rtx (SImode);
5979               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
5980               part_bytes_reg = tmp;
5981             }
5982         }
5983
5984     }
5985   else
5986     {
5987       if (last_bytes > 1)
5988         {
5989           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
5990           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
5991           last_bytes -= 2;
5992           if (last_bytes)
5993             {
5994               rtx tmp = gen_reg_rtx (SImode);
5995               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
5996               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
5997               part_bytes_reg = tmp;
5998               dstoffset += 2;
5999             }
6000         }
6001
6002       if (last_bytes)
6003         {
6004           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
6005           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6006         }
6007     }
6008
6009   return 1;
6010 }
6011
6012 /* Generate a memory reference for a half word, such that it will be loaded
6013    into the top 16 bits of the word.  We can assume that the address is
6014    known to be alignable and of the form reg, or plus (reg, const).  */
6015
6016 rtx
6017 arm_gen_rotated_half_load (rtx memref)
6018 {
6019   HOST_WIDE_INT offset = 0;
6020   rtx base = XEXP (memref, 0);
6021
6022   if (GET_CODE (base) == PLUS)
6023     {
6024       offset = INTVAL (XEXP (base, 1));
6025       base = XEXP (base, 0);
6026     }
6027
6028   /* If we aren't allowed to generate unaligned addresses, then fail.  */
6029   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0))
6030     return NULL;
6031
6032   base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
6033
6034   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
6035     return base;
6036
6037   return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
6038 }
6039
6040 /* Select a dominance comparison mode if possible for a test of the general
6041    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
6042    COND_OR == DOM_CC_X_AND_Y => (X && Y)
6043    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6044    COND_OR == DOM_CC_X_OR_Y => (X || Y)
6045    In all cases OP will be either EQ or NE, but we don't need to know which
6046    here.  If we are unable to support a dominance comparison we return
6047    CC mode.  This will then fail to match for the RTL expressions that
6048    generate this call.  */
6049 enum machine_mode
6050 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6051 {
6052   enum rtx_code cond1, cond2;
6053   int swapped = 0;
6054
6055   /* Currently we will probably get the wrong result if the individual
6056      comparisons are not simple.  This also ensures that it is safe to
6057      reverse a comparison if necessary.  */
6058   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6059        != CCmode)
6060       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6061           != CCmode))
6062     return CCmode;
6063
6064   /* The if_then_else variant of this tests the second condition if the
6065      first passes, but is true if the first fails.  Reverse the first
6066      condition to get a true "inclusive-or" expression.  */
6067   if (cond_or == DOM_CC_NX_OR_Y)
6068     cond1 = reverse_condition (cond1);
6069
6070   /* If the comparisons are not equal, and one doesn't dominate the other,
6071      then we can't do this.  */
6072   if (cond1 != cond2
6073       && !comparison_dominates_p (cond1, cond2)
6074       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6075     return CCmode;
6076
6077   if (swapped)
6078     {
6079       enum rtx_code temp = cond1;
6080       cond1 = cond2;
6081       cond2 = temp;
6082     }
6083
6084   switch (cond1)
6085     {
6086     case EQ:
6087       if (cond_or == DOM_CC_X_AND_Y)
6088         return CC_DEQmode;
6089
6090       switch (cond2)
6091         {
6092         case EQ: return CC_DEQmode;
6093         case LE: return CC_DLEmode;
6094         case LEU: return CC_DLEUmode;
6095         case GE: return CC_DGEmode;
6096         case GEU: return CC_DGEUmode;
6097         default: gcc_unreachable ();
6098         }
6099
6100     case LT:
6101       if (cond_or == DOM_CC_X_AND_Y)
6102         return CC_DLTmode;
6103       
6104       switch (cond2)
6105         {
6106         case  LT:
6107             return CC_DLTmode;
6108         case LE:
6109           return CC_DLEmode;
6110         case NE:
6111           return CC_DNEmode;
6112         default:
6113           gcc_unreachable ();
6114         }
6115
6116     case GT:
6117       if (cond_or == DOM_CC_X_AND_Y)
6118         return CC_DGTmode;
6119
6120       switch (cond2)
6121         {
6122         case GT:
6123           return CC_DGTmode;
6124         case GE:
6125           return CC_DGEmode;
6126         case NE:
6127           return CC_DNEmode;
6128         default:
6129           gcc_unreachable ();
6130         }
6131
6132     case LTU:
6133       if (cond_or == DOM_CC_X_AND_Y)
6134         return CC_DLTUmode;
6135
6136       switch (cond2)
6137         {
6138         case LTU:
6139           return CC_DLTUmode;
6140         case LEU:
6141           return CC_DLEUmode;
6142         case NE:
6143           return CC_DNEmode;
6144         default:
6145           gcc_unreachable ();
6146         }
6147
6148     case GTU:
6149       if (cond_or == DOM_CC_X_AND_Y)
6150         return CC_DGTUmode;
6151       
6152       switch (cond2)
6153         {
6154         case GTU:
6155           return CC_DGTUmode;
6156         case GEU:
6157           return CC_DGEUmode;
6158         case NE:
6159           return CC_DNEmode;
6160         default:
6161           gcc_unreachable ();
6162         }
6163
6164     /* The remaining cases only occur when both comparisons are the
6165        same.  */
6166     case NE:
6167       gcc_assert (cond1 == cond2);
6168       return CC_DNEmode;
6169
6170     case LE:
6171       gcc_assert (cond1 == cond2);
6172       return CC_DLEmode;
6173
6174     case GE:
6175       gcc_assert (cond1 == cond2);
6176       return CC_DGEmode;
6177
6178     case LEU:
6179       gcc_assert (cond1 == cond2);
6180       return CC_DLEUmode;
6181
6182     case GEU:
6183       gcc_assert (cond1 == cond2);
6184       return CC_DGEUmode;
6185
6186     default:
6187       gcc_unreachable ();
6188     }
6189 }
6190
6191 enum machine_mode
6192 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6193 {
6194   /* All floating point compares return CCFP if it is an equality
6195      comparison, and CCFPE otherwise.  */
6196   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6197     {
6198       switch (op)
6199         {
6200         case EQ:
6201         case NE:
6202         case UNORDERED:
6203         case ORDERED:
6204         case UNLT:
6205         case UNLE:
6206         case UNGT:
6207         case UNGE:
6208         case UNEQ:
6209         case LTGT:
6210           return CCFPmode;
6211
6212         case LT:
6213         case LE:
6214         case GT:
6215         case GE:
6216           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6217             return CCFPmode;
6218           return CCFPEmode;
6219
6220         default:
6221           gcc_unreachable ();
6222         }
6223     }
6224
6225   /* A compare with a shifted operand.  Because of canonicalization, the
6226      comparison will have to be swapped when we emit the assembler.  */
6227   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6228       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6229           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6230           || GET_CODE (x) == ROTATERT))
6231     return CC_SWPmode;
6232
6233   /* This operation is performed swapped, but since we only rely on the Z
6234      flag we don't need an additional mode.  */
6235   if (GET_MODE (y) == SImode && REG_P (y)
6236       && GET_CODE (x) == NEG
6237       && (op == EQ || op == NE))
6238     return CC_Zmode;
6239
6240   /* This is a special case that is used by combine to allow a
6241      comparison of a shifted byte load to be split into a zero-extend
6242      followed by a comparison of the shifted integer (only valid for
6243      equalities and unsigned inequalities).  */
6244   if (GET_MODE (x) == SImode
6245       && GET_CODE (x) == ASHIFT
6246       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6247       && GET_CODE (XEXP (x, 0)) == SUBREG
6248       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6249       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6250       && (op == EQ || op == NE
6251           || op == GEU || op == GTU || op == LTU || op == LEU)
6252       && GET_CODE (y) == CONST_INT)
6253     return CC_Zmode;
6254
6255   /* A construct for a conditional compare, if the false arm contains
6256      0, then both conditions must be true, otherwise either condition
6257      must be true.  Not all conditions are possible, so CCmode is
6258      returned if it can't be done.  */
6259   if (GET_CODE (x) == IF_THEN_ELSE
6260       && (XEXP (x, 2) == const0_rtx
6261           || XEXP (x, 2) == const1_rtx)
6262       && COMPARISON_P (XEXP (x, 0))
6263       && COMPARISON_P (XEXP (x, 1)))
6264     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6265                                          INTVAL (XEXP (x, 2)));
6266
6267   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
6268   if (GET_CODE (x) == AND
6269       && COMPARISON_P (XEXP (x, 0))
6270       && COMPARISON_P (XEXP (x, 1)))
6271     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6272                                          DOM_CC_X_AND_Y);
6273
6274   if (GET_CODE (x) == IOR
6275       && COMPARISON_P (XEXP (x, 0))
6276       && COMPARISON_P (XEXP (x, 1)))
6277     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6278                                          DOM_CC_X_OR_Y);
6279
6280   /* An operation (on Thumb) where we want to test for a single bit.
6281      This is done by shifting that bit up into the top bit of a
6282      scratch register; we can then branch on the sign bit.  */
6283   if (TARGET_THUMB
6284       && GET_MODE (x) == SImode
6285       && (op == EQ || op == NE)
6286       && (GET_CODE (x) == ZERO_EXTRACT))
6287     return CC_Nmode;
6288
6289   /* An operation that sets the condition codes as a side-effect, the
6290      V flag is not set correctly, so we can only use comparisons where
6291      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
6292      instead.)  */
6293   if (GET_MODE (x) == SImode
6294       && y == const0_rtx
6295       && (op == EQ || op == NE || op == LT || op == GE)
6296       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6297           || GET_CODE (x) == AND || GET_CODE (x) == IOR
6298           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6299           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6300           || GET_CODE (x) == LSHIFTRT
6301           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6302           || GET_CODE (x) == ROTATERT
6303           || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6304     return CC_NOOVmode;
6305
6306   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6307     return CC_Zmode;
6308
6309   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6310       && GET_CODE (x) == PLUS
6311       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6312     return CC_Cmode;
6313
6314   return CCmode;
6315 }
6316
6317 /* X and Y are two things to compare using CODE.  Emit the compare insn and
6318    return the rtx for register 0 in the proper mode.  FP means this is a
6319    floating point compare: I don't think that it is needed on the arm.  */
6320 rtx
6321 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6322 {
6323   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6324   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6325
6326   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6327                           gen_rtx_COMPARE (mode, x, y)));
6328
6329   return cc_reg;
6330 }
6331
6332 /* Generate a sequence of insns that will generate the correct return
6333    address mask depending on the physical architecture that the program
6334    is running on.  */
6335 rtx
6336 arm_gen_return_addr_mask (void)
6337 {
6338   rtx reg = gen_reg_rtx (Pmode);
6339
6340   emit_insn (gen_return_addr_mask (reg));
6341   return reg;
6342 }
6343
6344 void
6345 arm_reload_in_hi (rtx *operands)
6346 {
6347   rtx ref = operands[1];
6348   rtx base, scratch;
6349   HOST_WIDE_INT offset = 0;
6350
6351   if (GET_CODE (ref) == SUBREG)
6352     {
6353       offset = SUBREG_BYTE (ref);
6354       ref = SUBREG_REG (ref);
6355     }
6356
6357   if (GET_CODE (ref) == REG)
6358     {
6359       /* We have a pseudo which has been spilt onto the stack; there
6360          are two cases here: the first where there is a simple
6361          stack-slot replacement and a second where the stack-slot is
6362          out of range, or is used as a subreg.  */
6363       if (reg_equiv_mem[REGNO (ref)])
6364         {
6365           ref = reg_equiv_mem[REGNO (ref)];
6366           base = find_replacement (&XEXP (ref, 0));
6367         }
6368       else
6369         /* The slot is out of range, or was dressed up in a SUBREG.  */
6370         base = reg_equiv_address[REGNO (ref)];
6371     }
6372   else
6373     base = find_replacement (&XEXP (ref, 0));
6374
6375   /* Handle the case where the address is too complex to be offset by 1.  */
6376   if (GET_CODE (base) == MINUS
6377       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6378     {
6379       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6380
6381       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6382       base = base_plus;
6383     }
6384   else if (GET_CODE (base) == PLUS)
6385     {
6386       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6387       HOST_WIDE_INT hi, lo;
6388
6389       offset += INTVAL (XEXP (base, 1));
6390       base = XEXP (base, 0);
6391
6392       /* Rework the address into a legal sequence of insns.  */
6393       /* Valid range for lo is -4095 -> 4095 */
6394       lo = (offset >= 0
6395             ? (offset & 0xfff)
6396             : -((-offset) & 0xfff));
6397
6398       /* Corner case, if lo is the max offset then we would be out of range
6399          once we have added the additional 1 below, so bump the msb into the
6400          pre-loading insn(s).  */
6401       if (lo == 4095)
6402         lo &= 0x7ff;
6403
6404       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6405              ^ (HOST_WIDE_INT) 0x80000000)
6406             - (HOST_WIDE_INT) 0x80000000);
6407
6408       gcc_assert (hi + lo == offset);
6409
6410       if (hi != 0)
6411         {
6412           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6413
6414           /* Get the base address; addsi3 knows how to handle constants
6415              that require more than one insn.  */
6416           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6417           base = base_plus;
6418           offset = lo;
6419         }
6420     }
6421
6422   /* Operands[2] may overlap operands[0] (though it won't overlap
6423      operands[1]), that's why we asked for a DImode reg -- so we can
6424      use the bit that does not overlap.  */
6425   if (REGNO (operands[2]) == REGNO (operands[0]))
6426     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6427   else
6428     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6429
6430   emit_insn (gen_zero_extendqisi2 (scratch,
6431                                    gen_rtx_MEM (QImode,
6432                                                 plus_constant (base,
6433                                                                offset))));
6434   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6435                                    gen_rtx_MEM (QImode,
6436                                                 plus_constant (base,
6437                                                                offset + 1))));
6438   if (!BYTES_BIG_ENDIAN)
6439     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6440                         gen_rtx_IOR (SImode,
6441                                      gen_rtx_ASHIFT
6442                                      (SImode,
6443                                       gen_rtx_SUBREG (SImode, operands[0], 0),
6444                                       GEN_INT (8)),
6445                                      scratch)));
6446   else
6447     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6448                             gen_rtx_IOR (SImode,
6449                                          gen_rtx_ASHIFT (SImode, scratch,
6450                                                          GEN_INT (8)),
6451                                          gen_rtx_SUBREG (SImode, operands[0],
6452                                                          0))));
6453 }
6454
6455 /* Handle storing a half-word to memory during reload by synthesizing as two
6456    byte stores.  Take care not to clobber the input values until after we
6457    have moved them somewhere safe.  This code assumes that if the DImode
6458    scratch in operands[2] overlaps either the input value or output address
6459    in some way, then that value must die in this insn (we absolutely need
6460    two scratch registers for some corner cases).  */
6461 void
6462 arm_reload_out_hi (rtx *operands)
6463 {
6464   rtx ref = operands[0];
6465   rtx outval = operands[1];
6466   rtx base, scratch;
6467   HOST_WIDE_INT offset = 0;
6468
6469   if (GET_CODE (ref) == SUBREG)
6470     {
6471       offset = SUBREG_BYTE (ref);
6472       ref = SUBREG_REG (ref);
6473     }
6474
6475   if (GET_CODE (ref) == REG)
6476     {
6477       /* We have a pseudo which has been spilt onto the stack; there
6478          are two cases here: the first where there is a simple
6479          stack-slot replacement and a second where the stack-slot is
6480          out of range, or is used as a subreg.  */
6481       if (reg_equiv_mem[REGNO (ref)])
6482         {
6483           ref = reg_equiv_mem[REGNO (ref)];
6484           base = find_replacement (&XEXP (ref, 0));
6485         }
6486       else
6487         /* The slot is out of range, or was dressed up in a SUBREG.  */
6488         base = reg_equiv_address[REGNO (ref)];
6489     }
6490   else
6491     base = find_replacement (&XEXP (ref, 0));
6492
6493   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6494
6495   /* Handle the case where the address is too complex to be offset by 1.  */
6496   if (GET_CODE (base) == MINUS
6497       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6498     {
6499       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6500
6501       /* Be careful not to destroy OUTVAL.  */
6502       if (reg_overlap_mentioned_p (base_plus, outval))
6503         {
6504           /* Updating base_plus might destroy outval, see if we can
6505              swap the scratch and base_plus.  */
6506           if (!reg_overlap_mentioned_p (scratch, outval))
6507             {
6508               rtx tmp = scratch;
6509               scratch = base_plus;
6510               base_plus = tmp;
6511             }
6512           else
6513             {
6514               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6515
6516               /* Be conservative and copy OUTVAL into the scratch now,
6517                  this should only be necessary if outval is a subreg
6518                  of something larger than a word.  */
6519               /* XXX Might this clobber base?  I can't see how it can,
6520                  since scratch is known to overlap with OUTVAL, and
6521                  must be wider than a word.  */
6522               emit_insn (gen_movhi (scratch_hi, outval));
6523               outval = scratch_hi;
6524             }
6525         }
6526
6527       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6528       base = base_plus;
6529     }
6530   else if (GET_CODE (base) == PLUS)
6531     {
6532       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6533       HOST_WIDE_INT hi, lo;
6534
6535       offset += INTVAL (XEXP (base, 1));
6536       base = XEXP (base, 0);
6537
6538       /* Rework the address into a legal sequence of insns.  */
6539       /* Valid range for lo is -4095 -> 4095 */
6540       lo = (offset >= 0
6541             ? (offset & 0xfff)
6542             : -((-offset) & 0xfff));
6543
6544       /* Corner case, if lo is the max offset then we would be out of range
6545          once we have added the additional 1 below, so bump the msb into the
6546          pre-loading insn(s).  */
6547       if (lo == 4095)
6548         lo &= 0x7ff;
6549
6550       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6551              ^ (HOST_WIDE_INT) 0x80000000)
6552             - (HOST_WIDE_INT) 0x80000000);
6553
6554       gcc_assert (hi + lo == offset);
6555
6556       if (hi != 0)
6557         {
6558           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6559
6560           /* Be careful not to destroy OUTVAL.  */
6561           if (reg_overlap_mentioned_p (base_plus, outval))
6562             {
6563               /* Updating base_plus might destroy outval, see if we
6564                  can swap the scratch and base_plus.  */
6565               if (!reg_overlap_mentioned_p (scratch, outval))
6566                 {
6567                   rtx tmp = scratch;
6568                   scratch = base_plus;
6569                   base_plus = tmp;
6570                 }
6571               else
6572                 {
6573                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6574
6575                   /* Be conservative and copy outval into scratch now,
6576                      this should only be necessary if outval is a
6577                      subreg of something larger than a word.  */
6578                   /* XXX Might this clobber base?  I can't see how it
6579                      can, since scratch is known to overlap with
6580                      outval.  */
6581                   emit_insn (gen_movhi (scratch_hi, outval));
6582                   outval = scratch_hi;
6583                 }
6584             }
6585
6586           /* Get the base address; addsi3 knows how to handle constants
6587              that require more than one insn.  */
6588           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6589           base = base_plus;
6590           offset = lo;
6591         }
6592     }
6593
6594   if (BYTES_BIG_ENDIAN)
6595     {
6596       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6597                                          plus_constant (base, offset + 1)),
6598                             gen_lowpart (QImode, outval)));
6599       emit_insn (gen_lshrsi3 (scratch,
6600                               gen_rtx_SUBREG (SImode, outval, 0),
6601                               GEN_INT (8)));
6602       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6603                             gen_lowpart (QImode, scratch)));
6604     }
6605   else
6606     {
6607       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6608                             gen_lowpart (QImode, outval)));
6609       emit_insn (gen_lshrsi3 (scratch,
6610                               gen_rtx_SUBREG (SImode, outval, 0),
6611                               GEN_INT (8)));
6612       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6613                                          plus_constant (base, offset + 1)),
6614                             gen_lowpart (QImode, scratch)));
6615     }
6616 }
6617
6618 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
6619    (padded to the size of a word) should be passed in a register.  */
6620
6621 static bool
6622 arm_must_pass_in_stack (enum machine_mode mode, tree type)
6623 {
6624   if (TARGET_AAPCS_BASED)
6625     return must_pass_in_stack_var_size (mode, type);
6626   else
6627     return must_pass_in_stack_var_size_or_pad (mode, type);
6628 }
6629
6630
6631 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
6632    Return true if an argument passed on the stack should be padded upwards,
6633    i.e. if the least-significant byte has useful data.  */
6634
6635 bool
6636 arm_pad_arg_upward (enum machine_mode mode, tree type)
6637 {
6638   if (!TARGET_AAPCS_BASED)
6639     return DEFAULT_FUNCTION_ARG_PADDING(mode, type);
6640
6641   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
6642     return false;
6643
6644   return true;
6645 }
6646
6647
6648 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
6649    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
6650    byte of the register has useful data, and return the opposite if the
6651    most significant byte does.
6652    For AAPCS, small aggregates and small complex types are always padded
6653    upwards.  */
6654
6655 bool
6656 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
6657                     tree type, int first ATTRIBUTE_UNUSED)
6658 {
6659   if (TARGET_AAPCS_BASED
6660       && BYTES_BIG_ENDIAN
6661       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
6662       && int_size_in_bytes (type) <= 4)
6663     return true;
6664
6665   /* Otherwise, use default padding.  */
6666   return !BYTES_BIG_ENDIAN;
6667 }
6668
6669
6670 \f
6671 /* Print a symbolic form of X to the debug file, F.  */
6672 static void
6673 arm_print_value (FILE *f, rtx x)
6674 {
6675   switch (GET_CODE (x))
6676     {
6677     case CONST_INT:
6678       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6679       return;
6680
6681     case CONST_DOUBLE:
6682       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6683       return;
6684
6685     case CONST_VECTOR:
6686       {
6687         int i;
6688
6689         fprintf (f, "<");
6690         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6691           {
6692             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6693             if (i < (CONST_VECTOR_NUNITS (x) - 1))
6694               fputc (',', f);
6695           }
6696         fprintf (f, ">");
6697       }
6698       return;
6699
6700     case CONST_STRING:
6701       fprintf (f, "\"%s\"", XSTR (x, 0));
6702       return;
6703
6704     case SYMBOL_REF:
6705       fprintf (f, "`%s'", XSTR (x, 0));
6706       return;
6707
6708     case LABEL_REF:
6709       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6710       return;
6711
6712     case CONST:
6713       arm_print_value (f, XEXP (x, 0));
6714       return;
6715
6716     case PLUS:
6717       arm_print_value (f, XEXP (x, 0));
6718       fprintf (f, "+");
6719       arm_print_value (f, XEXP (x, 1));
6720       return;
6721
6722     case PC:
6723       fprintf (f, "pc");
6724       return;
6725
6726     default:
6727       fprintf (f, "????");
6728       return;
6729     }
6730 }
6731 \f
6732 /* Routines for manipulation of the constant pool.  */
6733
6734 /* Arm instructions cannot load a large constant directly into a
6735    register; they have to come from a pc relative load.  The constant
6736    must therefore be placed in the addressable range of the pc
6737    relative load.  Depending on the precise pc relative load
6738    instruction the range is somewhere between 256 bytes and 4k.  This
6739    means that we often have to dump a constant inside a function, and
6740    generate code to branch around it.
6741
6742    It is important to minimize this, since the branches will slow
6743    things down and make the code larger.
6744
6745    Normally we can hide the table after an existing unconditional
6746    branch so that there is no interruption of the flow, but in the
6747    worst case the code looks like this:
6748
6749         ldr     rn, L1
6750         ...
6751         b       L2
6752         align
6753         L1:     .long value
6754         L2:
6755         ...
6756
6757         ldr     rn, L3
6758         ...
6759         b       L4
6760         align
6761         L3:     .long value
6762         L4:
6763         ...
6764
6765    We fix this by performing a scan after scheduling, which notices
6766    which instructions need to have their operands fetched from the
6767    constant table and builds the table.
6768
6769    The algorithm starts by building a table of all the constants that
6770    need fixing up and all the natural barriers in the function (places
6771    where a constant table can be dropped without breaking the flow).
6772    For each fixup we note how far the pc-relative replacement will be
6773    able to reach and the offset of the instruction into the function.
6774
6775    Having built the table we then group the fixes together to form
6776    tables that are as large as possible (subject to addressing
6777    constraints) and emit each table of constants after the last
6778    barrier that is within range of all the instructions in the group.
6779    If a group does not contain a barrier, then we forcibly create one
6780    by inserting a jump instruction into the flow.  Once the table has
6781    been inserted, the insns are then modified to reference the
6782    relevant entry in the pool.
6783
6784    Possible enhancements to the algorithm (not implemented) are:
6785
6786    1) For some processors and object formats, there may be benefit in
6787    aligning the pools to the start of cache lines; this alignment
6788    would need to be taken into account when calculating addressability
6789    of a pool.  */
6790
6791 /* These typedefs are located at the start of this file, so that
6792    they can be used in the prototypes there.  This comment is to
6793    remind readers of that fact so that the following structures
6794    can be understood more easily.
6795
6796      typedef struct minipool_node    Mnode;
6797      typedef struct minipool_fixup   Mfix;  */
6798
6799 struct minipool_node
6800 {
6801   /* Doubly linked chain of entries.  */
6802   Mnode * next;
6803   Mnode * prev;
6804   /* The maximum offset into the code that this entry can be placed.  While
6805      pushing fixes for forward references, all entries are sorted in order
6806      of increasing max_address.  */
6807   HOST_WIDE_INT max_address;
6808   /* Similarly for an entry inserted for a backwards ref.  */
6809   HOST_WIDE_INT min_address;
6810   /* The number of fixes referencing this entry.  This can become zero
6811      if we "unpush" an entry.  In this case we ignore the entry when we
6812      come to emit the code.  */
6813   int refcount;
6814   /* The offset from the start of the minipool.  */
6815   HOST_WIDE_INT offset;
6816   /* The value in table.  */
6817   rtx value;
6818   /* The mode of value.  */
6819   enum machine_mode mode;
6820   /* The size of the value.  With iWMMXt enabled
6821      sizes > 4 also imply an alignment of 8-bytes.  */
6822   int fix_size;
6823 };
6824
6825 struct minipool_fixup
6826 {
6827   Mfix *            next;
6828   rtx               insn;
6829   HOST_WIDE_INT     address;
6830   rtx *             loc;
6831   enum machine_mode mode;
6832   int               fix_size;
6833   rtx               value;
6834   Mnode *           minipool;
6835   HOST_WIDE_INT     forwards;
6836   HOST_WIDE_INT     backwards;
6837 };
6838
6839 /* Fixes less than a word need padding out to a word boundary.  */
6840 #define MINIPOOL_FIX_SIZE(mode) \
6841   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6842
6843 static Mnode *  minipool_vector_head;
6844 static Mnode *  minipool_vector_tail;
6845 static rtx      minipool_vector_label;
6846
6847 /* The linked list of all minipool fixes required for this function.  */
6848 Mfix *          minipool_fix_head;
6849 Mfix *          minipool_fix_tail;
6850 /* The fix entry for the current minipool, once it has been placed.  */
6851 Mfix *          minipool_barrier;
6852
6853 /* Determines if INSN is the start of a jump table.  Returns the end
6854    of the TABLE or NULL_RTX.  */
6855 static rtx
6856 is_jump_table (rtx insn)
6857 {
6858   rtx table;
6859
6860   if (GET_CODE (insn) == JUMP_INSN
6861       && JUMP_LABEL (insn) != NULL
6862       && ((table = next_real_insn (JUMP_LABEL (insn)))
6863           == next_real_insn (insn))
6864       && table != NULL
6865       && GET_CODE (table) == JUMP_INSN
6866       && (GET_CODE (PATTERN (table)) == ADDR_VEC
6867           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6868     return table;
6869
6870   return NULL_RTX;
6871 }
6872
6873 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6874 #define JUMP_TABLES_IN_TEXT_SECTION 0
6875 #endif
6876
6877 static HOST_WIDE_INT
6878 get_jump_table_size (rtx insn)
6879 {
6880   /* ADDR_VECs only take room if read-only data does into the text
6881      section.  */
6882   if (JUMP_TABLES_IN_TEXT_SECTION
6883 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6884       || 1
6885 #endif
6886       )
6887     {
6888       rtx body = PATTERN (insn);
6889       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6890
6891       return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6892     }
6893
6894   return 0;
6895 }
6896
6897 /* Move a minipool fix MP from its current location to before MAX_MP.
6898    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6899    constraints may need updating.  */
6900 static Mnode *
6901 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6902                                HOST_WIDE_INT max_address)
6903 {
6904   /* The code below assumes these are different.  */
6905   gcc_assert (mp != max_mp);
6906
6907   if (max_mp == NULL)
6908     {
6909       if (max_address < mp->max_address)
6910         mp->max_address = max_address;
6911     }
6912   else
6913     {
6914       if (max_address > max_mp->max_address - mp->fix_size)
6915         mp->max_address = max_mp->max_address - mp->fix_size;
6916       else
6917         mp->max_address = max_address;
6918
6919       /* Unlink MP from its current position.  Since max_mp is non-null,
6920        mp->prev must be non-null.  */
6921       mp->prev->next = mp->next;
6922       if (mp->next != NULL)
6923         mp->next->prev = mp->prev;
6924       else
6925         minipool_vector_tail = mp->prev;
6926
6927       /* Re-insert it before MAX_MP.  */
6928       mp->next = max_mp;
6929       mp->prev = max_mp->prev;
6930       max_mp->prev = mp;
6931
6932       if (mp->prev != NULL)
6933         mp->prev->next = mp;
6934       else
6935         minipool_vector_head = mp;
6936     }
6937
6938   /* Save the new entry.  */
6939   max_mp = mp;
6940
6941   /* Scan over the preceding entries and adjust their addresses as
6942      required.  */
6943   while (mp->prev != NULL
6944          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6945     {
6946       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6947       mp = mp->prev;
6948     }
6949
6950   return max_mp;
6951 }
6952
6953 /* Add a constant to the minipool for a forward reference.  Returns the
6954    node added or NULL if the constant will not fit in this pool.  */
6955 static Mnode *
6956 add_minipool_forward_ref (Mfix *fix)
6957 {
6958   /* If set, max_mp is the first pool_entry that has a lower
6959      constraint than the one we are trying to add.  */
6960   Mnode *       max_mp = NULL;
6961   HOST_WIDE_INT max_address = fix->address + fix->forwards;
6962   Mnode *       mp;
6963
6964   /* If this fix's address is greater than the address of the first
6965      entry, then we can't put the fix in this pool.  We subtract the
6966      size of the current fix to ensure that if the table is fully
6967      packed we still have enough room to insert this value by suffling
6968      the other fixes forwards.  */
6969   if (minipool_vector_head &&
6970       fix->address >= minipool_vector_head->max_address - fix->fix_size)
6971     return NULL;
6972
6973   /* Scan the pool to see if a constant with the same value has
6974      already been added.  While we are doing this, also note the
6975      location where we must insert the constant if it doesn't already
6976      exist.  */
6977   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6978     {
6979       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6980           && fix->mode == mp->mode
6981           && (GET_CODE (fix->value) != CODE_LABEL
6982               || (CODE_LABEL_NUMBER (fix->value)
6983                   == CODE_LABEL_NUMBER (mp->value)))
6984           && rtx_equal_p (fix->value, mp->value))
6985         {
6986           /* More than one fix references this entry.  */
6987           mp->refcount++;
6988           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6989         }
6990
6991       /* Note the insertion point if necessary.  */
6992       if (max_mp == NULL
6993           && mp->max_address > max_address)
6994         max_mp = mp;
6995
6996       /* If we are inserting an 8-bytes aligned quantity and
6997          we have not already found an insertion point, then
6998          make sure that all such 8-byte aligned quantities are
6999          placed at the start of the pool.  */
7000       if (ARM_DOUBLEWORD_ALIGN
7001           && max_mp == NULL
7002           && fix->fix_size == 8
7003           && mp->fix_size != 8)
7004         {
7005           max_mp = mp;
7006           max_address = mp->max_address;
7007         }
7008     }
7009
7010   /* The value is not currently in the minipool, so we need to create
7011      a new entry for it.  If MAX_MP is NULL, the entry will be put on
7012      the end of the list since the placement is less constrained than
7013      any existing entry.  Otherwise, we insert the new fix before
7014      MAX_MP and, if necessary, adjust the constraints on the other
7015      entries.  */
7016   mp = xmalloc (sizeof (* mp));
7017   mp->fix_size = fix->fix_size;
7018   mp->mode = fix->mode;
7019   mp->value = fix->value;
7020   mp->refcount = 1;
7021   /* Not yet required for a backwards ref.  */
7022   mp->min_address = -65536;
7023
7024   if (max_mp == NULL)
7025     {
7026       mp->max_address = max_address;
7027       mp->next = NULL;
7028       mp->prev = minipool_vector_tail;
7029
7030       if (mp->prev == NULL)
7031         {
7032           minipool_vector_head = mp;
7033           minipool_vector_label = gen_label_rtx ();
7034         }
7035       else
7036         mp->prev->next = mp;
7037
7038       minipool_vector_tail = mp;
7039     }
7040   else
7041     {
7042       if (max_address > max_mp->max_address - mp->fix_size)
7043         mp->max_address = max_mp->max_address - mp->fix_size;
7044       else
7045         mp->max_address = max_address;
7046
7047       mp->next = max_mp;
7048       mp->prev = max_mp->prev;
7049       max_mp->prev = mp;
7050       if (mp->prev != NULL)
7051         mp->prev->next = mp;
7052       else
7053         minipool_vector_head = mp;
7054     }
7055
7056   /* Save the new entry.  */
7057   max_mp = mp;
7058
7059   /* Scan over the preceding entries and adjust their addresses as
7060      required.  */
7061   while (mp->prev != NULL
7062          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7063     {
7064       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7065       mp = mp->prev;
7066     }
7067
7068   return max_mp;
7069 }
7070
7071 static Mnode *
7072 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7073                                 HOST_WIDE_INT  min_address)
7074 {
7075   HOST_WIDE_INT offset;
7076
7077   /* The code below assumes these are different.  */
7078   gcc_assert (mp != min_mp);
7079
7080   if (min_mp == NULL)
7081     {
7082       if (min_address > mp->min_address)
7083         mp->min_address = min_address;
7084     }
7085   else
7086     {
7087       /* We will adjust this below if it is too loose.  */
7088       mp->min_address = min_address;
7089
7090       /* Unlink MP from its current position.  Since min_mp is non-null,
7091          mp->next must be non-null.  */
7092       mp->next->prev = mp->prev;
7093       if (mp->prev != NULL)
7094         mp->prev->next = mp->next;
7095       else
7096         minipool_vector_head = mp->next;
7097
7098       /* Reinsert it after MIN_MP.  */
7099       mp->prev = min_mp;
7100       mp->next = min_mp->next;
7101       min_mp->next = mp;
7102       if (mp->next != NULL)
7103         mp->next->prev = mp;
7104       else
7105         minipool_vector_tail = mp;
7106     }
7107
7108   min_mp = mp;
7109
7110   offset = 0;
7111   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7112     {
7113       mp->offset = offset;
7114       if (mp->refcount > 0)
7115         offset += mp->fix_size;
7116
7117       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7118         mp->next->min_address = mp->min_address + mp->fix_size;
7119     }
7120
7121   return min_mp;
7122 }
7123
7124 /* Add a constant to the minipool for a backward reference.  Returns the
7125    node added or NULL if the constant will not fit in this pool.
7126
7127    Note that the code for insertion for a backwards reference can be
7128    somewhat confusing because the calculated offsets for each fix do
7129    not take into account the size of the pool (which is still under
7130    construction.  */
7131 static Mnode *
7132 add_minipool_backward_ref (Mfix *fix)
7133 {
7134   /* If set, min_mp is the last pool_entry that has a lower constraint
7135      than the one we are trying to add.  */
7136   Mnode *min_mp = NULL;
7137   /* This can be negative, since it is only a constraint.  */
7138   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
7139   Mnode *mp;
7140
7141   /* If we can't reach the current pool from this insn, or if we can't
7142      insert this entry at the end of the pool without pushing other
7143      fixes out of range, then we don't try.  This ensures that we
7144      can't fail later on.  */
7145   if (min_address >= minipool_barrier->address
7146       || (minipool_vector_tail->min_address + fix->fix_size
7147           >= minipool_barrier->address))
7148     return NULL;
7149
7150   /* Scan the pool to see if a constant with the same value has
7151      already been added.  While we are doing this, also note the
7152      location where we must insert the constant if it doesn't already
7153      exist.  */
7154   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7155     {
7156       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7157           && fix->mode == mp->mode
7158           && (GET_CODE (fix->value) != CODE_LABEL
7159               || (CODE_LABEL_NUMBER (fix->value)
7160                   == CODE_LABEL_NUMBER (mp->value)))
7161           && rtx_equal_p (fix->value, mp->value)
7162           /* Check that there is enough slack to move this entry to the
7163              end of the table (this is conservative).  */
7164           && (mp->max_address
7165               > (minipool_barrier->address
7166                  + minipool_vector_tail->offset
7167                  + minipool_vector_tail->fix_size)))
7168         {
7169           mp->refcount++;
7170           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7171         }
7172
7173       if (min_mp != NULL)
7174         mp->min_address += fix->fix_size;
7175       else
7176         {
7177           /* Note the insertion point if necessary.  */
7178           if (mp->min_address < min_address)
7179             {
7180               /* For now, we do not allow the insertion of 8-byte alignment
7181                  requiring nodes anywhere but at the start of the pool.  */
7182               if (ARM_DOUBLEWORD_ALIGN
7183                   && fix->fix_size == 8 && mp->fix_size != 8)
7184                 return NULL;
7185               else
7186                 min_mp = mp;
7187             }
7188           else if (mp->max_address
7189                    < minipool_barrier->address + mp->offset + fix->fix_size)
7190             {
7191               /* Inserting before this entry would push the fix beyond
7192                  its maximum address (which can happen if we have
7193                  re-located a forwards fix); force the new fix to come
7194                  after it.  */
7195               min_mp = mp;
7196               min_address = mp->min_address + fix->fix_size;
7197             }
7198           /* If we are inserting an 8-bytes aligned quantity and
7199              we have not already found an insertion point, then
7200              make sure that all such 8-byte aligned quantities are
7201              placed at the start of the pool.  */
7202           else if (ARM_DOUBLEWORD_ALIGN
7203                    && min_mp == NULL
7204                    && fix->fix_size == 8
7205                    && mp->fix_size < 8)
7206             {
7207               min_mp = mp;
7208               min_address = mp->min_address + fix->fix_size;
7209             }
7210         }
7211     }
7212
7213   /* We need to create a new entry.  */
7214   mp = xmalloc (sizeof (* mp));
7215   mp->fix_size = fix->fix_size;
7216   mp->mode = fix->mode;
7217   mp->value = fix->value;
7218   mp->refcount = 1;
7219   mp->max_address = minipool_barrier->address + 65536;
7220
7221   mp->min_address = min_address;
7222
7223   if (min_mp == NULL)
7224     {
7225       mp->prev = NULL;
7226       mp->next = minipool_vector_head;
7227
7228       if (mp->next == NULL)
7229         {
7230           minipool_vector_tail = mp;
7231           minipool_vector_label = gen_label_rtx ();
7232         }
7233       else
7234         mp->next->prev = mp;
7235
7236       minipool_vector_head = mp;
7237     }
7238   else
7239     {
7240       mp->next = min_mp->next;
7241       mp->prev = min_mp;
7242       min_mp->next = mp;
7243
7244       if (mp->next != NULL)
7245         mp->next->prev = mp;
7246       else
7247         minipool_vector_tail = mp;
7248     }
7249
7250   /* Save the new entry.  */
7251   min_mp = mp;
7252
7253   if (mp->prev)
7254     mp = mp->prev;
7255   else
7256     mp->offset = 0;
7257
7258   /* Scan over the following entries and adjust their offsets.  */
7259   while (mp->next != NULL)
7260     {
7261       if (mp->next->min_address < mp->min_address + mp->fix_size)
7262         mp->next->min_address = mp->min_address + mp->fix_size;
7263
7264       if (mp->refcount)
7265         mp->next->offset = mp->offset + mp->fix_size;
7266       else
7267         mp->next->offset = mp->offset;
7268
7269       mp = mp->next;
7270     }
7271
7272   return min_mp;
7273 }
7274
7275 static void
7276 assign_minipool_offsets (Mfix *barrier)
7277 {
7278   HOST_WIDE_INT offset = 0;
7279   Mnode *mp;
7280
7281   minipool_barrier = barrier;
7282
7283   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7284     {
7285       mp->offset = offset;
7286
7287       if (mp->refcount > 0)
7288         offset += mp->fix_size;
7289     }
7290 }
7291
7292 /* Output the literal table */
7293 static void
7294 dump_minipool (rtx scan)
7295 {
7296   Mnode * mp;
7297   Mnode * nmp;
7298   int align64 = 0;
7299
7300   if (ARM_DOUBLEWORD_ALIGN)
7301     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7302       if (mp->refcount > 0 && mp->fix_size == 8)
7303         {
7304           align64 = 1;
7305           break;
7306         }
7307
7308   if (dump_file)
7309     fprintf (dump_file,
7310              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7311              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7312
7313   scan = emit_label_after (gen_label_rtx (), scan);
7314   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7315   scan = emit_label_after (minipool_vector_label, scan);
7316
7317   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7318     {
7319       if (mp->refcount > 0)
7320         {
7321           if (dump_file)
7322             {
7323               fprintf (dump_file,
7324                        ";;  Offset %u, min %ld, max %ld ",
7325                        (unsigned) mp->offset, (unsigned long) mp->min_address,
7326                        (unsigned long) mp->max_address);
7327               arm_print_value (dump_file, mp->value);
7328               fputc ('\n', dump_file);
7329             }
7330
7331           switch (mp->fix_size)
7332             {
7333 #ifdef HAVE_consttable_1
7334             case 1:
7335               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7336               break;
7337
7338 #endif
7339 #ifdef HAVE_consttable_2
7340             case 2:
7341               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7342               break;
7343
7344 #endif
7345 #ifdef HAVE_consttable_4
7346             case 4:
7347               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7348               break;
7349
7350 #endif
7351 #ifdef HAVE_consttable_8
7352             case 8:
7353               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7354               break;
7355
7356 #endif
7357             default:
7358               gcc_unreachable ();
7359             }
7360         }
7361
7362       nmp = mp->next;
7363       free (mp);
7364     }
7365
7366   minipool_vector_head = minipool_vector_tail = NULL;
7367   scan = emit_insn_after (gen_consttable_end (), scan);
7368   scan = emit_barrier_after (scan);
7369 }
7370
7371 /* Return the cost of forcibly inserting a barrier after INSN.  */
7372 static int
7373 arm_barrier_cost (rtx insn)
7374 {
7375   /* Basing the location of the pool on the loop depth is preferable,
7376      but at the moment, the basic block information seems to be
7377      corrupt by this stage of the compilation.  */
7378   int base_cost = 50;
7379   rtx next = next_nonnote_insn (insn);
7380
7381   if (next != NULL && GET_CODE (next) == CODE_LABEL)
7382     base_cost -= 20;
7383
7384   switch (GET_CODE (insn))
7385     {
7386     case CODE_LABEL:
7387       /* It will always be better to place the table before the label, rather
7388          than after it.  */
7389       return 50;
7390
7391     case INSN:
7392     case CALL_INSN:
7393       return base_cost;
7394
7395     case JUMP_INSN:
7396       return base_cost - 10;
7397
7398     default:
7399       return base_cost + 10;
7400     }
7401 }
7402
7403 /* Find the best place in the insn stream in the range
7404    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7405    Create the barrier by inserting a jump and add a new fix entry for
7406    it.  */
7407 static Mfix *
7408 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7409 {
7410   HOST_WIDE_INT count = 0;
7411   rtx barrier;
7412   rtx from = fix->insn;
7413   rtx selected = from;
7414   int selected_cost;
7415   HOST_WIDE_INT selected_address;
7416   Mfix * new_fix;
7417   HOST_WIDE_INT max_count = max_address - fix->address;
7418   rtx label = gen_label_rtx ();
7419
7420   selected_cost = arm_barrier_cost (from);
7421   selected_address = fix->address;
7422
7423   while (from && count < max_count)
7424     {
7425       rtx tmp;
7426       int new_cost;
7427
7428       /* This code shouldn't have been called if there was a natural barrier
7429          within range.  */
7430       gcc_assert (GET_CODE (from) != BARRIER);
7431
7432       /* Count the length of this insn.  */
7433       count += get_attr_length (from);
7434
7435       /* If there is a jump table, add its length.  */
7436       tmp = is_jump_table (from);
7437       if (tmp != NULL)
7438         {
7439           count += get_jump_table_size (tmp);
7440
7441           /* Jump tables aren't in a basic block, so base the cost on
7442              the dispatch insn.  If we select this location, we will
7443              still put the pool after the table.  */
7444           new_cost = arm_barrier_cost (from);
7445
7446           if (count < max_count && new_cost <= selected_cost)
7447             {
7448               selected = tmp;
7449               selected_cost = new_cost;
7450               selected_address = fix->address + count;
7451             }
7452
7453           /* Continue after the dispatch table.  */
7454           from = NEXT_INSN (tmp);
7455           continue;
7456         }
7457
7458       new_cost = arm_barrier_cost (from);
7459
7460       if (count < max_count && new_cost <= selected_cost)
7461         {
7462           selected = from;
7463           selected_cost = new_cost;
7464           selected_address = fix->address + count;
7465         }
7466
7467       from = NEXT_INSN (from);
7468     }
7469
7470   /* Create a new JUMP_INSN that branches around a barrier.  */
7471   from = emit_jump_insn_after (gen_jump (label), selected);
7472   JUMP_LABEL (from) = label;
7473   barrier = emit_barrier_after (from);
7474   emit_label_after (label, barrier);
7475
7476   /* Create a minipool barrier entry for the new barrier.  */
7477   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7478   new_fix->insn = barrier;
7479   new_fix->address = selected_address;
7480   new_fix->next = fix->next;
7481   fix->next = new_fix;
7482
7483   return new_fix;
7484 }
7485
7486 /* Record that there is a natural barrier in the insn stream at
7487    ADDRESS.  */
7488 static void
7489 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7490 {
7491   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7492
7493   fix->insn = insn;
7494   fix->address = address;
7495
7496   fix->next = NULL;
7497   if (minipool_fix_head != NULL)
7498     minipool_fix_tail->next = fix;
7499   else
7500     minipool_fix_head = fix;
7501
7502   minipool_fix_tail = fix;
7503 }
7504
7505 /* Record INSN, which will need fixing up to load a value from the
7506    minipool.  ADDRESS is the offset of the insn since the start of the
7507    function; LOC is a pointer to the part of the insn which requires
7508    fixing; VALUE is the constant that must be loaded, which is of type
7509    MODE.  */
7510 static void
7511 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7512                    enum machine_mode mode, rtx value)
7513 {
7514   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7515
7516 #ifdef AOF_ASSEMBLER
7517   /* PIC symbol references need to be converted into offsets into the
7518      based area.  */
7519   /* XXX This shouldn't be done here.  */
7520   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7521     value = aof_pic_entry (value);
7522 #endif /* AOF_ASSEMBLER */
7523
7524   fix->insn = insn;
7525   fix->address = address;
7526   fix->loc = loc;
7527   fix->mode = mode;
7528   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7529   fix->value = value;
7530   fix->forwards = get_attr_pool_range (insn);
7531   fix->backwards = get_attr_neg_pool_range (insn);
7532   fix->minipool = NULL;
7533
7534   /* If an insn doesn't have a range defined for it, then it isn't
7535      expecting to be reworked by this code.  Better to stop now than
7536      to generate duff assembly code.  */
7537   gcc_assert (fix->forwards || fix->backwards);
7538
7539   /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7540      So there might be an empty word before the start of the pool.
7541      Hence we reduce the forward range by 4 to allow for this
7542      possibility.  */
7543   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7544     fix->forwards -= 4;
7545
7546   if (dump_file)
7547     {
7548       fprintf (dump_file,
7549                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7550                GET_MODE_NAME (mode),
7551                INSN_UID (insn), (unsigned long) address,
7552                -1 * (long)fix->backwards, (long)fix->forwards);
7553       arm_print_value (dump_file, fix->value);
7554       fprintf (dump_file, "\n");
7555     }
7556
7557   /* Add it to the chain of fixes.  */
7558   fix->next = NULL;
7559
7560   if (minipool_fix_head != NULL)
7561     minipool_fix_tail->next = fix;
7562   else
7563     minipool_fix_head = fix;
7564
7565   minipool_fix_tail = fix;
7566 }
7567
7568 /* Return the cost of synthesizing a 64-bit constant VAL inline.
7569    Returns the number of insns needed, or 99 if we don't know how to
7570    do it.  */
7571 int
7572 arm_const_double_inline_cost (rtx val)
7573 {
7574   rtx lowpart, highpart;
7575   enum machine_mode mode;
7576   
7577   mode = GET_MODE (val);
7578
7579   if (mode == VOIDmode)
7580     mode = DImode;
7581
7582   gcc_assert (GET_MODE_SIZE (mode) == 8);
7583   
7584   lowpart = gen_lowpart (SImode, val);
7585   highpart = gen_highpart_mode (SImode, mode, val);
7586   
7587   gcc_assert (GET_CODE (lowpart) == CONST_INT);
7588   gcc_assert (GET_CODE (highpart) == CONST_INT);
7589
7590   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
7591                             NULL_RTX, NULL_RTX, 0, 0)
7592           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
7593                               NULL_RTX, NULL_RTX, 0, 0));
7594 }
7595
7596 /* Return true if it is worthwhile to split a 64-bit constant into two
7597    32-bit operations.  This is the case if optimizing for size, or
7598    if we have load delay slots, or if one 32-bit part can be done with
7599    a single data operation.  */
7600 bool
7601 arm_const_double_by_parts (rtx val)
7602 {
7603   enum machine_mode mode = GET_MODE (val);
7604   rtx part;
7605
7606   if (optimize_size || arm_ld_sched)
7607     return true;
7608
7609   if (mode == VOIDmode)
7610     mode = DImode;
7611   
7612   part = gen_highpart_mode (SImode, mode, val);
7613   
7614   gcc_assert (GET_CODE (part) == CONST_INT);
7615   
7616   if (const_ok_for_arm (INTVAL (part))
7617       || const_ok_for_arm (~INTVAL (part)))
7618     return true;
7619   
7620   part = gen_lowpart (SImode, val);
7621   
7622   gcc_assert (GET_CODE (part) == CONST_INT);
7623   
7624   if (const_ok_for_arm (INTVAL (part))
7625       || const_ok_for_arm (~INTVAL (part)))
7626     return true;
7627   
7628   return false;
7629 }
7630
7631 /* Scan INSN and note any of its operands that need fixing.
7632    If DO_PUSHES is false we do not actually push any of the fixups
7633    needed.  The function returns TRUE if any fixups were needed/pushed.
7634    This is used by arm_memory_load_p() which needs to know about loads
7635    of constants that will be converted into minipool loads.  */
7636 static bool
7637 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7638 {
7639   bool result = false;
7640   int opno;
7641
7642   extract_insn (insn);
7643
7644   if (!constrain_operands (1))
7645     fatal_insn_not_found (insn);
7646
7647   if (recog_data.n_alternatives == 0)
7648     return false;
7649
7650   /* Fill in recog_op_alt with information about the constraints of
7651      this insn.  */
7652   preprocess_constraints ();
7653
7654   for (opno = 0; opno < recog_data.n_operands; opno++)
7655     {
7656       /* Things we need to fix can only occur in inputs.  */
7657       if (recog_data.operand_type[opno] != OP_IN)
7658         continue;
7659
7660       /* If this alternative is a memory reference, then any mention
7661          of constants in this alternative is really to fool reload
7662          into allowing us to accept one there.  We need to fix them up
7663          now so that we output the right code.  */
7664       if (recog_op_alt[opno][which_alternative].memory_ok)
7665         {
7666           rtx op = recog_data.operand[opno];
7667
7668           if (CONSTANT_P (op))
7669             {
7670               if (do_pushes)
7671                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7672                                    recog_data.operand_mode[opno], op);
7673               result = true;
7674             }
7675           else if (GET_CODE (op) == MEM
7676                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7677                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7678             {
7679               if (do_pushes)
7680                 {
7681                   rtx cop = avoid_constant_pool_reference (op);
7682
7683                   /* Casting the address of something to a mode narrower
7684                      than a word can cause avoid_constant_pool_reference()
7685                      to return the pool reference itself.  That's no good to
7686                      us here.  Lets just hope that we can use the
7687                      constant pool value directly.  */
7688                   if (op == cop)
7689                     cop = get_pool_constant (XEXP (op, 0));
7690
7691                   push_minipool_fix (insn, address,
7692                                      recog_data.operand_loc[opno],
7693                                      recog_data.operand_mode[opno], cop);
7694                 }
7695
7696               result = true;
7697             }
7698         }
7699     }
7700
7701   return result;
7702 }
7703
7704 /* Gcc puts the pool in the wrong place for ARM, since we can only
7705    load addresses a limited distance around the pc.  We do some
7706    special munging to move the constant pool values to the correct
7707    point in the code.  */
7708 static void
7709 arm_reorg (void)
7710 {
7711   rtx insn;
7712   HOST_WIDE_INT address = 0;
7713   Mfix * fix;
7714
7715   minipool_fix_head = minipool_fix_tail = NULL;
7716
7717   /* The first insn must always be a note, or the code below won't
7718      scan it properly.  */
7719   insn = get_insns ();
7720   gcc_assert (GET_CODE (insn) == NOTE);
7721
7722   /* Scan all the insns and record the operands that will need fixing.  */
7723   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7724     {
7725       if (TARGET_CIRRUS_FIX_INVALID_INSNS
7726           && (arm_cirrus_insn_p (insn)
7727               || GET_CODE (insn) == JUMP_INSN
7728               || arm_memory_load_p (insn)))
7729         cirrus_reorg (insn);
7730
7731       if (GET_CODE (insn) == BARRIER)
7732         push_minipool_barrier (insn, address);
7733       else if (INSN_P (insn))
7734         {
7735           rtx table;
7736
7737           note_invalid_constants (insn, address, true);
7738           address += get_attr_length (insn);
7739
7740           /* If the insn is a vector jump, add the size of the table
7741              and skip the table.  */
7742           if ((table = is_jump_table (insn)) != NULL)
7743             {
7744               address += get_jump_table_size (table);
7745               insn = table;
7746             }
7747         }
7748     }
7749
7750   fix = minipool_fix_head;
7751
7752   /* Now scan the fixups and perform the required changes.  */
7753   while (fix)
7754     {
7755       Mfix * ftmp;
7756       Mfix * fdel;
7757       Mfix *  last_added_fix;
7758       Mfix * last_barrier = NULL;
7759       Mfix * this_fix;
7760
7761       /* Skip any further barriers before the next fix.  */
7762       while (fix && GET_CODE (fix->insn) == BARRIER)
7763         fix = fix->next;
7764
7765       /* No more fixes.  */
7766       if (fix == NULL)
7767         break;
7768
7769       last_added_fix = NULL;
7770
7771       for (ftmp = fix; ftmp; ftmp = ftmp->next)
7772         {
7773           if (GET_CODE (ftmp->insn) == BARRIER)
7774             {
7775               if (ftmp->address >= minipool_vector_head->max_address)
7776                 break;
7777
7778               last_barrier = ftmp;
7779             }
7780           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7781             break;
7782
7783           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
7784         }
7785
7786       /* If we found a barrier, drop back to that; any fixes that we
7787          could have reached but come after the barrier will now go in
7788          the next mini-pool.  */
7789       if (last_barrier != NULL)
7790         {
7791           /* Reduce the refcount for those fixes that won't go into this
7792              pool after all.  */
7793           for (fdel = last_barrier->next;
7794                fdel && fdel != ftmp;
7795                fdel = fdel->next)
7796             {
7797               fdel->minipool->refcount--;
7798               fdel->minipool = NULL;
7799             }
7800
7801           ftmp = last_barrier;
7802         }
7803       else
7804         {
7805           /* ftmp is first fix that we can't fit into this pool and
7806              there no natural barriers that we could use.  Insert a
7807              new barrier in the code somewhere between the previous
7808              fix and this one, and arrange to jump around it.  */
7809           HOST_WIDE_INT max_address;
7810
7811           /* The last item on the list of fixes must be a barrier, so
7812              we can never run off the end of the list of fixes without
7813              last_barrier being set.  */
7814           gcc_assert (ftmp);
7815
7816           max_address = minipool_vector_head->max_address;
7817           /* Check that there isn't another fix that is in range that
7818              we couldn't fit into this pool because the pool was
7819              already too large: we need to put the pool before such an
7820              instruction.  */
7821           if (ftmp->address < max_address)
7822             max_address = ftmp->address;
7823
7824           last_barrier = create_fix_barrier (last_added_fix, max_address);
7825         }
7826
7827       assign_minipool_offsets (last_barrier);
7828
7829       while (ftmp)
7830         {
7831           if (GET_CODE (ftmp->insn) != BARRIER
7832               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7833                   == NULL))
7834             break;
7835
7836           ftmp = ftmp->next;
7837         }
7838
7839       /* Scan over the fixes we have identified for this pool, fixing them
7840          up and adding the constants to the pool itself.  */
7841       for (this_fix = fix; this_fix && ftmp != this_fix;
7842            this_fix = this_fix->next)
7843         if (GET_CODE (this_fix->insn) != BARRIER)
7844           {
7845             rtx addr
7846               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
7847                                                   minipool_vector_label),
7848                                this_fix->minipool->offset);
7849             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7850           }
7851
7852       dump_minipool (last_barrier->insn);
7853       fix = ftmp;
7854     }
7855
7856   /* From now on we must synthesize any constants that we can't handle
7857      directly.  This can happen if the RTL gets split during final
7858      instruction generation.  */
7859   after_arm_reorg = 1;
7860
7861   /* Free the minipool memory.  */
7862   obstack_free (&minipool_obstack, minipool_startobj);
7863 }
7864 \f
7865 /* Routines to output assembly language.  */
7866
7867 /* If the rtx is the correct value then return the string of the number.
7868    In this way we can ensure that valid double constants are generated even
7869    when cross compiling.  */
7870 const char *
7871 fp_immediate_constant (rtx x)
7872 {
7873   REAL_VALUE_TYPE r;
7874   int i;
7875
7876   if (!fp_consts_inited)
7877     init_fp_table ();
7878
7879   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7880   for (i = 0; i < 8; i++)
7881     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7882       return strings_fp[i];
7883
7884   gcc_unreachable ();
7885 }
7886
7887 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
7888 static const char *
7889 fp_const_from_val (REAL_VALUE_TYPE *r)
7890 {
7891   int i;
7892
7893   if (!fp_consts_inited)
7894     init_fp_table ();
7895
7896   for (i = 0; i < 8; i++)
7897     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7898       return strings_fp[i];
7899
7900   gcc_unreachable ();
7901 }
7902
7903 /* Output the operands of a LDM/STM instruction to STREAM.
7904    MASK is the ARM register set mask of which only bits 0-15 are important.
7905    REG is the base register, either the frame pointer or the stack pointer,
7906    INSTR is the possibly suffixed load or store instruction.  */
7907
7908 static void
7909 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
7910                  unsigned long mask)
7911 {
7912   unsigned i;
7913   bool not_first = FALSE;
7914
7915   fputc ('\t', stream);
7916   asm_fprintf (stream, instr, reg);
7917   fputs (", {", stream);
7918
7919   for (i = 0; i <= LAST_ARM_REGNUM; i++)
7920     if (mask & (1 << i))
7921       {
7922         if (not_first)
7923           fprintf (stream, ", ");
7924
7925         asm_fprintf (stream, "%r", i);
7926         not_first = TRUE;
7927       }
7928
7929   fprintf (stream, "}\n");
7930 }
7931
7932
7933 /* Output a FLDMX instruction to STREAM.
7934    BASE if the register containing the address.
7935    REG and COUNT specify the register range.
7936    Extra registers may be added to avoid hardware bugs.  */
7937
7938 static void
7939 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7940 {
7941   int i;
7942
7943   /* Workaround ARM10 VFPr1 bug.  */
7944   if (count == 2 && !arm_arch6)
7945     {
7946       if (reg == 15)
7947         reg--;
7948       count++;
7949     }
7950
7951   fputc ('\t', stream);
7952   asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7953
7954   for (i = reg; i < reg + count; i++)
7955     {
7956       if (i > reg)
7957         fputs (", ", stream);
7958       asm_fprintf (stream, "d%d", i);
7959     }
7960   fputs ("}\n", stream);
7961
7962 }
7963
7964
7965 /* Output the assembly for a store multiple.  */
7966
7967 const char *
7968 vfp_output_fstmx (rtx * operands)
7969 {
7970   char pattern[100];
7971   int p;
7972   int base;
7973   int i;
7974
7975   strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7976   p = strlen (pattern);
7977
7978   gcc_assert (GET_CODE (operands[1]) == REG);
7979
7980   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7981   for (i = 1; i < XVECLEN (operands[2], 0); i++)
7982     {
7983       p += sprintf (&pattern[p], ", d%d", base + i);
7984     }
7985   strcpy (&pattern[p], "}");
7986
7987   output_asm_insn (pattern, operands);
7988   return "";
7989 }
7990
7991
7992 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
7993    number of bytes pushed.  */
7994
7995 static int
7996 vfp_emit_fstmx (int base_reg, int count)
7997 {
7998   rtx par;
7999   rtx dwarf;
8000   rtx tmp, reg;
8001   int i;
8002
8003   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
8004      register pairs are stored by a store multiple insn.  We avoid this
8005      by pushing an extra pair.  */
8006   if (count == 2 && !arm_arch6)
8007     {
8008       if (base_reg == LAST_VFP_REGNUM - 3)
8009         base_reg -= 2;
8010       count++;
8011     }
8012
8013   /* ??? The frame layout is implementation defined.  We describe
8014      standard format 1 (equivalent to a FSTMD insn and unused pad word).
8015      We really need some way of representing the whole block so that the
8016      unwinder can figure it out at runtime.  */
8017   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8018   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
8019
8020   reg = gen_rtx_REG (DFmode, base_reg);
8021   base_reg += 2;
8022
8023   XVECEXP (par, 0, 0)
8024     = gen_rtx_SET (VOIDmode,
8025                    gen_rtx_MEM (BLKmode,
8026                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
8027                    gen_rtx_UNSPEC (BLKmode,
8028                                    gen_rtvec (1, reg),
8029                                    UNSPEC_PUSH_MULT));
8030
8031   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8032                      gen_rtx_PLUS (SImode, stack_pointer_rtx,
8033                                    GEN_INT (-(count * 8 + 4))));
8034   RTX_FRAME_RELATED_P (tmp) = 1;
8035   XVECEXP (dwarf, 0, 0) = tmp;
8036
8037   tmp = gen_rtx_SET (VOIDmode,
8038                      gen_rtx_MEM (DFmode, stack_pointer_rtx),
8039                      reg);
8040   RTX_FRAME_RELATED_P (tmp) = 1;
8041   XVECEXP (dwarf, 0, 1) = tmp;
8042
8043   for (i = 1; i < count; i++)
8044     {
8045       reg = gen_rtx_REG (DFmode, base_reg);
8046       base_reg += 2;
8047       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8048
8049       tmp = gen_rtx_SET (VOIDmode,
8050                          gen_rtx_MEM (DFmode,
8051                                       gen_rtx_PLUS (SImode,
8052                                                     stack_pointer_rtx,
8053                                                     GEN_INT (i * 8))),
8054                          reg);
8055       RTX_FRAME_RELATED_P (tmp) = 1;
8056       XVECEXP (dwarf, 0, i + 1) = tmp;
8057     }
8058
8059   par = emit_insn (par);
8060   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8061                                        REG_NOTES (par));
8062   RTX_FRAME_RELATED_P (par) = 1;
8063
8064   return count * 8 + 4;
8065 }
8066
8067
8068 /* Output a 'call' insn.  */
8069 const char *
8070 output_call (rtx *operands)
8071 {
8072   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
8073
8074   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
8075   if (REGNO (operands[0]) == LR_REGNUM)
8076     {
8077       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8078       output_asm_insn ("mov%?\t%0, %|lr", operands);
8079     }
8080
8081   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8082
8083   if (TARGET_INTERWORK || arm_arch4t)
8084     output_asm_insn ("bx%?\t%0", operands);
8085   else
8086     output_asm_insn ("mov%?\t%|pc, %0", operands);
8087
8088   return "";
8089 }
8090
8091 /* Output a 'call' insn that is a reference in memory.  */
8092 const char *
8093 output_call_mem (rtx *operands)
8094 {
8095   if (TARGET_INTERWORK && !arm_arch5)
8096     {
8097       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8098       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8099       output_asm_insn ("bx%?\t%|ip", operands);
8100     }
8101   else if (regno_use_in (LR_REGNUM, operands[0]))
8102     {
8103       /* LR is used in the memory address.  We load the address in the
8104          first instruction.  It's safe to use IP as the target of the
8105          load since the call will kill it anyway.  */
8106       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8107       if (arm_arch5)
8108         output_asm_insn ("blx%?\t%|ip", operands);
8109       else
8110         {
8111           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8112           if (arm_arch4t)
8113             output_asm_insn ("bx%?\t%|ip", operands);
8114           else
8115             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8116         }
8117     }
8118   else
8119     {
8120       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8121       output_asm_insn ("ldr%?\t%|pc, %0", operands);
8122     }
8123
8124   return "";
8125 }
8126
8127
8128 /* Output a move from arm registers to an fpa registers.
8129    OPERANDS[0] is an fpa register.
8130    OPERANDS[1] is the first registers of an arm register pair.  */
8131 const char *
8132 output_mov_long_double_fpa_from_arm (rtx *operands)
8133 {
8134   int arm_reg0 = REGNO (operands[1]);
8135   rtx ops[3];
8136
8137   gcc_assert (arm_reg0 != IP_REGNUM);
8138
8139   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8140   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8141   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8142
8143   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8144   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8145
8146   return "";
8147 }
8148
8149 /* Output a move from an fpa register to arm registers.
8150    OPERANDS[0] is the first registers of an arm register pair.
8151    OPERANDS[1] is an fpa register.  */
8152 const char *
8153 output_mov_long_double_arm_from_fpa (rtx *operands)
8154 {
8155   int arm_reg0 = REGNO (operands[0]);
8156   rtx ops[3];
8157
8158   gcc_assert (arm_reg0 != IP_REGNUM);
8159
8160   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8161   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8162   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8163
8164   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8165   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8166   return "";
8167 }
8168
8169 /* Output a move from arm registers to arm registers of a long double
8170    OPERANDS[0] is the destination.
8171    OPERANDS[1] is the source.  */
8172 const char *
8173 output_mov_long_double_arm_from_arm (rtx *operands)
8174 {
8175   /* We have to be careful here because the two might overlap.  */
8176   int dest_start = REGNO (operands[0]);
8177   int src_start = REGNO (operands[1]);
8178   rtx ops[2];
8179   int i;
8180
8181   if (dest_start < src_start)
8182     {
8183       for (i = 0; i < 3; i++)
8184         {
8185           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8186           ops[1] = gen_rtx_REG (SImode, src_start + i);
8187           output_asm_insn ("mov%?\t%0, %1", ops);
8188         }
8189     }
8190   else
8191     {
8192       for (i = 2; i >= 0; i--)
8193         {
8194           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8195           ops[1] = gen_rtx_REG (SImode, src_start + i);
8196           output_asm_insn ("mov%?\t%0, %1", ops);
8197         }
8198     }
8199
8200   return "";
8201 }
8202
8203
8204 /* Output a move from arm registers to an fpa registers.
8205    OPERANDS[0] is an fpa register.
8206    OPERANDS[1] is the first registers of an arm register pair.  */
8207 const char *
8208 output_mov_double_fpa_from_arm (rtx *operands)
8209 {
8210   int arm_reg0 = REGNO (operands[1]);
8211   rtx ops[2];
8212
8213   gcc_assert (arm_reg0 != IP_REGNUM);
8214
8215   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8216   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8217   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8218   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8219   return "";
8220 }
8221
8222 /* Output a move from an fpa register to arm registers.
8223    OPERANDS[0] is the first registers of an arm register pair.
8224    OPERANDS[1] is an fpa register.  */
8225 const char *
8226 output_mov_double_arm_from_fpa (rtx *operands)
8227 {
8228   int arm_reg0 = REGNO (operands[0]);
8229   rtx ops[2];
8230
8231   gcc_assert (arm_reg0 != IP_REGNUM);
8232
8233   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8234   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8235   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8236   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8237   return "";
8238 }
8239
8240 /* Output a move between double words.
8241    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8242    or MEM<-REG and all MEMs must be offsettable addresses.  */
8243 const char *
8244 output_move_double (rtx *operands)
8245 {
8246   enum rtx_code code0 = GET_CODE (operands[0]);
8247   enum rtx_code code1 = GET_CODE (operands[1]);
8248   rtx otherops[3];
8249
8250   if (code0 == REG)
8251     {
8252       int reg0 = REGNO (operands[0]);
8253
8254       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8255
8256       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
8257
8258       switch (GET_CODE (XEXP (operands[1], 0)))
8259         {
8260         case REG:
8261           output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8262           break;
8263           
8264         case PRE_INC:
8265           gcc_assert (TARGET_LDRD);
8266           output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8267           break;
8268           
8269         case PRE_DEC:
8270           output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8271           break;
8272           
8273         case POST_INC:
8274           output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8275           break;
8276           
8277         case POST_DEC:
8278           gcc_assert (TARGET_LDRD);
8279           output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8280           break;
8281           
8282         case PRE_MODIFY:
8283         case POST_MODIFY:
8284           otherops[0] = operands[0];
8285           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8286           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8287           
8288           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8289             {
8290               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8291                 {
8292                   /* Registers overlap so split out the increment.  */
8293                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
8294                   output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8295                 }
8296               else
8297                 output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8298             }
8299           else
8300             {
8301               /* We only allow constant increments, so this is safe.  */
8302               output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8303             }
8304           break;
8305           
8306         case LABEL_REF:
8307         case CONST:
8308           output_asm_insn ("adr%?\t%0, %1", operands);
8309           output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8310           break;
8311           
8312         default:
8313           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8314                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8315             {
8316               otherops[0] = operands[0];
8317               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8318               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8319               
8320               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8321                 {
8322                   if (GET_CODE (otherops[2]) == CONST_INT)
8323                     {
8324                       switch ((int) INTVAL (otherops[2]))
8325                         {
8326                         case -8:
8327                           output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8328                           return "";
8329                         case -4:
8330                           output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8331                           return "";
8332                         case 4:
8333                           output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8334                           return "";
8335                         }
8336                     }
8337                   if (TARGET_LDRD
8338                       && (GET_CODE (otherops[2]) == REG
8339                           || (GET_CODE (otherops[2]) == CONST_INT
8340                               && INTVAL (otherops[2]) > -256
8341                               && INTVAL (otherops[2]) < 256)))
8342                     {
8343                       if (reg_overlap_mentioned_p (otherops[0],
8344                                                    otherops[2]))
8345                         {
8346                           /* Swap base and index registers over to
8347                              avoid a conflict.  */
8348                           otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8349                           otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8350                           
8351                         }
8352                       /* If both registers conflict, it will usually
8353                          have been fixed by a splitter.  */
8354                       if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8355                         {
8356                           output_asm_insn ("add%?\t%1, %1, %2", otherops);
8357                           output_asm_insn ("ldr%?d\t%0, [%1]",
8358                                            otherops);
8359                         }
8360                       else
8361                         output_asm_insn ("ldr%?d\t%0, [%1, %2]", otherops);
8362                       return "";
8363                     }
8364                   
8365                   if (GET_CODE (otherops[2]) == CONST_INT)
8366                     {
8367                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8368                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8369                       else
8370                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
8371                     }
8372                   else
8373                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
8374                 }
8375               else
8376                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8377
8378               return "ldm%?ia\t%0, %M0";
8379             }
8380           else
8381             {
8382               otherops[1] = adjust_address (operands[1], SImode, 4);
8383               /* Take care of overlapping base/data reg.  */
8384               if (reg_mentioned_p (operands[0], operands[1]))
8385                 {
8386                   output_asm_insn ("ldr%?\t%0, %1", otherops);
8387                   output_asm_insn ("ldr%?\t%0, %1", operands);
8388                 }
8389               else
8390                 {
8391                   output_asm_insn ("ldr%?\t%0, %1", operands);
8392                   output_asm_insn ("ldr%?\t%0, %1", otherops);
8393                 }
8394             }
8395         }
8396     }
8397   else
8398     {
8399       /* Constraints should ensure this.  */
8400       gcc_assert (code0 == MEM && code1 == REG);
8401       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
8402
8403       switch (GET_CODE (XEXP (operands[0], 0)))
8404         {
8405         case REG:
8406           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8407           break;
8408
8409         case PRE_INC:
8410           gcc_assert (TARGET_LDRD);
8411           output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8412           break;
8413
8414         case PRE_DEC:
8415           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8416           break;
8417
8418         case POST_INC:
8419           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8420           break;
8421
8422         case POST_DEC:
8423           gcc_assert (TARGET_LDRD);
8424           output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8425           break;
8426
8427         case PRE_MODIFY:
8428         case POST_MODIFY:
8429           otherops[0] = operands[1];
8430           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8431           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8432
8433           if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8434             output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8435           else
8436             output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8437           break;
8438
8439         case PLUS:
8440           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8441           if (GET_CODE (otherops[2]) == CONST_INT)
8442             {
8443               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8444                 {
8445                 case -8:
8446                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
8447                   return "";
8448
8449                 case -4:
8450                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
8451                   return "";
8452
8453                 case 4:
8454                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8455                   return "";
8456                 }
8457             }
8458           if (TARGET_LDRD
8459               && (GET_CODE (otherops[2]) == REG
8460                   || (GET_CODE (otherops[2]) == CONST_INT
8461                       && INTVAL (otherops[2]) > -256
8462                       && INTVAL (otherops[2]) < 256)))
8463             {
8464               otherops[0] = operands[1];
8465               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8466               output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8467               return "";
8468             }
8469           /* Fall through */
8470
8471         default:
8472           otherops[0] = adjust_address (operands[0], SImode, 4);
8473           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8474           output_asm_insn ("str%?\t%1, %0", operands);
8475           output_asm_insn ("str%?\t%1, %0", otherops);
8476         }
8477     }
8478
8479   return "";
8480 }
8481
8482 /* Output an ADD r, s, #n where n may be too big for one instruction.
8483    If adding zero to one register, output nothing.  */
8484 const char *
8485 output_add_immediate (rtx *operands)
8486 {
8487   HOST_WIDE_INT n = INTVAL (operands[2]);
8488
8489   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8490     {
8491       if (n < 0)
8492         output_multi_immediate (operands,
8493                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8494                                 -n);
8495       else
8496         output_multi_immediate (operands,
8497                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8498                                 n);
8499     }
8500
8501   return "";
8502 }
8503
8504 /* Output a multiple immediate operation.
8505    OPERANDS is the vector of operands referred to in the output patterns.
8506    INSTR1 is the output pattern to use for the first constant.
8507    INSTR2 is the output pattern to use for subsequent constants.
8508    IMMED_OP is the index of the constant slot in OPERANDS.
8509    N is the constant value.  */
8510 static const char *
8511 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8512                         int immed_op, HOST_WIDE_INT n)
8513 {
8514 #if HOST_BITS_PER_WIDE_INT > 32
8515   n &= 0xffffffff;
8516 #endif
8517
8518   if (n == 0)
8519     {
8520       /* Quick and easy output.  */
8521       operands[immed_op] = const0_rtx;
8522       output_asm_insn (instr1, operands);
8523     }
8524   else
8525     {
8526       int i;
8527       const char * instr = instr1;
8528
8529       /* Note that n is never zero here (which would give no output).  */
8530       for (i = 0; i < 32; i += 2)
8531         {
8532           if (n & (3 << i))
8533             {
8534               operands[immed_op] = GEN_INT (n & (255 << i));
8535               output_asm_insn (instr, operands);
8536               instr = instr2;
8537               i += 6;
8538             }
8539         }
8540     }
8541
8542   return "";
8543 }
8544
8545 /* Return the appropriate ARM instruction for the operation code.
8546    The returned result should not be overwritten.  OP is the rtx of the
8547    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8548    was shifted.  */
8549 const char *
8550 arithmetic_instr (rtx op, int shift_first_arg)
8551 {
8552   switch (GET_CODE (op))
8553     {
8554     case PLUS:
8555       return "add";
8556
8557     case MINUS:
8558       return shift_first_arg ? "rsb" : "sub";
8559
8560     case IOR:
8561       return "orr";
8562
8563     case XOR:
8564       return "eor";
8565
8566     case AND:
8567       return "and";
8568
8569     default:
8570       gcc_unreachable ();
8571     }
8572 }
8573
8574 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8575    for the operation code.  The returned result should not be overwritten.
8576    OP is the rtx code of the shift.
8577    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8578    shift.  */
8579 static const char *
8580 shift_op (rtx op, HOST_WIDE_INT *amountp)
8581 {
8582   const char * mnem;
8583   enum rtx_code code = GET_CODE (op);
8584
8585   switch (GET_CODE (XEXP (op, 1)))
8586     {
8587     case REG:
8588     case SUBREG:
8589       *amountp = -1;
8590       break;
8591
8592     case CONST_INT:
8593       *amountp = INTVAL (XEXP (op, 1));
8594       break;
8595
8596     default:
8597       gcc_unreachable ();
8598     }
8599
8600   switch (code)
8601     {
8602     case ASHIFT:
8603       mnem = "asl";
8604       break;
8605
8606     case ASHIFTRT:
8607       mnem = "asr";
8608       break;
8609
8610     case LSHIFTRT:
8611       mnem = "lsr";
8612       break;
8613
8614     case ROTATE:
8615       gcc_assert (*amountp != -1);
8616       *amountp = 32 - *amountp;
8617
8618       /* Fall through.  */
8619
8620     case ROTATERT:
8621       mnem = "ror";
8622       break;
8623
8624     case MULT:
8625       /* We never have to worry about the amount being other than a
8626          power of 2, since this case can never be reloaded from a reg.  */
8627       gcc_assert (*amountp != -1);
8628       *amountp = int_log2 (*amountp);
8629       return "asl";
8630
8631     default:
8632       gcc_unreachable ();
8633     }
8634
8635   if (*amountp != -1)
8636     {
8637       /* This is not 100% correct, but follows from the desire to merge
8638          multiplication by a power of 2 with the recognizer for a
8639          shift.  >=32 is not a valid shift for "asl", so we must try and
8640          output a shift that produces the correct arithmetical result.
8641          Using lsr #32 is identical except for the fact that the carry bit
8642          is not set correctly if we set the flags; but we never use the
8643          carry bit from such an operation, so we can ignore that.  */
8644       if (code == ROTATERT)
8645         /* Rotate is just modulo 32.  */
8646         *amountp &= 31;
8647       else if (*amountp != (*amountp & 31))
8648         {
8649           if (code == ASHIFT)
8650             mnem = "lsr";
8651           *amountp = 32;
8652         }
8653
8654       /* Shifts of 0 are no-ops.  */
8655       if (*amountp == 0)
8656         return NULL;
8657     }
8658
8659   return mnem;
8660 }
8661
8662 /* Obtain the shift from the POWER of two.  */
8663
8664 static HOST_WIDE_INT
8665 int_log2 (HOST_WIDE_INT power)
8666 {
8667   HOST_WIDE_INT shift = 0;
8668
8669   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8670     {
8671       gcc_assert (shift <= 31);
8672       shift++;
8673     }
8674
8675   return shift;
8676 }
8677
8678 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
8679    because /bin/as is horribly restrictive.  The judgement about
8680    whether or not each character is 'printable' (and can be output as
8681    is) or not (and must be printed with an octal escape) must be made
8682    with reference to the *host* character set -- the situation is
8683    similar to that discussed in the comments above pp_c_char in
8684    c-pretty-print.c.  */
8685
8686 #define MAX_ASCII_LEN 51
8687
8688 void
8689 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8690 {
8691   int i;
8692   int len_so_far = 0;
8693
8694   fputs ("\t.ascii\t\"", stream);
8695
8696   for (i = 0; i < len; i++)
8697     {
8698       int c = p[i];
8699
8700       if (len_so_far >= MAX_ASCII_LEN)
8701         {
8702           fputs ("\"\n\t.ascii\t\"", stream);
8703           len_so_far = 0;
8704         }
8705
8706       if (ISPRINT (c))
8707         {
8708           if (c == '\\' || c == '\"')
8709             {
8710               putc ('\\', stream);
8711               len_so_far++;
8712             }
8713           putc (c, stream);
8714           len_so_far++;
8715         }
8716       else
8717         {
8718           fprintf (stream, "\\%03o", c);
8719           len_so_far += 4;
8720         }
8721     }
8722
8723   fputs ("\"\n", stream);
8724 }
8725 \f
8726 /* Compute the register save mask for registers 0 through 12
8727    inclusive.  This code is used by arm_compute_save_reg_mask.  */
8728
8729 static unsigned long
8730 arm_compute_save_reg0_reg12_mask (void)
8731 {
8732   unsigned long func_type = arm_current_func_type ();
8733   unsigned long save_reg_mask = 0;
8734   unsigned int reg;
8735
8736   if (IS_INTERRUPT (func_type))
8737     {
8738       unsigned int max_reg;
8739       /* Interrupt functions must not corrupt any registers,
8740          even call clobbered ones.  If this is a leaf function
8741          we can just examine the registers used by the RTL, but
8742          otherwise we have to assume that whatever function is
8743          called might clobber anything, and so we have to save
8744          all the call-clobbered registers as well.  */
8745       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8746         /* FIQ handlers have registers r8 - r12 banked, so
8747            we only need to check r0 - r7, Normal ISRs only
8748            bank r14 and r15, so we must check up to r12.
8749            r13 is the stack pointer which is always preserved,
8750            so we do not need to consider it here.  */
8751         max_reg = 7;
8752       else
8753         max_reg = 12;
8754
8755       for (reg = 0; reg <= max_reg; reg++)
8756         if (regs_ever_live[reg]
8757             || (! current_function_is_leaf && call_used_regs [reg]))
8758           save_reg_mask |= (1 << reg);
8759
8760       /* Also save the pic base register if necessary.  */
8761       if (flag_pic
8762           && !TARGET_SINGLE_PIC_BASE
8763           && current_function_uses_pic_offset_table)
8764         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8765     }
8766   else
8767     {
8768       /* In the normal case we only need to save those registers
8769          which are call saved and which are used by this function.  */
8770       for (reg = 0; reg <= 10; reg++)
8771         if (regs_ever_live[reg] && ! call_used_regs [reg])
8772           save_reg_mask |= (1 << reg);
8773
8774       /* Handle the frame pointer as a special case.  */
8775       if (! TARGET_APCS_FRAME
8776           && ! frame_pointer_needed
8777           && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8778           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8779         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8780
8781       /* If we aren't loading the PIC register,
8782          don't stack it even though it may be live.  */
8783       if (flag_pic
8784           && !TARGET_SINGLE_PIC_BASE 
8785           && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
8786               || current_function_uses_pic_offset_table))
8787         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8788     }
8789
8790   /* Save registers so the exception handler can modify them.  */
8791   if (current_function_calls_eh_return)
8792     {
8793       unsigned int i;
8794
8795       for (i = 0; ; i++)
8796         {
8797           reg = EH_RETURN_DATA_REGNO (i);
8798           if (reg == INVALID_REGNUM)
8799             break;
8800           save_reg_mask |= 1 << reg;
8801         }
8802     }
8803
8804   return save_reg_mask;
8805 }
8806
8807 /* Compute a bit mask of which registers need to be
8808    saved on the stack for the current function.  */
8809
8810 static unsigned long
8811 arm_compute_save_reg_mask (void)
8812 {
8813   unsigned int save_reg_mask = 0;
8814   unsigned long func_type = arm_current_func_type ();
8815
8816   if (IS_NAKED (func_type))
8817     /* This should never really happen.  */
8818     return 0;
8819
8820   /* If we are creating a stack frame, then we must save the frame pointer,
8821      IP (which will hold the old stack pointer), LR and the PC.  */
8822   if (frame_pointer_needed)
8823     save_reg_mask |=
8824       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8825       | (1 << IP_REGNUM)
8826       | (1 << LR_REGNUM)
8827       | (1 << PC_REGNUM);
8828
8829   /* Volatile functions do not return, so there
8830      is no need to save any other registers.  */
8831   if (IS_VOLATILE (func_type))
8832     return save_reg_mask;
8833
8834   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8835
8836   /* Decide if we need to save the link register.
8837      Interrupt routines have their own banked link register,
8838      so they never need to save it.
8839      Otherwise if we do not use the link register we do not need to save
8840      it.  If we are pushing other registers onto the stack however, we
8841      can save an instruction in the epilogue by pushing the link register
8842      now and then popping it back into the PC.  This incurs extra memory
8843      accesses though, so we only do it when optimizing for size, and only
8844      if we know that we will not need a fancy return sequence.  */
8845   if (regs_ever_live [LR_REGNUM]
8846           || (save_reg_mask
8847               && optimize_size
8848               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
8849               && !current_function_calls_eh_return))
8850     save_reg_mask |= 1 << LR_REGNUM;
8851
8852   if (cfun->machine->lr_save_eliminated)
8853     save_reg_mask &= ~ (1 << LR_REGNUM);
8854
8855   if (TARGET_REALLY_IWMMXT
8856       && ((bit_count (save_reg_mask)
8857            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8858     {
8859       unsigned int reg;
8860
8861       /* The total number of registers that are going to be pushed
8862          onto the stack is odd.  We need to ensure that the stack
8863          is 64-bit aligned before we start to save iWMMXt registers,
8864          and also before we start to create locals.  (A local variable
8865          might be a double or long long which we will load/store using
8866          an iWMMXt instruction).  Therefore we need to push another
8867          ARM register, so that the stack will be 64-bit aligned.  We
8868          try to avoid using the arg registers (r0 -r3) as they might be
8869          used to pass values in a tail call.  */
8870       for (reg = 4; reg <= 12; reg++)
8871         if ((save_reg_mask & (1 << reg)) == 0)
8872           break;
8873
8874       if (reg <= 12)
8875         save_reg_mask |= (1 << reg);
8876       else
8877         {
8878           cfun->machine->sibcall_blocked = 1;
8879           save_reg_mask |= (1 << 3);
8880         }
8881     }
8882
8883   return save_reg_mask;
8884 }
8885
8886
8887 /* Compute a bit mask of which registers need to be
8888    saved on the stack for the current function.  */
8889 static unsigned long
8890 thumb_compute_save_reg_mask (void)
8891 {
8892   unsigned long mask;
8893   unsigned reg;
8894
8895   mask = 0;
8896   for (reg = 0; reg < 12; reg ++)
8897     if (regs_ever_live[reg] && !call_used_regs[reg])
8898       mask |= 1 << reg;
8899
8900   if (flag_pic && !TARGET_SINGLE_PIC_BASE)
8901     mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
8902
8903   if (TARGET_SINGLE_PIC_BASE)
8904     mask &= ~(1 << arm_pic_register);
8905
8906   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
8907   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
8908     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
8909
8910   /* LR will also be pushed if any lo regs are pushed.  */
8911   if (mask & 0xff || thumb_force_lr_save ())
8912     mask |= (1 << LR_REGNUM);
8913
8914   /* Make sure we have a low work register if we need one.
8915      We will need one if we are going to push a high register,
8916      but we are not currently intending to push a low register.  */
8917   if ((mask & 0xff) == 0
8918       && ((mask & 0x0f00) || TARGET_BACKTRACE))
8919     {
8920       /* Use thumb_find_work_register to choose which register
8921          we will use.  If the register is live then we will
8922          have to push it.  Use LAST_LO_REGNUM as our fallback
8923          choice for the register to select.  */
8924       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
8925
8926       if (! call_used_regs[reg])
8927         mask |= 1 << reg;
8928     }
8929
8930   return mask;
8931 }
8932
8933
8934 /* Return the number of bytes required to save VFP registers.  */
8935 static int
8936 arm_get_vfp_saved_size (void)
8937 {
8938   unsigned int regno;
8939   int count;
8940   int saved;
8941
8942   saved = 0;
8943   /* Space for saved VFP registers.  */
8944   if (TARGET_HARD_FLOAT && TARGET_VFP)
8945     {
8946       count = 0;
8947       for (regno = FIRST_VFP_REGNUM;
8948            regno < LAST_VFP_REGNUM;
8949            regno += 2)
8950         {
8951           if ((!regs_ever_live[regno] || call_used_regs[regno])
8952               && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8953             {
8954               if (count > 0)
8955                 {
8956                   /* Workaround ARM10 VFPr1 bug.  */
8957                   if (count == 2 && !arm_arch6)
8958                     count++;
8959                   saved += count * 8 + 4;
8960                 }
8961               count = 0;
8962             }
8963           else
8964             count++;
8965         }
8966       if (count > 0)
8967         {
8968           if (count == 2 && !arm_arch6)
8969             count++;
8970           saved += count * 8 + 4;
8971         }
8972     }
8973   return saved;
8974 }
8975
8976
8977 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
8978    everything bar the final return instruction.  */
8979 const char *
8980 output_return_instruction (rtx operand, int really_return, int reverse)
8981 {
8982   char conditional[10];
8983   char instr[100];
8984   unsigned reg;
8985   unsigned long live_regs_mask;
8986   unsigned long func_type;
8987   arm_stack_offsets *offsets;
8988
8989   func_type = arm_current_func_type ();
8990
8991   if (IS_NAKED (func_type))
8992     return "";
8993
8994   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
8995     {
8996       /* If this function was declared non-returning, and we have
8997          found a tail call, then we have to trust that the called
8998          function won't return.  */
8999       if (really_return)
9000         {
9001           rtx ops[2];
9002
9003           /* Otherwise, trap an attempted return by aborting.  */
9004           ops[0] = operand;
9005           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
9006                                        : "abort");
9007           assemble_external_libcall (ops[1]);
9008           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9009         }
9010
9011       return "";
9012     }
9013
9014   gcc_assert (!current_function_calls_alloca || really_return);
9015
9016   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9017
9018   return_used_this_function = 1;
9019
9020   live_regs_mask = arm_compute_save_reg_mask ();
9021
9022   if (live_regs_mask)
9023     {
9024       const char * return_reg;
9025
9026       /* If we do not have any special requirements for function exit
9027          (e.g. interworking, or ISR) then we can load the return address
9028          directly into the PC.  Otherwise we must load it into LR.  */
9029       if (really_return
9030           && ! TARGET_INTERWORK)
9031         return_reg = reg_names[PC_REGNUM];
9032       else
9033         return_reg = reg_names[LR_REGNUM];
9034
9035       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9036         {
9037           /* There are three possible reasons for the IP register
9038              being saved.  1) a stack frame was created, in which case
9039              IP contains the old stack pointer, or 2) an ISR routine
9040              corrupted it, or 3) it was saved to align the stack on
9041              iWMMXt.  In case 1, restore IP into SP, otherwise just
9042              restore IP.  */
9043           if (frame_pointer_needed)
9044             {
9045               live_regs_mask &= ~ (1 << IP_REGNUM);
9046               live_regs_mask |=   (1 << SP_REGNUM);
9047             }
9048           else
9049             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
9050         }
9051
9052       /* On some ARM architectures it is faster to use LDR rather than
9053          LDM to load a single register.  On other architectures, the
9054          cost is the same.  In 26 bit mode, or for exception handlers,
9055          we have to use LDM to load the PC so that the CPSR is also
9056          restored.  */
9057       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9058         if (live_regs_mask == (1U << reg))
9059           break;
9060
9061       if (reg <= LAST_ARM_REGNUM
9062           && (reg != LR_REGNUM
9063               || ! really_return
9064               || ! IS_INTERRUPT (func_type)))
9065         {
9066           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
9067                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9068         }
9069       else
9070         {
9071           char *p;
9072           int first = 1;
9073
9074           /* Generate the load multiple instruction to restore the
9075              registers.  Note we can get here, even if
9076              frame_pointer_needed is true, but only if sp already
9077              points to the base of the saved core registers.  */
9078           if (live_regs_mask & (1 << SP_REGNUM))
9079             {
9080               unsigned HOST_WIDE_INT stack_adjust;
9081
9082               offsets = arm_get_frame_offsets ();
9083               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9084               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
9085
9086               if (stack_adjust && arm_arch5)
9087                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9088               else
9089                 {
9090                   /* If we can't use ldmib (SA110 bug),
9091                      then try to pop r3 instead.  */
9092                   if (stack_adjust)
9093                     live_regs_mask |= 1 << 3;
9094                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9095                 }
9096             }
9097           else
9098             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9099
9100           p = instr + strlen (instr);
9101
9102           for (reg = 0; reg <= SP_REGNUM; reg++)
9103             if (live_regs_mask & (1 << reg))
9104               {
9105                 int l = strlen (reg_names[reg]);
9106
9107                 if (first)
9108                   first = 0;
9109                 else
9110                   {
9111                     memcpy (p, ", ", 2);
9112                     p += 2;
9113                   }
9114
9115                 memcpy (p, "%|", 2);
9116                 memcpy (p + 2, reg_names[reg], l);
9117                 p += l + 2;
9118               }
9119
9120           if (live_regs_mask & (1 << LR_REGNUM))
9121             {
9122               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9123               /* If returning from an interrupt, restore the CPSR.  */
9124               if (IS_INTERRUPT (func_type))
9125                 strcat (p, "^");
9126             }
9127           else
9128             strcpy (p, "}");
9129         }
9130
9131       output_asm_insn (instr, & operand);
9132
9133       /* See if we need to generate an extra instruction to
9134          perform the actual function return.  */
9135       if (really_return
9136           && func_type != ARM_FT_INTERWORKED
9137           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9138         {
9139           /* The return has already been handled
9140              by loading the LR into the PC.  */
9141           really_return = 0;
9142         }
9143     }
9144
9145   if (really_return)
9146     {
9147       switch ((int) ARM_FUNC_TYPE (func_type))
9148         {
9149         case ARM_FT_ISR:
9150         case ARM_FT_FIQ:
9151           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9152           break;
9153
9154         case ARM_FT_INTERWORKED:
9155           sprintf (instr, "bx%s\t%%|lr", conditional);
9156           break;
9157
9158         case ARM_FT_EXCEPTION:
9159           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9160           break;
9161
9162         default:
9163           /* Use bx if it's available.  */
9164           if (arm_arch5 || arm_arch4t)
9165             sprintf (instr, "bx%s\t%%|lr", conditional);
9166           else
9167             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9168           break;
9169         }
9170
9171       output_asm_insn (instr, & operand);
9172     }
9173
9174   return "";
9175 }
9176
9177 /* Write the function name into the code section, directly preceding
9178    the function prologue.
9179
9180    Code will be output similar to this:
9181      t0
9182          .ascii "arm_poke_function_name", 0
9183          .align
9184      t1
9185          .word 0xff000000 + (t1 - t0)
9186      arm_poke_function_name
9187          mov     ip, sp
9188          stmfd   sp!, {fp, ip, lr, pc}
9189          sub     fp, ip, #4
9190
9191    When performing a stack backtrace, code can inspect the value
9192    of 'pc' stored at 'fp' + 0.  If the trace function then looks
9193    at location pc - 12 and the top 8 bits are set, then we know
9194    that there is a function name embedded immediately preceding this
9195    location and has length ((pc[-3]) & 0xff000000).
9196
9197    We assume that pc is declared as a pointer to an unsigned long.
9198
9199    It is of no benefit to output the function name if we are assembling
9200    a leaf function.  These function types will not contain a stack
9201    backtrace structure, therefore it is not possible to determine the
9202    function name.  */
9203 void
9204 arm_poke_function_name (FILE *stream, const char *name)
9205 {
9206   unsigned long alignlength;
9207   unsigned long length;
9208   rtx           x;
9209
9210   length      = strlen (name) + 1;
9211   alignlength = ROUND_UP_WORD (length);
9212
9213   ASM_OUTPUT_ASCII (stream, name, length);
9214   ASM_OUTPUT_ALIGN (stream, 2);
9215   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9216   assemble_aligned_integer (UNITS_PER_WORD, x);
9217 }
9218
9219 /* Place some comments into the assembler stream
9220    describing the current function.  */
9221 static void
9222 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9223 {
9224   unsigned long func_type;
9225
9226   if (!TARGET_ARM)
9227     {
9228       thumb_output_function_prologue (f, frame_size);
9229       return;
9230     }
9231
9232   /* Sanity check.  */
9233   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
9234
9235   func_type = arm_current_func_type ();
9236
9237   switch ((int) ARM_FUNC_TYPE (func_type))
9238     {
9239     default:
9240     case ARM_FT_NORMAL:
9241       break;
9242     case ARM_FT_INTERWORKED:
9243       asm_fprintf (f, "\t%@ Function supports interworking.\n");
9244       break;
9245     case ARM_FT_ISR:
9246       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9247       break;
9248     case ARM_FT_FIQ:
9249       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9250       break;
9251     case ARM_FT_EXCEPTION:
9252       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9253       break;
9254     }
9255
9256   if (IS_NAKED (func_type))
9257     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9258
9259   if (IS_VOLATILE (func_type))
9260     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9261
9262   if (IS_NESTED (func_type))
9263     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9264
9265   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9266                current_function_args_size,
9267                current_function_pretend_args_size, frame_size);
9268
9269   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9270                frame_pointer_needed,
9271                cfun->machine->uses_anonymous_args);
9272
9273   if (cfun->machine->lr_save_eliminated)
9274     asm_fprintf (f, "\t%@ link register save eliminated.\n");
9275
9276   if (current_function_calls_eh_return)
9277     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9278
9279 #ifdef AOF_ASSEMBLER
9280   if (flag_pic)
9281     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9282 #endif
9283
9284   return_used_this_function = 0;
9285 }
9286
9287 const char *
9288 arm_output_epilogue (rtx sibling)
9289 {
9290   int reg;
9291   unsigned long saved_regs_mask;
9292   unsigned long func_type;
9293   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
9294      frame that is $fp + 4 for a non-variadic function.  */
9295   int floats_offset = 0;
9296   rtx operands[3];
9297   FILE * f = asm_out_file;
9298   unsigned int lrm_count = 0;
9299   int really_return = (sibling == NULL);
9300   int start_reg;
9301   arm_stack_offsets *offsets;
9302
9303   /* If we have already generated the return instruction
9304      then it is futile to generate anything else.  */
9305   if (use_return_insn (FALSE, sibling) && return_used_this_function)
9306     return "";
9307
9308   func_type = arm_current_func_type ();
9309
9310   if (IS_NAKED (func_type))
9311     /* Naked functions don't have epilogues.  */
9312     return "";
9313
9314   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9315     {
9316       rtx op;
9317
9318       /* A volatile function should never return.  Call abort.  */
9319       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9320       assemble_external_libcall (op);
9321       output_asm_insn ("bl\t%a0", &op);
9322
9323       return "";
9324     }
9325
9326   /* If we are throwing an exception, then we really must be doing a
9327      return, so we can't tail-call.  */
9328   gcc_assert (!current_function_calls_eh_return || really_return);
9329
9330   offsets = arm_get_frame_offsets ();
9331   saved_regs_mask = arm_compute_save_reg_mask ();
9332
9333   if (TARGET_IWMMXT)
9334     lrm_count = bit_count (saved_regs_mask);
9335
9336   floats_offset = offsets->saved_args;
9337   /* Compute how far away the floats will be.  */
9338   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9339     if (saved_regs_mask & (1 << reg))
9340       floats_offset += 4;
9341
9342   if (frame_pointer_needed)
9343     {
9344       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
9345       int vfp_offset = offsets->frame;
9346
9347       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9348         {
9349           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9350             if (regs_ever_live[reg] && !call_used_regs[reg])
9351               {
9352                 floats_offset += 12;
9353                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9354                              reg, FP_REGNUM, floats_offset - vfp_offset);
9355               }
9356         }
9357       else
9358         {
9359           start_reg = LAST_FPA_REGNUM;
9360
9361           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9362             {
9363               if (regs_ever_live[reg] && !call_used_regs[reg])
9364                 {
9365                   floats_offset += 12;
9366
9367                   /* We can't unstack more than four registers at once.  */
9368                   if (start_reg - reg == 3)
9369                     {
9370                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9371                                    reg, FP_REGNUM, floats_offset - vfp_offset);
9372                       start_reg = reg - 1;
9373                     }
9374                 }
9375               else
9376                 {
9377                   if (reg != start_reg)
9378                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9379                                  reg + 1, start_reg - reg,
9380                                  FP_REGNUM, floats_offset - vfp_offset);
9381                   start_reg = reg - 1;
9382                 }
9383             }
9384
9385           /* Just in case the last register checked also needs unstacking.  */
9386           if (reg != start_reg)
9387             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9388                          reg + 1, start_reg - reg,
9389                          FP_REGNUM, floats_offset - vfp_offset);
9390         }
9391
9392       if (TARGET_HARD_FLOAT && TARGET_VFP)
9393         {
9394           int saved_size;
9395
9396           /* The fldmx insn does not have base+offset addressing modes,
9397              so we use IP to hold the address.  */
9398           saved_size = arm_get_vfp_saved_size ();
9399
9400           if (saved_size > 0)
9401             {
9402               floats_offset += saved_size;
9403               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9404                            FP_REGNUM, floats_offset - vfp_offset);
9405             }
9406           start_reg = FIRST_VFP_REGNUM;
9407           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9408             {
9409               if ((!regs_ever_live[reg] || call_used_regs[reg])
9410                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9411                 {
9412                   if (start_reg != reg)
9413                     arm_output_fldmx (f, IP_REGNUM,
9414                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9415                                       (reg - start_reg) / 2);
9416                   start_reg = reg + 2;
9417                 }
9418             }
9419           if (start_reg != reg)
9420             arm_output_fldmx (f, IP_REGNUM,
9421                               (start_reg - FIRST_VFP_REGNUM) / 2,
9422                               (reg - start_reg) / 2);
9423         }
9424
9425       if (TARGET_IWMMXT)
9426         {
9427           /* The frame pointer is guaranteed to be non-double-word aligned.
9428              This is because it is set to (old_stack_pointer - 4) and the
9429              old_stack_pointer was double word aligned.  Thus the offset to
9430              the iWMMXt registers to be loaded must also be non-double-word
9431              sized, so that the resultant address *is* double-word aligned.
9432              We can ignore floats_offset since that was already included in
9433              the live_regs_mask.  */
9434           lrm_count += (lrm_count % 2 ? 2 : 1);
9435
9436           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9437             if (regs_ever_live[reg] && !call_used_regs[reg])
9438               {
9439                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9440                              reg, FP_REGNUM, lrm_count * 4);
9441                 lrm_count += 2;
9442               }
9443         }
9444
9445       /* saved_regs_mask should contain the IP, which at the time of stack
9446          frame generation actually contains the old stack pointer.  So a
9447          quick way to unwind the stack is just pop the IP register directly
9448          into the stack pointer.  */
9449       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
9450       saved_regs_mask &= ~ (1 << IP_REGNUM);
9451       saved_regs_mask |=   (1 << SP_REGNUM);
9452
9453       /* There are two registers left in saved_regs_mask - LR and PC.  We
9454          only need to restore the LR register (the return address), but to
9455          save time we can load it directly into the PC, unless we need a
9456          special function exit sequence, or we are not really returning.  */
9457       if (really_return
9458           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9459           && !current_function_calls_eh_return)
9460         /* Delete the LR from the register mask, so that the LR on
9461            the stack is loaded into the PC in the register mask.  */
9462         saved_regs_mask &= ~ (1 << LR_REGNUM);
9463       else
9464         saved_regs_mask &= ~ (1 << PC_REGNUM);
9465
9466       /* We must use SP as the base register, because SP is one of the
9467          registers being restored.  If an interrupt or page fault
9468          happens in the ldm instruction, the SP might or might not
9469          have been restored.  That would be bad, as then SP will no
9470          longer indicate the safe area of stack, and we can get stack
9471          corruption.  Using SP as the base register means that it will
9472          be reset correctly to the original value, should an interrupt
9473          occur.  If the stack pointer already points at the right
9474          place, then omit the subtraction.  */
9475       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9476           || current_function_calls_alloca)
9477         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9478                      4 * bit_count (saved_regs_mask));
9479       print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9480
9481       if (IS_INTERRUPT (func_type))
9482         /* Interrupt handlers will have pushed the
9483            IP onto the stack, so restore it now.  */
9484         print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9485     }
9486   else
9487     {
9488       /* Restore stack pointer if necessary.  */
9489       if (offsets->outgoing_args != offsets->saved_regs)
9490         {
9491           operands[0] = operands[1] = stack_pointer_rtx;
9492           operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9493           output_add_immediate (operands);
9494         }
9495
9496       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9497         {
9498           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9499             if (regs_ever_live[reg] && !call_used_regs[reg])
9500               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9501                            reg, SP_REGNUM);
9502         }
9503       else
9504         {
9505           start_reg = FIRST_FPA_REGNUM;
9506
9507           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9508             {
9509               if (regs_ever_live[reg] && !call_used_regs[reg])
9510                 {
9511                   if (reg - start_reg == 3)
9512                     {
9513                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9514                                    start_reg, SP_REGNUM);
9515                       start_reg = reg + 1;
9516                     }
9517                 }
9518               else
9519                 {
9520                   if (reg != start_reg)
9521                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9522                                  start_reg, reg - start_reg,
9523                                  SP_REGNUM);
9524
9525                   start_reg = reg + 1;
9526                 }
9527             }
9528
9529           /* Just in case the last register checked also needs unstacking.  */
9530           if (reg != start_reg)
9531             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9532                          start_reg, reg - start_reg, SP_REGNUM);
9533         }
9534
9535       if (TARGET_HARD_FLOAT && TARGET_VFP)
9536         {
9537           start_reg = FIRST_VFP_REGNUM;
9538           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9539             {
9540               if ((!regs_ever_live[reg] || call_used_regs[reg])
9541                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9542                 {
9543                   if (start_reg != reg)
9544                     arm_output_fldmx (f, SP_REGNUM,
9545                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9546                                       (reg - start_reg) / 2);
9547                   start_reg = reg + 2;
9548                 }
9549             }
9550           if (start_reg != reg)
9551             arm_output_fldmx (f, SP_REGNUM,
9552                               (start_reg - FIRST_VFP_REGNUM) / 2,
9553                               (reg - start_reg) / 2);
9554         }
9555       if (TARGET_IWMMXT)
9556         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9557           if (regs_ever_live[reg] && !call_used_regs[reg])
9558             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9559
9560       /* If we can, restore the LR into the PC.  */
9561       if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9562           && really_return
9563           && current_function_pretend_args_size == 0
9564           && saved_regs_mask & (1 << LR_REGNUM)
9565           && !current_function_calls_eh_return)
9566         {
9567           saved_regs_mask &= ~ (1 << LR_REGNUM);
9568           saved_regs_mask |=   (1 << PC_REGNUM);
9569         }
9570
9571       /* Load the registers off the stack.  If we only have one register
9572          to load use the LDR instruction - it is faster.  */
9573       if (saved_regs_mask == (1 << LR_REGNUM))
9574         {
9575           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9576         }
9577       else if (saved_regs_mask)
9578         {
9579           if (saved_regs_mask & (1 << SP_REGNUM))
9580             /* Note - write back to the stack register is not enabled
9581                (i.e. "ldmfd sp!...").  We know that the stack pointer is
9582                in the list of registers and if we add writeback the
9583                instruction becomes UNPREDICTABLE.  */
9584             print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9585           else
9586             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9587         }
9588
9589       if (current_function_pretend_args_size)
9590         {
9591           /* Unwind the pre-pushed regs.  */
9592           operands[0] = operands[1] = stack_pointer_rtx;
9593           operands[2] = GEN_INT (current_function_pretend_args_size);
9594           output_add_immediate (operands);
9595         }
9596     }
9597
9598   /* We may have already restored PC directly from the stack.  */
9599   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
9600     return "";
9601
9602   /* Stack adjustment for exception handler.  */
9603   if (current_function_calls_eh_return)
9604     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
9605                  ARM_EH_STACKADJ_REGNUM);
9606
9607   /* Generate the return instruction.  */
9608   switch ((int) ARM_FUNC_TYPE (func_type))
9609     {
9610     case ARM_FT_ISR:
9611     case ARM_FT_FIQ:
9612       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9613       break;
9614
9615     case ARM_FT_EXCEPTION:
9616       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9617       break;
9618
9619     case ARM_FT_INTERWORKED:
9620       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9621       break;
9622
9623     default:
9624       if (arm_arch5 || arm_arch4t)
9625         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9626       else
9627         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9628       break;
9629     }
9630
9631   return "";
9632 }
9633
9634 static void
9635 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9636                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9637 {
9638   arm_stack_offsets *offsets;
9639
9640   if (TARGET_THUMB)
9641     {
9642       int regno;
9643
9644       /* Emit any call-via-reg trampolines that are needed for v4t support
9645          of call_reg and call_value_reg type insns.  */
9646       for (regno = 0; regno < LR_REGNUM; regno++)
9647         {
9648           rtx label = cfun->machine->call_via[regno];
9649
9650           if (label != NULL)
9651             {
9652               function_section (current_function_decl);
9653               targetm.asm_out.internal_label (asm_out_file, "L",
9654                                               CODE_LABEL_NUMBER (label));
9655               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
9656             }
9657         }
9658
9659       /* ??? Probably not safe to set this here, since it assumes that a
9660          function will be emitted as assembly immediately after we generate
9661          RTL for it.  This does not happen for inline functions.  */
9662       return_used_this_function = 0;
9663     }
9664   else
9665     {
9666       /* We need to take into account any stack-frame rounding.  */
9667       offsets = arm_get_frame_offsets ();
9668
9669       gcc_assert (!use_return_insn (FALSE, NULL)
9670                   || !return_used_this_function
9671                   || offsets->saved_regs == offsets->outgoing_args
9672                   || frame_pointer_needed);
9673
9674       /* Reset the ARM-specific per-function variables.  */
9675       after_arm_reorg = 0;
9676     }
9677 }
9678
9679 /* Generate and emit an insn that we will recognize as a push_multi.
9680    Unfortunately, since this insn does not reflect very well the actual
9681    semantics of the operation, we need to annotate the insn for the benefit
9682    of DWARF2 frame unwind information.  */
9683 static rtx
9684 emit_multi_reg_push (unsigned long mask)
9685 {
9686   int num_regs = 0;
9687   int num_dwarf_regs;
9688   int i, j;
9689   rtx par;
9690   rtx dwarf;
9691   int dwarf_par_index;
9692   rtx tmp, reg;
9693
9694   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9695     if (mask & (1 << i))
9696       num_regs++;
9697
9698   gcc_assert (num_regs && num_regs <= 16);
9699
9700   /* We don't record the PC in the dwarf frame information.  */
9701   num_dwarf_regs = num_regs;
9702   if (mask & (1 << PC_REGNUM))
9703     num_dwarf_regs--;
9704
9705   /* For the body of the insn we are going to generate an UNSPEC in
9706      parallel with several USEs.  This allows the insn to be recognized
9707      by the push_multi pattern in the arm.md file.  The insn looks
9708      something like this:
9709
9710        (parallel [
9711            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9712                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9713            (use (reg:SI 11 fp))
9714            (use (reg:SI 12 ip))
9715            (use (reg:SI 14 lr))
9716            (use (reg:SI 15 pc))
9717         ])
9718
9719      For the frame note however, we try to be more explicit and actually
9720      show each register being stored into the stack frame, plus a (single)
9721      decrement of the stack pointer.  We do it this way in order to be
9722      friendly to the stack unwinding code, which only wants to see a single
9723      stack decrement per instruction.  The RTL we generate for the note looks
9724      something like this:
9725
9726       (sequence [
9727            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9728            (set (mem:SI (reg:SI sp)) (reg:SI r4))
9729            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9730            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9731            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9732         ])
9733
9734       This sequence is used both by the code to support stack unwinding for
9735       exceptions handlers and the code to generate dwarf2 frame debugging.  */
9736
9737   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9738   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9739   dwarf_par_index = 1;
9740
9741   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9742     {
9743       if (mask & (1 << i))
9744         {
9745           reg = gen_rtx_REG (SImode, i);
9746
9747           XVECEXP (par, 0, 0)
9748             = gen_rtx_SET (VOIDmode,
9749                            gen_rtx_MEM (BLKmode,
9750                                         gen_rtx_PRE_DEC (BLKmode,
9751                                                          stack_pointer_rtx)),
9752                            gen_rtx_UNSPEC (BLKmode,
9753                                            gen_rtvec (1, reg),
9754                                            UNSPEC_PUSH_MULT));
9755
9756           if (i != PC_REGNUM)
9757             {
9758               tmp = gen_rtx_SET (VOIDmode,
9759                                  gen_rtx_MEM (SImode, stack_pointer_rtx),
9760                                  reg);
9761               RTX_FRAME_RELATED_P (tmp) = 1;
9762               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9763               dwarf_par_index++;
9764             }
9765
9766           break;
9767         }
9768     }
9769
9770   for (j = 1, i++; j < num_regs; i++)
9771     {
9772       if (mask & (1 << i))
9773         {
9774           reg = gen_rtx_REG (SImode, i);
9775
9776           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9777
9778           if (i != PC_REGNUM)
9779             {
9780               tmp = gen_rtx_SET (VOIDmode,
9781                                  gen_rtx_MEM (SImode,
9782                                               plus_constant (stack_pointer_rtx,
9783                                                              4 * j)),
9784                                  reg);
9785               RTX_FRAME_RELATED_P (tmp) = 1;
9786               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9787             }
9788
9789           j++;
9790         }
9791     }
9792
9793   par = emit_insn (par);
9794
9795   tmp = gen_rtx_SET (SImode,
9796                      stack_pointer_rtx,
9797                      gen_rtx_PLUS (SImode,
9798                                    stack_pointer_rtx,
9799                                    GEN_INT (-4 * num_regs)));
9800   RTX_FRAME_RELATED_P (tmp) = 1;
9801   XVECEXP (dwarf, 0, 0) = tmp;
9802
9803   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9804                                        REG_NOTES (par));
9805   return par;
9806 }
9807
9808 static rtx
9809 emit_sfm (int base_reg, int count)
9810 {
9811   rtx par;
9812   rtx dwarf;
9813   rtx tmp, reg;
9814   int i;
9815
9816   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9817   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9818
9819   reg = gen_rtx_REG (XFmode, base_reg++);
9820
9821   XVECEXP (par, 0, 0)
9822     = gen_rtx_SET (VOIDmode,
9823                    gen_rtx_MEM (BLKmode,
9824                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9825                    gen_rtx_UNSPEC (BLKmode,
9826                                    gen_rtvec (1, reg),
9827                                    UNSPEC_PUSH_MULT));
9828   tmp = gen_rtx_SET (VOIDmode,
9829                      gen_rtx_MEM (XFmode, stack_pointer_rtx), reg);
9830   RTX_FRAME_RELATED_P (tmp) = 1;
9831   XVECEXP (dwarf, 0, 1) = tmp;
9832
9833   for (i = 1; i < count; i++)
9834     {
9835       reg = gen_rtx_REG (XFmode, base_reg++);
9836       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9837
9838       tmp = gen_rtx_SET (VOIDmode,
9839                          gen_rtx_MEM (XFmode,
9840                                       plus_constant (stack_pointer_rtx,
9841                                                      i * 12)),
9842                          reg);
9843       RTX_FRAME_RELATED_P (tmp) = 1;
9844       XVECEXP (dwarf, 0, i + 1) = tmp;
9845     }
9846
9847   tmp = gen_rtx_SET (VOIDmode,
9848                      stack_pointer_rtx,
9849                      gen_rtx_PLUS (SImode,
9850                                    stack_pointer_rtx,
9851                                    GEN_INT (-12 * count)));
9852   RTX_FRAME_RELATED_P (tmp) = 1;
9853   XVECEXP (dwarf, 0, 0) = tmp;
9854
9855   par = emit_insn (par);
9856   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9857                                        REG_NOTES (par));
9858   return par;
9859 }
9860
9861
9862 /* Return true if the current function needs to save/restore LR.  */
9863
9864 static bool
9865 thumb_force_lr_save (void)
9866 {
9867   return !cfun->machine->lr_save_eliminated
9868          && (!leaf_function_p ()
9869              || thumb_far_jump_used_p ()
9870              || regs_ever_live [LR_REGNUM]);
9871 }
9872
9873
9874 /* Compute the distance from register FROM to register TO.
9875    These can be the arg pointer (26), the soft frame pointer (25),
9876    the stack pointer (13) or the hard frame pointer (11).
9877    In thumb mode r7 is used as the soft frame pointer, if needed.
9878    Typical stack layout looks like this:
9879
9880        old stack pointer -> |    |
9881                              ----
9882                             |    | \
9883                             |    |   saved arguments for
9884                             |    |   vararg functions
9885                             |    | /
9886                               --
9887    hard FP & arg pointer -> |    | \
9888                             |    |   stack
9889                             |    |   frame
9890                             |    | /
9891                               --
9892                             |    | \
9893                             |    |   call saved
9894                             |    |   registers
9895       soft frame pointer -> |    | /
9896                               --
9897                             |    | \
9898                             |    |   local
9899                             |    |   variables
9900                             |    | /
9901                               --
9902                             |    | \
9903                             |    |   outgoing
9904                             |    |   arguments
9905    current stack pointer -> |    | /
9906                               --
9907
9908   For a given function some or all of these stack components
9909   may not be needed, giving rise to the possibility of
9910   eliminating some of the registers.
9911
9912   The values returned by this function must reflect the behavior
9913   of arm_expand_prologue() and arm_compute_save_reg_mask().
9914
9915   The sign of the number returned reflects the direction of stack
9916   growth, so the values are positive for all eliminations except
9917   from the soft frame pointer to the hard frame pointer.
9918
9919   SFP may point just inside the local variables block to ensure correct
9920   alignment.  */
9921
9922
9923 /* Calculate stack offsets.  These are used to calculate register elimination
9924    offsets and in prologue/epilogue code.  */
9925
9926 static arm_stack_offsets *
9927 arm_get_frame_offsets (void)
9928 {
9929   struct arm_stack_offsets *offsets;
9930   unsigned long func_type;
9931   int leaf;
9932   int saved;
9933   HOST_WIDE_INT frame_size;
9934
9935   offsets = &cfun->machine->stack_offsets;
9936
9937   /* We need to know if we are a leaf function.  Unfortunately, it
9938      is possible to be called after start_sequence has been called,
9939      which causes get_insns to return the insns for the sequence,
9940      not the function, which will cause leaf_function_p to return
9941      the incorrect result.
9942
9943      to know about leaf functions once reload has completed, and the
9944      frame size cannot be changed after that time, so we can safely
9945      use the cached value.  */
9946
9947   if (reload_completed)
9948     return offsets;
9949
9950   /* Initially this is the size of the local variables.  It will translated
9951      into an offset once we have determined the size of preceding data.  */
9952   frame_size = ROUND_UP_WORD (get_frame_size ());
9953
9954   leaf = leaf_function_p ();
9955
9956   /* Space for variadic functions.  */
9957   offsets->saved_args = current_function_pretend_args_size;
9958
9959   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
9960
9961   if (TARGET_ARM)
9962     {
9963       unsigned int regno;
9964
9965       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
9966
9967       /* We know that SP will be doubleword aligned on entry, and we must
9968          preserve that condition at any subroutine call.  We also require the
9969          soft frame pointer to be doubleword aligned.  */
9970
9971       if (TARGET_REALLY_IWMMXT)
9972         {
9973           /* Check for the call-saved iWMMXt registers.  */
9974           for (regno = FIRST_IWMMXT_REGNUM;
9975                regno <= LAST_IWMMXT_REGNUM;
9976                regno++)
9977             if (regs_ever_live [regno] && ! call_used_regs [regno])
9978               saved += 8;
9979         }
9980
9981       func_type = arm_current_func_type ();
9982       if (! IS_VOLATILE (func_type))
9983         {
9984           /* Space for saved FPA registers.  */
9985           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
9986           if (regs_ever_live[regno] && ! call_used_regs[regno])
9987             saved += 12;
9988
9989           /* Space for saved VFP registers.  */
9990           if (TARGET_HARD_FLOAT && TARGET_VFP)
9991             saved += arm_get_vfp_saved_size ();
9992         }
9993     }
9994   else /* TARGET_THUMB */
9995     {
9996       saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
9997       if (TARGET_BACKTRACE)
9998         saved += 16;
9999     }
10000
10001   /* Saved registers include the stack frame.  */
10002   offsets->saved_regs = offsets->saved_args + saved;
10003   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
10004   /* A leaf function does not need any stack alignment if it has nothing
10005      on the stack.  */
10006   if (leaf && frame_size == 0)
10007     {
10008       offsets->outgoing_args = offsets->soft_frame;
10009       return offsets;
10010     }
10011
10012   /* Ensure SFP has the correct alignment.  */
10013   if (ARM_DOUBLEWORD_ALIGN
10014       && (offsets->soft_frame & 7))
10015     offsets->soft_frame += 4;
10016
10017   offsets->outgoing_args = offsets->soft_frame + frame_size
10018                            + current_function_outgoing_args_size;
10019
10020   if (ARM_DOUBLEWORD_ALIGN)
10021     {
10022       /* Ensure SP remains doubleword aligned.  */
10023       if (offsets->outgoing_args & 7)
10024         offsets->outgoing_args += 4;
10025       gcc_assert (!(offsets->outgoing_args & 7));
10026     }
10027
10028   return offsets;
10029 }
10030
10031
10032 /* Calculate the relative offsets for the different stack pointers.  Positive
10033    offsets are in the direction of stack growth.  */
10034
10035 HOST_WIDE_INT
10036 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10037 {
10038   arm_stack_offsets *offsets;
10039
10040   offsets = arm_get_frame_offsets ();
10041
10042   /* OK, now we have enough information to compute the distances.
10043      There must be an entry in these switch tables for each pair
10044      of registers in ELIMINABLE_REGS, even if some of the entries
10045      seem to be redundant or useless.  */
10046   switch (from)
10047     {
10048     case ARG_POINTER_REGNUM:
10049       switch (to)
10050         {
10051         case THUMB_HARD_FRAME_POINTER_REGNUM:
10052           return 0;
10053
10054         case FRAME_POINTER_REGNUM:
10055           /* This is the reverse of the soft frame pointer
10056              to hard frame pointer elimination below.  */
10057           return offsets->soft_frame - offsets->saved_args;
10058
10059         case ARM_HARD_FRAME_POINTER_REGNUM:
10060           /* If there is no stack frame then the hard
10061              frame pointer and the arg pointer coincide.  */
10062           if (offsets->frame == offsets->saved_regs)
10063             return 0;
10064           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
10065           return (frame_pointer_needed
10066                   && cfun->static_chain_decl != NULL
10067                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10068
10069         case STACK_POINTER_REGNUM:
10070           /* If nothing has been pushed on the stack at all
10071              then this will return -4.  This *is* correct!  */
10072           return offsets->outgoing_args - (offsets->saved_args + 4);
10073
10074         default:
10075           gcc_unreachable ();
10076         }
10077       gcc_unreachable ();
10078
10079     case FRAME_POINTER_REGNUM:
10080       switch (to)
10081         {
10082         case THUMB_HARD_FRAME_POINTER_REGNUM:
10083           return 0;
10084
10085         case ARM_HARD_FRAME_POINTER_REGNUM:
10086           /* The hard frame pointer points to the top entry in the
10087              stack frame.  The soft frame pointer to the bottom entry
10088              in the stack frame.  If there is no stack frame at all,
10089              then they are identical.  */
10090
10091           return offsets->frame - offsets->soft_frame;
10092
10093         case STACK_POINTER_REGNUM:
10094           return offsets->outgoing_args - offsets->soft_frame;
10095
10096         default:
10097           gcc_unreachable ();
10098         }
10099       gcc_unreachable ();
10100
10101     default:
10102       /* You cannot eliminate from the stack pointer.
10103          In theory you could eliminate from the hard frame
10104          pointer to the stack pointer, but this will never
10105          happen, since if a stack frame is not needed the
10106          hard frame pointer will never be used.  */
10107       gcc_unreachable ();
10108     }
10109 }
10110
10111
10112 /* Generate the prologue instructions for entry into an ARM function.  */
10113 void
10114 arm_expand_prologue (void)
10115 {
10116   int reg;
10117   rtx amount;
10118   rtx insn;
10119   rtx ip_rtx;
10120   unsigned long live_regs_mask;
10121   unsigned long func_type;
10122   int fp_offset = 0;
10123   int saved_pretend_args = 0;
10124   int saved_regs = 0;
10125   unsigned HOST_WIDE_INT args_to_push;
10126   arm_stack_offsets *offsets;
10127
10128   func_type = arm_current_func_type ();
10129
10130   /* Naked functions don't have prologues.  */
10131   if (IS_NAKED (func_type))
10132     return;
10133
10134   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
10135   args_to_push = current_function_pretend_args_size;
10136
10137   /* Compute which register we will have to save onto the stack.  */
10138   live_regs_mask = arm_compute_save_reg_mask ();
10139
10140   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10141
10142   if (frame_pointer_needed)
10143     {
10144       if (IS_INTERRUPT (func_type))
10145         {
10146           /* Interrupt functions must not corrupt any registers.
10147              Creating a frame pointer however, corrupts the IP
10148              register, so we must push it first.  */
10149           insn = emit_multi_reg_push (1 << IP_REGNUM);
10150
10151           /* Do not set RTX_FRAME_RELATED_P on this insn.
10152              The dwarf stack unwinding code only wants to see one
10153              stack decrement per function, and this is not it.  If
10154              this instruction is labeled as being part of the frame
10155              creation sequence then dwarf2out_frame_debug_expr will
10156              die when it encounters the assignment of IP to FP
10157              later on, since the use of SP here establishes SP as
10158              the CFA register and not IP.
10159
10160              Anyway this instruction is not really part of the stack
10161              frame creation although it is part of the prologue.  */
10162         }
10163       else if (IS_NESTED (func_type))
10164         {
10165           /* The Static chain register is the same as the IP register
10166              used as a scratch register during stack frame creation.
10167              To get around this need to find somewhere to store IP
10168              whilst the frame is being created.  We try the following
10169              places in order:
10170
10171                1. The last argument register.
10172                2. A slot on the stack above the frame.  (This only
10173                   works if the function is not a varargs function).
10174                3. Register r3, after pushing the argument registers
10175                   onto the stack.
10176
10177              Note - we only need to tell the dwarf2 backend about the SP
10178              adjustment in the second variant; the static chain register
10179              doesn't need to be unwound, as it doesn't contain a value
10180              inherited from the caller.  */
10181
10182           if (regs_ever_live[3] == 0)
10183             {
10184               insn = gen_rtx_REG (SImode, 3);
10185               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10186               insn = emit_insn (insn);
10187             }
10188           else if (args_to_push == 0)
10189             {
10190               rtx dwarf;
10191               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10192               insn = gen_rtx_MEM (SImode, insn);
10193               insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10194               insn = emit_insn (insn);
10195
10196               fp_offset = 4;
10197
10198               /* Just tell the dwarf backend that we adjusted SP.  */
10199               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10200                                    gen_rtx_PLUS (SImode, stack_pointer_rtx,
10201                                                  GEN_INT (-fp_offset)));
10202               RTX_FRAME_RELATED_P (insn) = 1;
10203               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10204                                                     dwarf, REG_NOTES (insn));
10205             }
10206           else
10207             {
10208               /* Store the args on the stack.  */
10209               if (cfun->machine->uses_anonymous_args)
10210                 insn = emit_multi_reg_push
10211                   ((0xf0 >> (args_to_push / 4)) & 0xf);
10212               else
10213                 insn = emit_insn
10214                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10215                                GEN_INT (- args_to_push)));
10216
10217               RTX_FRAME_RELATED_P (insn) = 1;
10218
10219               saved_pretend_args = 1;
10220               fp_offset = args_to_push;
10221               args_to_push = 0;
10222
10223               /* Now reuse r3 to preserve IP.  */
10224               insn = gen_rtx_REG (SImode, 3);
10225               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10226               (void) emit_insn (insn);
10227             }
10228         }
10229
10230       if (fp_offset)
10231         {
10232           insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10233           insn = gen_rtx_SET  (SImode, ip_rtx, insn);
10234         }
10235       else
10236         insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10237
10238       insn = emit_insn (insn);
10239       RTX_FRAME_RELATED_P (insn) = 1;
10240     }
10241
10242   if (args_to_push)
10243     {
10244       /* Push the argument registers, or reserve space for them.  */
10245       if (cfun->machine->uses_anonymous_args)
10246         insn = emit_multi_reg_push
10247           ((0xf0 >> (args_to_push / 4)) & 0xf);
10248       else
10249         insn = emit_insn
10250           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10251                        GEN_INT (- args_to_push)));
10252       RTX_FRAME_RELATED_P (insn) = 1;
10253     }
10254
10255   /* If this is an interrupt service routine, and the link register
10256      is going to be pushed, and we are not creating a stack frame,
10257      (which would involve an extra push of IP and a pop in the epilogue)
10258      subtracting four from LR now will mean that the function return
10259      can be done with a single instruction.  */
10260   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10261       && (live_regs_mask & (1 << LR_REGNUM)) != 0
10262       && ! frame_pointer_needed)
10263     emit_insn (gen_rtx_SET (SImode,
10264                             gen_rtx_REG (SImode, LR_REGNUM),
10265                             gen_rtx_PLUS (SImode,
10266                                           gen_rtx_REG (SImode, LR_REGNUM),
10267                                           GEN_INT (-4))));
10268
10269   if (live_regs_mask)
10270     {
10271       insn = emit_multi_reg_push (live_regs_mask);
10272       saved_regs += bit_count (live_regs_mask) * 4;
10273       RTX_FRAME_RELATED_P (insn) = 1;
10274     }
10275
10276   if (TARGET_IWMMXT)
10277     for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10278       if (regs_ever_live[reg] && ! call_used_regs [reg])
10279         {
10280           insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10281           insn = gen_rtx_MEM (V2SImode, insn);
10282           insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10283                                          gen_rtx_REG (V2SImode, reg)));
10284           RTX_FRAME_RELATED_P (insn) = 1;
10285           saved_regs += 8;
10286         }
10287
10288   if (! IS_VOLATILE (func_type))
10289     {
10290       int start_reg;
10291
10292       /* Save any floating point call-saved registers used by this
10293          function.  */
10294       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10295         {
10296           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10297             if (regs_ever_live[reg] && !call_used_regs[reg])
10298               {
10299                 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10300                 insn = gen_rtx_MEM (XFmode, insn);
10301                 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10302                                                gen_rtx_REG (XFmode, reg)));
10303                 RTX_FRAME_RELATED_P (insn) = 1;
10304                 saved_regs += 12;
10305               }
10306         }
10307       else
10308         {
10309           start_reg = LAST_FPA_REGNUM;
10310
10311           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10312             {
10313               if (regs_ever_live[reg] && !call_used_regs[reg])
10314                 {
10315                   if (start_reg - reg == 3)
10316                     {
10317                       insn = emit_sfm (reg, 4);
10318                       RTX_FRAME_RELATED_P (insn) = 1;
10319                       saved_regs += 48;
10320                       start_reg = reg - 1;
10321                     }
10322                 }
10323               else
10324                 {
10325                   if (start_reg != reg)
10326                     {
10327                       insn = emit_sfm (reg + 1, start_reg - reg);
10328                       RTX_FRAME_RELATED_P (insn) = 1;
10329                       saved_regs += (start_reg - reg) * 12;
10330                     }
10331                   start_reg = reg - 1;
10332                 }
10333             }
10334
10335           if (start_reg != reg)
10336             {
10337               insn = emit_sfm (reg + 1, start_reg - reg);
10338               saved_regs += (start_reg - reg) * 12;
10339               RTX_FRAME_RELATED_P (insn) = 1;
10340             }
10341         }
10342       if (TARGET_HARD_FLOAT && TARGET_VFP)
10343         {
10344           start_reg = FIRST_VFP_REGNUM;
10345
10346           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10347             {
10348               if ((!regs_ever_live[reg] || call_used_regs[reg])
10349                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10350                 {
10351                   if (start_reg != reg)
10352                     saved_regs += vfp_emit_fstmx (start_reg,
10353                                                   (reg - start_reg) / 2);
10354                   start_reg = reg + 2;
10355                 }
10356             }
10357           if (start_reg != reg)
10358             saved_regs += vfp_emit_fstmx (start_reg,
10359                                           (reg - start_reg) / 2);
10360         }
10361     }
10362
10363   if (frame_pointer_needed)
10364     {
10365       /* Create the new frame pointer.  */
10366       insn = GEN_INT (-(4 + args_to_push + fp_offset));
10367       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10368       RTX_FRAME_RELATED_P (insn) = 1;
10369
10370       if (IS_NESTED (func_type))
10371         {
10372           /* Recover the static chain register.  */
10373           if (regs_ever_live [3] == 0
10374               || saved_pretend_args)
10375             insn = gen_rtx_REG (SImode, 3);
10376           else /* if (current_function_pretend_args_size == 0) */
10377             {
10378               insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10379                                    GEN_INT (4));
10380               insn = gen_rtx_MEM (SImode, insn);
10381             }
10382
10383           emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10384           /* Add a USE to stop propagate_one_insn() from barfing.  */
10385           emit_insn (gen_prologue_use (ip_rtx));
10386         }
10387     }
10388
10389   offsets = arm_get_frame_offsets ();
10390   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10391     {
10392       /* This add can produce multiple insns for a large constant, so we
10393          need to get tricky.  */
10394       rtx last = get_last_insn ();
10395
10396       amount = GEN_INT (offsets->saved_args + saved_regs
10397                         - offsets->outgoing_args);
10398
10399       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10400                                     amount));
10401       do
10402         {
10403           last = last ? NEXT_INSN (last) : get_insns ();
10404           RTX_FRAME_RELATED_P (last) = 1;
10405         }
10406       while (last != insn);
10407
10408       /* If the frame pointer is needed, emit a special barrier that
10409          will prevent the scheduler from moving stores to the frame
10410          before the stack adjustment.  */
10411       if (frame_pointer_needed)
10412         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10413                                          hard_frame_pointer_rtx));
10414     }
10415
10416
10417   if (flag_pic)
10418     arm_load_pic_register (INVALID_REGNUM);
10419
10420   /* If we are profiling, make sure no instructions are scheduled before
10421      the call to mcount.  Similarly if the user has requested no
10422      scheduling in the prolog.  */
10423   if (current_function_profile || !TARGET_SCHED_PROLOG)
10424     emit_insn (gen_blockage ());
10425
10426   /* If the link register is being kept alive, with the return address in it,
10427      then make sure that it does not get reused by the ce2 pass.  */
10428   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10429     {
10430       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10431       cfun->machine->lr_save_eliminated = 1;
10432     }
10433 }
10434 \f
10435 /* If CODE is 'd', then the X is a condition operand and the instruction
10436    should only be executed if the condition is true.
10437    if CODE is 'D', then the X is a condition operand and the instruction
10438    should only be executed if the condition is false: however, if the mode
10439    of the comparison is CCFPEmode, then always execute the instruction -- we
10440    do this because in these circumstances !GE does not necessarily imply LT;
10441    in these cases the instruction pattern will take care to make sure that
10442    an instruction containing %d will follow, thereby undoing the effects of
10443    doing this instruction unconditionally.
10444    If CODE is 'N' then X is a floating point operand that must be negated
10445    before output.
10446    If CODE is 'B' then output a bitwise inverted value of X (a const int).
10447    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
10448 void
10449 arm_print_operand (FILE *stream, rtx x, int code)
10450 {
10451   switch (code)
10452     {
10453     case '@':
10454       fputs (ASM_COMMENT_START, stream);
10455       return;
10456
10457     case '_':
10458       fputs (user_label_prefix, stream);
10459       return;
10460
10461     case '|':
10462       fputs (REGISTER_PREFIX, stream);
10463       return;
10464
10465     case '?':
10466       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10467         {
10468           if (TARGET_THUMB)
10469             {
10470               output_operand_lossage ("predicated Thumb instruction");
10471               break;
10472             }
10473           if (current_insn_predicate != NULL)
10474             {
10475               output_operand_lossage
10476                 ("predicated instruction in conditional sequence");
10477               break;
10478             }
10479
10480           fputs (arm_condition_codes[arm_current_cc], stream);
10481         }
10482       else if (current_insn_predicate)
10483         {
10484           enum arm_cond_code code;
10485
10486           if (TARGET_THUMB)
10487             {
10488               output_operand_lossage ("predicated Thumb instruction");
10489               break;
10490             }
10491
10492           code = get_arm_condition_code (current_insn_predicate);
10493           fputs (arm_condition_codes[code], stream);
10494         }
10495       return;
10496
10497     case 'N':
10498       {
10499         REAL_VALUE_TYPE r;
10500         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10501         r = REAL_VALUE_NEGATE (r);
10502         fprintf (stream, "%s", fp_const_from_val (&r));
10503       }
10504       return;
10505
10506     case 'B':
10507       if (GET_CODE (x) == CONST_INT)
10508         {
10509           HOST_WIDE_INT val;
10510           val = ARM_SIGN_EXTEND (~INTVAL (x));
10511           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10512         }
10513       else
10514         {
10515           putc ('~', stream);
10516           output_addr_const (stream, x);
10517         }
10518       return;
10519
10520     case 'i':
10521       fprintf (stream, "%s", arithmetic_instr (x, 1));
10522       return;
10523
10524     /* Truncate Cirrus shift counts.  */
10525     case 's':
10526       if (GET_CODE (x) == CONST_INT)
10527         {
10528           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10529           return;
10530         }
10531       arm_print_operand (stream, x, 0);
10532       return;
10533
10534     case 'I':
10535       fprintf (stream, "%s", arithmetic_instr (x, 0));
10536       return;
10537
10538     case 'S':
10539       {
10540         HOST_WIDE_INT val;
10541         const char * shift = shift_op (x, &val);
10542
10543         if (shift)
10544           {
10545             fprintf (stream, ", %s ", shift_op (x, &val));
10546             if (val == -1)
10547               arm_print_operand (stream, XEXP (x, 1), 0);
10548             else
10549               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10550           }
10551       }
10552       return;
10553
10554       /* An explanation of the 'Q', 'R' and 'H' register operands:
10555
10556          In a pair of registers containing a DI or DF value the 'Q'
10557          operand returns the register number of the register containing
10558          the least significant part of the value.  The 'R' operand returns
10559          the register number of the register containing the most
10560          significant part of the value.
10561
10562          The 'H' operand returns the higher of the two register numbers.
10563          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10564          same as the 'Q' operand, since the most significant part of the
10565          value is held in the lower number register.  The reverse is true
10566          on systems where WORDS_BIG_ENDIAN is false.
10567
10568          The purpose of these operands is to distinguish between cases
10569          where the endian-ness of the values is important (for example
10570          when they are added together), and cases where the endian-ness
10571          is irrelevant, but the order of register operations is important.
10572          For example when loading a value from memory into a register
10573          pair, the endian-ness does not matter.  Provided that the value
10574          from the lower memory address is put into the lower numbered
10575          register, and the value from the higher address is put into the
10576          higher numbered register, the load will work regardless of whether
10577          the value being loaded is big-wordian or little-wordian.  The
10578          order of the two register loads can matter however, if the address
10579          of the memory location is actually held in one of the registers
10580          being overwritten by the load.  */
10581     case 'Q':
10582       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10583         {
10584           output_operand_lossage ("invalid operand for code '%c'", code);
10585           return;
10586         }
10587
10588       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10589       return;
10590
10591     case 'R':
10592       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10593         {
10594           output_operand_lossage ("invalid operand for code '%c'", code);
10595           return;
10596         }
10597
10598       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10599       return;
10600
10601     case 'H':
10602       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10603         {
10604           output_operand_lossage ("invalid operand for code '%c'", code);
10605           return;
10606         }
10607
10608       asm_fprintf (stream, "%r", REGNO (x) + 1);
10609       return;
10610
10611     case 'm':
10612       asm_fprintf (stream, "%r",
10613                    GET_CODE (XEXP (x, 0)) == REG
10614                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10615       return;
10616
10617     case 'M':
10618       asm_fprintf (stream, "{%r-%r}",
10619                    REGNO (x),
10620                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10621       return;
10622
10623     case 'd':
10624       /* CONST_TRUE_RTX means always -- that's the default.  */
10625       if (x == const_true_rtx)
10626         return;
10627
10628       if (!COMPARISON_P (x))
10629         {
10630           output_operand_lossage ("invalid operand for code '%c'", code);
10631           return;
10632         }
10633
10634       fputs (arm_condition_codes[get_arm_condition_code (x)],
10635              stream);
10636       return;
10637
10638     case 'D':
10639       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
10640          want to do that.  */
10641       if (x == const_true_rtx)
10642         {
10643           output_operand_lossage ("instruction never exectued");
10644           return;
10645         }
10646       if (!COMPARISON_P (x))
10647         {
10648           output_operand_lossage ("invalid operand for code '%c'", code);
10649           return;
10650         }
10651
10652       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10653                                  (get_arm_condition_code (x))],
10654              stream);
10655       return;
10656
10657     /* Cirrus registers can be accessed in a variety of ways:
10658          single floating point (f)
10659          double floating point (d)
10660          32bit integer         (fx)
10661          64bit integer         (dx).  */
10662     case 'W':                   /* Cirrus register in F mode.  */
10663     case 'X':                   /* Cirrus register in D mode.  */
10664     case 'Y':                   /* Cirrus register in FX mode.  */
10665     case 'Z':                   /* Cirrus register in DX mode.  */
10666       gcc_assert (GET_CODE (x) == REG
10667                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
10668
10669       fprintf (stream, "mv%s%s",
10670                code == 'W' ? "f"
10671                : code == 'X' ? "d"
10672                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10673
10674       return;
10675
10676     /* Print cirrus register in the mode specified by the register's mode.  */
10677     case 'V':
10678       {
10679         int mode = GET_MODE (x);
10680
10681         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10682           {
10683             output_operand_lossage ("invalid operand for code '%c'", code);
10684             return;
10685           }
10686
10687         fprintf (stream, "mv%s%s",
10688                  mode == DFmode ? "d"
10689                  : mode == SImode ? "fx"
10690                  : mode == DImode ? "dx"
10691                  : "f", reg_names[REGNO (x)] + 2);
10692
10693         return;
10694       }
10695
10696     case 'U':
10697       if (GET_CODE (x) != REG
10698           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10699           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10700         /* Bad value for wCG register number.  */
10701         {
10702           output_operand_lossage ("invalid operand for code '%c'", code);
10703           return;
10704         }
10705
10706       else
10707         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10708       return;
10709
10710       /* Print an iWMMXt control register name.  */
10711     case 'w':
10712       if (GET_CODE (x) != CONST_INT
10713           || INTVAL (x) < 0
10714           || INTVAL (x) >= 16)
10715         /* Bad value for wC register number.  */
10716         {
10717           output_operand_lossage ("invalid operand for code '%c'", code);
10718           return;
10719         }
10720
10721       else
10722         {
10723           static const char * wc_reg_names [16] =
10724             {
10725               "wCID",  "wCon",  "wCSSF", "wCASF",
10726               "wC4",   "wC5",   "wC6",   "wC7",
10727               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10728               "wC12",  "wC13",  "wC14",  "wC15"
10729             };
10730
10731           fprintf (stream, wc_reg_names [INTVAL (x)]);
10732         }
10733       return;
10734
10735       /* Print a VFP double precision register name.  */
10736     case 'P':
10737       {
10738         int mode = GET_MODE (x);
10739         int num;
10740
10741         if (mode != DImode && mode != DFmode)
10742           {
10743             output_operand_lossage ("invalid operand for code '%c'", code);
10744             return;
10745           }
10746
10747         if (GET_CODE (x) != REG
10748             || !IS_VFP_REGNUM (REGNO (x)))
10749           {
10750             output_operand_lossage ("invalid operand for code '%c'", code);
10751             return;
10752           }
10753
10754         num = REGNO(x) - FIRST_VFP_REGNUM;
10755         if (num & 1)
10756           {
10757             output_operand_lossage ("invalid operand for code '%c'", code);
10758             return;
10759           }
10760
10761         fprintf (stream, "d%d", num >> 1);
10762       }
10763       return;
10764
10765     default:
10766       if (x == 0)
10767         {
10768           output_operand_lossage ("missing operand");
10769           return;
10770         }
10771
10772       switch (GET_CODE (x))
10773         {
10774         case REG:
10775           asm_fprintf (stream, "%r", REGNO (x));
10776           break;
10777
10778         case MEM:
10779           output_memory_reference_mode = GET_MODE (x);
10780           output_address (XEXP (x, 0));
10781           break;
10782
10783         case CONST_DOUBLE:
10784           fprintf (stream, "#%s", fp_immediate_constant (x));
10785           break;
10786
10787         default:
10788           gcc_assert (GET_CODE (x) != NEG);
10789           fputc ('#', stream);
10790           output_addr_const (stream, x);
10791           break;
10792         }
10793     }
10794 }
10795 \f
10796 #ifndef AOF_ASSEMBLER
10797 /* Target hook for assembling integer objects.  The ARM version needs to
10798    handle word-sized values specially.  */
10799 static bool
10800 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10801 {
10802   if (size == UNITS_PER_WORD && aligned_p)
10803     {
10804       fputs ("\t.word\t", asm_out_file);
10805       output_addr_const (asm_out_file, x);
10806
10807       /* Mark symbols as position independent.  We only do this in the
10808          .text segment, not in the .data segment.  */
10809       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10810           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10811         {
10812           if (GET_CODE (x) == SYMBOL_REF
10813               && (CONSTANT_POOL_ADDRESS_P (x)
10814                   || SYMBOL_REF_LOCAL_P (x)))
10815             fputs ("(GOTOFF)", asm_out_file);
10816           else if (GET_CODE (x) == LABEL_REF)
10817             fputs ("(GOTOFF)", asm_out_file);
10818           else
10819             fputs ("(GOT)", asm_out_file);
10820         }
10821       fputc ('\n', asm_out_file);
10822       return true;
10823     }
10824
10825   if (arm_vector_mode_supported_p (GET_MODE (x)))
10826     {
10827       int i, units;
10828
10829       gcc_assert (GET_CODE (x) == CONST_VECTOR);
10830
10831       units = CONST_VECTOR_NUNITS (x);
10832
10833       switch (GET_MODE (x))
10834         {
10835         case V2SImode: size = 4; break;
10836         case V4HImode: size = 2; break;
10837         case V8QImode: size = 1; break;
10838         default:
10839           gcc_unreachable ();
10840         }
10841
10842       for (i = 0; i < units; i++)
10843         {
10844           rtx elt;
10845
10846           elt = CONST_VECTOR_ELT (x, i);
10847           assemble_integer
10848             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10849         }
10850
10851       return true;
10852     }
10853
10854   return default_assemble_integer (x, size, aligned_p);
10855 }
10856
10857
10858 /* Add a function to the list of static constructors.  */
10859
10860 static void
10861 arm_elf_asm_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
10862 {
10863   if (!TARGET_AAPCS_BASED)
10864     {
10865       default_named_section_asm_out_constructor (symbol, priority);
10866       return;
10867     }
10868
10869   /* Put these in the .init_array section, using a special relocation.  */
10870   ctors_section ();
10871   assemble_align (POINTER_SIZE);
10872   fputs ("\t.word\t", asm_out_file);
10873   output_addr_const (asm_out_file, symbol);
10874   fputs ("(target1)\n", asm_out_file);
10875 }
10876 #endif
10877 \f
10878 /* A finite state machine takes care of noticing whether or not instructions
10879    can be conditionally executed, and thus decrease execution time and code
10880    size by deleting branch instructions.  The fsm is controlled by
10881    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
10882
10883 /* The state of the fsm controlling condition codes are:
10884    0: normal, do nothing special
10885    1: make ASM_OUTPUT_OPCODE not output this instruction
10886    2: make ASM_OUTPUT_OPCODE not output this instruction
10887    3: make instructions conditional
10888    4: make instructions conditional
10889
10890    State transitions (state->state by whom under condition):
10891    0 -> 1 final_prescan_insn if the `target' is a label
10892    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10893    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10894    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10895    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10896           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10897    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10898           (the target insn is arm_target_insn).
10899
10900    If the jump clobbers the conditions then we use states 2 and 4.
10901
10902    A similar thing can be done with conditional return insns.
10903
10904    XXX In case the `target' is an unconditional branch, this conditionalising
10905    of the instructions always reduces code size, but not always execution
10906    time.  But then, I want to reduce the code size to somewhere near what
10907    /bin/cc produces.  */
10908
10909 /* Returns the index of the ARM condition code string in
10910    `arm_condition_codes'.  COMPARISON should be an rtx like
10911    `(eq (...) (...))'.  */
10912 static enum arm_cond_code
10913 get_arm_condition_code (rtx comparison)
10914 {
10915   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10916   int code;
10917   enum rtx_code comp_code = GET_CODE (comparison);
10918
10919   if (GET_MODE_CLASS (mode) != MODE_CC)
10920     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10921                            XEXP (comparison, 1));
10922
10923   switch (mode)
10924     {
10925     case CC_DNEmode: code = ARM_NE; goto dominance;
10926     case CC_DEQmode: code = ARM_EQ; goto dominance;
10927     case CC_DGEmode: code = ARM_GE; goto dominance;
10928     case CC_DGTmode: code = ARM_GT; goto dominance;
10929     case CC_DLEmode: code = ARM_LE; goto dominance;
10930     case CC_DLTmode: code = ARM_LT; goto dominance;
10931     case CC_DGEUmode: code = ARM_CS; goto dominance;
10932     case CC_DGTUmode: code = ARM_HI; goto dominance;
10933     case CC_DLEUmode: code = ARM_LS; goto dominance;
10934     case CC_DLTUmode: code = ARM_CC;
10935
10936     dominance:
10937       gcc_assert (comp_code == EQ || comp_code == NE);
10938
10939       if (comp_code == EQ)
10940         return ARM_INVERSE_CONDITION_CODE (code);
10941       return code;
10942
10943     case CC_NOOVmode:
10944       switch (comp_code)
10945         {
10946         case NE: return ARM_NE;
10947         case EQ: return ARM_EQ;
10948         case GE: return ARM_PL;
10949         case LT: return ARM_MI;
10950         default: gcc_unreachable ();
10951         }
10952
10953     case CC_Zmode:
10954       switch (comp_code)
10955         {
10956         case NE: return ARM_NE;
10957         case EQ: return ARM_EQ;
10958         default: gcc_unreachable ();
10959         }
10960
10961     case CC_Nmode:
10962       switch (comp_code)
10963         {
10964         case NE: return ARM_MI;
10965         case EQ: return ARM_PL;
10966         default: gcc_unreachable ();
10967         }
10968
10969     case CCFPEmode:
10970     case CCFPmode:
10971       /* These encodings assume that AC=1 in the FPA system control
10972          byte.  This allows us to handle all cases except UNEQ and
10973          LTGT.  */
10974       switch (comp_code)
10975         {
10976         case GE: return ARM_GE;
10977         case GT: return ARM_GT;
10978         case LE: return ARM_LS;
10979         case LT: return ARM_MI;
10980         case NE: return ARM_NE;
10981         case EQ: return ARM_EQ;
10982         case ORDERED: return ARM_VC;
10983         case UNORDERED: return ARM_VS;
10984         case UNLT: return ARM_LT;
10985         case UNLE: return ARM_LE;
10986         case UNGT: return ARM_HI;
10987         case UNGE: return ARM_PL;
10988           /* UNEQ and LTGT do not have a representation.  */
10989         case UNEQ: /* Fall through.  */
10990         case LTGT: /* Fall through.  */
10991         default: gcc_unreachable ();
10992         }
10993
10994     case CC_SWPmode:
10995       switch (comp_code)
10996         {
10997         case NE: return ARM_NE;
10998         case EQ: return ARM_EQ;
10999         case GE: return ARM_LE;
11000         case GT: return ARM_LT;
11001         case LE: return ARM_GE;
11002         case LT: return ARM_GT;
11003         case GEU: return ARM_LS;
11004         case GTU: return ARM_CC;
11005         case LEU: return ARM_CS;
11006         case LTU: return ARM_HI;
11007         default: gcc_unreachable ();
11008         }
11009
11010     case CC_Cmode:
11011       switch (comp_code)
11012       {
11013       case LTU: return ARM_CS;
11014       case GEU: return ARM_CC;
11015       default: gcc_unreachable ();
11016       }
11017
11018     case CCmode:
11019       switch (comp_code)
11020         {
11021         case NE: return ARM_NE;
11022         case EQ: return ARM_EQ;
11023         case GE: return ARM_GE;
11024         case GT: return ARM_GT;
11025         case LE: return ARM_LE;
11026         case LT: return ARM_LT;
11027         case GEU: return ARM_CS;
11028         case GTU: return ARM_HI;
11029         case LEU: return ARM_LS;
11030         case LTU: return ARM_CC;
11031         default: gcc_unreachable ();
11032         }
11033
11034     default: gcc_unreachable ();
11035     }
11036 }
11037
11038 void
11039 arm_final_prescan_insn (rtx insn)
11040 {
11041   /* BODY will hold the body of INSN.  */
11042   rtx body = PATTERN (insn);
11043
11044   /* This will be 1 if trying to repeat the trick, and things need to be
11045      reversed if it appears to fail.  */
11046   int reverse = 0;
11047
11048   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11049      taken are clobbered, even if the rtl suggests otherwise.  It also
11050      means that we have to grub around within the jump expression to find
11051      out what the conditions are when the jump isn't taken.  */
11052   int jump_clobbers = 0;
11053
11054   /* If we start with a return insn, we only succeed if we find another one.  */
11055   int seeking_return = 0;
11056
11057   /* START_INSN will hold the insn from where we start looking.  This is the
11058      first insn after the following code_label if REVERSE is true.  */
11059   rtx start_insn = insn;
11060
11061   /* If in state 4, check if the target branch is reached, in order to
11062      change back to state 0.  */
11063   if (arm_ccfsm_state == 4)
11064     {
11065       if (insn == arm_target_insn)
11066         {
11067           arm_target_insn = NULL;
11068           arm_ccfsm_state = 0;
11069         }
11070       return;
11071     }
11072
11073   /* If in state 3, it is possible to repeat the trick, if this insn is an
11074      unconditional branch to a label, and immediately following this branch
11075      is the previous target label which is only used once, and the label this
11076      branch jumps to is not too far off.  */
11077   if (arm_ccfsm_state == 3)
11078     {
11079       if (simplejump_p (insn))
11080         {
11081           start_insn = next_nonnote_insn (start_insn);
11082           if (GET_CODE (start_insn) == BARRIER)
11083             {
11084               /* XXX Isn't this always a barrier?  */
11085               start_insn = next_nonnote_insn (start_insn);
11086             }
11087           if (GET_CODE (start_insn) == CODE_LABEL
11088               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11089               && LABEL_NUSES (start_insn) == 1)
11090             reverse = TRUE;
11091           else
11092             return;
11093         }
11094       else if (GET_CODE (body) == RETURN)
11095         {
11096           start_insn = next_nonnote_insn (start_insn);
11097           if (GET_CODE (start_insn) == BARRIER)
11098             start_insn = next_nonnote_insn (start_insn);
11099           if (GET_CODE (start_insn) == CODE_LABEL
11100               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11101               && LABEL_NUSES (start_insn) == 1)
11102             {
11103               reverse = TRUE;
11104               seeking_return = 1;
11105             }
11106           else
11107             return;
11108         }
11109       else
11110         return;
11111     }
11112
11113   gcc_assert (!arm_ccfsm_state || reverse);
11114   if (GET_CODE (insn) != JUMP_INSN)
11115     return;
11116
11117   /* This jump might be paralleled with a clobber of the condition codes
11118      the jump should always come first */
11119   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11120     body = XVECEXP (body, 0, 0);
11121
11122   if (reverse
11123       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11124           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11125     {
11126       int insns_skipped;
11127       int fail = FALSE, succeed = FALSE;
11128       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
11129       int then_not_else = TRUE;
11130       rtx this_insn = start_insn, label = 0;
11131
11132       /* If the jump cannot be done with one instruction, we cannot
11133          conditionally execute the instruction in the inverse case.  */
11134       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11135         {
11136           jump_clobbers = 1;
11137           return;
11138         }
11139
11140       /* Register the insn jumped to.  */
11141       if (reverse)
11142         {
11143           if (!seeking_return)
11144             label = XEXP (SET_SRC (body), 0);
11145         }
11146       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11147         label = XEXP (XEXP (SET_SRC (body), 1), 0);
11148       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11149         {
11150           label = XEXP (XEXP (SET_SRC (body), 2), 0);
11151           then_not_else = FALSE;
11152         }
11153       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11154         seeking_return = 1;
11155       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11156         {
11157           seeking_return = 1;
11158           then_not_else = FALSE;
11159         }
11160       else
11161         gcc_unreachable ();
11162
11163       /* See how many insns this branch skips, and what kind of insns.  If all
11164          insns are okay, and the label or unconditional branch to the same
11165          label is not too far away, succeed.  */
11166       for (insns_skipped = 0;
11167            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11168         {
11169           rtx scanbody;
11170
11171           this_insn = next_nonnote_insn (this_insn);
11172           if (!this_insn)
11173             break;
11174
11175           switch (GET_CODE (this_insn))
11176             {
11177             case CODE_LABEL:
11178               /* Succeed if it is the target label, otherwise fail since
11179                  control falls in from somewhere else.  */
11180               if (this_insn == label)
11181                 {
11182                   if (jump_clobbers)
11183                     {
11184                       arm_ccfsm_state = 2;
11185                       this_insn = next_nonnote_insn (this_insn);
11186                     }
11187                   else
11188                     arm_ccfsm_state = 1;
11189                   succeed = TRUE;
11190                 }
11191               else
11192                 fail = TRUE;
11193               break;
11194
11195             case BARRIER:
11196               /* Succeed if the following insn is the target label.
11197                  Otherwise fail.
11198                  If return insns are used then the last insn in a function
11199                  will be a barrier.  */
11200               this_insn = next_nonnote_insn (this_insn);
11201               if (this_insn && this_insn == label)
11202                 {
11203                   if (jump_clobbers)
11204                     {
11205                       arm_ccfsm_state = 2;
11206                       this_insn = next_nonnote_insn (this_insn);
11207                     }
11208                   else
11209                     arm_ccfsm_state = 1;
11210                   succeed = TRUE;
11211                 }
11212               else
11213                 fail = TRUE;
11214               break;
11215
11216             case CALL_INSN:
11217               /* The AAPCS says that conditional calls should not be
11218                  used since they make interworking inefficient (the
11219                  linker can't transform BL<cond> into BLX).  That's
11220                  only a problem if the machine has BLX.  */
11221               if (arm_arch5)
11222                 {
11223                   fail = TRUE;
11224                   break;
11225                 }
11226
11227               /* Succeed if the following insn is the target label, or
11228                  if the following two insns are a barrier and the
11229                  target label.  */
11230               this_insn = next_nonnote_insn (this_insn);
11231               if (this_insn && GET_CODE (this_insn) == BARRIER)
11232                 this_insn = next_nonnote_insn (this_insn);
11233
11234               if (this_insn && this_insn == label
11235                   && insns_skipped < max_insns_skipped)
11236                 {
11237                   if (jump_clobbers)
11238                     {
11239                       arm_ccfsm_state = 2;
11240                       this_insn = next_nonnote_insn (this_insn);
11241                     }
11242                   else
11243                     arm_ccfsm_state = 1;
11244                   succeed = TRUE;
11245                 }
11246               else
11247                 fail = TRUE;
11248               break;
11249
11250             case JUMP_INSN:
11251               /* If this is an unconditional branch to the same label, succeed.
11252                  If it is to another label, do nothing.  If it is conditional,
11253                  fail.  */
11254               /* XXX Probably, the tests for SET and the PC are
11255                  unnecessary.  */
11256
11257               scanbody = PATTERN (this_insn);
11258               if (GET_CODE (scanbody) == SET
11259                   && GET_CODE (SET_DEST (scanbody)) == PC)
11260                 {
11261                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11262                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11263                     {
11264                       arm_ccfsm_state = 2;
11265                       succeed = TRUE;
11266                     }
11267                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11268                     fail = TRUE;
11269                 }
11270               /* Fail if a conditional return is undesirable (e.g. on a
11271                  StrongARM), but still allow this if optimizing for size.  */
11272               else if (GET_CODE (scanbody) == RETURN
11273                        && !use_return_insn (TRUE, NULL)
11274                        && !optimize_size)
11275                 fail = TRUE;
11276               else if (GET_CODE (scanbody) == RETURN
11277                        && seeking_return)
11278                 {
11279                   arm_ccfsm_state = 2;
11280                   succeed = TRUE;
11281                 }
11282               else if (GET_CODE (scanbody) == PARALLEL)
11283                 {
11284                   switch (get_attr_conds (this_insn))
11285                     {
11286                     case CONDS_NOCOND:
11287                       break;
11288                     default:
11289                       fail = TRUE;
11290                       break;
11291                     }
11292                 }
11293               else
11294                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
11295
11296               break;
11297
11298             case INSN:
11299               /* Instructions using or affecting the condition codes make it
11300                  fail.  */
11301               scanbody = PATTERN (this_insn);
11302               if (!(GET_CODE (scanbody) == SET
11303                     || GET_CODE (scanbody) == PARALLEL)
11304                   || get_attr_conds (this_insn) != CONDS_NOCOND)
11305                 fail = TRUE;
11306
11307               /* A conditional cirrus instruction must be followed by
11308                  a non Cirrus instruction.  However, since we
11309                  conditionalize instructions in this function and by
11310                  the time we get here we can't add instructions
11311                  (nops), because shorten_branches() has already been
11312                  called, we will disable conditionalizing Cirrus
11313                  instructions to be safe.  */
11314               if (GET_CODE (scanbody) != USE
11315                   && GET_CODE (scanbody) != CLOBBER
11316                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11317                 fail = TRUE;
11318               break;
11319
11320             default:
11321               break;
11322             }
11323         }
11324       if (succeed)
11325         {
11326           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11327             arm_target_label = CODE_LABEL_NUMBER (label);
11328           else
11329             {
11330               gcc_assert (seeking_return || arm_ccfsm_state == 2);
11331               
11332               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11333                 {
11334                   this_insn = next_nonnote_insn (this_insn);
11335                   gcc_assert (!this_insn
11336                               || (GET_CODE (this_insn) != BARRIER
11337                                   && GET_CODE (this_insn) != CODE_LABEL));
11338                 }
11339               if (!this_insn)
11340                 {
11341                   /* Oh, dear! we ran off the end.. give up.  */
11342                   recog (PATTERN (insn), insn, NULL);
11343                   arm_ccfsm_state = 0;
11344                   arm_target_insn = NULL;
11345                   return;
11346                 }
11347               arm_target_insn = this_insn;
11348             }
11349           if (jump_clobbers)
11350             {
11351               gcc_assert (!reverse);
11352               arm_current_cc =
11353                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11354                                                             0), 0), 1));
11355               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11356                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11357               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11358                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11359             }
11360           else
11361             {
11362               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11363                  what it was.  */
11364               if (!reverse)
11365                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11366                                                                0));
11367             }
11368
11369           if (reverse || then_not_else)
11370             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11371         }
11372
11373       /* Restore recog_data (getting the attributes of other insns can
11374          destroy this array, but final.c assumes that it remains intact
11375          across this call; since the insn has been recognized already we
11376          call recog direct).  */
11377       recog (PATTERN (insn), insn, NULL);
11378     }
11379 }
11380
11381 /* Returns true if REGNO is a valid register
11382    for holding a quantity of type MODE.  */
11383 int
11384 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11385 {
11386   if (GET_MODE_CLASS (mode) == MODE_CC)
11387     return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11388
11389   if (TARGET_THUMB)
11390     /* For the Thumb we only allow values bigger than SImode in
11391        registers 0 - 6, so that there is always a second low
11392        register available to hold the upper part of the value.
11393        We probably we ought to ensure that the register is the
11394        start of an even numbered register pair.  */
11395     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11396
11397   if (IS_CIRRUS_REGNUM (regno))
11398     /* We have outlawed SI values in Cirrus registers because they
11399        reside in the lower 32 bits, but SF values reside in the
11400        upper 32 bits.  This causes gcc all sorts of grief.  We can't
11401        even split the registers into pairs because Cirrus SI values
11402        get sign extended to 64bits-- aldyh.  */
11403     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11404
11405   if (IS_VFP_REGNUM (regno))
11406     {
11407       if (mode == SFmode || mode == SImode)
11408         return TRUE;
11409
11410       /* DFmode values are only valid in even register pairs.  */
11411       if (mode == DFmode)
11412         return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11413       return FALSE;
11414     }
11415
11416   if (IS_IWMMXT_GR_REGNUM (regno))
11417     return mode == SImode;
11418
11419   if (IS_IWMMXT_REGNUM (regno))
11420     return VALID_IWMMXT_REG_MODE (mode);
11421
11422   /* We allow any value to be stored in the general registers.
11423      Restrict doubleword quantities to even register pairs so that we can
11424      use ldrd.  */
11425   if (regno <= LAST_ARM_REGNUM)
11426     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11427
11428   if (   regno == FRAME_POINTER_REGNUM
11429       || regno == ARG_POINTER_REGNUM)
11430     /* We only allow integers in the fake hard registers.  */
11431     return GET_MODE_CLASS (mode) == MODE_INT;
11432
11433   /* The only registers left are the FPA registers
11434      which we only allow to hold FP values.  */
11435   return GET_MODE_CLASS (mode) == MODE_FLOAT
11436     && regno >= FIRST_FPA_REGNUM
11437     && regno <= LAST_FPA_REGNUM;
11438 }
11439
11440 int
11441 arm_regno_class (int regno)
11442 {
11443   if (TARGET_THUMB)
11444     {
11445       if (regno == STACK_POINTER_REGNUM)
11446         return STACK_REG;
11447       if (regno == CC_REGNUM)
11448         return CC_REG;
11449       if (regno < 8)
11450         return LO_REGS;
11451       return HI_REGS;
11452     }
11453
11454   if (   regno <= LAST_ARM_REGNUM
11455       || regno == FRAME_POINTER_REGNUM
11456       || regno == ARG_POINTER_REGNUM)
11457     return GENERAL_REGS;
11458
11459   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11460     return NO_REGS;
11461
11462   if (IS_CIRRUS_REGNUM (regno))
11463     return CIRRUS_REGS;
11464
11465   if (IS_VFP_REGNUM (regno))
11466     return VFP_REGS;
11467
11468   if (IS_IWMMXT_REGNUM (regno))
11469     return IWMMXT_REGS;
11470
11471   if (IS_IWMMXT_GR_REGNUM (regno))
11472     return IWMMXT_GR_REGS;
11473
11474   return FPA_REGS;
11475 }
11476
11477 /* Handle a special case when computing the offset
11478    of an argument from the frame pointer.  */
11479 int
11480 arm_debugger_arg_offset (int value, rtx addr)
11481 {
11482   rtx insn;
11483
11484   /* We are only interested if dbxout_parms() failed to compute the offset.  */
11485   if (value != 0)
11486     return 0;
11487
11488   /* We can only cope with the case where the address is held in a register.  */
11489   if (GET_CODE (addr) != REG)
11490     return 0;
11491
11492   /* If we are using the frame pointer to point at the argument, then
11493      an offset of 0 is correct.  */
11494   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11495     return 0;
11496
11497   /* If we are using the stack pointer to point at the
11498      argument, then an offset of 0 is correct.  */
11499   if ((TARGET_THUMB || !frame_pointer_needed)
11500       && REGNO (addr) == SP_REGNUM)
11501     return 0;
11502
11503   /* Oh dear.  The argument is pointed to by a register rather
11504      than being held in a register, or being stored at a known
11505      offset from the frame pointer.  Since GDB only understands
11506      those two kinds of argument we must translate the address
11507      held in the register into an offset from the frame pointer.
11508      We do this by searching through the insns for the function
11509      looking to see where this register gets its value.  If the
11510      register is initialized from the frame pointer plus an offset
11511      then we are in luck and we can continue, otherwise we give up.
11512
11513      This code is exercised by producing debugging information
11514      for a function with arguments like this:
11515
11516            double func (double a, double b, int c, double d) {return d;}
11517
11518      Without this code the stab for parameter 'd' will be set to
11519      an offset of 0 from the frame pointer, rather than 8.  */
11520
11521   /* The if() statement says:
11522
11523      If the insn is a normal instruction
11524      and if the insn is setting the value in a register
11525      and if the register being set is the register holding the address of the argument
11526      and if the address is computing by an addition
11527      that involves adding to a register
11528      which is the frame pointer
11529      a constant integer
11530
11531      then...  */
11532
11533   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11534     {
11535       if (   GET_CODE (insn) == INSN
11536           && GET_CODE (PATTERN (insn)) == SET
11537           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11538           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11539           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11540           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11541           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11542              )
11543         {
11544           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11545
11546           break;
11547         }
11548     }
11549
11550   if (value == 0)
11551     {
11552       debug_rtx (addr);
11553       warning (0, "unable to compute real location of stacked parameter");
11554       value = 8; /* XXX magic hack */
11555     }
11556
11557   return value;
11558 }
11559 \f
11560 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
11561   do                                                                    \
11562     {                                                                   \
11563       if ((MASK) & insn_flags)                                          \
11564         lang_hooks.builtin_function ((NAME), (TYPE), (CODE),            \
11565                                      BUILT_IN_MD, NULL, NULL_TREE);     \
11566     }                                                                   \
11567   while (0)
11568
11569 struct builtin_description
11570 {
11571   const unsigned int       mask;
11572   const enum insn_code     icode;
11573   const char * const       name;
11574   const enum arm_builtins  code;
11575   const enum rtx_code      comparison;
11576   const unsigned int       flag;
11577 };
11578
11579 static const struct builtin_description bdesc_2arg[] =
11580 {
11581 #define IWMMXT_BUILTIN(code, string, builtin) \
11582   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11583     ARM_BUILTIN_##builtin, 0, 0 },
11584
11585   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11586   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11587   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11588   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11589   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11590   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11591   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11592   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11593   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11594   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11595   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11596   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11597   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11598   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11599   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11600   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11601   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11602   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11603   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11604   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11605   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11606   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11607   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11608   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11609   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11610   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11611   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11612   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11613   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11614   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11615   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11616   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11617   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11618   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11619   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11620   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11621   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11622   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11623   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11624   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11625   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11626   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11627   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11628   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11629   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11630   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11631   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11632   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11633   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11634   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11635   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11636   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11637   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11638   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11639   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11640   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11641   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11642   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11643
11644 #define IWMMXT_BUILTIN2(code, builtin) \
11645   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11646
11647   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11648   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11649   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11650   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11651   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11652   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11653   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
11654   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
11655   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
11656   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
11657   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
11658   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
11659   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
11660   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
11661   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
11662   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
11663   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
11664   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
11665   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
11666   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
11667   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
11668   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
11669   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
11670   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
11671   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
11672   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
11673   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
11674   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
11675   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
11676   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
11677   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
11678   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
11679 };
11680
11681 static const struct builtin_description bdesc_1arg[] =
11682 {
11683   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11684   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11685   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11686   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11687   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11688   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11689   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11690   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11691   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11692   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11693   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11694   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11695   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11696   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11697   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11698   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11699   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11700   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11701 };
11702
11703 /* Set up all the iWMMXt builtins.  This is
11704    not called if TARGET_IWMMXT is zero.  */
11705
11706 static void
11707 arm_init_iwmmxt_builtins (void)
11708 {
11709   const struct builtin_description * d;
11710   size_t i;
11711   tree endlink = void_list_node;
11712
11713   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11714   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11715   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11716
11717   tree int_ftype_int
11718     = build_function_type (integer_type_node,
11719                            tree_cons (NULL_TREE, integer_type_node, endlink));
11720   tree v8qi_ftype_v8qi_v8qi_int
11721     = build_function_type (V8QI_type_node,
11722                            tree_cons (NULL_TREE, V8QI_type_node,
11723                                       tree_cons (NULL_TREE, V8QI_type_node,
11724                                                  tree_cons (NULL_TREE,
11725                                                             integer_type_node,
11726                                                             endlink))));
11727   tree v4hi_ftype_v4hi_int
11728     = build_function_type (V4HI_type_node,
11729                            tree_cons (NULL_TREE, V4HI_type_node,
11730                                       tree_cons (NULL_TREE, integer_type_node,
11731                                                  endlink)));
11732   tree v2si_ftype_v2si_int
11733     = build_function_type (V2SI_type_node,
11734                            tree_cons (NULL_TREE, V2SI_type_node,
11735                                       tree_cons (NULL_TREE, integer_type_node,
11736                                                  endlink)));
11737   tree v2si_ftype_di_di
11738     = build_function_type (V2SI_type_node,
11739                            tree_cons (NULL_TREE, long_long_integer_type_node,
11740                                       tree_cons (NULL_TREE, long_long_integer_type_node,
11741                                                  endlink)));
11742   tree di_ftype_di_int
11743     = build_function_type (long_long_integer_type_node,
11744                            tree_cons (NULL_TREE, long_long_integer_type_node,
11745                                       tree_cons (NULL_TREE, integer_type_node,
11746                                                  endlink)));
11747   tree di_ftype_di_int_int
11748     = build_function_type (long_long_integer_type_node,
11749                            tree_cons (NULL_TREE, long_long_integer_type_node,
11750                                       tree_cons (NULL_TREE, integer_type_node,
11751                                                  tree_cons (NULL_TREE,
11752                                                             integer_type_node,
11753                                                             endlink))));
11754   tree int_ftype_v8qi
11755     = build_function_type (integer_type_node,
11756                            tree_cons (NULL_TREE, V8QI_type_node,
11757                                       endlink));
11758   tree int_ftype_v4hi
11759     = build_function_type (integer_type_node,
11760                            tree_cons (NULL_TREE, V4HI_type_node,
11761                                       endlink));
11762   tree int_ftype_v2si
11763     = build_function_type (integer_type_node,
11764                            tree_cons (NULL_TREE, V2SI_type_node,
11765                                       endlink));
11766   tree int_ftype_v8qi_int
11767     = build_function_type (integer_type_node,
11768                            tree_cons (NULL_TREE, V8QI_type_node,
11769                                       tree_cons (NULL_TREE, integer_type_node,
11770                                                  endlink)));
11771   tree int_ftype_v4hi_int
11772     = build_function_type (integer_type_node,
11773                            tree_cons (NULL_TREE, V4HI_type_node,
11774                                       tree_cons (NULL_TREE, integer_type_node,
11775                                                  endlink)));
11776   tree int_ftype_v2si_int
11777     = build_function_type (integer_type_node,
11778                            tree_cons (NULL_TREE, V2SI_type_node,
11779                                       tree_cons (NULL_TREE, integer_type_node,
11780                                                  endlink)));
11781   tree v8qi_ftype_v8qi_int_int
11782     = build_function_type (V8QI_type_node,
11783                            tree_cons (NULL_TREE, V8QI_type_node,
11784                                       tree_cons (NULL_TREE, integer_type_node,
11785                                                  tree_cons (NULL_TREE,
11786                                                             integer_type_node,
11787                                                             endlink))));
11788   tree v4hi_ftype_v4hi_int_int
11789     = build_function_type (V4HI_type_node,
11790                            tree_cons (NULL_TREE, V4HI_type_node,
11791                                       tree_cons (NULL_TREE, integer_type_node,
11792                                                  tree_cons (NULL_TREE,
11793                                                             integer_type_node,
11794                                                             endlink))));
11795   tree v2si_ftype_v2si_int_int
11796     = build_function_type (V2SI_type_node,
11797                            tree_cons (NULL_TREE, V2SI_type_node,
11798                                       tree_cons (NULL_TREE, integer_type_node,
11799                                                  tree_cons (NULL_TREE,
11800                                                             integer_type_node,
11801                                                             endlink))));
11802   /* Miscellaneous.  */
11803   tree v8qi_ftype_v4hi_v4hi
11804     = build_function_type (V8QI_type_node,
11805                            tree_cons (NULL_TREE, V4HI_type_node,
11806                                       tree_cons (NULL_TREE, V4HI_type_node,
11807                                                  endlink)));
11808   tree v4hi_ftype_v2si_v2si
11809     = build_function_type (V4HI_type_node,
11810                            tree_cons (NULL_TREE, V2SI_type_node,
11811                                       tree_cons (NULL_TREE, V2SI_type_node,
11812                                                  endlink)));
11813   tree v2si_ftype_v4hi_v4hi
11814     = build_function_type (V2SI_type_node,
11815                            tree_cons (NULL_TREE, V4HI_type_node,
11816                                       tree_cons (NULL_TREE, V4HI_type_node,
11817                                                  endlink)));
11818   tree v2si_ftype_v8qi_v8qi
11819     = build_function_type (V2SI_type_node,
11820                            tree_cons (NULL_TREE, V8QI_type_node,
11821                                       tree_cons (NULL_TREE, V8QI_type_node,
11822                                                  endlink)));
11823   tree v4hi_ftype_v4hi_di
11824     = build_function_type (V4HI_type_node,
11825                            tree_cons (NULL_TREE, V4HI_type_node,
11826                                       tree_cons (NULL_TREE,
11827                                                  long_long_integer_type_node,
11828                                                  endlink)));
11829   tree v2si_ftype_v2si_di
11830     = build_function_type (V2SI_type_node,
11831                            tree_cons (NULL_TREE, V2SI_type_node,
11832                                       tree_cons (NULL_TREE,
11833                                                  long_long_integer_type_node,
11834                                                  endlink)));
11835   tree void_ftype_int_int
11836     = build_function_type (void_type_node,
11837                            tree_cons (NULL_TREE, integer_type_node,
11838                                       tree_cons (NULL_TREE, integer_type_node,
11839                                                  endlink)));
11840   tree di_ftype_void
11841     = build_function_type (long_long_unsigned_type_node, endlink);
11842   tree di_ftype_v8qi
11843     = build_function_type (long_long_integer_type_node,
11844                            tree_cons (NULL_TREE, V8QI_type_node,
11845                                       endlink));
11846   tree di_ftype_v4hi
11847     = build_function_type (long_long_integer_type_node,
11848                            tree_cons (NULL_TREE, V4HI_type_node,
11849                                       endlink));
11850   tree di_ftype_v2si
11851     = build_function_type (long_long_integer_type_node,
11852                            tree_cons (NULL_TREE, V2SI_type_node,
11853                                       endlink));
11854   tree v2si_ftype_v4hi
11855     = build_function_type (V2SI_type_node,
11856                            tree_cons (NULL_TREE, V4HI_type_node,
11857                                       endlink));
11858   tree v4hi_ftype_v8qi
11859     = build_function_type (V4HI_type_node,
11860                            tree_cons (NULL_TREE, V8QI_type_node,
11861                                       endlink));
11862
11863   tree di_ftype_di_v4hi_v4hi
11864     = build_function_type (long_long_unsigned_type_node,
11865                            tree_cons (NULL_TREE,
11866                                       long_long_unsigned_type_node,
11867                                       tree_cons (NULL_TREE, V4HI_type_node,
11868                                                  tree_cons (NULL_TREE,
11869                                                             V4HI_type_node,
11870                                                             endlink))));
11871
11872   tree di_ftype_v4hi_v4hi
11873     = build_function_type (long_long_unsigned_type_node,
11874                            tree_cons (NULL_TREE, V4HI_type_node,
11875                                       tree_cons (NULL_TREE, V4HI_type_node,
11876                                                  endlink)));
11877
11878   /* Normal vector binops.  */
11879   tree v8qi_ftype_v8qi_v8qi
11880     = build_function_type (V8QI_type_node,
11881                            tree_cons (NULL_TREE, V8QI_type_node,
11882                                       tree_cons (NULL_TREE, V8QI_type_node,
11883                                                  endlink)));
11884   tree v4hi_ftype_v4hi_v4hi
11885     = build_function_type (V4HI_type_node,
11886                            tree_cons (NULL_TREE, V4HI_type_node,
11887                                       tree_cons (NULL_TREE, V4HI_type_node,
11888                                                  endlink)));
11889   tree v2si_ftype_v2si_v2si
11890     = build_function_type (V2SI_type_node,
11891                            tree_cons (NULL_TREE, V2SI_type_node,
11892                                       tree_cons (NULL_TREE, V2SI_type_node,
11893                                                  endlink)));
11894   tree di_ftype_di_di
11895     = build_function_type (long_long_unsigned_type_node,
11896                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
11897                                       tree_cons (NULL_TREE,
11898                                                  long_long_unsigned_type_node,
11899                                                  endlink)));
11900
11901   /* Add all builtins that are more or less simple operations on two
11902      operands.  */
11903   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11904     {
11905       /* Use one of the operands; the target can have a different mode for
11906          mask-generating compares.  */
11907       enum machine_mode mode;
11908       tree type;
11909
11910       if (d->name == 0)
11911         continue;
11912
11913       mode = insn_data[d->icode].operand[1].mode;
11914
11915       switch (mode)
11916         {
11917         case V8QImode:
11918           type = v8qi_ftype_v8qi_v8qi;
11919           break;
11920         case V4HImode:
11921           type = v4hi_ftype_v4hi_v4hi;
11922           break;
11923         case V2SImode:
11924           type = v2si_ftype_v2si_v2si;
11925           break;
11926         case DImode:
11927           type = di_ftype_di_di;
11928           break;
11929
11930         default:
11931           gcc_unreachable ();
11932         }
11933
11934       def_mbuiltin (d->mask, d->name, type, d->code);
11935     }
11936
11937   /* Add the remaining MMX insns with somewhat more complicated types.  */
11938   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11939   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11940   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11941
11942   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11943   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11944   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11945   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11946   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11947   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11948
11949   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11950   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11951   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11952   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11953   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11954   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11955
11956   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11957   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11958   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11959   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11960   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11961   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11962
11963   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11964   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11965   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11966   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11967   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11968   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11969
11970   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11971
11972   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11973   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11974   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11975   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11976
11977   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11978   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11979   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11980   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11981   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11982   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11983   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11984   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11985   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11986
11987   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11988   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11989   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11990
11991   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11992   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11993   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
11994
11995   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
11996   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
11997   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
11998   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
11999   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
12000   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
12001
12002   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
12003   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
12004   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
12005   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
12006   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
12007   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
12008   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
12009   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
12010   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
12011   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
12012   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
12013   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
12014
12015   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
12016   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
12017   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
12018   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
12019
12020   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
12021   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
12022   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
12023   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
12024   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
12025   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
12026   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
12027 }
12028
12029 static void
12030 arm_init_builtins (void)
12031 {
12032   if (TARGET_REALLY_IWMMXT)
12033     arm_init_iwmmxt_builtins ();
12034 }
12035
12036 /* Errors in the source file can cause expand_expr to return const0_rtx
12037    where we expect a vector.  To avoid crashing, use one of the vector
12038    clear instructions.  */
12039
12040 static rtx
12041 safe_vector_operand (rtx x, enum machine_mode mode)
12042 {
12043   if (x != const0_rtx)
12044     return x;
12045   x = gen_reg_rtx (mode);
12046
12047   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12048                                : gen_rtx_SUBREG (DImode, x, 0)));
12049   return x;
12050 }
12051
12052 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
12053
12054 static rtx
12055 arm_expand_binop_builtin (enum insn_code icode,
12056                           tree arglist, rtx target)
12057 {
12058   rtx pat;
12059   tree arg0 = TREE_VALUE (arglist);
12060   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12061   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12062   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12063   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12064   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12065   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12066
12067   if (VECTOR_MODE_P (mode0))
12068     op0 = safe_vector_operand (op0, mode0);
12069   if (VECTOR_MODE_P (mode1))
12070     op1 = safe_vector_operand (op1, mode1);
12071
12072   if (! target
12073       || GET_MODE (target) != tmode
12074       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12075     target = gen_reg_rtx (tmode);
12076
12077   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
12078
12079   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12080     op0 = copy_to_mode_reg (mode0, op0);
12081   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12082     op1 = copy_to_mode_reg (mode1, op1);
12083
12084   pat = GEN_FCN (icode) (target, op0, op1);
12085   if (! pat)
12086     return 0;
12087   emit_insn (pat);
12088   return target;
12089 }
12090
12091 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
12092
12093 static rtx
12094 arm_expand_unop_builtin (enum insn_code icode,
12095                          tree arglist, rtx target, int do_load)
12096 {
12097   rtx pat;
12098   tree arg0 = TREE_VALUE (arglist);
12099   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12100   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12101   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12102
12103   if (! target
12104       || GET_MODE (target) != tmode
12105       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12106     target = gen_reg_rtx (tmode);
12107   if (do_load)
12108     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12109   else
12110     {
12111       if (VECTOR_MODE_P (mode0))
12112         op0 = safe_vector_operand (op0, mode0);
12113
12114       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12115         op0 = copy_to_mode_reg (mode0, op0);
12116     }
12117
12118   pat = GEN_FCN (icode) (target, op0);
12119   if (! pat)
12120     return 0;
12121   emit_insn (pat);
12122   return target;
12123 }
12124
12125 /* Expand an expression EXP that calls a built-in function,
12126    with result going to TARGET if that's convenient
12127    (and in mode MODE if that's convenient).
12128    SUBTARGET may be used as the target for computing one of EXP's operands.
12129    IGNORE is nonzero if the value is to be ignored.  */
12130
12131 static rtx
12132 arm_expand_builtin (tree exp,
12133                     rtx target,
12134                     rtx subtarget ATTRIBUTE_UNUSED,
12135                     enum machine_mode mode ATTRIBUTE_UNUSED,
12136                     int ignore ATTRIBUTE_UNUSED)
12137 {
12138   const struct builtin_description * d;
12139   enum insn_code    icode;
12140   tree              fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12141   tree              arglist = TREE_OPERAND (exp, 1);
12142   tree              arg0;
12143   tree              arg1;
12144   tree              arg2;
12145   rtx               op0;
12146   rtx               op1;
12147   rtx               op2;
12148   rtx               pat;
12149   int               fcode = DECL_FUNCTION_CODE (fndecl);
12150   size_t            i;
12151   enum machine_mode tmode;
12152   enum machine_mode mode0;
12153   enum machine_mode mode1;
12154   enum machine_mode mode2;
12155
12156   switch (fcode)
12157     {
12158     case ARM_BUILTIN_TEXTRMSB:
12159     case ARM_BUILTIN_TEXTRMUB:
12160     case ARM_BUILTIN_TEXTRMSH:
12161     case ARM_BUILTIN_TEXTRMUH:
12162     case ARM_BUILTIN_TEXTRMSW:
12163     case ARM_BUILTIN_TEXTRMUW:
12164       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12165                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12166                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12167                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12168                : CODE_FOR_iwmmxt_textrmw);
12169
12170       arg0 = TREE_VALUE (arglist);
12171       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12172       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12173       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12174       tmode = insn_data[icode].operand[0].mode;
12175       mode0 = insn_data[icode].operand[1].mode;
12176       mode1 = insn_data[icode].operand[2].mode;
12177
12178       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12179         op0 = copy_to_mode_reg (mode0, op0);
12180       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12181         {
12182           /* @@@ better error message */
12183           error ("selector must be an immediate");
12184           return gen_reg_rtx (tmode);
12185         }
12186       if (target == 0
12187           || GET_MODE (target) != tmode
12188           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12189         target = gen_reg_rtx (tmode);
12190       pat = GEN_FCN (icode) (target, op0, op1);
12191       if (! pat)
12192         return 0;
12193       emit_insn (pat);
12194       return target;
12195
12196     case ARM_BUILTIN_TINSRB:
12197     case ARM_BUILTIN_TINSRH:
12198     case ARM_BUILTIN_TINSRW:
12199       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12200                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12201                : CODE_FOR_iwmmxt_tinsrw);
12202       arg0 = TREE_VALUE (arglist);
12203       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12204       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12205       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12206       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12207       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12208       tmode = insn_data[icode].operand[0].mode;
12209       mode0 = insn_data[icode].operand[1].mode;
12210       mode1 = insn_data[icode].operand[2].mode;
12211       mode2 = insn_data[icode].operand[3].mode;
12212
12213       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12214         op0 = copy_to_mode_reg (mode0, op0);
12215       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12216         op1 = copy_to_mode_reg (mode1, op1);
12217       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12218         {
12219           /* @@@ better error message */
12220           error ("selector must be an immediate");
12221           return const0_rtx;
12222         }
12223       if (target == 0
12224           || GET_MODE (target) != tmode
12225           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12226         target = gen_reg_rtx (tmode);
12227       pat = GEN_FCN (icode) (target, op0, op1, op2);
12228       if (! pat)
12229         return 0;
12230       emit_insn (pat);
12231       return target;
12232
12233     case ARM_BUILTIN_SETWCX:
12234       arg0 = TREE_VALUE (arglist);
12235       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12236       op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12237       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12238       emit_insn (gen_iwmmxt_tmcr (op1, op0));
12239       return 0;
12240
12241     case ARM_BUILTIN_GETWCX:
12242       arg0 = TREE_VALUE (arglist);
12243       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12244       target = gen_reg_rtx (SImode);
12245       emit_insn (gen_iwmmxt_tmrc (target, op0));
12246       return target;
12247
12248     case ARM_BUILTIN_WSHUFH:
12249       icode = CODE_FOR_iwmmxt_wshufh;
12250       arg0 = TREE_VALUE (arglist);
12251       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12252       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12253       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12254       tmode = insn_data[icode].operand[0].mode;
12255       mode1 = insn_data[icode].operand[1].mode;
12256       mode2 = insn_data[icode].operand[2].mode;
12257
12258       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12259         op0 = copy_to_mode_reg (mode1, op0);
12260       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12261         {
12262           /* @@@ better error message */
12263           error ("mask must be an immediate");
12264           return const0_rtx;
12265         }
12266       if (target == 0
12267           || GET_MODE (target) != tmode
12268           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12269         target = gen_reg_rtx (tmode);
12270       pat = GEN_FCN (icode) (target, op0, op1);
12271       if (! pat)
12272         return 0;
12273       emit_insn (pat);
12274       return target;
12275
12276     case ARM_BUILTIN_WSADB:
12277       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12278     case ARM_BUILTIN_WSADH:
12279       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12280     case ARM_BUILTIN_WSADBZ:
12281       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12282     case ARM_BUILTIN_WSADHZ:
12283       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12284
12285       /* Several three-argument builtins.  */
12286     case ARM_BUILTIN_WMACS:
12287     case ARM_BUILTIN_WMACU:
12288     case ARM_BUILTIN_WALIGN:
12289     case ARM_BUILTIN_TMIA:
12290     case ARM_BUILTIN_TMIAPH:
12291     case ARM_BUILTIN_TMIATT:
12292     case ARM_BUILTIN_TMIATB:
12293     case ARM_BUILTIN_TMIABT:
12294     case ARM_BUILTIN_TMIABB:
12295       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12296                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12297                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12298                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12299                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12300                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12301                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12302                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12303                : CODE_FOR_iwmmxt_walign);
12304       arg0 = TREE_VALUE (arglist);
12305       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12306       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12307       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12308       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12309       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12310       tmode = insn_data[icode].operand[0].mode;
12311       mode0 = insn_data[icode].operand[1].mode;
12312       mode1 = insn_data[icode].operand[2].mode;
12313       mode2 = insn_data[icode].operand[3].mode;
12314
12315       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12316         op0 = copy_to_mode_reg (mode0, op0);
12317       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12318         op1 = copy_to_mode_reg (mode1, op1);
12319       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12320         op2 = copy_to_mode_reg (mode2, op2);
12321       if (target == 0
12322           || GET_MODE (target) != tmode
12323           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12324         target = gen_reg_rtx (tmode);
12325       pat = GEN_FCN (icode) (target, op0, op1, op2);
12326       if (! pat)
12327         return 0;
12328       emit_insn (pat);
12329       return target;
12330
12331     case ARM_BUILTIN_WZERO:
12332       target = gen_reg_rtx (DImode);
12333       emit_insn (gen_iwmmxt_clrdi (target));
12334       return target;
12335
12336     default:
12337       break;
12338     }
12339
12340   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12341     if (d->code == (const enum arm_builtins) fcode)
12342       return arm_expand_binop_builtin (d->icode, arglist, target);
12343
12344   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12345     if (d->code == (const enum arm_builtins) fcode)
12346       return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12347
12348   /* @@@ Should really do something sensible here.  */
12349   return NULL_RTX;
12350 }
12351 \f
12352 /* Return the number (counting from 0) of
12353    the least significant set bit in MASK.  */
12354
12355 inline static int
12356 number_of_first_bit_set (unsigned mask)
12357 {
12358   int bit;
12359
12360   for (bit = 0;
12361        (mask & (1 << bit)) == 0;
12362        ++bit)
12363     continue;
12364
12365   return bit;
12366 }
12367
12368 /* Emit code to push or pop registers to or from the stack.  F is the
12369    assembly file.  MASK is the registers to push or pop.  PUSH is
12370    nonzero if we should push, and zero if we should pop.  For debugging
12371    output, if pushing, adjust CFA_OFFSET by the amount of space added
12372    to the stack.  REAL_REGS should have the same number of bits set as
12373    MASK, and will be used instead (in the same order) to describe which
12374    registers were saved - this is used to mark the save slots when we
12375    push high registers after moving them to low registers.  */
12376 static void
12377 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
12378                unsigned long real_regs)
12379 {
12380   int regno;
12381   int lo_mask = mask & 0xFF;
12382   int pushed_words = 0;
12383
12384   gcc_assert (mask);
12385
12386   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12387     {
12388       /* Special case.  Do not generate a POP PC statement here, do it in
12389          thumb_exit() */
12390       thumb_exit (f, -1);
12391       return;
12392     }
12393
12394   fprintf (f, "\t%s\t{", push ? "push" : "pop");
12395
12396   /* Look at the low registers first.  */
12397   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12398     {
12399       if (lo_mask & 1)
12400         {
12401           asm_fprintf (f, "%r", regno);
12402
12403           if ((lo_mask & ~1) != 0)
12404             fprintf (f, ", ");
12405
12406           pushed_words++;
12407         }
12408     }
12409
12410   if (push && (mask & (1 << LR_REGNUM)))
12411     {
12412       /* Catch pushing the LR.  */
12413       if (mask & 0xFF)
12414         fprintf (f, ", ");
12415
12416       asm_fprintf (f, "%r", LR_REGNUM);
12417
12418       pushed_words++;
12419     }
12420   else if (!push && (mask & (1 << PC_REGNUM)))
12421     {
12422       /* Catch popping the PC.  */
12423       if (TARGET_INTERWORK || TARGET_BACKTRACE
12424           || current_function_calls_eh_return)
12425         {
12426           /* The PC is never poped directly, instead
12427              it is popped into r3 and then BX is used.  */
12428           fprintf (f, "}\n");
12429
12430           thumb_exit (f, -1);
12431
12432           return;
12433         }
12434       else
12435         {
12436           if (mask & 0xFF)
12437             fprintf (f, ", ");
12438
12439           asm_fprintf (f, "%r", PC_REGNUM);
12440         }
12441     }
12442
12443   fprintf (f, "}\n");
12444
12445   if (push && pushed_words && dwarf2out_do_frame ())
12446     {
12447       char *l = dwarf2out_cfi_label ();
12448       int pushed_mask = real_regs;
12449
12450       *cfa_offset += pushed_words * 4;
12451       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12452
12453       pushed_words = 0;
12454       pushed_mask = real_regs;
12455       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12456         {
12457           if (pushed_mask & 1)
12458             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12459         }
12460     }
12461 }
12462
12463 /* Generate code to return from a thumb function.
12464    If 'reg_containing_return_addr' is -1, then the return address is
12465    actually on the stack, at the stack pointer.  */
12466 static void
12467 thumb_exit (FILE *f, int reg_containing_return_addr)
12468 {
12469   unsigned regs_available_for_popping;
12470   unsigned regs_to_pop;
12471   int pops_needed;
12472   unsigned available;
12473   unsigned required;
12474   int mode;
12475   int size;
12476   int restore_a4 = FALSE;
12477
12478   /* Compute the registers we need to pop.  */
12479   regs_to_pop = 0;
12480   pops_needed = 0;
12481
12482   if (reg_containing_return_addr == -1)
12483     {
12484       regs_to_pop |= 1 << LR_REGNUM;
12485       ++pops_needed;
12486     }
12487
12488   if (TARGET_BACKTRACE)
12489     {
12490       /* Restore the (ARM) frame pointer and stack pointer.  */
12491       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12492       pops_needed += 2;
12493     }
12494
12495   /* If there is nothing to pop then just emit the BX instruction and
12496      return.  */
12497   if (pops_needed == 0)
12498     {
12499       if (current_function_calls_eh_return)
12500         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12501
12502       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12503       return;
12504     }
12505   /* Otherwise if we are not supporting interworking and we have not created
12506      a backtrace structure and the function was not entered in ARM mode then
12507      just pop the return address straight into the PC.  */
12508   else if (!TARGET_INTERWORK
12509            && !TARGET_BACKTRACE
12510            && !is_called_in_ARM_mode (current_function_decl)
12511            && !current_function_calls_eh_return)
12512     {
12513       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12514       return;
12515     }
12516
12517   /* Find out how many of the (return) argument registers we can corrupt.  */
12518   regs_available_for_popping = 0;
12519
12520   /* If returning via __builtin_eh_return, the bottom three registers
12521      all contain information needed for the return.  */
12522   if (current_function_calls_eh_return)
12523     size = 12;
12524   else
12525     {
12526       /* If we can deduce the registers used from the function's
12527          return value.  This is more reliable that examining
12528          regs_ever_live[] because that will be set if the register is
12529          ever used in the function, not just if the register is used
12530          to hold a return value.  */
12531
12532       if (current_function_return_rtx != 0)
12533         mode = GET_MODE (current_function_return_rtx);
12534       else
12535         mode = DECL_MODE (DECL_RESULT (current_function_decl));
12536
12537       size = GET_MODE_SIZE (mode);
12538
12539       if (size == 0)
12540         {
12541           /* In a void function we can use any argument register.
12542              In a function that returns a structure on the stack
12543              we can use the second and third argument registers.  */
12544           if (mode == VOIDmode)
12545             regs_available_for_popping =
12546               (1 << ARG_REGISTER (1))
12547               | (1 << ARG_REGISTER (2))
12548               | (1 << ARG_REGISTER (3));
12549           else
12550             regs_available_for_popping =
12551               (1 << ARG_REGISTER (2))
12552               | (1 << ARG_REGISTER (3));
12553         }
12554       else if (size <= 4)
12555         regs_available_for_popping =
12556           (1 << ARG_REGISTER (2))
12557           | (1 << ARG_REGISTER (3));
12558       else if (size <= 8)
12559         regs_available_for_popping =
12560           (1 << ARG_REGISTER (3));
12561     }
12562
12563   /* Match registers to be popped with registers into which we pop them.  */
12564   for (available = regs_available_for_popping,
12565        required  = regs_to_pop;
12566        required != 0 && available != 0;
12567        available &= ~(available & - available),
12568        required  &= ~(required  & - required))
12569     -- pops_needed;
12570
12571   /* If we have any popping registers left over, remove them.  */
12572   if (available > 0)
12573     regs_available_for_popping &= ~available;
12574
12575   /* Otherwise if we need another popping register we can use
12576      the fourth argument register.  */
12577   else if (pops_needed)
12578     {
12579       /* If we have not found any free argument registers and
12580          reg a4 contains the return address, we must move it.  */
12581       if (regs_available_for_popping == 0
12582           && reg_containing_return_addr == LAST_ARG_REGNUM)
12583         {
12584           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12585           reg_containing_return_addr = LR_REGNUM;
12586         }
12587       else if (size > 12)
12588         {
12589           /* Register a4 is being used to hold part of the return value,
12590              but we have dire need of a free, low register.  */
12591           restore_a4 = TRUE;
12592
12593           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12594         }
12595
12596       if (reg_containing_return_addr != LAST_ARG_REGNUM)
12597         {
12598           /* The fourth argument register is available.  */
12599           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12600
12601           --pops_needed;
12602         }
12603     }
12604
12605   /* Pop as many registers as we can.  */
12606   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12607                  regs_available_for_popping);
12608
12609   /* Process the registers we popped.  */
12610   if (reg_containing_return_addr == -1)
12611     {
12612       /* The return address was popped into the lowest numbered register.  */
12613       regs_to_pop &= ~(1 << LR_REGNUM);
12614
12615       reg_containing_return_addr =
12616         number_of_first_bit_set (regs_available_for_popping);
12617
12618       /* Remove this register for the mask of available registers, so that
12619          the return address will not be corrupted by further pops.  */
12620       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12621     }
12622
12623   /* If we popped other registers then handle them here.  */
12624   if (regs_available_for_popping)
12625     {
12626       int frame_pointer;
12627
12628       /* Work out which register currently contains the frame pointer.  */
12629       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12630
12631       /* Move it into the correct place.  */
12632       asm_fprintf (f, "\tmov\t%r, %r\n",
12633                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12634
12635       /* (Temporarily) remove it from the mask of popped registers.  */
12636       regs_available_for_popping &= ~(1 << frame_pointer);
12637       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12638
12639       if (regs_available_for_popping)
12640         {
12641           int stack_pointer;
12642
12643           /* We popped the stack pointer as well,
12644              find the register that contains it.  */
12645           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12646
12647           /* Move it into the stack register.  */
12648           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12649
12650           /* At this point we have popped all necessary registers, so
12651              do not worry about restoring regs_available_for_popping
12652              to its correct value:
12653
12654              assert (pops_needed == 0)
12655              assert (regs_available_for_popping == (1 << frame_pointer))
12656              assert (regs_to_pop == (1 << STACK_POINTER))  */
12657         }
12658       else
12659         {
12660           /* Since we have just move the popped value into the frame
12661              pointer, the popping register is available for reuse, and
12662              we know that we still have the stack pointer left to pop.  */
12663           regs_available_for_popping |= (1 << frame_pointer);
12664         }
12665     }
12666
12667   /* If we still have registers left on the stack, but we no longer have
12668      any registers into which we can pop them, then we must move the return
12669      address into the link register and make available the register that
12670      contained it.  */
12671   if (regs_available_for_popping == 0 && pops_needed > 0)
12672     {
12673       regs_available_for_popping |= 1 << reg_containing_return_addr;
12674
12675       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12676                    reg_containing_return_addr);
12677
12678       reg_containing_return_addr = LR_REGNUM;
12679     }
12680
12681   /* If we have registers left on the stack then pop some more.
12682      We know that at most we will want to pop FP and SP.  */
12683   if (pops_needed > 0)
12684     {
12685       int  popped_into;
12686       int  move_to;
12687
12688       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12689                      regs_available_for_popping);
12690
12691       /* We have popped either FP or SP.
12692          Move whichever one it is into the correct register.  */
12693       popped_into = number_of_first_bit_set (regs_available_for_popping);
12694       move_to     = number_of_first_bit_set (regs_to_pop);
12695
12696       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12697
12698       regs_to_pop &= ~(1 << move_to);
12699
12700       --pops_needed;
12701     }
12702
12703   /* If we still have not popped everything then we must have only
12704      had one register available to us and we are now popping the SP.  */
12705   if (pops_needed > 0)
12706     {
12707       int  popped_into;
12708
12709       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12710                      regs_available_for_popping);
12711
12712       popped_into = number_of_first_bit_set (regs_available_for_popping);
12713
12714       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12715       /*
12716         assert (regs_to_pop == (1 << STACK_POINTER))
12717         assert (pops_needed == 1)
12718       */
12719     }
12720
12721   /* If necessary restore the a4 register.  */
12722   if (restore_a4)
12723     {
12724       if (reg_containing_return_addr != LR_REGNUM)
12725         {
12726           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12727           reg_containing_return_addr = LR_REGNUM;
12728         }
12729
12730       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12731     }
12732
12733   if (current_function_calls_eh_return)
12734     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12735
12736   /* Return to caller.  */
12737   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12738 }
12739
12740 \f
12741 void
12742 thumb_final_prescan_insn (rtx insn)
12743 {
12744   if (flag_print_asm_name)
12745     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12746                  INSN_ADDRESSES (INSN_UID (insn)));
12747 }
12748
12749 int
12750 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12751 {
12752   unsigned HOST_WIDE_INT mask = 0xff;
12753   int i;
12754
12755   if (val == 0) /* XXX */
12756     return 0;
12757
12758   for (i = 0; i < 25; i++)
12759     if ((val & (mask << i)) == val)
12760       return 1;
12761
12762   return 0;
12763 }
12764
12765 /* Returns nonzero if the current function contains,
12766    or might contain a far jump.  */
12767 static int
12768 thumb_far_jump_used_p (void)
12769 {
12770   rtx insn;
12771
12772   /* This test is only important for leaf functions.  */
12773   /* assert (!leaf_function_p ()); */
12774
12775   /* If we have already decided that far jumps may be used,
12776      do not bother checking again, and always return true even if
12777      it turns out that they are not being used.  Once we have made
12778      the decision that far jumps are present (and that hence the link
12779      register will be pushed onto the stack) we cannot go back on it.  */
12780   if (cfun->machine->far_jump_used)
12781     return 1;
12782
12783   /* If this function is not being called from the prologue/epilogue
12784      generation code then it must be being called from the
12785      INITIAL_ELIMINATION_OFFSET macro.  */
12786   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12787     {
12788       /* In this case we know that we are being asked about the elimination
12789          of the arg pointer register.  If that register is not being used,
12790          then there are no arguments on the stack, and we do not have to
12791          worry that a far jump might force the prologue to push the link
12792          register, changing the stack offsets.  In this case we can just
12793          return false, since the presence of far jumps in the function will
12794          not affect stack offsets.
12795
12796          If the arg pointer is live (or if it was live, but has now been
12797          eliminated and so set to dead) then we do have to test to see if
12798          the function might contain a far jump.  This test can lead to some
12799          false negatives, since before reload is completed, then length of
12800          branch instructions is not known, so gcc defaults to returning their
12801          longest length, which in turn sets the far jump attribute to true.
12802
12803          A false negative will not result in bad code being generated, but it
12804          will result in a needless push and pop of the link register.  We
12805          hope that this does not occur too often.
12806
12807          If we need doubleword stack alignment this could affect the other
12808          elimination offsets so we can't risk getting it wrong.  */
12809       if (regs_ever_live [ARG_POINTER_REGNUM])
12810         cfun->machine->arg_pointer_live = 1;
12811       else if (!cfun->machine->arg_pointer_live)
12812         return 0;
12813     }
12814
12815   /* Check to see if the function contains a branch
12816      insn with the far jump attribute set.  */
12817   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12818     {
12819       if (GET_CODE (insn) == JUMP_INSN
12820           /* Ignore tablejump patterns.  */
12821           && GET_CODE (PATTERN (insn)) != ADDR_VEC
12822           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12823           && get_attr_far_jump (insn) == FAR_JUMP_YES
12824           )
12825         {
12826           /* Record the fact that we have decided that
12827              the function does use far jumps.  */
12828           cfun->machine->far_jump_used = 1;
12829           return 1;
12830         }
12831     }
12832
12833   return 0;
12834 }
12835
12836 /* Return nonzero if FUNC must be entered in ARM mode.  */
12837 int
12838 is_called_in_ARM_mode (tree func)
12839 {
12840   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
12841
12842   /* Ignore the problem about functions whose address is taken.  */
12843   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12844     return TRUE;
12845
12846 #ifdef ARM_PE
12847   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12848 #else
12849   return FALSE;
12850 #endif
12851 }
12852
12853 /* The bits which aren't usefully expanded as rtl.  */
12854 const char *
12855 thumb_unexpanded_epilogue (void)
12856 {
12857   int regno;
12858   unsigned long live_regs_mask = 0;
12859   int high_regs_pushed = 0;
12860   int had_to_push_lr;
12861   int size;
12862   int mode;
12863
12864   if (return_used_this_function)
12865     return "";
12866
12867   if (IS_NAKED (arm_current_func_type ()))
12868     return "";
12869
12870   live_regs_mask = thumb_compute_save_reg_mask ();
12871   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
12872
12873   /* If we can deduce the registers used from the function's return value.
12874      This is more reliable that examining regs_ever_live[] because that
12875      will be set if the register is ever used in the function, not just if
12876      the register is used to hold a return value.  */
12877
12878   if (current_function_return_rtx != 0)
12879     mode = GET_MODE (current_function_return_rtx);
12880   else
12881     mode = DECL_MODE (DECL_RESULT (current_function_decl));
12882
12883   size = GET_MODE_SIZE (mode);
12884
12885   /* The prolog may have pushed some high registers to use as
12886      work registers.  e.g. the testsuite file:
12887      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12888      compiles to produce:
12889         push    {r4, r5, r6, r7, lr}
12890         mov     r7, r9
12891         mov     r6, r8
12892         push    {r6, r7}
12893      as part of the prolog.  We have to undo that pushing here.  */
12894
12895   if (high_regs_pushed)
12896     {
12897       unsigned long mask = live_regs_mask & 0xff;
12898       int next_hi_reg;
12899
12900       /* The available low registers depend on the size of the value we are
12901          returning.  */
12902       if (size <= 12)
12903         mask |=  1 << 3;
12904       if (size <= 8)
12905         mask |= 1 << 2;
12906
12907       if (mask == 0)
12908         /* Oh dear!  We have no low registers into which we can pop
12909            high registers!  */
12910         internal_error
12911           ("no low registers available for popping high registers");
12912
12913       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12914         if (live_regs_mask & (1 << next_hi_reg))
12915           break;
12916
12917       while (high_regs_pushed)
12918         {
12919           /* Find lo register(s) into which the high register(s) can
12920              be popped.  */
12921           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12922             {
12923               if (mask & (1 << regno))
12924                 high_regs_pushed--;
12925               if (high_regs_pushed == 0)
12926                 break;
12927             }
12928
12929           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
12930
12931           /* Pop the values into the low register(s).  */
12932           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
12933
12934           /* Move the value(s) into the high registers.  */
12935           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12936             {
12937               if (mask & (1 << regno))
12938                 {
12939                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12940                                regno);
12941
12942                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
12943                     if (live_regs_mask & (1 << next_hi_reg))
12944                       break;
12945                 }
12946             }
12947         }
12948       live_regs_mask &= ~0x0f00;
12949     }
12950
12951   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
12952   live_regs_mask &= 0xff;
12953
12954   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12955     {
12956       /* Pop the return address into the PC.  */
12957       if (had_to_push_lr)
12958         live_regs_mask |= 1 << PC_REGNUM;
12959
12960       /* Either no argument registers were pushed or a backtrace
12961          structure was created which includes an adjusted stack
12962          pointer, so just pop everything.  */
12963       if (live_regs_mask)
12964         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12965                        live_regs_mask);
12966
12967       /* We have either just popped the return address into the
12968          PC or it is was kept in LR for the entire function.  */
12969       if (!had_to_push_lr)
12970         thumb_exit (asm_out_file, LR_REGNUM);
12971     }
12972   else
12973     {
12974       /* Pop everything but the return address.  */
12975       if (live_regs_mask)
12976         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12977                        live_regs_mask);
12978
12979       if (had_to_push_lr)
12980         {
12981           if (size > 12)
12982             {
12983               /* We have no free low regs, so save one.  */
12984               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
12985                            LAST_ARG_REGNUM);
12986             }
12987
12988           /* Get the return address into a temporary register.  */
12989           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
12990                          1 << LAST_ARG_REGNUM);
12991
12992           if (size > 12)
12993             {
12994               /* Move the return address to lr.  */
12995               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
12996                            LAST_ARG_REGNUM);
12997               /* Restore the low register.  */
12998               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
12999                            IP_REGNUM);
13000               regno = LR_REGNUM;
13001             }
13002           else
13003             regno = LAST_ARG_REGNUM;
13004         }
13005       else
13006         regno = LR_REGNUM;
13007
13008       /* Remove the argument registers that were pushed onto the stack.  */
13009       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13010                    SP_REGNUM, SP_REGNUM,
13011                    current_function_pretend_args_size);
13012
13013       thumb_exit (asm_out_file, regno);
13014     }
13015
13016   return "";
13017 }
13018
13019 /* Functions to save and restore machine-specific function data.  */
13020 static struct machine_function *
13021 arm_init_machine_status (void)
13022 {
13023   struct machine_function *machine;
13024   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13025
13026 #if ARM_FT_UNKNOWN != 0
13027   machine->func_type = ARM_FT_UNKNOWN;
13028 #endif
13029   return machine;
13030 }
13031
13032 /* Return an RTX indicating where the return address to the
13033    calling function can be found.  */
13034 rtx
13035 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13036 {
13037   if (count != 0)
13038     return NULL_RTX;
13039
13040   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13041 }
13042
13043 /* Do anything needed before RTL is emitted for each function.  */
13044 void
13045 arm_init_expanders (void)
13046 {
13047   /* Arrange to initialize and mark the machine per-function status.  */
13048   init_machine_status = arm_init_machine_status;
13049
13050   /* This is to stop the combine pass optimizing away the alignment
13051      adjustment of va_arg.  */
13052   /* ??? It is claimed that this should not be necessary.  */
13053   if (cfun)
13054     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
13055 }
13056
13057
13058 /* Like arm_compute_initial_elimination offset.  Simpler because
13059    THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer.  */
13060
13061 HOST_WIDE_INT
13062 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13063 {
13064   arm_stack_offsets *offsets;
13065
13066   offsets = arm_get_frame_offsets ();
13067
13068   switch (from)
13069     {
13070     case ARG_POINTER_REGNUM:
13071       switch (to)
13072         {
13073         case STACK_POINTER_REGNUM:
13074           return offsets->outgoing_args - offsets->saved_args;
13075
13076         case FRAME_POINTER_REGNUM:
13077           return offsets->soft_frame - offsets->saved_args;
13078
13079         case THUMB_HARD_FRAME_POINTER_REGNUM:
13080         case ARM_HARD_FRAME_POINTER_REGNUM:
13081           return offsets->saved_regs - offsets->saved_args;
13082
13083         default:
13084           gcc_unreachable ();
13085         }
13086       break;
13087
13088     case FRAME_POINTER_REGNUM:
13089       switch (to)
13090         {
13091         case STACK_POINTER_REGNUM:
13092           return offsets->outgoing_args - offsets->soft_frame;
13093
13094         case THUMB_HARD_FRAME_POINTER_REGNUM:
13095         case ARM_HARD_FRAME_POINTER_REGNUM:
13096           return offsets->saved_regs - offsets->soft_frame;
13097
13098         default:
13099           gcc_unreachable ();
13100         }
13101       break;
13102
13103     default:
13104       gcc_unreachable ();
13105     }
13106 }
13107
13108
13109 /* Generate the rest of a function's prologue.  */
13110 void
13111 thumb_expand_prologue (void)
13112 {
13113   rtx insn, dwarf;
13114
13115   HOST_WIDE_INT amount;
13116   arm_stack_offsets *offsets;
13117   unsigned long func_type;
13118   int regno;
13119   unsigned long live_regs_mask;
13120
13121   func_type = arm_current_func_type ();
13122
13123   /* Naked functions don't have prologues.  */
13124   if (IS_NAKED (func_type))
13125     return;
13126
13127   if (IS_INTERRUPT (func_type))
13128     {
13129       error ("interrupt Service Routines cannot be coded in Thumb mode");
13130       return;
13131     }
13132
13133   live_regs_mask = thumb_compute_save_reg_mask ();
13134   /* Load the pic register before setting the frame pointer,
13135      so we can use r7 as a temporary work register.  */
13136   if (flag_pic)
13137     arm_load_pic_register (thumb_find_work_register (live_regs_mask));
13138
13139   offsets = arm_get_frame_offsets ();
13140
13141   if (frame_pointer_needed)
13142     {
13143       insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
13144                                    stack_pointer_rtx));
13145       RTX_FRAME_RELATED_P (insn) = 1;
13146     }
13147   else if (CALLER_INTERWORKING_SLOT_SIZE > 0)
13148     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
13149                     stack_pointer_rtx);
13150
13151   amount = offsets->outgoing_args - offsets->saved_regs;
13152   if (amount)
13153     {
13154       if (amount < 512)
13155         {
13156           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13157                                         GEN_INT (- amount)));
13158           RTX_FRAME_RELATED_P (insn) = 1;
13159         }
13160       else
13161         {
13162           rtx reg;
13163
13164           /* The stack decrement is too big for an immediate value in a single
13165              insn.  In theory we could issue multiple subtracts, but after
13166              three of them it becomes more space efficient to place the full
13167              value in the constant pool and load into a register.  (Also the
13168              ARM debugger really likes to see only one stack decrement per
13169              function).  So instead we look for a scratch register into which
13170              we can load the decrement, and then we subtract this from the
13171              stack pointer.  Unfortunately on the thumb the only available
13172              scratch registers are the argument registers, and we cannot use
13173              these as they may hold arguments to the function.  Instead we
13174              attempt to locate a call preserved register which is used by this
13175              function.  If we can find one, then we know that it will have
13176              been pushed at the start of the prologue and so we can corrupt
13177              it now.  */
13178           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13179             if (live_regs_mask & (1 << regno)
13180                 && !(frame_pointer_needed
13181                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13182               break;
13183
13184           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
13185             {
13186               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13187
13188               /* Choose an arbitrary, non-argument low register.  */
13189               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13190
13191               /* Save it by copying it into a high, scratch register.  */
13192               emit_insn (gen_movsi (spare, reg));
13193               /* Add a USE to stop propagate_one_insn() from barfing.  */
13194               emit_insn (gen_prologue_use (spare));
13195
13196               /* Decrement the stack.  */
13197               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13198               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13199                                             stack_pointer_rtx, reg));
13200               RTX_FRAME_RELATED_P (insn) = 1;
13201               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13202                                    plus_constant (stack_pointer_rtx,
13203                                                   -amount));
13204               RTX_FRAME_RELATED_P (dwarf) = 1;
13205               REG_NOTES (insn)
13206                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13207                                      REG_NOTES (insn));
13208
13209               /* Restore the low register's original value.  */
13210               emit_insn (gen_movsi (reg, spare));
13211
13212               /* Emit a USE of the restored scratch register, so that flow
13213                  analysis will not consider the restore redundant.  The
13214                  register won't be used again in this function and isn't
13215                  restored by the epilogue.  */
13216               emit_insn (gen_prologue_use (reg));
13217             }
13218           else
13219             {
13220               reg = gen_rtx_REG (SImode, regno);
13221
13222               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13223
13224               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13225                                             stack_pointer_rtx, reg));
13226               RTX_FRAME_RELATED_P (insn) = 1;
13227               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13228                                    plus_constant (stack_pointer_rtx,
13229                                                   -amount));
13230               RTX_FRAME_RELATED_P (dwarf) = 1;
13231               REG_NOTES (insn)
13232                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13233                                      REG_NOTES (insn));
13234             }
13235         }
13236       /* If the frame pointer is needed, emit a special barrier that
13237          will prevent the scheduler from moving stores to the frame
13238          before the stack adjustment.  */
13239       if (frame_pointer_needed)
13240         emit_insn (gen_stack_tie (stack_pointer_rtx,
13241                                   hard_frame_pointer_rtx));
13242     }
13243
13244   if (current_function_profile || !TARGET_SCHED_PROLOG)
13245     emit_insn (gen_blockage ());
13246
13247   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13248   if (live_regs_mask & 0xff)
13249     cfun->machine->lr_save_eliminated = 0;
13250
13251   /* If the link register is being kept alive, with the return address in it,
13252      then make sure that it does not get reused by the ce2 pass.  */
13253   if (cfun->machine->lr_save_eliminated)
13254     emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13255 }
13256
13257
13258 void
13259 thumb_expand_epilogue (void)
13260 {
13261   HOST_WIDE_INT amount;
13262   arm_stack_offsets *offsets;
13263   int regno;
13264
13265   /* Naked functions don't have prologues.  */
13266   if (IS_NAKED (arm_current_func_type ()))
13267     return;
13268
13269   offsets = arm_get_frame_offsets ();
13270   amount = offsets->outgoing_args - offsets->saved_regs;
13271
13272   if (frame_pointer_needed)
13273     emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13274   else if (amount)
13275     {
13276       if (amount < 512)
13277         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13278                                GEN_INT (amount)));
13279       else
13280         {
13281           /* r3 is always free in the epilogue.  */
13282           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13283
13284           emit_insn (gen_movsi (reg, GEN_INT (amount)));
13285           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13286         }
13287     }
13288
13289   /* Emit a USE (stack_pointer_rtx), so that
13290      the stack adjustment will not be deleted.  */
13291   emit_insn (gen_prologue_use (stack_pointer_rtx));
13292
13293   if (current_function_profile || !TARGET_SCHED_PROLOG)
13294     emit_insn (gen_blockage ());
13295
13296   /* Emit a clobber for each insn that will be restored in the epilogue,
13297      so that flow2 will get register lifetimes correct.  */
13298   for (regno = 0; regno < 13; regno++)
13299     if (regs_ever_live[regno] && !call_used_regs[regno])
13300       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13301
13302   if (! regs_ever_live[LR_REGNUM])
13303     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13304 }
13305
13306 static void
13307 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13308 {
13309   unsigned long live_regs_mask = 0;
13310   unsigned long l_mask;
13311   unsigned high_regs_pushed = 0;
13312   int cfa_offset = 0;
13313   int regno;
13314
13315   if (IS_NAKED (arm_current_func_type ()))
13316     return;
13317
13318   if (is_called_in_ARM_mode (current_function_decl))
13319     {
13320       const char * name;
13321
13322       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
13323       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
13324                   == SYMBOL_REF);
13325       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
13326
13327       /* Generate code sequence to switch us into Thumb mode.  */
13328       /* The .code 32 directive has already been emitted by
13329          ASM_DECLARE_FUNCTION_NAME.  */
13330       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13331       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13332
13333       /* Generate a label, so that the debugger will notice the
13334          change in instruction sets.  This label is also used by
13335          the assembler to bypass the ARM code when this function
13336          is called from a Thumb encoded function elsewhere in the
13337          same file.  Hence the definition of STUB_NAME here must
13338          agree with the definition in gas/config/tc-arm.c.  */
13339
13340 #define STUB_NAME ".real_start_of"
13341
13342       fprintf (f, "\t.code\t16\n");
13343 #ifdef ARM_PE
13344       if (arm_dllexport_name_p (name))
13345         name = arm_strip_name_encoding (name);
13346 #endif
13347       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13348       fprintf (f, "\t.thumb_func\n");
13349       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13350     }
13351
13352   if (current_function_pretend_args_size)
13353     {
13354       if (cfun->machine->uses_anonymous_args)
13355         {
13356           int num_pushes;
13357
13358           fprintf (f, "\tpush\t{");
13359
13360           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13361
13362           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13363                regno <= LAST_ARG_REGNUM;
13364                regno++)
13365             asm_fprintf (f, "%r%s", regno,
13366                          regno == LAST_ARG_REGNUM ? "" : ", ");
13367
13368           fprintf (f, "}\n");
13369         }
13370       else
13371         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13372                      SP_REGNUM, SP_REGNUM,
13373                      current_function_pretend_args_size);
13374
13375       /* We don't need to record the stores for unwinding (would it
13376          help the debugger any if we did?), but record the change in
13377          the stack pointer.  */
13378       if (dwarf2out_do_frame ())
13379         {
13380           char *l = dwarf2out_cfi_label ();
13381
13382           cfa_offset = cfa_offset + current_function_pretend_args_size;
13383           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13384         }
13385     }
13386
13387   /* Get the registers we are going to push.  */
13388   live_regs_mask = thumb_compute_save_reg_mask ();
13389   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
13390   l_mask = live_regs_mask & 0x40ff;
13391   /* Then count how many other high registers will need to be pushed.  */
13392   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13393
13394   if (TARGET_BACKTRACE)
13395     {
13396       unsigned offset;
13397       unsigned work_register;
13398
13399       /* We have been asked to create a stack backtrace structure.
13400          The code looks like this:
13401
13402          0   .align 2
13403          0   func:
13404          0     sub   SP, #16         Reserve space for 4 registers.
13405          2     push  {R7}            Push low registers.
13406          4     add   R7, SP, #20     Get the stack pointer before the push.
13407          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
13408          8     mov   R7, PC          Get hold of the start of this code plus 12.
13409         10     str   R7, [SP, #16]   Store it.
13410         12     mov   R7, FP          Get hold of the current frame pointer.
13411         14     str   R7, [SP, #4]    Store it.
13412         16     mov   R7, LR          Get hold of the current return address.
13413         18     str   R7, [SP, #12]   Store it.
13414         20     add   R7, SP, #16     Point at the start of the backtrace structure.
13415         22     mov   FP, R7          Put this value into the frame pointer.  */
13416
13417       work_register = thumb_find_work_register (live_regs_mask);
13418
13419       asm_fprintf
13420         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13421          SP_REGNUM, SP_REGNUM);
13422
13423       if (dwarf2out_do_frame ())
13424         {
13425           char *l = dwarf2out_cfi_label ();
13426
13427           cfa_offset = cfa_offset + 16;
13428           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13429         }
13430
13431       if (l_mask)
13432         {
13433           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13434           offset = bit_count (l_mask);
13435         }
13436       else
13437         offset = 0;
13438
13439       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13440                    offset + 16 + current_function_pretend_args_size);
13441
13442       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13443                    offset + 4);
13444
13445       /* Make sure that the instruction fetching the PC is in the right place
13446          to calculate "start of backtrace creation code + 12".  */
13447       if (l_mask)
13448         {
13449           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13450           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13451                        offset + 12);
13452           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13453                        ARM_HARD_FRAME_POINTER_REGNUM);
13454           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13455                        offset);
13456         }
13457       else
13458         {
13459           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13460                        ARM_HARD_FRAME_POINTER_REGNUM);
13461           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13462                        offset);
13463           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13464           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13465                        offset + 12);
13466         }
13467
13468       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13469       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13470                    offset + 8);
13471       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13472                    offset + 12);
13473       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13474                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13475     }
13476   /* Optimisation:  If we are not pushing any low registers but we are going
13477      to push some high registers then delay our first push.  This will just
13478      be a push of LR and we can combine it with the push of the first high
13479      register.  */
13480   else if ((l_mask & 0xff) != 0
13481            || (high_regs_pushed == 0 && l_mask))
13482     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13483
13484   if (high_regs_pushed)
13485     {
13486       unsigned pushable_regs;
13487       unsigned next_hi_reg;
13488
13489       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13490         if (live_regs_mask & (1 << next_hi_reg))
13491           break;
13492
13493       pushable_regs = l_mask & 0xff;
13494
13495       if (pushable_regs == 0)
13496         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
13497
13498       while (high_regs_pushed > 0)
13499         {
13500           unsigned long real_regs_mask = 0;
13501
13502           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
13503             {
13504               if (pushable_regs & (1 << regno))
13505                 {
13506                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13507
13508                   high_regs_pushed --;
13509                   real_regs_mask |= (1 << next_hi_reg);
13510
13511                   if (high_regs_pushed)
13512                     {
13513                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
13514                            next_hi_reg --)
13515                         if (live_regs_mask & (1 << next_hi_reg))
13516                           break;
13517                     }
13518                   else
13519                     {
13520                       pushable_regs &= ~((1 << regno) - 1);
13521                       break;
13522                     }
13523                 }
13524             }
13525
13526           /* If we had to find a work register and we have not yet
13527              saved the LR then add it to the list of regs to push.  */
13528           if (l_mask == (1 << LR_REGNUM))
13529             {
13530               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
13531                              1, &cfa_offset,
13532                              real_regs_mask | (1 << LR_REGNUM));
13533               l_mask = 0;
13534             }
13535           else
13536             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
13537         }
13538     }
13539 }
13540
13541 /* Handle the case of a double word load into a low register from
13542    a computed memory address.  The computed address may involve a
13543    register which is overwritten by the load.  */
13544 const char *
13545 thumb_load_double_from_address (rtx *operands)
13546 {
13547   rtx addr;
13548   rtx base;
13549   rtx offset;
13550   rtx arg1;
13551   rtx arg2;
13552
13553   gcc_assert (GET_CODE (operands[0]) == REG);
13554   gcc_assert (GET_CODE (operands[1]) == MEM);
13555
13556   /* Get the memory address.  */
13557   addr = XEXP (operands[1], 0);
13558
13559   /* Work out how the memory address is computed.  */
13560   switch (GET_CODE (addr))
13561     {
13562     case REG:
13563       operands[2] = gen_rtx_MEM (SImode,
13564                                  plus_constant (XEXP (operands[1], 0), 4));
13565
13566       if (REGNO (operands[0]) == REGNO (addr))
13567         {
13568           output_asm_insn ("ldr\t%H0, %2", operands);
13569           output_asm_insn ("ldr\t%0, %1", operands);
13570         }
13571       else
13572         {
13573           output_asm_insn ("ldr\t%0, %1", operands);
13574           output_asm_insn ("ldr\t%H0, %2", operands);
13575         }
13576       break;
13577
13578     case CONST:
13579       /* Compute <address> + 4 for the high order load.  */
13580       operands[2] = gen_rtx_MEM (SImode,
13581                                  plus_constant (XEXP (operands[1], 0), 4));
13582
13583       output_asm_insn ("ldr\t%0, %1", operands);
13584       output_asm_insn ("ldr\t%H0, %2", operands);
13585       break;
13586
13587     case PLUS:
13588       arg1   = XEXP (addr, 0);
13589       arg2   = XEXP (addr, 1);
13590
13591       if (CONSTANT_P (arg1))
13592         base = arg2, offset = arg1;
13593       else
13594         base = arg1, offset = arg2;
13595
13596       gcc_assert (GET_CODE (base) == REG);
13597
13598       /* Catch the case of <address> = <reg> + <reg> */
13599       if (GET_CODE (offset) == REG)
13600         {
13601           int reg_offset = REGNO (offset);
13602           int reg_base   = REGNO (base);
13603           int reg_dest   = REGNO (operands[0]);
13604
13605           /* Add the base and offset registers together into the
13606              higher destination register.  */
13607           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13608                        reg_dest + 1, reg_base, reg_offset);
13609
13610           /* Load the lower destination register from the address in
13611              the higher destination register.  */
13612           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13613                        reg_dest, reg_dest + 1);
13614
13615           /* Load the higher destination register from its own address
13616              plus 4.  */
13617           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13618                        reg_dest + 1, reg_dest + 1);
13619         }
13620       else
13621         {
13622           /* Compute <address> + 4 for the high order load.  */
13623           operands[2] = gen_rtx_MEM (SImode,
13624                                      plus_constant (XEXP (operands[1], 0), 4));
13625
13626           /* If the computed address is held in the low order register
13627              then load the high order register first, otherwise always
13628              load the low order register first.  */
13629           if (REGNO (operands[0]) == REGNO (base))
13630             {
13631               output_asm_insn ("ldr\t%H0, %2", operands);
13632               output_asm_insn ("ldr\t%0, %1", operands);
13633             }
13634           else
13635             {
13636               output_asm_insn ("ldr\t%0, %1", operands);
13637               output_asm_insn ("ldr\t%H0, %2", operands);
13638             }
13639         }
13640       break;
13641
13642     case LABEL_REF:
13643       /* With no registers to worry about we can just load the value
13644          directly.  */
13645       operands[2] = gen_rtx_MEM (SImode,
13646                                  plus_constant (XEXP (operands[1], 0), 4));
13647
13648       output_asm_insn ("ldr\t%H0, %2", operands);
13649       output_asm_insn ("ldr\t%0, %1", operands);
13650       break;
13651
13652     default:
13653       gcc_unreachable ();
13654     }
13655
13656   return "";
13657 }
13658
13659 const char *
13660 thumb_output_move_mem_multiple (int n, rtx *operands)
13661 {
13662   rtx tmp;
13663
13664   switch (n)
13665     {
13666     case 2:
13667       if (REGNO (operands[4]) > REGNO (operands[5]))
13668         {
13669           tmp = operands[4];
13670           operands[4] = operands[5];
13671           operands[5] = tmp;
13672         }
13673       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13674       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13675       break;
13676
13677     case 3:
13678       if (REGNO (operands[4]) > REGNO (operands[5]))
13679         {
13680           tmp = operands[4];
13681           operands[4] = operands[5];
13682           operands[5] = tmp;
13683         }
13684       if (REGNO (operands[5]) > REGNO (operands[6]))
13685         {
13686           tmp = operands[5];
13687           operands[5] = operands[6];
13688           operands[6] = tmp;
13689         }
13690       if (REGNO (operands[4]) > REGNO (operands[5]))
13691         {
13692           tmp = operands[4];
13693           operands[4] = operands[5];
13694           operands[5] = tmp;
13695         }
13696
13697       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13698       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13699       break;
13700
13701     default:
13702       gcc_unreachable ();
13703     }
13704
13705   return "";
13706 }
13707
13708 /* Output a call-via instruction for thumb state.  */
13709 const char *
13710 thumb_call_via_reg (rtx reg)
13711 {
13712   int regno = REGNO (reg);
13713   rtx *labelp;
13714
13715   gcc_assert (regno < LR_REGNUM);
13716
13717   /* If we are in the normal text section we can use a single instance
13718      per compilation unit.  If we are doing function sections, then we need
13719      an entry per section, since we can't rely on reachability.  */
13720   if (in_text_section ())
13721     {
13722       thumb_call_reg_needed = 1;
13723
13724       if (thumb_call_via_label[regno] == NULL)
13725         thumb_call_via_label[regno] = gen_label_rtx ();
13726       labelp = thumb_call_via_label + regno;
13727     }
13728   else
13729     {
13730       if (cfun->machine->call_via[regno] == NULL)
13731         cfun->machine->call_via[regno] = gen_label_rtx ();
13732       labelp = cfun->machine->call_via + regno;
13733     }
13734
13735   output_asm_insn ("bl\t%a0", labelp);
13736   return "";
13737 }
13738
13739 /* Routines for generating rtl.  */
13740 void
13741 thumb_expand_movmemqi (rtx *operands)
13742 {
13743   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13744   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13745   HOST_WIDE_INT len = INTVAL (operands[2]);
13746   HOST_WIDE_INT offset = 0;
13747
13748   while (len >= 12)
13749     {
13750       emit_insn (gen_movmem12b (out, in, out, in));
13751       len -= 12;
13752     }
13753
13754   if (len >= 8)
13755     {
13756       emit_insn (gen_movmem8b (out, in, out, in));
13757       len -= 8;
13758     }
13759
13760   if (len >= 4)
13761     {
13762       rtx reg = gen_reg_rtx (SImode);
13763       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13764       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13765       len -= 4;
13766       offset += 4;
13767     }
13768
13769   if (len >= 2)
13770     {
13771       rtx reg = gen_reg_rtx (HImode);
13772       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13773                                               plus_constant (in, offset))));
13774       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13775                             reg));
13776       len -= 2;
13777       offset += 2;
13778     }
13779
13780   if (len)
13781     {
13782       rtx reg = gen_reg_rtx (QImode);
13783       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13784                                               plus_constant (in, offset))));
13785       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13786                             reg));
13787     }
13788 }
13789
13790 void
13791 thumb_reload_out_hi (rtx *operands)
13792 {
13793   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13794 }
13795
13796 /* Handle reading a half-word from memory during reload.  */
13797 void
13798 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13799 {
13800   gcc_unreachable ();
13801 }
13802
13803 /* Return the length of a function name prefix
13804     that starts with the character 'c'.  */
13805 static int
13806 arm_get_strip_length (int c)
13807 {
13808   switch (c)
13809     {
13810     ARM_NAME_ENCODING_LENGTHS
13811       default: return 0;
13812     }
13813 }
13814
13815 /* Return a pointer to a function's name with any
13816    and all prefix encodings stripped from it.  */
13817 const char *
13818 arm_strip_name_encoding (const char *name)
13819 {
13820   int skip;
13821
13822   while ((skip = arm_get_strip_length (* name)))
13823     name += skip;
13824
13825   return name;
13826 }
13827
13828 /* If there is a '*' anywhere in the name's prefix, then
13829    emit the stripped name verbatim, otherwise prepend an
13830    underscore if leading underscores are being used.  */
13831 void
13832 arm_asm_output_labelref (FILE *stream, const char *name)
13833 {
13834   int skip;
13835   int verbatim = 0;
13836
13837   while ((skip = arm_get_strip_length (* name)))
13838     {
13839       verbatim |= (*name == '*');
13840       name += skip;
13841     }
13842
13843   if (verbatim)
13844     fputs (name, stream);
13845   else
13846     asm_fprintf (stream, "%U%s", name);
13847 }
13848
13849 static void
13850 arm_file_end (void)
13851 {
13852   int regno;
13853
13854   if (! thumb_call_reg_needed)
13855     return;
13856
13857   text_section ();
13858   asm_fprintf (asm_out_file, "\t.code 16\n");
13859   ASM_OUTPUT_ALIGN (asm_out_file, 1);
13860
13861   for (regno = 0; regno < LR_REGNUM; regno++)
13862     {
13863       rtx label = thumb_call_via_label[regno];
13864
13865       if (label != 0)
13866         {
13867           targetm.asm_out.internal_label (asm_out_file, "L",
13868                                           CODE_LABEL_NUMBER (label));
13869           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
13870         }
13871     }
13872 }
13873
13874 rtx aof_pic_label;
13875
13876 #ifdef AOF_ASSEMBLER
13877 /* Special functions only needed when producing AOF syntax assembler.  */
13878
13879 struct pic_chain
13880 {
13881   struct pic_chain * next;
13882   const char * symname;
13883 };
13884
13885 static struct pic_chain * aof_pic_chain = NULL;
13886
13887 rtx
13888 aof_pic_entry (rtx x)
13889 {
13890   struct pic_chain ** chainp;
13891   int offset;
13892
13893   if (aof_pic_label == NULL_RTX)
13894     {
13895       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13896     }
13897
13898   for (offset = 0, chainp = &aof_pic_chain; *chainp;
13899        offset += 4, chainp = &(*chainp)->next)
13900     if ((*chainp)->symname == XSTR (x, 0))
13901       return plus_constant (aof_pic_label, offset);
13902
13903   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13904   (*chainp)->next = NULL;
13905   (*chainp)->symname = XSTR (x, 0);
13906   return plus_constant (aof_pic_label, offset);
13907 }
13908
13909 void
13910 aof_dump_pic_table (FILE *f)
13911 {
13912   struct pic_chain * chain;
13913
13914   if (aof_pic_chain == NULL)
13915     return;
13916
13917   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13918                PIC_OFFSET_TABLE_REGNUM,
13919                PIC_OFFSET_TABLE_REGNUM);
13920   fputs ("|x$adcons|\n", f);
13921
13922   for (chain = aof_pic_chain; chain; chain = chain->next)
13923     {
13924       fputs ("\tDCD\t", f);
13925       assemble_name (f, chain->symname);
13926       fputs ("\n", f);
13927     }
13928 }
13929
13930 int arm_text_section_count = 1;
13931
13932 char *
13933 aof_text_section (void )
13934 {
13935   static char buf[100];
13936   sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13937            arm_text_section_count++);
13938   if (flag_pic)
13939     strcat (buf, ", PIC, REENTRANT");
13940   return buf;
13941 }
13942
13943 static int arm_data_section_count = 1;
13944
13945 char *
13946 aof_data_section (void)
13947 {
13948   static char buf[100];
13949   sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13950   return buf;
13951 }
13952
13953 /* The AOF assembler is religiously strict about declarations of
13954    imported and exported symbols, so that it is impossible to declare
13955    a function as imported near the beginning of the file, and then to
13956    export it later on.  It is, however, possible to delay the decision
13957    until all the functions in the file have been compiled.  To get
13958    around this, we maintain a list of the imports and exports, and
13959    delete from it any that are subsequently defined.  At the end of
13960    compilation we spit the remainder of the list out before the END
13961    directive.  */
13962
13963 struct import
13964 {
13965   struct import * next;
13966   const char * name;
13967 };
13968
13969 static struct import * imports_list = NULL;
13970
13971 void
13972 aof_add_import (const char *name)
13973 {
13974   struct import * new;
13975
13976   for (new = imports_list; new; new = new->next)
13977     if (new->name == name)
13978       return;
13979
13980   new = (struct import *) xmalloc (sizeof (struct import));
13981   new->next = imports_list;
13982   imports_list = new;
13983   new->name = name;
13984 }
13985
13986 void
13987 aof_delete_import (const char *name)
13988 {
13989   struct import ** old;
13990
13991   for (old = &imports_list; *old; old = & (*old)->next)
13992     {
13993       if ((*old)->name == name)
13994         {
13995           *old = (*old)->next;
13996           return;
13997         }
13998     }
13999 }
14000
14001 int arm_main_function = 0;
14002
14003 static void
14004 aof_dump_imports (FILE *f)
14005 {
14006   /* The AOF assembler needs this to cause the startup code to be extracted
14007      from the library.  Brining in __main causes the whole thing to work
14008      automagically.  */
14009   if (arm_main_function)
14010     {
14011       text_section ();
14012       fputs ("\tIMPORT __main\n", f);
14013       fputs ("\tDCD __main\n", f);
14014     }
14015
14016   /* Now dump the remaining imports.  */
14017   while (imports_list)
14018     {
14019       fprintf (f, "\tIMPORT\t");
14020       assemble_name (f, imports_list->name);
14021       fputc ('\n', f);
14022       imports_list = imports_list->next;
14023     }
14024 }
14025
14026 static void
14027 aof_globalize_label (FILE *stream, const char *name)
14028 {
14029   default_globalize_label (stream, name);
14030   if (! strcmp (name, "main"))
14031     arm_main_function = 1;
14032 }
14033
14034 static void
14035 aof_file_start (void)
14036 {
14037   fputs ("__r0\tRN\t0\n", asm_out_file);
14038   fputs ("__a1\tRN\t0\n", asm_out_file);
14039   fputs ("__a2\tRN\t1\n", asm_out_file);
14040   fputs ("__a3\tRN\t2\n", asm_out_file);
14041   fputs ("__a4\tRN\t3\n", asm_out_file);
14042   fputs ("__v1\tRN\t4\n", asm_out_file);
14043   fputs ("__v2\tRN\t5\n", asm_out_file);
14044   fputs ("__v3\tRN\t6\n", asm_out_file);
14045   fputs ("__v4\tRN\t7\n", asm_out_file);
14046   fputs ("__v5\tRN\t8\n", asm_out_file);
14047   fputs ("__v6\tRN\t9\n", asm_out_file);
14048   fputs ("__sl\tRN\t10\n", asm_out_file);
14049   fputs ("__fp\tRN\t11\n", asm_out_file);
14050   fputs ("__ip\tRN\t12\n", asm_out_file);
14051   fputs ("__sp\tRN\t13\n", asm_out_file);
14052   fputs ("__lr\tRN\t14\n", asm_out_file);
14053   fputs ("__pc\tRN\t15\n", asm_out_file);
14054   fputs ("__f0\tFN\t0\n", asm_out_file);
14055   fputs ("__f1\tFN\t1\n", asm_out_file);
14056   fputs ("__f2\tFN\t2\n", asm_out_file);
14057   fputs ("__f3\tFN\t3\n", asm_out_file);
14058   fputs ("__f4\tFN\t4\n", asm_out_file);
14059   fputs ("__f5\tFN\t5\n", asm_out_file);
14060   fputs ("__f6\tFN\t6\n", asm_out_file);
14061   fputs ("__f7\tFN\t7\n", asm_out_file);
14062   text_section ();
14063 }
14064
14065 static void
14066 aof_file_end (void)
14067 {
14068   if (flag_pic)
14069     aof_dump_pic_table (asm_out_file);
14070   arm_file_end ();
14071   aof_dump_imports (asm_out_file);
14072   fputs ("\tEND\n", asm_out_file);
14073 }
14074 #endif /* AOF_ASSEMBLER */
14075
14076 #ifndef ARM_PE
14077 /* Symbols in the text segment can be accessed without indirecting via the
14078    constant pool; it may take an extra binary operation, but this is still
14079    faster than indirecting via memory.  Don't do this when not optimizing,
14080    since we won't be calculating al of the offsets necessary to do this
14081    simplification.  */
14082
14083 static void
14084 arm_encode_section_info (tree decl, rtx rtl, int first)
14085 {
14086   /* This doesn't work with AOF syntax, since the string table may be in
14087      a different AREA.  */
14088 #ifndef AOF_ASSEMBLER
14089   if (optimize > 0 && TREE_CONSTANT (decl))
14090     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14091 #endif
14092
14093   /* If we are referencing a function that is weak then encode a long call
14094      flag in the function name, otherwise if the function is static or
14095      or known to be defined in this file then encode a short call flag.  */
14096   if (first && DECL_P (decl))
14097     {
14098       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14099         arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14100       else if (! TREE_PUBLIC (decl))
14101         arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14102     }
14103 }
14104 #endif /* !ARM_PE */
14105
14106 static void
14107 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14108 {
14109   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14110       && !strcmp (prefix, "L"))
14111     {
14112       arm_ccfsm_state = 0;
14113       arm_target_insn = NULL;
14114     }
14115   default_internal_label (stream, prefix, labelno);
14116 }
14117
14118 /* Output code to add DELTA to the first argument, and then jump
14119    to FUNCTION.  Used for C++ multiple inheritance.  */
14120 static void
14121 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14122                      HOST_WIDE_INT delta,
14123                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14124                      tree function)
14125 {
14126   static int thunk_label = 0;
14127   char label[256];
14128   int mi_delta = delta;
14129   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14130   int shift = 0;
14131   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14132                     ? 1 : 0);
14133   if (mi_delta < 0)
14134     mi_delta = - mi_delta;
14135   if (TARGET_THUMB)
14136     {
14137       int labelno = thunk_label++;
14138       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14139       fputs ("\tldr\tr12, ", file);
14140       assemble_name (file, label);
14141       fputc ('\n', file);
14142     }
14143   while (mi_delta != 0)
14144     {
14145       if ((mi_delta & (3 << shift)) == 0)
14146         shift += 2;
14147       else
14148         {
14149           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14150                        mi_op, this_regno, this_regno,
14151                        mi_delta & (0xff << shift));
14152           mi_delta &= ~(0xff << shift);
14153           shift += 8;
14154         }
14155     }
14156   if (TARGET_THUMB)
14157     {
14158       fprintf (file, "\tbx\tr12\n");
14159       ASM_OUTPUT_ALIGN (file, 2);
14160       assemble_name (file, label);
14161       fputs (":\n", file);
14162       assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14163     }
14164   else
14165     {
14166       fputs ("\tb\t", file);
14167       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14168       if (NEED_PLT_RELOC)
14169         fputs ("(PLT)", file);
14170       fputc ('\n', file);
14171     }
14172 }
14173
14174 int
14175 arm_emit_vector_const (FILE *file, rtx x)
14176 {
14177   int i;
14178   const char * pattern;
14179
14180   gcc_assert (GET_CODE (x) == CONST_VECTOR);
14181
14182   switch (GET_MODE (x))
14183     {
14184     case V2SImode: pattern = "%08x"; break;
14185     case V4HImode: pattern = "%04x"; break;
14186     case V8QImode: pattern = "%02x"; break;
14187     default:       gcc_unreachable ();
14188     }
14189
14190   fprintf (file, "0x");
14191   for (i = CONST_VECTOR_NUNITS (x); i--;)
14192     {
14193       rtx element;
14194
14195       element = CONST_VECTOR_ELT (x, i);
14196       fprintf (file, pattern, INTVAL (element));
14197     }
14198
14199   return 1;
14200 }
14201
14202 const char *
14203 arm_output_load_gr (rtx *operands)
14204 {
14205   rtx reg;
14206   rtx offset;
14207   rtx wcgr;
14208   rtx sum;
14209
14210   if (GET_CODE (operands [1]) != MEM
14211       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14212       || GET_CODE (reg = XEXP (sum, 0)) != REG
14213       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14214       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14215     return "wldrw%?\t%0, %1";
14216
14217   /* Fix up an out-of-range load of a GR register.  */
14218   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14219   wcgr = operands[0];
14220   operands[0] = reg;
14221   output_asm_insn ("ldr%?\t%0, %1", operands);
14222
14223   operands[0] = wcgr;
14224   operands[1] = reg;
14225   output_asm_insn ("tmcr%?\t%0, %1", operands);
14226   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14227
14228   return "";
14229 }
14230
14231 static rtx
14232 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14233                       int incoming ATTRIBUTE_UNUSED)
14234 {
14235 #if 0
14236   /* FIXME: The ARM backend has special code to handle structure
14237          returns, and will reserve its own hidden first argument.  So
14238          if this macro is enabled a *second* hidden argument will be
14239          reserved, which will break binary compatibility with old
14240          toolchains and also thunk handling.  One day this should be
14241          fixed.  */
14242   return 0;
14243 #else
14244   /* Register in which address to store a structure value
14245      is passed to a function.  */
14246   return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14247 #endif
14248 }
14249
14250 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14251
14252    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14253    named arg and all anonymous args onto the stack.
14254    XXX I know the prologue shouldn't be pushing registers, but it is faster
14255    that way.  */
14256
14257 static void
14258 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14259                             enum machine_mode mode ATTRIBUTE_UNUSED,
14260                             tree type ATTRIBUTE_UNUSED,
14261                             int *pretend_size,
14262                             int second_time ATTRIBUTE_UNUSED)
14263 {
14264   cfun->machine->uses_anonymous_args = 1;
14265   if (cum->nregs < NUM_ARG_REGS)
14266     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14267 }
14268
14269 /* Return nonzero if the CONSUMER instruction (a store) does not need
14270    PRODUCER's value to calculate the address.  */
14271
14272 int
14273 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14274 {
14275   rtx value = PATTERN (producer);
14276   rtx addr = PATTERN (consumer);
14277
14278   if (GET_CODE (value) == COND_EXEC)
14279     value = COND_EXEC_CODE (value);
14280   if (GET_CODE (value) == PARALLEL)
14281     value = XVECEXP (value, 0, 0);
14282   value = XEXP (value, 0);
14283   if (GET_CODE (addr) == COND_EXEC)
14284     addr = COND_EXEC_CODE (addr);
14285   if (GET_CODE (addr) == PARALLEL)
14286     addr = XVECEXP (addr, 0, 0);
14287   addr = XEXP (addr, 0);
14288
14289   return !reg_overlap_mentioned_p (value, addr);
14290 }
14291
14292 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14293    have an early register shift value or amount dependency on the
14294    result of PRODUCER.  */
14295
14296 int
14297 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14298 {
14299   rtx value = PATTERN (producer);
14300   rtx op = PATTERN (consumer);
14301   rtx early_op;
14302
14303   if (GET_CODE (value) == COND_EXEC)
14304     value = COND_EXEC_CODE (value);
14305   if (GET_CODE (value) == PARALLEL)
14306     value = XVECEXP (value, 0, 0);
14307   value = XEXP (value, 0);
14308   if (GET_CODE (op) == COND_EXEC)
14309     op = COND_EXEC_CODE (op);
14310   if (GET_CODE (op) == PARALLEL)
14311     op = XVECEXP (op, 0, 0);
14312   op = XEXP (op, 1);
14313
14314   early_op = XEXP (op, 0);
14315   /* This is either an actual independent shift, or a shift applied to
14316      the first operand of another operation.  We want the whole shift
14317      operation.  */
14318   if (GET_CODE (early_op) == REG)
14319     early_op = op;
14320
14321   return !reg_overlap_mentioned_p (value, early_op);
14322 }
14323
14324 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14325    have an early register shift value dependency on the result of
14326    PRODUCER.  */
14327
14328 int
14329 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14330 {
14331   rtx value = PATTERN (producer);
14332   rtx op = PATTERN (consumer);
14333   rtx early_op;
14334
14335   if (GET_CODE (value) == COND_EXEC)
14336     value = COND_EXEC_CODE (value);
14337   if (GET_CODE (value) == PARALLEL)
14338     value = XVECEXP (value, 0, 0);
14339   value = XEXP (value, 0);
14340   if (GET_CODE (op) == COND_EXEC)
14341     op = COND_EXEC_CODE (op);
14342   if (GET_CODE (op) == PARALLEL)
14343     op = XVECEXP (op, 0, 0);
14344   op = XEXP (op, 1);
14345
14346   early_op = XEXP (op, 0);
14347
14348   /* This is either an actual independent shift, or a shift applied to
14349      the first operand of another operation.  We want the value being
14350      shifted, in either case.  */
14351   if (GET_CODE (early_op) != REG)
14352     early_op = XEXP (early_op, 0);
14353
14354   return !reg_overlap_mentioned_p (value, early_op);
14355 }
14356
14357 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14358    have an early register mult dependency on the result of
14359    PRODUCER.  */
14360
14361 int
14362 arm_no_early_mul_dep (rtx producer, rtx consumer)
14363 {
14364   rtx value = PATTERN (producer);
14365   rtx op = PATTERN (consumer);
14366
14367   if (GET_CODE (value) == COND_EXEC)
14368     value = COND_EXEC_CODE (value);
14369   if (GET_CODE (value) == PARALLEL)
14370     value = XVECEXP (value, 0, 0);
14371   value = XEXP (value, 0);
14372   if (GET_CODE (op) == COND_EXEC)
14373     op = COND_EXEC_CODE (op);
14374   if (GET_CODE (op) == PARALLEL)
14375     op = XVECEXP (op, 0, 0);
14376   op = XEXP (op, 1);
14377
14378   return (GET_CODE (op) == PLUS
14379           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14380 }
14381
14382
14383 /* We can't rely on the caller doing the proper promotion when
14384    using APCS or ATPCS.  */
14385
14386 static bool
14387 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14388 {
14389     return !TARGET_AAPCS_BASED;
14390 }
14391
14392
14393 /* AAPCS based ABIs use short enums by default.  */
14394
14395 static bool
14396 arm_default_short_enums (void)
14397 {
14398   return TARGET_AAPCS_BASED;
14399 }
14400
14401
14402 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
14403
14404 static bool
14405 arm_align_anon_bitfield (void)
14406 {
14407   return TARGET_AAPCS_BASED;
14408 }
14409
14410
14411 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
14412
14413 static tree
14414 arm_cxx_guard_type (void)
14415 {
14416   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14417 }
14418
14419
14420 /* The EABI says test the least significan bit of a guard variable.  */
14421
14422 static bool
14423 arm_cxx_guard_mask_bit (void)
14424 {
14425   return TARGET_AAPCS_BASED;
14426 }
14427
14428
14429 /* The EABI specifies that all array cookies are 8 bytes long.  */
14430
14431 static tree
14432 arm_get_cookie_size (tree type)
14433 {
14434   tree size;
14435
14436   if (!TARGET_AAPCS_BASED)
14437     return default_cxx_get_cookie_size (type);
14438
14439   size = build_int_cst (sizetype, 8);
14440   return size;
14441 }
14442
14443
14444 /* The EABI says that array cookies should also contain the element size.  */
14445
14446 static bool
14447 arm_cookie_has_size (void)
14448 {
14449   return TARGET_AAPCS_BASED;
14450 }
14451
14452
14453 /* The EABI says constructors and destructors should return a pointer to
14454    the object constructed/destroyed.  */
14455
14456 static bool
14457 arm_cxx_cdtor_returns_this (void)
14458 {
14459   return TARGET_AAPCS_BASED;
14460 }
14461
14462 /* The EABI says that an inline function may never be the key
14463    method.  */
14464
14465 static bool
14466 arm_cxx_key_method_may_be_inline (void)
14467 {
14468   return !TARGET_AAPCS_BASED;
14469 }
14470
14471 static void
14472 arm_cxx_determine_class_data_visibility (tree decl)
14473 {
14474   if (!TARGET_AAPCS_BASED)
14475     return;
14476
14477   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
14478      is exported.  However, on systems without dynamic vague linkage,
14479      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
14480   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
14481     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
14482   else
14483     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
14484   DECL_VISIBILITY_SPECIFIED (decl) = 1;
14485 }
14486   
14487 static bool
14488 arm_cxx_class_data_always_comdat (void)
14489 {
14490   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
14491      vague linkage if the class has no key function.  */
14492   return !TARGET_AAPCS_BASED;
14493 }
14494
14495
14496 /* The EABI says __aeabi_atexit should be used to register static
14497    destructors.  */
14498
14499 static bool
14500 arm_cxx_use_aeabi_atexit (void)
14501 {
14502   return TARGET_AAPCS_BASED;
14503 }
14504
14505
14506 void
14507 arm_set_return_address (rtx source, rtx scratch)
14508 {
14509   arm_stack_offsets *offsets;
14510   HOST_WIDE_INT delta;
14511   rtx addr;
14512   unsigned long saved_regs;
14513
14514   saved_regs = arm_compute_save_reg_mask ();
14515
14516   if ((saved_regs & (1 << LR_REGNUM)) == 0)
14517     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14518   else
14519     {
14520       if (frame_pointer_needed)
14521         addr = plus_constant(hard_frame_pointer_rtx, -4);
14522       else
14523         {
14524           /* LR will be the first saved register.  */
14525           offsets = arm_get_frame_offsets ();
14526           delta = offsets->outgoing_args - (offsets->frame + 4);
14527
14528
14529           if (delta >= 4096)
14530             {
14531               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
14532                                      GEN_INT (delta & ~4095)));
14533               addr = scratch;
14534               delta &= 4095;
14535             }
14536           else
14537             addr = stack_pointer_rtx;
14538
14539           addr = plus_constant (addr, delta);
14540         }
14541       emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14542     }
14543 }
14544
14545
14546 void
14547 thumb_set_return_address (rtx source, rtx scratch)
14548 {
14549   arm_stack_offsets *offsets;
14550   HOST_WIDE_INT delta;
14551   int reg;
14552   rtx addr;
14553   unsigned long mask;
14554
14555   emit_insn (gen_rtx_USE (VOIDmode, source));
14556
14557   mask = thumb_compute_save_reg_mask ();
14558   if (mask & (1 << LR_REGNUM))
14559     {
14560       offsets = arm_get_frame_offsets ();
14561
14562       /* Find the saved regs.  */
14563       if (frame_pointer_needed)
14564         {
14565           delta = offsets->soft_frame - offsets->saved_args;
14566           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
14567         }
14568       else
14569         {
14570           delta = offsets->outgoing_args - offsets->saved_args;
14571           reg = SP_REGNUM;
14572         }
14573       /* Allow for the stack frame.  */
14574       if (TARGET_BACKTRACE)
14575         delta -= 16;
14576       /* The link register is always the first saved register.  */
14577       delta -= 4;
14578
14579       /* Construct the address.  */
14580       addr = gen_rtx_REG (SImode, reg);
14581       if ((reg != SP_REGNUM && delta >= 128)
14582           || delta >= 1024)
14583         {
14584           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
14585           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
14586           addr = scratch;
14587         }
14588       else
14589         addr = plus_constant (addr, delta);
14590
14591       emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14592     }
14593   else
14594     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14595 }
14596
14597 /* Implements target hook vector_mode_supported_p.  */
14598 bool
14599 arm_vector_mode_supported_p (enum machine_mode mode)
14600 {
14601   if ((mode == V2SImode)
14602       || (mode == V4HImode)
14603       || (mode == V8QImode))
14604     return true;
14605
14606   return false;
14607 }
14608
14609 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
14610    ARM insns and therefore guarantee that the shift count is modulo 256.
14611    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
14612    guarantee no particular behavior for out-of-range counts.  */
14613
14614 static unsigned HOST_WIDE_INT
14615 arm_shift_truncation_mask (enum machine_mode mode)
14616 {
14617   return mode == SImode ? 255 : 0;
14618 }
14619
14620
14621 /* Map internal gcc register numbers to DWARF2 register numbers.  */
14622
14623 unsigned int
14624 arm_dbx_register_number (unsigned int regno)
14625 {
14626   if (regno < 16)
14627     return regno;
14628
14629   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
14630      compatibility.  The EABI defines them as registers 96-103.  */
14631   if (IS_FPA_REGNUM (regno))
14632     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
14633
14634   if (IS_VFP_REGNUM (regno))
14635     return 64 + regno - FIRST_VFP_REGNUM;
14636
14637   if (IS_IWMMXT_GR_REGNUM (regno))
14638     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
14639
14640   if (IS_IWMMXT_REGNUM (regno))
14641     return 112 + regno - FIRST_IWMMXT_REGNUM;
14642
14643   gcc_unreachable ();
14644 }
14645