OSDN Git Service

* config/arm/arm.c (arm_legitimate_index_p): Correct maverick offsets.
[pf3gnuchains/gcc-fork.git] / gcc / config / arm / arm.c
1 /* Output routines for GCC for ARM.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004  Free Software Foundation, Inc.
4    Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5    and Martin Simmons (@harleqn.co.uk).
6    More major hacks by Richard Earnshaw (rearnsha@arm.com).
7
8    This file is part of GCC.
9
10    GCC is free software; you can redistribute it and/or modify it
11    under the terms of the GNU General Public License as published
12    by the Free Software Foundation; either version 2, or (at your
13    option) any later version.
14
15    GCC is distributed in the hope that it will be useful, but WITHOUT
16    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18    License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with GCC; see the file COPYING.  If not, write to
22    the Free Software Foundation, 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, USA.  */
24     
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "obstack.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "output.h"
38 #include "insn-attr.h"
39 #include "flags.h"
40 #include "reload.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "optabs.h"
44 #include "toplev.h"
45 #include "recog.h"
46 #include "ggc.h"
47 #include "except.h"
48 #include "c-pragma.h"
49 #include "integrate.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "debug.h"
54
55 /* Forward definitions of types.  */
56 typedef struct minipool_node    Mnode;
57 typedef struct minipool_fixup   Mfix;
58
59 const struct attribute_spec arm_attribute_table[];
60
61 /* Forward function declarations.  */
62 static arm_stack_offsets *arm_get_frame_offsets (void);
63 static void arm_add_gc_roots (void);
64 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
65                              HOST_WIDE_INT, rtx, rtx, int, int);
66 static unsigned bit_count (unsigned long);
67 static int arm_address_register_rtx_p (rtx, int);
68 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
69 static int thumb_base_register_rtx_p (rtx, enum machine_mode, int);
70 inline static int thumb_index_register_rtx_p (rtx, int);
71 static int thumb_far_jump_used_p (void);
72 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
73 static rtx emit_multi_reg_push (int);
74 static rtx emit_sfm (int, int);
75 #ifndef AOF_ASSEMBLER
76 static bool arm_assemble_integer (rtx, unsigned int, int);
77 #endif
78 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
79 static arm_cc get_arm_condition_code (rtx);
80 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
81 static rtx is_jump_table (rtx);
82 static const char *output_multi_immediate (rtx *, const char *, const char *,
83                                            int, HOST_WIDE_INT);
84 static void print_multi_reg (FILE *, const char *, int, int);
85 static const char *shift_op (rtx, HOST_WIDE_INT *);
86 static struct machine_function *arm_init_machine_status (void);
87 static int number_of_first_bit_set (int);
88 static void replace_symbols_in_block (tree, rtx, rtx);
89 static void thumb_exit (FILE *, int, rtx);
90 static void thumb_pushpop (FILE *, int, int, int *, int);
91 static rtx is_jump_table (rtx);
92 static HOST_WIDE_INT get_jump_table_size (rtx);
93 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
94 static Mnode *add_minipool_forward_ref (Mfix *);
95 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
96 static Mnode *add_minipool_backward_ref (Mfix *);
97 static void assign_minipool_offsets (Mfix *);
98 static void arm_print_value (FILE *, rtx);
99 static void dump_minipool (rtx);
100 static int arm_barrier_cost (rtx);
101 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
102 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
103 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
104                                rtx);
105 static void arm_reorg (void);
106 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
107 static int current_file_function_operand (rtx);
108 static unsigned long arm_compute_save_reg0_reg12_mask (void);
109 static unsigned long arm_compute_save_reg_mask (void);
110 static unsigned long arm_isr_value (tree);
111 static unsigned long arm_compute_func_type (void);
112 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
113 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
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 arm_use_dfa_pipeline_interface (void);
121 static int count_insns_for_constant (HOST_WIDE_INT, int);
122 static int arm_get_strip_length (int);
123 static bool arm_function_ok_for_sibcall (tree, tree);
124 static void arm_internal_label (FILE *, const char *, unsigned long);
125 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
126                                  tree);
127 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
128 static bool arm_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
145 #ifdef OBJECT_FORMAT_ELF
146 static void arm_elf_asm_named_section (const char *, unsigned int);
147 #endif
148 #ifndef ARM_PE
149 static void arm_encode_section_info (tree, rtx, int);
150 #endif
151 #ifdef AOF_ASSEMBLER
152 static void aof_globalize_label (FILE *, const char *);
153 static void aof_dump_imports (FILE *);
154 static void aof_dump_pic_table (FILE *);
155 static void aof_file_start (void);
156 static void aof_file_end (void);
157 #endif
158 static rtx arm_struct_value_rtx (tree, int);
159 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
160                                         tree, int *, int);
161
162 \f
163 /* Initialize the GCC target structure.  */
164 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
165 #undef  TARGET_MERGE_DECL_ATTRIBUTES
166 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
167 #endif
168
169 #undef  TARGET_ATTRIBUTE_TABLE
170 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
171
172 #ifdef AOF_ASSEMBLER
173 #undef  TARGET_ASM_BYTE_OP
174 #define TARGET_ASM_BYTE_OP "\tDCB\t"
175 #undef  TARGET_ASM_ALIGNED_HI_OP
176 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
177 #undef  TARGET_ASM_ALIGNED_SI_OP
178 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
179 #undef TARGET_ASM_GLOBALIZE_LABEL
180 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
181 #undef TARGET_ASM_FILE_START
182 #define TARGET_ASM_FILE_START aof_file_start
183 #undef TARGET_ASM_FILE_END
184 #define TARGET_ASM_FILE_END aof_file_end
185 #else
186 #undef  TARGET_ASM_ALIGNED_SI_OP
187 #define TARGET_ASM_ALIGNED_SI_OP NULL
188 #undef  TARGET_ASM_INTEGER
189 #define TARGET_ASM_INTEGER arm_assemble_integer
190 #endif
191
192 #undef  TARGET_ASM_FUNCTION_PROLOGUE
193 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
194
195 #undef  TARGET_ASM_FUNCTION_EPILOGUE
196 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
197
198 #undef  TARGET_COMP_TYPE_ATTRIBUTES
199 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
200
201 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
202 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
203
204 #undef  TARGET_SCHED_ADJUST_COST
205 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
206
207 #undef  TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE 
208 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE arm_use_dfa_pipeline_interface
209
210 #undef TARGET_ENCODE_SECTION_INFO
211 #ifdef ARM_PE
212 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
213 #else
214 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
215 #endif
216
217 #undef  TARGET_STRIP_NAME_ENCODING
218 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
219
220 #undef  TARGET_ASM_INTERNAL_LABEL
221 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
222
223 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
224 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
225
226 #undef  TARGET_ASM_OUTPUT_MI_THUNK
227 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
228 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
229 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
230
231 /* This will be overridden in arm_override_options.  */
232 #undef  TARGET_RTX_COSTS
233 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
234 #undef  TARGET_ADDRESS_COST
235 #define TARGET_ADDRESS_COST arm_address_cost
236
237 #undef  TARGET_MACHINE_DEPENDENT_REORG
238 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
239
240 #undef  TARGET_INIT_BUILTINS
241 #define TARGET_INIT_BUILTINS  arm_init_builtins
242 #undef  TARGET_EXPAND_BUILTIN
243 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
244
245 #undef TARGET_PROMOTE_FUNCTION_ARGS
246 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
247 #undef TARGET_PROMOTE_FUNCTION_RETURN
248 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
249 #undef TARGET_PROMOTE_PROTOTYPES
250 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_false
251
252 #undef TARGET_STRUCT_VALUE_RTX
253 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
254
255 #undef  TARGET_SETUP_INCOMING_VARARGS
256 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
257
258 struct gcc_target targetm = TARGET_INITIALIZER;
259 \f
260 /* Obstack for minipool constant handling.  */
261 static struct obstack minipool_obstack;
262 static char *         minipool_startobj;
263
264 /* The maximum number of insns skipped which
265    will be conditionalised if possible.  */
266 static int max_insns_skipped = 5;
267
268 extern FILE * asm_out_file;
269
270 /* True if we are currently building a constant table.  */
271 int making_const_table;
272
273 /* Define the information needed to generate branch insns.  This is
274    stored from the compare operation.  */
275 rtx arm_compare_op0, arm_compare_op1;
276
277 /* The processor for which instructions should be scheduled.  */
278 enum processor_type arm_tune = arm_none;
279
280 /* Which floating point model to use.  */
281 enum arm_fp_model arm_fp_model;
282
283 /* Which floating point hardware is available.  */
284 enum fputype arm_fpu_arch;
285
286 /* Which floating point hardware to schedule for.  */
287 enum fputype arm_fpu_tune;
288
289 /* Whether to use floating point hardware.  */
290 enum float_abi_type arm_float_abi;
291
292 /* Which ABI to use.  */
293 enum arm_abi_type arm_abi;
294
295 /* What program mode is the cpu running in? 26-bit mode or 32-bit mode.  */
296 enum prog_mode_type arm_prgmode;
297
298 /* Set by the -mfpu=... option.  */
299 const char * target_fpu_name = NULL;
300
301 /* Set by the -mfpe=... option.  */
302 const char * target_fpe_name = NULL;
303
304 /* Set by the -mfloat-abi=... option.  */
305 const char * target_float_abi_name = NULL;
306
307 /* Set by the -mabi=... option.  */
308 const char * target_abi_name = NULL;
309
310 /* Used to parse -mstructure_size_boundary command line option.  */
311 const char * structure_size_string = NULL;
312 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
313
314 /* Bit values used to identify processor capabilities.  */
315 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
316 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
317 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
318 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
319 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
320 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
321 #define FL_THUMB      (1 << 6)        /* Thumb aware */
322 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
323 #define FL_STRONG     (1 << 8)        /* StrongARM */
324 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
325 #define FL_XSCALE     (1 << 10)       /* XScale */
326 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
327 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
328                                          media instructions.  */
329 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
330
331 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
332
333 /* The bits in this mask specify which
334    instructions we are allowed to generate.  */
335 static unsigned long insn_flags = 0;
336
337 /* The bits in this mask specify which instruction scheduling options should
338    be used.  */
339 static unsigned long tune_flags = 0;
340
341 /* The following are used in the arm.md file as equivalents to bits
342    in the above two flag variables.  */
343
344 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
345 int arm_arch3m = 0;
346
347 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
348 int arm_arch4 = 0;
349
350 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
351 int arm_arch5 = 0;
352
353 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
354 int arm_arch5e = 0;
355
356 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
357 int arm_arch6 = 0;
358
359 /* Nonzero if this chip can benefit from load scheduling.  */
360 int arm_ld_sched = 0;
361
362 /* Nonzero if this chip is a StrongARM.  */
363 int arm_is_strong = 0;
364
365 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
366 int arm_arch_iwmmxt = 0;
367
368 /* Nonzero if this chip is an XScale.  */
369 int arm_arch_xscale = 0;
370
371 /* Nonzero if tuning for XScale  */
372 int arm_tune_xscale = 0;
373
374 /* Nonzero if this chip is an ARM6 or an ARM7.  */
375 int arm_is_6_or_7 = 0;
376
377 /* Nonzero if generating Thumb instructions.  */
378 int thumb_code = 0;
379
380 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
381    must report the mode of the memory reference from PRINT_OPERAND to
382    PRINT_OPERAND_ADDRESS.  */
383 enum machine_mode output_memory_reference_mode;
384
385 /* The register number to be used for the PIC offset register.  */
386 const char * arm_pic_register_string = NULL;
387 int arm_pic_register = INVALID_REGNUM;
388
389 /* Set to 1 when a return insn is output, this means that the epilogue
390    is not needed.  */
391 int return_used_this_function;
392
393 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
394    the next function.  */
395 static int after_arm_reorg = 0;
396
397 /* The maximum number of insns to be used when loading a constant.  */
398 static int arm_constant_limit = 3;
399
400 /* For an explanation of these variables, see final_prescan_insn below.  */
401 int arm_ccfsm_state;
402 enum arm_cond_code arm_current_cc;
403 rtx arm_target_insn;
404 int arm_target_label;
405
406 /* The condition codes of the ARM, and the inverse function.  */
407 static const char * const arm_condition_codes[] =
408 {
409   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
410   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
411 };
412
413 #define streq(string1, string2) (strcmp (string1, string2) == 0)
414 \f
415 /* Initialization code.  */
416
417 struct processors
418 {
419   const char *const name;
420   enum processor_type core;
421   const unsigned long flags;
422   bool (* rtx_costs) (rtx, int, int, int *);
423 };
424
425 /* Not all of these give usefully different compilation alternatives,
426    but there is no simple way of generalizing them.  */
427 static const struct processors all_cores[] =
428 {
429   /* ARM Cores */
430 #define ARM_CORE(NAME, FLAGS, COSTS) \
431   {#NAME, arm_none, FLAGS, arm_##COSTS##_rtx_costs},
432 #include "arm-cores.def"
433 #undef ARM_CORE
434   {NULL, arm_none, 0, NULL}
435 };
436
437 static const struct processors all_architectures[] =
438 {
439   /* ARM Architectures */
440   /* We don't specify rtx_costs here as it will be figured out
441      from the core.  */
442   
443   { "armv2",     arm2,       FL_CO_PROC | FL_MODE26 , NULL},
444   { "armv2a",    arm2,       FL_CO_PROC | FL_MODE26 , NULL},
445   { "armv3",     arm6,       FL_CO_PROC | FL_MODE26 | FL_MODE32 , NULL},
446   { "armv3m",    arm7m,      FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_ARCH3M , NULL},
447   { "armv4",     arm7tdmi,   FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_ARCH3M | FL_ARCH4 , NULL},
448   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
449      implementations that support it, so we will leave it out for now.  */
450   { "armv4t",    arm7tdmi,   FL_CO_PROC |             FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB , NULL},
451   { "armv5",     arm10tdmi,  FL_CO_PROC |             FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 , NULL},
452   { "armv5t",    arm10tdmi,  FL_CO_PROC |             FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 , NULL},
453   { "armv5te",   arm1026ejs, FL_CO_PROC |             FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 | FL_ARCH5E , NULL},
454   { "armv6",     arm1136js,  FL_CO_PROC |             FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 | FL_ARCH5E | FL_ARCH6 , NULL},
455   { "armv6j",    arm1136js,  FL_CO_PROC |             FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_ARCH5 | FL_ARCH5E | FL_ARCH6 , NULL},
456   { "ep9312",    ep9312,                              FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_LDSCHED | FL_CIRRUS , NULL},
457   {"iwmmxt",     iwmmxt,                              FL_MODE32 | FL_ARCH3M | FL_ARCH4 | FL_THUMB | FL_LDSCHED | FL_STRONG | FL_ARCH5 | FL_ARCH5E | FL_XSCALE | FL_IWMMXT , NULL},
458   { NULL, arm_none, 0 , NULL}
459 };
460
461 /* This is a magic structure.  The 'string' field is magically filled in
462    with a pointer to the value specified by the user on the command line
463    assuming that the user has specified such a value.  */
464
465 struct arm_cpu_select arm_select[] =
466 {
467   /* string       name            processors  */        
468   { NULL,       "-mcpu=",       all_cores  },
469   { NULL,       "-march=",      all_architectures },
470   { NULL,       "-mtune=",      all_cores }
471 };
472
473 struct fpu_desc
474 {
475   const char * name;
476   enum fputype fpu;
477 };
478
479
480 /* Available values for for -mfpu=.  */
481
482 static const struct fpu_desc all_fpus[] =
483 {
484   {"fpa",       FPUTYPE_FPA},
485   {"fpe2",      FPUTYPE_FPA_EMU2},
486   {"fpe3",      FPUTYPE_FPA_EMU2},
487   {"maverick",  FPUTYPE_MAVERICK},
488   {"vfp",       FPUTYPE_VFP}
489 };
490
491
492 /* Floating point models used by the different hardware.
493    See fputype in arm.h.  */
494
495 static const enum fputype fp_model_for_fpu[] =
496 {
497   /* No FP hardware.  */
498   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
499   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
500   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
501   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
502   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
503   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
504 };
505
506
507 struct float_abi
508 {
509   const char * name;
510   enum float_abi_type abi_type;
511 };
512
513
514 /* Available values for -mfloat-abi=.  */
515
516 static const struct float_abi all_float_abis[] =
517 {
518   {"soft",      ARM_FLOAT_ABI_SOFT},
519   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
520   {"hard",      ARM_FLOAT_ABI_HARD}
521 };
522
523
524 struct abi_name
525 {
526   const char *name;
527   enum arm_abi_type abi_type;
528 };
529
530
531 /* Available values for -mabi=.  */
532
533 static const struct abi_name arm_all_abis[] =
534 {
535   {"apcs-gnu",    ARM_ABI_APCS},
536   {"atpcs",   ARM_ABI_ATPCS},
537   {"aapcs",   ARM_ABI_AAPCS},
538   {"iwmmxt",  ARM_ABI_IWMMXT}
539 };
540
541 /* Return the number of bits set in VALUE.  */
542 static unsigned
543 bit_count (unsigned long value)
544 {
545   unsigned long count = 0;
546   
547   while (value)
548     {
549       count++;
550       value &= value - 1;  /* Clear the least-significant set bit.  */
551     }
552
553   return count;
554 }
555
556 /* Fix up any incompatible options that the user has specified.
557    This has now turned into a maze.  */
558 void
559 arm_override_options (void)
560 {
561   unsigned i;
562
563   /* Set up the flags based on the cpu/architecture selected by the user.  */
564   for (i = ARRAY_SIZE (arm_select); i--;)
565     {
566       struct arm_cpu_select * ptr = arm_select + i;
567       
568       if (ptr->string != NULL && ptr->string[0] != '\0')
569         {
570           const struct processors * sel;
571
572           for (sel = ptr->processors; sel->name != NULL; sel++)
573             if (streq (ptr->string, sel->name))
574               {
575                 /* Determine the processor core for which we should
576                    tune code-generation.  */
577                 if (/* -mcpu= is a sensible default.  */
578                     i == 0
579                     /* If -march= is used, and -mcpu= has not been used,
580                        assume that we should tune for a representative
581                        CPU from that architecture.  */
582                     || i == 1
583                     /* -mtune= overrides -mcpu= and -march=.  */
584                     || i == 2)
585                   arm_tune = (enum processor_type) (sel - ptr->processors);
586
587                 if (i != 2)
588                   {
589                     /* If we have been given an architecture and a processor
590                        make sure that they are compatible.  We only generate
591                        a warning though, and we prefer the CPU over the
592                        architecture.  */
593                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
594                       warning ("switch -mcpu=%s conflicts with -march= switch",
595                                ptr->string);
596                     
597                     insn_flags = sel->flags;
598                   }
599                 
600                 break;
601               }
602
603           if (sel->name == NULL)
604             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
605         }
606     }
607   
608   /* If the user did not specify a processor, choose one for them.  */
609   if (insn_flags == 0)
610     {
611       const struct processors * sel;
612       unsigned int        sought;
613       static const struct cpu_default
614       {
615         const int cpu;
616         const char *const name;
617       }
618       cpu_defaults[] =
619       {
620         { TARGET_CPU_arm2,      "arm2" },
621         { TARGET_CPU_arm6,      "arm6" },
622         { TARGET_CPU_arm610,    "arm610" },
623         { TARGET_CPU_arm710,    "arm710" },
624         { TARGET_CPU_arm7m,     "arm7m" },
625         { TARGET_CPU_arm7500fe, "arm7500fe" },
626         { TARGET_CPU_arm7tdmi,  "arm7tdmi" },
627         { TARGET_CPU_arm8,      "arm8" },
628         { TARGET_CPU_arm810,    "arm810" },
629         { TARGET_CPU_arm9,      "arm9" },
630         { TARGET_CPU_strongarm, "strongarm" },
631         { TARGET_CPU_xscale,    "xscale" },
632         { TARGET_CPU_ep9312,    "ep9312" },
633         { TARGET_CPU_iwmmxt,    "iwmmxt" },
634         { TARGET_CPU_arm926ejs, "arm926ejs" },
635         { TARGET_CPU_arm1026ejs, "arm1026ejs" },
636         { TARGET_CPU_arm1136js, "arm1136js" },
637         { TARGET_CPU_arm1136jfs, "arm1136jfs" },
638         { TARGET_CPU_generic,   "arm" },
639         { 0, 0 }
640       };
641       const struct cpu_default * def;
642           
643       /* Find the default.  */
644       for (def = cpu_defaults; def->name; def++)
645         if (def->cpu == TARGET_CPU_DEFAULT)
646           break;
647
648       /* Make sure we found the default CPU.  */
649       if (def->name == NULL)
650         abort ();
651       
652       /* Find the default CPU's flags.  */
653       for (sel = all_cores; sel->name != NULL; sel++)
654         if (streq (def->name, sel->name))
655           break;
656       
657       if (sel->name == NULL)
658         abort ();
659
660       insn_flags = sel->flags;
661
662       /* Now check to see if the user has specified some command line
663          switch that require certain abilities from the cpu.  */
664       sought = 0;
665       
666       if (TARGET_INTERWORK || TARGET_THUMB)
667         {
668           sought |= (FL_THUMB | FL_MODE32);
669           
670           /* Force apcs-32 to be used for interworking.  */
671           target_flags |= ARM_FLAG_APCS_32;
672
673           /* There are no ARM processors that support both APCS-26 and
674              interworking.  Therefore we force FL_MODE26 to be removed
675              from insn_flags here (if it was set), so that the search
676              below will always be able to find a compatible processor.  */
677           insn_flags &= ~FL_MODE26;
678         }
679       else if (!TARGET_APCS_32)
680         sought |= FL_MODE26;
681       
682       if (sought != 0 && ((sought & insn_flags) != sought))
683         {
684           /* Try to locate a CPU type that supports all of the abilities
685              of the default CPU, plus the extra abilities requested by
686              the user.  */
687           for (sel = all_cores; sel->name != NULL; sel++)
688             if ((sel->flags & sought) == (sought | insn_flags))
689               break;
690
691           if (sel->name == NULL)
692             {
693               unsigned current_bit_count = 0;
694               const struct processors * best_fit = NULL;
695               
696               /* Ideally we would like to issue an error message here
697                  saying that it was not possible to find a CPU compatible
698                  with the default CPU, but which also supports the command
699                  line options specified by the programmer, and so they
700                  ought to use the -mcpu=<name> command line option to
701                  override the default CPU type.
702
703                  Unfortunately this does not work with multilibing.  We
704                  need to be able to support multilibs for -mapcs-26 and for
705                  -mthumb-interwork and there is no CPU that can support both
706                  options.  Instead if we cannot find a cpu that has both the
707                  characteristics of the default cpu and the given command line
708                  options we scan the array again looking for a best match.  */
709               for (sel = all_cores; sel->name != NULL; sel++)
710                 if ((sel->flags & sought) == sought)
711                   {
712                     unsigned count;
713
714                     count = bit_count (sel->flags & insn_flags);
715
716                     if (count >= current_bit_count)
717                       {
718                         best_fit = sel;
719                         current_bit_count = count;
720                       }
721                   }
722
723               if (best_fit == NULL)
724                 abort ();
725               else
726                 sel = best_fit;
727             }
728
729           insn_flags = sel->flags;
730         }
731       if (arm_tune == arm_none)
732         arm_tune = (enum processor_type) (sel - all_cores);
733     }
734   
735   /* The processor for which we should tune should now have been
736      chosen.  */
737   if (arm_tune == arm_none)
738     abort ();
739   
740   tune_flags = all_cores[(int)arm_tune].flags;
741   targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
742
743   /* Make sure that the processor choice does not conflict with any of the
744      other command line choices.  */
745   if (TARGET_APCS_32 && !(insn_flags & FL_MODE32))
746     {
747       /* If APCS-32 was not the default then it must have been set by the
748          user, so issue a warning message.  If the user has specified
749          "-mapcs-32 -mcpu=arm2" then we loose here.  */
750       if ((TARGET_DEFAULT & ARM_FLAG_APCS_32) == 0)
751         warning ("target CPU does not support APCS-32" );
752       target_flags &= ~ARM_FLAG_APCS_32;
753     }
754   else if (!TARGET_APCS_32 && !(insn_flags & FL_MODE26))
755     {
756       warning ("target CPU does not support APCS-26" );
757       target_flags |= ARM_FLAG_APCS_32;
758     }
759   
760   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
761     {
762       warning ("target CPU does not support interworking" );
763       target_flags &= ~ARM_FLAG_INTERWORK;
764     }
765   
766   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
767     {
768       warning ("target CPU does not support THUMB instructions");
769       target_flags &= ~ARM_FLAG_THUMB;
770     }
771
772   if (TARGET_APCS_FRAME && TARGET_THUMB)
773     {
774       /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
775       target_flags &= ~ARM_FLAG_APCS_FRAME;
776     }
777
778   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
779      from here where no function is being compiled currently.  */
780   if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
781       && TARGET_ARM)
782     warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
783
784   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
785     warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
786
787   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
788     warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
789
790   /* If interworking is enabled then APCS-32 must be selected as well.  */
791   if (TARGET_INTERWORK)
792     {
793       if (!TARGET_APCS_32)
794         warning ("interworking forces APCS-32 to be used" );
795       target_flags |= ARM_FLAG_APCS_32;
796     }
797   
798   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
799     {
800       warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
801       target_flags |= ARM_FLAG_APCS_FRAME;
802     }
803   
804   if (TARGET_POKE_FUNCTION_NAME)
805     target_flags |= ARM_FLAG_APCS_FRAME;
806   
807   if (TARGET_APCS_REENT && flag_pic)
808     error ("-fpic and -mapcs-reent are incompatible");
809   
810   if (TARGET_APCS_REENT)
811     warning ("APCS reentrant code not supported.  Ignored");
812   
813   /* If this target is normally configured to use APCS frames, warn if they
814      are turned off and debugging is turned on.  */
815   if (TARGET_ARM
816       && write_symbols != NO_DEBUG
817       && !TARGET_APCS_FRAME
818       && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
819     warning ("-g with -mno-apcs-frame may not give sensible debugging");
820   
821   /* If stack checking is disabled, we can use r10 as the PIC register,
822      which keeps r9 available.  */
823   if (flag_pic)
824     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
825   
826   if (TARGET_APCS_FLOAT)
827     warning ("passing floating point arguments in fp regs not yet supported");
828   
829   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
830   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
831   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
832   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
833   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
834   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
835   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
836
837   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
838   arm_is_strong = (tune_flags & FL_STRONG) != 0;
839   thumb_code = (TARGET_ARM == 0);
840   arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
841                     && !(tune_flags & FL_ARCH4))) != 0;
842   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
843   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
844
845   if (target_abi_name)
846     {
847       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
848         {
849           if (streq (arm_all_abis[i].name, target_abi_name))
850             {
851               arm_abi = arm_all_abis[i].abi_type;
852               break;
853             }
854         }
855       if (i == ARRAY_SIZE (arm_all_abis))
856         error ("invalid ABI option: -mabi=%s", target_abi_name);
857     }
858   else
859     arm_abi = ARM_DEFAULT_ABI;
860
861   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
862     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
863
864   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
865     error ("iwmmxt abi requires an iwmmxt capable cpu");
866
867   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
868   if (target_fpu_name == NULL && target_fpe_name != NULL)
869     {
870       if (streq (target_fpe_name, "2"))
871         target_fpu_name = "fpe2";
872       else if (streq (target_fpe_name, "3"))
873         target_fpu_name = "fpe3";
874       else
875         error ("invalid floating point emulation option: -mfpe=%s",
876                target_fpe_name);
877     }
878   if (target_fpu_name != NULL)
879     {
880       /* The user specified a FPU.  */
881       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
882         {
883           if (streq (all_fpus[i].name, target_fpu_name))
884             {
885               arm_fpu_arch = all_fpus[i].fpu;
886               arm_fpu_tune = arm_fpu_arch;
887               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
888               break;
889             }
890         }
891       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
892         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
893     }
894   else
895     {
896 #ifdef FPUTYPE_DEFAULT
897       /* Use the default is it is specified for this platform.  */
898       arm_fpu_arch = FPUTYPE_DEFAULT;
899       arm_fpu_tune = FPUTYPE_DEFAULT;
900 #else
901       /* Pick one based on CPU type.  */
902       if ((insn_flags & FL_VFP) != 0)
903         arm_fpu_arch = FPUTYPE_VFP;
904       else if (insn_flags & FL_CIRRUS)
905         arm_fpu_arch = FPUTYPE_MAVERICK;
906       else
907         arm_fpu_arch = FPUTYPE_FPA_EMU2;
908 #endif
909       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
910         arm_fpu_tune = FPUTYPE_FPA;
911       else
912         arm_fpu_tune = arm_fpu_arch;
913       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
914       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
915         abort ();
916     }
917
918   if (target_float_abi_name != NULL)
919     {
920       /* The user specified a FP ABI.  */
921       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
922         {
923           if (streq (all_float_abis[i].name, target_float_abi_name))
924             {
925               arm_float_abi = all_float_abis[i].abi_type;
926               break;
927             }
928         }
929       if (i == ARRAY_SIZE (all_float_abis))
930         error ("invalid floating point abi: -mfloat-abi=%s",
931                target_float_abi_name);
932     }
933   else
934     {
935       /* Use soft-float target flag.  */
936       if (target_flags & ARM_FLAG_SOFT_FLOAT)
937         arm_float_abi = ARM_FLOAT_ABI_SOFT;
938       else
939         arm_float_abi = ARM_FLOAT_ABI_HARD;
940     }
941
942   if (arm_float_abi == ARM_FLOAT_ABI_SOFTFP)
943     sorry ("-mfloat-abi=softfp");
944   /* If soft-float is specified then don't use FPU.  */
945   if (TARGET_SOFT_FLOAT)
946     arm_fpu_arch = FPUTYPE_NONE;
947   
948   /* For arm2/3 there is no need to do any scheduling if there is only
949      a floating point emulator, or we are doing software floating-point.  */
950   if ((TARGET_SOFT_FLOAT
951        || arm_fpu_tune == FPUTYPE_FPA_EMU2
952        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
953       && (tune_flags & FL_MODE32) == 0)
954     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
955   
956   arm_prgmode = TARGET_APCS_32 ? PROG_MODE_PROG32 : PROG_MODE_PROG26;
957   
958   /* Override the default structure alignment for AAPCS ABI.  */
959   if (arm_abi == ARM_ABI_AAPCS)
960     arm_structure_size_boundary = 8;
961
962   if (structure_size_string != NULL)
963     {
964       int size = strtol (structure_size_string, NULL, 0);
965
966       if (size == 8 || size == 32
967           || (ARM_DOUBLEWORD_ALIGN && size == 64))
968         arm_structure_size_boundary = size;
969       else
970         warning ("structure size boundary can only be set to %s",
971                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
972     }
973
974   if (arm_pic_register_string != NULL)
975     {
976       int pic_register = decode_reg_name (arm_pic_register_string);
977
978       if (!flag_pic)
979         warning ("-mpic-register= is useless without -fpic");
980
981       /* Prevent the user from choosing an obviously stupid PIC register.  */
982       else if (pic_register < 0 || call_used_regs[pic_register]
983                || pic_register == HARD_FRAME_POINTER_REGNUM
984                || pic_register == STACK_POINTER_REGNUM
985                || pic_register >= PC_REGNUM)
986         error ("unable to use '%s' for PIC register", arm_pic_register_string);
987       else
988         arm_pic_register = pic_register;
989     }
990
991   if (TARGET_THUMB && flag_schedule_insns)
992     {
993       /* Don't warn since it's on by default in -O2.  */
994       flag_schedule_insns = 0;
995     }
996
997   if (optimize_size)
998     {
999       /* There's some dispute as to whether this should be 1 or 2.  However,
1000          experiments seem to show that in pathological cases a setting of
1001          1 degrades less severely than a setting of 2.  This could change if
1002          other parts of the compiler change their behavior.  */
1003       arm_constant_limit = 1;
1004
1005       /* If optimizing for size, bump the number of instructions that we
1006          are prepared to conditionally execute (even on a StrongARM).  */
1007       max_insns_skipped = 6;
1008     }
1009   else
1010     {
1011       /* For processors with load scheduling, it never costs more than
1012          2 cycles to load a constant, and the load scheduler may well
1013          reduce that to 1.  */
1014       if (tune_flags & FL_LDSCHED)
1015         arm_constant_limit = 1;
1016
1017       /* On XScale the longer latency of a load makes it more difficult
1018          to achieve a good schedule, so it's faster to synthesize
1019          constants that can be done in two insns.  */
1020       if (arm_tune_xscale)
1021         arm_constant_limit = 2;
1022
1023       /* StrongARM has early execution of branches, so a sequence
1024          that is worth skipping is shorter.  */
1025       if (arm_is_strong)
1026         max_insns_skipped = 3;
1027     }
1028
1029   /* Register global variables with the garbage collector.  */
1030   arm_add_gc_roots ();
1031 }
1032
1033 static void
1034 arm_add_gc_roots (void)
1035 {
1036   gcc_obstack_init(&minipool_obstack);
1037   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1038 }
1039 \f
1040 /* A table of known ARM exception types.
1041    For use with the interrupt function attribute.  */
1042
1043 typedef struct
1044 {
1045   const char *const arg;
1046   const unsigned long return_value;
1047 }
1048 isr_attribute_arg;
1049
1050 static const isr_attribute_arg isr_attribute_args [] =
1051 {
1052   { "IRQ",   ARM_FT_ISR },
1053   { "irq",   ARM_FT_ISR },
1054   { "FIQ",   ARM_FT_FIQ },
1055   { "fiq",   ARM_FT_FIQ },
1056   { "ABORT", ARM_FT_ISR },
1057   { "abort", ARM_FT_ISR },
1058   { "ABORT", ARM_FT_ISR },
1059   { "abort", ARM_FT_ISR },
1060   { "UNDEF", ARM_FT_EXCEPTION },
1061   { "undef", ARM_FT_EXCEPTION },
1062   { "SWI",   ARM_FT_EXCEPTION },
1063   { "swi",   ARM_FT_EXCEPTION },
1064   { NULL,    ARM_FT_NORMAL }
1065 };
1066
1067 /* Returns the (interrupt) function type of the current
1068    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1069
1070 static unsigned long
1071 arm_isr_value (tree argument)
1072 {
1073   const isr_attribute_arg * ptr;
1074   const char *              arg;
1075
1076   /* No argument - default to IRQ.  */
1077   if (argument == NULL_TREE)
1078     return ARM_FT_ISR;
1079
1080   /* Get the value of the argument.  */
1081   if (TREE_VALUE (argument) == NULL_TREE
1082       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1083     return ARM_FT_UNKNOWN;
1084
1085   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1086
1087   /* Check it against the list of known arguments.  */
1088   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1089     if (streq (arg, ptr->arg))
1090       return ptr->return_value;
1091
1092   /* An unrecognized interrupt type.  */
1093   return ARM_FT_UNKNOWN;
1094 }
1095
1096 /* Computes the type of the current function.  */
1097
1098 static unsigned long
1099 arm_compute_func_type (void)
1100 {
1101   unsigned long type = ARM_FT_UNKNOWN;
1102   tree a;
1103   tree attr;
1104   
1105   if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
1106     abort ();
1107
1108   /* Decide if the current function is volatile.  Such functions
1109      never return, and many memory cycles can be saved by not storing
1110      register values that will never be needed again.  This optimization
1111      was added to speed up context switching in a kernel application.  */
1112   if (optimize > 0
1113       && current_function_nothrow
1114       && TREE_THIS_VOLATILE (current_function_decl))
1115     type |= ARM_FT_VOLATILE;
1116   
1117   if (current_function_needs_context)
1118     type |= ARM_FT_NESTED;
1119
1120   attr = DECL_ATTRIBUTES (current_function_decl);
1121   
1122   a = lookup_attribute ("naked", attr);
1123   if (a != NULL_TREE)
1124     type |= ARM_FT_NAKED;
1125
1126   if (cfun->machine->eh_epilogue_sp_ofs != NULL_RTX)
1127     type |= ARM_FT_EXCEPTION_HANDLER;
1128   else
1129     {
1130       a = lookup_attribute ("isr", attr);
1131       if (a == NULL_TREE)
1132         a = lookup_attribute ("interrupt", attr);
1133       
1134       if (a == NULL_TREE)
1135         type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1136       else
1137         type |= arm_isr_value (TREE_VALUE (a));
1138     }
1139   
1140   return type;
1141 }
1142
1143 /* Returns the type of the current function.  */
1144
1145 unsigned long
1146 arm_current_func_type (void)
1147 {
1148   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1149     cfun->machine->func_type = arm_compute_func_type ();
1150
1151   return cfun->machine->func_type;
1152 }
1153 \f
1154 /* Return 1 if it is possible to return using a single instruction.  
1155    If SIBLING is non-null, this is a test for a return before a sibling
1156    call.  SIBLING is the call insn, so we can examine its register usage.  */
1157
1158 int
1159 use_return_insn (int iscond, rtx sibling)
1160 {
1161   int regno;
1162   unsigned int func_type;
1163   unsigned long saved_int_regs;
1164   unsigned HOST_WIDE_INT stack_adjust;
1165   arm_stack_offsets *offsets;
1166
1167   /* Never use a return instruction before reload has run.  */
1168   if (!reload_completed)
1169     return 0;
1170
1171   func_type = arm_current_func_type ();
1172
1173   /* Naked functions and volatile functions need special
1174      consideration.  */
1175   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1176     return 0;
1177
1178   /* So do interrupt functions that use the frame pointer.  */
1179   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1180     return 0;
1181
1182   offsets = arm_get_frame_offsets ();
1183   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1184
1185   /* As do variadic functions.  */
1186   if (current_function_pretend_args_size
1187       || cfun->machine->uses_anonymous_args
1188       /* Or if the function calls __builtin_eh_return () */
1189       || ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
1190       /* Or if the function calls alloca */
1191       || current_function_calls_alloca
1192       /* Or if there is a stack adjustment.  However, if the stack pointer
1193          is saved on the stack, we can use a pre-incrementing stack load.  */
1194       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1195     return 0;
1196
1197   saved_int_regs = arm_compute_save_reg_mask ();
1198
1199   /* Unfortunately, the insn
1200
1201        ldmib sp, {..., sp, ...}
1202
1203      triggers a bug on most SA-110 based devices, such that the stack
1204      pointer won't be correctly restored if the instruction takes a
1205      page fault.  We work around this problem by popping r3 along with
1206      the other registers, since that is never slower than executing
1207      another instruction.  
1208
1209      We test for !arm_arch5 here, because code for any architecture
1210      less than this could potentially be run on one of the buggy
1211      chips.  */
1212   if (stack_adjust == 4 && !arm_arch5)
1213     {
1214       /* Validate that r3 is a call-clobbered register (always true in
1215          the default abi) ...  */
1216       if (!call_used_regs[3])
1217         return 0;
1218
1219       /* ... that it isn't being used for a return value (always true
1220          until we implement return-in-regs), or for a tail-call
1221          argument ...  */
1222       if (sibling)
1223         {
1224           if (GET_CODE (sibling) != CALL_INSN)
1225             abort ();
1226
1227           if (find_regno_fusage (sibling, USE, 3))
1228             return 0;
1229         }
1230
1231       /* ... and that there are no call-saved registers in r0-r2
1232          (always true in the default ABI).  */
1233       if (saved_int_regs & 0x7)
1234         return 0;
1235     }
1236
1237   /* Can't be done if interworking with Thumb, and any registers have been
1238      stacked.  */
1239   if (TARGET_INTERWORK && saved_int_regs != 0)
1240     return 0;
1241
1242   /* On StrongARM, conditional returns are expensive if they aren't
1243      taken and multiple registers have been stacked.  */
1244   if (iscond && arm_is_strong)
1245     {
1246       /* Conditional return when just the LR is stored is a simple 
1247          conditional-load instruction, that's not expensive.  */
1248       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1249         return 0;
1250
1251       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1252         return 0;
1253     }
1254
1255   /* If there are saved registers but the LR isn't saved, then we need
1256      two instructions for the return.  */
1257   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1258     return 0;
1259
1260   /* Can't be done if any of the FPA regs are pushed,
1261      since this also requires an insn.  */
1262   if (TARGET_HARD_FLOAT && TARGET_FPA)
1263     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1264       if (regs_ever_live[regno] && !call_used_regs[regno])
1265         return 0;
1266
1267   /* Likewise VFP regs.  */
1268   if (TARGET_HARD_FLOAT && TARGET_VFP)
1269     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1270       if (regs_ever_live[regno] && !call_used_regs[regno])
1271         return 0;
1272
1273   if (TARGET_REALLY_IWMMXT)
1274     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1275       if (regs_ever_live[regno] && ! call_used_regs [regno])
1276         return 0;
1277
1278   return 1;
1279 }
1280
1281 /* Return TRUE if int I is a valid immediate ARM constant.  */
1282
1283 int
1284 const_ok_for_arm (HOST_WIDE_INT i)
1285 {
1286   unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1287
1288   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must 
1289      be all zero, or all one.  */
1290   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1291       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1292           != ((~(unsigned HOST_WIDE_INT) 0)
1293               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1294     return FALSE;
1295   
1296   /* Fast return for 0 and powers of 2 */
1297   if ((i & (i - 1)) == 0)
1298     return TRUE;
1299
1300   do
1301     {
1302       if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1303         return TRUE;
1304       mask =
1305           (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1306                           >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1307     }
1308   while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1309
1310   return FALSE;
1311 }
1312
1313 /* Return true if I is a valid constant for the operation CODE.  */
1314 static int
1315 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1316 {
1317   if (const_ok_for_arm (i))
1318     return 1;
1319
1320   switch (code)
1321     {
1322     case PLUS:
1323       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1324
1325     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1326     case XOR:
1327     case IOR:
1328       return 0;
1329
1330     case AND:
1331       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1332
1333     default:
1334       abort ();
1335     }
1336 }
1337
1338 /* Emit a sequence of insns to handle a large constant.
1339    CODE is the code of the operation required, it can be any of SET, PLUS,
1340    IOR, AND, XOR, MINUS;
1341    MODE is the mode in which the operation is being performed;
1342    VAL is the integer to operate on;
1343    SOURCE is the other operand (a register, or a null-pointer for SET);
1344    SUBTARGETS means it is safe to create scratch registers if that will
1345    either produce a simpler sequence, or we will want to cse the values.
1346    Return value is the number of insns emitted.  */
1347
1348 int
1349 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1350                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1351 {
1352   rtx cond;
1353
1354   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1355     cond = COND_EXEC_TEST (PATTERN (insn));
1356   else
1357     cond = NULL_RTX;
1358
1359   if (subtargets || code == SET
1360       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1361           && REGNO (target) != REGNO (source)))
1362     {
1363       /* After arm_reorg has been called, we can't fix up expensive
1364          constants by pushing them into memory so we must synthesize
1365          them in-line, regardless of the cost.  This is only likely to
1366          be more costly on chips that have load delay slots and we are
1367          compiling without running the scheduler (so no splitting
1368          occurred before the final instruction emission).
1369
1370          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1371       */
1372       if (!after_arm_reorg
1373           && !cond
1374           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source, 
1375                                 1, 0)
1376               > arm_constant_limit + (code != SET)))
1377         {
1378           if (code == SET)
1379             {
1380               /* Currently SET is the only monadic value for CODE, all
1381                  the rest are diadic.  */
1382               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1383               return 1;
1384             }
1385           else
1386             {
1387               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1388
1389               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1390               /* For MINUS, the value is subtracted from, since we never
1391                  have subtraction of a constant.  */
1392               if (code == MINUS)
1393                 emit_insn (gen_rtx_SET (VOIDmode, target,
1394                                         gen_rtx_MINUS (mode, temp, source)));
1395               else
1396                 emit_insn (gen_rtx_SET (VOIDmode, target,
1397                                         gen_rtx_fmt_ee (code, mode, source, temp)));
1398               return 2;
1399             }
1400         }
1401     }
1402
1403   return arm_gen_constant (code, mode, cond, val, target, source, subtargets, 
1404                            1);
1405 }
1406
1407 static int
1408 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1409 {
1410   HOST_WIDE_INT temp1;
1411   int num_insns = 0;
1412   do
1413     {
1414       int end;
1415           
1416       if (i <= 0)
1417         i += 32;
1418       if (remainder & (3 << (i - 2)))
1419         {
1420           end = i - 8;
1421           if (end < 0)
1422             end += 32;
1423           temp1 = remainder & ((0x0ff << end)
1424                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1425           remainder &= ~temp1;
1426           num_insns++;
1427           i -= 6;
1428         }
1429       i -= 2;
1430     } while (remainder);
1431   return num_insns;
1432 }
1433
1434 /* Emit an instruction with the indicated PATTERN.  If COND is
1435    non-NULL, conditionalize the execution of the instruction on COND
1436    being true.  */
1437
1438 static void
1439 emit_constant_insn (rtx cond, rtx pattern)
1440 {
1441   if (cond)
1442     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1443   emit_insn (pattern);
1444 }
1445
1446 /* As above, but extra parameter GENERATE which, if clear, suppresses
1447    RTL generation.  */
1448
1449 static int
1450 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1451                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1452                   int generate)
1453 {
1454   int can_invert = 0;
1455   int can_negate = 0;
1456   int can_negate_initial = 0;
1457   int can_shift = 0;
1458   int i;
1459   int num_bits_set = 0;
1460   int set_sign_bit_copies = 0;
1461   int clear_sign_bit_copies = 0;
1462   int clear_zero_bit_copies = 0;
1463   int set_zero_bit_copies = 0;
1464   int insns = 0;
1465   unsigned HOST_WIDE_INT temp1, temp2;
1466   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1467
1468   /* Find out which operations are safe for a given CODE.  Also do a quick
1469      check for degenerate cases; these can occur when DImode operations
1470      are split.  */
1471   switch (code)
1472     {
1473     case SET:
1474       can_invert = 1;
1475       can_shift = 1;
1476       can_negate = 1;
1477       break;
1478
1479     case PLUS:
1480       can_negate = 1;
1481       can_negate_initial = 1;
1482       break;
1483
1484     case IOR:
1485       if (remainder == 0xffffffff)
1486         {
1487           if (generate)
1488             emit_constant_insn (cond,
1489                                 gen_rtx_SET (VOIDmode, target,
1490                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1491           return 1;
1492         }
1493       if (remainder == 0)
1494         {
1495           if (reload_completed && rtx_equal_p (target, source))
1496             return 0;
1497           if (generate)
1498             emit_constant_insn (cond,
1499                                 gen_rtx_SET (VOIDmode, target, source));
1500           return 1;
1501         }
1502       break;
1503
1504     case AND:
1505       if (remainder == 0)
1506         {
1507           if (generate)
1508             emit_constant_insn (cond,
1509                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1510           return 1;
1511         }
1512       if (remainder == 0xffffffff)
1513         {
1514           if (reload_completed && rtx_equal_p (target, source))
1515             return 0;
1516           if (generate)
1517             emit_constant_insn (cond,
1518                                 gen_rtx_SET (VOIDmode, target, source));
1519           return 1;
1520         }
1521       can_invert = 1;
1522       break;
1523
1524     case XOR:
1525       if (remainder == 0)
1526         {
1527           if (reload_completed && rtx_equal_p (target, source))
1528             return 0;
1529           if (generate)
1530             emit_constant_insn (cond,
1531                                 gen_rtx_SET (VOIDmode, target, source));
1532           return 1;
1533         }
1534       if (remainder == 0xffffffff)
1535         {
1536           if (generate)
1537             emit_constant_insn (cond,
1538                                 gen_rtx_SET (VOIDmode, target,
1539                                              gen_rtx_NOT (mode, source)));
1540           return 1;
1541         }
1542
1543       /* We don't know how to handle this yet below.  */
1544       abort ();
1545
1546     case MINUS:
1547       /* We treat MINUS as (val - source), since (source - val) is always
1548          passed as (source + (-val)).  */
1549       if (remainder == 0)
1550         {
1551           if (generate)
1552             emit_constant_insn (cond,
1553                                 gen_rtx_SET (VOIDmode, target,
1554                                              gen_rtx_NEG (mode, source)));
1555           return 1;
1556         }
1557       if (const_ok_for_arm (val))
1558         {
1559           if (generate)
1560             emit_constant_insn (cond,
1561                                 gen_rtx_SET (VOIDmode, target, 
1562                                              gen_rtx_MINUS (mode, GEN_INT (val),
1563                                                             source)));
1564           return 1;
1565         }
1566       can_negate = 1;
1567
1568       break;
1569
1570     default:
1571       abort ();
1572     }
1573
1574   /* If we can do it in one insn get out quickly.  */
1575   if (const_ok_for_arm (val)
1576       || (can_negate_initial && const_ok_for_arm (-val))
1577       || (can_invert && const_ok_for_arm (~val)))
1578     {
1579       if (generate)
1580         emit_constant_insn (cond,
1581                             gen_rtx_SET (VOIDmode, target,
1582                                          (source 
1583                                           ? gen_rtx_fmt_ee (code, mode, source,
1584                                                             GEN_INT (val))
1585                                           : GEN_INT (val))));
1586       return 1;
1587     }
1588
1589   /* Calculate a few attributes that may be useful for specific
1590      optimizations.  */
1591   for (i = 31; i >= 0; i--)
1592     {
1593       if ((remainder & (1 << i)) == 0)
1594         clear_sign_bit_copies++;
1595       else
1596         break;
1597     }
1598
1599   for (i = 31; i >= 0; i--)
1600     {
1601       if ((remainder & (1 << i)) != 0)
1602         set_sign_bit_copies++;
1603       else
1604         break;
1605     }
1606
1607   for (i = 0; i <= 31; i++)
1608     {
1609       if ((remainder & (1 << i)) == 0)
1610         clear_zero_bit_copies++;
1611       else
1612         break;
1613     }
1614
1615   for (i = 0; i <= 31; i++)
1616     {
1617       if ((remainder & (1 << i)) != 0)
1618         set_zero_bit_copies++;
1619       else
1620         break;
1621     }
1622
1623   switch (code)
1624     {
1625     case SET:
1626       /* See if we can do this by sign_extending a constant that is known
1627          to be negative.  This is a good, way of doing it, since the shift
1628          may well merge into a subsequent insn.  */
1629       if (set_sign_bit_copies > 1)
1630         {
1631           if (const_ok_for_arm
1632               (temp1 = ARM_SIGN_EXTEND (remainder 
1633                                         << (set_sign_bit_copies - 1))))
1634             {
1635               if (generate)
1636                 {
1637                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1638                   emit_constant_insn (cond,
1639                                       gen_rtx_SET (VOIDmode, new_src, 
1640                                                    GEN_INT (temp1)));
1641                   emit_constant_insn (cond,
1642                                       gen_ashrsi3 (target, new_src, 
1643                                                    GEN_INT (set_sign_bit_copies - 1)));
1644                 }
1645               return 2;
1646             }
1647           /* For an inverted constant, we will need to set the low bits,
1648              these will be shifted out of harm's way.  */
1649           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1650           if (const_ok_for_arm (~temp1))
1651             {
1652               if (generate)
1653                 {
1654                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1655                   emit_constant_insn (cond,
1656                                       gen_rtx_SET (VOIDmode, new_src,
1657                                                    GEN_INT (temp1)));
1658                   emit_constant_insn (cond,
1659                                       gen_ashrsi3 (target, new_src, 
1660                                                    GEN_INT (set_sign_bit_copies - 1)));
1661                 }
1662               return 2;
1663             }
1664         }
1665
1666       /* See if we can generate this by setting the bottom (or the top)
1667          16 bits, and then shifting these into the other half of the
1668          word.  We only look for the simplest cases, to do more would cost
1669          too much.  Be careful, however, not to generate this when the
1670          alternative would take fewer insns.  */
1671       if (val & 0xffff0000)
1672         {
1673           temp1 = remainder & 0xffff0000;
1674           temp2 = remainder & 0x0000ffff;
1675
1676           /* Overlaps outside this range are best done using other methods.  */
1677           for (i = 9; i < 24; i++)
1678             {
1679               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1680                   && !const_ok_for_arm (temp2))
1681                 {
1682                   rtx new_src = (subtargets
1683                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1684                                  : target);
1685                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1686                                             source, subtargets, generate);
1687                   source = new_src;
1688                   if (generate)
1689                     emit_constant_insn 
1690                       (cond,
1691                        gen_rtx_SET
1692                        (VOIDmode, target,
1693                         gen_rtx_IOR (mode,
1694                                      gen_rtx_ASHIFT (mode, source,
1695                                                      GEN_INT (i)),
1696                                      source)));
1697                   return insns + 1;
1698                 }
1699             }
1700
1701           /* Don't duplicate cases already considered.  */
1702           for (i = 17; i < 24; i++)
1703             {
1704               if (((temp1 | (temp1 >> i)) == remainder)
1705                   && !const_ok_for_arm (temp1))
1706                 {
1707                   rtx new_src = (subtargets
1708                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1709                                  : target);
1710                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1711                                             source, subtargets, generate);
1712                   source = new_src;
1713                   if (generate)
1714                     emit_constant_insn
1715                       (cond,
1716                        gen_rtx_SET (VOIDmode, target,
1717                                     gen_rtx_IOR
1718                                     (mode,
1719                                      gen_rtx_LSHIFTRT (mode, source,
1720                                                        GEN_INT (i)),
1721                                      source)));
1722                   return insns + 1;
1723                 }
1724             }
1725         }
1726       break;
1727
1728     case IOR:
1729     case XOR:
1730       /* If we have IOR or XOR, and the constant can be loaded in a
1731          single instruction, and we can find a temporary to put it in,
1732          then this can be done in two instructions instead of 3-4.  */
1733       if (subtargets
1734           /* TARGET can't be NULL if SUBTARGETS is 0 */
1735           || (reload_completed && !reg_mentioned_p (target, source)))
1736         {
1737           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1738             {
1739               if (generate)
1740                 {
1741                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1742
1743                   emit_constant_insn (cond,
1744                                       gen_rtx_SET (VOIDmode, sub, 
1745                                                    GEN_INT (val)));
1746                   emit_constant_insn (cond,
1747                                       gen_rtx_SET (VOIDmode, target, 
1748                                                    gen_rtx_fmt_ee (code, mode,
1749                                                                    source, sub)));
1750                 }
1751               return 2;
1752             }
1753         }
1754
1755       if (code == XOR)
1756         break;
1757
1758       if (set_sign_bit_copies > 8
1759           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1760         {
1761           if (generate)
1762             {
1763               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1764               rtx shift = GEN_INT (set_sign_bit_copies);
1765
1766               emit_constant_insn 
1767                 (cond,
1768                  gen_rtx_SET (VOIDmode, sub,
1769                               gen_rtx_NOT (mode, 
1770                                            gen_rtx_ASHIFT (mode,
1771                                                            source, 
1772                                                            shift))));
1773               emit_constant_insn 
1774                 (cond,
1775                  gen_rtx_SET (VOIDmode, target,
1776                               gen_rtx_NOT (mode,
1777                                            gen_rtx_LSHIFTRT (mode, sub,
1778                                                              shift))));
1779             }
1780           return 2;
1781         }
1782
1783       if (set_zero_bit_copies > 8
1784           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1785         {
1786           if (generate)
1787             {
1788               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1789               rtx shift = GEN_INT (set_zero_bit_copies);
1790
1791               emit_constant_insn
1792                 (cond,
1793                  gen_rtx_SET (VOIDmode, sub,
1794                               gen_rtx_NOT (mode,
1795                                            gen_rtx_LSHIFTRT (mode,
1796                                                              source,
1797                                                              shift))));
1798               emit_constant_insn 
1799                 (cond,
1800                  gen_rtx_SET (VOIDmode, target,
1801                               gen_rtx_NOT (mode,
1802                                            gen_rtx_ASHIFT (mode, sub,
1803                                                            shift))));
1804             }
1805           return 2;
1806         }
1807
1808       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1809         {
1810           if (generate)
1811             {
1812               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1813               emit_constant_insn (cond,
1814                                   gen_rtx_SET (VOIDmode, sub,
1815                                                gen_rtx_NOT (mode, source)));
1816               source = sub;
1817               if (subtargets)
1818                 sub = gen_reg_rtx (mode);
1819               emit_constant_insn (cond,
1820                                   gen_rtx_SET (VOIDmode, sub,
1821                                                gen_rtx_AND (mode, source, 
1822                                                             GEN_INT (temp1))));
1823               emit_constant_insn (cond,
1824                                   gen_rtx_SET (VOIDmode, target,
1825                                                gen_rtx_NOT (mode, sub)));
1826             }
1827           return 3;
1828         }
1829       break;
1830
1831     case AND:
1832       /* See if two shifts will do 2 or more insn's worth of work.  */
1833       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1834         {
1835           HOST_WIDE_INT shift_mask = ((0xffffffff
1836                                        << (32 - clear_sign_bit_copies))
1837                                       & 0xffffffff);
1838
1839           if ((remainder | shift_mask) != 0xffffffff)
1840             {
1841               if (generate)
1842                 {
1843                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1844                   insns = arm_gen_constant (AND, mode, cond, 
1845                                             remainder | shift_mask,
1846                                             new_src, source, subtargets, 1);
1847                   source = new_src;
1848                 }
1849               else
1850                 {
1851                   rtx targ = subtargets ? NULL_RTX : target;
1852                   insns = arm_gen_constant (AND, mode, cond,
1853                                             remainder | shift_mask,
1854                                             targ, source, subtargets, 0);
1855                 }
1856             }
1857
1858           if (generate)
1859             {
1860               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1861               rtx shift = GEN_INT (clear_sign_bit_copies);
1862
1863               emit_insn (gen_ashlsi3 (new_src, source, shift));
1864               emit_insn (gen_lshrsi3 (target, new_src, shift));
1865             }
1866
1867           return insns + 2;
1868         }
1869
1870       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
1871         {
1872           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
1873           
1874           if ((remainder | shift_mask) != 0xffffffff)
1875             {
1876               if (generate)
1877                 {
1878                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1879
1880                   insns = arm_gen_constant (AND, mode, cond,
1881                                             remainder | shift_mask,
1882                                             new_src, source, subtargets, 1);
1883                   source = new_src;
1884                 }
1885               else
1886                 {
1887                   rtx targ = subtargets ? NULL_RTX : target;
1888
1889                   insns = arm_gen_constant (AND, mode, cond,
1890                                             remainder | shift_mask,
1891                                             targ, source, subtargets, 0);
1892                 }
1893             }
1894
1895           if (generate)
1896             {
1897               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1898               rtx shift = GEN_INT (clear_zero_bit_copies);
1899
1900               emit_insn (gen_lshrsi3 (new_src, source, shift));
1901               emit_insn (gen_ashlsi3 (target, new_src, shift));
1902             }
1903
1904           return insns + 2;
1905         }
1906
1907       break;
1908
1909     default:
1910       break;
1911     }
1912
1913   for (i = 0; i < 32; i++)
1914     if (remainder & (1 << i))
1915       num_bits_set++;
1916
1917   if (code == AND || (can_invert && num_bits_set > 16))
1918     remainder = (~remainder) & 0xffffffff;
1919   else if (code == PLUS && num_bits_set > 16)
1920     remainder = (-remainder) & 0xffffffff;
1921   else
1922     {
1923       can_invert = 0;
1924       can_negate = 0;
1925     }
1926
1927   /* Now try and find a way of doing the job in either two or three
1928      instructions.
1929      We start by looking for the largest block of zeros that are aligned on
1930      a 2-bit boundary, we then fill up the temps, wrapping around to the
1931      top of the word when we drop off the bottom.
1932      In the worst case this code should produce no more than four insns.  */
1933   {
1934     int best_start = 0;
1935     int best_consecutive_zeros = 0;
1936
1937     for (i = 0; i < 32; i += 2)
1938       {
1939         int consecutive_zeros = 0;
1940
1941         if (!(remainder & (3 << i)))
1942           {
1943             while ((i < 32) && !(remainder & (3 << i)))
1944               {
1945                 consecutive_zeros += 2;
1946                 i += 2;
1947               }
1948             if (consecutive_zeros > best_consecutive_zeros)
1949               {
1950                 best_consecutive_zeros = consecutive_zeros;
1951                 best_start = i - consecutive_zeros;
1952               }
1953             i -= 2;
1954           }
1955       }
1956
1957     /* So long as it won't require any more insns to do so, it's
1958        desirable to emit a small constant (in bits 0...9) in the last
1959        insn.  This way there is more chance that it can be combined with
1960        a later addressing insn to form a pre-indexed load or store
1961        operation.  Consider:
1962
1963                *((volatile int *)0xe0000100) = 1;
1964                *((volatile int *)0xe0000110) = 2;
1965
1966        We want this to wind up as:
1967
1968                 mov rA, #0xe0000000
1969                 mov rB, #1
1970                 str rB, [rA, #0x100]
1971                 mov rB, #2
1972                 str rB, [rA, #0x110]
1973
1974        rather than having to synthesize both large constants from scratch.
1975
1976        Therefore, we calculate how many insns would be required to emit
1977        the constant starting from `best_start', and also starting from 
1978        zero (ie with bit 31 first to be output).  If `best_start' doesn't 
1979        yield a shorter sequence, we may as well use zero.  */
1980     if (best_start != 0
1981         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
1982         && (count_insns_for_constant (remainder, 0) <= 
1983             count_insns_for_constant (remainder, best_start)))
1984       best_start = 0;
1985
1986     /* Now start emitting the insns.  */
1987     i = best_start;
1988     do
1989       {
1990         int end;
1991
1992         if (i <= 0)
1993           i += 32;
1994         if (remainder & (3 << (i - 2)))
1995           {
1996             end = i - 8;
1997             if (end < 0)
1998               end += 32;
1999             temp1 = remainder & ((0x0ff << end)
2000                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2001             remainder &= ~temp1;
2002
2003             if (generate)
2004               {
2005                 rtx new_src, temp1_rtx;
2006
2007                 if (code == SET || code == MINUS)
2008                   {
2009                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2010                     if (can_invert && code != MINUS)
2011                       temp1 = ~temp1;
2012                   }
2013                 else
2014                   {
2015                     if (remainder && subtargets)
2016                       new_src = gen_reg_rtx (mode);
2017                     else
2018                       new_src = target;
2019                     if (can_invert)
2020                       temp1 = ~temp1;
2021                     else if (can_negate)
2022                       temp1 = -temp1;
2023                   }
2024
2025                 temp1 = trunc_int_for_mode (temp1, mode);
2026                 temp1_rtx = GEN_INT (temp1);
2027
2028                 if (code == SET)
2029                   ;
2030                 else if (code == MINUS)
2031                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2032                 else
2033                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2034
2035                 emit_constant_insn (cond,
2036                                     gen_rtx_SET (VOIDmode, new_src, 
2037                                                  temp1_rtx));
2038                 source = new_src;
2039               }
2040
2041             if (code == SET)
2042               {
2043                 can_invert = 0;
2044                 code = PLUS;
2045               }
2046             else if (code == MINUS)
2047               code = PLUS;
2048
2049             insns++;
2050             i -= 6;
2051           }
2052         i -= 2;
2053       }
2054     while (remainder);
2055   }
2056
2057   return insns;
2058 }
2059
2060 /* Canonicalize a comparison so that we are more likely to recognize it.
2061    This can be done for a few constant compares, where we can make the
2062    immediate value easier to load.  */
2063
2064 enum rtx_code
2065 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2066 {
2067   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2068
2069   switch (code)
2070     {
2071     case EQ:
2072     case NE:
2073       return code;
2074
2075     case GT:
2076     case LE:
2077       if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2078           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2079         {
2080           *op1 = GEN_INT (i + 1);
2081           return code == GT ? GE : LT;
2082         }
2083       break;
2084
2085     case GE:
2086     case LT:
2087       if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2088           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2089         {
2090           *op1 = GEN_INT (i - 1);
2091           return code == GE ? GT : LE;
2092         }
2093       break;
2094
2095     case GTU:
2096     case LEU:
2097       if (i != ~((unsigned HOST_WIDE_INT) 0)
2098           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2099         {
2100           *op1 = GEN_INT (i + 1);
2101           return code == GTU ? GEU : LTU;
2102         }
2103       break;
2104
2105     case GEU:
2106     case LTU:
2107       if (i != 0
2108           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2109         {
2110           *op1 = GEN_INT (i - 1);
2111           return code == GEU ? GTU : LEU;
2112         }
2113       break;
2114
2115     default:
2116       abort ();
2117     }
2118
2119   return code;
2120 }
2121
2122
2123 /* Define how to find the value returned by a function.  */
2124
2125 rtx arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2126 {
2127   enum machine_mode mode;
2128   int unsignedp ATTRIBUTE_UNUSED;
2129   rtx r ATTRIBUTE_UNUSED;
2130
2131   
2132   mode = TYPE_MODE (type);
2133   /* Promote integer types.  */
2134   if (INTEGRAL_TYPE_P (type))
2135     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2136   return LIBCALL_VALUE(mode);
2137 }
2138
2139
2140 /* Decide whether a type should be returned in memory (true)
2141    or in a register (false).  This is called by the macro
2142    RETURN_IN_MEMORY.  */
2143 int
2144 arm_return_in_memory (tree type)
2145 {
2146   HOST_WIDE_INT size;
2147
2148   if (!AGGREGATE_TYPE_P (type))
2149     /* All simple types are returned in registers.  */
2150     return 0;
2151
2152   size = int_size_in_bytes (type);
2153
2154   if (arm_abi != ARM_ABI_APCS)
2155     {
2156       /* ATPCS and later return aggregate types in memory only if they are
2157          larger than a word (or are variable size).  */
2158       return (size < 0 || size > UNITS_PER_WORD);
2159     }
2160   
2161   /* For the arm-wince targets we choose to be compatible with Microsoft's
2162      ARM and Thumb compilers, which always return aggregates in memory.  */
2163 #ifndef ARM_WINCE
2164   /* All structures/unions bigger than one word are returned in memory.
2165      Also catch the case where int_size_in_bytes returns -1.  In this case
2166      the aggregate is either huge or of variable size, and in either case
2167      we will want to return it via memory and not in a register.  */
2168   if (size < 0 || size > UNITS_PER_WORD)
2169     return 1;
2170   
2171   if (TREE_CODE (type) == RECORD_TYPE)
2172     {
2173       tree field;
2174
2175       /* For a struct the APCS says that we only return in a register
2176          if the type is 'integer like' and every addressable element
2177          has an offset of zero.  For practical purposes this means
2178          that the structure can have at most one non bit-field element
2179          and that this element must be the first one in the structure.  */
2180       
2181       /* Find the first field, ignoring non FIELD_DECL things which will
2182          have been created by C++.  */
2183       for (field = TYPE_FIELDS (type);
2184            field && TREE_CODE (field) != FIELD_DECL;
2185            field = TREE_CHAIN (field))
2186         continue;
2187       
2188       if (field == NULL)
2189         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2190
2191       /* Check that the first field is valid for returning in a register.  */
2192
2193       /* ... Floats are not allowed */
2194       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2195         return 1;
2196
2197       /* ... Aggregates that are not themselves valid for returning in
2198          a register are not allowed.  */
2199       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2200         return 1;
2201
2202       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2203          since they are not addressable.  */
2204       for (field = TREE_CHAIN (field);
2205            field;
2206            field = TREE_CHAIN (field))
2207         {
2208           if (TREE_CODE (field) != FIELD_DECL)
2209             continue;
2210           
2211           if (!DECL_BIT_FIELD_TYPE (field))
2212             return 1;
2213         }
2214
2215       return 0;
2216     }
2217   
2218   if (TREE_CODE (type) == UNION_TYPE)
2219     {
2220       tree field;
2221
2222       /* Unions can be returned in registers if every element is
2223          integral, or can be returned in an integer register.  */
2224       for (field = TYPE_FIELDS (type);
2225            field;
2226            field = TREE_CHAIN (field))
2227         {
2228           if (TREE_CODE (field) != FIELD_DECL)
2229             continue;
2230
2231           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2232             return 1;
2233           
2234           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2235             return 1;
2236         }
2237       
2238       return 0;
2239     }
2240 #endif /* not ARM_WINCE */  
2241   
2242   /* Return all other types in memory.  */
2243   return 1;
2244 }
2245
2246 /* Indicate whether or not words of a double are in big-endian order.  */
2247
2248 int
2249 arm_float_words_big_endian (void)
2250 {
2251   if (TARGET_MAVERICK)
2252     return 0;
2253
2254   /* For FPA, float words are always big-endian.  For VFP, floats words
2255      follow the memory system mode.  */
2256
2257   if (TARGET_FPA)
2258     {
2259       return 1;
2260     }
2261
2262   if (TARGET_VFP)
2263     return (TARGET_BIG_END ? 1 : 0);
2264
2265   return 1;
2266 }
2267
2268 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2269    for a call to a function whose data type is FNTYPE.
2270    For a library call, FNTYPE is NULL.  */
2271 void
2272 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype, 
2273                           rtx libname  ATTRIBUTE_UNUSED,
2274                           tree fndecl ATTRIBUTE_UNUSED)
2275 {
2276   /* On the ARM, the offset starts at 0.  */
2277   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2278   pcum->iwmmxt_nregs = 0;
2279   pcum->can_split = true;
2280   
2281   pcum->call_cookie = CALL_NORMAL;
2282
2283   if (TARGET_LONG_CALLS)
2284     pcum->call_cookie = CALL_LONG;
2285     
2286   /* Check for long call/short call attributes.  The attributes
2287      override any command line option.  */
2288   if (fntype)
2289     {
2290       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2291         pcum->call_cookie = CALL_SHORT;
2292       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2293         pcum->call_cookie = CALL_LONG;
2294     }
2295
2296   /* Varargs vectors are treated the same as long long.
2297      named_count avoids having to change the way arm handles 'named' */
2298   pcum->named_count = 0;
2299   pcum->nargs = 0;
2300
2301   if (TARGET_REALLY_IWMMXT && fntype)
2302     {
2303       tree fn_arg;
2304
2305       for (fn_arg = TYPE_ARG_TYPES (fntype);
2306            fn_arg;
2307            fn_arg = TREE_CHAIN (fn_arg))
2308         pcum->named_count += 1;
2309
2310       if (! pcum->named_count)
2311         pcum->named_count = INT_MAX;
2312     }
2313 }
2314
2315
2316 /* Return true if mode/type need doubleword alignment.  */
2317 bool
2318 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2319 {
2320   return (mode == DImode
2321           || mode == DFmode
2322           || VECTOR_MODE_SUPPORTED_P (mode)
2323           || (mode == BLKmode
2324               && TYPE_ALIGN (type) > PARM_BOUNDARY));
2325 }
2326
2327
2328 /* Determine where to put an argument to a function.
2329    Value is zero to push the argument on the stack,
2330    or a hard register in which to store the argument.
2331
2332    MODE is the argument's machine mode.
2333    TYPE is the data type of the argument (as a tree).
2334     This is null for libcalls where that information may
2335     not be available.
2336    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2337     the preceding args and about the function being called.
2338    NAMED is nonzero if this argument is a named parameter
2339     (otherwise it is an extra parameter matching an ellipsis).  */
2340
2341 rtx
2342 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2343                   tree type, int named)
2344 {
2345   int nregs;
2346
2347   /* Varargs vectors are treated the same as long long.
2348      named_count avoids having to change the way arm handles 'named' */
2349   if (TARGET_IWMMXT_ABI
2350       && VECTOR_MODE_SUPPORTED_P (mode)
2351       && pcum->named_count > pcum->nargs + 1)
2352     {
2353       if (pcum->iwmmxt_nregs <= 9)
2354         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2355       else
2356         {
2357           pcum->can_split = false;
2358           return NULL_RTX;
2359         }
2360     }
2361
2362   /* Put doubleword aligned quantities in even register pairs.  */
2363   if (pcum->nregs & 1
2364       && ARM_DOUBLEWORD_ALIGN
2365       && arm_needs_doubleword_align (mode, type))
2366     pcum->nregs++;
2367
2368   if (mode == VOIDmode)
2369     /* Compute operand 2 of the call insn.  */
2370     return GEN_INT (pcum->call_cookie);
2371
2372   /* Only allow splitting an arg between regs and memory if all preceding
2373      args were allocated to regs.  For args passed by reference we only count
2374      the reference pointer.  */
2375   if (pcum->can_split)
2376     nregs = 1;
2377   else
2378     nregs = ARM_NUM_REGS2 (mode, type);
2379
2380   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2381     return NULL_RTX;
2382   
2383   return gen_rtx_REG (mode, pcum->nregs);
2384 }
2385
2386 /* Variable sized types are passed by reference.  This is a GCC
2387    extension to the ARM ABI.  */
2388
2389 int
2390 arm_function_arg_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2391                                     enum machine_mode mode ATTRIBUTE_UNUSED,
2392                                     tree type, int named ATTRIBUTE_UNUSED)
2393 {
2394   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2395 }
2396
2397 /* Implement va_arg.  */
2398
2399 rtx
2400 arm_va_arg (tree valist, tree type)
2401 {
2402   int align;
2403
2404   /* Variable sized types are passed by reference.  */
2405   if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
2406     {
2407       rtx addr = std_expand_builtin_va_arg (valist, build_pointer_type (type));
2408       return gen_rtx_MEM (ptr_mode, force_reg (Pmode, addr));
2409     }
2410
2411   align = FUNCTION_ARG_BOUNDARY (TYPE_MODE (type), type);
2412   if (align > PARM_BOUNDARY)
2413     {
2414       tree mask;
2415       tree t;
2416
2417       /* Maintain 64-bit alignment of the valist pointer by
2418          constructing:   valist = ((valist + (8 - 1)) & -8).  */
2419       mask = build_int_2 (- (align / BITS_PER_UNIT), -1);
2420       t = build_int_2 ((align / BITS_PER_UNIT) - 1, 0);
2421       t = build (PLUS_EXPR,    TREE_TYPE (valist), valist, t);
2422       t = build (BIT_AND_EXPR, TREE_TYPE (t), t, mask);
2423       t = build (MODIFY_EXPR,  TREE_TYPE (valist), valist, t);
2424       TREE_SIDE_EFFECTS (t) = 1;
2425       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2426
2427       /* This is to stop the combine pass optimizing
2428          away the alignment adjustment.  */
2429       mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
2430     }
2431
2432   return std_expand_builtin_va_arg (valist, type);
2433 }
2434 \f
2435 /* Encode the current state of the #pragma [no_]long_calls.  */
2436 typedef enum
2437 {
2438   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2439   LONG,         /* #pragma long_calls is in effect.  */
2440   SHORT         /* #pragma no_long_calls is in effect.  */
2441 } arm_pragma_enum;
2442
2443 static arm_pragma_enum arm_pragma_long_calls = OFF;
2444
2445 void
2446 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2447 {
2448   arm_pragma_long_calls = LONG;
2449 }
2450
2451 void
2452 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2453 {
2454   arm_pragma_long_calls = SHORT;
2455 }
2456
2457 void
2458 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2459 {
2460   arm_pragma_long_calls = OFF;
2461 }
2462 \f
2463 /* Table of machine attributes.  */
2464 const struct attribute_spec arm_attribute_table[] =
2465 {
2466   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2467   /* Function calls made to this symbol must be done indirectly, because
2468      it may lie outside of the 26 bit addressing range of a normal function
2469      call.  */
2470   { "long_call",    0, 0, false, true,  true,  NULL },
2471   /* Whereas these functions are always known to reside within the 26 bit
2472      addressing range.  */
2473   { "short_call",   0, 0, false, true,  true,  NULL },
2474   /* Interrupt Service Routines have special prologue and epilogue requirements.  */ 
2475   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2476   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2477   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2478 #ifdef ARM_PE
2479   /* ARM/PE has three new attributes:
2480      interfacearm - ?
2481      dllexport - for exporting a function/variable that will live in a dll
2482      dllimport - for importing a function/variable from a dll
2483
2484      Microsoft allows multiple declspecs in one __declspec, separating
2485      them with spaces.  We do NOT support this.  Instead, use __declspec
2486      multiple times.
2487   */
2488   { "dllimport",    0, 0, true,  false, false, NULL },
2489   { "dllexport",    0, 0, true,  false, false, NULL },
2490   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2491 #endif
2492   { NULL,           0, 0, false, false, false, NULL }
2493 };
2494
2495 /* Handle an attribute requiring a FUNCTION_DECL;
2496    arguments as in struct attribute_spec.handler.  */
2497 static tree
2498 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2499                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2500 {
2501   if (TREE_CODE (*node) != FUNCTION_DECL)
2502     {
2503       warning ("`%s' attribute only applies to functions",
2504                IDENTIFIER_POINTER (name));
2505       *no_add_attrs = true;
2506     }
2507
2508   return NULL_TREE;
2509 }
2510
2511 /* Handle an "interrupt" or "isr" attribute;
2512    arguments as in struct attribute_spec.handler.  */
2513 static tree
2514 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2515                           bool *no_add_attrs)
2516 {
2517   if (DECL_P (*node))
2518     {
2519       if (TREE_CODE (*node) != FUNCTION_DECL)
2520         {
2521           warning ("`%s' attribute only applies to functions",
2522                    IDENTIFIER_POINTER (name));
2523           *no_add_attrs = true;
2524         }
2525       /* FIXME: the argument if any is checked for type attributes;
2526          should it be checked for decl ones?  */
2527     }
2528   else
2529     {
2530       if (TREE_CODE (*node) == FUNCTION_TYPE
2531           || TREE_CODE (*node) == METHOD_TYPE)
2532         {
2533           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2534             {
2535               warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2536               *no_add_attrs = true;
2537             }
2538         }
2539       else if (TREE_CODE (*node) == POINTER_TYPE
2540                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2541                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2542                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2543         {
2544           *node = build_type_copy (*node);
2545           TREE_TYPE (*node) = build_type_attribute_variant
2546             (TREE_TYPE (*node),
2547              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2548           *no_add_attrs = true;
2549         }
2550       else
2551         {
2552           /* Possibly pass this attribute on from the type to a decl.  */
2553           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2554                        | (int) ATTR_FLAG_FUNCTION_NEXT
2555                        | (int) ATTR_FLAG_ARRAY_NEXT))
2556             {
2557               *no_add_attrs = true;
2558               return tree_cons (name, args, NULL_TREE);
2559             }
2560           else
2561             {
2562               warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2563             }
2564         }
2565     }
2566
2567   return NULL_TREE;
2568 }
2569
2570 /* Return 0 if the attributes for two types are incompatible, 1 if they
2571    are compatible, and 2 if they are nearly compatible (which causes a
2572    warning to be generated).  */
2573 static int
2574 arm_comp_type_attributes (tree type1, tree type2)
2575 {
2576   int l1, l2, s1, s2;
2577   
2578   /* Check for mismatch of non-default calling convention.  */
2579   if (TREE_CODE (type1) != FUNCTION_TYPE)
2580     return 1;
2581
2582   /* Check for mismatched call attributes.  */
2583   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2584   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2585   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2586   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2587
2588   /* Only bother to check if an attribute is defined.  */
2589   if (l1 | l2 | s1 | s2)
2590     {
2591       /* If one type has an attribute, the other must have the same attribute.  */
2592       if ((l1 != l2) || (s1 != s2))
2593         return 0;
2594
2595       /* Disallow mixed attributes.  */
2596       if ((l1 & s2) || (l2 & s1))
2597         return 0;
2598     }
2599   
2600   /* Check for mismatched ISR attribute.  */
2601   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2602   if (! l1)
2603     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2604   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2605   if (! l2)
2606     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2607   if (l1 != l2)
2608     return 0;
2609
2610   return 1;
2611 }
2612
2613 /*  Encode long_call or short_call attribute by prefixing
2614     symbol name in DECL with a special character FLAG.  */
2615 void
2616 arm_encode_call_attribute (tree decl, int flag)
2617 {
2618   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2619   int          len = strlen (str);
2620   char *       newstr;
2621
2622   /* Do not allow weak functions to be treated as short call.  */
2623   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2624     return;
2625
2626   newstr = alloca (len + 2);
2627   newstr[0] = flag;
2628   strcpy (newstr + 1, str);
2629
2630   newstr = (char *) ggc_alloc_string (newstr, len + 1);
2631   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2632 }
2633
2634 /*  Assigns default attributes to newly defined type.  This is used to
2635     set short_call/long_call attributes for function types of
2636     functions defined inside corresponding #pragma scopes.  */
2637 static void
2638 arm_set_default_type_attributes (tree type)
2639 {
2640   /* Add __attribute__ ((long_call)) to all functions, when
2641      inside #pragma long_calls or __attribute__ ((short_call)),
2642      when inside #pragma no_long_calls.  */
2643   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2644     {
2645       tree type_attr_list, attr_name;
2646       type_attr_list = TYPE_ATTRIBUTES (type);
2647
2648       if (arm_pragma_long_calls == LONG)
2649         attr_name = get_identifier ("long_call");
2650       else if (arm_pragma_long_calls == SHORT)
2651         attr_name = get_identifier ("short_call");
2652       else
2653         return;
2654
2655       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2656       TYPE_ATTRIBUTES (type) = type_attr_list;
2657     }
2658 }
2659 \f
2660 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2661    defined within the current compilation unit.  If this cannot be
2662    determined, then 0 is returned.  */
2663 static int
2664 current_file_function_operand (rtx sym_ref)
2665 {
2666   /* This is a bit of a fib.  A function will have a short call flag
2667      applied to its name if it has the short call attribute, or it has
2668      already been defined within the current compilation unit.  */
2669   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2670     return 1;
2671
2672   /* The current function is always defined within the current compilation
2673      unit.  if it s a weak definition however, then this may not be the real
2674      definition of the function, and so we have to say no.  */
2675   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2676       && !DECL_WEAK (current_function_decl))
2677     return 1;
2678
2679   /* We cannot make the determination - default to returning 0.  */
2680   return 0;
2681 }
2682
2683 /* Return nonzero if a 32 bit "long_call" should be generated for
2684    this call.  We generate a long_call if the function:
2685
2686         a.  has an __attribute__((long call))
2687      or b.  is within the scope of a #pragma long_calls
2688      or c.  the -mlong-calls command line switch has been specified
2689
2690    However we do not generate a long call if the function:
2691    
2692         d.  has an __attribute__ ((short_call))
2693      or e.  is inside the scope of a #pragma no_long_calls
2694      or f.  has an __attribute__ ((section))
2695      or g.  is defined within the current compilation unit.
2696    
2697    This function will be called by C fragments contained in the machine
2698    description file.  CALL_REF and CALL_COOKIE correspond to the matched
2699    rtl operands.  CALL_SYMBOL is used to distinguish between
2700    two different callers of the function.  It is set to 1 in the
2701    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2702    and "call_value" patterns.  This is because of the difference in the
2703    SYM_REFs passed by these patterns.  */
2704 int
2705 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2706 {
2707   if (!call_symbol)
2708     {
2709       if (GET_CODE (sym_ref) != MEM)
2710         return 0;
2711
2712       sym_ref = XEXP (sym_ref, 0);
2713     }
2714
2715   if (GET_CODE (sym_ref) != SYMBOL_REF)
2716     return 0;
2717
2718   if (call_cookie & CALL_SHORT)
2719     return 0;
2720
2721   if (TARGET_LONG_CALLS && flag_function_sections)
2722     return 1;
2723   
2724   if (current_file_function_operand (sym_ref))
2725     return 0;
2726   
2727   return (call_cookie & CALL_LONG)
2728     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2729     || TARGET_LONG_CALLS;
2730 }
2731
2732 /* Return nonzero if it is ok to make a tail-call to DECL.  */
2733 static bool
2734 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
2735 {
2736   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2737
2738   if (cfun->machine->sibcall_blocked)
2739     return false;
2740
2741   /* Never tailcall something for which we have no decl, or if we
2742      are in Thumb mode.  */
2743   if (decl == NULL || TARGET_THUMB)
2744     return false;
2745
2746   /* Get the calling method.  */
2747   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2748     call_type = CALL_SHORT;
2749   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2750     call_type = CALL_LONG;
2751
2752   /* Cannot tail-call to long calls, since these are out of range of
2753      a branch instruction.  However, if not compiling PIC, we know
2754      we can reach the symbol if it is in this compilation unit.  */
2755   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2756     return false;
2757
2758   /* If we are interworking and the function is not declared static
2759      then we can't tail-call it unless we know that it exists in this 
2760      compilation unit (since it might be a Thumb routine).  */
2761   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2762     return false;
2763
2764   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
2765   if (IS_INTERRUPT (arm_current_func_type ()))
2766     return false;
2767
2768   /* Everything else is ok.  */
2769   return true;
2770 }
2771
2772 \f
2773 /* Addressing mode support functions.  */
2774
2775 /* Return nonzero if X is a legitimate immediate operand when compiling
2776    for PIC.  */
2777 int
2778 legitimate_pic_operand_p (rtx x)
2779 {
2780   if (CONSTANT_P (x)
2781       && flag_pic
2782       && (GET_CODE (x) == SYMBOL_REF
2783           || (GET_CODE (x) == CONST
2784               && GET_CODE (XEXP (x, 0)) == PLUS
2785               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2786     return 0;
2787
2788   return 1;
2789 }
2790
2791 rtx
2792 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
2793 {
2794   if (GET_CODE (orig) == SYMBOL_REF
2795       || GET_CODE (orig) == LABEL_REF)
2796     {
2797 #ifndef AOF_ASSEMBLER
2798       rtx pic_ref, address;
2799 #endif
2800       rtx insn;
2801       int subregs = 0;
2802
2803       if (reg == 0)
2804         {
2805           if (no_new_pseudos)
2806             abort ();
2807           else
2808             reg = gen_reg_rtx (Pmode);
2809
2810           subregs = 1;
2811         }
2812
2813 #ifdef AOF_ASSEMBLER
2814       /* The AOF assembler can generate relocations for these directly, and
2815          understands that the PIC register has to be added into the offset.  */
2816       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
2817 #else
2818       if (subregs)
2819         address = gen_reg_rtx (Pmode);
2820       else
2821         address = reg;
2822
2823       if (TARGET_ARM)
2824         emit_insn (gen_pic_load_addr_arm (address, orig));
2825       else
2826         emit_insn (gen_pic_load_addr_thumb (address, orig));
2827
2828       if ((GET_CODE (orig) == LABEL_REF
2829            || (GET_CODE (orig) == SYMBOL_REF && 
2830                SYMBOL_REF_LOCAL_P (orig)))
2831           && NEED_GOT_RELOC)
2832         pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
2833       else
2834         {
2835           pic_ref = gen_rtx_MEM (Pmode,
2836                                  gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
2837                                                address));
2838           RTX_UNCHANGING_P (pic_ref) = 1;
2839         }
2840
2841       insn = emit_move_insn (reg, pic_ref);
2842 #endif
2843       current_function_uses_pic_offset_table = 1;
2844       /* Put a REG_EQUAL note on this insn, so that it can be optimized
2845          by loop.  */
2846       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
2847                                             REG_NOTES (insn));
2848       return reg;
2849     }
2850   else if (GET_CODE (orig) == CONST)
2851     {
2852       rtx base, offset;
2853
2854       if (GET_CODE (XEXP (orig, 0)) == PLUS
2855           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
2856         return orig;
2857
2858       if (reg == 0)
2859         {
2860           if (no_new_pseudos)
2861             abort ();
2862           else
2863             reg = gen_reg_rtx (Pmode);
2864         }
2865
2866       if (GET_CODE (XEXP (orig, 0)) == PLUS)
2867         {
2868           base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2869           offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2870                                            base == reg ? 0 : reg);
2871         }
2872       else
2873         abort ();
2874
2875       if (GET_CODE (offset) == CONST_INT)
2876         {
2877           /* The base register doesn't really matter, we only want to
2878              test the index for the appropriate mode.  */
2879           if (!arm_legitimate_index_p (mode, offset, SET, 0))
2880             {
2881               if (!no_new_pseudos)
2882                 offset = force_reg (Pmode, offset);
2883               else
2884                 abort ();
2885             }
2886
2887           if (GET_CODE (offset) == CONST_INT)
2888             return plus_constant (base, INTVAL (offset));
2889         }
2890
2891       if (GET_MODE_SIZE (mode) > 4
2892           && (GET_MODE_CLASS (mode) == MODE_INT
2893               || TARGET_SOFT_FLOAT))
2894         {
2895           emit_insn (gen_addsi3 (reg, base, offset));
2896           return reg;
2897         }
2898
2899       return gen_rtx_PLUS (Pmode, base, offset);
2900     }
2901
2902   return orig;
2903 }
2904
2905 /* Generate code to load the PIC register.  PROLOGUE is true if
2906    called from arm_expand_prologue (in which case we want the 
2907    generated insns at the start of the function);  false if called
2908    by an exception receiver that needs the PIC register reloaded
2909    (in which case the insns are just dumped at the current location).  */
2910 void
2911 arm_finalize_pic (int prologue ATTRIBUTE_UNUSED)
2912 {
2913 #ifndef AOF_ASSEMBLER
2914   rtx l1, pic_tmp, pic_tmp2, seq, pic_rtx;
2915   rtx global_offset_table;
2916
2917   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
2918     return;
2919
2920   if (!flag_pic)
2921     abort ();
2922
2923   start_sequence ();
2924   l1 = gen_label_rtx ();
2925
2926   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
2927   /* On the ARM the PC register contains 'dot + 8' at the time of the
2928      addition, on the Thumb it is 'dot + 4'.  */
2929   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
2930   if (GOT_PCREL)
2931     pic_tmp2 = gen_rtx_CONST (VOIDmode,
2932                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
2933   else
2934     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
2935
2936   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
2937   
2938   if (TARGET_ARM)
2939     {
2940       emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
2941       emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
2942     }
2943   else
2944     {
2945       emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
2946       emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
2947     }
2948
2949   seq = get_insns ();
2950   end_sequence ();
2951   if (prologue)
2952     emit_insn_after (seq, get_insns ());
2953   else
2954     emit_insn (seq);
2955
2956   /* Need to emit this whether or not we obey regdecls,
2957      since setjmp/longjmp can cause life info to screw up.  */
2958   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
2959 #endif /* AOF_ASSEMBLER */
2960 }
2961
2962 /* Return nonzero if X is valid as an ARM state addressing register.  */
2963 static int
2964 arm_address_register_rtx_p (rtx x, int strict_p)
2965 {
2966   int regno;
2967
2968   if (GET_CODE (x) != REG)
2969     return 0;
2970
2971   regno = REGNO (x);
2972
2973   if (strict_p)
2974     return ARM_REGNO_OK_FOR_BASE_P (regno);
2975
2976   return (regno <= LAST_ARM_REGNUM
2977           || regno >= FIRST_PSEUDO_REGISTER
2978           || regno == FRAME_POINTER_REGNUM
2979           || regno == ARG_POINTER_REGNUM);
2980 }
2981
2982 /* Return nonzero if X is a valid ARM state address operand.  */
2983 int
2984 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
2985                           int strict_p)
2986 {
2987   if (arm_address_register_rtx_p (x, strict_p))
2988     return 1;
2989
2990   else if (GET_CODE (x) == POST_INC || GET_CODE (x) == PRE_DEC)
2991     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
2992
2993   else if ((GET_CODE (x) == POST_MODIFY || GET_CODE (x) == PRE_MODIFY)
2994            && GET_MODE_SIZE (mode) <= 4
2995            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
2996            && GET_CODE (XEXP (x, 1)) == PLUS
2997            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
2998     return arm_legitimate_index_p (mode, XEXP (XEXP (x, 1), 1), outer,
2999                                    strict_p);
3000
3001   /* After reload constants split into minipools will have addresses
3002      from a LABEL_REF.  */
3003   else if (reload_completed
3004            && (GET_CODE (x) == LABEL_REF
3005                || (GET_CODE (x) == CONST
3006                    && GET_CODE (XEXP (x, 0)) == PLUS
3007                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3008                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3009     return 1;
3010
3011   else if (mode == TImode)
3012     return 0;
3013
3014   else if (mode == DImode || (TARGET_SOFT_FLOAT && mode == DFmode))
3015     {
3016       if (GET_CODE (x) == PLUS
3017           && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3018           && GET_CODE (XEXP (x, 1)) == CONST_INT)
3019         {
3020           HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
3021
3022           if (val == 4 || val == -4 || val == -8)
3023             return 1;
3024         }
3025     }
3026
3027   else if (TARGET_HARD_FLOAT && TARGET_VFP && mode == DFmode)
3028     {
3029       if (GET_CODE (x) == PLUS
3030           && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3031           && GET_CODE (XEXP (x, 1)) == CONST_INT)
3032         {
3033           HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
3034
3035           /* ??? valid arm offsets are a subset of VFP offsets.
3036              For now only allow this subset.  Proper fix is to add an
3037              additional memory constraint for arm address modes.
3038              Alternatively allow full vfp addressing and let
3039              output_move_double fix it up with a sub-optimal sequence.  */
3040           if (val == 4 || val == -4 || val == -8)
3041             return 1;
3042         }
3043     }
3044
3045   else if (GET_CODE (x) == PLUS)
3046     {
3047       rtx xop0 = XEXP (x, 0);
3048       rtx xop1 = XEXP (x, 1);
3049
3050       return ((arm_address_register_rtx_p (xop0, strict_p)
3051                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3052               || (arm_address_register_rtx_p (xop1, strict_p)
3053                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3054     }
3055
3056 #if 0
3057   /* Reload currently can't handle MINUS, so disable this for now */
3058   else if (GET_CODE (x) == MINUS)
3059     {
3060       rtx xop0 = XEXP (x, 0);
3061       rtx xop1 = XEXP (x, 1);
3062
3063       return (arm_address_register_rtx_p (xop0, strict_p)
3064               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3065     }
3066 #endif
3067
3068   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3069            && GET_CODE (x) == SYMBOL_REF
3070            && CONSTANT_POOL_ADDRESS_P (x)
3071            && ! (flag_pic
3072                  && symbol_mentioned_p (get_pool_constant (x))))
3073     return 1;
3074
3075   else if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == POST_DEC)
3076            && (GET_MODE_SIZE (mode) <= 4)
3077            && arm_address_register_rtx_p (XEXP (x, 0), strict_p))
3078     return 1;
3079
3080   return 0;
3081 }
3082
3083 /* Return nonzero if INDEX is valid for an address index operand in
3084    ARM state.  */
3085 static int
3086 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3087                         int strict_p)
3088 {
3089   HOST_WIDE_INT range;
3090   enum rtx_code code = GET_CODE (index);
3091
3092   /* Standard coprocessor addressing modes.  */
3093   if (TARGET_HARD_FLOAT
3094       && (TARGET_FPA || TARGET_MAVERICK)
3095       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3096           || (TARGET_MAVERICK && mode == DImode)))
3097     return (code == CONST_INT && INTVAL (index) < 1024
3098             && INTVAL (index) > -1024
3099             && (INTVAL (index) & 3) == 0);
3100
3101   if (arm_address_register_rtx_p (index, strict_p)
3102       && GET_MODE_SIZE (mode) <= 4)
3103     return 1;
3104
3105   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3106     return (code == CONST_INT
3107             && INTVAL (index) < 256
3108             && INTVAL (index) > -256);
3109
3110   if (GET_MODE_SIZE (mode) <= 4
3111       && ! (arm_arch4
3112             && (mode == HImode
3113                 || (mode == QImode && outer == SIGN_EXTEND))))
3114     {
3115       if (code == MULT)
3116         {
3117           rtx xiop0 = XEXP (index, 0);
3118           rtx xiop1 = XEXP (index, 1);
3119
3120           return ((arm_address_register_rtx_p (xiop0, strict_p)
3121                    && power_of_two_operand (xiop1, SImode))
3122                   || (arm_address_register_rtx_p (xiop1, strict_p)
3123                       && power_of_two_operand (xiop0, SImode)));
3124         }
3125       else if (code == LSHIFTRT || code == ASHIFTRT
3126                || code == ASHIFT || code == ROTATERT)
3127         {
3128           rtx op = XEXP (index, 1);
3129
3130           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3131                   && GET_CODE (op) == CONST_INT
3132                   && INTVAL (op) > 0
3133                   && INTVAL (op) <= 31);
3134         }
3135     }
3136
3137   /* For ARM v4 we may be doing a sign-extend operation during the
3138      load.  */
3139   if (arm_arch4)
3140     {
3141       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3142         range = 256;
3143       else
3144         range = 4096;
3145     }
3146   else
3147     range = (mode == HImode) ? 4095 : 4096;
3148
3149   return (code == CONST_INT
3150           && INTVAL (index) < range
3151           && INTVAL (index) > -range);
3152 }
3153
3154 /* Return nonzero if X is valid as a Thumb state base register.  */
3155 static int
3156 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3157 {
3158   int regno;
3159
3160   if (GET_CODE (x) != REG)
3161     return 0;
3162
3163   regno = REGNO (x);
3164
3165   if (strict_p)
3166     return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3167
3168   return (regno <= LAST_LO_REGNUM
3169           || regno > LAST_VIRTUAL_REGISTER
3170           || regno == FRAME_POINTER_REGNUM
3171           || (GET_MODE_SIZE (mode) >= 4
3172               && (regno == STACK_POINTER_REGNUM
3173                   || regno >= FIRST_PSEUDO_REGISTER
3174                   || x == hard_frame_pointer_rtx
3175                   || x == arg_pointer_rtx)));
3176 }
3177
3178 /* Return nonzero if x is a legitimate index register.  This is the case
3179    for any base register that can access a QImode object.  */
3180 inline static int
3181 thumb_index_register_rtx_p (rtx x, int strict_p)
3182 {
3183   return thumb_base_register_rtx_p (x, QImode, strict_p);
3184 }
3185
3186 /* Return nonzero if x is a legitimate Thumb-state address.
3187  
3188    The AP may be eliminated to either the SP or the FP, so we use the
3189    least common denominator, e.g. SImode, and offsets from 0 to 64.
3190
3191    ??? Verify whether the above is the right approach.
3192
3193    ??? Also, the FP may be eliminated to the SP, so perhaps that
3194    needs special handling also.
3195
3196    ??? Look at how the mips16 port solves this problem.  It probably uses
3197    better ways to solve some of these problems.
3198
3199    Although it is not incorrect, we don't accept QImode and HImode
3200    addresses based on the frame pointer or arg pointer until the
3201    reload pass starts.  This is so that eliminating such addresses
3202    into stack based ones won't produce impossible code.  */
3203 int
3204 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3205 {
3206   /* ??? Not clear if this is right.  Experiment.  */
3207   if (GET_MODE_SIZE (mode) < 4
3208       && !(reload_in_progress || reload_completed)
3209       && (reg_mentioned_p (frame_pointer_rtx, x)
3210           || reg_mentioned_p (arg_pointer_rtx, x)
3211           || reg_mentioned_p (virtual_incoming_args_rtx, x)
3212           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3213           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3214           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3215     return 0;
3216
3217   /* Accept any base register.  SP only in SImode or larger.  */
3218   else if (thumb_base_register_rtx_p (x, mode, strict_p))
3219     return 1;
3220
3221   /* This is PC relative data before arm_reorg runs.  */
3222   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3223            && GET_CODE (x) == SYMBOL_REF
3224            && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3225     return 1;
3226
3227   /* This is PC relative data after arm_reorg runs.  */
3228   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3229            && (GET_CODE (x) == LABEL_REF
3230                || (GET_CODE (x) == CONST
3231                    && GET_CODE (XEXP (x, 0)) == PLUS
3232                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3233                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3234     return 1;
3235
3236   /* Post-inc indexing only supported for SImode and larger.  */
3237   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3238            && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3239     return 1;
3240
3241   else if (GET_CODE (x) == PLUS)
3242     {
3243       /* REG+REG address can be any two index registers.  */
3244       /* We disallow FRAME+REG addressing since we know that FRAME
3245          will be replaced with STACK, and SP relative addressing only
3246          permits SP+OFFSET.  */
3247       if (GET_MODE_SIZE (mode) <= 4
3248           && XEXP (x, 0) != frame_pointer_rtx
3249           && XEXP (x, 1) != frame_pointer_rtx
3250           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3251           && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3252         return 1;
3253
3254       /* REG+const has 5-7 bit offset for non-SP registers.  */
3255       else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3256                 || XEXP (x, 0) == arg_pointer_rtx)
3257                && GET_CODE (XEXP (x, 1)) == CONST_INT
3258                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3259         return 1;
3260
3261       /* REG+const has 10 bit offset for SP, but only SImode and
3262          larger is supported.  */
3263       /* ??? Should probably check for DI/DFmode overflow here
3264          just like GO_IF_LEGITIMATE_OFFSET does.  */
3265       else if (GET_CODE (XEXP (x, 0)) == REG
3266                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3267                && GET_MODE_SIZE (mode) >= 4
3268                && GET_CODE (XEXP (x, 1)) == CONST_INT
3269                && INTVAL (XEXP (x, 1)) >= 0
3270                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3271                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3272         return 1;
3273
3274       else if (GET_CODE (XEXP (x, 0)) == REG
3275                && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3276                && GET_MODE_SIZE (mode) >= 4
3277                && GET_CODE (XEXP (x, 1)) == CONST_INT
3278                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3279         return 1;
3280     }
3281
3282   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3283            && GET_MODE_SIZE (mode) == 4
3284            && GET_CODE (x) == SYMBOL_REF
3285            && CONSTANT_POOL_ADDRESS_P (x)
3286            && !(flag_pic
3287                 && symbol_mentioned_p (get_pool_constant (x))))
3288     return 1;
3289
3290   return 0;
3291 }
3292
3293 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3294    instruction of mode MODE.  */
3295 int
3296 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3297 {
3298   switch (GET_MODE_SIZE (mode))
3299     {
3300     case 1:
3301       return val >= 0 && val < 32;
3302
3303     case 2:
3304       return val >= 0 && val < 64 && (val & 1) == 0;
3305
3306     default:
3307       return (val >= 0
3308               && (val + GET_MODE_SIZE (mode)) <= 128
3309               && (val & 3) == 0);
3310     }
3311 }
3312
3313 /* Try machine-dependent ways of modifying an illegitimate address
3314    to be legitimate.  If we find one, return the new, valid address.  */
3315 rtx
3316 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3317 {
3318   if (GET_CODE (x) == PLUS)
3319     {
3320       rtx xop0 = XEXP (x, 0);
3321       rtx xop1 = XEXP (x, 1);
3322
3323       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3324         xop0 = force_reg (SImode, xop0);
3325
3326       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3327         xop1 = force_reg (SImode, xop1);
3328
3329       if (ARM_BASE_REGISTER_RTX_P (xop0)
3330           && GET_CODE (xop1) == CONST_INT)
3331         {
3332           HOST_WIDE_INT n, low_n;
3333           rtx base_reg, val;
3334           n = INTVAL (xop1);
3335
3336           /* VFP addressing modes actually allow greater offsets, but for
3337              now we just stick with the lowest common denominator.  */
3338           if (mode == DImode
3339               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3340             {
3341               low_n = n & 0x0f;
3342               n &= ~0x0f;
3343               if (low_n > 4)
3344                 {
3345                   n += 16;
3346                   low_n -= 16;
3347                 }
3348             }
3349           else
3350             {
3351               low_n = ((mode) == TImode ? 0
3352                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3353               n -= low_n;
3354             }
3355
3356           base_reg = gen_reg_rtx (SImode);
3357           val = force_operand (gen_rtx_PLUS (SImode, xop0,
3358                                              GEN_INT (n)), NULL_RTX);
3359           emit_move_insn (base_reg, val);
3360           x = (low_n == 0 ? base_reg
3361                : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3362         }
3363       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3364         x = gen_rtx_PLUS (SImode, xop0, xop1);
3365     }
3366
3367   /* XXX We don't allow MINUS any more -- see comment in
3368      arm_legitimate_address_p ().  */
3369   else if (GET_CODE (x) == MINUS)
3370     {
3371       rtx xop0 = XEXP (x, 0);
3372       rtx xop1 = XEXP (x, 1);
3373
3374       if (CONSTANT_P (xop0))
3375         xop0 = force_reg (SImode, xop0);
3376
3377       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3378         xop1 = force_reg (SImode, xop1);
3379
3380       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3381         x = gen_rtx_MINUS (SImode, xop0, xop1);
3382     }
3383
3384   if (flag_pic)
3385     {
3386       /* We need to find and carefully transform any SYMBOL and LABEL
3387          references; so go back to the original address expression.  */
3388       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3389
3390       if (new_x != orig_x)
3391         x = new_x;
3392     }
3393
3394   return x;
3395 }
3396
3397
3398 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3399    to be legitimate.  If we find one, return the new, valid address.  */
3400 rtx
3401 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3402 {
3403   if (GET_CODE (x) == PLUS
3404       && GET_CODE (XEXP (x, 1)) == CONST_INT
3405       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3406           || INTVAL (XEXP (x, 1)) < 0))
3407     {
3408       rtx xop0 = XEXP (x, 0);
3409       rtx xop1 = XEXP (x, 1);
3410       HOST_WIDE_INT offset = INTVAL (xop1);
3411
3412       /* Try and fold the offset into a biasing of the base register and
3413          then offsetting that.  Don't do this when optimizing for space
3414          since it can cause too many CSEs.  */
3415       if (optimize_size && offset >= 0
3416           && offset < 256 + 31 * GET_MODE_SIZE (mode))
3417         {
3418           HOST_WIDE_INT delta;
3419
3420           if (offset >= 256)
3421             delta = offset - (256 - GET_MODE_SIZE (mode));
3422           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3423             delta = 31 * GET_MODE_SIZE (mode);
3424           else
3425             delta = offset & (~31 * GET_MODE_SIZE (mode));
3426
3427           xop0 = force_operand (plus_constant (xop0, offset - delta),
3428                                 NULL_RTX);
3429           x = plus_constant (xop0, delta);
3430         }
3431       else if (offset < 0 && offset > -256)
3432         /* Small negative offsets are best done with a subtract before the
3433            dereference, forcing these into a register normally takes two
3434            instructions.  */
3435         x = force_operand (x, NULL_RTX);
3436       else
3437         {
3438           /* For the remaining cases, force the constant into a register.  */
3439           xop1 = force_reg (SImode, xop1);
3440           x = gen_rtx_PLUS (SImode, xop0, xop1);
3441         }
3442     }
3443   else if (GET_CODE (x) == PLUS
3444            && s_register_operand (XEXP (x, 1), SImode)
3445            && !s_register_operand (XEXP (x, 0), SImode))
3446     {
3447       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3448
3449       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3450     }
3451
3452   if (flag_pic)
3453     {
3454       /* We need to find and carefully transform any SYMBOL and LABEL
3455          references; so go back to the original address expression.  */
3456       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3457
3458       if (new_x != orig_x)
3459         x = new_x;
3460     }
3461
3462   return x;
3463 }
3464
3465 \f
3466
3467 #define REG_OR_SUBREG_REG(X)                                            \
3468   (GET_CODE (X) == REG                                                  \
3469    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3470
3471 #define REG_OR_SUBREG_RTX(X)                    \
3472    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3473
3474 #ifndef COSTS_N_INSNS
3475 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3476 #endif
3477 static inline int
3478 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3479 {
3480   enum machine_mode mode = GET_MODE (x);
3481
3482   switch (code)
3483     {
3484     case ASHIFT:
3485     case ASHIFTRT:
3486     case LSHIFTRT:
3487     case ROTATERT:      
3488     case PLUS:
3489     case MINUS:
3490     case COMPARE:
3491     case NEG:
3492     case NOT:   
3493       return COSTS_N_INSNS (1);
3494       
3495     case MULT:                                                  
3496       if (GET_CODE (XEXP (x, 1)) == CONST_INT)                  
3497         {                                                               
3498           int cycles = 0;                                               
3499           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3500           
3501           while (i)                                             
3502             {                                                   
3503               i >>= 2;                                          
3504               cycles++;                                         
3505             }                                                   
3506           return COSTS_N_INSNS (2) + cycles;                    
3507         }
3508       return COSTS_N_INSNS (1) + 16;
3509       
3510     case SET:                                                   
3511       return (COSTS_N_INSNS (1)                                 
3512               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)            
3513                      + GET_CODE (SET_DEST (x)) == MEM));
3514       
3515     case CONST_INT:                                             
3516       if (outer == SET)                                         
3517         {                                                       
3518           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)                
3519             return 0;                                           
3520           if (thumb_shiftable_const (INTVAL (x)))                       
3521             return COSTS_N_INSNS (2);                           
3522           return COSTS_N_INSNS (3);                             
3523         }                                                               
3524       else if ((outer == PLUS || outer == COMPARE)
3525                && INTVAL (x) < 256 && INTVAL (x) > -256)                
3526         return 0;
3527       else if (outer == AND
3528                && INTVAL (x) < 256 && INTVAL (x) >= -256)
3529         return COSTS_N_INSNS (1);
3530       else if (outer == ASHIFT || outer == ASHIFTRT             
3531                || outer == LSHIFTRT)                            
3532         return 0;                                                       
3533       return COSTS_N_INSNS (2);
3534       
3535     case CONST:                                                 
3536     case CONST_DOUBLE:                                          
3537     case LABEL_REF:                                             
3538     case SYMBOL_REF:                                            
3539       return COSTS_N_INSNS (3);
3540       
3541     case UDIV:
3542     case UMOD:
3543     case DIV:
3544     case MOD:
3545       return 100;
3546
3547     case TRUNCATE:
3548       return 99;
3549
3550     case AND:
3551     case XOR:
3552     case IOR: 
3553       /* XXX guess.  */
3554       return 8;
3555
3556     case ADDRESSOF:
3557     case MEM:
3558       /* XXX another guess.  */
3559       /* Memory costs quite a lot for the first word, but subsequent words
3560          load at the equivalent of a single insn each.  */
3561       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3562               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3563                  ? 4 : 0));
3564
3565     case IF_THEN_ELSE:
3566       /* XXX a guess.  */
3567       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3568         return 14;
3569       return 2;
3570
3571     case ZERO_EXTEND:
3572       /* XXX still guessing.  */
3573       switch (GET_MODE (XEXP (x, 0)))
3574         {
3575         case QImode:
3576           return (1 + (mode == DImode ? 4 : 0)
3577                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3578           
3579         case HImode:
3580           return (4 + (mode == DImode ? 4 : 0)
3581                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3582           
3583         case SImode:
3584           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3585       
3586         default:
3587           return 99;
3588         }
3589       
3590     default:
3591       return 99;
3592     }
3593 }
3594
3595
3596 /* Worker routine for arm_rtx_costs.  */
3597 static inline int
3598 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3599 {
3600   enum machine_mode mode = GET_MODE (x);
3601   enum rtx_code subcode;
3602   int extra_cost;
3603
3604   switch (code)
3605     {
3606     case MEM:
3607       /* Memory costs quite a lot for the first word, but subsequent words
3608          load at the equivalent of a single insn each.  */
3609       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3610               + (GET_CODE (x) == SYMBOL_REF
3611                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3612
3613     case DIV:
3614     case MOD:
3615     case UDIV:
3616     case UMOD:
3617       return optimize_size ? COSTS_N_INSNS (2) : 100;
3618
3619     case ROTATE:
3620       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3621         return 4;
3622       /* Fall through */
3623     case ROTATERT:
3624       if (mode != SImode)
3625         return 8;
3626       /* Fall through */
3627     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3628       if (mode == DImode)
3629         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3630                 + ((GET_CODE (XEXP (x, 0)) == REG 
3631                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3632                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3633                    ? 0 : 8));
3634       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3635                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3636                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3637                    ? 0 : 4)
3638               + ((GET_CODE (XEXP (x, 1)) == REG
3639                   || (GET_CODE (XEXP (x, 1)) == SUBREG
3640                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3641                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3642                  ? 0 : 4));
3643
3644     case MINUS:
3645       if (mode == DImode)
3646         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3647                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3648                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
3649                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3650                    ? 0 : 8));
3651
3652       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3653         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3654                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3655                           && arm_const_double_rtx (XEXP (x, 1))))
3656                      ? 0 : 8)
3657                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3658                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3659                         && arm_const_double_rtx (XEXP (x, 0))))
3660                    ? 0 : 8));
3661
3662       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3663             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3664             && REG_OR_SUBREG_REG (XEXP (x, 1))))
3665           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3666                || subcode == ASHIFTRT || subcode == LSHIFTRT
3667                || subcode == ROTATE || subcode == ROTATERT
3668                || (subcode == MULT
3669                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3670                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3671                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3672               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3673               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3674                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3675               && REG_OR_SUBREG_REG (XEXP (x, 0))))
3676         return 1;
3677       /* Fall through */
3678
3679     case PLUS: 
3680       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3681         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3682                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3683                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3684                         && arm_const_double_rtx (XEXP (x, 1))))
3685                    ? 0 : 8));
3686
3687       /* Fall through */
3688     case AND: case XOR: case IOR: 
3689       extra_cost = 0;
3690
3691       /* Normally the frame registers will be spilt into reg+const during
3692          reload, so it is a bad idea to combine them with other instructions,
3693          since then they might not be moved outside of loops.  As a compromise
3694          we allow integration with ops that have a constant as their second
3695          operand.  */
3696       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3697            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3698            && GET_CODE (XEXP (x, 1)) != CONST_INT)
3699           || (REG_OR_SUBREG_REG (XEXP (x, 0))
3700               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3701         extra_cost = 4;
3702
3703       if (mode == DImode)
3704         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3705                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3706                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3707                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3708                    ? 0 : 8));
3709
3710       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3711         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3712                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3713                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3714                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3715                    ? 0 : 4));
3716
3717       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3718         return (1 + extra_cost
3719                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3720                      || subcode == LSHIFTRT || subcode == ASHIFTRT
3721                      || subcode == ROTATE || subcode == ROTATERT
3722                      || (subcode == MULT
3723                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3724                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3725                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3726                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3727                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3728                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3729                    ? 0 : 4));
3730
3731       return 8;
3732
3733     case MULT:
3734       /* This should have been handled by the CPU specific routines.  */
3735       abort ();
3736
3737     case TRUNCATE:
3738       if (arm_arch3m && mode == SImode
3739           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
3740           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
3741           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
3742               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
3743           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
3744               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
3745         return 8;
3746       return 99;
3747
3748     case NEG:
3749       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3750         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
3751       /* Fall through */
3752     case NOT:
3753       if (mode == DImode)
3754         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3755
3756       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3757
3758     case IF_THEN_ELSE:
3759       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3760         return 14;
3761       return 2;
3762
3763     case COMPARE:
3764       return 1;
3765
3766     case ABS:
3767       return 4 + (mode == DImode ? 4 : 0);
3768
3769     case SIGN_EXTEND:
3770       if (GET_MODE (XEXP (x, 0)) == QImode)
3771         return (4 + (mode == DImode ? 4 : 0)
3772                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3773       /* Fall through */
3774     case ZERO_EXTEND:
3775       switch (GET_MODE (XEXP (x, 0)))
3776         {
3777         case QImode:
3778           return (1 + (mode == DImode ? 4 : 0)
3779                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3780
3781         case HImode:
3782           return (4 + (mode == DImode ? 4 : 0)
3783                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3784
3785         case SImode:
3786           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3787
3788         case V8QImode:
3789         case V4HImode:
3790         case V2SImode:
3791         case V4QImode:
3792         case V2HImode:
3793             return 1;
3794
3795         default:
3796           break;
3797         }
3798       abort ();
3799
3800     case CONST_INT:                                             
3801       if (const_ok_for_arm (INTVAL (x)))                        
3802         return outer == SET ? 2 : -1;                   
3803       else if (outer == AND                             
3804                && const_ok_for_arm (~INTVAL (x)))               
3805         return -1;                                              
3806       else if ((outer == COMPARE                        
3807                 || outer == PLUS || outer == MINUS)     
3808                && const_ok_for_arm (-INTVAL (x)))               
3809         return -1;                                              
3810       else                                                      
3811         return 5;
3812       
3813     case CONST:                                                         
3814     case LABEL_REF:                                             
3815     case SYMBOL_REF:                                            
3816       return 6;
3817       
3818     case CONST_DOUBLE:                                          
3819       if (arm_const_double_rtx (x))
3820         return outer == SET ? 2 : -1;                   
3821       else if ((outer == COMPARE || outer == PLUS)      
3822                && neg_const_double_rtx_ok_for_fpa (x))          
3823         return -1;                                              
3824       return 7;
3825       
3826     default:
3827       return 99;
3828     }
3829 }
3830
3831 /* RTX costs for cores with a slow MUL implementation.  */
3832
3833 static bool
3834 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
3835 {
3836   enum machine_mode mode = GET_MODE (x);
3837
3838   if (TARGET_THUMB)
3839     {
3840       *total = thumb_rtx_costs (x, code, outer_code);
3841       return true;
3842     }
3843   
3844   switch (code)
3845     {
3846     case MULT:
3847       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3848           || mode == DImode)
3849         {
3850           *total = 30;
3851           return true;
3852         }
3853
3854       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3855         {
3856           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3857                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
3858           int cost, const_ok = const_ok_for_arm (i);
3859           int j, booth_unit_size;
3860
3861           /* Tune as appropriate.  */ 
3862           cost = const_ok ? 4 : 8;
3863           booth_unit_size = 2;
3864           for (j = 0; i && j < 32; j += booth_unit_size)
3865             {
3866               i >>= booth_unit_size;
3867               cost += 2;
3868             }
3869
3870           *total = cost;
3871           return true;
3872         }
3873
3874       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3875                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3876       return true;
3877   
3878     default:
3879       *total = arm_rtx_costs_1 (x, code, outer_code);
3880       return true;
3881     }
3882 }
3883
3884
3885 /* RTX cost for cores with a fast multiply unit (M variants).  */
3886
3887 static bool
3888 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
3889 {
3890   enum machine_mode mode = GET_MODE (x);
3891
3892   if (TARGET_THUMB)
3893     {
3894       *total = thumb_rtx_costs (x, code, outer_code);
3895       return true;
3896     }
3897   
3898   switch (code)
3899     {
3900     case MULT:
3901       /* There is no point basing this on the tuning, since it is always the
3902          fast variant if it exists at all.  */
3903       if (mode == DImode
3904           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
3905           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
3906               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
3907         {
3908           *total = 8;
3909           return true;
3910         }
3911       
3912
3913       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3914           || mode == DImode)
3915         {
3916           *total = 30;
3917           return true;
3918         }
3919
3920       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3921         {
3922           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3923                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
3924           int cost, const_ok = const_ok_for_arm (i);
3925           int j, booth_unit_size;
3926
3927           /* Tune as appropriate.  */ 
3928           cost = const_ok ? 4 : 8;
3929           booth_unit_size = 8;
3930           for (j = 0; i && j < 32; j += booth_unit_size)
3931             {
3932               i >>= booth_unit_size;
3933               cost += 2;
3934             }
3935
3936           *total = cost;
3937           return true;
3938         }
3939
3940       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
3941                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
3942       return true;
3943   
3944     default:
3945       *total = arm_rtx_costs_1 (x, code, outer_code);
3946       return true;
3947     }
3948 }
3949
3950
3951 /* RTX cost for XScale CPUs.  */
3952
3953 static bool
3954 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
3955 {
3956   enum machine_mode mode = GET_MODE (x);
3957
3958   if (TARGET_THUMB)
3959     {
3960       *total = thumb_rtx_costs (x, code, outer_code);
3961       return true;
3962     }
3963   
3964   switch (code)
3965     {
3966     case MULT:
3967       /* There is no point basing this on the tuning, since it is always the
3968          fast variant if it exists at all.  */
3969       if (mode == DImode
3970           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
3971           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
3972               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
3973         {
3974           *total = 8;
3975           return true;
3976         }
3977       
3978
3979       if (GET_MODE_CLASS (mode) == MODE_FLOAT
3980           || mode == DImode)
3981         {
3982           *total = 30;
3983           return true;
3984         }
3985
3986       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3987         {
3988           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
3989                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
3990           int cost, const_ok = const_ok_for_arm (i);
3991           unsigned HOST_WIDE_INT masked_const;
3992
3993           /* The cost will be related to two insns.
3994              First a load of the constant (MOV or LDR), then a multiply.  */
3995           cost = 2;
3996           if (! const_ok)
3997             cost += 1;      /* LDR is probably more expensive because
3998                                of longer result latency.  */
3999           masked_const = i & 0xffff8000;
4000           if (masked_const != 0 && masked_const != 0xffff8000)
4001             {
4002               masked_const = i & 0xf8000000;
4003               if (masked_const == 0 || masked_const == 0xf8000000)
4004                 cost += 1;
4005               else
4006                 cost += 2;
4007             }
4008           *total = cost;
4009           return true;
4010         }
4011
4012       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4013                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4014       return true;
4015   
4016     default:
4017       *total = arm_rtx_costs_1 (x, code, outer_code);
4018       return true;
4019     }
4020 }
4021
4022
4023 /* RTX costs for 9e (and later) cores.  */
4024
4025 static bool
4026 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4027 {
4028   enum machine_mode mode = GET_MODE (x);
4029   int nonreg_cost;
4030   int cost;
4031   
4032   if (TARGET_THUMB)
4033     {
4034       switch (code)
4035         {
4036         case MULT:
4037           *total = COSTS_N_INSNS (3);
4038           return true;
4039           
4040         default:
4041           *total = thumb_rtx_costs (x, code, outer_code);
4042           return true;
4043         }
4044     }
4045   
4046   switch (code)
4047     {
4048     case MULT:
4049       /* There is no point basing this on the tuning, since it is always the
4050          fast variant if it exists at all.  */
4051       if (mode == DImode
4052           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4053           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4054               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4055         {
4056           *total = 3;
4057           return true;
4058         }
4059       
4060
4061       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4062         {
4063           *total = 30;
4064           return true;
4065         }
4066       if (mode == DImode)
4067         {
4068           cost = 7;
4069           nonreg_cost = 8;
4070         }
4071       else
4072         {
4073           cost = 2;
4074           nonreg_cost = 4;
4075         }
4076
4077
4078       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4079                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4080       return true;
4081   
4082     default:
4083       *total = arm_rtx_costs_1 (x, code, outer_code);
4084       return true;
4085     }
4086 }
4087 /* All address computations that can be done are free, but rtx cost returns
4088    the same for practically all of them.  So we weight the different types
4089    of address here in the order (most pref first):
4090    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
4091 static inline int
4092 arm_arm_address_cost (rtx x)
4093 {
4094   enum rtx_code c  = GET_CODE (x);
4095
4096   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4097     return 0;
4098   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4099     return 10;
4100
4101   if (c == PLUS || c == MINUS)
4102     {
4103       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4104         return 2;
4105
4106       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4107         return 3;
4108
4109       return 4;
4110     }
4111
4112   return 6;
4113 }
4114
4115 static inline int
4116 arm_thumb_address_cost (rtx x)
4117 {
4118   enum rtx_code c  = GET_CODE (x);
4119
4120   if (c == REG)
4121     return 1;
4122   if (c == PLUS
4123       && GET_CODE (XEXP (x, 0)) == REG
4124       && GET_CODE (XEXP (x, 1)) == CONST_INT)
4125     return 1;
4126
4127   return 2;
4128 }
4129
4130 static int
4131 arm_address_cost (rtx x)
4132 {
4133   return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4134 }
4135
4136 static int
4137 arm_use_dfa_pipeline_interface (void)
4138 {
4139   return true;
4140 }
4141
4142 static int
4143 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4144 {
4145   rtx i_pat, d_pat;
4146
4147   /* Some true dependencies can have a higher cost depending
4148      on precisely how certain input operands are used.  */
4149   if (arm_tune_xscale
4150       && REG_NOTE_KIND (link) == 0
4151       && recog_memoized (insn) >= 0
4152       && recog_memoized (dep) >= 0)
4153     {
4154       int shift_opnum = get_attr_shift (insn);
4155       enum attr_type attr_type = get_attr_type (dep);
4156
4157       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4158          operand for INSN.  If we have a shifted input operand and the
4159          instruction we depend on is another ALU instruction, then we may
4160          have to account for an additional stall.  */
4161       if (shift_opnum != 0
4162           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4163         {
4164           rtx shifted_operand;
4165           int opno;
4166           
4167           /* Get the shifted operand.  */
4168           extract_insn (insn);
4169           shifted_operand = recog_data.operand[shift_opnum];
4170
4171           /* Iterate over all the operands in DEP.  If we write an operand
4172              that overlaps with SHIFTED_OPERAND, then we have increase the
4173              cost of this dependency.  */
4174           extract_insn (dep);
4175           preprocess_constraints ();
4176           for (opno = 0; opno < recog_data.n_operands; opno++)
4177             {
4178               /* We can ignore strict inputs.  */
4179               if (recog_data.operand_type[opno] == OP_IN)
4180                 continue;
4181
4182               if (reg_overlap_mentioned_p (recog_data.operand[opno],
4183                                            shifted_operand))
4184                 return 2;
4185             }
4186         }
4187     }
4188
4189   /* XXX This is not strictly true for the FPA.  */
4190   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4191       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4192     return 0;
4193
4194   /* Call insns don't incur a stall, even if they follow a load.  */
4195   if (REG_NOTE_KIND (link) == 0
4196       && GET_CODE (insn) == CALL_INSN)
4197     return 1;
4198
4199   if ((i_pat = single_set (insn)) != NULL
4200       && GET_CODE (SET_SRC (i_pat)) == MEM
4201       && (d_pat = single_set (dep)) != NULL
4202       && GET_CODE (SET_DEST (d_pat)) == MEM)
4203     {
4204       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4205       /* This is a load after a store, there is no conflict if the load reads
4206          from a cached area.  Assume that loads from the stack, and from the
4207          constant pool are cached, and that others will miss.  This is a 
4208          hack.  */
4209       
4210       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4211           || reg_mentioned_p (stack_pointer_rtx, src_mem)
4212           || reg_mentioned_p (frame_pointer_rtx, src_mem)
4213           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4214         return 1;
4215     }
4216
4217   return cost;
4218 }
4219
4220 static int fp_consts_inited = 0;
4221
4222 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
4223 static const char * const strings_fp[8] =
4224 {
4225   "0",   "1",   "2",   "3",
4226   "4",   "5",   "0.5", "10"
4227 };
4228
4229 static REAL_VALUE_TYPE values_fp[8];
4230
4231 static void
4232 init_fp_table (void)
4233 {
4234   int i;
4235   REAL_VALUE_TYPE r;
4236
4237   if (TARGET_VFP)
4238     fp_consts_inited = 1;
4239   else
4240     fp_consts_inited = 8;
4241
4242   for (i = 0; i < fp_consts_inited; i++)
4243     {
4244       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4245       values_fp[i] = r;
4246     }
4247 }
4248
4249 /* Return TRUE if rtx X is a valid immediate FP constant.  */
4250 int
4251 arm_const_double_rtx (rtx x)
4252 {
4253   REAL_VALUE_TYPE r;
4254   int i;
4255   
4256   if (!fp_consts_inited)
4257     init_fp_table ();
4258   
4259   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4260   if (REAL_VALUE_MINUS_ZERO (r))
4261     return 0;
4262
4263   for (i = 0; i < fp_consts_inited; i++)
4264     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4265       return 1;
4266
4267   return 0;
4268 }
4269
4270 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
4271 int
4272 neg_const_double_rtx_ok_for_fpa (rtx x)
4273 {
4274   REAL_VALUE_TYPE r;
4275   int i;
4276   
4277   if (!fp_consts_inited)
4278     init_fp_table ();
4279   
4280   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4281   r = REAL_VALUE_NEGATE (r);
4282   if (REAL_VALUE_MINUS_ZERO (r))
4283     return 0;
4284
4285   for (i = 0; i < 8; i++)
4286     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4287       return 1;
4288
4289   return 0;
4290 }
4291 \f
4292 /* Predicates for `match_operand' and `match_operator'.  */
4293
4294 /* s_register_operand is the same as register_operand, but it doesn't accept
4295    (SUBREG (MEM)...).
4296
4297    This function exists because at the time it was put in it led to better
4298    code.  SUBREG(MEM) always needs a reload in the places where
4299    s_register_operand is used, and this seemed to lead to excessive
4300    reloading.  */
4301 int
4302 s_register_operand (rtx op, enum machine_mode mode)
4303 {
4304   if (GET_MODE (op) != mode && mode != VOIDmode)
4305     return 0;
4306
4307   if (GET_CODE (op) == SUBREG)
4308     op = SUBREG_REG (op);
4309
4310   /* We don't consider registers whose class is NO_REGS
4311      to be a register operand.  */
4312   /* XXX might have to check for lo regs only for thumb ??? */
4313   return (GET_CODE (op) == REG
4314           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4315               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
4316 }
4317
4318 /* A hard register operand (even before reload.  */
4319 int
4320 arm_hard_register_operand (rtx op, enum machine_mode mode)
4321 {
4322   if (GET_MODE (op) != mode && mode != VOIDmode)
4323     return 0;
4324
4325   return (GET_CODE (op) == REG
4326           && REGNO (op) < FIRST_PSEUDO_REGISTER);
4327 }
4328     
4329 /* An arm register operand.  */
4330 int
4331 arm_general_register_operand (rtx op, enum machine_mode mode)
4332 {
4333   if (GET_MODE (op) != mode && mode != VOIDmode)
4334     return 0;
4335
4336   if (GET_CODE (op) == SUBREG)
4337     op = SUBREG_REG (op);
4338
4339   return (GET_CODE (op) == REG
4340           && (REGNO (op) <= LAST_ARM_REGNUM
4341               || REGNO (op) >= FIRST_PSEUDO_REGISTER));
4342 }
4343
4344 /* Only accept reg, subreg(reg), const_int.  */
4345 int
4346 reg_or_int_operand (rtx op, enum machine_mode mode)
4347 {
4348   if (GET_CODE (op) == CONST_INT)
4349     return 1;
4350
4351   if (GET_MODE (op) != mode && mode != VOIDmode)
4352     return 0;
4353
4354   if (GET_CODE (op) == SUBREG)
4355     op = SUBREG_REG (op);
4356
4357   /* We don't consider registers whose class is NO_REGS
4358      to be a register operand.  */
4359   return (GET_CODE (op) == REG
4360           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4361               || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
4362 }
4363
4364 /* Return 1 if OP is an item in memory, given that we are in reload.  */
4365 int
4366 arm_reload_memory_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4367 {
4368   int regno = true_regnum (op);
4369
4370   return (!CONSTANT_P (op)
4371           && (regno == -1
4372               || (GET_CODE (op) == REG
4373                   && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
4374 }
4375
4376 /* Return TRUE for valid operands for the rhs of an ARM instruction.  */
4377 int
4378 arm_rhs_operand (rtx op, enum machine_mode mode)
4379 {
4380   return (s_register_operand (op, mode)
4381           || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op))));
4382 }
4383
4384 /* Return TRUE for valid operands for the
4385    rhs of an ARM instruction, or a load.  */
4386 int
4387 arm_rhsm_operand (rtx op, enum machine_mode mode)
4388 {
4389   return (s_register_operand (op, mode)
4390           || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op)))
4391           || memory_operand (op, mode));
4392 }
4393
4394 /* Return TRUE for valid operands for the rhs of an ARM instruction, or if a
4395    constant that is valid when negated.  */
4396 int
4397 arm_add_operand (rtx op, enum machine_mode mode)
4398 {
4399   if (TARGET_THUMB)
4400     return thumb_cmp_operand (op, mode);
4401   
4402   return (s_register_operand (op, mode)
4403           || (GET_CODE (op) == CONST_INT
4404               && (const_ok_for_arm (INTVAL (op))
4405                   || const_ok_for_arm (-INTVAL (op)))));
4406 }
4407
4408 /* Return TRUE for valid ARM constants (or when valid if negated).  */
4409 int
4410 arm_addimm_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4411 {
4412   return (GET_CODE (op) == CONST_INT
4413           && (const_ok_for_arm (INTVAL (op))
4414               || const_ok_for_arm (-INTVAL (op))));
4415 }
4416
4417 int
4418 arm_not_operand (rtx op, enum machine_mode mode)
4419 {
4420   return (s_register_operand (op, mode)
4421           || (GET_CODE (op) == CONST_INT
4422               && (const_ok_for_arm (INTVAL (op))
4423                   || const_ok_for_arm (~INTVAL (op)))));
4424 }
4425
4426 /* Return TRUE if the operand is a memory reference which contains an
4427    offsettable address.  */
4428 int
4429 offsettable_memory_operand (rtx op, enum machine_mode mode)
4430 {
4431   if (mode == VOIDmode)
4432     mode = GET_MODE (op);
4433
4434   return (mode == GET_MODE (op)
4435           && GET_CODE (op) == MEM
4436           && offsettable_address_p (reload_completed | reload_in_progress,
4437                                     mode, XEXP (op, 0)));
4438 }
4439
4440 /* Return TRUE if the operand is a memory reference which is, or can be
4441    made word aligned by adjusting the offset.  */
4442 int
4443 alignable_memory_operand (rtx op, enum machine_mode mode)
4444 {
4445   rtx reg;
4446
4447   if (mode == VOIDmode)
4448     mode = GET_MODE (op);
4449
4450   if (mode != GET_MODE (op) || GET_CODE (op) != MEM)
4451     return 0;
4452
4453   op = XEXP (op, 0);
4454
4455   return ((GET_CODE (reg = op) == REG
4456            || (GET_CODE (op) == SUBREG
4457                && GET_CODE (reg = SUBREG_REG (op)) == REG)
4458            || (GET_CODE (op) == PLUS
4459                && GET_CODE (XEXP (op, 1)) == CONST_INT
4460                && (GET_CODE (reg = XEXP (op, 0)) == REG
4461                    || (GET_CODE (XEXP (op, 0)) == SUBREG
4462                        && GET_CODE (reg = SUBREG_REG (XEXP (op, 0))) == REG))))
4463           && REGNO_POINTER_ALIGN (REGNO (reg)) >= 32);
4464 }
4465
4466 /* Similar to s_register_operand, but does not allow hard integer 
4467    registers.  */
4468 int
4469 f_register_operand (rtx op, enum machine_mode mode)
4470 {
4471   if (GET_MODE (op) != mode && mode != VOIDmode)
4472     return 0;
4473
4474   if (GET_CODE (op) == SUBREG)
4475     op = SUBREG_REG (op);
4476
4477   /* We don't consider registers whose class is NO_REGS
4478      to be a register operand.  */
4479   return (GET_CODE (op) == REG
4480           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4481               || REGNO_REG_CLASS (REGNO (op)) == FPA_REGS));
4482 }
4483
4484 /* Return TRUE for valid operands for the rhs of an floating point insns.
4485    Allows regs or certain consts on FPA, just regs for everything else.  */
4486 int
4487 arm_float_rhs_operand (rtx op, enum machine_mode mode)
4488 {
4489   if (s_register_operand (op, mode))
4490     return TRUE;
4491
4492   if (GET_MODE (op) != mode && mode != VOIDmode)
4493     return FALSE;
4494
4495   if (TARGET_FPA && GET_CODE (op) == CONST_DOUBLE)
4496     return arm_const_double_rtx (op);
4497
4498   return FALSE;
4499 }
4500
4501 int
4502 arm_float_add_operand (rtx op, enum machine_mode mode)
4503 {
4504   if (s_register_operand (op, mode))
4505     return TRUE;
4506
4507   if (GET_MODE (op) != mode && mode != VOIDmode)
4508     return FALSE;
4509
4510   if (TARGET_FPA && GET_CODE (op) == CONST_DOUBLE)
4511     return (arm_const_double_rtx (op)
4512             || neg_const_double_rtx_ok_for_fpa (op));
4513
4514   return FALSE;
4515 }
4516
4517
4518 /* Return TRUE if OP is suitable for the rhs of a floating point comparison.
4519    Depends which fpu we are targeting.  */
4520
4521 int
4522 arm_float_compare_operand (rtx op, enum machine_mode mode)
4523 {
4524   if (TARGET_VFP)
4525     return vfp_compare_operand (op, mode);
4526   else
4527     return arm_float_rhs_operand (op, mode);
4528 }
4529
4530
4531 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
4532 int
4533 cirrus_memory_offset (rtx op)
4534 {
4535   /* Reject eliminable registers.  */
4536   if (! (reload_in_progress || reload_completed)
4537       && (   reg_mentioned_p (frame_pointer_rtx, op)
4538           || reg_mentioned_p (arg_pointer_rtx, op)
4539           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4540           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4541           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4542           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4543     return 0;
4544
4545   if (GET_CODE (op) == MEM)
4546     {
4547       rtx ind;
4548
4549       ind = XEXP (op, 0);
4550
4551       /* Match: (mem (reg)).  */
4552       if (GET_CODE (ind) == REG)
4553         return 1;
4554
4555       /* Match:
4556          (mem (plus (reg)
4557                     (const))).  */
4558       if (GET_CODE (ind) == PLUS
4559           && GET_CODE (XEXP (ind, 0)) == REG
4560           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4561           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4562         return 1;
4563     }
4564
4565   return 0;
4566 }
4567
4568 int
4569 arm_extendqisi_mem_op (rtx op, enum machine_mode mode)
4570 {
4571   if (!memory_operand (op, mode))
4572     return 0;
4573
4574   return arm_legitimate_address_p (mode, XEXP (op, 0), SIGN_EXTEND, 0);
4575 }
4576
4577 /* Return nonzero if OP is a Cirrus or general register.  */
4578 int
4579 cirrus_register_operand (rtx op, enum machine_mode mode)
4580 {
4581   if (GET_MODE (op) != mode && mode != VOIDmode)
4582     return FALSE;
4583
4584   if (GET_CODE (op) == SUBREG)
4585     op = SUBREG_REG (op);
4586
4587   return (GET_CODE (op) == REG
4588           && (REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS
4589               || REGNO_REG_CLASS (REGNO (op)) == GENERAL_REGS));
4590 }
4591
4592 /* Return nonzero if OP is a cirrus FP register.  */
4593 int
4594 cirrus_fp_register (rtx op, enum machine_mode mode)
4595 {
4596   if (GET_MODE (op) != mode && mode != VOIDmode)
4597     return FALSE;
4598
4599   if (GET_CODE (op) == SUBREG)
4600     op = SUBREG_REG (op);
4601
4602   return (GET_CODE (op) == REG
4603           && (REGNO (op) >= FIRST_PSEUDO_REGISTER
4604               || REGNO_REG_CLASS (REGNO (op)) == CIRRUS_REGS));
4605 }
4606
4607 /* Return nonzero if OP is a 6bit constant (0..63).  */
4608 int
4609 cirrus_shift_const (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4610 {
4611   return (GET_CODE (op) == CONST_INT
4612           && INTVAL (op) >= 0
4613           && INTVAL (op) < 64);
4614 }
4615
4616
4617 /* Return TRUE if OP is a valid VFP memory address pattern.  */
4618 /* Copied from cirrus_memory_offset but with restricted offset range.  */
4619
4620 int
4621 vfp_mem_operand (rtx op)
4622 {
4623   /* Reject eliminable registers.  */
4624
4625   if (! (reload_in_progress || reload_completed)
4626       && (   reg_mentioned_p (frame_pointer_rtx, op)
4627           || reg_mentioned_p (arg_pointer_rtx, op)
4628           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4629           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4630           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4631           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4632     return FALSE;
4633
4634   /* Constants are converted into offsets from labels.  */
4635   if (GET_CODE (op) == MEM)
4636     {
4637       rtx ind;
4638
4639       ind = XEXP (op, 0);
4640
4641       if (reload_completed
4642           && (GET_CODE (ind) == LABEL_REF
4643               || (GET_CODE (ind) == CONST
4644                   && GET_CODE (XEXP (ind, 0)) == PLUS
4645                   && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4646                   && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4647         return TRUE;
4648
4649       /* Match: (mem (reg)).  */
4650       if (GET_CODE (ind) == REG)
4651         return arm_address_register_rtx_p (ind, 0);
4652
4653       /* Match:
4654          (mem (plus (reg)
4655                     (const))).  */
4656       if (GET_CODE (ind) == PLUS
4657           && GET_CODE (XEXP (ind, 0)) == REG
4658           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4659           && GET_CODE (XEXP (ind, 1)) == CONST_INT
4660           && INTVAL (XEXP (ind, 1)) > -1024
4661           && INTVAL (XEXP (ind, 1)) <  1024)
4662         return TRUE;
4663     }
4664
4665   return FALSE;
4666 }
4667
4668
4669 /* Return TRUE if OP is a REG or constant zero.  */
4670 int
4671 vfp_compare_operand (rtx op, enum machine_mode mode)
4672 {
4673   if (s_register_operand (op, mode))
4674     return TRUE;
4675
4676   return (GET_CODE (op) == CONST_DOUBLE
4677           && arm_const_double_rtx (op));
4678 }
4679
4680
4681 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4682    VFP registers.  Otherwise return NO_REGS.  */
4683
4684 enum reg_class
4685 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4686 {
4687   if (vfp_mem_operand (x) || s_register_operand (x, mode))
4688     return NO_REGS;
4689
4690   return GENERAL_REGS;
4691 }
4692
4693
4694 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4695    Use by the Cirrus Maverick code which has to workaround
4696    a hardware bug triggered by such instructions.  */
4697 static bool
4698 arm_memory_load_p (rtx insn)
4699 {
4700   rtx body, lhs, rhs;;
4701
4702   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4703     return false;
4704
4705   body = PATTERN (insn);
4706
4707   if (GET_CODE (body) != SET)
4708     return false;
4709
4710   lhs = XEXP (body, 0);
4711   rhs = XEXP (body, 1);
4712
4713   lhs = REG_OR_SUBREG_RTX (lhs);
4714
4715   /* If the destination is not a general purpose
4716      register we do not have to worry.  */
4717   if (GET_CODE (lhs) != REG
4718       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4719     return false;
4720
4721   /* As well as loads from memory we also have to react
4722      to loads of invalid constants which will be turned
4723      into loads from the minipool.  */
4724   return (GET_CODE (rhs) == MEM
4725           || GET_CODE (rhs) == SYMBOL_REF
4726           || note_invalid_constants (insn, -1, false));
4727 }
4728
4729 /* Return TRUE if INSN is a Cirrus instruction.  */
4730 static bool
4731 arm_cirrus_insn_p (rtx insn)
4732 {
4733   enum attr_cirrus attr;
4734
4735   /* get_attr aborts on USE and CLOBBER.  */
4736   if (!insn
4737       || GET_CODE (insn) != INSN
4738       || GET_CODE (PATTERN (insn)) == USE
4739       || GET_CODE (PATTERN (insn)) == CLOBBER)
4740     return 0;
4741
4742   attr = get_attr_cirrus (insn);
4743
4744   return attr != CIRRUS_NOT;
4745 }
4746
4747 /* Cirrus reorg for invalid instruction combinations.  */
4748 static void
4749 cirrus_reorg (rtx first)
4750 {
4751   enum attr_cirrus attr;
4752   rtx body = PATTERN (first);
4753   rtx t;
4754   int nops;
4755
4756   /* Any branch must be followed by 2 non Cirrus instructions.  */
4757   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4758     {
4759       nops = 0;
4760       t = next_nonnote_insn (first);
4761
4762       if (arm_cirrus_insn_p (t))
4763         ++ nops;
4764
4765       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4766         ++ nops;
4767
4768       while (nops --)
4769         emit_insn_after (gen_nop (), first);
4770
4771       return;
4772     }
4773
4774   /* (float (blah)) is in parallel with a clobber.  */
4775   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4776     body = XVECEXP (body, 0, 0);
4777
4778   if (GET_CODE (body) == SET)
4779     {
4780       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4781
4782       /* cfldrd, cfldr64, cfstrd, cfstr64 must
4783          be followed by a non Cirrus insn.  */
4784       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
4785         {
4786           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
4787             emit_insn_after (gen_nop (), first);
4788
4789           return;
4790         }
4791       else if (arm_memory_load_p (first))
4792         {
4793           unsigned int arm_regno;
4794
4795           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
4796              ldr/cfmv64hr combination where the Rd field is the same
4797              in both instructions must be split with a non Cirrus
4798              insn.  Example:
4799
4800              ldr r0, blah
4801              nop
4802              cfmvsr mvf0, r0.  */
4803
4804           /* Get Arm register number for ldr insn.  */
4805           if (GET_CODE (lhs) == REG)
4806             arm_regno = REGNO (lhs);
4807           else if (GET_CODE (rhs) == REG)
4808             arm_regno = REGNO (rhs);
4809           else
4810             abort ();
4811
4812           /* Next insn.  */
4813           first = next_nonnote_insn (first);
4814
4815           if (! arm_cirrus_insn_p (first))
4816             return;
4817
4818           body = PATTERN (first);
4819
4820           /* (float (blah)) is in parallel with a clobber.  */
4821           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
4822             body = XVECEXP (body, 0, 0);
4823
4824           if (GET_CODE (body) == FLOAT)
4825             body = XEXP (body, 0);
4826
4827           if (get_attr_cirrus (first) == CIRRUS_MOVE
4828               && GET_CODE (XEXP (body, 1)) == REG
4829               && arm_regno == REGNO (XEXP (body, 1)))
4830             emit_insn_after (gen_nop (), first);
4831
4832           return;
4833         }
4834     }
4835
4836   /* get_attr aborts on USE and CLOBBER.  */
4837   if (!first
4838       || GET_CODE (first) != INSN
4839       || GET_CODE (PATTERN (first)) == USE
4840       || GET_CODE (PATTERN (first)) == CLOBBER)
4841     return;
4842
4843   attr = get_attr_cirrus (first);
4844
4845   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
4846      must be followed by a non-coprocessor instruction.  */
4847   if (attr == CIRRUS_COMPARE)
4848     {
4849       nops = 0;
4850
4851       t = next_nonnote_insn (first);
4852
4853       if (arm_cirrus_insn_p (t))
4854         ++ nops;
4855
4856       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4857         ++ nops;
4858
4859       while (nops --)
4860         emit_insn_after (gen_nop (), first);
4861
4862       return;
4863     }
4864 }
4865
4866 /* Return nonzero if OP is a constant power of two.  */
4867 int
4868 power_of_two_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
4869 {
4870   if (GET_CODE (op) == CONST_INT)
4871     {
4872       HOST_WIDE_INT value = INTVAL (op);
4873
4874       return value != 0  &&  (value & (value - 1)) == 0;
4875     }
4876
4877   return FALSE;
4878 }
4879
4880 /* Return TRUE for a valid operand of a DImode operation.
4881    Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
4882    Note that this disallows MEM(REG+REG), but allows
4883    MEM(PRE/POST_INC/DEC(REG)).  */
4884 int
4885 di_operand (rtx op, enum machine_mode mode)
4886 {
4887   if (s_register_operand (op, mode))
4888     return TRUE;
4889
4890   if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4891     return FALSE;
4892
4893   if (GET_CODE (op) == SUBREG)
4894     op = SUBREG_REG (op);
4895
4896   switch (GET_CODE (op))
4897     {
4898     case CONST_DOUBLE:
4899     case CONST_INT:
4900       return TRUE;
4901
4902     case MEM:
4903       return memory_address_p (DImode, XEXP (op, 0));
4904
4905     default:
4906       return FALSE;
4907     }
4908 }
4909
4910 /* Like di_operand, but don't accept constants.  */
4911 int
4912 nonimmediate_di_operand (rtx op, enum machine_mode mode)
4913 {
4914   if (s_register_operand (op, mode))
4915     return TRUE;
4916
4917   if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
4918     return FALSE;
4919
4920   if (GET_CODE (op) == SUBREG)
4921     op = SUBREG_REG (op);
4922
4923   if (GET_CODE (op) == MEM)
4924     return memory_address_p (DImode, XEXP (op, 0));
4925
4926   return FALSE;
4927 }
4928
4929 /* Return TRUE for a valid operand of a DFmode operation when soft-float.
4930    Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
4931    Note that this disallows MEM(REG+REG), but allows
4932    MEM(PRE/POST_INC/DEC(REG)).  */
4933 int
4934 soft_df_operand (rtx op, enum machine_mode mode)
4935 {
4936   if (s_register_operand (op, mode))
4937     return TRUE;
4938
4939   if (mode != VOIDmode && GET_MODE (op) != mode)
4940     return FALSE;
4941
4942   if (GET_CODE (op) == SUBREG && CONSTANT_P (SUBREG_REG (op)))
4943     return FALSE;
4944   
4945   if (GET_CODE (op) == SUBREG)
4946     op = SUBREG_REG (op);
4947   
4948   switch (GET_CODE (op))
4949     {
4950     case CONST_DOUBLE:
4951       return TRUE;
4952
4953     case MEM:
4954       return memory_address_p (DFmode, XEXP (op, 0));
4955
4956     default:
4957       return FALSE;
4958     }
4959 }
4960
4961 /* Like soft_df_operand, but don't accept constants.  */
4962 int
4963 nonimmediate_soft_df_operand (rtx op, enum machine_mode mode)
4964 {
4965   if (s_register_operand (op, mode))
4966     return TRUE;
4967
4968   if (mode != VOIDmode && GET_MODE (op) != mode)
4969     return FALSE;
4970
4971   if (GET_CODE (op) == SUBREG)
4972     op = SUBREG_REG (op);
4973
4974   if (GET_CODE (op) == MEM)
4975     return memory_address_p (DFmode, XEXP (op, 0));
4976   return FALSE;
4977 }
4978
4979 /* Return TRUE for valid index operands.  */
4980 int
4981 index_operand (rtx op, enum machine_mode mode)
4982 {
4983   return (s_register_operand (op, mode)
4984           || (immediate_operand (op, mode)
4985               && (GET_CODE (op) != CONST_INT
4986                   || (INTVAL (op) < 4096 && INTVAL (op) > -4096))));
4987 }
4988
4989 /* Return TRUE for valid shifts by a constant. This also accepts any
4990    power of two on the (somewhat overly relaxed) assumption that the
4991    shift operator in this case was a mult.  */
4992 int
4993 const_shift_operand (rtx op, enum machine_mode mode)
4994 {
4995   return (power_of_two_operand (op, mode)
4996           || (immediate_operand (op, mode)
4997               && (GET_CODE (op) != CONST_INT
4998                   || (INTVAL (op) < 32 && INTVAL (op) > 0))));
4999 }
5000
5001 /* Return TRUE for arithmetic operators which can be combined with a multiply
5002    (shift).  */
5003 int
5004 shiftable_operator (rtx x, enum machine_mode mode)
5005 {
5006   enum rtx_code code;
5007
5008   if (GET_MODE (x) != mode)
5009     return FALSE;
5010
5011   code = GET_CODE (x);
5012
5013   return (code == PLUS || code == MINUS
5014           || code == IOR || code == XOR || code == AND);
5015 }
5016
5017 /* Return TRUE for binary logical operators.  */
5018 int
5019 logical_binary_operator (rtx x, enum machine_mode mode)
5020 {
5021   enum rtx_code code;
5022
5023   if (GET_MODE (x) != mode)
5024     return FALSE;
5025
5026   code = GET_CODE (x);
5027
5028   return (code == IOR || code == XOR || code == AND);
5029 }
5030
5031 /* Return TRUE for shift operators.  */
5032 int
5033 shift_operator (rtx x,enum machine_mode mode)
5034 {
5035   enum rtx_code code;
5036
5037   if (GET_MODE (x) != mode)
5038     return FALSE;
5039
5040   code = GET_CODE (x);
5041
5042   if (code == MULT)
5043     return power_of_two_operand (XEXP (x, 1), mode);
5044
5045   return (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT
5046           || code == ROTATERT);
5047 }
5048
5049 /* Return TRUE if x is EQ or NE.  */
5050 int
5051 equality_operator (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
5052 {
5053   return GET_CODE (x) == EQ || GET_CODE (x) == NE;
5054 }
5055
5056 /* Return TRUE if x is a comparison operator other than LTGT or UNEQ.  */
5057 int
5058 arm_comparison_operator (rtx x, enum machine_mode mode)
5059 {
5060   return (comparison_operator (x, mode)
5061           && GET_CODE (x) != LTGT
5062           && GET_CODE (x) != UNEQ);
5063 }
5064
5065 /* Return TRUE for SMIN SMAX UMIN UMAX operators.  */
5066 int
5067 minmax_operator (rtx x, enum machine_mode mode)
5068 {
5069   enum rtx_code code = GET_CODE (x);
5070
5071   if (GET_MODE (x) != mode)
5072     return FALSE;
5073
5074   return code == SMIN || code == SMAX || code == UMIN || code == UMAX;
5075 }
5076
5077 /* Return TRUE if this is the condition code register, if we aren't given
5078    a mode, accept any class CCmode register.  */
5079 int
5080 cc_register (rtx x, enum machine_mode mode)
5081 {
5082   if (mode == VOIDmode)
5083     {
5084       mode = GET_MODE (x);
5085       
5086       if (GET_MODE_CLASS (mode) != MODE_CC)
5087         return FALSE;
5088     }
5089
5090   if (   GET_MODE (x) == mode
5091       && GET_CODE (x) == REG
5092       && REGNO    (x) == CC_REGNUM)
5093     return TRUE;
5094
5095   return FALSE;
5096 }
5097
5098 /* Return TRUE if this is the condition code register, if we aren't given
5099    a mode, accept any class CCmode register which indicates a dominance
5100    expression.  */
5101 int
5102 dominant_cc_register (rtx x, enum machine_mode mode)
5103 {
5104   if (mode == VOIDmode)
5105     {
5106       mode = GET_MODE (x);
5107       
5108       if (GET_MODE_CLASS (mode) != MODE_CC)
5109         return FALSE;
5110     }
5111
5112   if (mode != CC_DNEmode && mode != CC_DEQmode
5113       && mode != CC_DLEmode && mode != CC_DLTmode
5114       && mode != CC_DGEmode && mode != CC_DGTmode
5115       && mode != CC_DLEUmode && mode != CC_DLTUmode
5116       && mode != CC_DGEUmode && mode != CC_DGTUmode)
5117     return FALSE;
5118
5119   return cc_register (x, mode);
5120 }
5121
5122 /* Return TRUE if X references a SYMBOL_REF.  */
5123 int
5124 symbol_mentioned_p (rtx x)
5125 {
5126   const char * fmt;
5127   int i;
5128
5129   if (GET_CODE (x) == SYMBOL_REF)
5130     return 1;
5131
5132   fmt = GET_RTX_FORMAT (GET_CODE (x));
5133   
5134   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5135     {
5136       if (fmt[i] == 'E')
5137         {
5138           int j;
5139
5140           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5141             if (symbol_mentioned_p (XVECEXP (x, i, j)))
5142               return 1;
5143         }
5144       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5145         return 1;
5146     }
5147
5148   return 0;
5149 }
5150
5151 /* Return TRUE if X references a LABEL_REF.  */
5152 int
5153 label_mentioned_p (rtx x)
5154 {
5155   const char * fmt;
5156   int i;
5157
5158   if (GET_CODE (x) == LABEL_REF)
5159     return 1;
5160
5161   fmt = GET_RTX_FORMAT (GET_CODE (x));
5162   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5163     {
5164       if (fmt[i] == 'E')
5165         {
5166           int j;
5167
5168           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5169             if (label_mentioned_p (XVECEXP (x, i, j)))
5170               return 1;
5171         }
5172       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5173         return 1;
5174     }
5175
5176   return 0;
5177 }
5178
5179 enum rtx_code
5180 minmax_code (rtx x)
5181 {
5182   enum rtx_code code = GET_CODE (x);
5183
5184   if (code == SMAX)
5185     return GE;
5186   else if (code == SMIN)
5187     return LE;
5188   else if (code == UMIN)
5189     return LEU;
5190   else if (code == UMAX)
5191     return GEU;
5192
5193   abort ();
5194 }
5195
5196 /* Return 1 if memory locations are adjacent.  */
5197 int
5198 adjacent_mem_locations (rtx a, rtx b)
5199 {
5200   if ((GET_CODE (XEXP (a, 0)) == REG
5201        || (GET_CODE (XEXP (a, 0)) == PLUS
5202            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5203       && (GET_CODE (XEXP (b, 0)) == REG
5204           || (GET_CODE (XEXP (b, 0)) == PLUS
5205               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5206     {
5207       int val0 = 0, val1 = 0;
5208       int reg0, reg1;
5209   
5210       if (GET_CODE (XEXP (a, 0)) == PLUS)
5211         {
5212           reg0 = REGNO  (XEXP (XEXP (a, 0), 0));
5213           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5214         }
5215       else
5216         reg0 = REGNO (XEXP (a, 0));
5217
5218       if (GET_CODE (XEXP (b, 0)) == PLUS)
5219         {
5220           reg1 = REGNO  (XEXP (XEXP (b, 0), 0));
5221           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5222         }
5223       else
5224         reg1 = REGNO (XEXP (b, 0));
5225
5226       /* Don't accept any offset that will require multiple
5227          instructions to handle, since this would cause the
5228          arith_adjacentmem pattern to output an overlong sequence.  */
5229       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5230         return 0;
5231       
5232       return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
5233     }
5234   return 0;
5235 }
5236
5237 /* Return 1 if OP is a load multiple operation.  It is known to be
5238    parallel and the first section will be tested.  */
5239 int
5240 load_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5241 {
5242   HOST_WIDE_INT count = XVECLEN (op, 0);
5243   int dest_regno;
5244   rtx src_addr;
5245   HOST_WIDE_INT i = 1, base = 0;
5246   rtx elt;
5247
5248   if (count <= 1
5249       || GET_CODE (XVECEXP (op, 0, 0)) != SET)
5250     return 0;
5251
5252   /* Check to see if this might be a write-back.  */
5253   if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
5254     {
5255       i++;
5256       base = 1;
5257
5258       /* Now check it more carefully.  */
5259       if (GET_CODE (SET_DEST (elt)) != REG
5260           || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
5261           || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
5262           || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
5263         return 0;
5264     }
5265
5266   /* Perform a quick check so we don't blow up below.  */
5267   if (count <= i
5268       || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
5269       || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != REG
5270       || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != MEM)
5271     return 0;
5272
5273   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, i - 1)));
5274   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, i - 1)), 0);
5275
5276   for (; i < count; i++)
5277     {
5278       elt = XVECEXP (op, 0, i);
5279
5280       if (GET_CODE (elt) != SET
5281           || GET_CODE (SET_DEST (elt)) != REG
5282           || GET_MODE (SET_DEST (elt)) != SImode
5283           || REGNO (SET_DEST (elt)) != (unsigned int)(dest_regno + i - base)
5284           || GET_CODE (SET_SRC (elt)) != MEM
5285           || GET_MODE (SET_SRC (elt)) != SImode
5286           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
5287           || !rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
5288           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
5289           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != (i - base) * 4)
5290         return 0;
5291     }
5292
5293   return 1;
5294 }
5295
5296 /* Return 1 if OP is a store multiple operation.  It is known to be
5297    parallel and the first section will be tested.  */
5298 int
5299 store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5300 {
5301   HOST_WIDE_INT count = XVECLEN (op, 0);
5302   int src_regno;
5303   rtx dest_addr;
5304   HOST_WIDE_INT i = 1, base = 0;
5305   rtx elt;
5306
5307   if (count <= 1
5308       || GET_CODE (XVECEXP (op, 0, 0)) != SET)
5309     return 0;
5310
5311   /* Check to see if this might be a write-back.  */
5312   if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
5313     {
5314       i++;
5315       base = 1;
5316
5317       /* Now check it more carefully.  */
5318       if (GET_CODE (SET_DEST (elt)) != REG
5319           || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
5320           || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
5321           || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 1) * 4)
5322         return 0;
5323     }
5324
5325   /* Perform a quick check so we don't blow up below.  */
5326   if (count <= i
5327       || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
5328       || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != MEM
5329       || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != REG)
5330     return 0;
5331
5332   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, i - 1)));
5333   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, i - 1)), 0);
5334
5335   for (; i < count; i++)
5336     {
5337       elt = XVECEXP (op, 0, i);
5338
5339       if (GET_CODE (elt) != SET
5340           || GET_CODE (SET_SRC (elt)) != REG
5341           || GET_MODE (SET_SRC (elt)) != SImode
5342           || REGNO (SET_SRC (elt)) != (unsigned int)(src_regno + i - base)
5343           || GET_CODE (SET_DEST (elt)) != MEM
5344           || GET_MODE (SET_DEST (elt)) != SImode
5345           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
5346           || !rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
5347           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
5348           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != (i - base) * 4)
5349         return 0;
5350     }
5351
5352   return 1;
5353 }
5354
5355 int
5356 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5357                         HOST_WIDE_INT *load_offset)
5358 {
5359   int unsorted_regs[4];
5360   HOST_WIDE_INT unsorted_offsets[4];
5361   int order[4];
5362   int base_reg = -1;
5363   int i;
5364
5365   /* Can only handle 2, 3, or 4 insns at present,
5366      though could be easily extended if required.  */
5367   if (nops < 2 || nops > 4)
5368     abort ();
5369
5370   /* Loop over the operands and check that the memory references are
5371      suitable (ie immediate offsets from the same base register).  At
5372      the same time, extract the target register, and the memory
5373      offsets.  */
5374   for (i = 0; i < nops; i++)
5375     {
5376       rtx reg;
5377       rtx offset;
5378
5379       /* Convert a subreg of a mem into the mem itself.  */
5380       if (GET_CODE (operands[nops + i]) == SUBREG)
5381         operands[nops + i] = alter_subreg (operands + (nops + i));
5382
5383       if (GET_CODE (operands[nops + i]) != MEM)
5384         abort ();
5385
5386       /* Don't reorder volatile memory references; it doesn't seem worth
5387          looking for the case where the order is ok anyway.  */
5388       if (MEM_VOLATILE_P (operands[nops + i]))
5389         return 0;
5390
5391       offset = const0_rtx;
5392
5393       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5394            || (GET_CODE (reg) == SUBREG
5395                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5396           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5397               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5398                    == REG)
5399                   || (GET_CODE (reg) == SUBREG
5400                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5401               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5402                   == CONST_INT)))
5403         {
5404           if (i == 0)
5405             {
5406               base_reg = REGNO (reg);
5407               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5408                                   ? REGNO (operands[i])
5409                                   : REGNO (SUBREG_REG (operands[i])));
5410               order[0] = 0;
5411             }
5412           else 
5413             {
5414               if (base_reg != (int) REGNO (reg))
5415                 /* Not addressed from the same base register.  */
5416                 return 0;
5417
5418               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5419                                   ? REGNO (operands[i])
5420                                   : REGNO (SUBREG_REG (operands[i])));
5421               if (unsorted_regs[i] < unsorted_regs[order[0]])
5422                 order[0] = i;
5423             }
5424
5425           /* If it isn't an integer register, or if it overwrites the
5426              base register but isn't the last insn in the list, then
5427              we can't do this.  */
5428           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5429               || (i != nops - 1 && unsorted_regs[i] == base_reg))
5430             return 0;
5431
5432           unsorted_offsets[i] = INTVAL (offset);
5433         }
5434       else
5435         /* Not a suitable memory address.  */
5436         return 0;
5437     }
5438
5439   /* All the useful information has now been extracted from the
5440      operands into unsorted_regs and unsorted_offsets; additionally,
5441      order[0] has been set to the lowest numbered register in the
5442      list.  Sort the registers into order, and check that the memory
5443      offsets are ascending and adjacent.  */
5444
5445   for (i = 1; i < nops; i++)
5446     {
5447       int j;
5448
5449       order[i] = order[i - 1];
5450       for (j = 0; j < nops; j++)
5451         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5452             && (order[i] == order[i - 1]
5453                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5454           order[i] = j;
5455
5456       /* Have we found a suitable register? if not, one must be used more
5457          than once.  */
5458       if (order[i] == order[i - 1])
5459         return 0;
5460
5461       /* Is the memory address adjacent and ascending? */
5462       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5463         return 0;
5464     }
5465
5466   if (base)
5467     {
5468       *base = base_reg;
5469
5470       for (i = 0; i < nops; i++)
5471         regs[i] = unsorted_regs[order[i]];
5472
5473       *load_offset = unsorted_offsets[order[0]];
5474     }
5475
5476   if (unsorted_offsets[order[0]] == 0)
5477     return 1; /* ldmia */
5478
5479   if (unsorted_offsets[order[0]] == 4)
5480     return 2; /* ldmib */
5481
5482   if (unsorted_offsets[order[nops - 1]] == 0)
5483     return 3; /* ldmda */
5484
5485   if (unsorted_offsets[order[nops - 1]] == -4)
5486     return 4; /* ldmdb */
5487
5488   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5489      if the offset isn't small enough.  The reason 2 ldrs are faster
5490      is because these ARMs are able to do more than one cache access
5491      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
5492      whilst the ARM8 has a double bandwidth cache.  This means that
5493      these cores can do both an instruction fetch and a data fetch in
5494      a single cycle, so the trick of calculating the address into a
5495      scratch register (one of the result regs) and then doing a load
5496      multiple actually becomes slower (and no smaller in code size).
5497      That is the transformation
5498  
5499         ldr     rd1, [rbase + offset]
5500         ldr     rd2, [rbase + offset + 4]
5501  
5502      to
5503  
5504         add     rd1, rbase, offset
5505         ldmia   rd1, {rd1, rd2}
5506  
5507      produces worse code -- '3 cycles + any stalls on rd2' instead of
5508      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
5509      access per cycle, the first sequence could never complete in less
5510      than 6 cycles, whereas the ldm sequence would only take 5 and
5511      would make better use of sequential accesses if not hitting the
5512      cache.
5513
5514      We cheat here and test 'arm_ld_sched' which we currently know to
5515      only be true for the ARM8, ARM9 and StrongARM.  If this ever
5516      changes, then the test below needs to be reworked.  */
5517   if (nops == 2 && arm_ld_sched)
5518     return 0;
5519
5520   /* Can't do it without setting up the offset, only do this if it takes
5521      no more than one insn.  */
5522   return (const_ok_for_arm (unsorted_offsets[order[0]]) 
5523           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5524 }
5525
5526 const char *
5527 emit_ldm_seq (rtx *operands, int nops)
5528 {
5529   int regs[4];
5530   int base_reg;
5531   HOST_WIDE_INT offset;
5532   char buf[100];
5533   int i;
5534
5535   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5536     {
5537     case 1:
5538       strcpy (buf, "ldm%?ia\t");
5539       break;
5540
5541     case 2:
5542       strcpy (buf, "ldm%?ib\t");
5543       break;
5544
5545     case 3:
5546       strcpy (buf, "ldm%?da\t");
5547       break;
5548
5549     case 4:
5550       strcpy (buf, "ldm%?db\t");
5551       break;
5552
5553     case 5:
5554       if (offset >= 0)
5555         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5556                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5557                  (long) offset);
5558       else
5559         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5560                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5561                  (long) -offset);
5562       output_asm_insn (buf, operands);
5563       base_reg = regs[0];
5564       strcpy (buf, "ldm%?ia\t");
5565       break;
5566
5567     default:
5568       abort ();
5569     }
5570
5571   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX, 
5572            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5573
5574   for (i = 1; i < nops; i++)
5575     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5576              reg_names[regs[i]]);
5577
5578   strcat (buf, "}\t%@ phole ldm");
5579
5580   output_asm_insn (buf, operands);
5581   return "";
5582 }
5583
5584 int
5585 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5586                          HOST_WIDE_INT * load_offset)
5587 {
5588   int unsorted_regs[4];
5589   HOST_WIDE_INT unsorted_offsets[4];
5590   int order[4];
5591   int base_reg = -1;
5592   int i;
5593
5594   /* Can only handle 2, 3, or 4 insns at present, though could be easily
5595      extended if required.  */
5596   if (nops < 2 || nops > 4)
5597     abort ();
5598
5599   /* Loop over the operands and check that the memory references are
5600      suitable (ie immediate offsets from the same base register).  At
5601      the same time, extract the target register, and the memory
5602      offsets.  */
5603   for (i = 0; i < nops; i++)
5604     {
5605       rtx reg;
5606       rtx offset;
5607
5608       /* Convert a subreg of a mem into the mem itself.  */
5609       if (GET_CODE (operands[nops + i]) == SUBREG)
5610         operands[nops + i] = alter_subreg (operands + (nops + i));
5611
5612       if (GET_CODE (operands[nops + i]) != MEM)
5613         abort ();
5614
5615       /* Don't reorder volatile memory references; it doesn't seem worth
5616          looking for the case where the order is ok anyway.  */
5617       if (MEM_VOLATILE_P (operands[nops + i]))
5618         return 0;
5619
5620       offset = const0_rtx;
5621
5622       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5623            || (GET_CODE (reg) == SUBREG
5624                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5625           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5626               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5627                    == REG)
5628                   || (GET_CODE (reg) == SUBREG
5629                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5630               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5631                   == CONST_INT)))
5632         {
5633           if (i == 0)
5634             {
5635               base_reg = REGNO (reg);
5636               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5637                                   ? REGNO (operands[i])
5638                                   : REGNO (SUBREG_REG (operands[i])));
5639               order[0] = 0;
5640             }
5641           else 
5642             {
5643               if (base_reg != (int) REGNO (reg))
5644                 /* Not addressed from the same base register.  */
5645                 return 0;
5646
5647               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5648                                   ? REGNO (operands[i])
5649                                   : REGNO (SUBREG_REG (operands[i])));
5650               if (unsorted_regs[i] < unsorted_regs[order[0]])
5651                 order[0] = i;
5652             }
5653
5654           /* If it isn't an integer register, then we can't do this.  */
5655           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5656             return 0;
5657
5658           unsorted_offsets[i] = INTVAL (offset);
5659         }
5660       else
5661         /* Not a suitable memory address.  */
5662         return 0;
5663     }
5664
5665   /* All the useful information has now been extracted from the
5666      operands into unsorted_regs and unsorted_offsets; additionally,
5667      order[0] has been set to the lowest numbered register in the
5668      list.  Sort the registers into order, and check that the memory
5669      offsets are ascending and adjacent.  */
5670
5671   for (i = 1; i < nops; i++)
5672     {
5673       int j;
5674
5675       order[i] = order[i - 1];
5676       for (j = 0; j < nops; j++)
5677         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5678             && (order[i] == order[i - 1]
5679                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5680           order[i] = j;
5681
5682       /* Have we found a suitable register? if not, one must be used more
5683          than once.  */
5684       if (order[i] == order[i - 1])
5685         return 0;
5686
5687       /* Is the memory address adjacent and ascending? */
5688       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5689         return 0;
5690     }
5691
5692   if (base)
5693     {
5694       *base = base_reg;
5695
5696       for (i = 0; i < nops; i++)
5697         regs[i] = unsorted_regs[order[i]];
5698
5699       *load_offset = unsorted_offsets[order[0]];
5700     }
5701
5702   if (unsorted_offsets[order[0]] == 0)
5703     return 1; /* stmia */
5704
5705   if (unsorted_offsets[order[0]] == 4)
5706     return 2; /* stmib */
5707
5708   if (unsorted_offsets[order[nops - 1]] == 0)
5709     return 3; /* stmda */
5710
5711   if (unsorted_offsets[order[nops - 1]] == -4)
5712     return 4; /* stmdb */
5713
5714   return 0;
5715 }
5716
5717 const char *
5718 emit_stm_seq (rtx *operands, int nops)
5719 {
5720   int regs[4];
5721   int base_reg;
5722   HOST_WIDE_INT offset;
5723   char buf[100];
5724   int i;
5725
5726   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5727     {
5728     case 1:
5729       strcpy (buf, "stm%?ia\t");
5730       break;
5731
5732     case 2:
5733       strcpy (buf, "stm%?ib\t");
5734       break;
5735
5736     case 3:
5737       strcpy (buf, "stm%?da\t");
5738       break;
5739
5740     case 4:
5741       strcpy (buf, "stm%?db\t");
5742       break;
5743
5744     default:
5745       abort ();
5746     }
5747
5748   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX, 
5749            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5750
5751   for (i = 1; i < nops; i++)
5752     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5753              reg_names[regs[i]]);
5754
5755   strcat (buf, "}\t%@ phole stm");
5756
5757   output_asm_insn (buf, operands);
5758   return "";
5759 }
5760
5761 int
5762 multi_register_push (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
5763 {
5764   if (GET_CODE (op) != PARALLEL
5765       || (GET_CODE (XVECEXP (op, 0, 0)) != SET)
5766       || (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
5767       || (XINT (SET_SRC (XVECEXP (op, 0, 0)), 1) != UNSPEC_PUSH_MULT))
5768     return 0;
5769
5770   return 1;
5771 }
5772 \f
5773 /* Routines for use in generating RTL.  */
5774
5775 rtx
5776 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5777                        int write_back, int unchanging_p, int in_struct_p,
5778                        int scalar_p)
5779 {
5780   int i = 0, j;
5781   rtx result;
5782   int sign = up ? 1 : -1;
5783   rtx mem;
5784
5785   /* XScale has load-store double instructions, but they have stricter
5786      alignment requirements than load-store multiple, so we can not
5787      use them.
5788
5789      For XScale ldm requires 2 + NREGS cycles to complete and blocks
5790      the pipeline until completion.
5791
5792         NREGS           CYCLES
5793           1               3
5794           2               4
5795           3               5
5796           4               6
5797
5798      An ldr instruction takes 1-3 cycles, but does not block the
5799      pipeline.
5800
5801         NREGS           CYCLES
5802           1              1-3
5803           2              2-6
5804           3              3-9
5805           4              4-12
5806
5807      Best case ldr will always win.  However, the more ldr instructions
5808      we issue, the less likely we are to be able to schedule them well.
5809      Using ldr instructions also increases code size.
5810
5811      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5812      for counts of 3 or 4 regs.  */
5813   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5814     {
5815       rtx seq;
5816       
5817       start_sequence ();
5818       
5819       for (i = 0; i < count; i++)
5820         {
5821           mem = gen_rtx_MEM (SImode, plus_constant (from, i * 4 * sign));
5822           RTX_UNCHANGING_P (mem) = unchanging_p;
5823           MEM_IN_STRUCT_P (mem) = in_struct_p;
5824           MEM_SCALAR_P (mem) = scalar_p;
5825           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5826         }
5827
5828       if (write_back)
5829         emit_move_insn (from, plus_constant (from, count * 4 * sign));
5830
5831       seq = get_insns ();
5832       end_sequence ();
5833       
5834       return seq;
5835     }
5836
5837   result = gen_rtx_PARALLEL (VOIDmode,
5838                              rtvec_alloc (count + (write_back ? 1 : 0)));
5839   if (write_back)
5840     {
5841       XVECEXP (result, 0, 0)
5842         = gen_rtx_SET (GET_MODE (from), from,
5843                        plus_constant (from, count * 4 * sign));
5844       i = 1;
5845       count++;
5846     }
5847
5848   for (j = 0; i < count; i++, j++)
5849     {
5850       mem = gen_rtx_MEM (SImode, plus_constant (from, j * 4 * sign));
5851       RTX_UNCHANGING_P (mem) = unchanging_p;
5852       MEM_IN_STRUCT_P (mem) = in_struct_p;
5853       MEM_SCALAR_P (mem) = scalar_p;
5854       XVECEXP (result, 0, i)
5855         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5856     }
5857
5858   return result;
5859 }
5860
5861 rtx
5862 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5863                         int write_back, int unchanging_p, int in_struct_p,
5864                         int scalar_p)
5865 {
5866   int i = 0, j;
5867   rtx result;
5868   int sign = up ? 1 : -1;
5869   rtx mem;
5870
5871   /* See arm_gen_load_multiple for discussion of
5872      the pros/cons of ldm/stm usage for XScale.  */
5873   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5874     {
5875       rtx seq;
5876       
5877       start_sequence ();
5878       
5879       for (i = 0; i < count; i++)
5880         {
5881           mem = gen_rtx_MEM (SImode, plus_constant (to, i * 4 * sign));
5882           RTX_UNCHANGING_P (mem) = unchanging_p;
5883           MEM_IN_STRUCT_P (mem) = in_struct_p;
5884           MEM_SCALAR_P (mem) = scalar_p;
5885           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5886         }
5887
5888       if (write_back)
5889         emit_move_insn (to, plus_constant (to, count * 4 * sign));
5890
5891       seq = get_insns ();
5892       end_sequence ();
5893       
5894       return seq;
5895     }
5896
5897   result = gen_rtx_PARALLEL (VOIDmode,
5898                              rtvec_alloc (count + (write_back ? 1 : 0)));
5899   if (write_back)
5900     {
5901       XVECEXP (result, 0, 0)
5902         = gen_rtx_SET (GET_MODE (to), to,
5903                        plus_constant (to, count * 4 * sign));
5904       i = 1;
5905       count++;
5906     }
5907
5908   for (j = 0; i < count; i++, j++)
5909     {
5910       mem = gen_rtx_MEM (SImode, plus_constant (to, j * 4 * sign));
5911       RTX_UNCHANGING_P (mem) = unchanging_p;
5912       MEM_IN_STRUCT_P (mem) = in_struct_p;
5913       MEM_SCALAR_P (mem) = scalar_p;
5914
5915       XVECEXP (result, 0, i)
5916         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5917     }
5918
5919   return result;
5920 }
5921
5922 int
5923 arm_gen_movstrqi (rtx *operands)
5924 {
5925   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5926   int i;
5927   rtx src, dst;
5928   rtx st_src, st_dst, fin_src, fin_dst;
5929   rtx part_bytes_reg = NULL;
5930   rtx mem;
5931   int dst_unchanging_p, dst_in_struct_p, src_unchanging_p, src_in_struct_p;
5932   int dst_scalar_p, src_scalar_p;
5933
5934   if (GET_CODE (operands[2]) != CONST_INT
5935       || GET_CODE (operands[3]) != CONST_INT
5936       || INTVAL (operands[2]) > 64
5937       || INTVAL (operands[3]) & 3)
5938     return 0;
5939
5940   st_dst = XEXP (operands[0], 0);
5941   st_src = XEXP (operands[1], 0);
5942
5943   dst_unchanging_p = RTX_UNCHANGING_P (operands[0]);
5944   dst_in_struct_p = MEM_IN_STRUCT_P (operands[0]);
5945   dst_scalar_p = MEM_SCALAR_P (operands[0]);
5946   src_unchanging_p = RTX_UNCHANGING_P (operands[1]);
5947   src_in_struct_p = MEM_IN_STRUCT_P (operands[1]);
5948   src_scalar_p = MEM_SCALAR_P (operands[1]);
5949
5950   fin_dst = dst = copy_to_mode_reg (SImode, st_dst);
5951   fin_src = src = copy_to_mode_reg (SImode, st_src);
5952
5953   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5954   out_words_to_go = INTVAL (operands[2]) / 4;
5955   last_bytes = INTVAL (operands[2]) & 3;
5956
5957   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5958     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5959
5960   for (i = 0; in_words_to_go >= 2; i+=4)
5961     {
5962       if (in_words_to_go > 4)
5963         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5964                                           src_unchanging_p,
5965                                           src_in_struct_p,
5966                                           src_scalar_p));
5967       else
5968         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE, 
5969                                           FALSE, src_unchanging_p,
5970                                           src_in_struct_p, src_scalar_p));
5971
5972       if (out_words_to_go)
5973         {
5974           if (out_words_to_go > 4)
5975             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5976                                                dst_unchanging_p,
5977                                                dst_in_struct_p,
5978                                                dst_scalar_p));
5979           else if (out_words_to_go != 1)
5980             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5981                                                dst, TRUE, 
5982                                                (last_bytes == 0
5983                                                 ? FALSE : TRUE),
5984                                                dst_unchanging_p,
5985                                                dst_in_struct_p,
5986                                                dst_scalar_p));
5987           else
5988             {
5989               mem = gen_rtx_MEM (SImode, dst);
5990               RTX_UNCHANGING_P (mem) = dst_unchanging_p;
5991               MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
5992               MEM_SCALAR_P (mem) = dst_scalar_p;
5993               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5994               if (last_bytes != 0)
5995                 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5996             }
5997         }
5998
5999       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
6000       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
6001     }
6002
6003   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
6004   if (out_words_to_go)
6005     {
6006       rtx sreg;
6007       
6008       mem = gen_rtx_MEM (SImode, src);
6009       RTX_UNCHANGING_P (mem) = src_unchanging_p;
6010       MEM_IN_STRUCT_P (mem) = src_in_struct_p;
6011       MEM_SCALAR_P (mem) = src_scalar_p;
6012       emit_move_insn (sreg = gen_reg_rtx (SImode), mem);
6013       emit_move_insn (fin_src = gen_reg_rtx (SImode), plus_constant (src, 4));
6014       
6015       mem = gen_rtx_MEM (SImode, dst);
6016       RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6017       MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6018       MEM_SCALAR_P (mem) = dst_scalar_p;
6019       emit_move_insn (mem, sreg);
6020       emit_move_insn (fin_dst = gen_reg_rtx (SImode), plus_constant (dst, 4));
6021       in_words_to_go--;
6022       
6023       if (in_words_to_go)       /* Sanity check */
6024         abort ();
6025     }
6026
6027   if (in_words_to_go)
6028     {
6029       if (in_words_to_go < 0)
6030         abort ();
6031
6032       mem = gen_rtx_MEM (SImode, src);
6033       RTX_UNCHANGING_P (mem) = src_unchanging_p;
6034       MEM_IN_STRUCT_P (mem) = src_in_struct_p;
6035       MEM_SCALAR_P (mem) = src_scalar_p;
6036       part_bytes_reg = copy_to_mode_reg (SImode, mem);
6037     }
6038
6039   if (last_bytes && part_bytes_reg == NULL)
6040     abort ();
6041
6042   if (BYTES_BIG_ENDIAN && last_bytes)
6043     {
6044       rtx tmp = gen_reg_rtx (SImode);
6045
6046       /* The bytes we want are in the top end of the word.  */
6047       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6048                               GEN_INT (8 * (4 - last_bytes))));
6049       part_bytes_reg = tmp;
6050       
6051       while (last_bytes)
6052         {
6053           mem = gen_rtx_MEM (QImode, plus_constant (dst, last_bytes - 1));
6054           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6055           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6056           MEM_SCALAR_P (mem) = dst_scalar_p;
6057           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6058
6059           if (--last_bytes)
6060             {
6061               tmp = gen_reg_rtx (SImode);
6062               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6063               part_bytes_reg = tmp;
6064             }
6065         }
6066           
6067     }
6068   else
6069     {
6070       if (last_bytes > 1)
6071         {
6072           mem = gen_rtx_MEM (HImode, dst);
6073           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6074           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6075           MEM_SCALAR_P (mem) = dst_scalar_p;
6076           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
6077           last_bytes -= 2;
6078           if (last_bytes)
6079             {
6080               rtx tmp = gen_reg_rtx (SImode);
6081
6082               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
6083               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
6084               part_bytes_reg = tmp;
6085             }
6086         }
6087       
6088       if (last_bytes)
6089         {
6090           mem = gen_rtx_MEM (QImode, dst);
6091           RTX_UNCHANGING_P (mem) = dst_unchanging_p;
6092           MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
6093           MEM_SCALAR_P (mem) = dst_scalar_p;
6094           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6095         }
6096     }
6097
6098   return 1;
6099 }
6100
6101 /* Generate a memory reference for a half word, such that it will be loaded
6102    into the top 16 bits of the word.  We can assume that the address is
6103    known to be alignable and of the form reg, or plus (reg, const).  */
6104
6105 rtx
6106 arm_gen_rotated_half_load (rtx memref)
6107 {
6108   HOST_WIDE_INT offset = 0;
6109   rtx base = XEXP (memref, 0);
6110
6111   if (GET_CODE (base) == PLUS)
6112     {
6113       offset = INTVAL (XEXP (base, 1));
6114       base = XEXP (base, 0);
6115     }
6116
6117   /* If we aren't allowed to generate unaligned addresses, then fail.  */
6118   if (TARGET_MMU_TRAPS
6119       && ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0)))
6120     return NULL;
6121
6122   base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
6123
6124   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
6125     return base;
6126
6127   return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
6128 }
6129
6130 /* Select a dominance comparison mode if possible for a test of the general
6131    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
6132    COND_OR == DOM_CC_X_AND_Y => (X && Y) 
6133    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6134    COND_OR == DOM_CC_X_OR_Y => (X || Y) 
6135    In all cases OP will be either EQ or NE, but we don't need to know which
6136    here.  If we are unable to support a dominance comparison we return 
6137    CC mode.  This will then fail to match for the RTL expressions that
6138    generate this call.  */
6139 enum machine_mode
6140 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6141 {
6142   enum rtx_code cond1, cond2;
6143   int swapped = 0;
6144
6145   /* Currently we will probably get the wrong result if the individual
6146      comparisons are not simple.  This also ensures that it is safe to
6147      reverse a comparison if necessary.  */
6148   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6149        != CCmode)
6150       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6151           != CCmode))
6152     return CCmode;
6153
6154   /* The if_then_else variant of this tests the second condition if the
6155      first passes, but is true if the first fails.  Reverse the first
6156      condition to get a true "inclusive-or" expression.  */
6157   if (cond_or == DOM_CC_NX_OR_Y)
6158     cond1 = reverse_condition (cond1);
6159
6160   /* If the comparisons are not equal, and one doesn't dominate the other,
6161      then we can't do this.  */
6162   if (cond1 != cond2 
6163       && !comparison_dominates_p (cond1, cond2)
6164       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6165     return CCmode;
6166
6167   if (swapped)
6168     {
6169       enum rtx_code temp = cond1;
6170       cond1 = cond2;
6171       cond2 = temp;
6172     }
6173
6174   switch (cond1)
6175     {
6176     case EQ:
6177       if (cond2 == EQ || cond_or == DOM_CC_X_AND_Y)
6178         return CC_DEQmode;
6179
6180       switch (cond2)
6181         {
6182         case LE: return CC_DLEmode;
6183         case LEU: return CC_DLEUmode;
6184         case GE: return CC_DGEmode;
6185         case GEU: return CC_DGEUmode;
6186         default: break;
6187         }
6188
6189       break;
6190
6191     case LT:
6192       if (cond2 == LT || cond_or == DOM_CC_X_AND_Y)
6193         return CC_DLTmode;
6194       if (cond2 == LE)
6195         return CC_DLEmode;
6196       if (cond2 == NE)
6197         return CC_DNEmode;
6198       break;
6199
6200     case GT:
6201       if (cond2 == GT || cond_or == DOM_CC_X_AND_Y)
6202         return CC_DGTmode;
6203       if (cond2 == GE)
6204         return CC_DGEmode;
6205       if (cond2 == NE)
6206         return CC_DNEmode;
6207       break;
6208       
6209     case LTU:
6210       if (cond2 == LTU || cond_or == DOM_CC_X_AND_Y)
6211         return CC_DLTUmode;
6212       if (cond2 == LEU)
6213         return CC_DLEUmode;
6214       if (cond2 == NE)
6215         return CC_DNEmode;
6216       break;
6217
6218     case GTU:
6219       if (cond2 == GTU || cond_or == DOM_CC_X_AND_Y)
6220         return CC_DGTUmode;
6221       if (cond2 == GEU)
6222         return CC_DGEUmode;
6223       if (cond2 == NE)
6224         return CC_DNEmode;
6225       break;
6226
6227     /* The remaining cases only occur when both comparisons are the
6228        same.  */
6229     case NE:
6230       return CC_DNEmode;
6231
6232     case LE:
6233       return CC_DLEmode;
6234
6235     case GE:
6236       return CC_DGEmode;
6237
6238     case LEU:
6239       return CC_DLEUmode;
6240
6241     case GEU:
6242       return CC_DGEUmode;
6243
6244     default:
6245       break;
6246     }
6247
6248   abort ();
6249 }
6250
6251 enum machine_mode
6252 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6253 {
6254   /* All floating point compares return CCFP if it is an equality
6255      comparison, and CCFPE otherwise.  */
6256   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6257     {
6258       switch (op)
6259         {
6260         case EQ:
6261         case NE:
6262         case UNORDERED:
6263         case ORDERED:
6264         case UNLT:
6265         case UNLE:
6266         case UNGT:
6267         case UNGE:
6268         case UNEQ:
6269         case LTGT:
6270           return CCFPmode;
6271
6272         case LT:
6273         case LE:
6274         case GT:
6275         case GE:
6276           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6277             return CCFPmode;
6278           return CCFPEmode;
6279
6280         default:
6281           abort ();
6282         }
6283     }
6284   
6285   /* A compare with a shifted operand.  Because of canonicalization, the
6286      comparison will have to be swapped when we emit the assembler.  */
6287   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6288       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6289           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6290           || GET_CODE (x) == ROTATERT))
6291     return CC_SWPmode;
6292
6293   /* This is a special case that is used by combine to allow a 
6294      comparison of a shifted byte load to be split into a zero-extend
6295      followed by a comparison of the shifted integer (only valid for
6296      equalities and unsigned inequalities).  */
6297   if (GET_MODE (x) == SImode
6298       && GET_CODE (x) == ASHIFT
6299       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6300       && GET_CODE (XEXP (x, 0)) == SUBREG
6301       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6302       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6303       && (op == EQ || op == NE
6304           || op == GEU || op == GTU || op == LTU || op == LEU)
6305       && GET_CODE (y) == CONST_INT)
6306     return CC_Zmode;
6307
6308   /* A construct for a conditional compare, if the false arm contains
6309      0, then both conditions must be true, otherwise either condition
6310      must be true.  Not all conditions are possible, so CCmode is
6311      returned if it can't be done.  */
6312   if (GET_CODE (x) == IF_THEN_ELSE
6313       && (XEXP (x, 2) == const0_rtx
6314           || XEXP (x, 2) == const1_rtx)
6315       && COMPARISON_P (XEXP (x, 0))
6316       && COMPARISON_P (XEXP (x, 1)))
6317     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1), 
6318                                          INTVAL (XEXP (x, 2)));
6319
6320   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
6321   if (GET_CODE (x) == AND
6322       && COMPARISON_P (XEXP (x, 0))
6323       && COMPARISON_P (XEXP (x, 1)))
6324     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6325                                          DOM_CC_X_AND_Y);
6326
6327   if (GET_CODE (x) == IOR
6328       && COMPARISON_P (XEXP (x, 0))
6329       && COMPARISON_P (XEXP (x, 1)))
6330     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6331                                          DOM_CC_X_OR_Y);
6332
6333   /* An operation (on Thumb) where we want to test for a single bit.
6334      This is done by shifting that bit up into the top bit of a
6335      scratch register; we can then branch on the sign bit.  */
6336   if (TARGET_THUMB
6337       && GET_MODE (x) == SImode
6338       && (op == EQ || op == NE)
6339       && (GET_CODE (x) == ZERO_EXTRACT))
6340     return CC_Nmode;
6341
6342   /* An operation that sets the condition codes as a side-effect, the
6343      V flag is not set correctly, so we can only use comparisons where
6344      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
6345      instead.)  */
6346   if (GET_MODE (x) == SImode
6347       && y == const0_rtx
6348       && (op == EQ || op == NE || op == LT || op == GE)
6349       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6350           || GET_CODE (x) == AND || GET_CODE (x) == IOR
6351           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6352           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6353           || GET_CODE (x) == LSHIFTRT
6354           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6355           || GET_CODE (x) == ROTATERT
6356           || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6357     return CC_NOOVmode;
6358
6359   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6360     return CC_Zmode;
6361
6362   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6363       && GET_CODE (x) == PLUS
6364       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6365     return CC_Cmode;
6366
6367   return CCmode;
6368 }
6369
6370 /* X and Y are two things to compare using CODE.  Emit the compare insn and
6371    return the rtx for register 0 in the proper mode.  FP means this is a
6372    floating point compare: I don't think that it is needed on the arm.  */
6373 rtx
6374 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6375 {
6376   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6377   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6378
6379   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6380                           gen_rtx_COMPARE (mode, x, y)));
6381
6382   return cc_reg;
6383 }
6384
6385 /* Generate a sequence of insns that will generate the correct return
6386    address mask depending on the physical architecture that the program
6387    is running on.  */
6388 rtx
6389 arm_gen_return_addr_mask (void)
6390 {
6391   rtx reg = gen_reg_rtx (Pmode);
6392
6393   emit_insn (gen_return_addr_mask (reg));
6394   return reg;
6395 }
6396
6397 void
6398 arm_reload_in_hi (rtx *operands)
6399 {
6400   rtx ref = operands[1];
6401   rtx base, scratch;
6402   HOST_WIDE_INT offset = 0;
6403
6404   if (GET_CODE (ref) == SUBREG)
6405     {
6406       offset = SUBREG_BYTE (ref);
6407       ref = SUBREG_REG (ref);
6408     }
6409
6410   if (GET_CODE (ref) == REG)
6411     {
6412       /* We have a pseudo which has been spilt onto the stack; there
6413          are two cases here: the first where there is a simple
6414          stack-slot replacement and a second where the stack-slot is
6415          out of range, or is used as a subreg.  */
6416       if (reg_equiv_mem[REGNO (ref)])
6417         {
6418           ref = reg_equiv_mem[REGNO (ref)];
6419           base = find_replacement (&XEXP (ref, 0));
6420         }
6421       else
6422         /* The slot is out of range, or was dressed up in a SUBREG.  */
6423         base = reg_equiv_address[REGNO (ref)];
6424     }
6425   else
6426     base = find_replacement (&XEXP (ref, 0));
6427
6428   /* Handle the case where the address is too complex to be offset by 1.  */
6429   if (GET_CODE (base) == MINUS
6430       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6431     {
6432       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6433
6434       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6435       base = base_plus;
6436     }
6437   else if (GET_CODE (base) == PLUS)
6438     {
6439       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6440       HOST_WIDE_INT hi, lo;
6441
6442       offset += INTVAL (XEXP (base, 1));
6443       base = XEXP (base, 0);
6444
6445       /* Rework the address into a legal sequence of insns.  */
6446       /* Valid range for lo is -4095 -> 4095 */
6447       lo = (offset >= 0
6448             ? (offset & 0xfff)
6449             : -((-offset) & 0xfff));
6450
6451       /* Corner case, if lo is the max offset then we would be out of range
6452          once we have added the additional 1 below, so bump the msb into the
6453          pre-loading insn(s).  */
6454       if (lo == 4095)
6455         lo &= 0x7ff;
6456
6457       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6458              ^ (HOST_WIDE_INT) 0x80000000)
6459             - (HOST_WIDE_INT) 0x80000000);
6460
6461       if (hi + lo != offset)
6462         abort ();
6463
6464       if (hi != 0)
6465         {
6466           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6467
6468           /* Get the base address; addsi3 knows how to handle constants
6469              that require more than one insn.  */
6470           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6471           base = base_plus;
6472           offset = lo;
6473         }
6474     }
6475
6476   /* Operands[2] may overlap operands[0] (though it won't overlap
6477      operands[1]), that's why we asked for a DImode reg -- so we can
6478      use the bit that does not overlap.  */
6479   if (REGNO (operands[2]) == REGNO (operands[0]))
6480     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6481   else
6482     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6483
6484   emit_insn (gen_zero_extendqisi2 (scratch,
6485                                    gen_rtx_MEM (QImode,
6486                                                 plus_constant (base,
6487                                                                offset))));
6488   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6489                                    gen_rtx_MEM (QImode, 
6490                                                 plus_constant (base,
6491                                                                offset + 1))));
6492   if (!BYTES_BIG_ENDIAN)
6493     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6494                         gen_rtx_IOR (SImode, 
6495                                      gen_rtx_ASHIFT
6496                                      (SImode,
6497                                       gen_rtx_SUBREG (SImode, operands[0], 0),
6498                                       GEN_INT (8)),
6499                                      scratch)));
6500   else
6501     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6502                             gen_rtx_IOR (SImode, 
6503                                          gen_rtx_ASHIFT (SImode, scratch,
6504                                                          GEN_INT (8)),
6505                                          gen_rtx_SUBREG (SImode, operands[0],
6506                                                          0))));
6507 }
6508
6509 /* Handle storing a half-word to memory during reload by synthesizing as two
6510    byte stores.  Take care not to clobber the input values until after we
6511    have moved them somewhere safe.  This code assumes that if the DImode
6512    scratch in operands[2] overlaps either the input value or output address
6513    in some way, then that value must die in this insn (we absolutely need
6514    two scratch registers for some corner cases).  */
6515 void
6516 arm_reload_out_hi (rtx *operands)
6517 {
6518   rtx ref = operands[0];
6519   rtx outval = operands[1];
6520   rtx base, scratch;
6521   HOST_WIDE_INT offset = 0;
6522
6523   if (GET_CODE (ref) == SUBREG)
6524     {
6525       offset = SUBREG_BYTE (ref);
6526       ref = SUBREG_REG (ref);
6527     }
6528
6529   if (GET_CODE (ref) == REG)
6530     {
6531       /* We have a pseudo which has been spilt onto the stack; there
6532          are two cases here: the first where there is a simple
6533          stack-slot replacement and a second where the stack-slot is
6534          out of range, or is used as a subreg.  */
6535       if (reg_equiv_mem[REGNO (ref)])
6536         {
6537           ref = reg_equiv_mem[REGNO (ref)];
6538           base = find_replacement (&XEXP (ref, 0));
6539         }
6540       else
6541         /* The slot is out of range, or was dressed up in a SUBREG.  */
6542         base = reg_equiv_address[REGNO (ref)];
6543     }
6544   else
6545     base = find_replacement (&XEXP (ref, 0));
6546
6547   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6548
6549   /* Handle the case where the address is too complex to be offset by 1.  */
6550   if (GET_CODE (base) == MINUS
6551       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6552     {
6553       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6554
6555       /* Be careful not to destroy OUTVAL.  */
6556       if (reg_overlap_mentioned_p (base_plus, outval))
6557         {
6558           /* Updating base_plus might destroy outval, see if we can
6559              swap the scratch and base_plus.  */
6560           if (!reg_overlap_mentioned_p (scratch, outval))
6561             {
6562               rtx tmp = scratch;
6563               scratch = base_plus;
6564               base_plus = tmp;
6565             }
6566           else
6567             {
6568               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6569
6570               /* Be conservative and copy OUTVAL into the scratch now,
6571                  this should only be necessary if outval is a subreg
6572                  of something larger than a word.  */
6573               /* XXX Might this clobber base?  I can't see how it can,
6574                  since scratch is known to overlap with OUTVAL, and
6575                  must be wider than a word.  */
6576               emit_insn (gen_movhi (scratch_hi, outval));
6577               outval = scratch_hi;
6578             }
6579         }
6580
6581       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6582       base = base_plus;
6583     }
6584   else if (GET_CODE (base) == PLUS)
6585     {
6586       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6587       HOST_WIDE_INT hi, lo;
6588
6589       offset += INTVAL (XEXP (base, 1));
6590       base = XEXP (base, 0);
6591
6592       /* Rework the address into a legal sequence of insns.  */
6593       /* Valid range for lo is -4095 -> 4095 */
6594       lo = (offset >= 0
6595             ? (offset & 0xfff)
6596             : -((-offset) & 0xfff));
6597
6598       /* Corner case, if lo is the max offset then we would be out of range
6599          once we have added the additional 1 below, so bump the msb into the
6600          pre-loading insn(s).  */
6601       if (lo == 4095)
6602         lo &= 0x7ff;
6603
6604       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6605              ^ (HOST_WIDE_INT) 0x80000000)
6606             - (HOST_WIDE_INT) 0x80000000);
6607
6608       if (hi + lo != offset)
6609         abort ();
6610
6611       if (hi != 0)
6612         {
6613           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6614
6615           /* Be careful not to destroy OUTVAL.  */
6616           if (reg_overlap_mentioned_p (base_plus, outval))
6617             {
6618               /* Updating base_plus might destroy outval, see if we
6619                  can swap the scratch and base_plus.  */
6620               if (!reg_overlap_mentioned_p (scratch, outval))
6621                 {
6622                   rtx tmp = scratch;
6623                   scratch = base_plus;
6624                   base_plus = tmp;
6625                 }
6626               else
6627                 {
6628                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6629
6630                   /* Be conservative and copy outval into scratch now,
6631                      this should only be necessary if outval is a
6632                      subreg of something larger than a word.  */
6633                   /* XXX Might this clobber base?  I can't see how it
6634                      can, since scratch is known to overlap with
6635                      outval.  */
6636                   emit_insn (gen_movhi (scratch_hi, outval));
6637                   outval = scratch_hi;
6638                 }
6639             }
6640
6641           /* Get the base address; addsi3 knows how to handle constants
6642              that require more than one insn.  */
6643           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6644           base = base_plus;
6645           offset = lo;
6646         }
6647     }
6648
6649   if (BYTES_BIG_ENDIAN)
6650     {
6651       emit_insn (gen_movqi (gen_rtx_MEM (QImode, 
6652                                          plus_constant (base, offset + 1)),
6653                             gen_lowpart (QImode, outval)));
6654       emit_insn (gen_lshrsi3 (scratch,
6655                               gen_rtx_SUBREG (SImode, outval, 0),
6656                               GEN_INT (8)));
6657       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6658                             gen_lowpart (QImode, scratch)));
6659     }
6660   else
6661     {
6662       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6663                             gen_lowpart (QImode, outval)));
6664       emit_insn (gen_lshrsi3 (scratch,
6665                               gen_rtx_SUBREG (SImode, outval, 0),
6666                               GEN_INT (8)));
6667       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6668                                          plus_constant (base, offset + 1)),
6669                             gen_lowpart (QImode, scratch)));
6670     }
6671 }
6672 \f
6673 /* Print a symbolic form of X to the debug file, F.  */
6674 static void
6675 arm_print_value (FILE *f, rtx x)
6676 {
6677   switch (GET_CODE (x))
6678     {
6679     case CONST_INT:
6680       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6681       return;
6682
6683     case CONST_DOUBLE:
6684       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6685       return;
6686
6687     case CONST_VECTOR:
6688       {
6689         int i;
6690
6691         fprintf (f, "<");
6692         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6693           {
6694             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6695             if (i < (CONST_VECTOR_NUNITS (x) - 1))
6696               fputc (',', f);
6697           }
6698         fprintf (f, ">");
6699       }
6700       return;
6701
6702     case CONST_STRING:
6703       fprintf (f, "\"%s\"", XSTR (x, 0));
6704       return;
6705
6706     case SYMBOL_REF:
6707       fprintf (f, "`%s'", XSTR (x, 0));
6708       return;
6709
6710     case LABEL_REF:
6711       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6712       return;
6713
6714     case CONST:
6715       arm_print_value (f, XEXP (x, 0));
6716       return;
6717
6718     case PLUS:
6719       arm_print_value (f, XEXP (x, 0));
6720       fprintf (f, "+");
6721       arm_print_value (f, XEXP (x, 1));
6722       return;
6723
6724     case PC:
6725       fprintf (f, "pc");
6726       return;
6727
6728     default:
6729       fprintf (f, "????");
6730       return;
6731     }
6732 }
6733 \f
6734 /* Routines for manipulation of the constant pool.  */
6735
6736 /* Arm instructions cannot load a large constant directly into a
6737    register; they have to come from a pc relative load.  The constant
6738    must therefore be placed in the addressable range of the pc
6739    relative load.  Depending on the precise pc relative load
6740    instruction the range is somewhere between 256 bytes and 4k.  This
6741    means that we often have to dump a constant inside a function, and
6742    generate code to branch around it.
6743
6744    It is important to minimize this, since the branches will slow
6745    things down and make the code larger.
6746
6747    Normally we can hide the table after an existing unconditional
6748    branch so that there is no interruption of the flow, but in the
6749    worst case the code looks like this:
6750
6751         ldr     rn, L1
6752         ...
6753         b       L2
6754         align
6755         L1:     .long value
6756         L2:
6757         ...
6758
6759         ldr     rn, L3
6760         ...
6761         b       L4
6762         align
6763         L3:     .long value
6764         L4:
6765         ...
6766
6767    We fix this by performing a scan after scheduling, which notices
6768    which instructions need to have their operands fetched from the
6769    constant table and builds the table.
6770
6771    The algorithm starts by building a table of all the constants that
6772    need fixing up and all the natural barriers in the function (places
6773    where a constant table can be dropped without breaking the flow).
6774    For each fixup we note how far the pc-relative replacement will be
6775    able to reach and the offset of the instruction into the function.
6776
6777    Having built the table we then group the fixes together to form
6778    tables that are as large as possible (subject to addressing
6779    constraints) and emit each table of constants after the last
6780    barrier that is within range of all the instructions in the group.
6781    If a group does not contain a barrier, then we forcibly create one
6782    by inserting a jump instruction into the flow.  Once the table has
6783    been inserted, the insns are then modified to reference the
6784    relevant entry in the pool.
6785
6786    Possible enhancements to the algorithm (not implemented) are:
6787
6788    1) For some processors and object formats, there may be benefit in
6789    aligning the pools to the start of cache lines; this alignment
6790    would need to be taken into account when calculating addressability
6791    of a pool.  */
6792
6793 /* These typedefs are located at the start of this file, so that
6794    they can be used in the prototypes there.  This comment is to
6795    remind readers of that fact so that the following structures
6796    can be understood more easily.
6797
6798      typedef struct minipool_node    Mnode;
6799      typedef struct minipool_fixup   Mfix;  */
6800
6801 struct minipool_node
6802 {
6803   /* Doubly linked chain of entries.  */
6804   Mnode * next;
6805   Mnode * prev;
6806   /* The maximum offset into the code that this entry can be placed.  While
6807      pushing fixes for forward references, all entries are sorted in order
6808      of increasing max_address.  */
6809   HOST_WIDE_INT max_address;
6810   /* Similarly for an entry inserted for a backwards ref.  */
6811   HOST_WIDE_INT min_address;
6812   /* The number of fixes referencing this entry.  This can become zero
6813      if we "unpush" an entry.  In this case we ignore the entry when we
6814      come to emit the code.  */
6815   int refcount;
6816   /* The offset from the start of the minipool.  */
6817   HOST_WIDE_INT offset;
6818   /* The value in table.  */
6819   rtx value;
6820   /* The mode of value.  */
6821   enum machine_mode mode;
6822   /* The size of the value.  With iWMMXt enabled
6823      sizes > 4 also imply an alignment of 8-bytes.  */
6824   int fix_size;
6825 };
6826
6827 struct minipool_fixup
6828 {
6829   Mfix *            next;
6830   rtx               insn;
6831   HOST_WIDE_INT     address;
6832   rtx *             loc;
6833   enum machine_mode mode;
6834   int               fix_size;
6835   rtx               value;
6836   Mnode *           minipool;
6837   HOST_WIDE_INT     forwards;
6838   HOST_WIDE_INT     backwards;
6839 };
6840
6841 /* Fixes less than a word need padding out to a word boundary.  */
6842 #define MINIPOOL_FIX_SIZE(mode) \
6843   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6844
6845 static Mnode *  minipool_vector_head;
6846 static Mnode *  minipool_vector_tail;
6847 static rtx      minipool_vector_label;
6848
6849 /* The linked list of all minipool fixes required for this function.  */
6850 Mfix *          minipool_fix_head;
6851 Mfix *          minipool_fix_tail;
6852 /* The fix entry for the current minipool, once it has been placed.  */
6853 Mfix *          minipool_barrier;
6854
6855 /* Determines if INSN is the start of a jump table.  Returns the end
6856    of the TABLE or NULL_RTX.  */
6857 static rtx
6858 is_jump_table (rtx insn)
6859 {
6860   rtx table;
6861   
6862   if (GET_CODE (insn) == JUMP_INSN
6863       && JUMP_LABEL (insn) != NULL
6864       && ((table = next_real_insn (JUMP_LABEL (insn)))
6865           == next_real_insn (insn))
6866       && table != NULL
6867       && GET_CODE (table) == JUMP_INSN
6868       && (GET_CODE (PATTERN (table)) == ADDR_VEC
6869           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6870     return table;
6871
6872   return NULL_RTX;
6873 }
6874
6875 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6876 #define JUMP_TABLES_IN_TEXT_SECTION 0
6877 #endif
6878
6879 static HOST_WIDE_INT
6880 get_jump_table_size (rtx insn)
6881 {
6882   /* ADDR_VECs only take room if read-only data does into the text
6883      section.  */
6884   if (JUMP_TABLES_IN_TEXT_SECTION
6885 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6886       || 1
6887 #endif
6888       )
6889     {
6890       rtx body = PATTERN (insn);
6891       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6892
6893       return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6894     }
6895
6896   return 0;
6897 }
6898
6899 /* Move a minipool fix MP from its current location to before MAX_MP.
6900    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6901    constraints may need updating.  */
6902 static Mnode *
6903 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6904                                HOST_WIDE_INT max_address)
6905 {
6906   /* This should never be true and the code below assumes these are
6907      different.  */
6908   if (mp == max_mp)
6909     abort ();
6910
6911   if (max_mp == NULL)
6912     {
6913       if (max_address < mp->max_address)
6914         mp->max_address = max_address;
6915     }
6916   else
6917     {
6918       if (max_address > max_mp->max_address - mp->fix_size)
6919         mp->max_address = max_mp->max_address - mp->fix_size;
6920       else
6921         mp->max_address = max_address;
6922
6923       /* Unlink MP from its current position.  Since max_mp is non-null,
6924        mp->prev must be non-null.  */
6925       mp->prev->next = mp->next;
6926       if (mp->next != NULL)
6927         mp->next->prev = mp->prev;
6928       else
6929         minipool_vector_tail = mp->prev;
6930
6931       /* Re-insert it before MAX_MP.  */
6932       mp->next = max_mp;
6933       mp->prev = max_mp->prev;
6934       max_mp->prev = mp;
6935       
6936       if (mp->prev != NULL)
6937         mp->prev->next = mp;
6938       else
6939         minipool_vector_head = mp;
6940     }
6941
6942   /* Save the new entry.  */
6943   max_mp = mp;
6944
6945   /* Scan over the preceding entries and adjust their addresses as
6946      required.  */
6947   while (mp->prev != NULL
6948          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6949     {
6950       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6951       mp = mp->prev;
6952     }
6953
6954   return max_mp;
6955 }
6956
6957 /* Add a constant to the minipool for a forward reference.  Returns the
6958    node added or NULL if the constant will not fit in this pool.  */
6959 static Mnode *
6960 add_minipool_forward_ref (Mfix *fix)
6961 {
6962   /* If set, max_mp is the first pool_entry that has a lower
6963      constraint than the one we are trying to add.  */
6964   Mnode *       max_mp = NULL;
6965   HOST_WIDE_INT max_address = fix->address + fix->forwards;
6966   Mnode *       mp;
6967   
6968   /* If this fix's address is greater than the address of the first
6969      entry, then we can't put the fix in this pool.  We subtract the
6970      size of the current fix to ensure that if the table is fully
6971      packed we still have enough room to insert this value by suffling
6972      the other fixes forwards.  */
6973   if (minipool_vector_head &&
6974       fix->address >= minipool_vector_head->max_address - fix->fix_size)
6975     return NULL;
6976
6977   /* Scan the pool to see if a constant with the same value has
6978      already been added.  While we are doing this, also note the
6979      location where we must insert the constant if it doesn't already
6980      exist.  */
6981   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6982     {
6983       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6984           && fix->mode == mp->mode
6985           && (GET_CODE (fix->value) != CODE_LABEL
6986               || (CODE_LABEL_NUMBER (fix->value)
6987                   == CODE_LABEL_NUMBER (mp->value)))
6988           && rtx_equal_p (fix->value, mp->value))
6989         {
6990           /* More than one fix references this entry.  */
6991           mp->refcount++;
6992           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6993         }
6994
6995       /* Note the insertion point if necessary.  */
6996       if (max_mp == NULL
6997           && mp->max_address > max_address)
6998         max_mp = mp;
6999
7000       /* If we are inserting an 8-bytes aligned quantity and
7001          we have not already found an insertion point, then
7002          make sure that all such 8-byte aligned quantities are
7003          placed at the start of the pool.  */
7004       if (ARM_DOUBLEWORD_ALIGN
7005           && max_mp == NULL
7006           && fix->fix_size == 8
7007           && mp->fix_size != 8)
7008         {
7009           max_mp = mp;
7010           max_address = mp->max_address;
7011         }
7012     }
7013
7014   /* The value is not currently in the minipool, so we need to create
7015      a new entry for it.  If MAX_MP is NULL, the entry will be put on
7016      the end of the list since the placement is less constrained than
7017      any existing entry.  Otherwise, we insert the new fix before
7018      MAX_MP and, if necessary, adjust the constraints on the other
7019      entries.  */
7020   mp = xmalloc (sizeof (* mp));
7021   mp->fix_size = fix->fix_size;
7022   mp->mode = fix->mode;
7023   mp->value = fix->value;
7024   mp->refcount = 1;
7025   /* Not yet required for a backwards ref.  */
7026   mp->min_address = -65536;
7027
7028   if (max_mp == NULL)
7029     {
7030       mp->max_address = max_address;
7031       mp->next = NULL;
7032       mp->prev = minipool_vector_tail;
7033
7034       if (mp->prev == NULL)
7035         {
7036           minipool_vector_head = mp;
7037           minipool_vector_label = gen_label_rtx ();
7038         }
7039       else
7040         mp->prev->next = mp;
7041
7042       minipool_vector_tail = mp;
7043     }
7044   else
7045     {
7046       if (max_address > max_mp->max_address - mp->fix_size)
7047         mp->max_address = max_mp->max_address - mp->fix_size;
7048       else
7049         mp->max_address = max_address;
7050
7051       mp->next = max_mp;
7052       mp->prev = max_mp->prev;
7053       max_mp->prev = mp;
7054       if (mp->prev != NULL)
7055         mp->prev->next = mp;
7056       else
7057         minipool_vector_head = mp;
7058     }
7059
7060   /* Save the new entry.  */
7061   max_mp = mp;
7062
7063   /* Scan over the preceding entries and adjust their addresses as
7064      required.  */
7065   while (mp->prev != NULL
7066          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7067     {
7068       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7069       mp = mp->prev;
7070     }
7071
7072   return max_mp;
7073 }
7074
7075 static Mnode *
7076 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7077                                 HOST_WIDE_INT  min_address)
7078 {
7079   HOST_WIDE_INT offset;
7080
7081   /* This should never be true, and the code below assumes these are
7082      different.  */
7083   if (mp == min_mp)
7084     abort ();
7085
7086   if (min_mp == NULL)
7087     {
7088       if (min_address > mp->min_address)
7089         mp->min_address = min_address;
7090     }
7091   else
7092     {
7093       /* We will adjust this below if it is too loose.  */
7094       mp->min_address = min_address;
7095
7096       /* Unlink MP from its current position.  Since min_mp is non-null,
7097          mp->next must be non-null.  */
7098       mp->next->prev = mp->prev;
7099       if (mp->prev != NULL)
7100         mp->prev->next = mp->next;
7101       else
7102         minipool_vector_head = mp->next;
7103
7104       /* Reinsert it after MIN_MP.  */
7105       mp->prev = min_mp;
7106       mp->next = min_mp->next;
7107       min_mp->next = mp;
7108       if (mp->next != NULL)
7109         mp->next->prev = mp;
7110       else
7111         minipool_vector_tail = mp;
7112     }
7113
7114   min_mp = mp;
7115
7116   offset = 0;
7117   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7118     {
7119       mp->offset = offset;
7120       if (mp->refcount > 0)
7121         offset += mp->fix_size;
7122
7123       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7124         mp->next->min_address = mp->min_address + mp->fix_size;
7125     }
7126
7127   return min_mp;
7128 }      
7129
7130 /* Add a constant to the minipool for a backward reference.  Returns the
7131    node added or NULL if the constant will not fit in this pool.  
7132
7133    Note that the code for insertion for a backwards reference can be
7134    somewhat confusing because the calculated offsets for each fix do
7135    not take into account the size of the pool (which is still under
7136    construction.  */
7137 static Mnode *
7138 add_minipool_backward_ref (Mfix *fix)
7139 {
7140   /* If set, min_mp is the last pool_entry that has a lower constraint
7141      than the one we are trying to add.  */
7142   Mnode *min_mp = NULL;
7143   /* This can be negative, since it is only a constraint.  */
7144   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
7145   Mnode *mp;
7146
7147   /* If we can't reach the current pool from this insn, or if we can't
7148      insert this entry at the end of the pool without pushing other
7149      fixes out of range, then we don't try.  This ensures that we
7150      can't fail later on.  */
7151   if (min_address >= minipool_barrier->address
7152       || (minipool_vector_tail->min_address + fix->fix_size
7153           >= minipool_barrier->address))
7154     return NULL;
7155
7156   /* Scan the pool to see if a constant with the same value has
7157      already been added.  While we are doing this, also note the
7158      location where we must insert the constant if it doesn't already
7159      exist.  */
7160   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7161     {
7162       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7163           && fix->mode == mp->mode
7164           && (GET_CODE (fix->value) != CODE_LABEL
7165               || (CODE_LABEL_NUMBER (fix->value)
7166                   == CODE_LABEL_NUMBER (mp->value)))
7167           && rtx_equal_p (fix->value, mp->value)
7168           /* Check that there is enough slack to move this entry to the
7169              end of the table (this is conservative).  */
7170           && (mp->max_address 
7171               > (minipool_barrier->address 
7172                  + minipool_vector_tail->offset
7173                  + minipool_vector_tail->fix_size)))
7174         {
7175           mp->refcount++;
7176           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7177         }
7178
7179       if (min_mp != NULL)
7180         mp->min_address += fix->fix_size;
7181       else
7182         {
7183           /* Note the insertion point if necessary.  */
7184           if (mp->min_address < min_address)
7185             {
7186               /* For now, we do not allow the insertion of 8-byte alignment
7187                  requiring nodes anywhere but at the start of the pool.  */
7188               if (ARM_DOUBLEWORD_ALIGN
7189                   && fix->fix_size == 8 && mp->fix_size != 8)
7190                 return NULL;
7191               else
7192                 min_mp = mp;
7193             }
7194           else if (mp->max_address
7195                    < minipool_barrier->address + mp->offset + fix->fix_size)
7196             {
7197               /* Inserting before this entry would push the fix beyond
7198                  its maximum address (which can happen if we have
7199                  re-located a forwards fix); force the new fix to come
7200                  after it.  */
7201               min_mp = mp;
7202               min_address = mp->min_address + fix->fix_size;
7203             }
7204           /* If we are inserting an 8-bytes aligned quantity and
7205              we have not already found an insertion point, then
7206              make sure that all such 8-byte aligned quantities are
7207              placed at the start of the pool.  */
7208           else if (ARM_DOUBLEWORD_ALIGN
7209                    && min_mp == NULL
7210                    && fix->fix_size == 8
7211                    && mp->fix_size < 8)
7212             {
7213               min_mp = mp;
7214               min_address = mp->min_address + fix->fix_size;
7215             }
7216         }
7217     }
7218
7219   /* We need to create a new entry.  */
7220   mp = xmalloc (sizeof (* mp));
7221   mp->fix_size = fix->fix_size;
7222   mp->mode = fix->mode;
7223   mp->value = fix->value;
7224   mp->refcount = 1;
7225   mp->max_address = minipool_barrier->address + 65536;
7226
7227   mp->min_address = min_address;
7228
7229   if (min_mp == NULL)
7230     {
7231       mp->prev = NULL;
7232       mp->next = minipool_vector_head;
7233
7234       if (mp->next == NULL)
7235         {
7236           minipool_vector_tail = mp;
7237           minipool_vector_label = gen_label_rtx ();
7238         }
7239       else
7240         mp->next->prev = mp;
7241
7242       minipool_vector_head = mp;
7243     }
7244   else
7245     {
7246       mp->next = min_mp->next;
7247       mp->prev = min_mp;
7248       min_mp->next = mp;
7249       
7250       if (mp->next != NULL)
7251         mp->next->prev = mp;
7252       else
7253         minipool_vector_tail = mp;
7254     }
7255
7256   /* Save the new entry.  */
7257   min_mp = mp;
7258
7259   if (mp->prev)
7260     mp = mp->prev;
7261   else
7262     mp->offset = 0;
7263
7264   /* Scan over the following entries and adjust their offsets.  */
7265   while (mp->next != NULL)
7266     {
7267       if (mp->next->min_address < mp->min_address + mp->fix_size)
7268         mp->next->min_address = mp->min_address + mp->fix_size;
7269
7270       if (mp->refcount)
7271         mp->next->offset = mp->offset + mp->fix_size;
7272       else
7273         mp->next->offset = mp->offset;
7274
7275       mp = mp->next;
7276     }
7277
7278   return min_mp;
7279 }
7280
7281 static void
7282 assign_minipool_offsets (Mfix *barrier)
7283 {
7284   HOST_WIDE_INT offset = 0;
7285   Mnode *mp;
7286
7287   minipool_barrier = barrier;
7288
7289   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7290     {
7291       mp->offset = offset;
7292       
7293       if (mp->refcount > 0)
7294         offset += mp->fix_size;
7295     }
7296 }
7297
7298 /* Output the literal table */
7299 static void
7300 dump_minipool (rtx scan)
7301 {
7302   Mnode * mp;
7303   Mnode * nmp;
7304   int align64 = 0;
7305
7306   if (ARM_DOUBLEWORD_ALIGN)
7307     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7308       if (mp->refcount > 0 && mp->fix_size == 8)
7309         {
7310           align64 = 1;
7311           break;
7312         }
7313
7314   if (dump_file)
7315     fprintf (dump_file,
7316              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7317              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7318
7319   scan = emit_label_after (gen_label_rtx (), scan);
7320   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7321   scan = emit_label_after (minipool_vector_label, scan);
7322
7323   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7324     {
7325       if (mp->refcount > 0)
7326         {
7327           if (dump_file)
7328             {
7329               fprintf (dump_file, 
7330                        ";;  Offset %u, min %ld, max %ld ",
7331                        (unsigned) mp->offset, (unsigned long) mp->min_address,
7332                        (unsigned long) mp->max_address);
7333               arm_print_value (dump_file, mp->value);
7334               fputc ('\n', dump_file);
7335             }
7336
7337           switch (mp->fix_size)
7338             {
7339 #ifdef HAVE_consttable_1
7340             case 1:
7341               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7342               break;
7343
7344 #endif
7345 #ifdef HAVE_consttable_2
7346             case 2:
7347               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7348               break;
7349
7350 #endif
7351 #ifdef HAVE_consttable_4
7352             case 4:
7353               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7354               break;
7355
7356 #endif
7357 #ifdef HAVE_consttable_8
7358             case 8:
7359               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7360               break;
7361
7362 #endif
7363             default:
7364               abort ();
7365               break;
7366             }
7367         }
7368
7369       nmp = mp->next;
7370       free (mp);
7371     }
7372
7373   minipool_vector_head = minipool_vector_tail = NULL;
7374   scan = emit_insn_after (gen_consttable_end (), scan);
7375   scan = emit_barrier_after (scan);
7376 }
7377
7378 /* Return the cost of forcibly inserting a barrier after INSN.  */
7379 static int
7380 arm_barrier_cost (rtx insn)
7381 {
7382   /* Basing the location of the pool on the loop depth is preferable,
7383      but at the moment, the basic block information seems to be
7384      corrupt by this stage of the compilation.  */
7385   int base_cost = 50;
7386   rtx next = next_nonnote_insn (insn);
7387
7388   if (next != NULL && GET_CODE (next) == CODE_LABEL)
7389     base_cost -= 20;
7390
7391   switch (GET_CODE (insn))
7392     {
7393     case CODE_LABEL:
7394       /* It will always be better to place the table before the label, rather
7395          than after it.  */
7396       return 50;  
7397
7398     case INSN:
7399     case CALL_INSN:
7400       return base_cost;
7401
7402     case JUMP_INSN:
7403       return base_cost - 10;
7404
7405     default:
7406       return base_cost + 10;
7407     }
7408 }
7409
7410 /* Find the best place in the insn stream in the range
7411    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7412    Create the barrier by inserting a jump and add a new fix entry for
7413    it.  */
7414 static Mfix *
7415 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7416 {
7417   HOST_WIDE_INT count = 0;
7418   rtx barrier;
7419   rtx from = fix->insn;
7420   rtx selected = from;
7421   int selected_cost;
7422   HOST_WIDE_INT selected_address;
7423   Mfix * new_fix;
7424   HOST_WIDE_INT max_count = max_address - fix->address;
7425   rtx label = gen_label_rtx ();
7426
7427   selected_cost = arm_barrier_cost (from);
7428   selected_address = fix->address;
7429
7430   while (from && count < max_count)
7431     {
7432       rtx tmp;
7433       int new_cost;
7434
7435       /* This code shouldn't have been called if there was a natural barrier
7436          within range.  */
7437       if (GET_CODE (from) == BARRIER)
7438         abort ();
7439
7440       /* Count the length of this insn.  */
7441       count += get_attr_length (from);
7442
7443       /* If there is a jump table, add its length.  */
7444       tmp = is_jump_table (from);
7445       if (tmp != NULL)
7446         {
7447           count += get_jump_table_size (tmp);
7448
7449           /* Jump tables aren't in a basic block, so base the cost on
7450              the dispatch insn.  If we select this location, we will
7451              still put the pool after the table.  */
7452           new_cost = arm_barrier_cost (from);
7453
7454           if (count < max_count && new_cost <= selected_cost)
7455             {
7456               selected = tmp;
7457               selected_cost = new_cost;
7458               selected_address = fix->address + count;
7459             }
7460
7461           /* Continue after the dispatch table.  */
7462           from = NEXT_INSN (tmp);
7463           continue;
7464         }
7465
7466       new_cost = arm_barrier_cost (from);
7467       
7468       if (count < max_count && new_cost <= selected_cost)
7469         {
7470           selected = from;
7471           selected_cost = new_cost;
7472           selected_address = fix->address + count;
7473         }
7474
7475       from = NEXT_INSN (from);
7476     }
7477
7478   /* Create a new JUMP_INSN that branches around a barrier.  */
7479   from = emit_jump_insn_after (gen_jump (label), selected);
7480   JUMP_LABEL (from) = label;
7481   barrier = emit_barrier_after (from);
7482   emit_label_after (label, barrier);
7483
7484   /* Create a minipool barrier entry for the new barrier.  */
7485   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7486   new_fix->insn = barrier;
7487   new_fix->address = selected_address;
7488   new_fix->next = fix->next;
7489   fix->next = new_fix;
7490
7491   return new_fix;
7492 }
7493
7494 /* Record that there is a natural barrier in the insn stream at
7495    ADDRESS.  */
7496 static void
7497 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7498 {
7499   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7500
7501   fix->insn = insn;
7502   fix->address = address;
7503
7504   fix->next = NULL;
7505   if (minipool_fix_head != NULL)
7506     minipool_fix_tail->next = fix;
7507   else
7508     minipool_fix_head = fix;
7509
7510   minipool_fix_tail = fix;
7511 }
7512
7513 /* Record INSN, which will need fixing up to load a value from the
7514    minipool.  ADDRESS is the offset of the insn since the start of the
7515    function; LOC is a pointer to the part of the insn which requires
7516    fixing; VALUE is the constant that must be loaded, which is of type
7517    MODE.  */
7518 static void
7519 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7520                    enum machine_mode mode, rtx value)
7521 {
7522   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7523
7524 #ifdef AOF_ASSEMBLER
7525   /* PIC symbol references need to be converted into offsets into the
7526      based area.  */
7527   /* XXX This shouldn't be done here.  */
7528   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7529     value = aof_pic_entry (value);
7530 #endif /* AOF_ASSEMBLER */
7531
7532   fix->insn = insn;
7533   fix->address = address;
7534   fix->loc = loc;
7535   fix->mode = mode;
7536   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7537   fix->value = value;
7538   fix->forwards = get_attr_pool_range (insn);
7539   fix->backwards = get_attr_neg_pool_range (insn);
7540   fix->minipool = NULL;
7541
7542   /* If an insn doesn't have a range defined for it, then it isn't
7543      expecting to be reworked by this code.  Better to abort now than
7544      to generate duff assembly code.  */
7545   if (fix->forwards == 0 && fix->backwards == 0)
7546     abort ();
7547
7548   /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7549      So there might be an empty word before the start of the pool.
7550      Hence we reduce the forward range by 4 to allow for this
7551      possibility.  */
7552   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7553     fix->forwards -= 4;
7554
7555   if (dump_file)
7556     {
7557       fprintf (dump_file,
7558                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7559                GET_MODE_NAME (mode),
7560                INSN_UID (insn), (unsigned long) address, 
7561                -1 * (long)fix->backwards, (long)fix->forwards);
7562       arm_print_value (dump_file, fix->value);
7563       fprintf (dump_file, "\n");
7564     }
7565
7566   /* Add it to the chain of fixes.  */
7567   fix->next = NULL;
7568   
7569   if (minipool_fix_head != NULL)
7570     minipool_fix_tail->next = fix;
7571   else
7572     minipool_fix_head = fix;
7573
7574   minipool_fix_tail = fix;
7575 }
7576
7577 /* Scan INSN and note any of its operands that need fixing.
7578    If DO_PUSHES is false we do not actually push any of the fixups
7579    needed.  The function returns TRUE is any fixups were needed/pushed.
7580    This is used by arm_memory_load_p() which needs to know about loads
7581    of constants that will be converted into minipool loads.  */
7582 static bool
7583 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7584 {
7585   bool result = false;
7586   int opno;
7587
7588   extract_insn (insn);
7589
7590   if (!constrain_operands (1))
7591     fatal_insn_not_found (insn);
7592
7593   if (recog_data.n_alternatives == 0)
7594     return false;
7595
7596   /* Fill in recog_op_alt with information about the constraints of this insn.  */
7597   preprocess_constraints ();
7598
7599   for (opno = 0; opno < recog_data.n_operands; opno++)
7600     {
7601       /* Things we need to fix can only occur in inputs.  */
7602       if (recog_data.operand_type[opno] != OP_IN)
7603         continue;
7604
7605       /* If this alternative is a memory reference, then any mention
7606          of constants in this alternative is really to fool reload
7607          into allowing us to accept one there.  We need to fix them up
7608          now so that we output the right code.  */
7609       if (recog_op_alt[opno][which_alternative].memory_ok)
7610         {
7611           rtx op = recog_data.operand[opno];
7612
7613           if (CONSTANT_P (op))
7614             {
7615               if (do_pushes)
7616                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7617                                    recog_data.operand_mode[opno], op);
7618               result = true;
7619             }
7620           else if (GET_CODE (op) == MEM
7621                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7622                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7623             {
7624               if (do_pushes)
7625                 {
7626                   rtx cop = avoid_constant_pool_reference (op);
7627
7628                   /* Casting the address of something to a mode narrower
7629                      than a word can cause avoid_constant_pool_reference()
7630                      to return the pool reference itself.  That's no good to
7631                      us here.  Lets just hope that we can use the 
7632                      constant pool value directly.  */
7633                   if (op == cop)
7634                     cop = get_pool_constant (XEXP (op, 0));
7635
7636                   push_minipool_fix (insn, address,
7637                                      recog_data.operand_loc[opno],
7638                                      recog_data.operand_mode[opno], cop);
7639                 }
7640
7641               result = true;
7642             }
7643         }
7644     }
7645
7646   return result;
7647 }
7648
7649 /* Gcc puts the pool in the wrong place for ARM, since we can only
7650    load addresses a limited distance around the pc.  We do some
7651    special munging to move the constant pool values to the correct
7652    point in the code.  */
7653 static void
7654 arm_reorg (void)
7655 {
7656   rtx insn;
7657   HOST_WIDE_INT address = 0;
7658   Mfix * fix;
7659
7660   minipool_fix_head = minipool_fix_tail = NULL;
7661
7662   /* The first insn must always be a note, or the code below won't
7663      scan it properly.  */
7664   insn = get_insns ();
7665   if (GET_CODE (insn) != NOTE)
7666     abort ();
7667
7668   /* Scan all the insns and record the operands that will need fixing.  */
7669   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7670     {
7671       if (TARGET_CIRRUS_FIX_INVALID_INSNS
7672           && (arm_cirrus_insn_p (insn)
7673               || GET_CODE (insn) == JUMP_INSN
7674               || arm_memory_load_p (insn)))
7675         cirrus_reorg (insn);
7676
7677       if (GET_CODE (insn) == BARRIER)
7678         push_minipool_barrier (insn, address);
7679       else if (INSN_P (insn))
7680         {
7681           rtx table;
7682
7683           note_invalid_constants (insn, address, true);
7684           address += get_attr_length (insn);
7685
7686           /* If the insn is a vector jump, add the size of the table
7687              and skip the table.  */
7688           if ((table = is_jump_table (insn)) != NULL)
7689             {
7690               address += get_jump_table_size (table);
7691               insn = table;
7692             }
7693         }
7694     }
7695
7696   fix = minipool_fix_head;
7697   
7698   /* Now scan the fixups and perform the required changes.  */
7699   while (fix)
7700     {
7701       Mfix * ftmp;
7702       Mfix * fdel;
7703       Mfix *  last_added_fix;
7704       Mfix * last_barrier = NULL;
7705       Mfix * this_fix;
7706
7707       /* Skip any further barriers before the next fix.  */
7708       while (fix && GET_CODE (fix->insn) == BARRIER)
7709         fix = fix->next;
7710
7711       /* No more fixes.  */
7712       if (fix == NULL)
7713         break;
7714
7715       last_added_fix = NULL;
7716
7717       for (ftmp = fix; ftmp; ftmp = ftmp->next)
7718         {
7719           if (GET_CODE (ftmp->insn) == BARRIER)
7720             {
7721               if (ftmp->address >= minipool_vector_head->max_address)
7722                 break;
7723
7724               last_barrier = ftmp;
7725             }
7726           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7727             break;
7728
7729           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
7730         }
7731
7732       /* If we found a barrier, drop back to that; any fixes that we
7733          could have reached but come after the barrier will now go in
7734          the next mini-pool.  */
7735       if (last_barrier != NULL)
7736         {
7737           /* Reduce the refcount for those fixes that won't go into this 
7738              pool after all.  */
7739           for (fdel = last_barrier->next;
7740                fdel && fdel != ftmp;
7741                fdel = fdel->next)
7742             {
7743               fdel->minipool->refcount--;
7744               fdel->minipool = NULL;
7745             }
7746
7747           ftmp = last_barrier;
7748         }
7749       else
7750         {
7751           /* ftmp is first fix that we can't fit into this pool and
7752              there no natural barriers that we could use.  Insert a
7753              new barrier in the code somewhere between the previous
7754              fix and this one, and arrange to jump around it.  */
7755           HOST_WIDE_INT max_address;
7756
7757           /* The last item on the list of fixes must be a barrier, so
7758              we can never run off the end of the list of fixes without
7759              last_barrier being set.  */
7760           if (ftmp == NULL)
7761             abort ();
7762
7763           max_address = minipool_vector_head->max_address;
7764           /* Check that there isn't another fix that is in range that
7765              we couldn't fit into this pool because the pool was
7766              already too large: we need to put the pool before such an
7767              instruction.  */
7768           if (ftmp->address < max_address)
7769             max_address = ftmp->address;
7770
7771           last_barrier = create_fix_barrier (last_added_fix, max_address);
7772         }
7773
7774       assign_minipool_offsets (last_barrier);
7775
7776       while (ftmp)
7777         {
7778           if (GET_CODE (ftmp->insn) != BARRIER
7779               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7780                   == NULL))
7781             break;
7782
7783           ftmp = ftmp->next;
7784         }
7785
7786       /* Scan over the fixes we have identified for this pool, fixing them
7787          up and adding the constants to the pool itself.  */
7788       for (this_fix = fix; this_fix && ftmp != this_fix;
7789            this_fix = this_fix->next)
7790         if (GET_CODE (this_fix->insn) != BARRIER)
7791           {
7792             rtx addr
7793               = plus_constant (gen_rtx_LABEL_REF (VOIDmode, 
7794                                                   minipool_vector_label),
7795                                this_fix->minipool->offset);
7796             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7797           }
7798
7799       dump_minipool (last_barrier->insn);
7800       fix = ftmp;
7801     }
7802
7803   /* From now on we must synthesize any constants that we can't handle
7804      directly.  This can happen if the RTL gets split during final
7805      instruction generation.  */
7806   after_arm_reorg = 1;
7807
7808   /* Free the minipool memory.  */
7809   obstack_free (&minipool_obstack, minipool_startobj);
7810 }
7811 \f
7812 /* Routines to output assembly language.  */
7813
7814 /* If the rtx is the correct value then return the string of the number.
7815    In this way we can ensure that valid double constants are generated even
7816    when cross compiling.  */
7817 const char *
7818 fp_immediate_constant (rtx x)
7819 {
7820   REAL_VALUE_TYPE r;
7821   int i;
7822   
7823   if (!fp_consts_inited)
7824     init_fp_table ();
7825   
7826   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7827   for (i = 0; i < 8; i++)
7828     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7829       return strings_fp[i];
7830
7831   abort ();
7832 }
7833
7834 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
7835 static const char *
7836 fp_const_from_val (REAL_VALUE_TYPE *r)
7837 {
7838   int i;
7839
7840   if (!fp_consts_inited)
7841     init_fp_table ();
7842
7843   for (i = 0; i < 8; i++)
7844     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7845       return strings_fp[i];
7846
7847   abort ();
7848 }
7849
7850 /* Output the operands of a LDM/STM instruction to STREAM.
7851    MASK is the ARM register set mask of which only bits 0-15 are important.
7852    REG is the base register, either the frame pointer or the stack pointer,
7853    INSTR is the possibly suffixed load or store instruction.  */
7854 static void
7855 print_multi_reg (FILE *stream, const char *instr, int reg, int mask)
7856 {
7857   int i;
7858   int not_first = FALSE;
7859
7860   fputc ('\t', stream);
7861   asm_fprintf (stream, instr, reg);
7862   fputs (", {", stream);
7863   
7864   for (i = 0; i <= LAST_ARM_REGNUM; i++)
7865     if (mask & (1 << i))
7866       {
7867         if (not_first)
7868           fprintf (stream, ", ");
7869         
7870         asm_fprintf (stream, "%r", i);
7871         not_first = TRUE;
7872       }
7873
7874   fprintf (stream, "}");
7875
7876   /* Add a ^ character for the 26-bit ABI, but only if we were loading
7877      the PC.  Otherwise we would generate an UNPREDICTABLE instruction.
7878      Strictly speaking the instruction would be unpredicatble only if
7879      we were writing back the base register as well, but since we never
7880      want to generate an LDM type 2 instruction (register bank switching)
7881      which is what you get if the PC is not being loaded, we do not need
7882      to check for writeback.  */
7883   if (! TARGET_APCS_32
7884       && ((mask & (1 << PC_REGNUM)) != 0))
7885     fprintf (stream, "^");
7886   
7887   fprintf (stream, "\n");
7888 }
7889
7890
7891 /* Output a FLDMX instruction to STREAM.
7892    BASE if the register containing the address.
7893    REG and COUNT specify the register range.
7894    Extra registers may be added to avoid hardware bugs.  */
7895
7896 static void
7897 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7898 {
7899   int i;
7900
7901   /* Workaround ARM10 VFPr1 bug.  */
7902   if (count == 2 && !arm_arch6)
7903     {
7904       if (reg == 15)
7905         reg--;
7906       count++;
7907     }
7908
7909   fputc ('\t', stream);
7910   asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7911
7912   for (i = reg; i < reg + count; i++)
7913     {
7914       if (i > reg)
7915         fputs (", ", stream);
7916       asm_fprintf (stream, "d%d", i);
7917     }
7918   fputs ("}\n", stream);
7919
7920 }
7921
7922
7923 /* Output the assembly for a store multiple.  */
7924
7925 const char *
7926 vfp_output_fstmx (rtx * operands)
7927 {
7928   char pattern[100];
7929   int p;
7930   int base;
7931   int i;
7932
7933   strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7934   p = strlen (pattern);
7935
7936   if (GET_CODE (operands[1]) != REG)
7937     abort ();
7938
7939   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7940   for (i = 1; i < XVECLEN (operands[2], 0); i++)
7941     {
7942       p += sprintf (&pattern[p], ", d%d", base + i);
7943     }
7944   strcpy (&pattern[p], "}");
7945
7946   output_asm_insn (pattern, operands);
7947   return "";
7948 }
7949
7950
7951 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
7952    number of bytes pushed.  */
7953
7954 static int
7955 vfp_emit_fstmx (int base_reg, int count)
7956 {
7957   rtx par;
7958   rtx dwarf;
7959   rtx tmp, reg;
7960   int i;
7961
7962   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
7963      register pairs are stored by a store multiple insn.  We avoid this
7964      by pushing an extra pair.  */
7965   if (count == 2 && !arm_arch6)
7966     {
7967       if (base_reg == LAST_VFP_REGNUM - 3)
7968         base_reg -= 2;
7969       count++;
7970     }
7971
7972   /* ??? The frame layout is implementation defined.  We describe
7973      standard format 1 (equivalent to a FSTMD insn and unused pad word).
7974      We really need some way of representing the whole block so that the
7975      unwinder can figure it out at runtime.  */
7976   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7977   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
7978
7979   reg = gen_rtx_REG (DFmode, base_reg);
7980   base_reg += 2;
7981
7982   XVECEXP (par, 0, 0)
7983     = gen_rtx_SET (VOIDmode,
7984                    gen_rtx_MEM (BLKmode,
7985                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7986                    gen_rtx_UNSPEC (BLKmode,
7987                                    gen_rtvec (1, reg),
7988                                    UNSPEC_PUSH_MULT));
7989
7990   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7991                      gen_rtx_PLUS (SImode, stack_pointer_rtx,
7992                                    GEN_INT (-(count * 8 + 4))));
7993   RTX_FRAME_RELATED_P (tmp) = 1;
7994   XVECEXP (dwarf, 0, 0) = tmp;
7995
7996   tmp = gen_rtx_SET (VOIDmode,
7997                      gen_rtx_MEM (DFmode, stack_pointer_rtx),
7998                      reg);
7999   RTX_FRAME_RELATED_P (tmp) = 1;
8000   XVECEXP (dwarf, 0, 1) = tmp;
8001
8002   for (i = 1; i < count; i++)
8003     {
8004       reg = gen_rtx_REG (DFmode, base_reg);
8005       base_reg += 2;
8006       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8007
8008       tmp = gen_rtx_SET (VOIDmode,
8009                          gen_rtx_MEM (DFmode,
8010                                       gen_rtx_PLUS (SImode,
8011                                                     stack_pointer_rtx,
8012                                                     GEN_INT (i * 8))),
8013                          reg);
8014       RTX_FRAME_RELATED_P (tmp) = 1;
8015       XVECEXP (dwarf, 0, i + 1) = tmp;
8016     }
8017
8018   par = emit_insn (par);
8019   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8020                                        REG_NOTES (par));
8021   RTX_FRAME_RELATED_P (par) = 1;
8022
8023   return count * 8 + 4;
8024 }
8025
8026
8027 /* Output a 'call' insn.  */
8028 const char *
8029 output_call (rtx *operands)
8030 {
8031   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
8032
8033   if (REGNO (operands[0]) == LR_REGNUM)
8034     {
8035       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8036       output_asm_insn ("mov%?\t%0, %|lr", operands);
8037     }
8038   
8039   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8040   
8041   if (TARGET_INTERWORK)
8042     output_asm_insn ("bx%?\t%0", operands);
8043   else
8044     output_asm_insn ("mov%?\t%|pc, %0", operands);
8045   
8046   return "";
8047 }
8048
8049 /* Output a 'call' insn that is a reference in memory.  */
8050 const char *
8051 output_call_mem (rtx *operands)
8052 {
8053   if (TARGET_INTERWORK)
8054     {
8055       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8056       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8057       output_asm_insn ("bx%?\t%|ip", operands);
8058     }
8059   else if (regno_use_in (LR_REGNUM, operands[0]))
8060     {
8061       /* LR is used in the memory address.  We load the address in the
8062          first instruction.  It's safe to use IP as the target of the
8063          load since the call will kill it anyway.  */
8064       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8065       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8066       output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8067     }
8068   else
8069     {
8070       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8071       output_asm_insn ("ldr%?\t%|pc, %0", operands);
8072     }
8073
8074   return "";
8075 }
8076
8077
8078 /* Output a move from arm registers to an fpa registers.
8079    OPERANDS[0] is an fpa register.
8080    OPERANDS[1] is the first registers of an arm register pair.  */
8081 const char *
8082 output_mov_long_double_fpa_from_arm (rtx *operands)
8083 {
8084   int arm_reg0 = REGNO (operands[1]);
8085   rtx ops[3];
8086
8087   if (arm_reg0 == IP_REGNUM)
8088     abort ();
8089
8090   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8091   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8092   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8093   
8094   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8095   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8096   
8097   return "";
8098 }
8099
8100 /* Output a move from an fpa register to arm registers.
8101    OPERANDS[0] is the first registers of an arm register pair.
8102    OPERANDS[1] is an fpa register.  */
8103 const char *
8104 output_mov_long_double_arm_from_fpa (rtx *operands)
8105 {
8106   int arm_reg0 = REGNO (operands[0]);
8107   rtx ops[3];
8108
8109   if (arm_reg0 == IP_REGNUM)
8110     abort ();
8111
8112   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8113   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8114   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8115
8116   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8117   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8118   return "";
8119 }
8120
8121 /* Output a move from arm registers to arm registers of a long double
8122    OPERANDS[0] is the destination.
8123    OPERANDS[1] is the source.  */
8124 const char *
8125 output_mov_long_double_arm_from_arm (rtx *operands)
8126 {
8127   /* We have to be careful here because the two might overlap.  */
8128   int dest_start = REGNO (operands[0]);
8129   int src_start = REGNO (operands[1]);
8130   rtx ops[2];
8131   int i;
8132
8133   if (dest_start < src_start)
8134     {
8135       for (i = 0; i < 3; i++)
8136         {
8137           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8138           ops[1] = gen_rtx_REG (SImode, src_start + i);
8139           output_asm_insn ("mov%?\t%0, %1", ops);
8140         }
8141     }
8142   else
8143     {
8144       for (i = 2; i >= 0; i--)
8145         {
8146           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8147           ops[1] = gen_rtx_REG (SImode, src_start + i);
8148           output_asm_insn ("mov%?\t%0, %1", ops);
8149         }
8150     }
8151
8152   return "";
8153 }
8154
8155
8156 /* Output a move from arm registers to an fpa registers.
8157    OPERANDS[0] is an fpa register.
8158    OPERANDS[1] is the first registers of an arm register pair.  */
8159 const char *
8160 output_mov_double_fpa_from_arm (rtx *operands)
8161 {
8162   int arm_reg0 = REGNO (operands[1]);
8163   rtx ops[2];
8164
8165   if (arm_reg0 == IP_REGNUM)
8166     abort ();
8167   
8168   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8169   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8170   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8171   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8172   return "";
8173 }
8174
8175 /* Output a move from an fpa register to arm registers.
8176    OPERANDS[0] is the first registers of an arm register pair.
8177    OPERANDS[1] is an fpa register.  */
8178 const char *
8179 output_mov_double_arm_from_fpa (rtx *operands)
8180 {
8181   int arm_reg0 = REGNO (operands[0]);
8182   rtx ops[2];
8183
8184   if (arm_reg0 == IP_REGNUM)
8185     abort ();
8186
8187   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8188   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8189   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8190   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8191   return "";
8192 }
8193
8194 /* Output a move between double words.
8195    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8196    or MEM<-REG and all MEMs must be offsettable addresses.  */
8197 const char *
8198 output_move_double (rtx *operands)
8199 {
8200   enum rtx_code code0 = GET_CODE (operands[0]);
8201   enum rtx_code code1 = GET_CODE (operands[1]);
8202   rtx otherops[3];
8203
8204   if (code0 == REG)
8205     {
8206       int reg0 = REGNO (operands[0]);
8207
8208       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8209       
8210       if (code1 == REG)
8211         {
8212           int reg1 = REGNO (operands[1]);
8213           if (reg1 == IP_REGNUM)
8214             abort ();
8215
8216           /* Ensure the second source is not overwritten.  */
8217           if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
8218             output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
8219           else
8220             output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
8221         }
8222       else if (code1 == CONST_VECTOR)
8223         {
8224           HOST_WIDE_INT hint = 0;
8225
8226           switch (GET_MODE (operands[1]))
8227             {
8228             case V2SImode:
8229               otherops[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 1)));
8230               operands[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 0)));
8231               break;
8232
8233             case V4HImode:
8234               if (BYTES_BIG_ENDIAN)
8235                 {
8236                   hint = INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8237                   hint <<= 16;
8238                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8239                 }
8240               else
8241                 {
8242                   hint = INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8243                   hint <<= 16;
8244                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8245                 }
8246
8247               otherops[1] = GEN_INT (hint);
8248               hint = 0;
8249
8250               if (BYTES_BIG_ENDIAN)
8251                 {
8252                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8253                   hint <<= 16;
8254                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8255                 }
8256               else
8257                 {
8258                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8259                   hint <<= 16;
8260                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8261                 }
8262
8263               operands[1] = GEN_INT (hint);
8264               break;
8265
8266             case V8QImode:
8267               if (BYTES_BIG_ENDIAN)
8268                 {
8269                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8270                   hint <<= 8;
8271                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8272                   hint <<= 8;
8273                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8274                   hint <<= 8;
8275                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8276                 }
8277               else
8278                 {
8279                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8280                   hint <<= 8;
8281                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8282                   hint <<= 8;
8283                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8284                   hint <<= 8;
8285                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8286                 }
8287
8288               otherops[1] = GEN_INT (hint);
8289               hint = 0;
8290
8291               if (BYTES_BIG_ENDIAN)
8292                 {
8293                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8294                   hint <<= 8;
8295                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8296                   hint <<= 8;
8297                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8298                   hint <<= 8;
8299                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8300                 }
8301               else
8302                 {
8303                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8304                   hint <<= 8;
8305                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8306                   hint <<= 8;
8307                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8308                   hint <<= 8;
8309                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8310                 }
8311
8312               operands[1] = GEN_INT (hint);
8313               break;
8314               
8315             default:
8316               abort ();
8317             }
8318           output_mov_immediate (operands);
8319           output_mov_immediate (otherops);
8320         }
8321       else if (code1 == CONST_DOUBLE)
8322         {
8323           if (GET_MODE (operands[1]) == DFmode)
8324             {
8325               REAL_VALUE_TYPE r;
8326               long l[2];
8327
8328               REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
8329               REAL_VALUE_TO_TARGET_DOUBLE (r, l);
8330               otherops[1] = GEN_INT (l[1]);
8331               operands[1] = GEN_INT (l[0]);
8332             }
8333           else if (GET_MODE (operands[1]) != VOIDmode)
8334             abort ();
8335           else if (WORDS_BIG_ENDIAN)
8336             {
8337               otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8338               operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8339             }
8340           else
8341             {
8342               otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8343               operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8344             }
8345           
8346           output_mov_immediate (operands);
8347           output_mov_immediate (otherops);
8348         }
8349       else if (code1 == CONST_INT)
8350         {
8351 #if HOST_BITS_PER_WIDE_INT > 32
8352           /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
8353              what the upper word is.  */
8354           if (WORDS_BIG_ENDIAN)
8355             {
8356               otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8357               operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8358             }
8359           else
8360             {
8361               otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8362               operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8363             }
8364 #else
8365           /* Sign extend the intval into the high-order word.  */
8366           if (WORDS_BIG_ENDIAN)
8367             {
8368               otherops[1] = operands[1];
8369               operands[1] = (INTVAL (operands[1]) < 0
8370                              ? constm1_rtx : const0_rtx);
8371             }
8372           else
8373             otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
8374 #endif
8375           output_mov_immediate (otherops);
8376           output_mov_immediate (operands);
8377         }
8378       else if (code1 == MEM)
8379         {
8380           switch (GET_CODE (XEXP (operands[1], 0)))
8381             {
8382             case REG:
8383               output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8384               break;
8385
8386             case PRE_INC:
8387               abort (); /* Should never happen now.  */
8388               break;
8389
8390             case PRE_DEC:
8391               output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8392               break;
8393
8394             case POST_INC:
8395               output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8396               break;
8397
8398             case POST_DEC:
8399               abort (); /* Should never happen now.  */
8400               break;
8401
8402             case LABEL_REF:
8403             case CONST:
8404               output_asm_insn ("adr%?\t%0, %1", operands);
8405               output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8406               break;
8407
8408             default:
8409               if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8410                                    GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8411                 {
8412                   otherops[0] = operands[0];
8413                   otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8414                   otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8415
8416                   if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8417                     {
8418                       if (GET_CODE (otherops[2]) == CONST_INT)
8419                         {
8420                           switch ((int) INTVAL (otherops[2]))
8421                             {
8422                             case -8:
8423                               output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8424                               return "";
8425                             case -4:
8426                               output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8427                               return "";
8428                             case 4:
8429                               output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8430                               return "";
8431                             }
8432
8433                           if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8434                             output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8435                           else
8436                             output_asm_insn ("add%?\t%0, %1, %2", otherops);
8437                         }
8438                       else
8439                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
8440                     }
8441                   else
8442                     output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8443                   
8444                   return "ldm%?ia\t%0, %M0";
8445                 }
8446               else
8447                 {
8448                   otherops[1] = adjust_address (operands[1], SImode, 4);
8449                   /* Take care of overlapping base/data reg.  */
8450                   if (reg_mentioned_p (operands[0], operands[1]))
8451                     {
8452                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8453                       output_asm_insn ("ldr%?\t%0, %1", operands);
8454                     }
8455                   else
8456                     {
8457                       output_asm_insn ("ldr%?\t%0, %1", operands);
8458                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8459                     }
8460                 }
8461             }
8462         }
8463       else
8464         abort ();  /* Constraints should prevent this.  */
8465     }
8466   else if (code0 == MEM && code1 == REG)
8467     {
8468       if (REGNO (operands[1]) == IP_REGNUM)
8469         abort ();
8470
8471       switch (GET_CODE (XEXP (operands[0], 0)))
8472         {
8473         case REG:
8474           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8475           break;
8476
8477         case PRE_INC:
8478           abort (); /* Should never happen now.  */
8479           break;
8480
8481         case PRE_DEC:
8482           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8483           break;
8484
8485         case POST_INC:
8486           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8487           break;
8488
8489         case POST_DEC:
8490           abort (); /* Should never happen now.  */
8491           break;
8492
8493         case PLUS:
8494           if (GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == CONST_INT)
8495             {
8496               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8497                 {
8498                 case -8:
8499                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
8500                   return "";
8501
8502                 case -4:
8503                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
8504                   return "";
8505
8506                 case 4:
8507                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8508                   return "";
8509                 }
8510             }
8511           /* Fall through */
8512
8513         default:
8514           otherops[0] = adjust_address (operands[0], SImode, 4);
8515           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8516           output_asm_insn ("str%?\t%1, %0", operands);
8517           output_asm_insn ("str%?\t%1, %0", otherops);
8518         }
8519     }
8520   else
8521     /* Constraints should prevent this.  */
8522     abort ();
8523
8524   return "";
8525 }
8526
8527
8528 /* Output an arbitrary MOV reg, #n.
8529    OPERANDS[0] is a register.  OPERANDS[1] is a const_int.  */
8530 const char *
8531 output_mov_immediate (rtx *operands)
8532 {
8533   HOST_WIDE_INT n = INTVAL (operands[1]);
8534
8535   /* Try to use one MOV.  */
8536   if (const_ok_for_arm (n))
8537     output_asm_insn ("mov%?\t%0, %1", operands);
8538
8539   /* Try to use one MVN.  */
8540   else if (const_ok_for_arm (~n))
8541     {
8542       operands[1] = GEN_INT (~n);
8543       output_asm_insn ("mvn%?\t%0, %1", operands);
8544     }
8545   else
8546     {
8547       int n_ones = 0;
8548       int i;
8549
8550       /* If all else fails, make it out of ORRs or BICs as appropriate.  */
8551       for (i = 0; i < 32; i++)
8552         if (n & 1 << i)
8553           n_ones++;
8554
8555       if (n_ones > 16)  /* Shorter to use MVN with BIC in this case.  */
8556         output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1, ~ n);
8557       else
8558         output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
8559     }
8560
8561   return "";
8562 }
8563
8564 /* Output an ADD r, s, #n where n may be too big for one instruction.
8565    If adding zero to one register, output nothing.  */
8566 const char *
8567 output_add_immediate (rtx *operands)
8568 {
8569   HOST_WIDE_INT n = INTVAL (operands[2]);
8570
8571   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8572     {
8573       if (n < 0)
8574         output_multi_immediate (operands,
8575                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8576                                 -n);
8577       else
8578         output_multi_immediate (operands,
8579                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8580                                 n);
8581     }
8582
8583   return "";
8584 }
8585
8586 /* Output a multiple immediate operation.
8587    OPERANDS is the vector of operands referred to in the output patterns.
8588    INSTR1 is the output pattern to use for the first constant.
8589    INSTR2 is the output pattern to use for subsequent constants.
8590    IMMED_OP is the index of the constant slot in OPERANDS.
8591    N is the constant value.  */
8592 static const char *
8593 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8594                         int immed_op, HOST_WIDE_INT n)
8595 {
8596 #if HOST_BITS_PER_WIDE_INT > 32
8597   n &= 0xffffffff;
8598 #endif
8599
8600   if (n == 0)
8601     {
8602       /* Quick and easy output.  */
8603       operands[immed_op] = const0_rtx;
8604       output_asm_insn (instr1, operands);
8605     }
8606   else
8607     {
8608       int i;
8609       const char * instr = instr1;
8610
8611       /* Note that n is never zero here (which would give no output).  */
8612       for (i = 0; i < 32; i += 2)
8613         {
8614           if (n & (3 << i))
8615             {
8616               operands[immed_op] = GEN_INT (n & (255 << i));
8617               output_asm_insn (instr, operands);
8618               instr = instr2;
8619               i += 6;
8620             }
8621         }
8622     }
8623   
8624   return "";
8625 }
8626
8627 /* Return the appropriate ARM instruction for the operation code.
8628    The returned result should not be overwritten.  OP is the rtx of the
8629    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8630    was shifted.  */
8631 const char *
8632 arithmetic_instr (rtx op, int shift_first_arg)
8633 {
8634   switch (GET_CODE (op))
8635     {
8636     case PLUS:
8637       return "add";
8638
8639     case MINUS:
8640       return shift_first_arg ? "rsb" : "sub";
8641
8642     case IOR:
8643       return "orr";
8644
8645     case XOR:
8646       return "eor";
8647
8648     case AND:
8649       return "and";
8650
8651     default:
8652       abort ();
8653     }
8654 }
8655
8656 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8657    for the operation code.  The returned result should not be overwritten.
8658    OP is the rtx code of the shift.
8659    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8660    shift.  */
8661 static const char *
8662 shift_op (rtx op, HOST_WIDE_INT *amountp)
8663 {
8664   const char * mnem;
8665   enum rtx_code code = GET_CODE (op);
8666
8667   if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
8668     *amountp = -1;
8669   else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
8670     *amountp = INTVAL (XEXP (op, 1));
8671   else
8672     abort ();
8673
8674   switch (code)
8675     {
8676     case ASHIFT:
8677       mnem = "asl";
8678       break;
8679
8680     case ASHIFTRT:
8681       mnem = "asr";
8682       break;
8683
8684     case LSHIFTRT:
8685       mnem = "lsr";
8686       break;
8687
8688     case ROTATERT:
8689       mnem = "ror";
8690       break;
8691
8692     case MULT:
8693       /* We never have to worry about the amount being other than a
8694          power of 2, since this case can never be reloaded from a reg.  */
8695       if (*amountp != -1)
8696         *amountp = int_log2 (*amountp);
8697       else
8698         abort ();
8699       return "asl";
8700
8701     default:
8702       abort ();
8703     }
8704
8705   if (*amountp != -1)
8706     {
8707       /* This is not 100% correct, but follows from the desire to merge
8708          multiplication by a power of 2 with the recognizer for a
8709          shift.  >=32 is not a valid shift for "asl", so we must try and
8710          output a shift that produces the correct arithmetical result.
8711          Using lsr #32 is identical except for the fact that the carry bit
8712          is not set correctly if we set the flags; but we never use the 
8713          carry bit from such an operation, so we can ignore that.  */
8714       if (code == ROTATERT)
8715         /* Rotate is just modulo 32.  */
8716         *amountp &= 31;
8717       else if (*amountp != (*amountp & 31))
8718         {
8719           if (code == ASHIFT)
8720             mnem = "lsr";
8721           *amountp = 32;
8722         }
8723
8724       /* Shifts of 0 are no-ops.  */
8725       if (*amountp == 0)
8726         return NULL;
8727     }     
8728
8729   return mnem;
8730 }
8731
8732 /* Obtain the shift from the POWER of two.  */
8733
8734 static HOST_WIDE_INT
8735 int_log2 (HOST_WIDE_INT power)
8736 {
8737   HOST_WIDE_INT shift = 0;
8738
8739   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8740     {
8741       if (shift > 31)
8742         abort ();
8743       shift++;
8744     }
8745
8746   return shift;
8747 }
8748
8749 /* Output a .ascii pseudo-op, keeping track of lengths.  This is because
8750    /bin/as is horribly restrictive.  */
8751 #define MAX_ASCII_LEN 51
8752
8753 void
8754 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8755 {
8756   int i;
8757   int len_so_far = 0;
8758
8759   fputs ("\t.ascii\t\"", stream);
8760   
8761   for (i = 0; i < len; i++)
8762     {
8763       int c = p[i];
8764
8765       if (len_so_far >= MAX_ASCII_LEN)
8766         {
8767           fputs ("\"\n\t.ascii\t\"", stream);
8768           len_so_far = 0;
8769         }
8770
8771       switch (c)
8772         {
8773         case TARGET_TAB:                
8774           fputs ("\\t", stream);
8775           len_so_far += 2;                      
8776           break;
8777           
8778         case TARGET_FF:
8779           fputs ("\\f", stream);
8780           len_so_far += 2;
8781           break;
8782           
8783         case TARGET_BS:
8784           fputs ("\\b", stream);
8785           len_so_far += 2;
8786           break;
8787           
8788         case TARGET_CR:
8789           fputs ("\\r", stream);
8790           len_so_far += 2;
8791           break;
8792           
8793         case TARGET_NEWLINE:
8794           fputs ("\\n", stream);
8795           c = p [i + 1];
8796           if ((c >= ' ' && c <= '~')
8797               || c == TARGET_TAB)
8798             /* This is a good place for a line break.  */
8799             len_so_far = MAX_ASCII_LEN;
8800           else
8801             len_so_far += 2;
8802           break;
8803           
8804         case '\"':
8805         case '\\':
8806           putc ('\\', stream);
8807           len_so_far++;
8808           /* Drop through.  */
8809
8810         default:
8811           if (c >= ' ' && c <= '~')
8812             {
8813               putc (c, stream);
8814               len_so_far++;
8815             }
8816           else
8817             {
8818               fprintf (stream, "\\%03o", c);
8819               len_so_far += 4;
8820             }
8821           break;
8822         }
8823     }
8824
8825   fputs ("\"\n", stream);
8826 }
8827 \f
8828 /* Compute the register sabe mask for registers 0 through 12
8829    inclusive.  This code is used by arm_compute_save_reg_mask.  */
8830 static unsigned long
8831 arm_compute_save_reg0_reg12_mask (void)
8832 {
8833   unsigned long func_type = arm_current_func_type ();
8834   unsigned int save_reg_mask = 0;
8835   unsigned int reg;
8836
8837   if (IS_INTERRUPT (func_type))
8838     {
8839       unsigned int max_reg;
8840       /* Interrupt functions must not corrupt any registers,
8841          even call clobbered ones.  If this is a leaf function
8842          we can just examine the registers used by the RTL, but
8843          otherwise we have to assume that whatever function is
8844          called might clobber anything, and so we have to save
8845          all the call-clobbered registers as well.  */
8846       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8847         /* FIQ handlers have registers r8 - r12 banked, so
8848            we only need to check r0 - r7, Normal ISRs only
8849            bank r14 and r15, so we must check up to r12.
8850            r13 is the stack pointer which is always preserved,
8851            so we do not need to consider it here.  */
8852         max_reg = 7;
8853       else
8854         max_reg = 12;
8855         
8856       for (reg = 0; reg <= max_reg; reg++)
8857         if (regs_ever_live[reg]
8858             || (! current_function_is_leaf && call_used_regs [reg]))
8859           save_reg_mask |= (1 << reg);
8860     }
8861   else
8862     {
8863       /* In the normal case we only need to save those registers
8864          which are call saved and which are used by this function.  */
8865       for (reg = 0; reg <= 10; reg++)
8866         if (regs_ever_live[reg] && ! call_used_regs [reg])
8867           save_reg_mask |= (1 << reg);
8868
8869       /* Handle the frame pointer as a special case.  */
8870       if (! TARGET_APCS_FRAME
8871           && ! frame_pointer_needed
8872           && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8873           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8874         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8875
8876       /* If we aren't loading the PIC register,
8877          don't stack it even though it may be live.  */
8878       if (flag_pic
8879           && ! TARGET_SINGLE_PIC_BASE 
8880           && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
8881         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8882     }
8883
8884   return save_reg_mask;
8885 }
8886
8887 /* Compute a bit mask of which registers need to be
8888    saved on the stack for the current function.  */
8889
8890 static unsigned long
8891 arm_compute_save_reg_mask (void)
8892 {
8893   unsigned int save_reg_mask = 0;
8894   unsigned long func_type = arm_current_func_type ();
8895
8896   if (IS_NAKED (func_type))
8897     /* This should never really happen.  */
8898     return 0;
8899
8900   /* If we are creating a stack frame, then we must save the frame pointer,
8901      IP (which will hold the old stack pointer), LR and the PC.  */
8902   if (frame_pointer_needed)
8903     save_reg_mask |=
8904       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8905       | (1 << IP_REGNUM)
8906       | (1 << LR_REGNUM)
8907       | (1 << PC_REGNUM);
8908
8909   /* Volatile functions do not return, so there
8910      is no need to save any other registers.  */
8911   if (IS_VOLATILE (func_type))
8912     return save_reg_mask;
8913
8914   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8915
8916   /* Decide if we need to save the link register.
8917      Interrupt routines have their own banked link register,
8918      so they never need to save it.
8919      Otherwise if we do not use the link register we do not need to save
8920      it.  If we are pushing other registers onto the stack however, we
8921      can save an instruction in the epilogue by pushing the link register
8922      now and then popping it back into the PC.  This incurs extra memory
8923      accesses though, so we only do it when optimizing for size, and only
8924      if we know that we will not need a fancy return sequence.  */
8925   if (regs_ever_live [LR_REGNUM]
8926           || (save_reg_mask
8927               && optimize_size
8928               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL))
8929     save_reg_mask |= 1 << LR_REGNUM;
8930
8931   if (cfun->machine->lr_save_eliminated)
8932     save_reg_mask &= ~ (1 << LR_REGNUM);
8933
8934   if (TARGET_REALLY_IWMMXT
8935       && ((bit_count (save_reg_mask)
8936            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8937     {
8938       unsigned int reg;
8939
8940       /* The total number of registers that are going to be pushed
8941          onto the stack is odd.  We need to ensure that the stack
8942          is 64-bit aligned before we start to save iWMMXt registers,
8943          and also before we start to create locals.  (A local variable
8944          might be a double or long long which we will load/store using
8945          an iWMMXt instruction).  Therefore we need to push another
8946          ARM register, so that the stack will be 64-bit aligned.  We
8947          try to avoid using the arg registers (r0 -r3) as they might be
8948          used to pass values in a tail call.  */
8949       for (reg = 4; reg <= 12; reg++)
8950         if ((save_reg_mask & (1 << reg)) == 0)
8951           break;
8952
8953       if (reg <= 12)
8954         save_reg_mask |= (1 << reg);
8955       else
8956         {
8957           cfun->machine->sibcall_blocked = 1;
8958           save_reg_mask |= (1 << 3);
8959         }
8960     }
8961
8962   return save_reg_mask;
8963 }
8964
8965
8966 /* Return the number of bytes required to save VFP registers.  */
8967 static int
8968 arm_get_vfp_saved_size (void)
8969 {
8970   unsigned int regno;
8971   int count;
8972   int saved;
8973
8974   saved = 0;
8975   /* Space for saved VFP registers.  */
8976   if (TARGET_HARD_FLOAT && TARGET_VFP)
8977     {
8978       count = 0;
8979       for (regno = FIRST_VFP_REGNUM;
8980            regno < LAST_VFP_REGNUM;
8981            regno += 2)
8982         {
8983           if ((!regs_ever_live[regno] || call_used_regs[regno])
8984               && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8985             {
8986               if (count > 0)
8987                 {
8988                   /* Workaround ARM10 VFPr1 bug.  */
8989                   if (count == 2 && !arm_arch6)
8990                     count++;
8991                   saved += count * 8 + 4;
8992                 }
8993               count = 0;
8994             }
8995           else
8996             count++;
8997         }
8998       if (count > 0)
8999         {
9000           if (count == 2 && !arm_arch6)
9001             count++;
9002           saved += count * 8 + 4;
9003         }
9004     }
9005   return saved;
9006 }
9007
9008
9009 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
9010    everything bar the final return instruction.  */
9011 const char *
9012 output_return_instruction (rtx operand, int really_return, int reverse)
9013 {
9014   char conditional[10];
9015   char instr[100];
9016   int reg;
9017   unsigned long live_regs_mask;
9018   unsigned long func_type;
9019   arm_stack_offsets *offsets;
9020
9021   func_type = arm_current_func_type ();
9022
9023   if (IS_NAKED (func_type))
9024     return "";
9025
9026   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9027     {
9028       /* If this function was declared non-returning, and we have
9029          found a tail call, then we have to trust that the called
9030          function won't return.  */
9031       if (really_return)
9032         {
9033           rtx ops[2];
9034       
9035           /* Otherwise, trap an attempted return by aborting.  */
9036           ops[0] = operand;
9037           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" 
9038                                        : "abort");
9039           assemble_external_libcall (ops[1]);
9040           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9041         }
9042       
9043       return "";
9044     }
9045
9046   if (current_function_calls_alloca && !really_return)
9047     abort ();
9048
9049   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9050
9051   return_used_this_function = 1;
9052
9053   live_regs_mask = arm_compute_save_reg_mask ();
9054
9055   if (live_regs_mask)
9056     {
9057       const char * return_reg;
9058
9059       /* If we do not have any special requirements for function exit 
9060          (eg interworking, or ISR) then we can load the return address 
9061          directly into the PC.  Otherwise we must load it into LR.  */
9062       if (really_return
9063           && ! TARGET_INTERWORK)
9064         return_reg = reg_names[PC_REGNUM];
9065       else
9066         return_reg = reg_names[LR_REGNUM];
9067
9068       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9069         {
9070           /* There are three possible reasons for the IP register
9071              being saved.  1) a stack frame was created, in which case
9072              IP contains the old stack pointer, or 2) an ISR routine
9073              corrupted it, or 3) it was saved to align the stack on
9074              iWMMXt.  In case 1, restore IP into SP, otherwise just
9075              restore IP.  */
9076           if (frame_pointer_needed)
9077             {
9078               live_regs_mask &= ~ (1 << IP_REGNUM);
9079               live_regs_mask |=   (1 << SP_REGNUM);
9080             }
9081           else
9082             {
9083               if (! IS_INTERRUPT (func_type)
9084                   && ! TARGET_REALLY_IWMMXT)
9085                 abort ();
9086             }
9087         }
9088
9089       /* On some ARM architectures it is faster to use LDR rather than
9090          LDM to load a single register.  On other architectures, the
9091          cost is the same.  In 26 bit mode, or for exception handlers,
9092          we have to use LDM to load the PC so that the CPSR is also
9093          restored.  */
9094       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9095         {
9096           if (live_regs_mask == (unsigned int)(1 << reg))
9097             break;
9098         }
9099       if (reg <= LAST_ARM_REGNUM
9100           && (reg != LR_REGNUM
9101               || ! really_return 
9102               || (TARGET_APCS_32 && ! IS_INTERRUPT (func_type))))
9103         {
9104           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional, 
9105                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9106         }
9107       else
9108         {
9109           char *p;
9110           int first = 1;
9111
9112           /* Generate the load multiple instruction to restore the
9113              registers.  Note we can get here, even if
9114              frame_pointer_needed is true, but only if sp already
9115              points to the base of the saved core registers.  */
9116           if (live_regs_mask & (1 << SP_REGNUM))
9117             {
9118               unsigned HOST_WIDE_INT stack_adjust;
9119
9120               offsets = arm_get_frame_offsets ();
9121               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9122               if (stack_adjust != 0 && stack_adjust != 4)
9123                 abort ();
9124
9125               if (stack_adjust && arm_arch5)
9126                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9127               else
9128                 {
9129                   /* If we can't use ldmib (SA110 bug), then try to pop r3
9130                      instead.  */
9131                   if (stack_adjust)
9132                     live_regs_mask |= 1 << 3;
9133                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9134                 }
9135             }
9136           else
9137             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9138
9139           p = instr + strlen (instr);
9140
9141           for (reg = 0; reg <= SP_REGNUM; reg++)
9142             if (live_regs_mask & (1 << reg))
9143               {
9144                 int l = strlen (reg_names[reg]);
9145
9146                 if (first)
9147                   first = 0;
9148                 else
9149                   {
9150                     memcpy (p, ", ", 2);
9151                     p += 2;
9152                   }
9153
9154                 memcpy (p, "%|", 2);
9155                 memcpy (p + 2, reg_names[reg], l);
9156                 p += l + 2;
9157               }
9158           
9159           if (live_regs_mask & (1 << LR_REGNUM))
9160             {
9161               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9162               /* Decide if we need to add the ^ symbol to the end of the
9163                  register list.  This causes the saved condition codes
9164                  register to be copied into the current condition codes
9165                  register.  We do the copy if we are conforming to the 32-bit
9166                  ABI and this is an interrupt function, or if we are
9167                  conforming to the 26-bit ABI.  There is a special case for
9168                  the 26-bit ABI however, which is if we are writing back the
9169                  stack pointer but not loading the PC.  In this case adding
9170                  the ^ symbol would create a type 2 LDM instruction, where
9171                  writeback is UNPREDICTABLE.  We are safe in leaving the ^
9172                  character off in this case however, since the actual return
9173                  instruction will be a MOVS which will restore the CPSR.  */
9174               if ((TARGET_APCS_32 && IS_INTERRUPT (func_type))
9175                   || (! TARGET_APCS_32 && really_return))
9176                 strcat (p, "^");
9177             }
9178           else
9179             strcpy (p, "}");
9180         }
9181
9182       output_asm_insn (instr, & operand);
9183
9184       /* See if we need to generate an extra instruction to
9185          perform the actual function return.  */
9186       if (really_return
9187           && func_type != ARM_FT_INTERWORKED
9188           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9189         {
9190           /* The return has already been handled
9191              by loading the LR into the PC.  */
9192           really_return = 0;
9193         }
9194     }
9195
9196   if (really_return)
9197     {
9198       switch ((int) ARM_FUNC_TYPE (func_type))
9199         {
9200         case ARM_FT_ISR:
9201         case ARM_FT_FIQ:
9202           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9203           break;
9204
9205         case ARM_FT_INTERWORKED:
9206           sprintf (instr, "bx%s\t%%|lr", conditional);
9207           break;
9208
9209         case ARM_FT_EXCEPTION:
9210           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9211           break;
9212
9213         default:
9214           /* ARMv5 implementations always provide BX, so interworking
9215              is the default unless APCS-26 is in use.  */
9216           if ((insn_flags & FL_ARCH5) != 0 && TARGET_APCS_32)
9217             sprintf (instr, "bx%s\t%%|lr", conditional);            
9218           else
9219             sprintf (instr, "mov%s%s\t%%|pc, %%|lr",
9220                      conditional, TARGET_APCS_32 ? "" : "s");
9221           break;
9222         }
9223
9224       output_asm_insn (instr, & operand);
9225     }
9226
9227   return "";
9228 }
9229
9230 /* Write the function name into the code section, directly preceding
9231    the function prologue.
9232
9233    Code will be output similar to this:
9234      t0
9235          .ascii "arm_poke_function_name", 0
9236          .align
9237      t1
9238          .word 0xff000000 + (t1 - t0)
9239      arm_poke_function_name
9240          mov     ip, sp
9241          stmfd   sp!, {fp, ip, lr, pc}
9242          sub     fp, ip, #4
9243
9244    When performing a stack backtrace, code can inspect the value
9245    of 'pc' stored at 'fp' + 0.  If the trace function then looks
9246    at location pc - 12 and the top 8 bits are set, then we know
9247    that there is a function name embedded immediately preceding this
9248    location and has length ((pc[-3]) & 0xff000000).
9249
9250    We assume that pc is declared as a pointer to an unsigned long.
9251
9252    It is of no benefit to output the function name if we are assembling
9253    a leaf function.  These function types will not contain a stack
9254    backtrace structure, therefore it is not possible to determine the
9255    function name.  */
9256 void
9257 arm_poke_function_name (FILE *stream, const char *name)
9258 {
9259   unsigned long alignlength;
9260   unsigned long length;
9261   rtx           x;
9262
9263   length      = strlen (name) + 1;
9264   alignlength = ROUND_UP_WORD (length);
9265   
9266   ASM_OUTPUT_ASCII (stream, name, length);
9267   ASM_OUTPUT_ALIGN (stream, 2);
9268   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9269   assemble_aligned_integer (UNITS_PER_WORD, x);
9270 }
9271
9272 /* Place some comments into the assembler stream
9273    describing the current function.  */
9274 static void
9275 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9276 {
9277   unsigned long func_type;
9278
9279   if (!TARGET_ARM)
9280     {
9281       thumb_output_function_prologue (f, frame_size);
9282       return;
9283     }
9284   
9285   /* Sanity check.  */
9286   if (arm_ccfsm_state || arm_target_insn)
9287     abort ();
9288
9289   func_type = arm_current_func_type ();
9290   
9291   switch ((int) ARM_FUNC_TYPE (func_type))
9292     {
9293     default:
9294     case ARM_FT_NORMAL:
9295       break;
9296     case ARM_FT_INTERWORKED:
9297       asm_fprintf (f, "\t%@ Function supports interworking.\n");
9298       break;
9299     case ARM_FT_EXCEPTION_HANDLER:
9300       asm_fprintf (f, "\t%@ C++ Exception Handler.\n");
9301       break;
9302     case ARM_FT_ISR:
9303       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9304       break;
9305     case ARM_FT_FIQ:
9306       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9307       break;
9308     case ARM_FT_EXCEPTION:
9309       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9310       break;
9311     }
9312   
9313   if (IS_NAKED (func_type))
9314     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9315
9316   if (IS_VOLATILE (func_type))
9317     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9318
9319   if (IS_NESTED (func_type))
9320     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9321     
9322   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9323                current_function_args_size,
9324                current_function_pretend_args_size, frame_size);
9325
9326   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9327                frame_pointer_needed,
9328                cfun->machine->uses_anonymous_args);
9329
9330   if (cfun->machine->lr_save_eliminated)
9331     asm_fprintf (f, "\t%@ link register save eliminated.\n");
9332
9333 #ifdef AOF_ASSEMBLER
9334   if (flag_pic)
9335     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9336 #endif
9337
9338   return_used_this_function = 0;  
9339 }
9340
9341 const char *
9342 arm_output_epilogue (rtx sibling)
9343 {
9344   int reg;
9345   unsigned long saved_regs_mask;
9346   unsigned long func_type;
9347   /* Floats_offset is the offset from the "virtual" frame.  In an APCS 
9348      frame that is $fp + 4 for a non-variadic function.  */
9349   int floats_offset = 0;
9350   rtx operands[3];
9351   FILE * f = asm_out_file;
9352   rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
9353   unsigned int lrm_count = 0;
9354   int really_return = (sibling == NULL);
9355   int start_reg;
9356   arm_stack_offsets *offsets;
9357
9358   /* If we have already generated the return instruction
9359      then it is futile to generate anything else.  */
9360   if (use_return_insn (FALSE, sibling) && return_used_this_function)
9361     return "";
9362
9363   func_type = arm_current_func_type ();
9364
9365   if (IS_NAKED (func_type))
9366     /* Naked functions don't have epilogues.  */
9367     return "";
9368
9369   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9370     {
9371       rtx op;
9372           
9373       /* A volatile function should never return.  Call abort.  */
9374       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9375       assemble_external_libcall (op);
9376       output_asm_insn ("bl\t%a0", &op);
9377       
9378       return "";
9379     }
9380
9381   if (ARM_FUNC_TYPE (func_type) == ARM_FT_EXCEPTION_HANDLER
9382       && ! really_return)
9383     /* If we are throwing an exception, then we really must
9384        be doing a return,  so we can't tail-call.  */
9385     abort ();
9386   
9387   offsets = arm_get_frame_offsets ();
9388   saved_regs_mask = arm_compute_save_reg_mask ();
9389
9390   if (TARGET_IWMMXT)
9391     lrm_count = bit_count (saved_regs_mask);
9392
9393   floats_offset = offsets->saved_args;
9394   /* Compute how far away the floats will be.  */
9395   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9396     if (saved_regs_mask & (1 << reg))
9397       floats_offset += 4;
9398   
9399   if (frame_pointer_needed)
9400     {
9401       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
9402       int vfp_offset = offsets->frame;
9403
9404       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9405         {
9406           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9407             if (regs_ever_live[reg] && !call_used_regs[reg])
9408               {
9409                 floats_offset += 12;
9410                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n", 
9411                              reg, FP_REGNUM, floats_offset - vfp_offset);
9412               }
9413         }
9414       else
9415         {
9416           start_reg = LAST_FPA_REGNUM;
9417
9418           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9419             {
9420               if (regs_ever_live[reg] && !call_used_regs[reg])
9421                 {
9422                   floats_offset += 12;
9423                   
9424                   /* We can't unstack more than four registers at once.  */
9425                   if (start_reg - reg == 3)
9426                     {
9427                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9428                                    reg, FP_REGNUM, floats_offset - vfp_offset);
9429                       start_reg = reg - 1;
9430                     }
9431                 }
9432               else
9433                 {
9434                   if (reg != start_reg)
9435                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9436                                  reg + 1, start_reg - reg,
9437                                  FP_REGNUM, floats_offset - vfp_offset);
9438                   start_reg = reg - 1;
9439                 }
9440             }
9441
9442           /* Just in case the last register checked also needs unstacking.  */
9443           if (reg != start_reg)
9444             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9445                          reg + 1, start_reg - reg,
9446                          FP_REGNUM, floats_offset - vfp_offset);
9447         }
9448
9449       if (TARGET_HARD_FLOAT && TARGET_VFP)
9450         {
9451           int saved_size;
9452
9453           /* The fldmx insn does not have base+offset addressing modes,
9454              so we use IP to hold the address.  */
9455           saved_size = arm_get_vfp_saved_size ();
9456
9457           if (saved_size > 0)
9458             {
9459               floats_offset += saved_size;
9460               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9461                            FP_REGNUM, floats_offset - vfp_offset);
9462             }
9463           start_reg = FIRST_VFP_REGNUM;
9464           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9465             {
9466               if ((!regs_ever_live[reg] || call_used_regs[reg])
9467                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9468                 {
9469                   if (start_reg != reg)
9470                     arm_output_fldmx (f, IP_REGNUM,
9471                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9472                                       (reg - start_reg) / 2);
9473                   start_reg = reg + 2;
9474                 }
9475             }
9476           if (start_reg != reg)
9477             arm_output_fldmx (f, IP_REGNUM,
9478                               (start_reg - FIRST_VFP_REGNUM) / 2,
9479                               (reg - start_reg) / 2);
9480         }
9481
9482       if (TARGET_IWMMXT)
9483         {
9484           /* The frame pointer is guaranteed to be non-double-word aligned.
9485              This is because it is set to (old_stack_pointer - 4) and the
9486              old_stack_pointer was double word aligned.  Thus the offset to
9487              the iWMMXt registers to be loaded must also be non-double-word
9488              sized, so that the resultant address *is* double-word aligned.
9489              We can ignore floats_offset since that was already included in
9490              the live_regs_mask.  */
9491           lrm_count += (lrm_count % 2 ? 2 : 1);
9492               
9493           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9494             if (regs_ever_live[reg] && !call_used_regs[reg])
9495               {
9496                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n", 
9497                              reg, FP_REGNUM, lrm_count * 4);
9498                 lrm_count += 2; 
9499               }
9500         }
9501
9502       /* saved_regs_mask should contain the IP, which at the time of stack
9503          frame generation actually contains the old stack pointer.  So a
9504          quick way to unwind the stack is just pop the IP register directly
9505          into the stack pointer.  */
9506       if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
9507         abort ();
9508       saved_regs_mask &= ~ (1 << IP_REGNUM);
9509       saved_regs_mask |=   (1 << SP_REGNUM);
9510
9511       /* There are two registers left in saved_regs_mask - LR and PC.  We
9512          only need to restore the LR register (the return address), but to
9513          save time we can load it directly into the PC, unless we need a
9514          special function exit sequence, or we are not really returning.  */
9515       if (really_return && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
9516         /* Delete the LR from the register mask, so that the LR on
9517            the stack is loaded into the PC in the register mask.  */
9518         saved_regs_mask &= ~ (1 << LR_REGNUM);
9519       else
9520         saved_regs_mask &= ~ (1 << PC_REGNUM);
9521
9522       /* We must use SP as the base register, because SP is one of the
9523          registers being restored.  If an interrupt or page fault
9524          happens in the ldm instruction, the SP might or might not
9525          have been restored.  That would be bad, as then SP will no
9526          longer indicate the safe area of stack, and we can get stack
9527          corruption.  Using SP as the base register means that it will
9528          be reset correctly to the original value, should an interrupt
9529          occur.  If the stack pointer already points at the right
9530          place, then omit the subtraction.  */
9531       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9532           || current_function_calls_alloca)
9533         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9534                      4 * bit_count (saved_regs_mask));
9535       print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9536
9537       if (IS_INTERRUPT (func_type))
9538         /* Interrupt handlers will have pushed the
9539            IP onto the stack, so restore it now.  */
9540         print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9541     }
9542   else
9543     {
9544       /* Restore stack pointer if necessary.  */
9545       if (offsets->outgoing_args != offsets->saved_regs)
9546         {
9547           operands[0] = operands[1] = stack_pointer_rtx;
9548           operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9549           output_add_immediate (operands);
9550         }
9551
9552       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9553         {
9554           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9555             if (regs_ever_live[reg] && !call_used_regs[reg])
9556               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9557                            reg, SP_REGNUM);
9558         }
9559       else
9560         {
9561           start_reg = FIRST_FPA_REGNUM;
9562
9563           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9564             {
9565               if (regs_ever_live[reg] && !call_used_regs[reg])
9566                 {
9567                   if (reg - start_reg == 3)
9568                     {
9569                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9570                                    start_reg, SP_REGNUM);
9571                       start_reg = reg + 1;
9572                     }
9573                 }
9574               else
9575                 {
9576                   if (reg != start_reg)
9577                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9578                                  start_reg, reg - start_reg,
9579                                  SP_REGNUM);
9580                   
9581                   start_reg = reg + 1;
9582                 }
9583             }
9584
9585           /* Just in case the last register checked also needs unstacking.  */
9586           if (reg != start_reg)
9587             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9588                          start_reg, reg - start_reg, SP_REGNUM);
9589         }
9590
9591       if (TARGET_HARD_FLOAT && TARGET_VFP)
9592         {
9593           start_reg = FIRST_VFP_REGNUM;
9594           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9595             {
9596               if ((!regs_ever_live[reg] || call_used_regs[reg])
9597                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9598                 {
9599                   if (start_reg != reg)
9600                     arm_output_fldmx (f, SP_REGNUM,
9601                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9602                                       (reg - start_reg) / 2);
9603                   start_reg = reg + 2;
9604                 }
9605             }
9606           if (start_reg != reg)
9607             arm_output_fldmx (f, SP_REGNUM,
9608                               (start_reg - FIRST_VFP_REGNUM) / 2,
9609                               (reg - start_reg) / 2);
9610         }
9611       if (TARGET_IWMMXT)
9612         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9613           if (regs_ever_live[reg] && !call_used_regs[reg])
9614             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9615
9616       /* If we can, restore the LR into the PC.  */
9617       if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9618           && really_return
9619           && current_function_pretend_args_size == 0
9620           && saved_regs_mask & (1 << LR_REGNUM))
9621         {
9622           saved_regs_mask &= ~ (1 << LR_REGNUM);
9623           saved_regs_mask |=   (1 << PC_REGNUM);
9624         }
9625
9626       /* Load the registers off the stack.  If we only have one register
9627          to load use the LDR instruction - it is faster.  */
9628       if (saved_regs_mask == (1 << LR_REGNUM))
9629         {
9630           /* The exception handler ignores the LR, so we do
9631              not really need to load it off the stack.  */
9632           if (eh_ofs)
9633             asm_fprintf (f, "\tadd\t%r, %r, #4\n", SP_REGNUM, SP_REGNUM);
9634           else
9635             asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9636         }
9637       else if (saved_regs_mask)
9638         {
9639           if (saved_regs_mask & (1 << SP_REGNUM))
9640             /* Note - write back to the stack register is not enabled
9641                (ie "ldmfd sp!...").  We know that the stack pointer is
9642                in the list of registers and if we add writeback the
9643                instruction becomes UNPREDICTABLE.  */
9644             print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9645           else
9646             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9647         }
9648
9649       if (current_function_pretend_args_size)
9650         {
9651           /* Unwind the pre-pushed regs.  */
9652           operands[0] = operands[1] = stack_pointer_rtx;
9653           operands[2] = GEN_INT (current_function_pretend_args_size);
9654           output_add_immediate (operands);
9655         }
9656     }
9657
9658   if (! really_return
9659     || (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9660         && current_function_pretend_args_size == 0
9661         && saved_regs_mask & (1 << PC_REGNUM)))
9662     return "";
9663
9664   /* Generate the return instruction.  */
9665   switch ((int) ARM_FUNC_TYPE (func_type))
9666     {
9667     case ARM_FT_EXCEPTION_HANDLER:
9668       /* Even in 26-bit mode we do a mov (rather than a movs)
9669          because we don't have the PSR bits set in the address.  */
9670       asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, EXCEPTION_LR_REGNUM);
9671       break;
9672
9673     case ARM_FT_ISR:
9674     case ARM_FT_FIQ:
9675       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9676       break;
9677
9678     case ARM_FT_EXCEPTION:
9679       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9680       break;
9681
9682     case ARM_FT_INTERWORKED:
9683       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9684       break;
9685
9686     default:
9687       if (frame_pointer_needed)
9688         /* If we used the frame pointer then the return address
9689            will have been loaded off the stack directly into the
9690            PC, so there is no need to issue a MOV instruction
9691            here.  */
9692         ;
9693       else if (current_function_pretend_args_size == 0
9694                && (saved_regs_mask & (1 << LR_REGNUM)))
9695         /* Similarly we may have been able to load LR into the PC
9696            even if we did not create a stack frame.  */
9697         ;
9698       else if (TARGET_APCS_32)
9699         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9700       else
9701         asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9702       break;
9703     }
9704
9705   return "";
9706 }
9707
9708 static void
9709 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9710                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9711 {
9712   arm_stack_offsets *offsets;
9713
9714   if (TARGET_THUMB)
9715     {
9716       /* ??? Probably not safe to set this here, since it assumes that a
9717          function will be emitted as assembly immediately after we generate
9718          RTL for it.  This does not happen for inline functions.  */
9719       return_used_this_function = 0;
9720     }
9721   else
9722     {
9723       /* We need to take into account any stack-frame rounding.  */
9724       offsets = arm_get_frame_offsets ();
9725
9726       if (use_return_insn (FALSE, NULL)
9727           && return_used_this_function
9728           && offsets->saved_regs != offsets->outgoing_args
9729           && !frame_pointer_needed)
9730         abort ();
9731
9732       /* Reset the ARM-specific per-function variables.  */
9733       after_arm_reorg = 0;
9734     }
9735 }
9736
9737 /* Generate and emit an insn that we will recognize as a push_multi.
9738    Unfortunately, since this insn does not reflect very well the actual
9739    semantics of the operation, we need to annotate the insn for the benefit
9740    of DWARF2 frame unwind information.  */
9741 static rtx
9742 emit_multi_reg_push (int mask)
9743 {
9744   int num_regs = 0;
9745   int num_dwarf_regs;
9746   int i, j;
9747   rtx par;
9748   rtx dwarf;
9749   int dwarf_par_index;
9750   rtx tmp, reg;
9751
9752   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9753     if (mask & (1 << i))
9754       num_regs++;
9755
9756   if (num_regs == 0 || num_regs > 16)
9757     abort ();
9758
9759   /* We don't record the PC in the dwarf frame information.  */
9760   num_dwarf_regs = num_regs;
9761   if (mask & (1 << PC_REGNUM))
9762     num_dwarf_regs--;
9763
9764   /* For the body of the insn we are going to generate an UNSPEC in
9765      parallel with several USEs.  This allows the insn to be recognized
9766      by the push_multi pattern in the arm.md file.  The insn looks
9767      something like this:
9768
9769        (parallel [ 
9770            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9771                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9772            (use (reg:SI 11 fp))
9773            (use (reg:SI 12 ip))
9774            (use (reg:SI 14 lr))
9775            (use (reg:SI 15 pc))
9776         ])
9777
9778      For the frame note however, we try to be more explicit and actually
9779      show each register being stored into the stack frame, plus a (single)
9780      decrement of the stack pointer.  We do it this way in order to be
9781      friendly to the stack unwinding code, which only wants to see a single
9782      stack decrement per instruction.  The RTL we generate for the note looks
9783      something like this:
9784
9785       (sequence [ 
9786            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9787            (set (mem:SI (reg:SI sp)) (reg:SI r4))
9788            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9789            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9790            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9791         ])
9792
9793       This sequence is used both by the code to support stack unwinding for
9794       exceptions handlers and the code to generate dwarf2 frame debugging.  */
9795   
9796   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9797   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9798   dwarf_par_index = 1;
9799
9800   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9801     {
9802       if (mask & (1 << i))
9803         {
9804           reg = gen_rtx_REG (SImode, i);
9805
9806           XVECEXP (par, 0, 0)
9807             = gen_rtx_SET (VOIDmode,
9808                            gen_rtx_MEM (BLKmode,
9809                                         gen_rtx_PRE_DEC (BLKmode,
9810                                                          stack_pointer_rtx)),
9811                            gen_rtx_UNSPEC (BLKmode,
9812                                            gen_rtvec (1, reg),
9813                                            UNSPEC_PUSH_MULT));
9814
9815           if (i != PC_REGNUM)
9816             {
9817               tmp = gen_rtx_SET (VOIDmode,
9818                                  gen_rtx_MEM (SImode, stack_pointer_rtx),
9819                                  reg);
9820               RTX_FRAME_RELATED_P (tmp) = 1;
9821               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9822               dwarf_par_index++;
9823             }
9824
9825           break;
9826         }
9827     }
9828
9829   for (j = 1, i++; j < num_regs; i++)
9830     {
9831       if (mask & (1 << i))
9832         {
9833           reg = gen_rtx_REG (SImode, i);
9834
9835           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9836
9837           if (i != PC_REGNUM)
9838             {
9839               tmp = gen_rtx_SET (VOIDmode,
9840                                  gen_rtx_MEM (SImode,
9841                                               plus_constant (stack_pointer_rtx,
9842                                                              4 * j)),
9843                                  reg);
9844               RTX_FRAME_RELATED_P (tmp) = 1;
9845               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9846             }
9847
9848           j++;
9849         }
9850     }
9851
9852   par = emit_insn (par);
9853   
9854   tmp = gen_rtx_SET (SImode,
9855                      stack_pointer_rtx,
9856                      gen_rtx_PLUS (SImode,
9857                                    stack_pointer_rtx,
9858                                    GEN_INT (-4 * num_regs)));
9859   RTX_FRAME_RELATED_P (tmp) = 1;
9860   XVECEXP (dwarf, 0, 0) = tmp;
9861   
9862   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9863                                        REG_NOTES (par));
9864   return par;
9865 }
9866
9867 static rtx
9868 emit_sfm (int base_reg, int count)
9869 {
9870   rtx par;
9871   rtx dwarf;
9872   rtx tmp, reg;
9873   int i;
9874
9875   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9876   dwarf = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9877
9878   reg = gen_rtx_REG (XFmode, base_reg++);
9879
9880   XVECEXP (par, 0, 0)
9881     = gen_rtx_SET (VOIDmode, 
9882                    gen_rtx_MEM (BLKmode,
9883                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9884                    gen_rtx_UNSPEC (BLKmode,
9885                                    gen_rtvec (1, reg),
9886                                    UNSPEC_PUSH_MULT));
9887   tmp
9888     = gen_rtx_SET (VOIDmode, 
9889                    gen_rtx_MEM (XFmode,
9890                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9891                    reg);
9892   RTX_FRAME_RELATED_P (tmp) = 1;
9893   XVECEXP (dwarf, 0, count - 1) = tmp;    
9894   
9895   for (i = 1; i < count; i++)
9896     {
9897       reg = gen_rtx_REG (XFmode, base_reg++);
9898       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9899
9900       tmp = gen_rtx_SET (VOIDmode, 
9901                          gen_rtx_MEM (XFmode,
9902                                       gen_rtx_PRE_DEC (BLKmode,
9903                                                        stack_pointer_rtx)),
9904                          reg);
9905       RTX_FRAME_RELATED_P (tmp) = 1;
9906       XVECEXP (dwarf, 0, count - i - 1) = tmp;    
9907     }
9908
9909   par = emit_insn (par);
9910   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9911                                        REG_NOTES (par));
9912   return par;
9913 }
9914
9915
9916 /* Compute the distance from register FROM to register TO.
9917    These can be the arg pointer (26), the soft frame pointer (25),
9918    the stack pointer (13) or the hard frame pointer (11).
9919    Typical stack layout looks like this:
9920
9921        old stack pointer -> |    |
9922                              ----
9923                             |    | \
9924                             |    |   saved arguments for
9925                             |    |   vararg functions
9926                             |    | /
9927                               --
9928    hard FP & arg pointer -> |    | \
9929                             |    |   stack
9930                             |    |   frame
9931                             |    | /
9932                               --
9933                             |    | \
9934                             |    |   call saved
9935                             |    |   registers
9936       soft frame pointer -> |    | /
9937                               --
9938                             |    | \
9939                             |    |   local
9940                             |    |   variables
9941                             |    | /
9942                               --
9943                             |    | \
9944                             |    |   outgoing
9945                             |    |   arguments
9946    current stack pointer -> |    | /
9947                               --
9948
9949   For a given function some or all of these stack components
9950   may not be needed, giving rise to the possibility of
9951   eliminating some of the registers.
9952
9953   The values returned by this function must reflect the behavior
9954   of arm_expand_prologue() and arm_compute_save_reg_mask().
9955
9956   The sign of the number returned reflects the direction of stack
9957   growth, so the values are positive for all eliminations except
9958   from the soft frame pointer to the hard frame pointer.
9959
9960   SFP may point just inside the local variables block to ensure correct
9961   alignment.  */
9962
9963
9964 /* Calculate stack offsets.  These are used to calculate register elimination
9965    offsets and in prologue/epilogue code.  */
9966
9967 static arm_stack_offsets *
9968 arm_get_frame_offsets (void)
9969 {
9970   struct arm_stack_offsets *offsets;
9971   unsigned long func_type;
9972   int leaf;
9973   int saved;
9974   HOST_WIDE_INT frame_size;
9975
9976   offsets = &cfun->machine->stack_offsets;
9977   
9978   /* We need to know if we are a leaf function.  Unfortunately, it
9979      is possible to be called after start_sequence has been called,
9980      which causes get_insns to return the insns for the sequence,
9981      not the function, which will cause leaf_function_p to return
9982      the incorrect result.
9983
9984      to know about leaf functions once reload has completed, and the
9985      frame size cannot be changed after that time, so we can safely
9986      use the cached value.  */
9987
9988   if (reload_completed)
9989     return offsets;
9990
9991   /* Initially this is the size of the local variables.  It will translated
9992      into an offset once we have determined the size of preceding data.  */
9993   frame_size = ROUND_UP_WORD (get_frame_size ());
9994
9995   leaf = leaf_function_p ();
9996
9997   /* Space for variadic functions.  */
9998   offsets->saved_args = current_function_pretend_args_size;
9999
10000   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10001
10002   if (TARGET_ARM)
10003     {
10004       unsigned int regno;
10005
10006       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
10007
10008       /* We know that SP will be doubleword aligned on entry, and we must
10009          preserve that condition at any subroutine call.  We also require the
10010          soft frame pointer to be doubleword aligned.  */
10011
10012       if (TARGET_REALLY_IWMMXT)
10013         {
10014           /* Check for the call-saved iWMMXt registers.  */
10015           for (regno = FIRST_IWMMXT_REGNUM;
10016                regno <= LAST_IWMMXT_REGNUM;
10017                regno++)
10018             if (regs_ever_live [regno] && ! call_used_regs [regno])
10019               saved += 8;
10020         }
10021
10022       func_type = arm_current_func_type ();
10023       if (! IS_VOLATILE (func_type))
10024         {
10025           /* Space for saved FPA registers.  */
10026           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10027           if (regs_ever_live[regno] && ! call_used_regs[regno])
10028             saved += 12;
10029
10030           /* Space for saved VFP registers.  */
10031           if (TARGET_HARD_FLOAT && TARGET_VFP)
10032             saved += arm_get_vfp_saved_size ();
10033         }
10034     }
10035   else /* TARGET_THUMB */
10036     {
10037       int reg;
10038       int count_regs;
10039
10040       saved = 0;
10041       count_regs = 0;
10042       for (reg = 8; reg < 13; reg ++)
10043         if (THUMB_REG_PUSHED_P (reg))
10044           count_regs ++;
10045       if (count_regs)
10046         saved += 4 * count_regs;
10047       count_regs = 0;
10048       for (reg = 0; reg <= LAST_LO_REGNUM; reg ++)
10049         if (THUMB_REG_PUSHED_P (reg))
10050           count_regs ++;
10051       if (count_regs || ! leaf_function_p ()
10052           || thumb_far_jump_used_p ())
10053         saved += 4 * (count_regs + 1);
10054       if (TARGET_BACKTRACE)
10055         {
10056           if ((count_regs & 0xFF) == 0 && (regs_ever_live[3] != 0))
10057             saved += 20;
10058           else
10059             saved += 16;
10060         }
10061     }
10062
10063   /* Saved registers include the stack frame.  */
10064   offsets->saved_regs = offsets->saved_args + saved;
10065   offsets->soft_frame = offsets->saved_regs;
10066   /* A leaf function does not need any stack alignment if it has nothing
10067      on the stack.  */
10068   if (leaf && frame_size == 0)
10069     {
10070       offsets->outgoing_args = offsets->soft_frame;
10071       return offsets;
10072     }
10073
10074   /* Ensure SFP has the correct alignment.  */
10075   if (ARM_DOUBLEWORD_ALIGN
10076       && (offsets->soft_frame & 7))
10077     offsets->soft_frame += 4;
10078
10079   offsets->outgoing_args = offsets->soft_frame + frame_size
10080                            + current_function_outgoing_args_size;
10081
10082   if (ARM_DOUBLEWORD_ALIGN)
10083     {
10084       /* Ensure SP remains doubleword aligned.  */
10085       if (offsets->outgoing_args & 7)
10086         offsets->outgoing_args += 4;
10087       if (offsets->outgoing_args & 7)
10088         abort ();
10089     }
10090
10091   return offsets;
10092 }
10093
10094
10095 /* Calculate the relative offsets for the different stack pointers.  Positive
10096    offsets are in the direction of stack growth.  */
10097
10098 unsigned int
10099 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10100 {
10101   arm_stack_offsets *offsets;
10102
10103   offsets = arm_get_frame_offsets ();
10104
10105   /* OK, now we have enough information to compute the distances.
10106      There must be an entry in these switch tables for each pair
10107      of registers in ELIMINABLE_REGS, even if some of the entries
10108      seem to be redundant or useless.  */
10109   switch (from)
10110     {
10111     case ARG_POINTER_REGNUM:
10112       switch (to)
10113         {
10114         case THUMB_HARD_FRAME_POINTER_REGNUM:
10115           return 0;
10116
10117         case FRAME_POINTER_REGNUM:
10118           /* This is the reverse of the soft frame pointer
10119              to hard frame pointer elimination below.  */
10120           return offsets->soft_frame - offsets->saved_args;
10121
10122         case ARM_HARD_FRAME_POINTER_REGNUM:
10123           /* If there is no stack frame then the hard
10124              frame pointer and the arg pointer coincide.  */
10125           if (offsets->frame == offsets->saved_regs)
10126             return 0;
10127           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
10128           return (frame_pointer_needed
10129                   && current_function_needs_context
10130                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10131
10132         case STACK_POINTER_REGNUM:
10133           /* If nothing has been pushed on the stack at all
10134              then this will return -4.  This *is* correct!  */
10135           return offsets->outgoing_args - (offsets->saved_args + 4);
10136
10137         default:
10138           abort ();
10139         }
10140       break;
10141
10142     case FRAME_POINTER_REGNUM:
10143       switch (to)
10144         {
10145         case THUMB_HARD_FRAME_POINTER_REGNUM:
10146           return 0;
10147
10148         case ARM_HARD_FRAME_POINTER_REGNUM:
10149           /* The hard frame pointer points to the top entry in the
10150              stack frame.  The soft frame pointer to the bottom entry
10151              in the stack frame.  If there is no stack frame at all,
10152              then they are identical.  */
10153
10154           return offsets->frame - offsets->soft_frame;
10155
10156         case STACK_POINTER_REGNUM:
10157           return offsets->outgoing_args - offsets->soft_frame;
10158
10159         default:
10160           abort ();
10161         }
10162       break;
10163
10164     default:
10165       /* You cannot eliminate from the stack pointer.
10166          In theory you could eliminate from the hard frame
10167          pointer to the stack pointer, but this will never
10168          happen, since if a stack frame is not needed the
10169          hard frame pointer will never be used.  */
10170       abort ();
10171     }
10172 }
10173
10174
10175 /* Generate the prologue instructions for entry into an ARM function.  */
10176 void
10177 arm_expand_prologue (void)
10178 {
10179   int reg;
10180   rtx amount;
10181   rtx insn;
10182   rtx ip_rtx;
10183   unsigned long live_regs_mask;
10184   unsigned long func_type;
10185   int fp_offset = 0;
10186   int saved_pretend_args = 0;
10187   int saved_regs = 0;
10188   unsigned int args_to_push;
10189   arm_stack_offsets *offsets;
10190
10191   func_type = arm_current_func_type ();
10192
10193   /* Naked functions don't have prologues.  */
10194   if (IS_NAKED (func_type))
10195     return;
10196
10197   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
10198   args_to_push = current_function_pretend_args_size;
10199   
10200   /* Compute which register we will have to save onto the stack.  */
10201   live_regs_mask = arm_compute_save_reg_mask ();
10202
10203   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10204
10205   if (frame_pointer_needed)
10206     {
10207       if (IS_INTERRUPT (func_type))
10208         {
10209           /* Interrupt functions must not corrupt any registers.
10210              Creating a frame pointer however, corrupts the IP
10211              register, so we must push it first.  */
10212           insn = emit_multi_reg_push (1 << IP_REGNUM);
10213
10214           /* Do not set RTX_FRAME_RELATED_P on this insn.
10215              The dwarf stack unwinding code only wants to see one
10216              stack decrement per function, and this is not it.  If
10217              this instruction is labeled as being part of the frame
10218              creation sequence then dwarf2out_frame_debug_expr will
10219              abort when it encounters the assignment of IP to FP
10220              later on, since the use of SP here establishes SP as
10221              the CFA register and not IP.
10222
10223              Anyway this instruction is not really part of the stack
10224              frame creation although it is part of the prologue.  */
10225         }
10226       else if (IS_NESTED (func_type))
10227         {
10228           /* The Static chain register is the same as the IP register
10229              used as a scratch register during stack frame creation.
10230              To get around this need to find somewhere to store IP
10231              whilst the frame is being created.  We try the following
10232              places in order:
10233              
10234                1. The last argument register.
10235                2. A slot on the stack above the frame.  (This only
10236                   works if the function is not a varargs function).
10237                3. Register r3, after pushing the argument registers
10238                   onto the stack.
10239
10240              Note - we only need to tell the dwarf2 backend about the SP
10241              adjustment in the second variant; the static chain register
10242              doesn't need to be unwound, as it doesn't contain a value
10243              inherited from the caller.  */
10244
10245           if (regs_ever_live[3] == 0)
10246             {
10247               insn = gen_rtx_REG (SImode, 3);
10248               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10249               insn = emit_insn (insn);
10250             }
10251           else if (args_to_push == 0)
10252             {
10253               rtx dwarf;
10254               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10255               insn = gen_rtx_MEM (SImode, insn);
10256               insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10257               insn = emit_insn (insn);
10258
10259               fp_offset = 4;
10260
10261               /* Just tell the dwarf backend that we adjusted SP.  */
10262               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10263                                    gen_rtx_PLUS (SImode, stack_pointer_rtx,
10264                                                  GEN_INT (-fp_offset)));
10265               RTX_FRAME_RELATED_P (insn) = 1;
10266               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10267                                                     dwarf, REG_NOTES (insn));
10268             }
10269           else
10270             {
10271               /* Store the args on the stack.  */
10272               if (cfun->machine->uses_anonymous_args)
10273                 insn = emit_multi_reg_push
10274                   ((0xf0 >> (args_to_push / 4)) & 0xf);
10275               else
10276                 insn = emit_insn
10277                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, 
10278                                GEN_INT (- args_to_push)));
10279
10280               RTX_FRAME_RELATED_P (insn) = 1;
10281
10282               saved_pretend_args = 1;
10283               fp_offset = args_to_push;
10284               args_to_push = 0;
10285
10286               /* Now reuse r3 to preserve IP.  */
10287               insn = gen_rtx_REG (SImode, 3);
10288               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10289               (void) emit_insn (insn);
10290             }
10291         }
10292
10293       if (fp_offset)
10294         {
10295           insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10296           insn = gen_rtx_SET  (SImode, ip_rtx, insn);
10297         }
10298       else
10299         insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10300       
10301       insn = emit_insn (insn);
10302       RTX_FRAME_RELATED_P (insn) = 1;
10303     }
10304
10305   if (args_to_push)
10306     {
10307       /* Push the argument registers, or reserve space for them.  */
10308       if (cfun->machine->uses_anonymous_args)
10309         insn = emit_multi_reg_push
10310           ((0xf0 >> (args_to_push / 4)) & 0xf);
10311       else
10312         insn = emit_insn
10313           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, 
10314                        GEN_INT (- args_to_push)));
10315       RTX_FRAME_RELATED_P (insn) = 1;
10316     }
10317
10318   /* If this is an interrupt service routine, and the link register
10319      is going to be pushed, and we are not creating a stack frame,
10320      (which would involve an extra push of IP and a pop in the epilogue)
10321      subtracting four from LR now will mean that the function return
10322      can be done with a single instruction.  */
10323   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10324       && (live_regs_mask & (1 << LR_REGNUM)) != 0
10325       && ! frame_pointer_needed)
10326     emit_insn (gen_rtx_SET (SImode, 
10327                             gen_rtx_REG (SImode, LR_REGNUM),
10328                             gen_rtx_PLUS (SImode,
10329                                           gen_rtx_REG (SImode, LR_REGNUM),
10330                                           GEN_INT (-4))));
10331
10332   if (live_regs_mask)
10333     {
10334       insn = emit_multi_reg_push (live_regs_mask);
10335       saved_regs += bit_count (live_regs_mask) * 4;
10336       RTX_FRAME_RELATED_P (insn) = 1;
10337     }
10338
10339   if (TARGET_IWMMXT)
10340     for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10341       if (regs_ever_live[reg] && ! call_used_regs [reg])
10342         {
10343           insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10344           insn = gen_rtx_MEM (V2SImode, insn);
10345           insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10346                                          gen_rtx_REG (V2SImode, reg)));
10347           RTX_FRAME_RELATED_P (insn) = 1;
10348           saved_regs += 8;
10349         }
10350
10351   if (! IS_VOLATILE (func_type))
10352     {
10353       int start_reg;
10354
10355       /* Save any floating point call-saved registers used by this
10356          function.  */
10357       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10358         {
10359           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10360             if (regs_ever_live[reg] && !call_used_regs[reg])
10361               {
10362                 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10363                 insn = gen_rtx_MEM (XFmode, insn);
10364                 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10365                                                gen_rtx_REG (XFmode, reg)));
10366                 RTX_FRAME_RELATED_P (insn) = 1;
10367                 saved_regs += 12;
10368               }
10369         }
10370       else
10371         {
10372           start_reg = LAST_FPA_REGNUM;
10373
10374           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10375             {
10376               if (regs_ever_live[reg] && !call_used_regs[reg])
10377                 {
10378                   if (start_reg - reg == 3)
10379                     {
10380                       insn = emit_sfm (reg, 4);
10381                       RTX_FRAME_RELATED_P (insn) = 1;
10382                       start_reg = reg - 1;
10383                     }
10384                 }
10385               else
10386                 {
10387                   if (start_reg != reg)
10388                     {
10389                       insn = emit_sfm (reg + 1, start_reg - reg);
10390                       RTX_FRAME_RELATED_P (insn) = 1;
10391                       saved_regs += (start_reg - reg) * 12;
10392                     }
10393                   start_reg = reg - 1;
10394                 }
10395             }
10396
10397           if (start_reg != reg)
10398             {
10399               insn = emit_sfm (reg + 1, start_reg - reg);
10400               saved_regs += (start_reg - reg) * 12;
10401               RTX_FRAME_RELATED_P (insn) = 1;
10402             }
10403         }
10404       if (TARGET_HARD_FLOAT && TARGET_VFP)
10405         {
10406           start_reg = FIRST_VFP_REGNUM;
10407
10408           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10409             {
10410               if ((!regs_ever_live[reg] || call_used_regs[reg])
10411                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10412                 {
10413                   if (start_reg != reg)
10414                     saved_regs += vfp_emit_fstmx (start_reg,
10415                                                   (reg - start_reg) / 2);
10416                   start_reg = reg + 2;
10417                 }
10418             }
10419           if (start_reg != reg)
10420             saved_regs += vfp_emit_fstmx (start_reg,
10421                                           (reg - start_reg) / 2);
10422         }
10423     }
10424
10425   if (frame_pointer_needed)
10426     {
10427       /* Create the new frame pointer.  */
10428       insn = GEN_INT (-(4 + args_to_push + fp_offset));
10429       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10430       RTX_FRAME_RELATED_P (insn) = 1;
10431       
10432       if (IS_NESTED (func_type))
10433         {
10434           /* Recover the static chain register.  */
10435           if (regs_ever_live [3] == 0
10436               || saved_pretend_args)
10437             insn = gen_rtx_REG (SImode, 3);
10438           else /* if (current_function_pretend_args_size == 0) */
10439             {
10440               insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10441                                    GEN_INT (4));
10442               insn = gen_rtx_MEM (SImode, insn);
10443             }
10444
10445           emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10446           /* Add a USE to stop propagate_one_insn() from barfing.  */
10447           emit_insn (gen_prologue_use (ip_rtx));
10448         }
10449     }
10450
10451   offsets = arm_get_frame_offsets ();
10452   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10453     {
10454       /* This add can produce multiple insns for a large constant, so we
10455          need to get tricky.  */
10456       rtx last = get_last_insn ();
10457
10458       amount = GEN_INT (offsets->saved_args + saved_regs
10459                         - offsets->outgoing_args);
10460
10461       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10462                                     amount));
10463       do
10464         {
10465           last = last ? NEXT_INSN (last) : get_insns ();
10466           RTX_FRAME_RELATED_P (last) = 1;
10467         }
10468       while (last != insn);
10469
10470       /* If the frame pointer is needed, emit a special barrier that
10471          will prevent the scheduler from moving stores to the frame
10472          before the stack adjustment.  */
10473       if (frame_pointer_needed)
10474         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10475                                          hard_frame_pointer_rtx));
10476     }
10477
10478   /* If we are profiling, make sure no instructions are scheduled before
10479      the call to mcount.  Similarly if the user has requested no
10480      scheduling in the prolog.  */
10481   if (current_function_profile || TARGET_NO_SCHED_PRO)
10482     emit_insn (gen_blockage ());
10483
10484   /* If the link register is being kept alive, with the return address in it,
10485      then make sure that it does not get reused by the ce2 pass.  */
10486   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10487     {
10488       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10489       cfun->machine->lr_save_eliminated = 1;
10490     }
10491 }
10492 \f
10493 /* If CODE is 'd', then the X is a condition operand and the instruction
10494    should only be executed if the condition is true.
10495    if CODE is 'D', then the X is a condition operand and the instruction
10496    should only be executed if the condition is false: however, if the mode
10497    of the comparison is CCFPEmode, then always execute the instruction -- we
10498    do this because in these circumstances !GE does not necessarily imply LT;
10499    in these cases the instruction pattern will take care to make sure that
10500    an instruction containing %d will follow, thereby undoing the effects of
10501    doing this instruction unconditionally.
10502    If CODE is 'N' then X is a floating point operand that must be negated
10503    before output.
10504    If CODE is 'B' then output a bitwise inverted value of X (a const int).
10505    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
10506 void
10507 arm_print_operand (FILE *stream, rtx x, int code)
10508 {
10509   switch (code)
10510     {
10511     case '@':
10512       fputs (ASM_COMMENT_START, stream);
10513       return;
10514
10515     case '_':
10516       fputs (user_label_prefix, stream);
10517       return;
10518           
10519     case '|':
10520       fputs (REGISTER_PREFIX, stream);
10521       return;
10522
10523     case '?':
10524       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10525         {
10526           if (TARGET_THUMB || current_insn_predicate != NULL)
10527             abort ();
10528
10529           fputs (arm_condition_codes[arm_current_cc], stream);
10530         }
10531       else if (current_insn_predicate)
10532         {
10533           enum arm_cond_code code;
10534
10535           if (TARGET_THUMB)
10536             abort ();
10537
10538           code = get_arm_condition_code (current_insn_predicate);
10539           fputs (arm_condition_codes[code], stream);
10540         }
10541       return;
10542
10543     case 'N':
10544       {
10545         REAL_VALUE_TYPE r;
10546         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10547         r = REAL_VALUE_NEGATE (r);
10548         fprintf (stream, "%s", fp_const_from_val (&r));
10549       }
10550       return;
10551
10552     case 'B':
10553       if (GET_CODE (x) == CONST_INT)
10554         {
10555           HOST_WIDE_INT val;
10556           val = ARM_SIGN_EXTEND (~INTVAL (x));
10557           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10558         }
10559       else
10560         {
10561           putc ('~', stream);
10562           output_addr_const (stream, x);
10563         }
10564       return;
10565
10566     case 'i':
10567       fprintf (stream, "%s", arithmetic_instr (x, 1));
10568       return;
10569
10570     /* Truncate Cirrus shift counts.  */
10571     case 's':
10572       if (GET_CODE (x) == CONST_INT)
10573         {
10574           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10575           return;
10576         }
10577       arm_print_operand (stream, x, 0);
10578       return;
10579
10580     case 'I':
10581       fprintf (stream, "%s", arithmetic_instr (x, 0));
10582       return;
10583
10584     case 'S':
10585       {
10586         HOST_WIDE_INT val;
10587         const char * shift = shift_op (x, &val);
10588
10589         if (shift)
10590           {
10591             fprintf (stream, ", %s ", shift_op (x, &val));
10592             if (val == -1)
10593               arm_print_operand (stream, XEXP (x, 1), 0);
10594             else
10595               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10596           }
10597       }
10598       return;
10599
10600       /* An explanation of the 'Q', 'R' and 'H' register operands:
10601          
10602          In a pair of registers containing a DI or DF value the 'Q'
10603          operand returns the register number of the register containing
10604          the least significant part of the value.  The 'R' operand returns
10605          the register number of the register containing the most
10606          significant part of the value.
10607          
10608          The 'H' operand returns the higher of the two register numbers.
10609          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10610          same as the 'Q' operand, since the most significant part of the
10611          value is held in the lower number register.  The reverse is true
10612          on systems where WORDS_BIG_ENDIAN is false.
10613          
10614          The purpose of these operands is to distinguish between cases
10615          where the endian-ness of the values is important (for example
10616          when they are added together), and cases where the endian-ness
10617          is irrelevant, but the order of register operations is important.
10618          For example when loading a value from memory into a register
10619          pair, the endian-ness does not matter.  Provided that the value
10620          from the lower memory address is put into the lower numbered
10621          register, and the value from the higher address is put into the
10622          higher numbered register, the load will work regardless of whether
10623          the value being loaded is big-wordian or little-wordian.  The
10624          order of the two register loads can matter however, if the address
10625          of the memory location is actually held in one of the registers
10626          being overwritten by the load.  */
10627     case 'Q':
10628       if (REGNO (x) > LAST_ARM_REGNUM)
10629         abort ();
10630       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10631       return;
10632
10633     case 'R':
10634       if (REGNO (x) > LAST_ARM_REGNUM)
10635         abort ();
10636       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10637       return;
10638
10639     case 'H':
10640       if (REGNO (x) > LAST_ARM_REGNUM)
10641         abort ();
10642       asm_fprintf (stream, "%r", REGNO (x) + 1);
10643       return;
10644
10645     case 'm':
10646       asm_fprintf (stream, "%r", 
10647                    GET_CODE (XEXP (x, 0)) == REG
10648                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10649       return;
10650
10651     case 'M':
10652       asm_fprintf (stream, "{%r-%r}",
10653                    REGNO (x),
10654                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10655       return;
10656
10657     case 'd':
10658       /* CONST_TRUE_RTX means always -- that's the default.  */
10659       if (x == const_true_rtx)
10660         return;
10661       
10662       fputs (arm_condition_codes[get_arm_condition_code (x)],
10663              stream);
10664       return;
10665
10666     case 'D':
10667       /* CONST_TRUE_RTX means not always -- ie never.  We shouldn't ever
10668          want to do that.  */
10669       if (x == const_true_rtx)
10670         abort ();
10671
10672       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10673                                  (get_arm_condition_code (x))],
10674              stream);
10675       return;
10676
10677     /* Cirrus registers can be accessed in a variety of ways:
10678          single floating point (f)
10679          double floating point (d)
10680          32bit integer         (fx)
10681          64bit integer         (dx).  */
10682     case 'W':                   /* Cirrus register in F mode.  */
10683     case 'X':                   /* Cirrus register in D mode.  */
10684     case 'Y':                   /* Cirrus register in FX mode.  */
10685     case 'Z':                   /* Cirrus register in DX mode.  */
10686       if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10687         abort ();
10688
10689       fprintf (stream, "mv%s%s",
10690                code == 'W' ? "f"
10691                : code == 'X' ? "d"
10692                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10693
10694       return;
10695
10696     /* Print cirrus register in the mode specified by the register's mode.  */
10697     case 'V':
10698       {
10699         int mode = GET_MODE (x);
10700
10701         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10702           abort ();
10703
10704         fprintf (stream, "mv%s%s",
10705                  mode == DFmode ? "d"
10706                  : mode == SImode ? "fx"
10707                  : mode == DImode ? "dx"
10708                  : "f", reg_names[REGNO (x)] + 2);
10709
10710         return;
10711       }
10712
10713     case 'U':
10714       if (GET_CODE (x) != REG
10715           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10716           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10717         /* Bad value for wCG register number.  */
10718         abort ();
10719       else
10720         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10721       return;
10722
10723       /* Print an iWMMXt control register name.  */
10724     case 'w':
10725       if (GET_CODE (x) != CONST_INT
10726           || INTVAL (x) < 0
10727           || INTVAL (x) >= 16)
10728         /* Bad value for wC register number.  */
10729         abort ();
10730       else
10731         {
10732           static const char * wc_reg_names [16] =
10733             {
10734               "wCID",  "wCon",  "wCSSF", "wCASF",
10735               "wC4",   "wC5",   "wC6",   "wC7",
10736               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10737               "wC12",  "wC13",  "wC14",  "wC15"
10738             };
10739           
10740           fprintf (stream, wc_reg_names [INTVAL (x)]);
10741         }
10742       return;
10743
10744       /* Print a VFP double precision register name.  */
10745     case 'P':
10746       {
10747         int mode = GET_MODE (x);
10748         int num;
10749
10750         if (mode != DImode && mode != DFmode)
10751           abort ();
10752
10753         if (GET_CODE (x) != REG
10754             || !IS_VFP_REGNUM (REGNO (x)))
10755           abort ();
10756
10757         num = REGNO(x) - FIRST_VFP_REGNUM;
10758         if (num & 1)
10759           abort ();
10760
10761         fprintf (stream, "d%d", num >> 1);
10762       }
10763       return;
10764
10765     default:
10766       if (x == 0)
10767         abort ();
10768
10769       if (GET_CODE (x) == REG)
10770         asm_fprintf (stream, "%r", REGNO (x));
10771       else if (GET_CODE (x) == MEM)
10772         {
10773           output_memory_reference_mode = GET_MODE (x);
10774           output_address (XEXP (x, 0));
10775         }
10776       else if (GET_CODE (x) == CONST_DOUBLE)
10777         fprintf (stream, "#%s", fp_immediate_constant (x));
10778       else if (GET_CODE (x) == NEG)
10779         abort (); /* This should never happen now.  */
10780       else
10781         {
10782           fputc ('#', stream);
10783           output_addr_const (stream, x);
10784         }
10785     }
10786 }
10787 \f
10788 #ifndef AOF_ASSEMBLER
10789 /* Target hook for assembling integer objects.  The ARM version needs to
10790    handle word-sized values specially.  */
10791 static bool
10792 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10793 {
10794   if (size == UNITS_PER_WORD && aligned_p)
10795     {
10796       fputs ("\t.word\t", asm_out_file);
10797       output_addr_const (asm_out_file, x);
10798
10799       /* Mark symbols as position independent.  We only do this in the
10800          .text segment, not in the .data segment.  */
10801       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10802           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10803         {
10804           if (GET_CODE (x) == SYMBOL_REF
10805               && (CONSTANT_POOL_ADDRESS_P (x)
10806                   || SYMBOL_REF_LOCAL_P (x)))
10807             fputs ("(GOTOFF)", asm_out_file);
10808           else if (GET_CODE (x) == LABEL_REF)
10809             fputs ("(GOTOFF)", asm_out_file);
10810           else
10811             fputs ("(GOT)", asm_out_file);
10812         }
10813       fputc ('\n', asm_out_file);
10814       return true;
10815     }
10816
10817   if (VECTOR_MODE_SUPPORTED_P (GET_MODE (x)))
10818     {
10819       int i, units;
10820
10821       if (GET_CODE (x) != CONST_VECTOR)
10822         abort ();
10823
10824       units = CONST_VECTOR_NUNITS (x);
10825
10826       switch (GET_MODE (x))
10827         {
10828         case V2SImode: size = 4; break;
10829         case V4HImode: size = 2; break;
10830         case V8QImode: size = 1; break;
10831         default:
10832           abort ();
10833         }
10834
10835       for (i = 0; i < units; i++)
10836         {
10837           rtx elt;
10838
10839           elt = CONST_VECTOR_ELT (x, i);
10840           assemble_integer
10841             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10842         }
10843
10844       return true;
10845     }
10846
10847   return default_assemble_integer (x, size, aligned_p);
10848 }
10849 #endif
10850 \f
10851 /* A finite state machine takes care of noticing whether or not instructions
10852    can be conditionally executed, and thus decrease execution time and code
10853    size by deleting branch instructions.  The fsm is controlled by
10854    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
10855
10856 /* The state of the fsm controlling condition codes are:
10857    0: normal, do nothing special
10858    1: make ASM_OUTPUT_OPCODE not output this instruction
10859    2: make ASM_OUTPUT_OPCODE not output this instruction
10860    3: make instructions conditional
10861    4: make instructions conditional
10862
10863    State transitions (state->state by whom under condition):
10864    0 -> 1 final_prescan_insn if the `target' is a label
10865    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10866    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10867    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10868    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10869           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10870    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10871           (the target insn is arm_target_insn).
10872
10873    If the jump clobbers the conditions then we use states 2 and 4.
10874
10875    A similar thing can be done with conditional return insns.
10876
10877    XXX In case the `target' is an unconditional branch, this conditionalising
10878    of the instructions always reduces code size, but not always execution
10879    time.  But then, I want to reduce the code size to somewhere near what
10880    /bin/cc produces.  */
10881
10882 /* Returns the index of the ARM condition code string in
10883    `arm_condition_codes'.  COMPARISON should be an rtx like
10884    `(eq (...) (...))'.  */
10885 static enum arm_cond_code
10886 get_arm_condition_code (rtx comparison)
10887 {
10888   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10889   int code;
10890   enum rtx_code comp_code = GET_CODE (comparison);
10891
10892   if (GET_MODE_CLASS (mode) != MODE_CC)
10893     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10894                            XEXP (comparison, 1));
10895
10896   switch (mode)
10897     {
10898     case CC_DNEmode: code = ARM_NE; goto dominance;
10899     case CC_DEQmode: code = ARM_EQ; goto dominance;
10900     case CC_DGEmode: code = ARM_GE; goto dominance;
10901     case CC_DGTmode: code = ARM_GT; goto dominance;
10902     case CC_DLEmode: code = ARM_LE; goto dominance;
10903     case CC_DLTmode: code = ARM_LT; goto dominance;
10904     case CC_DGEUmode: code = ARM_CS; goto dominance;
10905     case CC_DGTUmode: code = ARM_HI; goto dominance;
10906     case CC_DLEUmode: code = ARM_LS; goto dominance;
10907     case CC_DLTUmode: code = ARM_CC;
10908
10909     dominance:
10910       if (comp_code != EQ && comp_code != NE)
10911         abort ();
10912
10913       if (comp_code == EQ)
10914         return ARM_INVERSE_CONDITION_CODE (code);
10915       return code;
10916
10917     case CC_NOOVmode:
10918       switch (comp_code)
10919         {
10920         case NE: return ARM_NE;
10921         case EQ: return ARM_EQ;
10922         case GE: return ARM_PL;
10923         case LT: return ARM_MI;
10924         default: abort ();
10925         }
10926
10927     case CC_Zmode:
10928       switch (comp_code)
10929         {
10930         case NE: return ARM_NE;
10931         case EQ: return ARM_EQ;
10932         default: abort ();
10933         }
10934
10935     case CC_Nmode:
10936       switch (comp_code)
10937         {
10938         case NE: return ARM_MI;
10939         case EQ: return ARM_PL;
10940         default: abort ();
10941         }
10942
10943     case CCFPEmode:
10944     case CCFPmode:
10945       /* These encodings assume that AC=1 in the FPA system control
10946          byte.  This allows us to handle all cases except UNEQ and
10947          LTGT.  */
10948       switch (comp_code)
10949         {
10950         case GE: return ARM_GE;
10951         case GT: return ARM_GT;
10952         case LE: return ARM_LS;
10953         case LT: return ARM_MI;
10954         case NE: return ARM_NE;
10955         case EQ: return ARM_EQ;
10956         case ORDERED: return ARM_VC;
10957         case UNORDERED: return ARM_VS;
10958         case UNLT: return ARM_LT;
10959         case UNLE: return ARM_LE;
10960         case UNGT: return ARM_HI;
10961         case UNGE: return ARM_PL;
10962           /* UNEQ and LTGT do not have a representation.  */
10963         case UNEQ: /* Fall through.  */
10964         case LTGT: /* Fall through.  */
10965         default: abort ();
10966         }
10967
10968     case CC_SWPmode:
10969       switch (comp_code)
10970         {
10971         case NE: return ARM_NE;
10972         case EQ: return ARM_EQ;
10973         case GE: return ARM_LE;
10974         case GT: return ARM_LT;
10975         case LE: return ARM_GE;
10976         case LT: return ARM_GT;
10977         case GEU: return ARM_LS;
10978         case GTU: return ARM_CC;
10979         case LEU: return ARM_CS;
10980         case LTU: return ARM_HI;
10981         default: abort ();
10982         }
10983
10984     case CC_Cmode:
10985       switch (comp_code)
10986       {
10987       case LTU: return ARM_CS;
10988       case GEU: return ARM_CC;
10989       default: abort ();
10990       }
10991       
10992     case CCmode:
10993       switch (comp_code)
10994         {
10995         case NE: return ARM_NE;
10996         case EQ: return ARM_EQ;
10997         case GE: return ARM_GE;
10998         case GT: return ARM_GT;
10999         case LE: return ARM_LE;
11000         case LT: return ARM_LT;
11001         case GEU: return ARM_CS;
11002         case GTU: return ARM_HI;
11003         case LEU: return ARM_LS;
11004         case LTU: return ARM_CC;
11005         default: abort ();
11006         }
11007
11008     default: abort ();
11009     }
11010
11011   abort ();
11012 }
11013
11014 void
11015 arm_final_prescan_insn (rtx insn)
11016 {
11017   /* BODY will hold the body of INSN.  */
11018   rtx body = PATTERN (insn);
11019
11020   /* This will be 1 if trying to repeat the trick, and things need to be
11021      reversed if it appears to fail.  */
11022   int reverse = 0;
11023
11024   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11025      taken are clobbered, even if the rtl suggests otherwise.  It also
11026      means that we have to grub around within the jump expression to find
11027      out what the conditions are when the jump isn't taken.  */
11028   int jump_clobbers = 0;
11029   
11030   /* If we start with a return insn, we only succeed if we find another one.  */
11031   int seeking_return = 0;
11032   
11033   /* START_INSN will hold the insn from where we start looking.  This is the
11034      first insn after the following code_label if REVERSE is true.  */
11035   rtx start_insn = insn;
11036
11037   /* If in state 4, check if the target branch is reached, in order to
11038      change back to state 0.  */
11039   if (arm_ccfsm_state == 4)
11040     {
11041       if (insn == arm_target_insn)
11042         {
11043           arm_target_insn = NULL;
11044           arm_ccfsm_state = 0;
11045         }
11046       return;
11047     }
11048
11049   /* If in state 3, it is possible to repeat the trick, if this insn is an
11050      unconditional branch to a label, and immediately following this branch
11051      is the previous target label which is only used once, and the label this
11052      branch jumps to is not too far off.  */
11053   if (arm_ccfsm_state == 3)
11054     {
11055       if (simplejump_p (insn))
11056         {
11057           start_insn = next_nonnote_insn (start_insn);
11058           if (GET_CODE (start_insn) == BARRIER)
11059             {
11060               /* XXX Isn't this always a barrier?  */
11061               start_insn = next_nonnote_insn (start_insn);
11062             }
11063           if (GET_CODE (start_insn) == CODE_LABEL
11064               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11065               && LABEL_NUSES (start_insn) == 1)
11066             reverse = TRUE;
11067           else
11068             return;
11069         }
11070       else if (GET_CODE (body) == RETURN)
11071         {
11072           start_insn = next_nonnote_insn (start_insn);
11073           if (GET_CODE (start_insn) == BARRIER)
11074             start_insn = next_nonnote_insn (start_insn);
11075           if (GET_CODE (start_insn) == CODE_LABEL
11076               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11077               && LABEL_NUSES (start_insn) == 1)
11078             {
11079               reverse = TRUE;
11080               seeking_return = 1;
11081             }
11082           else
11083             return;
11084         }
11085       else
11086         return;
11087     }
11088
11089   if (arm_ccfsm_state != 0 && !reverse)
11090     abort ();
11091   if (GET_CODE (insn) != JUMP_INSN)
11092     return;
11093
11094   /* This jump might be paralleled with a clobber of the condition codes 
11095      the jump should always come first */
11096   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11097     body = XVECEXP (body, 0, 0);
11098
11099   if (reverse
11100       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11101           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11102     {
11103       int insns_skipped;
11104       int fail = FALSE, succeed = FALSE;
11105       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
11106       int then_not_else = TRUE;
11107       rtx this_insn = start_insn, label = 0;
11108
11109       /* If the jump cannot be done with one instruction, we cannot 
11110          conditionally execute the instruction in the inverse case.  */
11111       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11112         {
11113           jump_clobbers = 1;
11114           return;
11115         }
11116       
11117       /* Register the insn jumped to.  */
11118       if (reverse)
11119         {
11120           if (!seeking_return)
11121             label = XEXP (SET_SRC (body), 0);
11122         }
11123       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11124         label = XEXP (XEXP (SET_SRC (body), 1), 0);
11125       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11126         {
11127           label = XEXP (XEXP (SET_SRC (body), 2), 0);
11128           then_not_else = FALSE;
11129         }
11130       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11131         seeking_return = 1;
11132       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11133         {
11134           seeking_return = 1;
11135           then_not_else = FALSE;
11136         }
11137       else
11138         abort ();
11139
11140       /* See how many insns this branch skips, and what kind of insns.  If all
11141          insns are okay, and the label or unconditional branch to the same
11142          label is not too far away, succeed.  */
11143       for (insns_skipped = 0;
11144            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11145         {
11146           rtx scanbody;
11147
11148           this_insn = next_nonnote_insn (this_insn);
11149           if (!this_insn)
11150             break;
11151
11152           switch (GET_CODE (this_insn))
11153             {
11154             case CODE_LABEL:
11155               /* Succeed if it is the target label, otherwise fail since
11156                  control falls in from somewhere else.  */
11157               if (this_insn == label)
11158                 {
11159                   if (jump_clobbers)
11160                     {
11161                       arm_ccfsm_state = 2;
11162                       this_insn = next_nonnote_insn (this_insn);
11163                     }
11164                   else
11165                     arm_ccfsm_state = 1;
11166                   succeed = TRUE;
11167                 }
11168               else
11169                 fail = TRUE;
11170               break;
11171
11172             case BARRIER:
11173               /* Succeed if the following insn is the target label.
11174                  Otherwise fail.  
11175                  If return insns are used then the last insn in a function 
11176                  will be a barrier.  */
11177               this_insn = next_nonnote_insn (this_insn);
11178               if (this_insn && this_insn == label)
11179                 {
11180                   if (jump_clobbers)
11181                     {
11182                       arm_ccfsm_state = 2;
11183                       this_insn = next_nonnote_insn (this_insn);
11184                     }
11185                   else
11186                     arm_ccfsm_state = 1;
11187                   succeed = TRUE;
11188                 }
11189               else
11190                 fail = TRUE;
11191               break;
11192
11193             case CALL_INSN:
11194               /* If using 32-bit addresses the cc is not preserved over
11195                  calls.  */
11196               if (TARGET_APCS_32)
11197                 {
11198                   /* Succeed if the following insn is the target label,
11199                      or if the following two insns are a barrier and
11200                      the target label.  */
11201                   this_insn = next_nonnote_insn (this_insn);
11202                   if (this_insn && GET_CODE (this_insn) == BARRIER)
11203                     this_insn = next_nonnote_insn (this_insn);
11204
11205                   if (this_insn && this_insn == label
11206                       && insns_skipped < max_insns_skipped)
11207                     {
11208                       if (jump_clobbers)
11209                         {
11210                           arm_ccfsm_state = 2;
11211                           this_insn = next_nonnote_insn (this_insn);
11212                         }
11213                       else
11214                         arm_ccfsm_state = 1;
11215                       succeed = TRUE;
11216                     }
11217                   else
11218                     fail = TRUE;
11219                 }
11220               break;
11221
11222             case JUMP_INSN:
11223               /* If this is an unconditional branch to the same label, succeed.
11224                  If it is to another label, do nothing.  If it is conditional,
11225                  fail.  */
11226               /* XXX Probably, the tests for SET and the PC are
11227                  unnecessary.  */
11228
11229               scanbody = PATTERN (this_insn);
11230               if (GET_CODE (scanbody) == SET
11231                   && GET_CODE (SET_DEST (scanbody)) == PC)
11232                 {
11233                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11234                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11235                     {
11236                       arm_ccfsm_state = 2;
11237                       succeed = TRUE;
11238                     }
11239                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11240                     fail = TRUE;
11241                 }
11242               /* Fail if a conditional return is undesirable (eg on a
11243                  StrongARM), but still allow this if optimizing for size.  */
11244               else if (GET_CODE (scanbody) == RETURN
11245                        && !use_return_insn (TRUE, NULL)
11246                        && !optimize_size)
11247                 fail = TRUE;
11248               else if (GET_CODE (scanbody) == RETURN
11249                        && seeking_return)
11250                 {
11251                   arm_ccfsm_state = 2;
11252                   succeed = TRUE;
11253                 }
11254               else if (GET_CODE (scanbody) == PARALLEL)
11255                 {
11256                   switch (get_attr_conds (this_insn))
11257                     {
11258                     case CONDS_NOCOND:
11259                       break;
11260                     default:
11261                       fail = TRUE;
11262                       break;
11263                     }
11264                 }
11265               else
11266                 fail = TRUE;    /* Unrecognized jump (eg epilogue).  */
11267
11268               break;
11269
11270             case INSN:
11271               /* Instructions using or affecting the condition codes make it
11272                  fail.  */
11273               scanbody = PATTERN (this_insn);
11274               if (!(GET_CODE (scanbody) == SET
11275                     || GET_CODE (scanbody) == PARALLEL)
11276                   || get_attr_conds (this_insn) != CONDS_NOCOND)
11277                 fail = TRUE;
11278
11279               /* A conditional cirrus instruction must be followed by
11280                  a non Cirrus instruction.  However, since we
11281                  conditionalize instructions in this function and by
11282                  the time we get here we can't add instructions
11283                  (nops), because shorten_branches() has already been
11284                  called, we will disable conditionalizing Cirrus
11285                  instructions to be safe.  */
11286               if (GET_CODE (scanbody) != USE
11287                   && GET_CODE (scanbody) != CLOBBER
11288                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11289                 fail = TRUE;
11290               break;
11291
11292             default:
11293               break;
11294             }
11295         }
11296       if (succeed)
11297         {
11298           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11299             arm_target_label = CODE_LABEL_NUMBER (label);
11300           else if (seeking_return || arm_ccfsm_state == 2)
11301             {
11302               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11303                 {
11304                   this_insn = next_nonnote_insn (this_insn);
11305                   if (this_insn && (GET_CODE (this_insn) == BARRIER
11306                                     || GET_CODE (this_insn) == CODE_LABEL))
11307                     abort ();
11308                 }
11309               if (!this_insn)
11310                 {
11311                   /* Oh, dear! we ran off the end.. give up.  */
11312                   recog (PATTERN (insn), insn, NULL);
11313                   arm_ccfsm_state = 0;
11314                   arm_target_insn = NULL;
11315                   return;
11316                 }
11317               arm_target_insn = this_insn;
11318             }
11319           else
11320             abort ();
11321           if (jump_clobbers)
11322             {
11323               if (reverse)
11324                 abort ();
11325               arm_current_cc = 
11326                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11327                                                             0), 0), 1));
11328               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11329                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11330               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11331                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11332             }
11333           else
11334             {
11335               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11336                  what it was.  */
11337               if (!reverse)
11338                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11339                                                                0));
11340             }
11341
11342           if (reverse || then_not_else)
11343             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11344         }
11345       
11346       /* Restore recog_data (getting the attributes of other insns can
11347          destroy this array, but final.c assumes that it remains intact
11348          across this call; since the insn has been recognized already we
11349          call recog direct).  */
11350       recog (PATTERN (insn), insn, NULL);
11351     }
11352 }
11353
11354 /* Returns true if REGNO is a valid register
11355    for holding a quantity of tyoe MODE.  */
11356 int
11357 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11358 {
11359   if (GET_MODE_CLASS (mode) == MODE_CC)
11360     return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11361   
11362   if (TARGET_THUMB)
11363     /* For the Thumb we only allow values bigger than SImode in
11364        registers 0 - 6, so that there is always a second low
11365        register available to hold the upper part of the value.
11366        We probably we ought to ensure that the register is the
11367        start of an even numbered register pair.  */
11368     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11369
11370   if (IS_CIRRUS_REGNUM (regno))
11371     /* We have outlawed SI values in Cirrus registers because they
11372        reside in the lower 32 bits, but SF values reside in the
11373        upper 32 bits.  This causes gcc all sorts of grief.  We can't
11374        even split the registers into pairs because Cirrus SI values
11375        get sign extended to 64bits-- aldyh.  */
11376     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11377
11378   if (IS_VFP_REGNUM (regno))
11379     {
11380       if (mode == SFmode || mode == SImode)
11381         return TRUE;
11382
11383       /* DFmode values are only valid in even register pairs.  */
11384       if (mode == DFmode)
11385         return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11386       return FALSE;
11387     }
11388
11389   if (IS_IWMMXT_GR_REGNUM (regno))
11390     return mode == SImode;
11391
11392   if (IS_IWMMXT_REGNUM (regno))
11393     return VALID_IWMMXT_REG_MODE (mode);
11394
11395   if (regno <= LAST_ARM_REGNUM)
11396     /* We allow any value to be stored in the general registers.  */
11397     return 1;
11398
11399   if (   regno == FRAME_POINTER_REGNUM
11400       || regno == ARG_POINTER_REGNUM)
11401     /* We only allow integers in the fake hard registers.  */
11402     return GET_MODE_CLASS (mode) == MODE_INT;
11403
11404   /* The only registers left are the FPA registers
11405      which we only allow to hold FP values.  */
11406   return GET_MODE_CLASS (mode) == MODE_FLOAT
11407     && regno >= FIRST_FPA_REGNUM
11408     && regno <= LAST_FPA_REGNUM;
11409 }
11410
11411 int
11412 arm_regno_class (int regno)
11413 {
11414   if (TARGET_THUMB)
11415     {
11416       if (regno == STACK_POINTER_REGNUM)
11417         return STACK_REG;
11418       if (regno == CC_REGNUM)
11419         return CC_REG;
11420       if (regno < 8)
11421         return LO_REGS;
11422       return HI_REGS;
11423     }
11424
11425   if (   regno <= LAST_ARM_REGNUM
11426       || regno == FRAME_POINTER_REGNUM
11427       || regno == ARG_POINTER_REGNUM)
11428     return GENERAL_REGS;
11429   
11430   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11431     return NO_REGS;
11432
11433   if (IS_CIRRUS_REGNUM (regno))
11434     return CIRRUS_REGS;
11435
11436   if (IS_VFP_REGNUM (regno))
11437     return VFP_REGS;
11438
11439   if (IS_IWMMXT_REGNUM (regno))
11440     return IWMMXT_REGS;
11441
11442   if (IS_IWMMXT_GR_REGNUM (regno))
11443     return IWMMXT_GR_REGS;
11444
11445   return FPA_REGS;
11446 }
11447
11448 /* Handle a special case when computing the offset
11449    of an argument from the frame pointer.  */
11450 int
11451 arm_debugger_arg_offset (int value, rtx addr)
11452 {
11453   rtx insn;
11454
11455   /* We are only interested if dbxout_parms() failed to compute the offset.  */
11456   if (value != 0)
11457     return 0;
11458
11459   /* We can only cope with the case where the address is held in a register.  */
11460   if (GET_CODE (addr) != REG)
11461     return 0;
11462
11463   /* If we are using the frame pointer to point at the argument, then
11464      an offset of 0 is correct.  */
11465   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11466     return 0;
11467   
11468   /* If we are using the stack pointer to point at the
11469      argument, then an offset of 0 is correct.  */
11470   if ((TARGET_THUMB || !frame_pointer_needed)
11471       && REGNO (addr) == SP_REGNUM)
11472     return 0;
11473   
11474   /* Oh dear.  The argument is pointed to by a register rather
11475      than being held in a register, or being stored at a known
11476      offset from the frame pointer.  Since GDB only understands
11477      those two kinds of argument we must translate the address
11478      held in the register into an offset from the frame pointer.
11479      We do this by searching through the insns for the function
11480      looking to see where this register gets its value.  If the
11481      register is initialized from the frame pointer plus an offset
11482      then we are in luck and we can continue, otherwise we give up.
11483      
11484      This code is exercised by producing debugging information
11485      for a function with arguments like this:
11486      
11487            double func (double a, double b, int c, double d) {return d;}
11488      
11489      Without this code the stab for parameter 'd' will be set to
11490      an offset of 0 from the frame pointer, rather than 8.  */
11491
11492   /* The if() statement says:
11493
11494      If the insn is a normal instruction
11495      and if the insn is setting the value in a register
11496      and if the register being set is the register holding the address of the argument
11497      and if the address is computing by an addition
11498      that involves adding to a register
11499      which is the frame pointer
11500      a constant integer
11501
11502      then...  */
11503   
11504   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11505     {
11506       if (   GET_CODE (insn) == INSN 
11507           && GET_CODE (PATTERN (insn)) == SET
11508           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11509           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11510           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11511           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11512           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11513              )
11514         {
11515           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11516           
11517           break;
11518         }
11519     }
11520   
11521   if (value == 0)
11522     {
11523       debug_rtx (addr);
11524       warning ("unable to compute real location of stacked parameter");
11525       value = 8; /* XXX magic hack */
11526     }
11527
11528   return value;
11529 }
11530 \f
11531 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
11532   do                                                                    \
11533     {                                                                   \
11534       if ((MASK) & insn_flags)                                          \
11535         builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, NULL, NULL_TREE);        \
11536     }                                                                   \
11537   while (0)
11538
11539 struct builtin_description
11540 {
11541   const unsigned int       mask;
11542   const enum insn_code     icode;
11543   const char * const       name;
11544   const enum arm_builtins  code;
11545   const enum rtx_code      comparison;
11546   const unsigned int       flag;
11547 };
11548
11549 static const struct builtin_description bdesc_2arg[] =
11550 {
11551 #define IWMMXT_BUILTIN(code, string, builtin) \
11552   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11553     ARM_BUILTIN_##builtin, 0, 0 },
11554
11555   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11556   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11557   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11558   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11559   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11560   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11561   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11562   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11563   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11564   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11565   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11566   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11567   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11568   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11569   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11570   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11571   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11572   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11573   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11574   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11575   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11576   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11577   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11578   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11579   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11580   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11581   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11582   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11583   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11584   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11585   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11586   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11587   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11588   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11589   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11590   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11591   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11592   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11593   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11594   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11595   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11596   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11597   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11598   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11599   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11600   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11601   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11602   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11603   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11604   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11605   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11606   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11607   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11608   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11609   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11610   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11611   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11612   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11613
11614 #define IWMMXT_BUILTIN2(code, builtin) \
11615   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11616   
11617   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11618   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11619   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11620   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11621   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11622   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11623   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
11624   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
11625   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
11626   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
11627   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
11628   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
11629   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
11630   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
11631   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
11632   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
11633   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
11634   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
11635   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
11636   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
11637   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
11638   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
11639   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
11640   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
11641   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
11642   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
11643   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
11644   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
11645   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
11646   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
11647   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
11648   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
11649 };
11650
11651 static const struct builtin_description bdesc_1arg[] =
11652 {
11653   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11654   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11655   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11656   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11657   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11658   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11659   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11660   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11661   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11662   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11663   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11664   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11665   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11666   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11667   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11668   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11669   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11670   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11671 };
11672
11673 /* Set up all the iWMMXt builtins.  This is
11674    not called if TARGET_IWMMXT is zero.  */
11675
11676 static void
11677 arm_init_iwmmxt_builtins (void)
11678 {
11679   const struct builtin_description * d;
11680   size_t i;
11681   tree endlink = void_list_node;
11682
11683   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11684   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11685   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11686
11687   tree int_ftype_int
11688     = build_function_type (integer_type_node,
11689                            tree_cons (NULL_TREE, integer_type_node, endlink));
11690   tree v8qi_ftype_v8qi_v8qi_int
11691     = build_function_type (V8QI_type_node,
11692                            tree_cons (NULL_TREE, V8QI_type_node,
11693                                       tree_cons (NULL_TREE, V8QI_type_node,
11694                                                  tree_cons (NULL_TREE,
11695                                                             integer_type_node,
11696                                                             endlink))));
11697   tree v4hi_ftype_v4hi_int
11698     = build_function_type (V4HI_type_node,
11699                            tree_cons (NULL_TREE, V4HI_type_node,
11700                                       tree_cons (NULL_TREE, integer_type_node,
11701                                                  endlink)));
11702   tree v2si_ftype_v2si_int
11703     = build_function_type (V2SI_type_node,
11704                            tree_cons (NULL_TREE, V2SI_type_node,
11705                                       tree_cons (NULL_TREE, integer_type_node,
11706                                                  endlink)));
11707   tree v2si_ftype_di_di
11708     = build_function_type (V2SI_type_node,
11709                            tree_cons (NULL_TREE, long_long_integer_type_node,
11710                                       tree_cons (NULL_TREE, long_long_integer_type_node,
11711                                                  endlink)));
11712   tree di_ftype_di_int
11713     = build_function_type (long_long_integer_type_node,
11714                            tree_cons (NULL_TREE, long_long_integer_type_node,
11715                                       tree_cons (NULL_TREE, integer_type_node,
11716                                                  endlink)));
11717   tree di_ftype_di_int_int
11718     = build_function_type (long_long_integer_type_node,
11719                            tree_cons (NULL_TREE, long_long_integer_type_node,
11720                                       tree_cons (NULL_TREE, integer_type_node,
11721                                                  tree_cons (NULL_TREE,
11722                                                             integer_type_node,
11723                                                             endlink))));
11724   tree int_ftype_v8qi
11725     = build_function_type (integer_type_node,
11726                            tree_cons (NULL_TREE, V8QI_type_node,
11727                                       endlink));
11728   tree int_ftype_v4hi
11729     = build_function_type (integer_type_node,
11730                            tree_cons (NULL_TREE, V4HI_type_node,
11731                                       endlink));
11732   tree int_ftype_v2si
11733     = build_function_type (integer_type_node,
11734                            tree_cons (NULL_TREE, V2SI_type_node,
11735                                       endlink));
11736   tree int_ftype_v8qi_int
11737     = build_function_type (integer_type_node,
11738                            tree_cons (NULL_TREE, V8QI_type_node,
11739                                       tree_cons (NULL_TREE, integer_type_node,
11740                                                  endlink)));
11741   tree int_ftype_v4hi_int
11742     = build_function_type (integer_type_node,
11743                            tree_cons (NULL_TREE, V4HI_type_node,
11744                                       tree_cons (NULL_TREE, integer_type_node,
11745                                                  endlink)));
11746   tree int_ftype_v2si_int
11747     = build_function_type (integer_type_node,
11748                            tree_cons (NULL_TREE, V2SI_type_node,
11749                                       tree_cons (NULL_TREE, integer_type_node,
11750                                                  endlink)));
11751   tree v8qi_ftype_v8qi_int_int
11752     = build_function_type (V8QI_type_node,
11753                            tree_cons (NULL_TREE, V8QI_type_node,
11754                                       tree_cons (NULL_TREE, integer_type_node,
11755                                                  tree_cons (NULL_TREE,
11756                                                             integer_type_node,
11757                                                             endlink))));
11758   tree v4hi_ftype_v4hi_int_int
11759     = build_function_type (V4HI_type_node,
11760                            tree_cons (NULL_TREE, V4HI_type_node,
11761                                       tree_cons (NULL_TREE, integer_type_node,
11762                                                  tree_cons (NULL_TREE,
11763                                                             integer_type_node,
11764                                                             endlink))));
11765   tree v2si_ftype_v2si_int_int
11766     = build_function_type (V2SI_type_node,
11767                            tree_cons (NULL_TREE, V2SI_type_node,
11768                                       tree_cons (NULL_TREE, integer_type_node,
11769                                                  tree_cons (NULL_TREE,
11770                                                             integer_type_node,
11771                                                             endlink))));
11772   /* Miscellaneous.  */
11773   tree v8qi_ftype_v4hi_v4hi
11774     = build_function_type (V8QI_type_node,
11775                            tree_cons (NULL_TREE, V4HI_type_node,
11776                                       tree_cons (NULL_TREE, V4HI_type_node,
11777                                                  endlink)));
11778   tree v4hi_ftype_v2si_v2si
11779     = build_function_type (V4HI_type_node,
11780                            tree_cons (NULL_TREE, V2SI_type_node,
11781                                       tree_cons (NULL_TREE, V2SI_type_node,
11782                                                  endlink)));
11783   tree v2si_ftype_v4hi_v4hi
11784     = build_function_type (V2SI_type_node,
11785                            tree_cons (NULL_TREE, V4HI_type_node,
11786                                       tree_cons (NULL_TREE, V4HI_type_node,
11787                                                  endlink)));
11788   tree v2si_ftype_v8qi_v8qi
11789     = build_function_type (V2SI_type_node,
11790                            tree_cons (NULL_TREE, V8QI_type_node,
11791                                       tree_cons (NULL_TREE, V8QI_type_node,
11792                                                  endlink)));
11793   tree v4hi_ftype_v4hi_di
11794     = build_function_type (V4HI_type_node,
11795                            tree_cons (NULL_TREE, V4HI_type_node,
11796                                       tree_cons (NULL_TREE,
11797                                                  long_long_integer_type_node,
11798                                                  endlink)));
11799   tree v2si_ftype_v2si_di
11800     = build_function_type (V2SI_type_node,
11801                            tree_cons (NULL_TREE, V2SI_type_node,
11802                                       tree_cons (NULL_TREE,
11803                                                  long_long_integer_type_node,
11804                                                  endlink)));
11805   tree void_ftype_int_int
11806     = build_function_type (void_type_node,
11807                            tree_cons (NULL_TREE, integer_type_node,
11808                                       tree_cons (NULL_TREE, integer_type_node,
11809                                                  endlink)));
11810   tree di_ftype_void
11811     = build_function_type (long_long_unsigned_type_node, endlink);
11812   tree di_ftype_v8qi
11813     = build_function_type (long_long_integer_type_node,
11814                            tree_cons (NULL_TREE, V8QI_type_node,
11815                                       endlink));
11816   tree di_ftype_v4hi
11817     = build_function_type (long_long_integer_type_node,
11818                            tree_cons (NULL_TREE, V4HI_type_node,
11819                                       endlink));
11820   tree di_ftype_v2si
11821     = build_function_type (long_long_integer_type_node,
11822                            tree_cons (NULL_TREE, V2SI_type_node,
11823                                       endlink));
11824   tree v2si_ftype_v4hi
11825     = build_function_type (V2SI_type_node,
11826                            tree_cons (NULL_TREE, V4HI_type_node,
11827                                       endlink));
11828   tree v4hi_ftype_v8qi
11829     = build_function_type (V4HI_type_node,
11830                            tree_cons (NULL_TREE, V8QI_type_node,
11831                                       endlink));
11832
11833   tree di_ftype_di_v4hi_v4hi
11834     = build_function_type (long_long_unsigned_type_node,
11835                            tree_cons (NULL_TREE,
11836                                       long_long_unsigned_type_node,
11837                                       tree_cons (NULL_TREE, V4HI_type_node,
11838                                                  tree_cons (NULL_TREE,
11839                                                             V4HI_type_node,
11840                                                             endlink))));
11841
11842   tree di_ftype_v4hi_v4hi
11843     = build_function_type (long_long_unsigned_type_node,
11844                            tree_cons (NULL_TREE, V4HI_type_node,
11845                                       tree_cons (NULL_TREE, V4HI_type_node,
11846                                                  endlink)));
11847
11848   /* Normal vector binops.  */
11849   tree v8qi_ftype_v8qi_v8qi
11850     = build_function_type (V8QI_type_node,
11851                            tree_cons (NULL_TREE, V8QI_type_node,
11852                                       tree_cons (NULL_TREE, V8QI_type_node,
11853                                                  endlink)));
11854   tree v4hi_ftype_v4hi_v4hi
11855     = build_function_type (V4HI_type_node,
11856                            tree_cons (NULL_TREE, V4HI_type_node,
11857                                       tree_cons (NULL_TREE, V4HI_type_node,
11858                                                  endlink)));
11859   tree v2si_ftype_v2si_v2si
11860     = build_function_type (V2SI_type_node,
11861                            tree_cons (NULL_TREE, V2SI_type_node,
11862                                       tree_cons (NULL_TREE, V2SI_type_node,
11863                                                  endlink)));
11864   tree di_ftype_di_di
11865     = build_function_type (long_long_unsigned_type_node,
11866                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
11867                                       tree_cons (NULL_TREE,
11868                                                  long_long_unsigned_type_node,
11869                                                  endlink)));
11870
11871   /* Add all builtins that are more or less simple operations on two
11872      operands.  */
11873   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11874     {
11875       /* Use one of the operands; the target can have a different mode for
11876          mask-generating compares.  */
11877       enum machine_mode mode;
11878       tree type;
11879
11880       if (d->name == 0)
11881         continue;
11882
11883       mode = insn_data[d->icode].operand[1].mode;
11884
11885       switch (mode)
11886         {
11887         case V8QImode:
11888           type = v8qi_ftype_v8qi_v8qi;
11889           break;
11890         case V4HImode:
11891           type = v4hi_ftype_v4hi_v4hi;
11892           break;
11893         case V2SImode:
11894           type = v2si_ftype_v2si_v2si;
11895           break;
11896         case DImode:
11897           type = di_ftype_di_di;
11898           break;
11899
11900         default:
11901           abort ();
11902         }
11903
11904       def_mbuiltin (d->mask, d->name, type, d->code);
11905     }
11906
11907   /* Add the remaining MMX insns with somewhat more complicated types.  */
11908   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11909   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11910   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11911
11912   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11913   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11914   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11915   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11916   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11917   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11918
11919   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11920   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11921   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11922   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11923   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11924   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11925
11926   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11927   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11928   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11929   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11930   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11931   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11932
11933   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11934   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11935   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11936   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11937   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11938   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11939
11940   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11941
11942   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11943   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11944   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11945   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11946
11947   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11948   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11949   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11950   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11951   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11952   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11953   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11954   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11955   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11956
11957   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11958   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11959   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11960
11961   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11962   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11963   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
11964
11965   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
11966   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
11967   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
11968   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
11969   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
11970   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
11971
11972   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
11973   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
11974   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
11975   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
11976   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
11977   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
11978   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
11979   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
11980   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
11981   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
11982   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
11983   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
11984
11985   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
11986   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
11987   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
11988   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
11989
11990   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
11991   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
11992   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
11993   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
11994   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
11995   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
11996   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
11997 }
11998
11999 static void
12000 arm_init_builtins (void)
12001 {
12002   if (TARGET_REALLY_IWMMXT)
12003     arm_init_iwmmxt_builtins ();
12004 }
12005
12006 /* Errors in the source file can cause expand_expr to return const0_rtx
12007    where we expect a vector.  To avoid crashing, use one of the vector
12008    clear instructions.  */
12009
12010 static rtx
12011 safe_vector_operand (rtx x, enum machine_mode mode)
12012 {
12013   if (x != const0_rtx)
12014     return x;
12015   x = gen_reg_rtx (mode);
12016
12017   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12018                                : gen_rtx_SUBREG (DImode, x, 0)));
12019   return x;
12020 }
12021
12022 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
12023
12024 static rtx
12025 arm_expand_binop_builtin (enum insn_code icode,
12026                           tree arglist, rtx target)
12027 {
12028   rtx pat;
12029   tree arg0 = TREE_VALUE (arglist);
12030   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12031   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12032   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12033   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12034   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12035   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12036
12037   if (VECTOR_MODE_P (mode0))
12038     op0 = safe_vector_operand (op0, mode0);
12039   if (VECTOR_MODE_P (mode1))
12040     op1 = safe_vector_operand (op1, mode1);
12041
12042   if (! target
12043       || GET_MODE (target) != tmode
12044       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12045     target = gen_reg_rtx (tmode);
12046
12047   /* In case the insn wants input operands in modes different from
12048      the result, abort.  */
12049   if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
12050     abort ();
12051
12052   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12053     op0 = copy_to_mode_reg (mode0, op0);
12054   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12055     op1 = copy_to_mode_reg (mode1, op1);
12056
12057   pat = GEN_FCN (icode) (target, op0, op1);
12058   if (! pat)
12059     return 0;
12060   emit_insn (pat);
12061   return target;
12062 }
12063
12064 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
12065
12066 static rtx
12067 arm_expand_unop_builtin (enum insn_code icode,
12068                          tree arglist, rtx target, int do_load)
12069 {
12070   rtx pat;
12071   tree arg0 = TREE_VALUE (arglist);
12072   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12073   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12074   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12075
12076   if (! target
12077       || GET_MODE (target) != tmode
12078       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12079     target = gen_reg_rtx (tmode);
12080   if (do_load)
12081     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12082   else
12083     {
12084       if (VECTOR_MODE_P (mode0))
12085         op0 = safe_vector_operand (op0, mode0);
12086
12087       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12088         op0 = copy_to_mode_reg (mode0, op0);
12089     }
12090
12091   pat = GEN_FCN (icode) (target, op0);
12092   if (! pat)
12093     return 0;
12094   emit_insn (pat);
12095   return target;
12096 }
12097
12098 /* Expand an expression EXP that calls a built-in function,
12099    with result going to TARGET if that's convenient
12100    (and in mode MODE if that's convenient).
12101    SUBTARGET may be used as the target for computing one of EXP's operands.
12102    IGNORE is nonzero if the value is to be ignored.  */
12103
12104 static rtx
12105 arm_expand_builtin (tree exp,
12106                     rtx target,
12107                     rtx subtarget ATTRIBUTE_UNUSED,
12108                     enum machine_mode mode ATTRIBUTE_UNUSED,
12109                     int ignore ATTRIBUTE_UNUSED)
12110 {
12111   const struct builtin_description * d;
12112   enum insn_code    icode;
12113   tree              fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12114   tree              arglist = TREE_OPERAND (exp, 1);
12115   tree              arg0;
12116   tree              arg1;
12117   tree              arg2;
12118   rtx               op0;
12119   rtx               op1;
12120   rtx               op2;
12121   rtx               pat;
12122   int               fcode = DECL_FUNCTION_CODE (fndecl);
12123   size_t            i;
12124   enum machine_mode tmode;
12125   enum machine_mode mode0;
12126   enum machine_mode mode1;
12127   enum machine_mode mode2;
12128
12129   switch (fcode)
12130     {
12131     case ARM_BUILTIN_TEXTRMSB:
12132     case ARM_BUILTIN_TEXTRMUB:
12133     case ARM_BUILTIN_TEXTRMSH:
12134     case ARM_BUILTIN_TEXTRMUH:
12135     case ARM_BUILTIN_TEXTRMSW:
12136     case ARM_BUILTIN_TEXTRMUW:
12137       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12138                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12139                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12140                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12141                : CODE_FOR_iwmmxt_textrmw);
12142
12143       arg0 = TREE_VALUE (arglist);
12144       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12145       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12146       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12147       tmode = insn_data[icode].operand[0].mode;
12148       mode0 = insn_data[icode].operand[1].mode;
12149       mode1 = insn_data[icode].operand[2].mode;
12150
12151       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12152         op0 = copy_to_mode_reg (mode0, op0);
12153       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12154         {
12155           /* @@@ better error message */
12156           error ("selector must be an immediate");
12157           return gen_reg_rtx (tmode);
12158         }
12159       if (target == 0
12160           || GET_MODE (target) != tmode
12161           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12162         target = gen_reg_rtx (tmode);
12163       pat = GEN_FCN (icode) (target, op0, op1);
12164       if (! pat)
12165         return 0;
12166       emit_insn (pat);
12167       return target;
12168
12169     case ARM_BUILTIN_TINSRB:
12170     case ARM_BUILTIN_TINSRH:
12171     case ARM_BUILTIN_TINSRW:
12172       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12173                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12174                : CODE_FOR_iwmmxt_tinsrw);
12175       arg0 = TREE_VALUE (arglist);
12176       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12177       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12178       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12179       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12180       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12181       tmode = insn_data[icode].operand[0].mode;
12182       mode0 = insn_data[icode].operand[1].mode;
12183       mode1 = insn_data[icode].operand[2].mode;
12184       mode2 = insn_data[icode].operand[3].mode;
12185
12186       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12187         op0 = copy_to_mode_reg (mode0, op0);
12188       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12189         op1 = copy_to_mode_reg (mode1, op1);
12190       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12191         {
12192           /* @@@ better error message */
12193           error ("selector must be an immediate");
12194           return const0_rtx;
12195         }
12196       if (target == 0
12197           || GET_MODE (target) != tmode
12198           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12199         target = gen_reg_rtx (tmode);
12200       pat = GEN_FCN (icode) (target, op0, op1, op2);
12201       if (! pat)
12202         return 0;
12203       emit_insn (pat);
12204       return target;
12205
12206     case ARM_BUILTIN_SETWCX:
12207       arg0 = TREE_VALUE (arglist);
12208       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12209       op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12210       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12211       emit_insn (gen_iwmmxt_tmcr (op1, op0));
12212       return 0;
12213
12214     case ARM_BUILTIN_GETWCX:
12215       arg0 = TREE_VALUE (arglist);
12216       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12217       target = gen_reg_rtx (SImode);
12218       emit_insn (gen_iwmmxt_tmrc (target, op0));
12219       return target;
12220
12221     case ARM_BUILTIN_WSHUFH:
12222       icode = CODE_FOR_iwmmxt_wshufh;
12223       arg0 = TREE_VALUE (arglist);
12224       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12225       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12226       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12227       tmode = insn_data[icode].operand[0].mode;
12228       mode1 = insn_data[icode].operand[1].mode;
12229       mode2 = insn_data[icode].operand[2].mode;
12230
12231       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12232         op0 = copy_to_mode_reg (mode1, op0);
12233       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12234         {
12235           /* @@@ better error message */
12236           error ("mask must be an immediate");
12237           return const0_rtx;
12238         }
12239       if (target == 0
12240           || GET_MODE (target) != tmode
12241           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12242         target = gen_reg_rtx (tmode);
12243       pat = GEN_FCN (icode) (target, op0, op1);
12244       if (! pat)
12245         return 0;
12246       emit_insn (pat);
12247       return target;
12248
12249     case ARM_BUILTIN_WSADB:
12250       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12251     case ARM_BUILTIN_WSADH:
12252       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12253     case ARM_BUILTIN_WSADBZ:
12254       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12255     case ARM_BUILTIN_WSADHZ:
12256       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12257
12258       /* Several three-argument builtins.  */
12259     case ARM_BUILTIN_WMACS:
12260     case ARM_BUILTIN_WMACU:
12261     case ARM_BUILTIN_WALIGN:
12262     case ARM_BUILTIN_TMIA:
12263     case ARM_BUILTIN_TMIAPH:
12264     case ARM_BUILTIN_TMIATT:
12265     case ARM_BUILTIN_TMIATB:
12266     case ARM_BUILTIN_TMIABT:
12267     case ARM_BUILTIN_TMIABB:
12268       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12269                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12270                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12271                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12272                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12273                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12274                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12275                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12276                : CODE_FOR_iwmmxt_walign);
12277       arg0 = TREE_VALUE (arglist);
12278       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12279       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12280       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12281       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12282       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12283       tmode = insn_data[icode].operand[0].mode;
12284       mode0 = insn_data[icode].operand[1].mode;
12285       mode1 = insn_data[icode].operand[2].mode;
12286       mode2 = insn_data[icode].operand[3].mode;
12287
12288       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12289         op0 = copy_to_mode_reg (mode0, op0);
12290       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12291         op1 = copy_to_mode_reg (mode1, op1);
12292       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12293         op2 = copy_to_mode_reg (mode2, op2);
12294       if (target == 0
12295           || GET_MODE (target) != tmode
12296           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12297         target = gen_reg_rtx (tmode);
12298       pat = GEN_FCN (icode) (target, op0, op1, op2);
12299       if (! pat)
12300         return 0;
12301       emit_insn (pat);
12302       return target;
12303       
12304     case ARM_BUILTIN_WZERO:
12305       target = gen_reg_rtx (DImode);
12306       emit_insn (gen_iwmmxt_clrdi (target));
12307       return target;
12308
12309     default:
12310       break;
12311     }
12312
12313   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12314     if (d->code == (const enum arm_builtins) fcode)
12315       return arm_expand_binop_builtin (d->icode, arglist, target);
12316
12317   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12318     if (d->code == (const enum arm_builtins) fcode)
12319       return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12320
12321   /* @@@ Should really do something sensible here.  */
12322   return NULL_RTX;
12323 }
12324 \f
12325 /* Recursively search through all of the blocks in a function
12326    checking to see if any of the variables created in that
12327    function match the RTX called 'orig'.  If they do then
12328    replace them with the RTX called 'new'.  */
12329 static void
12330 replace_symbols_in_block (tree block, rtx orig, rtx new)
12331 {
12332   for (; block; block = BLOCK_CHAIN (block))
12333     {
12334       tree sym;
12335       
12336       if (!TREE_USED (block))
12337         continue;
12338
12339       for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
12340         {
12341           if (  (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
12342               || DECL_IGNORED_P (sym)
12343               || TREE_CODE (sym) != VAR_DECL
12344               || DECL_EXTERNAL (sym)
12345               || !rtx_equal_p (DECL_RTL (sym), orig)
12346               )
12347             continue;
12348
12349           SET_DECL_RTL (sym, new);
12350         }
12351       
12352       replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
12353     }
12354 }
12355
12356 /* Return the number (counting from 0) of
12357    the least significant set bit in MASK.  */
12358
12359 inline static int
12360 number_of_first_bit_set (int mask)
12361 {
12362   int bit;
12363
12364   for (bit = 0;
12365        (mask & (1 << bit)) == 0;
12366        ++bit)
12367     continue;
12368
12369   return bit;
12370 }
12371
12372 /* Generate code to return from a thumb function.
12373    If 'reg_containing_return_addr' is -1, then the return address is
12374    actually on the stack, at the stack pointer.  */
12375 static void
12376 thumb_exit (FILE *f, int reg_containing_return_addr, rtx eh_ofs)
12377 {
12378   unsigned regs_available_for_popping;
12379   unsigned regs_to_pop;
12380   int pops_needed;
12381   unsigned available;
12382   unsigned required;
12383   int mode;
12384   int size;
12385   int restore_a4 = FALSE;
12386
12387   /* Compute the registers we need to pop.  */
12388   regs_to_pop = 0;
12389   pops_needed = 0;
12390
12391   /* There is an assumption here, that if eh_ofs is not NULL, the
12392      normal return address will have been pushed.  */
12393   if (reg_containing_return_addr == -1 || eh_ofs)
12394     {
12395       /* When we are generating a return for __builtin_eh_return, 
12396          reg_containing_return_addr must specify the return regno.  */
12397       if (eh_ofs && reg_containing_return_addr == -1)
12398         abort ();
12399
12400       regs_to_pop |= 1 << LR_REGNUM;
12401       ++pops_needed;
12402     }
12403
12404   if (TARGET_BACKTRACE)
12405     {
12406       /* Restore the (ARM) frame pointer and stack pointer.  */
12407       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12408       pops_needed += 2;
12409     }
12410
12411   /* If there is nothing to pop then just emit the BX instruction and
12412      return.  */
12413   if (pops_needed == 0)
12414     {
12415       if (eh_ofs)
12416         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12417
12418       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12419       return;
12420     }
12421   /* Otherwise if we are not supporting interworking and we have not created
12422      a backtrace structure and the function was not entered in ARM mode then
12423      just pop the return address straight into the PC.  */
12424   else if (!TARGET_INTERWORK
12425            && !TARGET_BACKTRACE
12426            && !is_called_in_ARM_mode (current_function_decl))
12427     {
12428       if (eh_ofs)
12429         {
12430           asm_fprintf (f, "\tadd\t%r, #4\n", SP_REGNUM);
12431           asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12432           asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12433         }
12434       else
12435         asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12436
12437       return;
12438     }
12439
12440   /* Find out how many of the (return) argument registers we can corrupt.  */
12441   regs_available_for_popping = 0;
12442
12443   /* If returning via __builtin_eh_return, the bottom three registers
12444      all contain information needed for the return.  */
12445   if (eh_ofs)
12446     size = 12;
12447   else
12448     {
12449 #ifdef RTX_CODE
12450       /* If we can deduce the registers used from the function's
12451          return value.  This is more reliable that examining
12452          regs_ever_live[] because that will be set if the register is
12453          ever used in the function, not just if the register is used
12454          to hold a return value.  */
12455
12456       if (current_function_return_rtx != 0)
12457         mode = GET_MODE (current_function_return_rtx);
12458       else
12459 #endif
12460         mode = DECL_MODE (DECL_RESULT (current_function_decl));
12461
12462       size = GET_MODE_SIZE (mode);
12463
12464       if (size == 0)
12465         {
12466           /* In a void function we can use any argument register.
12467              In a function that returns a structure on the stack
12468              we can use the second and third argument registers.  */
12469           if (mode == VOIDmode)
12470             regs_available_for_popping =
12471               (1 << ARG_REGISTER (1))
12472               | (1 << ARG_REGISTER (2))
12473               | (1 << ARG_REGISTER (3));
12474           else
12475             regs_available_for_popping =
12476               (1 << ARG_REGISTER (2))
12477               | (1 << ARG_REGISTER (3));
12478         }
12479       else if (size <= 4)
12480         regs_available_for_popping =
12481           (1 << ARG_REGISTER (2))
12482           | (1 << ARG_REGISTER (3));
12483       else if (size <= 8)
12484         regs_available_for_popping =
12485           (1 << ARG_REGISTER (3));
12486     }
12487
12488   /* Match registers to be popped with registers into which we pop them.  */
12489   for (available = regs_available_for_popping,
12490        required  = regs_to_pop;
12491        required != 0 && available != 0;
12492        available &= ~(available & - available),
12493        required  &= ~(required  & - required))
12494     -- pops_needed;
12495
12496   /* If we have any popping registers left over, remove them.  */
12497   if (available > 0)
12498     regs_available_for_popping &= ~available;
12499   
12500   /* Otherwise if we need another popping register we can use
12501      the fourth argument register.  */
12502   else if (pops_needed)
12503     {
12504       /* If we have not found any free argument registers and
12505          reg a4 contains the return address, we must move it.  */
12506       if (regs_available_for_popping == 0
12507           && reg_containing_return_addr == LAST_ARG_REGNUM)
12508         {
12509           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12510           reg_containing_return_addr = LR_REGNUM;
12511         }
12512       else if (size > 12)
12513         {
12514           /* Register a4 is being used to hold part of the return value,
12515              but we have dire need of a free, low register.  */
12516           restore_a4 = TRUE;
12517           
12518           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12519         }
12520       
12521       if (reg_containing_return_addr != LAST_ARG_REGNUM)
12522         {
12523           /* The fourth argument register is available.  */
12524           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12525           
12526           --pops_needed;
12527         }
12528     }
12529
12530   /* Pop as many registers as we can.  */
12531   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12532                  regs_available_for_popping);
12533
12534   /* Process the registers we popped.  */
12535   if (reg_containing_return_addr == -1)
12536     {
12537       /* The return address was popped into the lowest numbered register.  */
12538       regs_to_pop &= ~(1 << LR_REGNUM);
12539       
12540       reg_containing_return_addr =
12541         number_of_first_bit_set (regs_available_for_popping);
12542
12543       /* Remove this register for the mask of available registers, so that
12544          the return address will not be corrupted by further pops.  */
12545       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12546     }
12547
12548   /* If we popped other registers then handle them here.  */
12549   if (regs_available_for_popping)
12550     {
12551       int frame_pointer;
12552       
12553       /* Work out which register currently contains the frame pointer.  */
12554       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12555
12556       /* Move it into the correct place.  */
12557       asm_fprintf (f, "\tmov\t%r, %r\n",
12558                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12559
12560       /* (Temporarily) remove it from the mask of popped registers.  */
12561       regs_available_for_popping &= ~(1 << frame_pointer);
12562       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12563       
12564       if (regs_available_for_popping)
12565         {
12566           int stack_pointer;
12567           
12568           /* We popped the stack pointer as well,
12569              find the register that contains it.  */
12570           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12571
12572           /* Move it into the stack register.  */
12573           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12574           
12575           /* At this point we have popped all necessary registers, so
12576              do not worry about restoring regs_available_for_popping
12577              to its correct value:
12578
12579              assert (pops_needed == 0)
12580              assert (regs_available_for_popping == (1 << frame_pointer))
12581              assert (regs_to_pop == (1 << STACK_POINTER))  */
12582         }
12583       else
12584         {
12585           /* Since we have just move the popped value into the frame
12586              pointer, the popping register is available for reuse, and
12587              we know that we still have the stack pointer left to pop.  */
12588           regs_available_for_popping |= (1 << frame_pointer);
12589         }
12590     }
12591   
12592   /* If we still have registers left on the stack, but we no longer have
12593      any registers into which we can pop them, then we must move the return
12594      address into the link register and make available the register that
12595      contained it.  */
12596   if (regs_available_for_popping == 0 && pops_needed > 0)
12597     {
12598       regs_available_for_popping |= 1 << reg_containing_return_addr;
12599       
12600       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12601                    reg_containing_return_addr);
12602       
12603       reg_containing_return_addr = LR_REGNUM;
12604     }
12605
12606   /* If we have registers left on the stack then pop some more.
12607      We know that at most we will want to pop FP and SP.  */
12608   if (pops_needed > 0)
12609     {
12610       int  popped_into;
12611       int  move_to;
12612       
12613       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12614                      regs_available_for_popping);
12615
12616       /* We have popped either FP or SP.
12617          Move whichever one it is into the correct register.  */
12618       popped_into = number_of_first_bit_set (regs_available_for_popping);
12619       move_to     = number_of_first_bit_set (regs_to_pop);
12620
12621       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12622
12623       regs_to_pop &= ~(1 << move_to);
12624
12625       --pops_needed;
12626     }
12627   
12628   /* If we still have not popped everything then we must have only
12629      had one register available to us and we are now popping the SP.  */
12630   if (pops_needed > 0)
12631     {
12632       int  popped_into;
12633       
12634       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12635                      regs_available_for_popping);
12636
12637       popped_into = number_of_first_bit_set (regs_available_for_popping);
12638
12639       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12640       /*
12641         assert (regs_to_pop == (1 << STACK_POINTER))
12642         assert (pops_needed == 1)
12643       */
12644     }
12645
12646   /* If necessary restore the a4 register.  */
12647   if (restore_a4)
12648     {
12649       if (reg_containing_return_addr != LR_REGNUM)
12650         {
12651           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12652           reg_containing_return_addr = LR_REGNUM;
12653         }
12654     
12655       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12656     }
12657
12658   if (eh_ofs)
12659     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
12660
12661   /* Return to caller.  */
12662   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12663 }
12664
12665 /* Emit code to push or pop registers to or from the stack.  F is the
12666    assembly file.  MASK is the registers to push or pop.  PUSH is
12667    nonzero if we should push, and zero if we should pop.  For debugging
12668    output, if pushing, adjust CFA_OFFSET by the amount of space added
12669    to the stack.  REAL_REGS should have the same number of bits set as
12670    MASK, and will be used instead (in the same order) to describe which
12671    registers were saved - this is used to mark the save slots when we
12672    push high registers after moving them to low registers.  */
12673 static void
12674 thumb_pushpop (FILE *f, int mask, int push, int *cfa_offset, int real_regs)
12675 {
12676   int regno;
12677   int lo_mask = mask & 0xFF;
12678   int pushed_words = 0;
12679
12680   if (lo_mask == 0 && !push && (mask & (1 << 15)))
12681     {
12682       /* Special case.  Do not generate a POP PC statement here, do it in
12683          thumb_exit() */
12684       thumb_exit (f, -1, NULL_RTX);
12685       return;
12686     }
12687       
12688   fprintf (f, "\t%s\t{", push ? "push" : "pop");
12689
12690   /* Look at the low registers first.  */
12691   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12692     {
12693       if (lo_mask & 1)
12694         {
12695           asm_fprintf (f, "%r", regno);
12696           
12697           if ((lo_mask & ~1) != 0)
12698             fprintf (f, ", ");
12699
12700           pushed_words++;
12701         }
12702     }
12703   
12704   if (push && (mask & (1 << LR_REGNUM)))
12705     {
12706       /* Catch pushing the LR.  */
12707       if (mask & 0xFF)
12708         fprintf (f, ", ");
12709       
12710       asm_fprintf (f, "%r", LR_REGNUM);
12711
12712       pushed_words++;
12713     }
12714   else if (!push && (mask & (1 << PC_REGNUM)))
12715     {
12716       /* Catch popping the PC.  */
12717       if (TARGET_INTERWORK || TARGET_BACKTRACE)
12718         {
12719           /* The PC is never poped directly, instead
12720              it is popped into r3 and then BX is used.  */
12721           fprintf (f, "}\n");
12722
12723           thumb_exit (f, -1, NULL_RTX);
12724
12725           return;
12726         }
12727       else
12728         {
12729           if (mask & 0xFF)
12730             fprintf (f, ", ");
12731           
12732           asm_fprintf (f, "%r", PC_REGNUM);
12733         }
12734     }
12735        
12736   fprintf (f, "}\n");
12737
12738   if (push && pushed_words && dwarf2out_do_frame ())
12739     {
12740       char *l = dwarf2out_cfi_label ();
12741       int pushed_mask = real_regs;
12742
12743       *cfa_offset += pushed_words * 4;
12744       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12745
12746       pushed_words = 0;
12747       pushed_mask = real_regs;
12748       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12749         {
12750           if (pushed_mask & 1)
12751             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12752         }
12753     }
12754 }
12755 \f
12756 void
12757 thumb_final_prescan_insn (rtx insn)
12758 {
12759   if (flag_print_asm_name)
12760     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12761                  INSN_ADDRESSES (INSN_UID (insn)));
12762 }
12763
12764 int
12765 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12766 {
12767   unsigned HOST_WIDE_INT mask = 0xff;
12768   int i;
12769
12770   if (val == 0) /* XXX */
12771     return 0;
12772   
12773   for (i = 0; i < 25; i++)
12774     if ((val & (mask << i)) == val)
12775       return 1;
12776
12777   return 0;
12778 }
12779
12780 /* Returns nonzero if the current function contains,
12781    or might contain a far jump.  */
12782 static int
12783 thumb_far_jump_used_p (void)
12784 {
12785   rtx insn;
12786
12787   /* This test is only important for leaf functions.  */
12788   /* assert (!leaf_function_p ()); */
12789   
12790   /* If we have already decided that far jumps may be used,
12791      do not bother checking again, and always return true even if
12792      it turns out that they are not being used.  Once we have made
12793      the decision that far jumps are present (and that hence the link
12794      register will be pushed onto the stack) we cannot go back on it.  */
12795   if (cfun->machine->far_jump_used)
12796     return 1;
12797
12798   /* If this function is not being called from the prologue/epilogue
12799      generation code then it must be being called from the
12800      INITIAL_ELIMINATION_OFFSET macro.  */
12801   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12802     {
12803       /* In this case we know that we are being asked about the elimination
12804          of the arg pointer register.  If that register is not being used,
12805          then there are no arguments on the stack, and we do not have to
12806          worry that a far jump might force the prologue to push the link
12807          register, changing the stack offsets.  In this case we can just
12808          return false, since the presence of far jumps in the function will
12809          not affect stack offsets.
12810
12811          If the arg pointer is live (or if it was live, but has now been
12812          eliminated and so set to dead) then we do have to test to see if
12813          the function might contain a far jump.  This test can lead to some
12814          false negatives, since before reload is completed, then length of
12815          branch instructions is not known, so gcc defaults to returning their
12816          longest length, which in turn sets the far jump attribute to true.
12817
12818          A false negative will not result in bad code being generated, but it
12819          will result in a needless push and pop of the link register.  We
12820          hope that this does not occur too often.
12821
12822          If we need doubleword stack alignment this could affect the other
12823          elimination offsets so we can't risk getting it wrong.  */
12824       if (regs_ever_live [ARG_POINTER_REGNUM])
12825         cfun->machine->arg_pointer_live = 1;
12826       else if (!cfun->machine->arg_pointer_live)
12827         return 0;
12828     }
12829
12830   /* Check to see if the function contains a branch
12831      insn with the far jump attribute set.  */
12832   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12833     {
12834       if (GET_CODE (insn) == JUMP_INSN
12835           /* Ignore tablejump patterns.  */
12836           && GET_CODE (PATTERN (insn)) != ADDR_VEC
12837           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12838           && get_attr_far_jump (insn) == FAR_JUMP_YES
12839           )
12840         {
12841           /* Record the fact that we have decided that
12842              the function does use far jumps.  */
12843           cfun->machine->far_jump_used = 1;
12844           return 1;
12845         }
12846     }
12847   
12848   return 0;
12849 }
12850
12851 /* Return nonzero if FUNC must be entered in ARM mode.  */
12852 int
12853 is_called_in_ARM_mode (tree func)
12854 {
12855   if (TREE_CODE (func) != FUNCTION_DECL)
12856     abort ();
12857
12858   /* Ignore the problem about functions whoes address is taken.  */
12859   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12860     return TRUE;
12861
12862 #ifdef ARM_PE 
12863   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12864 #else
12865   return FALSE;
12866 #endif
12867 }
12868
12869 /* The bits which aren't usefully expanded as rtl.  */
12870 const char *
12871 thumb_unexpanded_epilogue (void)
12872 {
12873   int regno;
12874   int live_regs_mask = 0;
12875   int high_regs_pushed = 0;
12876   int leaf_function = leaf_function_p ();
12877   int had_to_push_lr;
12878   rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
12879
12880   if (return_used_this_function)
12881     return "";
12882
12883   if (IS_NAKED (arm_current_func_type ()))
12884     return "";
12885
12886   for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12887     if (THUMB_REG_PUSHED_P (regno))
12888       live_regs_mask |= 1 << regno;
12889
12890   for (regno = 8; regno < 13; regno++)
12891     if (THUMB_REG_PUSHED_P (regno))
12892       high_regs_pushed++;
12893
12894   /* The prolog may have pushed some high registers to use as
12895      work registers.  eg the testsuite file:
12896      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12897      compiles to produce:
12898         push    {r4, r5, r6, r7, lr}
12899         mov     r7, r9
12900         mov     r6, r8
12901         push    {r6, r7}
12902      as part of the prolog.  We have to undo that pushing here.  */
12903   
12904   if (high_regs_pushed)
12905     {
12906       int mask = live_regs_mask;
12907       int next_hi_reg;
12908       int size;
12909       int mode;
12910        
12911 #ifdef RTX_CODE
12912       /* If we can deduce the registers used from the function's return value.
12913          This is more reliable that examining regs_ever_live[] because that
12914          will be set if the register is ever used in the function, not just if
12915          the register is used to hold a return value.  */
12916
12917       if (current_function_return_rtx != 0)
12918         mode = GET_MODE (current_function_return_rtx);
12919       else
12920 #endif
12921         mode = DECL_MODE (DECL_RESULT (current_function_decl));
12922
12923       size = GET_MODE_SIZE (mode);
12924
12925       /* Unless we are returning a type of size > 12 register r3 is
12926          available.  */
12927       if (size < 13)
12928         mask |=  1 << 3;
12929
12930       if (mask == 0)
12931         /* Oh dear!  We have no low registers into which we can pop
12932            high registers!  */
12933         internal_error
12934           ("no low registers available for popping high registers");
12935       
12936       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12937         if (THUMB_REG_PUSHED_P (next_hi_reg))
12938           break;
12939
12940       while (high_regs_pushed)
12941         {
12942           /* Find lo register(s) into which the high register(s) can
12943              be popped.  */
12944           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12945             {
12946               if (mask & (1 << regno))
12947                 high_regs_pushed--;
12948               if (high_regs_pushed == 0)
12949                 break;
12950             }
12951
12952           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
12953
12954           /* Pop the values into the low register(s).  */
12955           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
12956
12957           /* Move the value(s) into the high registers.  */
12958           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12959             {
12960               if (mask & (1 << regno))
12961                 {
12962                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12963                                regno);
12964                   
12965                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
12966                     if (THUMB_REG_PUSHED_P (next_hi_reg))
12967                       break;
12968                 }
12969             }
12970         }
12971     }
12972
12973   had_to_push_lr = (live_regs_mask || !leaf_function
12974                     || thumb_far_jump_used_p ());
12975   
12976   if (TARGET_BACKTRACE
12977       && ((live_regs_mask & 0xFF) == 0)
12978       && regs_ever_live [LAST_ARG_REGNUM] != 0)
12979     {
12980       /* The stack backtrace structure creation code had to
12981          push R7 in order to get a work register, so we pop
12982          it now.  */
12983       live_regs_mask |= (1 << LAST_LO_REGNUM);
12984     }
12985   
12986   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12987     {
12988       if (had_to_push_lr
12989           && !is_called_in_ARM_mode (current_function_decl)
12990           && !eh_ofs)
12991         live_regs_mask |= 1 << PC_REGNUM;
12992
12993       /* Either no argument registers were pushed or a backtrace
12994          structure was created which includes an adjusted stack
12995          pointer, so just pop everything.  */
12996       if (live_regs_mask)
12997         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12998                        live_regs_mask);
12999       
13000       if (eh_ofs)
13001         thumb_exit (asm_out_file, 2, eh_ofs);
13002       /* We have either just popped the return address into the
13003          PC or it is was kept in LR for the entire function or
13004          it is still on the stack because we do not want to
13005          return by doing a pop {pc}.  */
13006       else if ((live_regs_mask & (1 << PC_REGNUM)) == 0)
13007         thumb_exit (asm_out_file,
13008                     (had_to_push_lr
13009                      && is_called_in_ARM_mode (current_function_decl)) ?
13010                     -1 : LR_REGNUM, NULL_RTX);
13011     }
13012   else
13013     {
13014       /* Pop everything but the return address.  */
13015       live_regs_mask &= ~(1 << PC_REGNUM);
13016       
13017       if (live_regs_mask)
13018         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13019                        live_regs_mask);
13020
13021       if (had_to_push_lr)
13022         /* Get the return address into a temporary register.  */
13023         thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
13024                        1 << LAST_ARG_REGNUM);
13025       
13026       /* Remove the argument registers that were pushed onto the stack.  */
13027       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13028                    SP_REGNUM, SP_REGNUM,
13029                    current_function_pretend_args_size);
13030       
13031       if (eh_ofs)
13032         thumb_exit (asm_out_file, 2, eh_ofs);
13033       else
13034         thumb_exit (asm_out_file,
13035                     had_to_push_lr ? LAST_ARG_REGNUM : LR_REGNUM, NULL_RTX);
13036     }
13037
13038   return "";
13039 }
13040
13041 /* Functions to save and restore machine-specific function data.  */
13042 static struct machine_function *
13043 arm_init_machine_status (void)
13044 {
13045   struct machine_function *machine;
13046   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13047
13048 #if ARM_FT_UNKNOWN != 0  
13049   machine->func_type = ARM_FT_UNKNOWN;
13050 #endif
13051   return machine;
13052 }
13053
13054 /* Return an RTX indicating where the return address to the
13055    calling function can be found.  */
13056 rtx
13057 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13058 {
13059   if (count != 0)
13060     return NULL_RTX;
13061
13062   if (TARGET_APCS_32)
13063     return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13064   else
13065     {
13066       rtx lr = gen_rtx_AND (Pmode, gen_rtx_REG (Pmode, LR_REGNUM),
13067                             GEN_INT (RETURN_ADDR_MASK26));
13068       return get_func_hard_reg_initial_val (cfun, lr);
13069     }
13070 }
13071
13072 /* Do anything needed before RTL is emitted for each function.  */
13073 void
13074 arm_init_expanders (void)
13075 {
13076   /* Arrange to initialize and mark the machine per-function status.  */
13077   init_machine_status = arm_init_machine_status;
13078 }
13079
13080
13081 /* Like arm_compute_initial_elimination offset.  Simpler because
13082    THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer.  */
13083
13084 HOST_WIDE_INT
13085 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13086 {
13087   arm_stack_offsets *offsets;
13088
13089   offsets = arm_get_frame_offsets ();
13090
13091   switch (from)
13092     {
13093     case ARG_POINTER_REGNUM:
13094       switch (to)
13095         {
13096         case STACK_POINTER_REGNUM:
13097           return offsets->outgoing_args - offsets->saved_args;
13098
13099         case FRAME_POINTER_REGNUM:
13100           return offsets->soft_frame - offsets->saved_args;
13101
13102         case THUMB_HARD_FRAME_POINTER_REGNUM:
13103         case ARM_HARD_FRAME_POINTER_REGNUM:
13104           return offsets->saved_regs - offsets->saved_args;
13105
13106         default:
13107           abort();
13108         }
13109       break;
13110
13111     case FRAME_POINTER_REGNUM:
13112       switch (to)
13113         {
13114         case STACK_POINTER_REGNUM:
13115           return offsets->outgoing_args - offsets->soft_frame;
13116
13117         case THUMB_HARD_FRAME_POINTER_REGNUM:
13118         case ARM_HARD_FRAME_POINTER_REGNUM:
13119           return offsets->saved_regs - offsets->soft_frame;
13120
13121         default:
13122           abort();
13123         }
13124       break;
13125
13126     default:
13127       abort ();
13128     }
13129 }
13130
13131
13132 /* Generate the rest of a function's prologue.  */
13133 void
13134 thumb_expand_prologue (void)
13135 {
13136   rtx insn, dwarf;
13137
13138   HOST_WIDE_INT amount;
13139   arm_stack_offsets *offsets;
13140   unsigned long func_type;
13141
13142   func_type = arm_current_func_type ();
13143   
13144   /* Naked functions don't have prologues.  */
13145   if (IS_NAKED (func_type))
13146     return;
13147
13148   if (IS_INTERRUPT (func_type))
13149     {
13150       error ("interrupt Service Routines cannot be coded in Thumb mode");
13151       return;
13152     }
13153
13154   offsets = arm_get_frame_offsets ();
13155
13156   if (frame_pointer_needed)
13157     {
13158       insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
13159                                    stack_pointer_rtx));
13160       RTX_FRAME_RELATED_P (insn) = 1;
13161     }
13162
13163   amount = offsets->outgoing_args - offsets->saved_regs;
13164   if (amount)
13165     {
13166       if (amount < 512)
13167         {
13168           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13169                                         GEN_INT (- amount)));
13170           RTX_FRAME_RELATED_P (insn) = 1;
13171         }
13172       else
13173         {
13174           int regno;
13175           rtx reg;
13176
13177           /* The stack decrement is too big for an immediate value in a single
13178              insn.  In theory we could issue multiple subtracts, but after
13179              three of them it becomes more space efficient to place the full
13180              value in the constant pool and load into a register.  (Also the
13181              ARM debugger really likes to see only one stack decrement per
13182              function).  So instead we look for a scratch register into which
13183              we can load the decrement, and then we subtract this from the
13184              stack pointer.  Unfortunately on the thumb the only available
13185              scratch registers are the argument registers, and we cannot use
13186              these as they may hold arguments to the function.  Instead we
13187              attempt to locate a call preserved register which is used by this
13188              function.  If we can find one, then we know that it will have
13189              been pushed at the start of the prologue and so we can corrupt
13190              it now.  */
13191           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13192             if (THUMB_REG_PUSHED_P (regno)
13193                 && !(frame_pointer_needed
13194                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13195               break;
13196
13197           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
13198             {
13199               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13200
13201               /* Choose an arbitrary, non-argument low register.  */
13202               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13203
13204               /* Save it by copying it into a high, scratch register.  */
13205               emit_insn (gen_movsi (spare, reg));
13206               /* Add a USE to stop propagate_one_insn() from barfing.  */
13207               emit_insn (gen_prologue_use (spare));
13208
13209               /* Decrement the stack.  */
13210               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13211               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13212                                             stack_pointer_rtx, reg));
13213               RTX_FRAME_RELATED_P (insn) = 1;
13214               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13215                                    plus_constant (stack_pointer_rtx,
13216                                                   GEN_INT (- amount)));
13217               RTX_FRAME_RELATED_P (dwarf) = 1;
13218               REG_NOTES (insn)
13219                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13220                                      REG_NOTES (insn));
13221
13222               /* Restore the low register's original value.  */
13223               emit_insn (gen_movsi (reg, spare));
13224               
13225               /* Emit a USE of the restored scratch register, so that flow
13226                  analysis will not consider the restore redundant.  The
13227                  register won't be used again in this function and isn't
13228                  restored by the epilogue.  */
13229               emit_insn (gen_prologue_use (reg));
13230             }
13231           else
13232             {
13233               reg = gen_rtx_REG (SImode, regno);
13234
13235               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13236
13237               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13238                                             stack_pointer_rtx, reg));
13239               RTX_FRAME_RELATED_P (insn) = 1;
13240               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13241                                    plus_constant (stack_pointer_rtx,
13242                                                   GEN_INT (- amount)));
13243               RTX_FRAME_RELATED_P (dwarf) = 1;
13244               REG_NOTES (insn)
13245                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13246                                      REG_NOTES (insn));
13247             }
13248         }
13249       /* If the frame pointer is needed, emit a special barrier that
13250          will prevent the scheduler from moving stores to the frame
13251          before the stack adjustment.  */
13252       if (frame_pointer_needed)
13253         emit_insn (gen_stack_tie (stack_pointer_rtx,
13254                                   hard_frame_pointer_rtx));
13255     }
13256   
13257   if (current_function_profile || TARGET_NO_SCHED_PRO)
13258     emit_insn (gen_blockage ());
13259 }
13260
13261 void
13262 thumb_expand_epilogue (void)
13263 {
13264   HOST_WIDE_INT amount;
13265   arm_stack_offsets *offsets;
13266   int regno;
13267
13268   /* Naked functions don't have prologues.  */
13269   if (IS_NAKED (arm_current_func_type ()))
13270     return;
13271
13272   offsets = arm_get_frame_offsets ();
13273   amount = offsets->outgoing_args - offsets->saved_regs;
13274
13275   if (frame_pointer_needed)
13276     emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13277   else if (amount)
13278     {
13279       if (amount < 512)
13280         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13281                                GEN_INT (amount)));
13282       else
13283         {
13284           /* r3 is always free in the epilogue.  */
13285           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13286
13287           emit_insn (gen_movsi (reg, GEN_INT (amount)));
13288           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13289         }
13290     }
13291       
13292   /* Emit a USE (stack_pointer_rtx), so that
13293      the stack adjustment will not be deleted.  */
13294   emit_insn (gen_prologue_use (stack_pointer_rtx));
13295
13296   if (current_function_profile || TARGET_NO_SCHED_PRO)
13297     emit_insn (gen_blockage ());
13298
13299   /* Emit a clobber for each insn that will be restored in the epilogue,
13300      so that flow2 will get register lifetimes correct.  */
13301   for (regno = 0; regno < 13; regno++)
13302     if (regs_ever_live[regno] && !call_used_regs[regno])
13303       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13304
13305   if (! regs_ever_live[LR_REGNUM])
13306     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13307 }
13308
13309 static void
13310 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13311 {
13312   int live_regs_mask = 0;
13313   int high_regs_pushed = 0;
13314   int cfa_offset = 0;
13315   int regno;
13316
13317   if (IS_NAKED (arm_current_func_type ()))
13318     return;
13319
13320   if (is_called_in_ARM_mode (current_function_decl))
13321     {
13322       const char * name;
13323
13324       if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
13325         abort ();
13326       if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
13327         abort ();
13328       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
13329       
13330       /* Generate code sequence to switch us into Thumb mode.  */
13331       /* The .code 32 directive has already been emitted by
13332          ASM_DECLARE_FUNCTION_NAME.  */
13333       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13334       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13335
13336       /* Generate a label, so that the debugger will notice the
13337          change in instruction sets.  This label is also used by
13338          the assembler to bypass the ARM code when this function
13339          is called from a Thumb encoded function elsewhere in the
13340          same file.  Hence the definition of STUB_NAME here must
13341          agree with the definition in gas/config/tc-arm.c.  */
13342       
13343 #define STUB_NAME ".real_start_of"
13344       
13345       fprintf (f, "\t.code\t16\n");
13346 #ifdef ARM_PE
13347       if (arm_dllexport_name_p (name))
13348         name = arm_strip_name_encoding (name);
13349 #endif        
13350       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13351       fprintf (f, "\t.thumb_func\n");
13352       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13353     }
13354     
13355   if (current_function_pretend_args_size)
13356     {
13357       if (cfun->machine->uses_anonymous_args)
13358         {
13359           int num_pushes;
13360           
13361           fprintf (f, "\tpush\t{");
13362
13363           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13364           
13365           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13366                regno <= LAST_ARG_REGNUM;
13367                regno++)
13368             asm_fprintf (f, "%r%s", regno,
13369                          regno == LAST_ARG_REGNUM ? "" : ", ");
13370
13371           fprintf (f, "}\n");
13372         }
13373       else
13374         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", 
13375                      SP_REGNUM, SP_REGNUM,
13376                      current_function_pretend_args_size);
13377
13378       /* We don't need to record the stores for unwinding (would it
13379          help the debugger any if we did?), but record the change in
13380          the stack pointer.  */
13381       if (dwarf2out_do_frame ())
13382         {
13383           char *l = dwarf2out_cfi_label ();
13384           cfa_offset = cfa_offset + current_function_pretend_args_size;
13385           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13386         }
13387     }
13388
13389   for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13390     if (THUMB_REG_PUSHED_P (regno))
13391       live_regs_mask |= 1 << regno;
13392
13393   if (live_regs_mask || !leaf_function_p () || thumb_far_jump_used_p ())
13394     live_regs_mask |= 1 << LR_REGNUM;
13395
13396   if (TARGET_BACKTRACE)
13397     {
13398       int    offset;
13399       int    work_register = 0;
13400       int    wr;
13401       
13402       /* We have been asked to create a stack backtrace structure.
13403          The code looks like this:
13404          
13405          0   .align 2
13406          0   func:
13407          0     sub   SP, #16         Reserve space for 4 registers.
13408          2     push  {R7}            Get a work register.
13409          4     add   R7, SP, #20     Get the stack pointer before the push.
13410          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
13411          8     mov   R7, PC          Get hold of the start of this code plus 12.
13412         10     str   R7, [SP, #16]   Store it.
13413         12     mov   R7, FP          Get hold of the current frame pointer.
13414         14     str   R7, [SP, #4]    Store it.
13415         16     mov   R7, LR          Get hold of the current return address.
13416         18     str   R7, [SP, #12]   Store it.
13417         20     add   R7, SP, #16     Point at the start of the backtrace structure.
13418         22     mov   FP, R7          Put this value into the frame pointer.  */
13419
13420       if ((live_regs_mask & 0xFF) == 0)
13421         {
13422           /* See if the a4 register is free.  */
13423
13424           if (regs_ever_live [LAST_ARG_REGNUM] == 0)
13425             work_register = LAST_ARG_REGNUM;
13426           else    /* We must push a register of our own.  */
13427             live_regs_mask |= (1 << LAST_LO_REGNUM);
13428         }
13429
13430       if (work_register == 0)
13431         {
13432           /* Select a register from the list that will be pushed to
13433              use as our work register.  */
13434           for (work_register = (LAST_LO_REGNUM + 1); work_register--;)
13435             if ((1 << work_register) & live_regs_mask)
13436               break;
13437         }
13438       
13439       asm_fprintf
13440         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13441          SP_REGNUM, SP_REGNUM);
13442
13443       if (dwarf2out_do_frame ())
13444         {
13445           char *l = dwarf2out_cfi_label ();
13446           cfa_offset = cfa_offset + 16;
13447           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13448         }
13449
13450       if (live_regs_mask)
13451         thumb_pushpop (f, live_regs_mask, 1, &cfa_offset, live_regs_mask);
13452       
13453       for (offset = 0, wr = 1 << 15; wr != 0; wr >>= 1)
13454         if (wr & live_regs_mask)
13455           offset += 4;
13456       
13457       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13458                    offset + 16 + current_function_pretend_args_size);
13459       
13460       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13461                    offset + 4);
13462
13463       /* Make sure that the instruction fetching the PC is in the right place
13464          to calculate "start of backtrace creation code + 12".  */
13465       if (live_regs_mask)
13466         {
13467           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13468           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13469                        offset + 12);
13470           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13471                        ARM_HARD_FRAME_POINTER_REGNUM);
13472           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13473                        offset);
13474         }
13475       else
13476         {
13477           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13478                        ARM_HARD_FRAME_POINTER_REGNUM);
13479           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13480                        offset);
13481           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13482           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13483                        offset + 12);
13484         }
13485       
13486       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13487       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13488                    offset + 8);
13489       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13490                    offset + 12);
13491       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13492                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13493     }
13494   else if (live_regs_mask)
13495     thumb_pushpop (f, live_regs_mask, 1, &cfa_offset, live_regs_mask);
13496
13497   for (regno = 8; regno < 13; regno++)
13498     if (THUMB_REG_PUSHED_P (regno))
13499       high_regs_pushed++;
13500
13501   if (high_regs_pushed)
13502     {
13503       int pushable_regs = 0;
13504       int mask = live_regs_mask & 0xff;
13505       int next_hi_reg;
13506
13507       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13508         if (THUMB_REG_PUSHED_P (next_hi_reg))
13509           break;
13510
13511       pushable_regs = mask;
13512
13513       if (pushable_regs == 0)
13514         {
13515           /* Desperation time -- this probably will never happen.  */
13516           if (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM))
13517             asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, LAST_ARG_REGNUM);
13518           mask = 1 << LAST_ARG_REGNUM;
13519         }
13520
13521       while (high_regs_pushed > 0)
13522         {
13523           int real_regs_mask = 0;
13524
13525           for (regno = LAST_LO_REGNUM; regno >= 0; regno--)
13526             {
13527               if (mask & (1 << regno))
13528                 {
13529                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13530                   
13531                   high_regs_pushed--;
13532                   real_regs_mask |= (1 << next_hi_reg);
13533                   
13534                   if (high_regs_pushed)
13535                     {
13536                       for (next_hi_reg--; next_hi_reg > LAST_LO_REGNUM;
13537                            next_hi_reg--)
13538                         if (THUMB_REG_PUSHED_P (next_hi_reg))
13539                           break;
13540                     }
13541                   else
13542                     {
13543                       mask &= ~((1 << regno) - 1);
13544                       break;
13545                     }
13546                 }
13547             }
13548
13549           thumb_pushpop (f, mask, 1, &cfa_offset, real_regs_mask);
13550         }
13551
13552       if (pushable_regs == 0
13553           && (THUMB_REG_PUSHED_P (LAST_ARG_REGNUM)))
13554         asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
13555     }
13556 }
13557
13558 /* Handle the case of a double word load into a low register from
13559    a computed memory address.  The computed address may involve a
13560    register which is overwritten by the load.  */
13561 const char *
13562 thumb_load_double_from_address (rtx *operands)
13563 {
13564   rtx addr;
13565   rtx base;
13566   rtx offset;
13567   rtx arg1;
13568   rtx arg2;
13569   
13570   if (GET_CODE (operands[0]) != REG)
13571     abort ();
13572   
13573   if (GET_CODE (operands[1]) != MEM)
13574     abort ();
13575
13576   /* Get the memory address.  */
13577   addr = XEXP (operands[1], 0);
13578       
13579   /* Work out how the memory address is computed.  */
13580   switch (GET_CODE (addr))
13581     {
13582     case REG:
13583       operands[2] = gen_rtx_MEM (SImode,
13584                                  plus_constant (XEXP (operands[1], 0), 4));
13585
13586       if (REGNO (operands[0]) == REGNO (addr))
13587         {
13588           output_asm_insn ("ldr\t%H0, %2", operands);
13589           output_asm_insn ("ldr\t%0, %1", operands);
13590         }
13591       else
13592         {
13593           output_asm_insn ("ldr\t%0, %1", operands);
13594           output_asm_insn ("ldr\t%H0, %2", operands);
13595         }
13596       break;
13597       
13598     case CONST:
13599       /* Compute <address> + 4 for the high order load.  */
13600       operands[2] = gen_rtx_MEM (SImode,
13601                                  plus_constant (XEXP (operands[1], 0), 4));
13602       
13603       output_asm_insn ("ldr\t%0, %1", operands);
13604       output_asm_insn ("ldr\t%H0, %2", operands);
13605       break;
13606           
13607     case PLUS:
13608       arg1   = XEXP (addr, 0);
13609       arg2   = XEXP (addr, 1);
13610             
13611       if (CONSTANT_P (arg1))
13612         base = arg2, offset = arg1;
13613       else
13614         base = arg1, offset = arg2;
13615   
13616       if (GET_CODE (base) != REG)
13617         abort ();
13618
13619       /* Catch the case of <address> = <reg> + <reg> */
13620       if (GET_CODE (offset) == REG)
13621         {
13622           int reg_offset = REGNO (offset);
13623           int reg_base   = REGNO (base);
13624           int reg_dest   = REGNO (operands[0]);
13625           
13626           /* Add the base and offset registers together into the
13627              higher destination register.  */
13628           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13629                        reg_dest + 1, reg_base, reg_offset);
13630           
13631           /* Load the lower destination register from the address in
13632              the higher destination register.  */
13633           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13634                        reg_dest, reg_dest + 1);
13635           
13636           /* Load the higher destination register from its own address
13637              plus 4.  */
13638           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13639                        reg_dest + 1, reg_dest + 1);
13640         }
13641       else
13642         {
13643           /* Compute <address> + 4 for the high order load.  */
13644           operands[2] = gen_rtx_MEM (SImode,
13645                                      plus_constant (XEXP (operands[1], 0), 4));
13646           
13647           /* If the computed address is held in the low order register
13648              then load the high order register first, otherwise always
13649              load the low order register first.  */
13650           if (REGNO (operands[0]) == REGNO (base))
13651             {
13652               output_asm_insn ("ldr\t%H0, %2", operands);
13653               output_asm_insn ("ldr\t%0, %1", operands);
13654             }
13655           else
13656             {
13657               output_asm_insn ("ldr\t%0, %1", operands);
13658               output_asm_insn ("ldr\t%H0, %2", operands);
13659             }
13660         }
13661       break;
13662
13663     case LABEL_REF:
13664       /* With no registers to worry about we can just load the value
13665          directly.  */
13666       operands[2] = gen_rtx_MEM (SImode,
13667                                  plus_constant (XEXP (operands[1], 0), 4));
13668           
13669       output_asm_insn ("ldr\t%H0, %2", operands);
13670       output_asm_insn ("ldr\t%0, %1", operands);
13671       break;
13672       
13673     default:
13674       abort ();
13675       break;
13676     }
13677   
13678   return "";
13679 }
13680
13681 const char *
13682 thumb_output_move_mem_multiple (int n, rtx *operands)
13683 {
13684   rtx tmp;
13685
13686   switch (n)
13687     {
13688     case 2:
13689       if (REGNO (operands[4]) > REGNO (operands[5]))
13690         {
13691           tmp = operands[4];
13692           operands[4] = operands[5];
13693           operands[5] = tmp;
13694         }
13695       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13696       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13697       break;
13698
13699     case 3:
13700       if (REGNO (operands[4]) > REGNO (operands[5]))
13701         {
13702           tmp = operands[4];
13703           operands[4] = operands[5];
13704           operands[5] = tmp;
13705         }
13706       if (REGNO (operands[5]) > REGNO (operands[6]))
13707         {
13708           tmp = operands[5];
13709           operands[5] = operands[6];
13710           operands[6] = tmp;
13711         }
13712       if (REGNO (operands[4]) > REGNO (operands[5]))
13713         {
13714           tmp = operands[4];
13715           operands[4] = operands[5];
13716           operands[5] = tmp;
13717         }
13718       
13719       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13720       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13721       break;
13722
13723     default:
13724       abort ();
13725     }
13726
13727   return "";
13728 }
13729
13730 /* Routines for generating rtl.  */
13731 void
13732 thumb_expand_movstrqi (rtx *operands)
13733 {
13734   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13735   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13736   HOST_WIDE_INT len = INTVAL (operands[2]);
13737   HOST_WIDE_INT offset = 0;
13738
13739   while (len >= 12)
13740     {
13741       emit_insn (gen_movmem12b (out, in, out, in));
13742       len -= 12;
13743     }
13744   
13745   if (len >= 8)
13746     {
13747       emit_insn (gen_movmem8b (out, in, out, in));
13748       len -= 8;
13749     }
13750   
13751   if (len >= 4)
13752     {
13753       rtx reg = gen_reg_rtx (SImode);
13754       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13755       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13756       len -= 4;
13757       offset += 4;
13758     }
13759   
13760   if (len >= 2)
13761     {
13762       rtx reg = gen_reg_rtx (HImode);
13763       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode, 
13764                                               plus_constant (in, offset))));
13765       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13766                             reg));
13767       len -= 2;
13768       offset += 2;
13769     }
13770   
13771   if (len)
13772     {
13773       rtx reg = gen_reg_rtx (QImode);
13774       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13775                                               plus_constant (in, offset))));
13776       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13777                             reg));
13778     }
13779 }
13780
13781 int
13782 thumb_cmp_operand (rtx op, enum machine_mode mode)
13783 {
13784   return ((GET_CODE (op) == CONST_INT
13785            && INTVAL (op) < 256
13786            && INTVAL (op) >= 0)
13787           || s_register_operand (op, mode));
13788 }
13789
13790 int
13791 thumb_cmpneg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
13792 {
13793   return (GET_CODE (op) == CONST_INT
13794           && INTVAL (op) < 0
13795           && INTVAL (op) > -256);
13796 }
13797
13798 /* Return TRUE if a result can be stored in OP without clobbering the
13799    condition code register.  Prior to reload we only accept a
13800    register.  After reload we have to be able to handle memory as
13801    well, since a pseudo may not get a hard reg and reload cannot
13802    handle output-reloads on jump insns.
13803
13804    We could possibly handle mem before reload as well, but that might
13805    complicate things with the need to handle increment
13806    side-effects.  */
13807
13808 int
13809 thumb_cbrch_target_operand (rtx op, enum machine_mode mode)
13810 {
13811   return (s_register_operand (op, mode)
13812           || ((reload_in_progress || reload_completed)
13813               && memory_operand (op, mode)));
13814 }
13815
13816 /* Handle storing a half-word to memory during reload.  */ 
13817 void
13818 thumb_reload_out_hi (rtx *operands)
13819 {
13820   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13821 }
13822
13823 /* Handle reading a half-word from memory during reload.  */ 
13824 void
13825 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13826 {
13827   abort ();
13828 }
13829
13830 /* Return the length of a function name prefix
13831     that starts with the character 'c'.  */
13832 static int
13833 arm_get_strip_length (int c)
13834 {
13835   switch (c)
13836     {
13837     ARM_NAME_ENCODING_LENGTHS
13838       default: return 0; 
13839     }
13840 }
13841
13842 /* Return a pointer to a function's name with any
13843    and all prefix encodings stripped from it.  */
13844 const char *
13845 arm_strip_name_encoding (const char *name)
13846 {
13847   int skip;
13848   
13849   while ((skip = arm_get_strip_length (* name)))
13850     name += skip;
13851
13852   return name;
13853 }
13854
13855 /* If there is a '*' anywhere in the name's prefix, then
13856    emit the stripped name verbatim, otherwise prepend an
13857    underscore if leading underscores are being used.  */
13858 void
13859 arm_asm_output_labelref (FILE *stream, const char *name)
13860 {
13861   int skip;
13862   int verbatim = 0;
13863
13864   while ((skip = arm_get_strip_length (* name)))
13865     {
13866       verbatim |= (*name == '*');
13867       name += skip;
13868     }
13869
13870   if (verbatim)
13871     fputs (name, stream);
13872   else
13873     asm_fprintf (stream, "%U%s", name);
13874 }
13875
13876 rtx aof_pic_label;
13877
13878 #ifdef AOF_ASSEMBLER
13879 /* Special functions only needed when producing AOF syntax assembler.  */
13880
13881 struct pic_chain
13882 {
13883   struct pic_chain * next;
13884   const char * symname;
13885 };
13886
13887 static struct pic_chain * aof_pic_chain = NULL;
13888
13889 rtx
13890 aof_pic_entry (rtx x)
13891 {
13892   struct pic_chain ** chainp;
13893   int offset;
13894
13895   if (aof_pic_label == NULL_RTX)
13896     {
13897       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13898     }
13899
13900   for (offset = 0, chainp = &aof_pic_chain; *chainp;
13901        offset += 4, chainp = &(*chainp)->next)
13902     if ((*chainp)->symname == XSTR (x, 0))
13903       return plus_constant (aof_pic_label, offset);
13904
13905   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13906   (*chainp)->next = NULL;
13907   (*chainp)->symname = XSTR (x, 0);
13908   return plus_constant (aof_pic_label, offset);
13909 }
13910
13911 void
13912 aof_dump_pic_table (FILE *f)
13913 {
13914   struct pic_chain * chain;
13915
13916   if (aof_pic_chain == NULL)
13917     return;
13918
13919   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13920                PIC_OFFSET_TABLE_REGNUM,
13921                PIC_OFFSET_TABLE_REGNUM);
13922   fputs ("|x$adcons|\n", f);
13923   
13924   for (chain = aof_pic_chain; chain; chain = chain->next)
13925     {
13926       fputs ("\tDCD\t", f);
13927       assemble_name (f, chain->symname);
13928       fputs ("\n", f);
13929     }
13930 }
13931
13932 int arm_text_section_count = 1;
13933
13934 char *
13935 aof_text_section (void )
13936 {
13937   static char buf[100];
13938   sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13939            arm_text_section_count++);
13940   if (flag_pic)
13941     strcat (buf, ", PIC, REENTRANT");
13942   return buf;
13943 }
13944
13945 static int arm_data_section_count = 1;
13946
13947 char *
13948 aof_data_section (void)
13949 {
13950   static char buf[100];
13951   sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13952   return buf;
13953 }
13954
13955 /* The AOF assembler is religiously strict about declarations of
13956    imported and exported symbols, so that it is impossible to declare
13957    a function as imported near the beginning of the file, and then to
13958    export it later on.  It is, however, possible to delay the decision
13959    until all the functions in the file have been compiled.  To get
13960    around this, we maintain a list of the imports and exports, and
13961    delete from it any that are subsequently defined.  At the end of
13962    compilation we spit the remainder of the list out before the END
13963    directive.  */
13964
13965 struct import
13966 {
13967   struct import * next;
13968   const char * name;
13969 };
13970
13971 static struct import * imports_list = NULL;
13972
13973 void
13974 aof_add_import (const char *name)
13975 {
13976   struct import * new;
13977
13978   for (new = imports_list; new; new = new->next)
13979     if (new->name == name)
13980       return;
13981
13982   new = (struct import *) xmalloc (sizeof (struct import));
13983   new->next = imports_list;
13984   imports_list = new;
13985   new->name = name;
13986 }
13987
13988 void
13989 aof_delete_import (const char *name)
13990 {
13991   struct import ** old;
13992
13993   for (old = &imports_list; *old; old = & (*old)->next)
13994     {
13995       if ((*old)->name == name)
13996         {
13997           *old = (*old)->next;
13998           return;
13999         }
14000     }
14001 }
14002
14003 int arm_main_function = 0;
14004
14005 static void
14006 aof_dump_imports (FILE *f)
14007 {
14008   /* The AOF assembler needs this to cause the startup code to be extracted
14009      from the library.  Brining in __main causes the whole thing to work
14010      automagically.  */
14011   if (arm_main_function)
14012     {
14013       text_section ();
14014       fputs ("\tIMPORT __main\n", f);
14015       fputs ("\tDCD __main\n", f);
14016     }
14017
14018   /* Now dump the remaining imports.  */
14019   while (imports_list)
14020     {
14021       fprintf (f, "\tIMPORT\t");
14022       assemble_name (f, imports_list->name);
14023       fputc ('\n', f);
14024       imports_list = imports_list->next;
14025     }
14026 }
14027
14028 static void
14029 aof_globalize_label (FILE *stream, const char *name)
14030 {
14031   default_globalize_label (stream, name);
14032   if (! strcmp (name, "main"))
14033     arm_main_function = 1;
14034 }
14035
14036 static void
14037 aof_file_start (void)
14038 {
14039   fputs ("__r0\tRN\t0\n", asm_out_file);
14040   fputs ("__a1\tRN\t0\n", asm_out_file);
14041   fputs ("__a2\tRN\t1\n", asm_out_file);
14042   fputs ("__a3\tRN\t2\n", asm_out_file);
14043   fputs ("__a4\tRN\t3\n", asm_out_file);
14044   fputs ("__v1\tRN\t4\n", asm_out_file);
14045   fputs ("__v2\tRN\t5\n", asm_out_file);
14046   fputs ("__v3\tRN\t6\n", asm_out_file);
14047   fputs ("__v4\tRN\t7\n", asm_out_file);
14048   fputs ("__v5\tRN\t8\n", asm_out_file);
14049   fputs ("__v6\tRN\t9\n", asm_out_file);
14050   fputs ("__sl\tRN\t10\n", asm_out_file);
14051   fputs ("__fp\tRN\t11\n", asm_out_file);
14052   fputs ("__ip\tRN\t12\n", asm_out_file);
14053   fputs ("__sp\tRN\t13\n", asm_out_file);
14054   fputs ("__lr\tRN\t14\n", asm_out_file);
14055   fputs ("__pc\tRN\t15\n", asm_out_file);
14056   fputs ("__f0\tFN\t0\n", asm_out_file);
14057   fputs ("__f1\tFN\t1\n", asm_out_file);
14058   fputs ("__f2\tFN\t2\n", asm_out_file);
14059   fputs ("__f3\tFN\t3\n", asm_out_file);
14060   fputs ("__f4\tFN\t4\n", asm_out_file);
14061   fputs ("__f5\tFN\t5\n", asm_out_file);
14062   fputs ("__f6\tFN\t6\n", asm_out_file);
14063   fputs ("__f7\tFN\t7\n", asm_out_file);
14064   text_section ();
14065 }
14066
14067 static void
14068 aof_file_end (void)
14069 {
14070   if (flag_pic)
14071     aof_dump_pic_table (asm_out_file);
14072   aof_dump_imports (asm_out_file);
14073   fputs ("\tEND\n", asm_out_file);
14074 }
14075 #endif /* AOF_ASSEMBLER */
14076
14077 #ifdef OBJECT_FORMAT_ELF
14078 /* Switch to an arbitrary section NAME with attributes as specified
14079    by FLAGS.  ALIGN specifies any known alignment requirements for
14080    the section; 0 if the default should be used.
14081
14082    Differs from the default elf version only in the prefix character
14083    used before the section type.  */
14084
14085 static void
14086 arm_elf_asm_named_section (const char *name, unsigned int flags)
14087 {
14088   char flagchars[10], *f = flagchars;
14089
14090   if (! named_section_first_declaration (name))
14091     {
14092       fprintf (asm_out_file, "\t.section\t%s\n", name);
14093       return;
14094     }
14095
14096   if (!(flags & SECTION_DEBUG))
14097     *f++ = 'a';
14098   if (flags & SECTION_WRITE)
14099     *f++ = 'w';
14100   if (flags & SECTION_CODE)
14101     *f++ = 'x';
14102   if (flags & SECTION_SMALL)
14103     *f++ = 's';
14104   if (flags & SECTION_MERGE)
14105     *f++ = 'M';
14106   if (flags & SECTION_STRINGS)
14107     *f++ = 'S';
14108   if (flags & SECTION_TLS)
14109     *f++ = 'T';
14110   *f = '\0';
14111
14112   fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
14113
14114   if (!(flags & SECTION_NOTYPE))
14115     {
14116       const char *type;
14117
14118       if (flags & SECTION_BSS)
14119         type = "nobits";
14120       else
14121         type = "progbits";
14122
14123       fprintf (asm_out_file, ",%%%s", type);
14124
14125       if (flags & SECTION_ENTSIZE)
14126         fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
14127     }
14128
14129   putc ('\n', asm_out_file);
14130 }
14131 #endif
14132
14133 #ifndef ARM_PE
14134 /* Symbols in the text segment can be accessed without indirecting via the
14135    constant pool; it may take an extra binary operation, but this is still
14136    faster than indirecting via memory.  Don't do this when not optimizing,
14137    since we won't be calculating al of the offsets necessary to do this
14138    simplification.  */
14139
14140 static void
14141 arm_encode_section_info (tree decl, rtx rtl, int first)
14142 {
14143   /* This doesn't work with AOF syntax, since the string table may be in
14144      a different AREA.  */
14145 #ifndef AOF_ASSEMBLER
14146   if (optimize > 0 && TREE_CONSTANT (decl))
14147     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14148 #endif
14149
14150   /* If we are referencing a function that is weak then encode a long call
14151      flag in the function name, otherwise if the function is static or
14152      or known to be defined in this file then encode a short call flag.  */
14153   if (first && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
14154     {
14155       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14156         arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14157       else if (! TREE_PUBLIC (decl))
14158         arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14159     }
14160 }
14161 #endif /* !ARM_PE */
14162
14163 static void
14164 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14165 {
14166   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14167       && !strcmp (prefix, "L"))
14168     {
14169       arm_ccfsm_state = 0;
14170       arm_target_insn = NULL;
14171     }
14172   default_internal_label (stream, prefix, labelno);
14173 }
14174
14175 /* Output code to add DELTA to the first argument, and then jump
14176    to FUNCTION.  Used for C++ multiple inheritance.  */
14177 static void
14178 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14179                      HOST_WIDE_INT delta,
14180                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14181                      tree function)
14182 {
14183   static int thunk_label = 0;
14184   char label[256];
14185   int mi_delta = delta;
14186   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14187   int shift = 0;
14188   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14189                     ? 1 : 0);
14190   if (mi_delta < 0)
14191     mi_delta = - mi_delta;
14192   if (TARGET_THUMB)
14193     {
14194       int labelno = thunk_label++;
14195       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14196       fputs ("\tldr\tr12, ", file);
14197       assemble_name (file, label);
14198       fputc ('\n', file);
14199     }
14200   while (mi_delta != 0)
14201     {
14202       if ((mi_delta & (3 << shift)) == 0)
14203         shift += 2;
14204       else
14205         {
14206           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14207                        mi_op, this_regno, this_regno,
14208                        mi_delta & (0xff << shift));
14209           mi_delta &= ~(0xff << shift);
14210           shift += 8;
14211         }
14212     }
14213   if (TARGET_THUMB)
14214     {
14215       fprintf (file, "\tbx\tr12\n");
14216       ASM_OUTPUT_ALIGN (file, 2);
14217       assemble_name (file, label);
14218       fputs (":\n", file);
14219       assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14220     }
14221   else
14222     {
14223       fputs ("\tb\t", file);
14224       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14225       if (NEED_PLT_RELOC)
14226         fputs ("(PLT)", file);
14227       fputc ('\n', file);
14228     }
14229 }
14230
14231 int
14232 arm_emit_vector_const (FILE *file, rtx x)
14233 {
14234   int i;
14235   const char * pattern;
14236
14237   if (GET_CODE (x) != CONST_VECTOR)
14238     abort ();
14239
14240   switch (GET_MODE (x))
14241     {
14242     case V2SImode: pattern = "%08x"; break;
14243     case V4HImode: pattern = "%04x"; break;
14244     case V8QImode: pattern = "%02x"; break;
14245     default:       abort ();
14246     }
14247
14248   fprintf (file, "0x");
14249   for (i = CONST_VECTOR_NUNITS (x); i--;)
14250     {
14251       rtx element;
14252
14253       element = CONST_VECTOR_ELT (x, i);
14254       fprintf (file, pattern, INTVAL (element));
14255     }
14256
14257   return 1;
14258 }
14259
14260 const char *
14261 arm_output_load_gr (rtx *operands)
14262 {
14263   rtx reg;
14264   rtx offset;
14265   rtx wcgr;
14266   rtx sum;
14267   
14268   if (GET_CODE (operands [1]) != MEM
14269       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14270       || GET_CODE (reg = XEXP (sum, 0)) != REG
14271       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14272       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14273     return "wldrw%?\t%0, %1";
14274   
14275   /* Fix up an out-of-range load of a GR register.  */  
14276   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14277   wcgr = operands[0];
14278   operands[0] = reg;
14279   output_asm_insn ("ldr%?\t%0, %1", operands);
14280
14281   operands[0] = wcgr;
14282   operands[1] = reg;
14283   output_asm_insn ("tmcr%?\t%0, %1", operands);
14284   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14285
14286   return "";
14287 }
14288
14289 static rtx
14290 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14291                       int incoming ATTRIBUTE_UNUSED)
14292 {
14293 #if 0
14294   /* FIXME: The ARM backend has special code to handle structure
14295          returns, and will reserve its own hidden first argument.  So
14296          if this macro is enabled a *second* hidden argument will be
14297          reserved, which will break binary compatibility with old
14298          toolchains and also thunk handling.  One day this should be
14299          fixed.  */
14300   return 0;
14301 #else
14302   /* Register in which address to store a structure value
14303      is passed to a function.  */
14304   return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14305 #endif
14306 }
14307
14308 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14309
14310    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14311    named arg and all anonymous args onto the stack.
14312    XXX I know the prologue shouldn't be pushing registers, but it is faster
14313    that way.  */
14314
14315 static void
14316 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14317                             enum machine_mode mode ATTRIBUTE_UNUSED,
14318                             tree type ATTRIBUTE_UNUSED,
14319                             int *pretend_size,
14320                             int second_time ATTRIBUTE_UNUSED)
14321 {
14322   cfun->machine->uses_anonymous_args = 1;
14323   if (cum->nregs < NUM_ARG_REGS)
14324     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14325 }
14326
14327 /* Return nonzero if the CONSUMER instruction (a store) does not need
14328    PRODUCER's value to calculate the address.  */
14329
14330 int
14331 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14332 {
14333   rtx value = PATTERN (producer);
14334   rtx addr = PATTERN (consumer);
14335
14336   if (GET_CODE (value) == COND_EXEC)
14337     value = COND_EXEC_CODE (value);
14338   if (GET_CODE (value) == PARALLEL)
14339     value = XVECEXP (value, 0, 0);
14340   value = XEXP (value, 0);
14341   if (GET_CODE (addr) == COND_EXEC)
14342     addr = COND_EXEC_CODE (addr);
14343   if (GET_CODE (addr) == PARALLEL)
14344     addr = XVECEXP (addr, 0, 0);
14345   addr = XEXP (addr, 0);
14346   
14347   return !reg_overlap_mentioned_p (value, addr);
14348 }
14349
14350 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14351    have an early register shift value or amount dependency on the
14352    result of PRODUCER.  */
14353
14354 int
14355 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14356 {
14357   rtx value = PATTERN (producer);
14358   rtx op = PATTERN (consumer);
14359   rtx early_op;
14360
14361   if (GET_CODE (value) == COND_EXEC)
14362     value = COND_EXEC_CODE (value);
14363   if (GET_CODE (value) == PARALLEL)
14364     value = XVECEXP (value, 0, 0);
14365   value = XEXP (value, 0);
14366   if (GET_CODE (op) == COND_EXEC)
14367     op = COND_EXEC_CODE (op);
14368   if (GET_CODE (op) == PARALLEL)
14369     op = XVECEXP (op, 0, 0);
14370   op = XEXP (op, 1);
14371   
14372   early_op = XEXP (op, 0);
14373   /* This is either an actual independent shift, or a shift applied to
14374      the first operand of another operation.  We want the whole shift
14375      operation.  */
14376   if (GET_CODE (early_op) == REG)
14377     early_op = op;
14378
14379   return !reg_overlap_mentioned_p (value, early_op);
14380 }
14381
14382 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14383    have an early register shift value dependency on the result of
14384    PRODUCER.  */
14385
14386 int
14387 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14388 {
14389   rtx value = PATTERN (producer);
14390   rtx op = PATTERN (consumer);
14391   rtx early_op;
14392
14393   if (GET_CODE (value) == COND_EXEC)
14394     value = COND_EXEC_CODE (value);
14395   if (GET_CODE (value) == PARALLEL)
14396     value = XVECEXP (value, 0, 0);
14397   value = XEXP (value, 0);
14398   if (GET_CODE (op) == COND_EXEC)
14399     op = COND_EXEC_CODE (op);
14400   if (GET_CODE (op) == PARALLEL)
14401     op = XVECEXP (op, 0, 0);
14402   op = XEXP (op, 1);
14403   
14404   early_op = XEXP (op, 0);
14405
14406   /* This is either an actual independent shift, or a shift applied to
14407      the first operand of another operation.  We want the value being
14408      shifted, in either case.  */
14409   if (GET_CODE (early_op) != REG)
14410     early_op = XEXP (early_op, 0);
14411   
14412   return !reg_overlap_mentioned_p (value, early_op);
14413 }
14414
14415 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14416    have an early register mult dependency on the result of
14417    PRODUCER.  */
14418
14419 int
14420 arm_no_early_mul_dep (rtx producer, rtx consumer)
14421 {
14422   rtx value = PATTERN (producer);
14423   rtx op = PATTERN (consumer);
14424
14425   if (GET_CODE (value) == COND_EXEC)
14426     value = COND_EXEC_CODE (value);
14427   if (GET_CODE (value) == PARALLEL)
14428     value = XVECEXP (value, 0, 0);
14429   value = XEXP (value, 0);
14430   if (GET_CODE (op) == COND_EXEC)
14431     op = COND_EXEC_CODE (op);
14432   if (GET_CODE (op) == PARALLEL)
14433     op = XVECEXP (op, 0, 0);
14434   op = XEXP (op, 1);
14435   
14436   return (GET_CODE (op) == PLUS
14437           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14438 }
14439