OSDN Git Service

c1d1fa8e8973e9a52c7765e8b0e23e6be5275313
[pf3gnuchains/gcc-fork.git] / gcc / config / arm / arm.c
1 /* Output routines for GCC for ARM.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005  Free Software Foundation, Inc.
4    Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5    and Martin Simmons (@harleqn.co.uk).
6    More major hacks by Richard Earnshaw (rearnsha@arm.com).
7
8    This file is part of GCC.
9
10    GCC is free software; you can redistribute it and/or modify it
11    under the terms of the GNU General Public License as published
12    by the Free Software Foundation; either version 2, or (at your
13    option) any later version.
14
15    GCC is distributed in the hope that it will be useful, but WITHOUT
16    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18    License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with GCC; see the file COPYING.  If not, write to
22    the Free Software Foundation, 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, USA.  */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "obstack.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "output.h"
38 #include "insn-attr.h"
39 #include "flags.h"
40 #include "reload.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "optabs.h"
44 #include "toplev.h"
45 #include "recog.h"
46 #include "ggc.h"
47 #include "except.h"
48 #include "c-pragma.h"
49 #include "integrate.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "debug.h"
54 #include "langhooks.h"
55
56 /* Forward definitions of types.  */
57 typedef struct minipool_node    Mnode;
58 typedef struct minipool_fixup   Mfix;
59
60 const struct attribute_spec arm_attribute_table[];
61
62 /* Forward function declarations.  */
63 static arm_stack_offsets *arm_get_frame_offsets (void);
64 static void arm_add_gc_roots (void);
65 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
66                              HOST_WIDE_INT, rtx, rtx, int, int);
67 static unsigned bit_count (unsigned long);
68 static int arm_address_register_rtx_p (rtx, int);
69 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
70 static int thumb_base_register_rtx_p (rtx, enum machine_mode, int);
71 inline static int thumb_index_register_rtx_p (rtx, int);
72 static int thumb_far_jump_used_p (void);
73 static bool thumb_force_lr_save (void);
74 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
75 static rtx emit_sfm (int, int);
76 #ifndef AOF_ASSEMBLER
77 static bool arm_assemble_integer (rtx, unsigned int, int);
78 #endif
79 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
80 static arm_cc get_arm_condition_code (rtx);
81 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
82 static rtx is_jump_table (rtx);
83 static const char *output_multi_immediate (rtx *, const char *, const char *,
84                                            int, HOST_WIDE_INT);
85 static const char *shift_op (rtx, HOST_WIDE_INT *);
86 static struct machine_function *arm_init_machine_status (void);
87 static void thumb_exit (FILE *, int);
88 static rtx is_jump_table (rtx);
89 static HOST_WIDE_INT get_jump_table_size (rtx);
90 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
91 static Mnode *add_minipool_forward_ref (Mfix *);
92 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
93 static Mnode *add_minipool_backward_ref (Mfix *);
94 static void assign_minipool_offsets (Mfix *);
95 static void arm_print_value (FILE *, rtx);
96 static void dump_minipool (rtx);
97 static int arm_barrier_cost (rtx);
98 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
99 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
100 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
101                                rtx);
102 static void arm_reorg (void);
103 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
104 static int current_file_function_operand (rtx);
105 static unsigned long arm_compute_save_reg0_reg12_mask (void);
106 static unsigned long arm_compute_save_reg_mask (void);
107 static unsigned long arm_isr_value (tree);
108 static unsigned long arm_compute_func_type (void);
109 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
110 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
111 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
112 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
113 #endif
114 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
115 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
116 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
117 static int arm_comp_type_attributes (tree, tree);
118 static void arm_set_default_type_attributes (tree);
119 static int arm_adjust_cost (rtx, rtx, rtx, int);
120 static int count_insns_for_constant (HOST_WIDE_INT, int);
121 static int arm_get_strip_length (int);
122 static bool arm_function_ok_for_sibcall (tree, tree);
123 static void arm_internal_label (FILE *, const char *, unsigned long);
124 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
125                                  tree);
126 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
127 static bool arm_size_rtx_costs (rtx, int, int, int *);
128 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
129 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
130 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
131 static bool arm_9e_rtx_costs (rtx, int, int, int *);
132 static int arm_address_cost (rtx);
133 static bool arm_memory_load_p (rtx);
134 static bool arm_cirrus_insn_p (rtx);
135 static void cirrus_reorg (rtx);
136 static void arm_init_builtins (void);
137 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
138 static void arm_init_iwmmxt_builtins (void);
139 static rtx safe_vector_operand (rtx, enum machine_mode);
140 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
141 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
142 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
143 static void emit_constant_insn (rtx cond, rtx pattern);
144 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
145                                   tree, bool);
146
147 #ifdef OBJECT_FORMAT_ELF
148 static void arm_elf_asm_constructor (rtx, int);
149 #endif
150 #ifndef ARM_PE
151 static void arm_encode_section_info (tree, rtx, int);
152 #endif
153
154 static void arm_file_end (void);
155
156 #ifdef AOF_ASSEMBLER
157 static void aof_globalize_label (FILE *, const char *);
158 static void aof_dump_imports (FILE *);
159 static void aof_dump_pic_table (FILE *);
160 static void aof_file_start (void);
161 static void aof_file_end (void);
162 #endif
163 static rtx arm_struct_value_rtx (tree, int);
164 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
165                                         tree, int *, int);
166 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
167                                    enum machine_mode, tree, bool);
168 static bool arm_promote_prototypes (tree);
169 static bool arm_default_short_enums (void);
170 static bool arm_align_anon_bitfield (void);
171 static bool arm_return_in_msb (tree);
172 static bool arm_must_pass_in_stack (enum machine_mode, tree);
173
174 static tree arm_cxx_guard_type (void);
175 static bool arm_cxx_guard_mask_bit (void);
176 static tree arm_get_cookie_size (tree);
177 static bool arm_cookie_has_size (void);
178 static bool arm_cxx_cdtor_returns_this (void);
179 static bool arm_cxx_key_method_may_be_inline (void);
180 static void arm_cxx_determine_class_data_visibility (tree);
181 static bool arm_cxx_class_data_always_comdat (void);
182 static bool arm_cxx_use_aeabi_atexit (void);
183 static void arm_init_libfuncs (void);
184 static bool arm_handle_option (size_t, const char *, int);
185 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
186 \f
187 /* Initialize the GCC target structure.  */
188 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
189 #undef  TARGET_MERGE_DECL_ATTRIBUTES
190 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
191 #endif
192
193 #undef  TARGET_ATTRIBUTE_TABLE
194 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
195
196 #undef TARGET_ASM_FILE_END
197 #define TARGET_ASM_FILE_END arm_file_end
198
199 #ifdef AOF_ASSEMBLER
200 #undef  TARGET_ASM_BYTE_OP
201 #define TARGET_ASM_BYTE_OP "\tDCB\t"
202 #undef  TARGET_ASM_ALIGNED_HI_OP
203 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
204 #undef  TARGET_ASM_ALIGNED_SI_OP
205 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
206 #undef TARGET_ASM_GLOBALIZE_LABEL
207 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
208 #undef TARGET_ASM_FILE_START
209 #define TARGET_ASM_FILE_START aof_file_start
210 #undef TARGET_ASM_FILE_END
211 #define TARGET_ASM_FILE_END aof_file_end
212 #else
213 #undef  TARGET_ASM_ALIGNED_SI_OP
214 #define TARGET_ASM_ALIGNED_SI_OP NULL
215 #undef  TARGET_ASM_INTEGER
216 #define TARGET_ASM_INTEGER arm_assemble_integer
217 #endif
218
219 #undef  TARGET_ASM_FUNCTION_PROLOGUE
220 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
221
222 #undef  TARGET_ASM_FUNCTION_EPILOGUE
223 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
224
225 #undef  TARGET_DEFAULT_TARGET_FLAGS
226 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
227 #undef  TARGET_HANDLE_OPTION
228 #define TARGET_HANDLE_OPTION arm_handle_option
229
230 #undef  TARGET_COMP_TYPE_ATTRIBUTES
231 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
232
233 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
234 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
235
236 #undef  TARGET_SCHED_ADJUST_COST
237 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
238
239 #undef TARGET_ENCODE_SECTION_INFO
240 #ifdef ARM_PE
241 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
242 #else
243 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
244 #endif
245
246 #undef  TARGET_STRIP_NAME_ENCODING
247 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
248
249 #undef  TARGET_ASM_INTERNAL_LABEL
250 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
251
252 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
253 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
254
255 #undef  TARGET_ASM_OUTPUT_MI_THUNK
256 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
257 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
258 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
259
260 /* This will be overridden in arm_override_options.  */
261 #undef  TARGET_RTX_COSTS
262 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
263 #undef  TARGET_ADDRESS_COST
264 #define TARGET_ADDRESS_COST arm_address_cost
265
266 #undef TARGET_SHIFT_TRUNCATION_MASK
267 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
268 #undef TARGET_VECTOR_MODE_SUPPORTED_P
269 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
270
271 #undef  TARGET_MACHINE_DEPENDENT_REORG
272 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
273
274 #undef  TARGET_INIT_BUILTINS
275 #define TARGET_INIT_BUILTINS  arm_init_builtins
276 #undef  TARGET_EXPAND_BUILTIN
277 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
278
279 #undef TARGET_INIT_LIBFUNCS
280 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
281
282 #undef TARGET_PROMOTE_FUNCTION_ARGS
283 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
284 #undef TARGET_PROMOTE_FUNCTION_RETURN
285 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
286 #undef TARGET_PROMOTE_PROTOTYPES
287 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
288 #undef TARGET_PASS_BY_REFERENCE
289 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
290 #undef TARGET_ARG_PARTIAL_BYTES
291 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
292
293 #undef TARGET_STRUCT_VALUE_RTX
294 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
295
296 #undef  TARGET_SETUP_INCOMING_VARARGS
297 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
298
299 #undef TARGET_DEFAULT_SHORT_ENUMS
300 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
301
302 #undef TARGET_ALIGN_ANON_BITFIELD
303 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
304
305 #undef TARGET_CXX_GUARD_TYPE
306 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
307
308 #undef TARGET_CXX_GUARD_MASK_BIT
309 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
310
311 #undef TARGET_CXX_GET_COOKIE_SIZE
312 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
313
314 #undef TARGET_CXX_COOKIE_HAS_SIZE
315 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
316
317 #undef TARGET_CXX_CDTOR_RETURNS_THIS
318 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
319
320 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
321 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
322
323 #undef TARGET_CXX_USE_AEABI_ATEXIT
324 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
325
326 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
327 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
328   arm_cxx_determine_class_data_visibility
329
330 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
331 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
332
333 #undef TARGET_RETURN_IN_MSB
334 #define TARGET_RETURN_IN_MSB arm_return_in_msb
335
336 #undef TARGET_MUST_PASS_IN_STACK
337 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
338
339 struct gcc_target targetm = TARGET_INITIALIZER;
340 \f
341 /* Obstack for minipool constant handling.  */
342 static struct obstack minipool_obstack;
343 static char *         minipool_startobj;
344
345 /* The maximum number of insns skipped which
346    will be conditionalised if possible.  */
347 static int max_insns_skipped = 5;
348
349 extern FILE * asm_out_file;
350
351 /* True if we are currently building a constant table.  */
352 int making_const_table;
353
354 /* Define the information needed to generate branch insns.  This is
355    stored from the compare operation.  */
356 rtx arm_compare_op0, arm_compare_op1;
357
358 /* The processor for which instructions should be scheduled.  */
359 enum processor_type arm_tune = arm_none;
360
361 /* Which floating point model to use.  */
362 enum arm_fp_model arm_fp_model;
363
364 /* Which floating point hardware is available.  */
365 enum fputype arm_fpu_arch;
366
367 /* Which floating point hardware to schedule for.  */
368 enum fputype arm_fpu_tune;
369
370 /* Whether to use floating point hardware.  */
371 enum float_abi_type arm_float_abi;
372
373 /* Which ABI to use.  */
374 enum arm_abi_type arm_abi;
375
376 /* Set by the -mfpu=... option.  */
377 static const char * target_fpu_name = NULL;
378
379 /* Set by the -mfpe=... option.  */
380 static const char * target_fpe_name = NULL;
381
382 /* Set by the -mfloat-abi=... option.  */
383 static const char * target_float_abi_name = NULL;
384
385 /* Set by the -mabi=... option.  */
386 static const char * target_abi_name = NULL;
387
388 /* Used to parse -mstructure_size_boundary command line option.  */
389 static const char * structure_size_string = NULL;
390 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
391
392 /* Used for Thumb call_via trampolines.  */
393 rtx thumb_call_via_label[14];
394 static int thumb_call_reg_needed;
395
396 /* Bit values used to identify processor capabilities.  */
397 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
398 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
399 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
400 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
401 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
402 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
403 #define FL_THUMB      (1 << 6)        /* Thumb aware */
404 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
405 #define FL_STRONG     (1 << 8)        /* StrongARM */
406 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
407 #define FL_XSCALE     (1 << 10)       /* XScale */
408 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
409 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
410                                          media instructions.  */
411 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
412 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
413                                          Note: ARM6 & 7 derivatives only.  */
414
415 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
416
417 #define FL_FOR_ARCH2    0
418 #define FL_FOR_ARCH3    FL_MODE32
419 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
420 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
421 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
422 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
423 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
424 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
425 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
426 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
427 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
428 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
429 #define FL_FOR_ARCH6K   FL_FOR_ARCH6
430 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
431 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6
432
433 /* The bits in this mask specify which
434    instructions we are allowed to generate.  */
435 static unsigned long insn_flags = 0;
436
437 /* The bits in this mask specify which instruction scheduling options should
438    be used.  */
439 static unsigned long tune_flags = 0;
440
441 /* The following are used in the arm.md file as equivalents to bits
442    in the above two flag variables.  */
443
444 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
445 int arm_arch3m = 0;
446
447 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
448 int arm_arch4 = 0;
449
450 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
451 int arm_arch4t = 0;
452
453 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
454 int arm_arch5 = 0;
455
456 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
457 int arm_arch5e = 0;
458
459 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
460 int arm_arch6 = 0;
461
462 /* Nonzero if this chip can benefit from load scheduling.  */
463 int arm_ld_sched = 0;
464
465 /* Nonzero if this chip is a StrongARM.  */
466 int arm_tune_strongarm = 0;
467
468 /* Nonzero if this chip is a Cirrus variant.  */
469 int arm_arch_cirrus = 0;
470
471 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
472 int arm_arch_iwmmxt = 0;
473
474 /* Nonzero if this chip is an XScale.  */
475 int arm_arch_xscale = 0;
476
477 /* Nonzero if tuning for XScale  */
478 int arm_tune_xscale = 0;
479
480 /* Nonzero if we want to tune for stores that access the write-buffer. 
481    This typically means an ARM6 or ARM7 with MMU or MPU.  */
482 int arm_tune_wbuf = 0;
483
484 /* Nonzero if generating Thumb instructions.  */
485 int thumb_code = 0;
486
487 /* Nonzero if we should define __THUMB_INTERWORK__ in the
488    preprocessor.
489    XXX This is a bit of a hack, it's intended to help work around
490    problems in GLD which doesn't understand that armv5t code is
491    interworking clean.  */
492 int arm_cpp_interwork = 0;
493
494 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
495    must report the mode of the memory reference from PRINT_OPERAND to
496    PRINT_OPERAND_ADDRESS.  */
497 enum machine_mode output_memory_reference_mode;
498
499 /* The register number to be used for the PIC offset register.  */
500 static const char * arm_pic_register_string = NULL;
501 int arm_pic_register = INVALID_REGNUM;
502
503 /* Set to 1 when a return insn is output, this means that the epilogue
504    is not needed.  */
505 int return_used_this_function;
506
507 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
508    the next function.  */
509 static int after_arm_reorg = 0;
510
511 /* The maximum number of insns to be used when loading a constant.  */
512 static int arm_constant_limit = 3;
513
514 /* For an explanation of these variables, see final_prescan_insn below.  */
515 int arm_ccfsm_state;
516 enum arm_cond_code arm_current_cc;
517 rtx arm_target_insn;
518 int arm_target_label;
519
520 /* The condition codes of the ARM, and the inverse function.  */
521 static const char * const arm_condition_codes[] =
522 {
523   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
524   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
525 };
526
527 #define streq(string1, string2) (strcmp (string1, string2) == 0)
528 \f
529 /* Initialization code.  */
530
531 struct processors
532 {
533   const char *const name;
534   enum processor_type core;
535   const char *arch;
536   const unsigned long flags;
537   bool (* rtx_costs) (rtx, int, int, int *);
538 };
539
540 /* Not all of these give usefully different compilation alternatives,
541    but there is no simple way of generalizing them.  */
542 static const struct processors all_cores[] =
543 {
544   /* ARM Cores */
545 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
546   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
547 #include "arm-cores.def"
548 #undef ARM_CORE
549   {NULL, arm_none, NULL, 0, NULL}
550 };
551
552 static const struct processors all_architectures[] =
553 {
554   /* ARM Architectures */
555   /* We don't specify rtx_costs here as it will be figured out
556      from the core.  */
557
558   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
559   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
560   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
561   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
562   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
563   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
564      implementations that support it, so we will leave it out for now.  */
565   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
566   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
567   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
568   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
569   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
570   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
571   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
572   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
573   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
574   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
575   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
576   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
577   {NULL, arm_none, NULL, 0 , NULL}
578 };
579
580 struct arm_cpu_select
581 {
582   const char *              string;
583   const char *              name;
584   const struct processors * processors;
585 };
586
587 /* This is a magic structure.  The 'string' field is magically filled in
588    with a pointer to the value specified by the user on the command line
589    assuming that the user has specified such a value.  */
590
591 static struct arm_cpu_select arm_select[] =
592 {
593   /* string       name            processors  */
594   { NULL,       "-mcpu=",       all_cores  },
595   { NULL,       "-march=",      all_architectures },
596   { NULL,       "-mtune=",      all_cores }
597 };
598
599 /* Defines representing the indexes into the above table.  */
600 #define ARM_OPT_SET_CPU 0
601 #define ARM_OPT_SET_ARCH 1
602 #define ARM_OPT_SET_TUNE 2
603
604 /* The name of the proprocessor macro to define for this architecture.  */
605
606 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
607
608 struct fpu_desc
609 {
610   const char * name;
611   enum fputype fpu;
612 };
613
614
615 /* Available values for for -mfpu=.  */
616
617 static const struct fpu_desc all_fpus[] =
618 {
619   {"fpa",       FPUTYPE_FPA},
620   {"fpe2",      FPUTYPE_FPA_EMU2},
621   {"fpe3",      FPUTYPE_FPA_EMU2},
622   {"maverick",  FPUTYPE_MAVERICK},
623   {"vfp",       FPUTYPE_VFP}
624 };
625
626
627 /* Floating point models used by the different hardware.
628    See fputype in arm.h.  */
629
630 static const enum fputype fp_model_for_fpu[] =
631 {
632   /* No FP hardware.  */
633   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
634   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
635   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
636   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
637   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
638   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
639 };
640
641
642 struct float_abi
643 {
644   const char * name;
645   enum float_abi_type abi_type;
646 };
647
648
649 /* Available values for -mfloat-abi=.  */
650
651 static const struct float_abi all_float_abis[] =
652 {
653   {"soft",      ARM_FLOAT_ABI_SOFT},
654   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
655   {"hard",      ARM_FLOAT_ABI_HARD}
656 };
657
658
659 struct abi_name
660 {
661   const char *name;
662   enum arm_abi_type abi_type;
663 };
664
665
666 /* Available values for -mabi=.  */
667
668 static const struct abi_name arm_all_abis[] =
669 {
670   {"apcs-gnu",    ARM_ABI_APCS},
671   {"atpcs",   ARM_ABI_ATPCS},
672   {"aapcs",   ARM_ABI_AAPCS},
673   {"iwmmxt",  ARM_ABI_IWMMXT}
674 };
675
676 /* Return the number of bits set in VALUE.  */
677 static unsigned
678 bit_count (unsigned long value)
679 {
680   unsigned long count = 0;
681
682   while (value)
683     {
684       count++;
685       value &= value - 1;  /* Clear the least-significant set bit.  */
686     }
687
688   return count;
689 }
690
691 /* Set up library functions unique to ARM.  */
692
693 static void
694 arm_init_libfuncs (void)
695 {
696   /* There are no special library functions unless we are using the
697      ARM BPABI.  */
698   if (!TARGET_BPABI)
699     return;
700
701   /* The functions below are described in Section 4 of the "Run-Time
702      ABI for the ARM architecture", Version 1.0.  */
703
704   /* Double-precision floating-point arithmetic.  Table 2.  */
705   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
706   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
707   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
708   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
709   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
710
711   /* Double-precision comparisons.  Table 3.  */
712   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
713   set_optab_libfunc (ne_optab, DFmode, NULL);
714   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
715   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
716   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
717   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
718   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
719
720   /* Single-precision floating-point arithmetic.  Table 4.  */
721   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
722   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
723   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
724   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
725   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
726
727   /* Single-precision comparisons.  Table 5.  */
728   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
729   set_optab_libfunc (ne_optab, SFmode, NULL);
730   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
731   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
732   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
733   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
734   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
735
736   /* Floating-point to integer conversions.  Table 6.  */
737   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
738   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
739   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
740   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
741   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
742   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
743   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
744   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
745
746   /* Conversions between floating types.  Table 7.  */
747   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
748   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
749
750   /* Integer to floating-point conversions.  Table 8.  */
751   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
752   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
753   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
754   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
755   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
756   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
757   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
758   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
759
760   /* Long long.  Table 9.  */
761   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
762   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
763   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
764   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
765   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
766   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
767   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
768   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
769
770   /* Integer (32/32->32) division.  \S 4.3.1.  */
771   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
772   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
773
774   /* The divmod functions are designed so that they can be used for
775      plain division, even though they return both the quotient and the
776      remainder.  The quotient is returned in the usual location (i.e.,
777      r0 for SImode, {r0, r1} for DImode), just as would be expected
778      for an ordinary division routine.  Because the AAPCS calling
779      conventions specify that all of { r0, r1, r2, r3 } are
780      callee-saved registers, there is no need to tell the compiler
781      explicitly that those registers are clobbered by these
782      routines.  */
783   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
784   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
785   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idivmod");
786   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidivmod");
787
788   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
789      divmod libcalls instead.  */
790   set_optab_libfunc (smod_optab, DImode, NULL);
791   set_optab_libfunc (umod_optab, DImode, NULL);
792   set_optab_libfunc (smod_optab, SImode, NULL);
793   set_optab_libfunc (umod_optab, SImode, NULL);
794 }
795
796 /* Implement TARGET_HANDLE_OPTION.  */
797
798 static bool
799 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
800 {
801   switch (code)
802     {
803     case OPT_mabi_:
804       target_abi_name = arg;
805       return true;
806
807     case OPT_march_:
808       arm_select[1].string = arg;
809       return true;
810
811     case OPT_mcpu_:
812       arm_select[0].string = arg;
813       return true;
814
815     case OPT_mfloat_abi_:
816       target_float_abi_name = arg;
817       return true;
818
819     case OPT_mfp_:
820     case OPT_mfpe_:
821       target_fpe_name = arg;
822       return true;
823
824     case OPT_mfpu_:
825       target_fpu_name = arg;
826       return true;
827
828     case OPT_mhard_float:
829       target_float_abi_name = "hard";
830       return true;
831
832     case OPT_mpic_register_:
833       arm_pic_register_string = arg;
834       return true;
835
836     case OPT_msoft_float:
837       target_float_abi_name = "soft";
838       return true;
839
840     case OPT_mstructure_size_boundary_:
841       structure_size_string = arg;
842       return true;
843
844     case OPT_mtune_:
845       arm_select[2].string = arg;
846       return true;
847
848     default:
849       return true;
850     }
851 }
852
853 /* Fix up any incompatible options that the user has specified.
854    This has now turned into a maze.  */
855 void
856 arm_override_options (void)
857 {
858   unsigned i;
859   enum processor_type target_arch_cpu = arm_none;
860
861   /* Set up the flags based on the cpu/architecture selected by the user.  */
862   for (i = ARRAY_SIZE (arm_select); i--;)
863     {
864       struct arm_cpu_select * ptr = arm_select + i;
865
866       if (ptr->string != NULL && ptr->string[0] != '\0')
867         {
868           const struct processors * sel;
869
870           for (sel = ptr->processors; sel->name != NULL; sel++)
871             if (streq (ptr->string, sel->name))
872               {
873                 /* Set the architecture define.  */
874                 if (i != ARM_OPT_SET_TUNE)
875                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
876
877                 /* Determine the processor core for which we should
878                    tune code-generation.  */
879                 if (/* -mcpu= is a sensible default.  */
880                     i == ARM_OPT_SET_CPU
881                     /* -mtune= overrides -mcpu= and -march=.  */
882                     || i == ARM_OPT_SET_TUNE)
883                   arm_tune = (enum processor_type) (sel - ptr->processors);
884
885                 /* Remember the CPU associated with this architecture.
886                    If no other option is used to set the CPU type,
887                    we'll use this to guess the most suitable tuning
888                    options.  */
889                 if (i == ARM_OPT_SET_ARCH)
890                   target_arch_cpu = sel->core;
891                 
892                 if (i != ARM_OPT_SET_TUNE)
893                   {
894                     /* If we have been given an architecture and a processor
895                        make sure that they are compatible.  We only generate
896                        a warning though, and we prefer the CPU over the
897                        architecture.  */
898                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
899                       warning (0, "switch -mcpu=%s conflicts with -march= switch",
900                                ptr->string);
901
902                     insn_flags = sel->flags;
903                   }
904
905                 break;
906               }
907
908           if (sel->name == NULL)
909             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
910         }
911     }
912
913   /* Guess the tuning options from the architecture if necessary.  */
914   if (arm_tune == arm_none)
915     arm_tune = target_arch_cpu;
916
917   /* If the user did not specify a processor, choose one for them.  */
918   if (insn_flags == 0)
919     {
920       const struct processors * sel;
921       unsigned int        sought;
922       enum processor_type cpu;
923
924       cpu = TARGET_CPU_DEFAULT;
925       if (cpu == arm_none)
926         {
927 #ifdef SUBTARGET_CPU_DEFAULT
928           /* Use the subtarget default CPU if none was specified by
929              configure.  */
930           cpu = SUBTARGET_CPU_DEFAULT;
931 #endif
932           /* Default to ARM6.  */
933           if (cpu == arm_none)
934             cpu = arm6;
935         }
936       sel = &all_cores[cpu];
937
938       insn_flags = sel->flags;
939
940       /* Now check to see if the user has specified some command line
941          switch that require certain abilities from the cpu.  */
942       sought = 0;
943
944       if (TARGET_INTERWORK || TARGET_THUMB)
945         {
946           sought |= (FL_THUMB | FL_MODE32);
947
948           /* There are no ARM processors that support both APCS-26 and
949              interworking.  Therefore we force FL_MODE26 to be removed
950              from insn_flags here (if it was set), so that the search
951              below will always be able to find a compatible processor.  */
952           insn_flags &= ~FL_MODE26;
953         }
954
955       if (sought != 0 && ((sought & insn_flags) != sought))
956         {
957           /* Try to locate a CPU type that supports all of the abilities
958              of the default CPU, plus the extra abilities requested by
959              the user.  */
960           for (sel = all_cores; sel->name != NULL; sel++)
961             if ((sel->flags & sought) == (sought | insn_flags))
962               break;
963
964           if (sel->name == NULL)
965             {
966               unsigned current_bit_count = 0;
967               const struct processors * best_fit = NULL;
968
969               /* Ideally we would like to issue an error message here
970                  saying that it was not possible to find a CPU compatible
971                  with the default CPU, but which also supports the command
972                  line options specified by the programmer, and so they
973                  ought to use the -mcpu=<name> command line option to
974                  override the default CPU type.
975
976                  If we cannot find a cpu that has both the
977                  characteristics of the default cpu and the given
978                  command line options we scan the array again looking
979                  for a best match.  */
980               for (sel = all_cores; sel->name != NULL; sel++)
981                 if ((sel->flags & sought) == sought)
982                   {
983                     unsigned count;
984
985                     count = bit_count (sel->flags & insn_flags);
986
987                     if (count >= current_bit_count)
988                       {
989                         best_fit = sel;
990                         current_bit_count = count;
991                       }
992                   }
993
994               gcc_assert (best_fit);
995               sel = best_fit;
996             }
997
998           insn_flags = sel->flags;
999         }
1000       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1001       if (arm_tune == arm_none)
1002         arm_tune = (enum processor_type) (sel - all_cores);
1003     }
1004
1005   /* The processor for which we should tune should now have been
1006      chosen.  */
1007   gcc_assert (arm_tune != arm_none);
1008
1009   tune_flags = all_cores[(int)arm_tune].flags;
1010   if (optimize_size)
1011     targetm.rtx_costs = arm_size_rtx_costs;
1012   else
1013     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
1014
1015   /* Make sure that the processor choice does not conflict with any of the
1016      other command line choices.  */
1017   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
1018     {
1019       warning (0, "target CPU does not support interworking" );
1020       target_flags &= ~MASK_INTERWORK;
1021     }
1022
1023   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1024     {
1025       warning (0, "target CPU does not support THUMB instructions");
1026       target_flags &= ~MASK_THUMB;
1027     }
1028
1029   if (TARGET_APCS_FRAME && TARGET_THUMB)
1030     {
1031       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1032       target_flags &= ~MASK_APCS_FRAME;
1033     }
1034
1035   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1036      from here where no function is being compiled currently.  */
1037   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1038     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1039
1040   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1041     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1042
1043   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1044     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1045
1046   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1047     {
1048       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1049       target_flags |= MASK_APCS_FRAME;
1050     }
1051
1052   if (TARGET_POKE_FUNCTION_NAME)
1053     target_flags |= MASK_APCS_FRAME;
1054
1055   if (TARGET_APCS_REENT && flag_pic)
1056     error ("-fpic and -mapcs-reent are incompatible");
1057
1058   if (TARGET_APCS_REENT)
1059     warning (0, "APCS reentrant code not supported.  Ignored");
1060
1061   /* If this target is normally configured to use APCS frames, warn if they
1062      are turned off and debugging is turned on.  */
1063   if (TARGET_ARM
1064       && write_symbols != NO_DEBUG
1065       && !TARGET_APCS_FRAME
1066       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1067     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1068
1069   /* If stack checking is disabled, we can use r10 as the PIC register,
1070      which keeps r9 available.  */
1071   if (flag_pic)
1072     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
1073
1074   if (TARGET_APCS_FLOAT)
1075     warning (0, "passing floating point arguments in fp regs not yet supported");
1076
1077   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1078   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1079   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1080   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1081   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1082   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1083   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1084   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1085   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1086
1087   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1088   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1089   thumb_code = (TARGET_ARM == 0);
1090   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1091   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1092   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1093
1094   /* V5 code we generate is completely interworking capable, so we turn off
1095      TARGET_INTERWORK here to avoid many tests later on.  */
1096
1097   /* XXX However, we must pass the right pre-processor defines to CPP
1098      or GLD can get confused.  This is a hack.  */
1099   if (TARGET_INTERWORK)
1100     arm_cpp_interwork = 1;
1101
1102   if (arm_arch5)
1103     target_flags &= ~MASK_INTERWORK;
1104
1105   if (target_abi_name)
1106     {
1107       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1108         {
1109           if (streq (arm_all_abis[i].name, target_abi_name))
1110             {
1111               arm_abi = arm_all_abis[i].abi_type;
1112               break;
1113             }
1114         }
1115       if (i == ARRAY_SIZE (arm_all_abis))
1116         error ("invalid ABI option: -mabi=%s", target_abi_name);
1117     }
1118   else
1119     arm_abi = ARM_DEFAULT_ABI;
1120
1121   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1122     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1123
1124   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1125     error ("iwmmxt abi requires an iwmmxt capable cpu");
1126
1127   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1128   if (target_fpu_name == NULL && target_fpe_name != NULL)
1129     {
1130       if (streq (target_fpe_name, "2"))
1131         target_fpu_name = "fpe2";
1132       else if (streq (target_fpe_name, "3"))
1133         target_fpu_name = "fpe3";
1134       else
1135         error ("invalid floating point emulation option: -mfpe=%s",
1136                target_fpe_name);
1137     }
1138   if (target_fpu_name != NULL)
1139     {
1140       /* The user specified a FPU.  */
1141       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1142         {
1143           if (streq (all_fpus[i].name, target_fpu_name))
1144             {
1145               arm_fpu_arch = all_fpus[i].fpu;
1146               arm_fpu_tune = arm_fpu_arch;
1147               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1148               break;
1149             }
1150         }
1151       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1152         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1153     }
1154   else
1155     {
1156 #ifdef FPUTYPE_DEFAULT
1157       /* Use the default if it is specified for this platform.  */
1158       arm_fpu_arch = FPUTYPE_DEFAULT;
1159       arm_fpu_tune = FPUTYPE_DEFAULT;
1160 #else
1161       /* Pick one based on CPU type.  */
1162       /* ??? Some targets assume FPA is the default.
1163       if ((insn_flags & FL_VFP) != 0)
1164         arm_fpu_arch = FPUTYPE_VFP;
1165       else
1166       */
1167       if (arm_arch_cirrus)
1168         arm_fpu_arch = FPUTYPE_MAVERICK;
1169       else
1170         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1171 #endif
1172       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1173         arm_fpu_tune = FPUTYPE_FPA;
1174       else
1175         arm_fpu_tune = arm_fpu_arch;
1176       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1177       gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1178     }
1179
1180   if (target_float_abi_name != NULL)
1181     {
1182       /* The user specified a FP ABI.  */
1183       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1184         {
1185           if (streq (all_float_abis[i].name, target_float_abi_name))
1186             {
1187               arm_float_abi = all_float_abis[i].abi_type;
1188               break;
1189             }
1190         }
1191       if (i == ARRAY_SIZE (all_float_abis))
1192         error ("invalid floating point abi: -mfloat-abi=%s",
1193                target_float_abi_name);
1194     }
1195   else
1196     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1197
1198   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1199     sorry ("-mfloat-abi=hard and VFP");
1200
1201   /* If soft-float is specified then don't use FPU.  */
1202   if (TARGET_SOFT_FLOAT)
1203     arm_fpu_arch = FPUTYPE_NONE;
1204
1205   /* For arm2/3 there is no need to do any scheduling if there is only
1206      a floating point emulator, or we are doing software floating-point.  */
1207   if ((TARGET_SOFT_FLOAT
1208        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1209        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1210       && (tune_flags & FL_MODE32) == 0)
1211     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1212
1213   /* Override the default structure alignment for AAPCS ABI.  */
1214   if (arm_abi == ARM_ABI_AAPCS)
1215     arm_structure_size_boundary = 8;
1216
1217   if (structure_size_string != NULL)
1218     {
1219       int size = strtol (structure_size_string, NULL, 0);
1220
1221       if (size == 8 || size == 32
1222           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1223         arm_structure_size_boundary = size;
1224       else
1225         warning (0, "structure size boundary can only be set to %s",
1226                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1227     }
1228
1229   if (arm_pic_register_string != NULL)
1230     {
1231       int pic_register = decode_reg_name (arm_pic_register_string);
1232
1233       if (!flag_pic)
1234         warning (0, "-mpic-register= is useless without -fpic");
1235
1236       /* Prevent the user from choosing an obviously stupid PIC register.  */
1237       else if (pic_register < 0 || call_used_regs[pic_register]
1238                || pic_register == HARD_FRAME_POINTER_REGNUM
1239                || pic_register == STACK_POINTER_REGNUM
1240                || pic_register >= PC_REGNUM)
1241         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1242       else
1243         arm_pic_register = pic_register;
1244     }
1245
1246   if (TARGET_THUMB && flag_schedule_insns)
1247     {
1248       /* Don't warn since it's on by default in -O2.  */
1249       flag_schedule_insns = 0;
1250     }
1251
1252   if (optimize_size)
1253     {
1254       /* There's some dispute as to whether this should be 1 or 2.  However,
1255          experiments seem to show that in pathological cases a setting of
1256          1 degrades less severely than a setting of 2.  This could change if
1257          other parts of the compiler change their behavior.  */
1258       arm_constant_limit = 1;
1259
1260       /* If optimizing for size, bump the number of instructions that we
1261          are prepared to conditionally execute (even on a StrongARM).  */
1262       max_insns_skipped = 6;
1263     }
1264   else
1265     {
1266       /* For processors with load scheduling, it never costs more than
1267          2 cycles to load a constant, and the load scheduler may well
1268          reduce that to 1.  */
1269       if (arm_ld_sched)
1270         arm_constant_limit = 1;
1271
1272       /* On XScale the longer latency of a load makes it more difficult
1273          to achieve a good schedule, so it's faster to synthesize
1274          constants that can be done in two insns.  */
1275       if (arm_tune_xscale)
1276         arm_constant_limit = 2;
1277
1278       /* StrongARM has early execution of branches, so a sequence
1279          that is worth skipping is shorter.  */
1280       if (arm_tune_strongarm)
1281         max_insns_skipped = 3;
1282     }
1283
1284   /* Register global variables with the garbage collector.  */
1285   arm_add_gc_roots ();
1286 }
1287
1288 static void
1289 arm_add_gc_roots (void)
1290 {
1291   gcc_obstack_init(&minipool_obstack);
1292   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1293 }
1294 \f
1295 /* A table of known ARM exception types.
1296    For use with the interrupt function attribute.  */
1297
1298 typedef struct
1299 {
1300   const char *const arg;
1301   const unsigned long return_value;
1302 }
1303 isr_attribute_arg;
1304
1305 static const isr_attribute_arg isr_attribute_args [] =
1306 {
1307   { "IRQ",   ARM_FT_ISR },
1308   { "irq",   ARM_FT_ISR },
1309   { "FIQ",   ARM_FT_FIQ },
1310   { "fiq",   ARM_FT_FIQ },
1311   { "ABORT", ARM_FT_ISR },
1312   { "abort", ARM_FT_ISR },
1313   { "ABORT", ARM_FT_ISR },
1314   { "abort", ARM_FT_ISR },
1315   { "UNDEF", ARM_FT_EXCEPTION },
1316   { "undef", ARM_FT_EXCEPTION },
1317   { "SWI",   ARM_FT_EXCEPTION },
1318   { "swi",   ARM_FT_EXCEPTION },
1319   { NULL,    ARM_FT_NORMAL }
1320 };
1321
1322 /* Returns the (interrupt) function type of the current
1323    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1324
1325 static unsigned long
1326 arm_isr_value (tree argument)
1327 {
1328   const isr_attribute_arg * ptr;
1329   const char *              arg;
1330
1331   /* No argument - default to IRQ.  */
1332   if (argument == NULL_TREE)
1333     return ARM_FT_ISR;
1334
1335   /* Get the value of the argument.  */
1336   if (TREE_VALUE (argument) == NULL_TREE
1337       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1338     return ARM_FT_UNKNOWN;
1339
1340   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1341
1342   /* Check it against the list of known arguments.  */
1343   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1344     if (streq (arg, ptr->arg))
1345       return ptr->return_value;
1346
1347   /* An unrecognized interrupt type.  */
1348   return ARM_FT_UNKNOWN;
1349 }
1350
1351 /* Computes the type of the current function.  */
1352
1353 static unsigned long
1354 arm_compute_func_type (void)
1355 {
1356   unsigned long type = ARM_FT_UNKNOWN;
1357   tree a;
1358   tree attr;
1359
1360   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1361
1362   /* Decide if the current function is volatile.  Such functions
1363      never return, and many memory cycles can be saved by not storing
1364      register values that will never be needed again.  This optimization
1365      was added to speed up context switching in a kernel application.  */
1366   if (optimize > 0
1367       && TREE_NOTHROW (current_function_decl)
1368       && TREE_THIS_VOLATILE (current_function_decl))
1369     type |= ARM_FT_VOLATILE;
1370
1371   if (cfun->static_chain_decl != NULL)
1372     type |= ARM_FT_NESTED;
1373
1374   attr = DECL_ATTRIBUTES (current_function_decl);
1375
1376   a = lookup_attribute ("naked", attr);
1377   if (a != NULL_TREE)
1378     type |= ARM_FT_NAKED;
1379
1380   a = lookup_attribute ("isr", attr);
1381   if (a == NULL_TREE)
1382     a = lookup_attribute ("interrupt", attr);
1383
1384   if (a == NULL_TREE)
1385     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1386   else
1387     type |= arm_isr_value (TREE_VALUE (a));
1388
1389   return type;
1390 }
1391
1392 /* Returns the type of the current function.  */
1393
1394 unsigned long
1395 arm_current_func_type (void)
1396 {
1397   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1398     cfun->machine->func_type = arm_compute_func_type ();
1399
1400   return cfun->machine->func_type;
1401 }
1402 \f
1403 /* Return 1 if it is possible to return using a single instruction.
1404    If SIBLING is non-null, this is a test for a return before a sibling
1405    call.  SIBLING is the call insn, so we can examine its register usage.  */
1406
1407 int
1408 use_return_insn (int iscond, rtx sibling)
1409 {
1410   int regno;
1411   unsigned int func_type;
1412   unsigned long saved_int_regs;
1413   unsigned HOST_WIDE_INT stack_adjust;
1414   arm_stack_offsets *offsets;
1415
1416   /* Never use a return instruction before reload has run.  */
1417   if (!reload_completed)
1418     return 0;
1419
1420   func_type = arm_current_func_type ();
1421
1422   /* Naked functions and volatile functions need special
1423      consideration.  */
1424   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1425     return 0;
1426
1427   /* So do interrupt functions that use the frame pointer.  */
1428   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1429     return 0;
1430
1431   offsets = arm_get_frame_offsets ();
1432   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1433
1434   /* As do variadic functions.  */
1435   if (current_function_pretend_args_size
1436       || cfun->machine->uses_anonymous_args
1437       /* Or if the function calls __builtin_eh_return () */
1438       || current_function_calls_eh_return
1439       /* Or if the function calls alloca */
1440       || current_function_calls_alloca
1441       /* Or if there is a stack adjustment.  However, if the stack pointer
1442          is saved on the stack, we can use a pre-incrementing stack load.  */
1443       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1444     return 0;
1445
1446   saved_int_regs = arm_compute_save_reg_mask ();
1447
1448   /* Unfortunately, the insn
1449
1450        ldmib sp, {..., sp, ...}
1451
1452      triggers a bug on most SA-110 based devices, such that the stack
1453      pointer won't be correctly restored if the instruction takes a
1454      page fault.  We work around this problem by popping r3 along with
1455      the other registers, since that is never slower than executing
1456      another instruction.
1457
1458      We test for !arm_arch5 here, because code for any architecture
1459      less than this could potentially be run on one of the buggy
1460      chips.  */
1461   if (stack_adjust == 4 && !arm_arch5)
1462     {
1463       /* Validate that r3 is a call-clobbered register (always true in
1464          the default abi) ...  */
1465       if (!call_used_regs[3])
1466         return 0;
1467
1468       /* ... that it isn't being used for a return value (always true
1469          until we implement return-in-regs), or for a tail-call
1470          argument ...  */
1471       if (sibling)
1472         {
1473           gcc_assert (GET_CODE (sibling) == CALL_INSN);
1474
1475           if (find_regno_fusage (sibling, USE, 3))
1476             return 0;
1477         }
1478
1479       /* ... and that there are no call-saved registers in r0-r2
1480          (always true in the default ABI).  */
1481       if (saved_int_regs & 0x7)
1482         return 0;
1483     }
1484
1485   /* Can't be done if interworking with Thumb, and any registers have been
1486      stacked.  */
1487   if (TARGET_INTERWORK && saved_int_regs != 0)
1488     return 0;
1489
1490   /* On StrongARM, conditional returns are expensive if they aren't
1491      taken and multiple registers have been stacked.  */
1492   if (iscond && arm_tune_strongarm)
1493     {
1494       /* Conditional return when just the LR is stored is a simple
1495          conditional-load instruction, that's not expensive.  */
1496       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1497         return 0;
1498
1499       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1500         return 0;
1501     }
1502
1503   /* If there are saved registers but the LR isn't saved, then we need
1504      two instructions for the return.  */
1505   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1506     return 0;
1507
1508   /* Can't be done if any of the FPA regs are pushed,
1509      since this also requires an insn.  */
1510   if (TARGET_HARD_FLOAT && TARGET_FPA)
1511     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1512       if (regs_ever_live[regno] && !call_used_regs[regno])
1513         return 0;
1514
1515   /* Likewise VFP regs.  */
1516   if (TARGET_HARD_FLOAT && TARGET_VFP)
1517     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1518       if (regs_ever_live[regno] && !call_used_regs[regno])
1519         return 0;
1520
1521   if (TARGET_REALLY_IWMMXT)
1522     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1523       if (regs_ever_live[regno] && ! call_used_regs [regno])
1524         return 0;
1525
1526   return 1;
1527 }
1528
1529 /* Return TRUE if int I is a valid immediate ARM constant.  */
1530
1531 int
1532 const_ok_for_arm (HOST_WIDE_INT i)
1533 {
1534   int lowbit;
1535   
1536   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1537      be all zero, or all one.  */
1538   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1539       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1540           != ((~(unsigned HOST_WIDE_INT) 0)
1541               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1542     return FALSE;
1543
1544   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
1545   
1546   /* Fast return for 0 and small values.  We must do this for zero, since
1547      the code below can't handle that one case.  */
1548   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
1549     return TRUE;
1550
1551   /* Get the number of trailing zeros, rounded down to the nearest even
1552      number.  */
1553   lowbit = (ffs ((int) i) - 1) & ~1;
1554
1555   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1556     return TRUE;
1557   else if (lowbit <= 4
1558            && ((i & ~0xc000003f) == 0
1559                || (i & ~0xf000000f) == 0
1560                || (i & ~0xfc000003) == 0))
1561     return TRUE;
1562
1563   return FALSE;
1564 }
1565
1566 /* Return true if I is a valid constant for the operation CODE.  */
1567 static int
1568 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1569 {
1570   if (const_ok_for_arm (i))
1571     return 1;
1572
1573   switch (code)
1574     {
1575     case PLUS:
1576       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1577
1578     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1579     case XOR:
1580     case IOR:
1581       return 0;
1582
1583     case AND:
1584       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1585
1586     default:
1587       gcc_unreachable ();
1588     }
1589 }
1590
1591 /* Emit a sequence of insns to handle a large constant.
1592    CODE is the code of the operation required, it can be any of SET, PLUS,
1593    IOR, AND, XOR, MINUS;
1594    MODE is the mode in which the operation is being performed;
1595    VAL is the integer to operate on;
1596    SOURCE is the other operand (a register, or a null-pointer for SET);
1597    SUBTARGETS means it is safe to create scratch registers if that will
1598    either produce a simpler sequence, or we will want to cse the values.
1599    Return value is the number of insns emitted.  */
1600
1601 int
1602 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1603                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1604 {
1605   rtx cond;
1606
1607   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1608     cond = COND_EXEC_TEST (PATTERN (insn));
1609   else
1610     cond = NULL_RTX;
1611
1612   if (subtargets || code == SET
1613       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1614           && REGNO (target) != REGNO (source)))
1615     {
1616       /* After arm_reorg has been called, we can't fix up expensive
1617          constants by pushing them into memory so we must synthesize
1618          them in-line, regardless of the cost.  This is only likely to
1619          be more costly on chips that have load delay slots and we are
1620          compiling without running the scheduler (so no splitting
1621          occurred before the final instruction emission).
1622
1623          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1624       */
1625       if (!after_arm_reorg
1626           && !cond
1627           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1628                                 1, 0)
1629               > arm_constant_limit + (code != SET)))
1630         {
1631           if (code == SET)
1632             {
1633               /* Currently SET is the only monadic value for CODE, all
1634                  the rest are diadic.  */
1635               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1636               return 1;
1637             }
1638           else
1639             {
1640               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1641
1642               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1643               /* For MINUS, the value is subtracted from, since we never
1644                  have subtraction of a constant.  */
1645               if (code == MINUS)
1646                 emit_insn (gen_rtx_SET (VOIDmode, target,
1647                                         gen_rtx_MINUS (mode, temp, source)));
1648               else
1649                 emit_insn (gen_rtx_SET (VOIDmode, target,
1650                                         gen_rtx_fmt_ee (code, mode, source, temp)));
1651               return 2;
1652             }
1653         }
1654     }
1655
1656   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1657                            1);
1658 }
1659
1660 static int
1661 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1662 {
1663   HOST_WIDE_INT temp1;
1664   int num_insns = 0;
1665   do
1666     {
1667       int end;
1668
1669       if (i <= 0)
1670         i += 32;
1671       if (remainder & (3 << (i - 2)))
1672         {
1673           end = i - 8;
1674           if (end < 0)
1675             end += 32;
1676           temp1 = remainder & ((0x0ff << end)
1677                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1678           remainder &= ~temp1;
1679           num_insns++;
1680           i -= 6;
1681         }
1682       i -= 2;
1683     } while (remainder);
1684   return num_insns;
1685 }
1686
1687 /* Emit an instruction with the indicated PATTERN.  If COND is
1688    non-NULL, conditionalize the execution of the instruction on COND
1689    being true.  */
1690
1691 static void
1692 emit_constant_insn (rtx cond, rtx pattern)
1693 {
1694   if (cond)
1695     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1696   emit_insn (pattern);
1697 }
1698
1699 /* As above, but extra parameter GENERATE which, if clear, suppresses
1700    RTL generation.  */
1701
1702 static int
1703 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1704                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1705                   int generate)
1706 {
1707   int can_invert = 0;
1708   int can_negate = 0;
1709   int can_negate_initial = 0;
1710   int can_shift = 0;
1711   int i;
1712   int num_bits_set = 0;
1713   int set_sign_bit_copies = 0;
1714   int clear_sign_bit_copies = 0;
1715   int clear_zero_bit_copies = 0;
1716   int set_zero_bit_copies = 0;
1717   int insns = 0;
1718   unsigned HOST_WIDE_INT temp1, temp2;
1719   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1720
1721   /* Find out which operations are safe for a given CODE.  Also do a quick
1722      check for degenerate cases; these can occur when DImode operations
1723      are split.  */
1724   switch (code)
1725     {
1726     case SET:
1727       can_invert = 1;
1728       can_shift = 1;
1729       can_negate = 1;
1730       break;
1731
1732     case PLUS:
1733       can_negate = 1;
1734       can_negate_initial = 1;
1735       break;
1736
1737     case IOR:
1738       if (remainder == 0xffffffff)
1739         {
1740           if (generate)
1741             emit_constant_insn (cond,
1742                                 gen_rtx_SET (VOIDmode, target,
1743                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1744           return 1;
1745         }
1746       if (remainder == 0)
1747         {
1748           if (reload_completed && rtx_equal_p (target, source))
1749             return 0;
1750           if (generate)
1751             emit_constant_insn (cond,
1752                                 gen_rtx_SET (VOIDmode, target, source));
1753           return 1;
1754         }
1755       break;
1756
1757     case AND:
1758       if (remainder == 0)
1759         {
1760           if (generate)
1761             emit_constant_insn (cond,
1762                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1763           return 1;
1764         }
1765       if (remainder == 0xffffffff)
1766         {
1767           if (reload_completed && rtx_equal_p (target, source))
1768             return 0;
1769           if (generate)
1770             emit_constant_insn (cond,
1771                                 gen_rtx_SET (VOIDmode, target, source));
1772           return 1;
1773         }
1774       can_invert = 1;
1775       break;
1776
1777     case XOR:
1778       if (remainder == 0)
1779         {
1780           if (reload_completed && rtx_equal_p (target, source))
1781             return 0;
1782           if (generate)
1783             emit_constant_insn (cond,
1784                                 gen_rtx_SET (VOIDmode, target, source));
1785           return 1;
1786         }
1787       
1788       /* We don't know how to handle other cases yet.  */
1789       gcc_assert (remainder == 0xffffffff);
1790       
1791       if (generate)
1792         emit_constant_insn (cond,
1793                             gen_rtx_SET (VOIDmode, target,
1794                                          gen_rtx_NOT (mode, source)));
1795       return 1;
1796
1797     case MINUS:
1798       /* We treat MINUS as (val - source), since (source - val) is always
1799          passed as (source + (-val)).  */
1800       if (remainder == 0)
1801         {
1802           if (generate)
1803             emit_constant_insn (cond,
1804                                 gen_rtx_SET (VOIDmode, target,
1805                                              gen_rtx_NEG (mode, source)));
1806           return 1;
1807         }
1808       if (const_ok_for_arm (val))
1809         {
1810           if (generate)
1811             emit_constant_insn (cond,
1812                                 gen_rtx_SET (VOIDmode, target,
1813                                              gen_rtx_MINUS (mode, GEN_INT (val),
1814                                                             source)));
1815           return 1;
1816         }
1817       can_negate = 1;
1818
1819       break;
1820
1821     default:
1822       gcc_unreachable ();
1823     }
1824
1825   /* If we can do it in one insn get out quickly.  */
1826   if (const_ok_for_arm (val)
1827       || (can_negate_initial && const_ok_for_arm (-val))
1828       || (can_invert && const_ok_for_arm (~val)))
1829     {
1830       if (generate)
1831         emit_constant_insn (cond,
1832                             gen_rtx_SET (VOIDmode, target,
1833                                          (source
1834                                           ? gen_rtx_fmt_ee (code, mode, source,
1835                                                             GEN_INT (val))
1836                                           : GEN_INT (val))));
1837       return 1;
1838     }
1839
1840   /* Calculate a few attributes that may be useful for specific
1841      optimizations.  */
1842   for (i = 31; i >= 0; i--)
1843     {
1844       if ((remainder & (1 << i)) == 0)
1845         clear_sign_bit_copies++;
1846       else
1847         break;
1848     }
1849
1850   for (i = 31; i >= 0; i--)
1851     {
1852       if ((remainder & (1 << i)) != 0)
1853         set_sign_bit_copies++;
1854       else
1855         break;
1856     }
1857
1858   for (i = 0; i <= 31; i++)
1859     {
1860       if ((remainder & (1 << i)) == 0)
1861         clear_zero_bit_copies++;
1862       else
1863         break;
1864     }
1865
1866   for (i = 0; i <= 31; i++)
1867     {
1868       if ((remainder & (1 << i)) != 0)
1869         set_zero_bit_copies++;
1870       else
1871         break;
1872     }
1873
1874   switch (code)
1875     {
1876     case SET:
1877       /* See if we can do this by sign_extending a constant that is known
1878          to be negative.  This is a good, way of doing it, since the shift
1879          may well merge into a subsequent insn.  */
1880       if (set_sign_bit_copies > 1)
1881         {
1882           if (const_ok_for_arm
1883               (temp1 = ARM_SIGN_EXTEND (remainder
1884                                         << (set_sign_bit_copies - 1))))
1885             {
1886               if (generate)
1887                 {
1888                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1889                   emit_constant_insn (cond,
1890                                       gen_rtx_SET (VOIDmode, new_src,
1891                                                    GEN_INT (temp1)));
1892                   emit_constant_insn (cond,
1893                                       gen_ashrsi3 (target, new_src,
1894                                                    GEN_INT (set_sign_bit_copies - 1)));
1895                 }
1896               return 2;
1897             }
1898           /* For an inverted constant, we will need to set the low bits,
1899              these will be shifted out of harm's way.  */
1900           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1901           if (const_ok_for_arm (~temp1))
1902             {
1903               if (generate)
1904                 {
1905                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1906                   emit_constant_insn (cond,
1907                                       gen_rtx_SET (VOIDmode, new_src,
1908                                                    GEN_INT (temp1)));
1909                   emit_constant_insn (cond,
1910                                       gen_ashrsi3 (target, new_src,
1911                                                    GEN_INT (set_sign_bit_copies - 1)));
1912                 }
1913               return 2;
1914             }
1915         }
1916
1917       /* See if we can calculate the value as the difference between two
1918          valid immediates.  */
1919       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
1920         {
1921           int topshift = clear_sign_bit_copies & ~1;
1922
1923           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
1924                                    & (0xff000000 >> topshift));
1925
1926           /* If temp1 is zero, then that means the 9 most significant
1927              bits of remainder were 1 and we've caused it to overflow.
1928              When topshift is 0 we don't need to do anything since we
1929              can borrow from 'bit 32'.  */
1930           if (temp1 == 0 && topshift != 0)
1931             temp1 = 0x80000000 >> (topshift - 1);
1932
1933           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
1934           
1935           if (const_ok_for_arm (temp2))
1936             {
1937               if (generate)
1938                 {
1939                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1940                   emit_constant_insn (cond,
1941                                       gen_rtx_SET (VOIDmode, new_src,
1942                                                    GEN_INT (temp1)));
1943                   emit_constant_insn (cond,
1944                                       gen_addsi3 (target, new_src,
1945                                                   GEN_INT (-temp2)));
1946                 }
1947
1948               return 2;
1949             }
1950         }
1951
1952       /* See if we can generate this by setting the bottom (or the top)
1953          16 bits, and then shifting these into the other half of the
1954          word.  We only look for the simplest cases, to do more would cost
1955          too much.  Be careful, however, not to generate this when the
1956          alternative would take fewer insns.  */
1957       if (val & 0xffff0000)
1958         {
1959           temp1 = remainder & 0xffff0000;
1960           temp2 = remainder & 0x0000ffff;
1961
1962           /* Overlaps outside this range are best done using other methods.  */
1963           for (i = 9; i < 24; i++)
1964             {
1965               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1966                   && !const_ok_for_arm (temp2))
1967                 {
1968                   rtx new_src = (subtargets
1969                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1970                                  : target);
1971                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1972                                             source, subtargets, generate);
1973                   source = new_src;
1974                   if (generate)
1975                     emit_constant_insn
1976                       (cond,
1977                        gen_rtx_SET
1978                        (VOIDmode, target,
1979                         gen_rtx_IOR (mode,
1980                                      gen_rtx_ASHIFT (mode, source,
1981                                                      GEN_INT (i)),
1982                                      source)));
1983                   return insns + 1;
1984                 }
1985             }
1986
1987           /* Don't duplicate cases already considered.  */
1988           for (i = 17; i < 24; i++)
1989             {
1990               if (((temp1 | (temp1 >> i)) == remainder)
1991                   && !const_ok_for_arm (temp1))
1992                 {
1993                   rtx new_src = (subtargets
1994                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1995                                  : target);
1996                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1997                                             source, subtargets, generate);
1998                   source = new_src;
1999                   if (generate)
2000                     emit_constant_insn
2001                       (cond,
2002                        gen_rtx_SET (VOIDmode, target,
2003                                     gen_rtx_IOR
2004                                     (mode,
2005                                      gen_rtx_LSHIFTRT (mode, source,
2006                                                        GEN_INT (i)),
2007                                      source)));
2008                   return insns + 1;
2009                 }
2010             }
2011         }
2012       break;
2013
2014     case IOR:
2015     case XOR:
2016       /* If we have IOR or XOR, and the constant can be loaded in a
2017          single instruction, and we can find a temporary to put it in,
2018          then this can be done in two instructions instead of 3-4.  */
2019       if (subtargets
2020           /* TARGET can't be NULL if SUBTARGETS is 0 */
2021           || (reload_completed && !reg_mentioned_p (target, source)))
2022         {
2023           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2024             {
2025               if (generate)
2026                 {
2027                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2028
2029                   emit_constant_insn (cond,
2030                                       gen_rtx_SET (VOIDmode, sub,
2031                                                    GEN_INT (val)));
2032                   emit_constant_insn (cond,
2033                                       gen_rtx_SET (VOIDmode, target,
2034                                                    gen_rtx_fmt_ee (code, mode,
2035                                                                    source, sub)));
2036                 }
2037               return 2;
2038             }
2039         }
2040
2041       if (code == XOR)
2042         break;
2043
2044       if (set_sign_bit_copies > 8
2045           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2046         {
2047           if (generate)
2048             {
2049               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2050               rtx shift = GEN_INT (set_sign_bit_copies);
2051
2052               emit_constant_insn
2053                 (cond,
2054                  gen_rtx_SET (VOIDmode, sub,
2055                               gen_rtx_NOT (mode,
2056                                            gen_rtx_ASHIFT (mode,
2057                                                            source,
2058                                                            shift))));
2059               emit_constant_insn
2060                 (cond,
2061                  gen_rtx_SET (VOIDmode, target,
2062                               gen_rtx_NOT (mode,
2063                                            gen_rtx_LSHIFTRT (mode, sub,
2064                                                              shift))));
2065             }
2066           return 2;
2067         }
2068
2069       if (set_zero_bit_copies > 8
2070           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2071         {
2072           if (generate)
2073             {
2074               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2075               rtx shift = GEN_INT (set_zero_bit_copies);
2076
2077               emit_constant_insn
2078                 (cond,
2079                  gen_rtx_SET (VOIDmode, sub,
2080                               gen_rtx_NOT (mode,
2081                                            gen_rtx_LSHIFTRT (mode,
2082                                                              source,
2083                                                              shift))));
2084               emit_constant_insn
2085                 (cond,
2086                  gen_rtx_SET (VOIDmode, target,
2087                               gen_rtx_NOT (mode,
2088                                            gen_rtx_ASHIFT (mode, sub,
2089                                                            shift))));
2090             }
2091           return 2;
2092         }
2093
2094       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2095         {
2096           if (generate)
2097             {
2098               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2099               emit_constant_insn (cond,
2100                                   gen_rtx_SET (VOIDmode, sub,
2101                                                gen_rtx_NOT (mode, source)));
2102               source = sub;
2103               if (subtargets)
2104                 sub = gen_reg_rtx (mode);
2105               emit_constant_insn (cond,
2106                                   gen_rtx_SET (VOIDmode, sub,
2107                                                gen_rtx_AND (mode, source,
2108                                                             GEN_INT (temp1))));
2109               emit_constant_insn (cond,
2110                                   gen_rtx_SET (VOIDmode, target,
2111                                                gen_rtx_NOT (mode, sub)));
2112             }
2113           return 3;
2114         }
2115       break;
2116
2117     case AND:
2118       /* See if two shifts will do 2 or more insn's worth of work.  */
2119       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2120         {
2121           HOST_WIDE_INT shift_mask = ((0xffffffff
2122                                        << (32 - clear_sign_bit_copies))
2123                                       & 0xffffffff);
2124
2125           if ((remainder | shift_mask) != 0xffffffff)
2126             {
2127               if (generate)
2128                 {
2129                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2130                   insns = arm_gen_constant (AND, mode, cond,
2131                                             remainder | shift_mask,
2132                                             new_src, source, subtargets, 1);
2133                   source = new_src;
2134                 }
2135               else
2136                 {
2137                   rtx targ = subtargets ? NULL_RTX : target;
2138                   insns = arm_gen_constant (AND, mode, cond,
2139                                             remainder | shift_mask,
2140                                             targ, source, subtargets, 0);
2141                 }
2142             }
2143
2144           if (generate)
2145             {
2146               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2147               rtx shift = GEN_INT (clear_sign_bit_copies);
2148
2149               emit_insn (gen_ashlsi3 (new_src, source, shift));
2150               emit_insn (gen_lshrsi3 (target, new_src, shift));
2151             }
2152
2153           return insns + 2;
2154         }
2155
2156       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2157         {
2158           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2159
2160           if ((remainder | shift_mask) != 0xffffffff)
2161             {
2162               if (generate)
2163                 {
2164                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2165
2166                   insns = arm_gen_constant (AND, mode, cond,
2167                                             remainder | shift_mask,
2168                                             new_src, source, subtargets, 1);
2169                   source = new_src;
2170                 }
2171               else
2172                 {
2173                   rtx targ = subtargets ? NULL_RTX : target;
2174
2175                   insns = arm_gen_constant (AND, mode, cond,
2176                                             remainder | shift_mask,
2177                                             targ, source, subtargets, 0);
2178                 }
2179             }
2180
2181           if (generate)
2182             {
2183               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2184               rtx shift = GEN_INT (clear_zero_bit_copies);
2185
2186               emit_insn (gen_lshrsi3 (new_src, source, shift));
2187               emit_insn (gen_ashlsi3 (target, new_src, shift));
2188             }
2189
2190           return insns + 2;
2191         }
2192
2193       break;
2194
2195     default:
2196       break;
2197     }
2198
2199   for (i = 0; i < 32; i++)
2200     if (remainder & (1 << i))
2201       num_bits_set++;
2202
2203   if (code == AND || (can_invert && num_bits_set > 16))
2204     remainder = (~remainder) & 0xffffffff;
2205   else if (code == PLUS && num_bits_set > 16)
2206     remainder = (-remainder) & 0xffffffff;
2207   else
2208     {
2209       can_invert = 0;
2210       can_negate = 0;
2211     }
2212
2213   /* Now try and find a way of doing the job in either two or three
2214      instructions.
2215      We start by looking for the largest block of zeros that are aligned on
2216      a 2-bit boundary, we then fill up the temps, wrapping around to the
2217      top of the word when we drop off the bottom.
2218      In the worst case this code should produce no more than four insns.  */
2219   {
2220     int best_start = 0;
2221     int best_consecutive_zeros = 0;
2222
2223     for (i = 0; i < 32; i += 2)
2224       {
2225         int consecutive_zeros = 0;
2226
2227         if (!(remainder & (3 << i)))
2228           {
2229             while ((i < 32) && !(remainder & (3 << i)))
2230               {
2231                 consecutive_zeros += 2;
2232                 i += 2;
2233               }
2234             if (consecutive_zeros > best_consecutive_zeros)
2235               {
2236                 best_consecutive_zeros = consecutive_zeros;
2237                 best_start = i - consecutive_zeros;
2238               }
2239             i -= 2;
2240           }
2241       }
2242
2243     /* So long as it won't require any more insns to do so, it's
2244        desirable to emit a small constant (in bits 0...9) in the last
2245        insn.  This way there is more chance that it can be combined with
2246        a later addressing insn to form a pre-indexed load or store
2247        operation.  Consider:
2248
2249                *((volatile int *)0xe0000100) = 1;
2250                *((volatile int *)0xe0000110) = 2;
2251
2252        We want this to wind up as:
2253
2254                 mov rA, #0xe0000000
2255                 mov rB, #1
2256                 str rB, [rA, #0x100]
2257                 mov rB, #2
2258                 str rB, [rA, #0x110]
2259
2260        rather than having to synthesize both large constants from scratch.
2261
2262        Therefore, we calculate how many insns would be required to emit
2263        the constant starting from `best_start', and also starting from
2264        zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2265        yield a shorter sequence, we may as well use zero.  */
2266     if (best_start != 0
2267         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2268         && (count_insns_for_constant (remainder, 0) <=
2269             count_insns_for_constant (remainder, best_start)))
2270       best_start = 0;
2271
2272     /* Now start emitting the insns.  */
2273     i = best_start;
2274     do
2275       {
2276         int end;
2277
2278         if (i <= 0)
2279           i += 32;
2280         if (remainder & (3 << (i - 2)))
2281           {
2282             end = i - 8;
2283             if (end < 0)
2284               end += 32;
2285             temp1 = remainder & ((0x0ff << end)
2286                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2287             remainder &= ~temp1;
2288
2289             if (generate)
2290               {
2291                 rtx new_src, temp1_rtx;
2292
2293                 if (code == SET || code == MINUS)
2294                   {
2295                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2296                     if (can_invert && code != MINUS)
2297                       temp1 = ~temp1;
2298                   }
2299                 else
2300                   {
2301                     if (remainder && subtargets)
2302                       new_src = gen_reg_rtx (mode);
2303                     else
2304                       new_src = target;
2305                     if (can_invert)
2306                       temp1 = ~temp1;
2307                     else if (can_negate)
2308                       temp1 = -temp1;
2309                   }
2310
2311                 temp1 = trunc_int_for_mode (temp1, mode);
2312                 temp1_rtx = GEN_INT (temp1);
2313
2314                 if (code == SET)
2315                   ;
2316                 else if (code == MINUS)
2317                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2318                 else
2319                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2320
2321                 emit_constant_insn (cond,
2322                                     gen_rtx_SET (VOIDmode, new_src,
2323                                                  temp1_rtx));
2324                 source = new_src;
2325               }
2326
2327             if (code == SET)
2328               {
2329                 can_invert = 0;
2330                 code = PLUS;
2331               }
2332             else if (code == MINUS)
2333               code = PLUS;
2334
2335             insns++;
2336             i -= 6;
2337           }
2338         i -= 2;
2339       }
2340     while (remainder);
2341   }
2342
2343   return insns;
2344 }
2345
2346 /* Canonicalize a comparison so that we are more likely to recognize it.
2347    This can be done for a few constant compares, where we can make the
2348    immediate value easier to load.  */
2349
2350 enum rtx_code
2351 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2352 {
2353   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2354
2355   switch (code)
2356     {
2357     case EQ:
2358     case NE:
2359       return code;
2360
2361     case GT:
2362     case LE:
2363       if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2364           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2365         {
2366           *op1 = GEN_INT (i + 1);
2367           return code == GT ? GE : LT;
2368         }
2369       break;
2370
2371     case GE:
2372     case LT:
2373       if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2374           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2375         {
2376           *op1 = GEN_INT (i - 1);
2377           return code == GE ? GT : LE;
2378         }
2379       break;
2380
2381     case GTU:
2382     case LEU:
2383       if (i != ~((unsigned HOST_WIDE_INT) 0)
2384           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2385         {
2386           *op1 = GEN_INT (i + 1);
2387           return code == GTU ? GEU : LTU;
2388         }
2389       break;
2390
2391     case GEU:
2392     case LTU:
2393       if (i != 0
2394           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2395         {
2396           *op1 = GEN_INT (i - 1);
2397           return code == GEU ? GTU : LEU;
2398         }
2399       break;
2400
2401     default:
2402       gcc_unreachable ();
2403     }
2404
2405   return code;
2406 }
2407
2408
2409 /* Define how to find the value returned by a function.  */
2410
2411 rtx
2412 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2413 {
2414   enum machine_mode mode;
2415   int unsignedp ATTRIBUTE_UNUSED;
2416   rtx r ATTRIBUTE_UNUSED;
2417
2418   mode = TYPE_MODE (type);
2419   /* Promote integer types.  */
2420   if (INTEGRAL_TYPE_P (type))
2421     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2422
2423   /* Promotes small structs returned in a register to full-word size
2424      for big-endian AAPCS.  */
2425   if (arm_return_in_msb (type))
2426     {
2427       HOST_WIDE_INT size = int_size_in_bytes (type);
2428       if (size % UNITS_PER_WORD != 0)
2429         {
2430           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2431           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2432         }
2433     }
2434   
2435   return LIBCALL_VALUE(mode);
2436 }
2437
2438 /* Determine the amount of memory needed to store the possible return 
2439    registers of an untyped call.  */
2440 int
2441 arm_apply_result_size (void)
2442 {
2443   int size = 16;
2444
2445   if (TARGET_ARM)
2446     {
2447       if (TARGET_HARD_FLOAT_ABI)
2448         {
2449           if (TARGET_FPA)
2450             size += 12;
2451           if (TARGET_MAVERICK)
2452             size += 8;
2453         }
2454       if (TARGET_IWMMXT_ABI)
2455         size += 8;
2456     }
2457
2458   return size;
2459 }
2460
2461 /* Decide whether a type should be returned in memory (true)
2462    or in a register (false).  This is called by the macro
2463    RETURN_IN_MEMORY.  */
2464 int
2465 arm_return_in_memory (tree type)
2466 {
2467   HOST_WIDE_INT size;
2468
2469   if (!AGGREGATE_TYPE_P (type) &&
2470       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2471     /* All simple types are returned in registers.
2472        For AAPCS, complex types are treated the same as aggregates.  */
2473     return 0;
2474
2475   size = int_size_in_bytes (type);
2476
2477   if (arm_abi != ARM_ABI_APCS)
2478     {
2479       /* ATPCS and later return aggregate types in memory only if they are
2480          larger than a word (or are variable size).  */
2481       return (size < 0 || size > UNITS_PER_WORD);
2482     }
2483
2484   /* For the arm-wince targets we choose to be compatible with Microsoft's
2485      ARM and Thumb compilers, which always return aggregates in memory.  */
2486 #ifndef ARM_WINCE
2487   /* All structures/unions bigger than one word are returned in memory.
2488      Also catch the case where int_size_in_bytes returns -1.  In this case
2489      the aggregate is either huge or of variable size, and in either case
2490      we will want to return it via memory and not in a register.  */
2491   if (size < 0 || size > UNITS_PER_WORD)
2492     return 1;
2493
2494   if (TREE_CODE (type) == RECORD_TYPE)
2495     {
2496       tree field;
2497
2498       /* For a struct the APCS says that we only return in a register
2499          if the type is 'integer like' and every addressable element
2500          has an offset of zero.  For practical purposes this means
2501          that the structure can have at most one non bit-field element
2502          and that this element must be the first one in the structure.  */
2503
2504       /* Find the first field, ignoring non FIELD_DECL things which will
2505          have been created by C++.  */
2506       for (field = TYPE_FIELDS (type);
2507            field && TREE_CODE (field) != FIELD_DECL;
2508            field = TREE_CHAIN (field))
2509         continue;
2510
2511       if (field == NULL)
2512         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2513
2514       /* Check that the first field is valid for returning in a register.  */
2515
2516       /* ... Floats are not allowed */
2517       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2518         return 1;
2519
2520       /* ... Aggregates that are not themselves valid for returning in
2521          a register are not allowed.  */
2522       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2523         return 1;
2524
2525       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2526          since they are not addressable.  */
2527       for (field = TREE_CHAIN (field);
2528            field;
2529            field = TREE_CHAIN (field))
2530         {
2531           if (TREE_CODE (field) != FIELD_DECL)
2532             continue;
2533
2534           if (!DECL_BIT_FIELD_TYPE (field))
2535             return 1;
2536         }
2537
2538       return 0;
2539     }
2540
2541   if (TREE_CODE (type) == UNION_TYPE)
2542     {
2543       tree field;
2544
2545       /* Unions can be returned in registers if every element is
2546          integral, or can be returned in an integer register.  */
2547       for (field = TYPE_FIELDS (type);
2548            field;
2549            field = TREE_CHAIN (field))
2550         {
2551           if (TREE_CODE (field) != FIELD_DECL)
2552             continue;
2553
2554           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2555             return 1;
2556
2557           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2558             return 1;
2559         }
2560
2561       return 0;
2562     }
2563 #endif /* not ARM_WINCE */
2564
2565   /* Return all other types in memory.  */
2566   return 1;
2567 }
2568
2569 /* Indicate whether or not words of a double are in big-endian order.  */
2570
2571 int
2572 arm_float_words_big_endian (void)
2573 {
2574   if (TARGET_MAVERICK)
2575     return 0;
2576
2577   /* For FPA, float words are always big-endian.  For VFP, floats words
2578      follow the memory system mode.  */
2579
2580   if (TARGET_FPA)
2581     {
2582       return 1;
2583     }
2584
2585   if (TARGET_VFP)
2586     return (TARGET_BIG_END ? 1 : 0);
2587
2588   return 1;
2589 }
2590
2591 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2592    for a call to a function whose data type is FNTYPE.
2593    For a library call, FNTYPE is NULL.  */
2594 void
2595 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2596                           rtx libname  ATTRIBUTE_UNUSED,
2597                           tree fndecl ATTRIBUTE_UNUSED)
2598 {
2599   /* On the ARM, the offset starts at 0.  */
2600   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2601   pcum->iwmmxt_nregs = 0;
2602   pcum->can_split = true;
2603
2604   pcum->call_cookie = CALL_NORMAL;
2605
2606   if (TARGET_LONG_CALLS)
2607     pcum->call_cookie = CALL_LONG;
2608
2609   /* Check for long call/short call attributes.  The attributes
2610      override any command line option.  */
2611   if (fntype)
2612     {
2613       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2614         pcum->call_cookie = CALL_SHORT;
2615       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2616         pcum->call_cookie = CALL_LONG;
2617     }
2618
2619   /* Varargs vectors are treated the same as long long.
2620      named_count avoids having to change the way arm handles 'named' */
2621   pcum->named_count = 0;
2622   pcum->nargs = 0;
2623
2624   if (TARGET_REALLY_IWMMXT && fntype)
2625     {
2626       tree fn_arg;
2627
2628       for (fn_arg = TYPE_ARG_TYPES (fntype);
2629            fn_arg;
2630            fn_arg = TREE_CHAIN (fn_arg))
2631         pcum->named_count += 1;
2632
2633       if (! pcum->named_count)
2634         pcum->named_count = INT_MAX;
2635     }
2636 }
2637
2638
2639 /* Return true if mode/type need doubleword alignment.  */
2640 bool
2641 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2642 {
2643   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2644           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2645 }
2646
2647
2648 /* Determine where to put an argument to a function.
2649    Value is zero to push the argument on the stack,
2650    or a hard register in which to store the argument.
2651
2652    MODE is the argument's machine mode.
2653    TYPE is the data type of the argument (as a tree).
2654     This is null for libcalls where that information may
2655     not be available.
2656    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2657     the preceding args and about the function being called.
2658    NAMED is nonzero if this argument is a named parameter
2659     (otherwise it is an extra parameter matching an ellipsis).  */
2660
2661 rtx
2662 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2663                   tree type, int named)
2664 {
2665   int nregs;
2666
2667   /* Varargs vectors are treated the same as long long.
2668      named_count avoids having to change the way arm handles 'named' */
2669   if (TARGET_IWMMXT_ABI
2670       && arm_vector_mode_supported_p (mode)
2671       && pcum->named_count > pcum->nargs + 1)
2672     {
2673       if (pcum->iwmmxt_nregs <= 9)
2674         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2675       else
2676         {
2677           pcum->can_split = false;
2678           return NULL_RTX;
2679         }
2680     }
2681
2682   /* Put doubleword aligned quantities in even register pairs.  */
2683   if (pcum->nregs & 1
2684       && ARM_DOUBLEWORD_ALIGN
2685       && arm_needs_doubleword_align (mode, type))
2686     pcum->nregs++;
2687
2688   if (mode == VOIDmode)
2689     /* Compute operand 2 of the call insn.  */
2690     return GEN_INT (pcum->call_cookie);
2691
2692   /* Only allow splitting an arg between regs and memory if all preceding
2693      args were allocated to regs.  For args passed by reference we only count
2694      the reference pointer.  */
2695   if (pcum->can_split)
2696     nregs = 1;
2697   else
2698     nregs = ARM_NUM_REGS2 (mode, type);
2699
2700   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2701     return NULL_RTX;
2702
2703   return gen_rtx_REG (mode, pcum->nregs);
2704 }
2705
2706 static int
2707 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2708                        tree type, bool named ATTRIBUTE_UNUSED)
2709 {
2710   int nregs = pcum->nregs;
2711
2712   if (arm_vector_mode_supported_p (mode))
2713     return 0;
2714
2715   if (NUM_ARG_REGS > nregs
2716       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2717       && pcum->can_split)
2718     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2719
2720   return 0;
2721 }
2722
2723 /* Variable sized types are passed by reference.  This is a GCC
2724    extension to the ARM ABI.  */
2725
2726 static bool
2727 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2728                        enum machine_mode mode ATTRIBUTE_UNUSED,
2729                        tree type, bool named ATTRIBUTE_UNUSED)
2730 {
2731   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2732 }
2733 \f
2734 /* Encode the current state of the #pragma [no_]long_calls.  */
2735 typedef enum
2736 {
2737   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2738   LONG,         /* #pragma long_calls is in effect.  */
2739   SHORT         /* #pragma no_long_calls is in effect.  */
2740 } arm_pragma_enum;
2741
2742 static arm_pragma_enum arm_pragma_long_calls = OFF;
2743
2744 void
2745 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2746 {
2747   arm_pragma_long_calls = LONG;
2748 }
2749
2750 void
2751 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2752 {
2753   arm_pragma_long_calls = SHORT;
2754 }
2755
2756 void
2757 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2758 {
2759   arm_pragma_long_calls = OFF;
2760 }
2761 \f
2762 /* Table of machine attributes.  */
2763 const struct attribute_spec arm_attribute_table[] =
2764 {
2765   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2766   /* Function calls made to this symbol must be done indirectly, because
2767      it may lie outside of the 26 bit addressing range of a normal function
2768      call.  */
2769   { "long_call",    0, 0, false, true,  true,  NULL },
2770   /* Whereas these functions are always known to reside within the 26 bit
2771      addressing range.  */
2772   { "short_call",   0, 0, false, true,  true,  NULL },
2773   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
2774   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2775   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2776   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2777 #ifdef ARM_PE
2778   /* ARM/PE has three new attributes:
2779      interfacearm - ?
2780      dllexport - for exporting a function/variable that will live in a dll
2781      dllimport - for importing a function/variable from a dll
2782
2783      Microsoft allows multiple declspecs in one __declspec, separating
2784      them with spaces.  We do NOT support this.  Instead, use __declspec
2785      multiple times.
2786   */
2787   { "dllimport",    0, 0, true,  false, false, NULL },
2788   { "dllexport",    0, 0, true,  false, false, NULL },
2789   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2790 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2791   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
2792   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
2793   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
2794 #endif
2795   { NULL,           0, 0, false, false, false, NULL }
2796 };
2797
2798 /* Handle an attribute requiring a FUNCTION_DECL;
2799    arguments as in struct attribute_spec.handler.  */
2800 static tree
2801 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2802                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2803 {
2804   if (TREE_CODE (*node) != FUNCTION_DECL)
2805     {
2806       warning (0, "%qs attribute only applies to functions",
2807                IDENTIFIER_POINTER (name));
2808       *no_add_attrs = true;
2809     }
2810
2811   return NULL_TREE;
2812 }
2813
2814 /* Handle an "interrupt" or "isr" attribute;
2815    arguments as in struct attribute_spec.handler.  */
2816 static tree
2817 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2818                           bool *no_add_attrs)
2819 {
2820   if (DECL_P (*node))
2821     {
2822       if (TREE_CODE (*node) != FUNCTION_DECL)
2823         {
2824           warning (0, "%qs attribute only applies to functions",
2825                    IDENTIFIER_POINTER (name));
2826           *no_add_attrs = true;
2827         }
2828       /* FIXME: the argument if any is checked for type attributes;
2829          should it be checked for decl ones?  */
2830     }
2831   else
2832     {
2833       if (TREE_CODE (*node) == FUNCTION_TYPE
2834           || TREE_CODE (*node) == METHOD_TYPE)
2835         {
2836           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2837             {
2838               warning (0, "%qs attribute ignored", IDENTIFIER_POINTER (name));
2839               *no_add_attrs = true;
2840             }
2841         }
2842       else if (TREE_CODE (*node) == POINTER_TYPE
2843                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2844                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2845                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2846         {
2847           *node = build_variant_type_copy (*node);
2848           TREE_TYPE (*node) = build_type_attribute_variant
2849             (TREE_TYPE (*node),
2850              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2851           *no_add_attrs = true;
2852         }
2853       else
2854         {
2855           /* Possibly pass this attribute on from the type to a decl.  */
2856           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2857                        | (int) ATTR_FLAG_FUNCTION_NEXT
2858                        | (int) ATTR_FLAG_ARRAY_NEXT))
2859             {
2860               *no_add_attrs = true;
2861               return tree_cons (name, args, NULL_TREE);
2862             }
2863           else
2864             {
2865               warning (0, "%qs attribute ignored", IDENTIFIER_POINTER (name));
2866             }
2867         }
2868     }
2869
2870   return NULL_TREE;
2871 }
2872
2873 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2874 /* Handle the "notshared" attribute.  This attribute is another way of
2875    requesting hidden visibility.  ARM's compiler supports
2876    "__declspec(notshared)"; we support the same thing via an
2877    attribute.  */
2878
2879 static tree
2880 arm_handle_notshared_attribute (tree *node, 
2881                                 tree name ATTRIBUTE_UNUSED, 
2882                                 tree args ATTRIBUTE_UNUSED, 
2883                                 int flags ATTRIBUTE_UNUSED, 
2884                                 bool *no_add_attrs)
2885 {
2886   tree decl = TYPE_NAME (*node);
2887
2888   if (decl)
2889     {
2890       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2891       DECL_VISIBILITY_SPECIFIED (decl) = 1;
2892       *no_add_attrs = false;
2893     }
2894   return NULL_TREE;
2895 }
2896 #endif
2897
2898 /* Return 0 if the attributes for two types are incompatible, 1 if they
2899    are compatible, and 2 if they are nearly compatible (which causes a
2900    warning to be generated).  */
2901 static int
2902 arm_comp_type_attributes (tree type1, tree type2)
2903 {
2904   int l1, l2, s1, s2;
2905
2906   /* Check for mismatch of non-default calling convention.  */
2907   if (TREE_CODE (type1) != FUNCTION_TYPE)
2908     return 1;
2909
2910   /* Check for mismatched call attributes.  */
2911   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2912   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2913   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2914   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2915
2916   /* Only bother to check if an attribute is defined.  */
2917   if (l1 | l2 | s1 | s2)
2918     {
2919       /* If one type has an attribute, the other must have the same attribute.  */
2920       if ((l1 != l2) || (s1 != s2))
2921         return 0;
2922
2923       /* Disallow mixed attributes.  */
2924       if ((l1 & s2) || (l2 & s1))
2925         return 0;
2926     }
2927
2928   /* Check for mismatched ISR attribute.  */
2929   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2930   if (! l1)
2931     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2932   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2933   if (! l2)
2934     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2935   if (l1 != l2)
2936     return 0;
2937
2938   return 1;
2939 }
2940
2941 /*  Encode long_call or short_call attribute by prefixing
2942     symbol name in DECL with a special character FLAG.  */
2943 void
2944 arm_encode_call_attribute (tree decl, int flag)
2945 {
2946   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2947   int          len = strlen (str);
2948   char *       newstr;
2949
2950   /* Do not allow weak functions to be treated as short call.  */
2951   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2952     return;
2953
2954   newstr = alloca (len + 2);
2955   newstr[0] = flag;
2956   strcpy (newstr + 1, str);
2957
2958   newstr = (char *) ggc_alloc_string (newstr, len + 1);
2959   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2960 }
2961
2962 /*  Assigns default attributes to newly defined type.  This is used to
2963     set short_call/long_call attributes for function types of
2964     functions defined inside corresponding #pragma scopes.  */
2965 static void
2966 arm_set_default_type_attributes (tree type)
2967 {
2968   /* Add __attribute__ ((long_call)) to all functions, when
2969      inside #pragma long_calls or __attribute__ ((short_call)),
2970      when inside #pragma no_long_calls.  */
2971   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2972     {
2973       tree type_attr_list, attr_name;
2974       type_attr_list = TYPE_ATTRIBUTES (type);
2975
2976       if (arm_pragma_long_calls == LONG)
2977         attr_name = get_identifier ("long_call");
2978       else if (arm_pragma_long_calls == SHORT)
2979         attr_name = get_identifier ("short_call");
2980       else
2981         return;
2982
2983       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2984       TYPE_ATTRIBUTES (type) = type_attr_list;
2985     }
2986 }
2987 \f
2988 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2989    defined within the current compilation unit.  If this cannot be
2990    determined, then 0 is returned.  */
2991 static int
2992 current_file_function_operand (rtx sym_ref)
2993 {
2994   /* This is a bit of a fib.  A function will have a short call flag
2995      applied to its name if it has the short call attribute, or it has
2996      already been defined within the current compilation unit.  */
2997   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2998     return 1;
2999
3000   /* The current function is always defined within the current compilation
3001      unit.  If it s a weak definition however, then this may not be the real
3002      definition of the function, and so we have to say no.  */
3003   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
3004       && !DECL_WEAK (current_function_decl))
3005     return 1;
3006
3007   /* We cannot make the determination - default to returning 0.  */
3008   return 0;
3009 }
3010
3011 /* Return nonzero if a 32 bit "long_call" should be generated for
3012    this call.  We generate a long_call if the function:
3013
3014         a.  has an __attribute__((long call))
3015      or b.  is within the scope of a #pragma long_calls
3016      or c.  the -mlong-calls command line switch has been specified
3017          .  and either:
3018                 1. -ffunction-sections is in effect
3019              or 2. the current function has __attribute__ ((section))
3020              or 3. the target function has __attribute__ ((section))
3021
3022    However we do not generate a long call if the function:
3023
3024         d.  has an __attribute__ ((short_call))
3025      or e.  is inside the scope of a #pragma no_long_calls
3026      or f.  is defined within the current compilation unit.
3027
3028    This function will be called by C fragments contained in the machine
3029    description file.  SYM_REF and CALL_COOKIE correspond to the matched
3030    rtl operands.  CALL_SYMBOL is used to distinguish between
3031    two different callers of the function.  It is set to 1 in the
3032    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
3033    and "call_value" patterns.  This is because of the difference in the
3034    SYM_REFs passed by these patterns.  */
3035 int
3036 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
3037 {
3038   if (!call_symbol)
3039     {
3040       if (GET_CODE (sym_ref) != MEM)
3041         return 0;
3042
3043       sym_ref = XEXP (sym_ref, 0);
3044     }
3045
3046   if (GET_CODE (sym_ref) != SYMBOL_REF)
3047     return 0;
3048
3049   if (call_cookie & CALL_SHORT)
3050     return 0;
3051
3052   if (TARGET_LONG_CALLS)
3053     {
3054       if (flag_function_sections
3055           || DECL_SECTION_NAME (current_function_decl))
3056         /* c.3 is handled by the definition of the
3057            ARM_DECLARE_FUNCTION_SIZE macro.  */
3058         return 1;
3059     }
3060
3061   if (current_file_function_operand (sym_ref))
3062     return 0;
3063
3064   return (call_cookie & CALL_LONG)
3065     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
3066     || TARGET_LONG_CALLS;
3067 }
3068
3069 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3070 static bool
3071 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3072 {
3073   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
3074
3075   if (cfun->machine->sibcall_blocked)
3076     return false;
3077
3078   /* Never tailcall something for which we have no decl, or if we
3079      are in Thumb mode.  */
3080   if (decl == NULL || TARGET_THUMB)
3081     return false;
3082
3083   /* Get the calling method.  */
3084   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3085     call_type = CALL_SHORT;
3086   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3087     call_type = CALL_LONG;
3088
3089   /* Cannot tail-call to long calls, since these are out of range of
3090      a branch instruction.  However, if not compiling PIC, we know
3091      we can reach the symbol if it is in this compilation unit.  */
3092   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
3093     return false;
3094
3095   /* If we are interworking and the function is not declared static
3096      then we can't tail-call it unless we know that it exists in this
3097      compilation unit (since it might be a Thumb routine).  */
3098   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3099     return false;
3100
3101   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3102   if (IS_INTERRUPT (arm_current_func_type ()))
3103     return false;
3104
3105   /* Everything else is ok.  */
3106   return true;
3107 }
3108
3109 \f
3110 /* Addressing mode support functions.  */
3111
3112 /* Return nonzero if X is a legitimate immediate operand when compiling
3113    for PIC.  */
3114 int
3115 legitimate_pic_operand_p (rtx x)
3116 {
3117   if (CONSTANT_P (x)
3118       && flag_pic
3119       && (GET_CODE (x) == SYMBOL_REF
3120           || (GET_CODE (x) == CONST
3121               && GET_CODE (XEXP (x, 0)) == PLUS
3122               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
3123     return 0;
3124
3125   return 1;
3126 }
3127
3128 rtx
3129 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3130 {
3131   if (GET_CODE (orig) == SYMBOL_REF
3132       || GET_CODE (orig) == LABEL_REF)
3133     {
3134 #ifndef AOF_ASSEMBLER
3135       rtx pic_ref, address;
3136 #endif
3137       rtx insn;
3138       int subregs = 0;
3139
3140       if (reg == 0)
3141         {
3142           gcc_assert (!no_new_pseudos);
3143           reg = gen_reg_rtx (Pmode);
3144
3145           subregs = 1;
3146         }
3147
3148 #ifdef AOF_ASSEMBLER
3149       /* The AOF assembler can generate relocations for these directly, and
3150          understands that the PIC register has to be added into the offset.  */
3151       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3152 #else
3153       if (subregs)
3154         address = gen_reg_rtx (Pmode);
3155       else
3156         address = reg;
3157
3158       if (TARGET_ARM)
3159         emit_insn (gen_pic_load_addr_arm (address, orig));
3160       else
3161         emit_insn (gen_pic_load_addr_thumb (address, orig));
3162
3163       if ((GET_CODE (orig) == LABEL_REF
3164            || (GET_CODE (orig) == SYMBOL_REF &&
3165                SYMBOL_REF_LOCAL_P (orig)))
3166           && NEED_GOT_RELOC)
3167         pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
3168       else
3169         {
3170           pic_ref = gen_const_mem (Pmode,
3171                                    gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3172                                                  address));
3173         }
3174
3175       insn = emit_move_insn (reg, pic_ref);
3176 #endif
3177       current_function_uses_pic_offset_table = 1;
3178       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3179          by loop.  */
3180       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3181                                             REG_NOTES (insn));
3182       return reg;
3183     }
3184   else if (GET_CODE (orig) == CONST)
3185     {
3186       rtx base, offset;
3187
3188       if (GET_CODE (XEXP (orig, 0)) == PLUS
3189           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3190         return orig;
3191
3192       if (reg == 0)
3193         {
3194           gcc_assert (!no_new_pseudos);
3195           reg = gen_reg_rtx (Pmode);
3196         }
3197
3198       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3199       
3200       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3201       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3202                                        base == reg ? 0 : reg);
3203
3204       if (GET_CODE (offset) == CONST_INT)
3205         {
3206           /* The base register doesn't really matter, we only want to
3207              test the index for the appropriate mode.  */
3208           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3209             {
3210               gcc_assert (!no_new_pseudos);
3211               offset = force_reg (Pmode, offset);
3212             }
3213
3214           if (GET_CODE (offset) == CONST_INT)
3215             return plus_constant (base, INTVAL (offset));
3216         }
3217
3218       if (GET_MODE_SIZE (mode) > 4
3219           && (GET_MODE_CLASS (mode) == MODE_INT
3220               || TARGET_SOFT_FLOAT))
3221         {
3222           emit_insn (gen_addsi3 (reg, base, offset));
3223           return reg;
3224         }
3225
3226       return gen_rtx_PLUS (Pmode, base, offset);
3227     }
3228
3229   return orig;
3230 }
3231
3232
3233 /* Find a spare low register to use during the prolog of a function.  */
3234
3235 static int
3236 thumb_find_work_register (unsigned long pushed_regs_mask)
3237 {
3238   int reg;
3239
3240   /* Check the argument registers first as these are call-used.  The
3241      register allocation order means that sometimes r3 might be used
3242      but earlier argument registers might not, so check them all.  */
3243   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3244     if (!regs_ever_live[reg])
3245       return reg;
3246
3247   /* Before going on to check the call-saved registers we can try a couple
3248      more ways of deducing that r3 is available.  The first is when we are
3249      pushing anonymous arguments onto the stack and we have less than 4
3250      registers worth of fixed arguments(*).  In this case r3 will be part of
3251      the variable argument list and so we can be sure that it will be
3252      pushed right at the start of the function.  Hence it will be available
3253      for the rest of the prologue.
3254      (*): ie current_function_pretend_args_size is greater than 0.  */
3255   if (cfun->machine->uses_anonymous_args
3256       && current_function_pretend_args_size > 0)
3257     return LAST_ARG_REGNUM;
3258
3259   /* The other case is when we have fixed arguments but less than 4 registers
3260      worth.  In this case r3 might be used in the body of the function, but
3261      it is not being used to convey an argument into the function.  In theory
3262      we could just check current_function_args_size to see how many bytes are
3263      being passed in argument registers, but it seems that it is unreliable.
3264      Sometimes it will have the value 0 when in fact arguments are being
3265      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3266      check the args_info.nregs field as well.  The problem with this field is
3267      that it makes no allowances for arguments that are passed to the
3268      function but which are not used.  Hence we could miss an opportunity
3269      when a function has an unused argument in r3.  But it is better to be
3270      safe than to be sorry.  */
3271   if (! cfun->machine->uses_anonymous_args
3272       && current_function_args_size >= 0
3273       && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3274       && cfun->args_info.nregs < 4)
3275     return LAST_ARG_REGNUM;
3276   
3277   /* Otherwise look for a call-saved register that is going to be pushed.  */
3278   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3279     if (pushed_regs_mask & (1 << reg))
3280       return reg;
3281
3282   /* Something went wrong - thumb_compute_save_reg_mask()
3283      should have arranged for a suitable register to be pushed.  */
3284   gcc_unreachable ();
3285 }
3286
3287
3288 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3289    low register.  */
3290
3291 void
3292 arm_load_pic_register (unsigned int scratch)
3293 {
3294 #ifndef AOF_ASSEMBLER
3295   rtx l1, pic_tmp, pic_tmp2, pic_rtx;
3296   rtx global_offset_table;
3297
3298   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3299     return;
3300
3301   gcc_assert (flag_pic);
3302
3303   l1 = gen_label_rtx ();
3304
3305   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3306   /* On the ARM the PC register contains 'dot + 8' at the time of the
3307      addition, on the Thumb it is 'dot + 4'.  */
3308   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
3309   if (GOT_PCREL)
3310     pic_tmp2 = gen_rtx_CONST (VOIDmode,
3311                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3312   else
3313     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3314
3315   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3316
3317   if (TARGET_ARM)
3318     {
3319       emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3320       emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
3321     }
3322   else
3323     {
3324       if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3325         {
3326           /* We will have pushed the pic register, so should always be
3327              able to find a work register.  */
3328           pic_tmp = gen_rtx_REG (SImode, scratch);
3329           emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3330           emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3331         }
3332       else
3333         emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3334       emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
3335     }
3336
3337   /* Need to emit this whether or not we obey regdecls,
3338      since setjmp/longjmp can cause life info to screw up.  */
3339   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3340 #endif /* AOF_ASSEMBLER */
3341 }
3342
3343
3344 /* Return nonzero if X is valid as an ARM state addressing register.  */
3345 static int
3346 arm_address_register_rtx_p (rtx x, int strict_p)
3347 {
3348   int regno;
3349
3350   if (GET_CODE (x) != REG)
3351     return 0;
3352
3353   regno = REGNO (x);
3354
3355   if (strict_p)
3356     return ARM_REGNO_OK_FOR_BASE_P (regno);
3357
3358   return (regno <= LAST_ARM_REGNUM
3359           || regno >= FIRST_PSEUDO_REGISTER
3360           || regno == FRAME_POINTER_REGNUM
3361           || regno == ARG_POINTER_REGNUM);
3362 }
3363
3364 /* Return nonzero if X is a valid ARM state address operand.  */
3365 int
3366 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3367                           int strict_p)
3368 {
3369   bool use_ldrd;
3370   enum rtx_code code = GET_CODE (x);
3371
3372   if (arm_address_register_rtx_p (x, strict_p))
3373     return 1;
3374
3375   use_ldrd = (TARGET_LDRD
3376               && (mode == DImode
3377                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3378
3379   if (code == POST_INC || code == PRE_DEC
3380       || ((code == PRE_INC || code == POST_DEC)
3381           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3382     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3383
3384   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3385            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3386            && GET_CODE (XEXP (x, 1)) == PLUS
3387            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3388     {
3389       rtx addend = XEXP (XEXP (x, 1), 1);
3390
3391       /* Don't allow ldrd post increment by register because it's hard
3392          to fixup invalid register choices.  */
3393       if (use_ldrd
3394           && GET_CODE (x) == POST_MODIFY
3395           && GET_CODE (addend) == REG)
3396         return 0;
3397
3398       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3399               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3400     }
3401
3402   /* After reload constants split into minipools will have addresses
3403      from a LABEL_REF.  */
3404   else if (reload_completed
3405            && (code == LABEL_REF
3406                || (code == CONST
3407                    && GET_CODE (XEXP (x, 0)) == PLUS
3408                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3409                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3410     return 1;
3411
3412   else if (mode == TImode)
3413     return 0;
3414
3415   else if (code == PLUS)
3416     {
3417       rtx xop0 = XEXP (x, 0);
3418       rtx xop1 = XEXP (x, 1);
3419
3420       return ((arm_address_register_rtx_p (xop0, strict_p)
3421                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3422               || (arm_address_register_rtx_p (xop1, strict_p)
3423                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3424     }
3425
3426 #if 0
3427   /* Reload currently can't handle MINUS, so disable this for now */
3428   else if (GET_CODE (x) == MINUS)
3429     {
3430       rtx xop0 = XEXP (x, 0);
3431       rtx xop1 = XEXP (x, 1);
3432
3433       return (arm_address_register_rtx_p (xop0, strict_p)
3434               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3435     }
3436 #endif
3437
3438   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3439            && code == SYMBOL_REF
3440            && CONSTANT_POOL_ADDRESS_P (x)
3441            && ! (flag_pic
3442                  && symbol_mentioned_p (get_pool_constant (x))))
3443     return 1;
3444
3445   return 0;
3446 }
3447
3448 /* Return nonzero if INDEX is valid for an address index operand in
3449    ARM state.  */
3450 static int
3451 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3452                         int strict_p)
3453 {
3454   HOST_WIDE_INT range;
3455   enum rtx_code code = GET_CODE (index);
3456
3457   /* Standard coprocessor addressing modes.  */
3458   if (TARGET_HARD_FLOAT
3459       && (TARGET_FPA || TARGET_MAVERICK)
3460       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3461           || (TARGET_MAVERICK && mode == DImode)))
3462     return (code == CONST_INT && INTVAL (index) < 1024
3463             && INTVAL (index) > -1024
3464             && (INTVAL (index) & 3) == 0);
3465
3466   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3467     return (code == CONST_INT
3468             && INTVAL (index) < 1024
3469             && INTVAL (index) > -1024
3470             && (INTVAL (index) & 3) == 0);
3471
3472   if (arm_address_register_rtx_p (index, strict_p)
3473       && (GET_MODE_SIZE (mode) <= 4))
3474     return 1;
3475
3476   if (mode == DImode || mode == DFmode)
3477     {
3478       if (code == CONST_INT)
3479         {
3480           HOST_WIDE_INT val = INTVAL (index);
3481
3482           if (TARGET_LDRD)
3483             return val > -256 && val < 256;
3484           else
3485             return val > -4096 && val < 4092;
3486         }
3487
3488       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3489     }
3490
3491   if (GET_MODE_SIZE (mode) <= 4
3492       && ! (arm_arch4
3493             && (mode == HImode
3494                 || (mode == QImode && outer == SIGN_EXTEND))))
3495     {
3496       if (code == MULT)
3497         {
3498           rtx xiop0 = XEXP (index, 0);
3499           rtx xiop1 = XEXP (index, 1);
3500
3501           return ((arm_address_register_rtx_p (xiop0, strict_p)
3502                    && power_of_two_operand (xiop1, SImode))
3503                   || (arm_address_register_rtx_p (xiop1, strict_p)
3504                       && power_of_two_operand (xiop0, SImode)));
3505         }
3506       else if (code == LSHIFTRT || code == ASHIFTRT
3507                || code == ASHIFT || code == ROTATERT)
3508         {
3509           rtx op = XEXP (index, 1);
3510
3511           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3512                   && GET_CODE (op) == CONST_INT
3513                   && INTVAL (op) > 0
3514                   && INTVAL (op) <= 31);
3515         }
3516     }
3517
3518   /* For ARM v4 we may be doing a sign-extend operation during the
3519      load.  */
3520   if (arm_arch4)
3521     {
3522       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3523         range = 256;
3524       else
3525         range = 4096;
3526     }
3527   else
3528     range = (mode == HImode) ? 4095 : 4096;
3529
3530   return (code == CONST_INT
3531           && INTVAL (index) < range
3532           && INTVAL (index) > -range);
3533 }
3534
3535 /* Return nonzero if X is valid as a Thumb state base register.  */
3536 static int
3537 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3538 {
3539   int regno;
3540
3541   if (GET_CODE (x) != REG)
3542     return 0;
3543
3544   regno = REGNO (x);
3545
3546   if (strict_p)
3547     return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3548
3549   return (regno <= LAST_LO_REGNUM
3550           || regno > LAST_VIRTUAL_REGISTER
3551           || regno == FRAME_POINTER_REGNUM
3552           || (GET_MODE_SIZE (mode) >= 4
3553               && (regno == STACK_POINTER_REGNUM
3554                   || regno >= FIRST_PSEUDO_REGISTER
3555                   || x == hard_frame_pointer_rtx
3556                   || x == arg_pointer_rtx)));
3557 }
3558
3559 /* Return nonzero if x is a legitimate index register.  This is the case
3560    for any base register that can access a QImode object.  */
3561 inline static int
3562 thumb_index_register_rtx_p (rtx x, int strict_p)
3563 {
3564   return thumb_base_register_rtx_p (x, QImode, strict_p);
3565 }
3566
3567 /* Return nonzero if x is a legitimate Thumb-state address.
3568
3569    The AP may be eliminated to either the SP or the FP, so we use the
3570    least common denominator, e.g. SImode, and offsets from 0 to 64.
3571
3572    ??? Verify whether the above is the right approach.
3573
3574    ??? Also, the FP may be eliminated to the SP, so perhaps that
3575    needs special handling also.
3576
3577    ??? Look at how the mips16 port solves this problem.  It probably uses
3578    better ways to solve some of these problems.
3579
3580    Although it is not incorrect, we don't accept QImode and HImode
3581    addresses based on the frame pointer or arg pointer until the
3582    reload pass starts.  This is so that eliminating such addresses
3583    into stack based ones won't produce impossible code.  */
3584 int
3585 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3586 {
3587   /* ??? Not clear if this is right.  Experiment.  */
3588   if (GET_MODE_SIZE (mode) < 4
3589       && !(reload_in_progress || reload_completed)
3590       && (reg_mentioned_p (frame_pointer_rtx, x)
3591           || reg_mentioned_p (arg_pointer_rtx, x)
3592           || reg_mentioned_p (virtual_incoming_args_rtx, x)
3593           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3594           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3595           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3596     return 0;
3597
3598   /* Accept any base register.  SP only in SImode or larger.  */
3599   else if (thumb_base_register_rtx_p (x, mode, strict_p))
3600     return 1;
3601
3602   /* This is PC relative data before arm_reorg runs.  */
3603   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3604            && GET_CODE (x) == SYMBOL_REF
3605            && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3606     return 1;
3607
3608   /* This is PC relative data after arm_reorg runs.  */
3609   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3610            && (GET_CODE (x) == LABEL_REF
3611                || (GET_CODE (x) == CONST
3612                    && GET_CODE (XEXP (x, 0)) == PLUS
3613                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3614                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3615     return 1;
3616
3617   /* Post-inc indexing only supported for SImode and larger.  */
3618   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3619            && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3620     return 1;
3621
3622   else if (GET_CODE (x) == PLUS)
3623     {
3624       /* REG+REG address can be any two index registers.  */
3625       /* We disallow FRAME+REG addressing since we know that FRAME
3626          will be replaced with STACK, and SP relative addressing only
3627          permits SP+OFFSET.  */
3628       if (GET_MODE_SIZE (mode) <= 4
3629           && XEXP (x, 0) != frame_pointer_rtx
3630           && XEXP (x, 1) != frame_pointer_rtx
3631           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3632           && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3633         return 1;
3634
3635       /* REG+const has 5-7 bit offset for non-SP registers.  */
3636       else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3637                 || XEXP (x, 0) == arg_pointer_rtx)
3638                && GET_CODE (XEXP (x, 1)) == CONST_INT
3639                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3640         return 1;
3641
3642       /* REG+const has 10 bit offset for SP, but only SImode and
3643          larger is supported.  */
3644       /* ??? Should probably check for DI/DFmode overflow here
3645          just like GO_IF_LEGITIMATE_OFFSET does.  */
3646       else if (GET_CODE (XEXP (x, 0)) == REG
3647                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3648                && GET_MODE_SIZE (mode) >= 4
3649                && GET_CODE (XEXP (x, 1)) == CONST_INT
3650                && INTVAL (XEXP (x, 1)) >= 0
3651                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3652                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3653         return 1;
3654
3655       else if (GET_CODE (XEXP (x, 0)) == REG
3656                && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3657                && GET_MODE_SIZE (mode) >= 4
3658                && GET_CODE (XEXP (x, 1)) == CONST_INT
3659                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3660         return 1;
3661     }
3662
3663   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3664            && GET_MODE_SIZE (mode) == 4
3665            && GET_CODE (x) == SYMBOL_REF
3666            && CONSTANT_POOL_ADDRESS_P (x)
3667            && !(flag_pic
3668                 && symbol_mentioned_p (get_pool_constant (x))))
3669     return 1;
3670
3671   return 0;
3672 }
3673
3674 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3675    instruction of mode MODE.  */
3676 int
3677 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3678 {
3679   switch (GET_MODE_SIZE (mode))
3680     {
3681     case 1:
3682       return val >= 0 && val < 32;
3683
3684     case 2:
3685       return val >= 0 && val < 64 && (val & 1) == 0;
3686
3687     default:
3688       return (val >= 0
3689               && (val + GET_MODE_SIZE (mode)) <= 128
3690               && (val & 3) == 0);
3691     }
3692 }
3693
3694 /* Try machine-dependent ways of modifying an illegitimate address
3695    to be legitimate.  If we find one, return the new, valid address.  */
3696 rtx
3697 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3698 {
3699   if (GET_CODE (x) == PLUS)
3700     {
3701       rtx xop0 = XEXP (x, 0);
3702       rtx xop1 = XEXP (x, 1);
3703
3704       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3705         xop0 = force_reg (SImode, xop0);
3706
3707       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3708         xop1 = force_reg (SImode, xop1);
3709
3710       if (ARM_BASE_REGISTER_RTX_P (xop0)
3711           && GET_CODE (xop1) == CONST_INT)
3712         {
3713           HOST_WIDE_INT n, low_n;
3714           rtx base_reg, val;
3715           n = INTVAL (xop1);
3716
3717           /* VFP addressing modes actually allow greater offsets, but for
3718              now we just stick with the lowest common denominator.  */
3719           if (mode == DImode
3720               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3721             {
3722               low_n = n & 0x0f;
3723               n &= ~0x0f;
3724               if (low_n > 4)
3725                 {
3726                   n += 16;
3727                   low_n -= 16;
3728                 }
3729             }
3730           else
3731             {
3732               low_n = ((mode) == TImode ? 0
3733                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3734               n -= low_n;
3735             }
3736
3737           base_reg = gen_reg_rtx (SImode);
3738           val = force_operand (gen_rtx_PLUS (SImode, xop0,
3739                                              GEN_INT (n)), NULL_RTX);
3740           emit_move_insn (base_reg, val);
3741           x = (low_n == 0 ? base_reg
3742                : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3743         }
3744       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3745         x = gen_rtx_PLUS (SImode, xop0, xop1);
3746     }
3747
3748   /* XXX We don't allow MINUS any more -- see comment in
3749      arm_legitimate_address_p ().  */
3750   else if (GET_CODE (x) == MINUS)
3751     {
3752       rtx xop0 = XEXP (x, 0);
3753       rtx xop1 = XEXP (x, 1);
3754
3755       if (CONSTANT_P (xop0))
3756         xop0 = force_reg (SImode, xop0);
3757
3758       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3759         xop1 = force_reg (SImode, xop1);
3760
3761       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3762         x = gen_rtx_MINUS (SImode, xop0, xop1);
3763     }
3764
3765   if (flag_pic)
3766     {
3767       /* We need to find and carefully transform any SYMBOL and LABEL
3768          references; so go back to the original address expression.  */
3769       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3770
3771       if (new_x != orig_x)
3772         x = new_x;
3773     }
3774
3775   return x;
3776 }
3777
3778
3779 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3780    to be legitimate.  If we find one, return the new, valid address.  */
3781 rtx
3782 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3783 {
3784   if (GET_CODE (x) == PLUS
3785       && GET_CODE (XEXP (x, 1)) == CONST_INT
3786       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3787           || INTVAL (XEXP (x, 1)) < 0))
3788     {
3789       rtx xop0 = XEXP (x, 0);
3790       rtx xop1 = XEXP (x, 1);
3791       HOST_WIDE_INT offset = INTVAL (xop1);
3792
3793       /* Try and fold the offset into a biasing of the base register and
3794          then offsetting that.  Don't do this when optimizing for space
3795          since it can cause too many CSEs.  */
3796       if (optimize_size && offset >= 0
3797           && offset < 256 + 31 * GET_MODE_SIZE (mode))
3798         {
3799           HOST_WIDE_INT delta;
3800
3801           if (offset >= 256)
3802             delta = offset - (256 - GET_MODE_SIZE (mode));
3803           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3804             delta = 31 * GET_MODE_SIZE (mode);
3805           else
3806             delta = offset & (~31 * GET_MODE_SIZE (mode));
3807
3808           xop0 = force_operand (plus_constant (xop0, offset - delta),
3809                                 NULL_RTX);
3810           x = plus_constant (xop0, delta);
3811         }
3812       else if (offset < 0 && offset > -256)
3813         /* Small negative offsets are best done with a subtract before the
3814            dereference, forcing these into a register normally takes two
3815            instructions.  */
3816         x = force_operand (x, NULL_RTX);
3817       else
3818         {
3819           /* For the remaining cases, force the constant into a register.  */
3820           xop1 = force_reg (SImode, xop1);
3821           x = gen_rtx_PLUS (SImode, xop0, xop1);
3822         }
3823     }
3824   else if (GET_CODE (x) == PLUS
3825            && s_register_operand (XEXP (x, 1), SImode)
3826            && !s_register_operand (XEXP (x, 0), SImode))
3827     {
3828       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3829
3830       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3831     }
3832
3833   if (flag_pic)
3834     {
3835       /* We need to find and carefully transform any SYMBOL and LABEL
3836          references; so go back to the original address expression.  */
3837       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3838
3839       if (new_x != orig_x)
3840         x = new_x;
3841     }
3842
3843   return x;
3844 }
3845
3846 \f
3847
3848 #define REG_OR_SUBREG_REG(X)                                            \
3849   (GET_CODE (X) == REG                                                  \
3850    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3851
3852 #define REG_OR_SUBREG_RTX(X)                    \
3853    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3854
3855 #ifndef COSTS_N_INSNS
3856 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3857 #endif
3858 static inline int
3859 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3860 {
3861   enum machine_mode mode = GET_MODE (x);
3862
3863   switch (code)
3864     {
3865     case ASHIFT:
3866     case ASHIFTRT:
3867     case LSHIFTRT:
3868     case ROTATERT:
3869     case PLUS:
3870     case MINUS:
3871     case COMPARE:
3872     case NEG:
3873     case NOT:
3874       return COSTS_N_INSNS (1);
3875
3876     case MULT:
3877       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3878         {
3879           int cycles = 0;
3880           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3881
3882           while (i)
3883             {
3884               i >>= 2;
3885               cycles++;
3886             }
3887           return COSTS_N_INSNS (2) + cycles;
3888         }
3889       return COSTS_N_INSNS (1) + 16;
3890
3891     case SET:
3892       return (COSTS_N_INSNS (1)
3893               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3894                      + GET_CODE (SET_DEST (x)) == MEM));
3895
3896     case CONST_INT:
3897       if (outer == SET)
3898         {
3899           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3900             return 0;
3901           if (thumb_shiftable_const (INTVAL (x)))
3902             return COSTS_N_INSNS (2);
3903           return COSTS_N_INSNS (3);
3904         }
3905       else if ((outer == PLUS || outer == COMPARE)
3906                && INTVAL (x) < 256 && INTVAL (x) > -256)
3907         return 0;
3908       else if (outer == AND
3909                && INTVAL (x) < 256 && INTVAL (x) >= -256)
3910         return COSTS_N_INSNS (1);
3911       else if (outer == ASHIFT || outer == ASHIFTRT
3912                || outer == LSHIFTRT)
3913         return 0;
3914       return COSTS_N_INSNS (2);
3915
3916     case CONST:
3917     case CONST_DOUBLE:
3918     case LABEL_REF:
3919     case SYMBOL_REF:
3920       return COSTS_N_INSNS (3);
3921
3922     case UDIV:
3923     case UMOD:
3924     case DIV:
3925     case MOD:
3926       return 100;
3927
3928     case TRUNCATE:
3929       return 99;
3930
3931     case AND:
3932     case XOR:
3933     case IOR:
3934       /* XXX guess.  */
3935       return 8;
3936
3937     case MEM:
3938       /* XXX another guess.  */
3939       /* Memory costs quite a lot for the first word, but subsequent words
3940          load at the equivalent of a single insn each.  */
3941       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3942               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3943                  ? 4 : 0));
3944
3945     case IF_THEN_ELSE:
3946       /* XXX a guess.  */
3947       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3948         return 14;
3949       return 2;
3950
3951     case ZERO_EXTEND:
3952       /* XXX still guessing.  */
3953       switch (GET_MODE (XEXP (x, 0)))
3954         {
3955         case QImode:
3956           return (1 + (mode == DImode ? 4 : 0)
3957                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3958
3959         case HImode:
3960           return (4 + (mode == DImode ? 4 : 0)
3961                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3962
3963         case SImode:
3964           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3965
3966         default:
3967           return 99;
3968         }
3969
3970     default:
3971       return 99;
3972     }
3973 }
3974
3975
3976 /* Worker routine for arm_rtx_costs.  */
3977 static inline int
3978 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3979 {
3980   enum machine_mode mode = GET_MODE (x);
3981   enum rtx_code subcode;
3982   int extra_cost;
3983
3984   switch (code)
3985     {
3986     case MEM:
3987       /* Memory costs quite a lot for the first word, but subsequent words
3988          load at the equivalent of a single insn each.  */
3989       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3990               + (GET_CODE (x) == SYMBOL_REF
3991                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3992
3993     case DIV:
3994     case MOD:
3995     case UDIV:
3996     case UMOD:
3997       return optimize_size ? COSTS_N_INSNS (2) : 100;
3998
3999     case ROTATE:
4000       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4001         return 4;
4002       /* Fall through */
4003     case ROTATERT:
4004       if (mode != SImode)
4005         return 8;
4006       /* Fall through */
4007     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4008       if (mode == DImode)
4009         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
4010                 + ((GET_CODE (XEXP (x, 0)) == REG
4011                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4012                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4013                    ? 0 : 8));
4014       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
4015                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4016                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4017                    ? 0 : 4)
4018               + ((GET_CODE (XEXP (x, 1)) == REG
4019    &n