* config/arm/arm-protos.h (arm_load_pic_register): Update prototype.
[official-gcc.git] / gcc / config / arm / arm.c
blob02ee7cad19d2688f250adacba3203668bccd8de8
1 /* Output routines for GCC for ARM.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5 and Martin Simmons (@harleqn.co.uk).
6 More major hacks by Richard Earnshaw (rearnsha@arm.com).
8 This file is part of GCC.
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.
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.
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. */
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"
56 /* Forward definitions of types. */
57 typedef struct minipool_node Mnode;
58 typedef struct minipool_fixup Mfix;
60 const struct attribute_spec arm_attribute_table[];
62 /* Forward function declarations. */
63 static arm_stack_offsets *arm_get_frame_offsets (void);
64 static void arm_add_gc_roots (void);
65 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
66 HOST_WIDE_INT, rtx, rtx, int, int);
67 static unsigned bit_count (unsigned long);
68 static int arm_address_register_rtx_p (rtx, int);
69 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
70 static int thumb_base_register_rtx_p (rtx, enum machine_mode, int);
71 inline static int thumb_index_register_rtx_p (rtx, int);
72 static int thumb_far_jump_used_p (void);
73 static bool thumb_force_lr_save (void);
74 static unsigned long thumb_compute_save_reg_mask (void);
75 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
76 static rtx emit_multi_reg_push (int);
77 static rtx emit_sfm (int, int);
78 #ifndef AOF_ASSEMBLER
79 static bool arm_assemble_integer (rtx, unsigned int, int);
80 #endif
81 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
82 static arm_cc get_arm_condition_code (rtx);
83 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
84 static rtx is_jump_table (rtx);
85 static const char *output_multi_immediate (rtx *, const char *, const char *,
86 int, HOST_WIDE_INT);
87 static void print_multi_reg (FILE *, const char *, int, int);
88 static const char *shift_op (rtx, HOST_WIDE_INT *);
89 static struct machine_function *arm_init_machine_status (void);
90 static int number_of_first_bit_set (int);
91 static void replace_symbols_in_block (tree, rtx, rtx);
92 static void thumb_exit (FILE *, int);
93 static void thumb_pushpop (FILE *, int, int, int *, int);
94 static rtx is_jump_table (rtx);
95 static HOST_WIDE_INT get_jump_table_size (rtx);
96 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
97 static Mnode *add_minipool_forward_ref (Mfix *);
98 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
99 static Mnode *add_minipool_backward_ref (Mfix *);
100 static void assign_minipool_offsets (Mfix *);
101 static void arm_print_value (FILE *, rtx);
102 static void dump_minipool (rtx);
103 static int arm_barrier_cost (rtx);
104 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
105 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
106 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
107 rtx);
108 static void arm_reorg (void);
109 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
110 static int current_file_function_operand (rtx);
111 static unsigned long arm_compute_save_reg0_reg12_mask (void);
112 static unsigned long arm_compute_save_reg_mask (void);
113 static unsigned long arm_isr_value (tree);
114 static unsigned long arm_compute_func_type (void);
115 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
116 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
117 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
118 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
119 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
120 static int arm_comp_type_attributes (tree, tree);
121 static void arm_set_default_type_attributes (tree);
122 static int arm_adjust_cost (rtx, rtx, rtx, int);
123 static int count_insns_for_constant (HOST_WIDE_INT, int);
124 static int arm_get_strip_length (int);
125 static bool arm_function_ok_for_sibcall (tree, tree);
126 static void arm_internal_label (FILE *, const char *, unsigned long);
127 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
128 tree);
129 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
130 static bool arm_size_rtx_costs (rtx, int, int, int *);
131 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
132 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
133 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
134 static bool arm_9e_rtx_costs (rtx, int, int, int *);
135 static int arm_address_cost (rtx);
136 static bool arm_memory_load_p (rtx);
137 static bool arm_cirrus_insn_p (rtx);
138 static void cirrus_reorg (rtx);
139 static void arm_init_builtins (void);
140 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
141 static void arm_init_iwmmxt_builtins (void);
142 static rtx safe_vector_operand (rtx, enum machine_mode);
143 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
144 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
145 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
146 static void emit_constant_insn (rtx cond, rtx pattern);
148 #ifndef ARM_PE
149 static void arm_encode_section_info (tree, rtx, int);
150 #endif
151 #ifdef AOF_ASSEMBLER
152 static void aof_globalize_label (FILE *, const char *);
153 static void aof_dump_imports (FILE *);
154 static void aof_dump_pic_table (FILE *);
155 static void aof_file_start (void);
156 static void aof_file_end (void);
157 #endif
158 static rtx arm_struct_value_rtx (tree, int);
159 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
160 tree, int *, int);
161 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
162 enum machine_mode, tree, bool);
163 static bool arm_promote_prototypes (tree);
164 static bool arm_default_short_enums (void);
165 static bool arm_align_anon_bitfield (void);
167 static tree arm_cxx_guard_type (void);
168 static bool arm_cxx_guard_mask_bit (void);
169 static tree arm_get_cookie_size (tree);
170 static bool arm_cookie_has_size (void);
171 static bool arm_cxx_cdtor_returns_this (void);
172 static bool arm_cxx_key_method_may_be_inline (void);
173 static bool arm_cxx_export_class_data (void);
174 static void arm_init_libfuncs (void);
175 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
177 /* Initialize the GCC target structure. */
178 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
179 #undef TARGET_MERGE_DECL_ATTRIBUTES
180 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
181 #endif
183 #undef TARGET_ATTRIBUTE_TABLE
184 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
186 #ifdef AOF_ASSEMBLER
187 #undef TARGET_ASM_BYTE_OP
188 #define TARGET_ASM_BYTE_OP "\tDCB\t"
189 #undef TARGET_ASM_ALIGNED_HI_OP
190 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
191 #undef TARGET_ASM_ALIGNED_SI_OP
192 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
193 #undef TARGET_ASM_GLOBALIZE_LABEL
194 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
195 #undef TARGET_ASM_FILE_START
196 #define TARGET_ASM_FILE_START aof_file_start
197 #undef TARGET_ASM_FILE_END
198 #define TARGET_ASM_FILE_END aof_file_end
199 #else
200 #undef TARGET_ASM_ALIGNED_SI_OP
201 #define TARGET_ASM_ALIGNED_SI_OP NULL
202 #undef TARGET_ASM_INTEGER
203 #define TARGET_ASM_INTEGER arm_assemble_integer
204 #endif
206 #undef TARGET_ASM_FUNCTION_PROLOGUE
207 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
209 #undef TARGET_ASM_FUNCTION_EPILOGUE
210 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
212 #undef TARGET_COMP_TYPE_ATTRIBUTES
213 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
215 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
216 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
218 #undef TARGET_SCHED_ADJUST_COST
219 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
221 #undef TARGET_ENCODE_SECTION_INFO
222 #ifdef ARM_PE
223 #define TARGET_ENCODE_SECTION_INFO arm_pe_encode_section_info
224 #else
225 #define TARGET_ENCODE_SECTION_INFO arm_encode_section_info
226 #endif
228 #undef TARGET_STRIP_NAME_ENCODING
229 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
231 #undef TARGET_ASM_INTERNAL_LABEL
232 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
234 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
235 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
237 #undef TARGET_ASM_OUTPUT_MI_THUNK
238 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
239 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
240 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
242 /* This will be overridden in arm_override_options. */
243 #undef TARGET_RTX_COSTS
244 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
245 #undef TARGET_ADDRESS_COST
246 #define TARGET_ADDRESS_COST arm_address_cost
248 #undef TARGET_SHIFT_TRUNCATION_MASK
249 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
250 #undef TARGET_VECTOR_MODE_SUPPORTED_P
251 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
253 #undef TARGET_MACHINE_DEPENDENT_REORG
254 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
256 #undef TARGET_INIT_BUILTINS
257 #define TARGET_INIT_BUILTINS arm_init_builtins
258 #undef TARGET_EXPAND_BUILTIN
259 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
261 #undef TARGET_INIT_LIBFUNCS
262 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
264 #undef TARGET_PROMOTE_FUNCTION_ARGS
265 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
266 #undef TARGET_PROMOTE_FUNCTION_RETURN
267 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
268 #undef TARGET_PROMOTE_PROTOTYPES
269 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
270 #undef TARGET_PASS_BY_REFERENCE
271 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
273 #undef TARGET_STRUCT_VALUE_RTX
274 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
276 #undef TARGET_SETUP_INCOMING_VARARGS
277 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
279 #undef TARGET_DEFAULT_SHORT_ENUMS
280 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
282 #undef TARGET_ALIGN_ANON_BITFIELD
283 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
285 #undef TARGET_CXX_GUARD_TYPE
286 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
288 #undef TARGET_CXX_GUARD_MASK_BIT
289 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
291 #undef TARGET_CXX_GET_COOKIE_SIZE
292 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
294 #undef TARGET_CXX_COOKIE_HAS_SIZE
295 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
297 #undef TARGET_CXX_CDTOR_RETURNS_THIS
298 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
300 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
301 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
303 #undef TARGET_CXX_EXPORT_CLASS_DATA
304 #define TARGET_CXX_EXPORT_CLASS_DATA arm_cxx_export_class_data
306 struct gcc_target targetm = TARGET_INITIALIZER;
308 /* Obstack for minipool constant handling. */
309 static struct obstack minipool_obstack;
310 static char * minipool_startobj;
312 /* The maximum number of insns skipped which
313 will be conditionalised if possible. */
314 static int max_insns_skipped = 5;
316 extern FILE * asm_out_file;
318 /* True if we are currently building a constant table. */
319 int making_const_table;
321 /* Define the information needed to generate branch insns. This is
322 stored from the compare operation. */
323 rtx arm_compare_op0, arm_compare_op1;
325 /* The processor for which instructions should be scheduled. */
326 enum processor_type arm_tune = arm_none;
328 /* Which floating point model to use. */
329 enum arm_fp_model arm_fp_model;
331 /* Which floating point hardware is available. */
332 enum fputype arm_fpu_arch;
334 /* Which floating point hardware to schedule for. */
335 enum fputype arm_fpu_tune;
337 /* Whether to use floating point hardware. */
338 enum float_abi_type arm_float_abi;
340 /* Which ABI to use. */
341 enum arm_abi_type arm_abi;
343 /* Set by the -mfpu=... option. */
344 const char * target_fpu_name = NULL;
346 /* Set by the -mfpe=... option. */
347 const char * target_fpe_name = NULL;
349 /* Set by the -mfloat-abi=... option. */
350 const char * target_float_abi_name = NULL;
352 /* Set by the -mabi=... option. */
353 const char * target_abi_name = NULL;
355 /* Used to parse -mstructure_size_boundary command line option. */
356 const char * structure_size_string = NULL;
357 int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
359 /* Bit values used to identify processor capabilities. */
360 #define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
361 #define FL_ARCH3M (1 << 1) /* Extended multiply */
362 #define FL_MODE26 (1 << 2) /* 26-bit mode support */
363 #define FL_MODE32 (1 << 3) /* 32-bit mode support */
364 #define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
365 #define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
366 #define FL_THUMB (1 << 6) /* Thumb aware */
367 #define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
368 #define FL_STRONG (1 << 8) /* StrongARM */
369 #define FL_ARCH5E (1 << 9) /* DSP extensions to v5 */
370 #define FL_XSCALE (1 << 10) /* XScale */
371 #define FL_CIRRUS (1 << 11) /* Cirrus/DSP. */
372 #define FL_ARCH6 (1 << 12) /* Architecture rel 6. Adds
373 media instructions. */
374 #define FL_VFPV2 (1 << 13) /* Vector Floating Point V2. */
376 #define FL_IWMMXT (1 << 29) /* XScale v2 or "Intel Wireless MMX technology". */
378 #define FL_FOR_ARCH2 0
379 #define FL_FOR_ARCH3 FL_MODE32
380 #define FL_FOR_ARCH3M (FL_FOR_ARCH3 | FL_ARCH3M)
381 #define FL_FOR_ARCH4 (FL_FOR_ARCH3M | FL_ARCH4)
382 #define FL_FOR_ARCH4T (FL_FOR_ARCH4 | FL_THUMB)
383 #define FL_FOR_ARCH5 (FL_FOR_ARCH4 | FL_ARCH5)
384 #define FL_FOR_ARCH5T (FL_FOR_ARCH5 | FL_THUMB)
385 #define FL_FOR_ARCH5E (FL_FOR_ARCH5 | FL_ARCH5E)
386 #define FL_FOR_ARCH5TE (FL_FOR_ARCH5E | FL_THUMB)
387 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
388 #define FL_FOR_ARCH6 (FL_FOR_ARCH5TE | FL_ARCH6)
389 #define FL_FOR_ARCH6J FL_FOR_ARCH6
390 #define FL_FOR_ARCH6K FL_FOR_ARCH6
391 #define FL_FOR_ARCH6Z FL_FOR_ARCH6
392 #define FL_FOR_ARCH6ZK FL_FOR_ARCH6
394 /* The bits in this mask specify which
395 instructions we are allowed to generate. */
396 static unsigned long insn_flags = 0;
398 /* The bits in this mask specify which instruction scheduling options should
399 be used. */
400 static unsigned long tune_flags = 0;
402 /* The following are used in the arm.md file as equivalents to bits
403 in the above two flag variables. */
405 /* Nonzero if this chip supports the ARM Architecture 3M extensions. */
406 int arm_arch3m = 0;
408 /* Nonzero if this chip supports the ARM Architecture 4 extensions. */
409 int arm_arch4 = 0;
411 /* Nonzero if this chip supports the ARM Architecture 4t extensions. */
412 int arm_arch4t = 0;
414 /* Nonzero if this chip supports the ARM Architecture 5 extensions. */
415 int arm_arch5 = 0;
417 /* Nonzero if this chip supports the ARM Architecture 5E extensions. */
418 int arm_arch5e = 0;
420 /* Nonzero if this chip supports the ARM Architecture 6 extensions. */
421 int arm_arch6 = 0;
423 /* Nonzero if this chip can benefit from load scheduling. */
424 int arm_ld_sched = 0;
426 /* Nonzero if this chip is a StrongARM. */
427 int arm_is_strong = 0;
429 /* Nonzero if this chip is a Cirrus variant. */
430 int arm_arch_cirrus = 0;
432 /* Nonzero if this chip supports Intel Wireless MMX technology. */
433 int arm_arch_iwmmxt = 0;
435 /* Nonzero if this chip is an XScale. */
436 int arm_arch_xscale = 0;
438 /* Nonzero if tuning for XScale */
439 int arm_tune_xscale = 0;
441 /* Nonzero if this chip is an ARM6 or an ARM7. */
442 int arm_is_6_or_7 = 0;
444 /* Nonzero if generating Thumb instructions. */
445 int thumb_code = 0;
447 /* Nonzero if we should define __THUMB_INTERWORK__ in the
448 preprocessor.
449 XXX This is a bit of a hack, it's intended to help work around
450 problems in GLD which doesn't understand that armv5t code is
451 interworking clean. */
452 int arm_cpp_interwork = 0;
454 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
455 must report the mode of the memory reference from PRINT_OPERAND to
456 PRINT_OPERAND_ADDRESS. */
457 enum machine_mode output_memory_reference_mode;
459 /* The register number to be used for the PIC offset register. */
460 const char * arm_pic_register_string = NULL;
461 int arm_pic_register = INVALID_REGNUM;
463 /* Set to 1 when a return insn is output, this means that the epilogue
464 is not needed. */
465 int return_used_this_function;
467 /* Set to 1 after arm_reorg has started. Reset to start at the start of
468 the next function. */
469 static int after_arm_reorg = 0;
471 /* The maximum number of insns to be used when loading a constant. */
472 static int arm_constant_limit = 3;
474 /* For an explanation of these variables, see final_prescan_insn below. */
475 int arm_ccfsm_state;
476 enum arm_cond_code arm_current_cc;
477 rtx arm_target_insn;
478 int arm_target_label;
480 /* The condition codes of the ARM, and the inverse function. */
481 static const char * const arm_condition_codes[] =
483 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
484 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
487 #define streq(string1, string2) (strcmp (string1, string2) == 0)
489 /* Initialization code. */
491 struct processors
493 const char *const name;
494 enum processor_type core;
495 const char *arch;
496 const unsigned long flags;
497 bool (* rtx_costs) (rtx, int, int, int *);
500 /* Not all of these give usefully different compilation alternatives,
501 but there is no simple way of generalizing them. */
502 static const struct processors all_cores[] =
504 /* ARM Cores */
505 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
506 {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
507 #include "arm-cores.def"
508 #undef ARM_CORE
509 {NULL, arm_none, NULL, 0, NULL}
512 static const struct processors all_architectures[] =
514 /* ARM Architectures */
515 /* We don't specify rtx_costs here as it will be figured out
516 from the core. */
518 {"armv2", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
519 {"armv2a", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
520 {"armv3", arm6, "3", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
521 {"armv3m", arm7m, "3M", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
522 {"armv4", arm7tdmi, "4", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
523 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
524 implementations that support it, so we will leave it out for now. */
525 {"armv4t", arm7tdmi, "4T", FL_CO_PROC | FL_FOR_ARCH4T, NULL},
526 {"armv5", arm10tdmi, "5", FL_CO_PROC | FL_FOR_ARCH5, NULL},
527 {"armv5t", arm10tdmi, "5T", FL_CO_PROC | FL_FOR_ARCH5T, NULL},
528 {"armv5e", arm1026ejs, "5E", FL_CO_PROC | FL_FOR_ARCH5E, NULL},
529 {"armv5te", arm1026ejs, "5TE", FL_CO_PROC | FL_FOR_ARCH5TE, NULL},
530 {"armv6", arm1136js, "6", FL_CO_PROC | FL_FOR_ARCH6, NULL},
531 {"armv6j", arm1136js, "6J", FL_CO_PROC | FL_FOR_ARCH6J, NULL},
532 {"armv6k", mpcore, "6K", FL_CO_PROC | FL_FOR_ARCH6K, NULL},
533 {"armv6z", arm1176jzs, "6Z", FL_CO_PROC | FL_FOR_ARCH6Z, NULL},
534 {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC | FL_FOR_ARCH6ZK, NULL},
535 {"ep9312", ep9312, "4T", FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
536 {"iwmmxt", iwmmxt, "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
537 {NULL, arm_none, NULL, 0 , NULL}
540 /* This is a magic structure. The 'string' field is magically filled in
541 with a pointer to the value specified by the user on the command line
542 assuming that the user has specified such a value. */
544 struct arm_cpu_select arm_select[] =
546 /* string name processors */
547 { NULL, "-mcpu=", all_cores },
548 { NULL, "-march=", all_architectures },
549 { NULL, "-mtune=", all_cores }
553 /* The name of the proprocessor macro to define for this architecture. */
555 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
557 struct fpu_desc
559 const char * name;
560 enum fputype fpu;
564 /* Available values for for -mfpu=. */
566 static const struct fpu_desc all_fpus[] =
568 {"fpa", FPUTYPE_FPA},
569 {"fpe2", FPUTYPE_FPA_EMU2},
570 {"fpe3", FPUTYPE_FPA_EMU2},
571 {"maverick", FPUTYPE_MAVERICK},
572 {"vfp", FPUTYPE_VFP}
576 /* Floating point models used by the different hardware.
577 See fputype in arm.h. */
579 static const enum fputype fp_model_for_fpu[] =
581 /* No FP hardware. */
582 ARM_FP_MODEL_UNKNOWN, /* FPUTYPE_NONE */
583 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA */
584 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU2 */
585 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU3 */
586 ARM_FP_MODEL_MAVERICK, /* FPUTYPE_MAVERICK */
587 ARM_FP_MODEL_VFP /* FPUTYPE_VFP */
591 struct float_abi
593 const char * name;
594 enum float_abi_type abi_type;
598 /* Available values for -mfloat-abi=. */
600 static const struct float_abi all_float_abis[] =
602 {"soft", ARM_FLOAT_ABI_SOFT},
603 {"softfp", ARM_FLOAT_ABI_SOFTFP},
604 {"hard", ARM_FLOAT_ABI_HARD}
608 struct abi_name
610 const char *name;
611 enum arm_abi_type abi_type;
615 /* Available values for -mabi=. */
617 static const struct abi_name arm_all_abis[] =
619 {"apcs-gnu", ARM_ABI_APCS},
620 {"atpcs", ARM_ABI_ATPCS},
621 {"aapcs", ARM_ABI_AAPCS},
622 {"iwmmxt", ARM_ABI_IWMMXT}
625 /* Return the number of bits set in VALUE. */
626 static unsigned
627 bit_count (unsigned long value)
629 unsigned long count = 0;
631 while (value)
633 count++;
634 value &= value - 1; /* Clear the least-significant set bit. */
637 return count;
640 /* Set up library functions unique to ARM. */
642 static void
643 arm_init_libfuncs (void)
645 /* There are no special library functions unless we are using the
646 ARM BPABI. */
647 if (!TARGET_BPABI)
648 return;
650 /* The functions below are described in Section 4 of the "Run-Time
651 ABI for the ARM architecture", Version 1.0. */
653 /* Double-precision floating-point arithmetic. Table 2. */
654 set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
655 set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
656 set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
657 set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
658 set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
660 /* Double-precision comparisons. Table 3. */
661 set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
662 set_optab_libfunc (ne_optab, DFmode, NULL);
663 set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
664 set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
665 set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
666 set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
667 set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
669 /* Single-precision floating-point arithmetic. Table 4. */
670 set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
671 set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
672 set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
673 set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
674 set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
676 /* Single-precision comparisons. Table 5. */
677 set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
678 set_optab_libfunc (ne_optab, SFmode, NULL);
679 set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
680 set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
681 set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
682 set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
683 set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
685 /* Floating-point to integer conversions. Table 6. */
686 set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
687 set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
688 set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
689 set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
690 set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
691 set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
692 set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
693 set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
695 /* Conversions between floating types. Table 7. */
696 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
697 set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
699 /* Integer to floating-point conversions. Table 8. */
700 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
701 set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
702 set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
703 set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
704 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
705 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
706 set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
707 set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
709 /* Long long. Table 9. */
710 set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
711 set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
712 set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
713 set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
714 set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
715 set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
716 set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
717 set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
719 /* Integer (32/32->32) division. \S 4.3.1. */
720 set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
721 set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
723 /* The divmod functions are designed so that they can be used for
724 plain division, even though they return both the quotient and the
725 remainder. The quotient is returned in the usual location (i.e.,
726 r0 for SImode, {r0, r1} for DImode), just as would be expected
727 for an ordinary division routine. Because the AAPCS calling
728 conventions specify that all of { r0, r1, r2, r3 } are
729 callee-saved registers, there is no need to tell the compiler
730 explicitly that those registers are clobbered by these
731 routines. */
732 set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
733 set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
734 set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idivmod");
735 set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidivmod");
738 /* Fix up any incompatible options that the user has specified.
739 This has now turned into a maze. */
740 void
741 arm_override_options (void)
743 unsigned i;
745 /* Set up the flags based on the cpu/architecture selected by the user. */
746 for (i = ARRAY_SIZE (arm_select); i--;)
748 struct arm_cpu_select * ptr = arm_select + i;
750 if (ptr->string != NULL && ptr->string[0] != '\0')
752 const struct processors * sel;
754 for (sel = ptr->processors; sel->name != NULL; sel++)
755 if (streq (ptr->string, sel->name))
757 /* Set the architecture define. */
758 if (i != 2)
759 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
761 /* Determine the processor core for which we should
762 tune code-generation. */
763 if (/* -mcpu= is a sensible default. */
764 i == 0
765 /* If -march= is used, and -mcpu= has not been used,
766 assume that we should tune for a representative
767 CPU from that architecture. */
768 || i == 1
769 /* -mtune= overrides -mcpu= and -march=. */
770 || i == 2)
771 arm_tune = (enum processor_type) (sel - ptr->processors);
773 if (i != 2)
775 /* If we have been given an architecture and a processor
776 make sure that they are compatible. We only generate
777 a warning though, and we prefer the CPU over the
778 architecture. */
779 if (insn_flags != 0 && (insn_flags ^ sel->flags))
780 warning ("switch -mcpu=%s conflicts with -march= switch",
781 ptr->string);
783 insn_flags = sel->flags;
786 break;
789 if (sel->name == NULL)
790 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
794 /* If the user did not specify a processor, choose one for them. */
795 if (insn_flags == 0)
797 const struct processors * sel;
798 unsigned int sought;
799 enum processor_type cpu;
801 cpu = TARGET_CPU_DEFAULT;
802 if (cpu == arm_none)
804 #ifdef SUBTARGET_CPU_DEFAULT
805 /* Use the subtarget default CPU if none was specified by
806 configure. */
807 cpu = SUBTARGET_CPU_DEFAULT;
808 #endif
809 /* Default to ARM6. */
810 if (cpu == arm_none)
811 cpu = arm6;
813 sel = &all_cores[cpu];
815 insn_flags = sel->flags;
817 /* Now check to see if the user has specified some command line
818 switch that require certain abilities from the cpu. */
819 sought = 0;
821 if (TARGET_INTERWORK || TARGET_THUMB)
823 sought |= (FL_THUMB | FL_MODE32);
825 /* There are no ARM processors that support both APCS-26 and
826 interworking. Therefore we force FL_MODE26 to be removed
827 from insn_flags here (if it was set), so that the search
828 below will always be able to find a compatible processor. */
829 insn_flags &= ~FL_MODE26;
832 if (sought != 0 && ((sought & insn_flags) != sought))
834 /* Try to locate a CPU type that supports all of the abilities
835 of the default CPU, plus the extra abilities requested by
836 the user. */
837 for (sel = all_cores; sel->name != NULL; sel++)
838 if ((sel->flags & sought) == (sought | insn_flags))
839 break;
841 if (sel->name == NULL)
843 unsigned current_bit_count = 0;
844 const struct processors * best_fit = NULL;
846 /* Ideally we would like to issue an error message here
847 saying that it was not possible to find a CPU compatible
848 with the default CPU, but which also supports the command
849 line options specified by the programmer, and so they
850 ought to use the -mcpu=<name> command line option to
851 override the default CPU type.
853 If we cannot find a cpu that has both the
854 characteristics of the default cpu and the given
855 command line options we scan the array again looking
856 for a best match. */
857 for (sel = all_cores; sel->name != NULL; sel++)
858 if ((sel->flags & sought) == sought)
860 unsigned count;
862 count = bit_count (sel->flags & insn_flags);
864 if (count >= current_bit_count)
866 best_fit = sel;
867 current_bit_count = count;
871 if (best_fit == NULL)
872 abort ();
873 else
874 sel = best_fit;
877 insn_flags = sel->flags;
879 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
880 if (arm_tune == arm_none)
881 arm_tune = (enum processor_type) (sel - all_cores);
884 /* The processor for which we should tune should now have been
885 chosen. */
886 if (arm_tune == arm_none)
887 abort ();
889 tune_flags = all_cores[(int)arm_tune].flags;
890 if (optimize_size)
891 targetm.rtx_costs = arm_size_rtx_costs;
892 else
893 targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
895 /* Make sure that the processor choice does not conflict with any of the
896 other command line choices. */
897 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
899 warning ("target CPU does not support interworking" );
900 target_flags &= ~ARM_FLAG_INTERWORK;
903 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
905 warning ("target CPU does not support THUMB instructions");
906 target_flags &= ~ARM_FLAG_THUMB;
909 if (TARGET_APCS_FRAME && TARGET_THUMB)
911 /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
912 target_flags &= ~ARM_FLAG_APCS_FRAME;
915 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
916 from here where no function is being compiled currently. */
917 if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
918 && TARGET_ARM)
919 warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
921 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
922 warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
924 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
925 warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
927 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
929 warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
930 target_flags |= ARM_FLAG_APCS_FRAME;
933 if (TARGET_POKE_FUNCTION_NAME)
934 target_flags |= ARM_FLAG_APCS_FRAME;
936 if (TARGET_APCS_REENT && flag_pic)
937 error ("-fpic and -mapcs-reent are incompatible");
939 if (TARGET_APCS_REENT)
940 warning ("APCS reentrant code not supported. Ignored");
942 /* If this target is normally configured to use APCS frames, warn if they
943 are turned off and debugging is turned on. */
944 if (TARGET_ARM
945 && write_symbols != NO_DEBUG
946 && !TARGET_APCS_FRAME
947 && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
948 warning ("-g with -mno-apcs-frame may not give sensible debugging");
950 /* If stack checking is disabled, we can use r10 as the PIC register,
951 which keeps r9 available. */
952 if (flag_pic)
953 arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
955 if (TARGET_APCS_FLOAT)
956 warning ("passing floating point arguments in fp regs not yet supported");
958 /* Initialize boolean versions of the flags, for use in the arm.md file. */
959 arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
960 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
961 arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
962 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
963 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
964 arm_arch6 = (insn_flags & FL_ARCH6) != 0;
965 arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
966 arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
968 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
969 arm_is_strong = (tune_flags & FL_STRONG) != 0;
970 thumb_code = (TARGET_ARM == 0);
971 arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
972 && !(tune_flags & FL_ARCH4))) != 0;
973 arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
974 arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
976 /* V5 code we generate is completely interworking capable, so we turn off
977 TARGET_INTERWORK here to avoid many tests later on. */
979 /* XXX However, we must pass the right pre-processor defines to CPP
980 or GLD can get confused. This is a hack. */
981 if (TARGET_INTERWORK)
982 arm_cpp_interwork = 1;
984 if (arm_arch5)
985 target_flags &= ~ARM_FLAG_INTERWORK;
987 if (target_abi_name)
989 for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
991 if (streq (arm_all_abis[i].name, target_abi_name))
993 arm_abi = arm_all_abis[i].abi_type;
994 break;
997 if (i == ARRAY_SIZE (arm_all_abis))
998 error ("invalid ABI option: -mabi=%s", target_abi_name);
1000 else
1001 arm_abi = ARM_DEFAULT_ABI;
1003 if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1004 error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1006 if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1007 error ("iwmmxt abi requires an iwmmxt capable cpu");
1009 arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1010 if (target_fpu_name == NULL && target_fpe_name != NULL)
1012 if (streq (target_fpe_name, "2"))
1013 target_fpu_name = "fpe2";
1014 else if (streq (target_fpe_name, "3"))
1015 target_fpu_name = "fpe3";
1016 else
1017 error ("invalid floating point emulation option: -mfpe=%s",
1018 target_fpe_name);
1020 if (target_fpu_name != NULL)
1022 /* The user specified a FPU. */
1023 for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1025 if (streq (all_fpus[i].name, target_fpu_name))
1027 arm_fpu_arch = all_fpus[i].fpu;
1028 arm_fpu_tune = arm_fpu_arch;
1029 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1030 break;
1033 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1034 error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1036 else
1038 #ifdef FPUTYPE_DEFAULT
1039 /* Use the default if it is specified for this platform. */
1040 arm_fpu_arch = FPUTYPE_DEFAULT;
1041 arm_fpu_tune = FPUTYPE_DEFAULT;
1042 #else
1043 /* Pick one based on CPU type. */
1044 /* ??? Some targets assume FPA is the default.
1045 if ((insn_flags & FL_VFP) != 0)
1046 arm_fpu_arch = FPUTYPE_VFP;
1047 else
1049 if (arm_arch_cirrus)
1050 arm_fpu_arch = FPUTYPE_MAVERICK;
1051 else
1052 arm_fpu_arch = FPUTYPE_FPA_EMU2;
1053 #endif
1054 if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1055 arm_fpu_tune = FPUTYPE_FPA;
1056 else
1057 arm_fpu_tune = arm_fpu_arch;
1058 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1059 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1060 abort ();
1063 if (target_float_abi_name != NULL)
1065 /* The user specified a FP ABI. */
1066 for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1068 if (streq (all_float_abis[i].name, target_float_abi_name))
1070 arm_float_abi = all_float_abis[i].abi_type;
1071 break;
1074 if (i == ARRAY_SIZE (all_float_abis))
1075 error ("invalid floating point abi: -mfloat-abi=%s",
1076 target_float_abi_name);
1078 else
1080 /* Use soft-float target flag. */
1081 if (target_flags & ARM_FLAG_SOFT_FLOAT)
1082 arm_float_abi = ARM_FLOAT_ABI_SOFT;
1083 else
1084 arm_float_abi = ARM_FLOAT_ABI_HARD;
1087 if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1088 sorry ("-mfloat-abi=hard and VFP");
1090 /* If soft-float is specified then don't use FPU. */
1091 if (TARGET_SOFT_FLOAT)
1092 arm_fpu_arch = FPUTYPE_NONE;
1094 /* For arm2/3 there is no need to do any scheduling if there is only
1095 a floating point emulator, or we are doing software floating-point. */
1096 if ((TARGET_SOFT_FLOAT
1097 || arm_fpu_tune == FPUTYPE_FPA_EMU2
1098 || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1099 && (tune_flags & FL_MODE32) == 0)
1100 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1102 /* Override the default structure alignment for AAPCS ABI. */
1103 if (arm_abi == ARM_ABI_AAPCS)
1104 arm_structure_size_boundary = 8;
1106 if (structure_size_string != NULL)
1108 int size = strtol (structure_size_string, NULL, 0);
1110 if (size == 8 || size == 32
1111 || (ARM_DOUBLEWORD_ALIGN && size == 64))
1112 arm_structure_size_boundary = size;
1113 else
1114 warning ("structure size boundary can only be set to %s",
1115 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1118 if (arm_pic_register_string != NULL)
1120 int pic_register = decode_reg_name (arm_pic_register_string);
1122 if (!flag_pic)
1123 warning ("-mpic-register= is useless without -fpic");
1125 /* Prevent the user from choosing an obviously stupid PIC register. */
1126 else if (pic_register < 0 || call_used_regs[pic_register]
1127 || pic_register == HARD_FRAME_POINTER_REGNUM
1128 || pic_register == STACK_POINTER_REGNUM
1129 || pic_register >= PC_REGNUM)
1130 error ("unable to use '%s' for PIC register", arm_pic_register_string);
1131 else
1132 arm_pic_register = pic_register;
1135 if (TARGET_THUMB && flag_schedule_insns)
1137 /* Don't warn since it's on by default in -O2. */
1138 flag_schedule_insns = 0;
1141 if (optimize_size)
1143 /* There's some dispute as to whether this should be 1 or 2. However,
1144 experiments seem to show that in pathological cases a setting of
1145 1 degrades less severely than a setting of 2. This could change if
1146 other parts of the compiler change their behavior. */
1147 arm_constant_limit = 1;
1149 /* If optimizing for size, bump the number of instructions that we
1150 are prepared to conditionally execute (even on a StrongARM). */
1151 max_insns_skipped = 6;
1153 else
1155 /* For processors with load scheduling, it never costs more than
1156 2 cycles to load a constant, and the load scheduler may well
1157 reduce that to 1. */
1158 if (tune_flags & FL_LDSCHED)
1159 arm_constant_limit = 1;
1161 /* On XScale the longer latency of a load makes it more difficult
1162 to achieve a good schedule, so it's faster to synthesize
1163 constants that can be done in two insns. */
1164 if (arm_tune_xscale)
1165 arm_constant_limit = 2;
1167 /* StrongARM has early execution of branches, so a sequence
1168 that is worth skipping is shorter. */
1169 if (arm_is_strong)
1170 max_insns_skipped = 3;
1173 /* Register global variables with the garbage collector. */
1174 arm_add_gc_roots ();
1177 static void
1178 arm_add_gc_roots (void)
1180 gcc_obstack_init(&minipool_obstack);
1181 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1184 /* A table of known ARM exception types.
1185 For use with the interrupt function attribute. */
1187 typedef struct
1189 const char *const arg;
1190 const unsigned long return_value;
1192 isr_attribute_arg;
1194 static const isr_attribute_arg isr_attribute_args [] =
1196 { "IRQ", ARM_FT_ISR },
1197 { "irq", ARM_FT_ISR },
1198 { "FIQ", ARM_FT_FIQ },
1199 { "fiq", ARM_FT_FIQ },
1200 { "ABORT", ARM_FT_ISR },
1201 { "abort", ARM_FT_ISR },
1202 { "ABORT", ARM_FT_ISR },
1203 { "abort", ARM_FT_ISR },
1204 { "UNDEF", ARM_FT_EXCEPTION },
1205 { "undef", ARM_FT_EXCEPTION },
1206 { "SWI", ARM_FT_EXCEPTION },
1207 { "swi", ARM_FT_EXCEPTION },
1208 { NULL, ARM_FT_NORMAL }
1211 /* Returns the (interrupt) function type of the current
1212 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
1214 static unsigned long
1215 arm_isr_value (tree argument)
1217 const isr_attribute_arg * ptr;
1218 const char * arg;
1220 /* No argument - default to IRQ. */
1221 if (argument == NULL_TREE)
1222 return ARM_FT_ISR;
1224 /* Get the value of the argument. */
1225 if (TREE_VALUE (argument) == NULL_TREE
1226 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1227 return ARM_FT_UNKNOWN;
1229 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1231 /* Check it against the list of known arguments. */
1232 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1233 if (streq (arg, ptr->arg))
1234 return ptr->return_value;
1236 /* An unrecognized interrupt type. */
1237 return ARM_FT_UNKNOWN;
1240 /* Computes the type of the current function. */
1242 static unsigned long
1243 arm_compute_func_type (void)
1245 unsigned long type = ARM_FT_UNKNOWN;
1246 tree a;
1247 tree attr;
1249 if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
1250 abort ();
1252 /* Decide if the current function is volatile. Such functions
1253 never return, and many memory cycles can be saved by not storing
1254 register values that will never be needed again. This optimization
1255 was added to speed up context switching in a kernel application. */
1256 if (optimize > 0
1257 && TREE_NOTHROW (current_function_decl)
1258 && TREE_THIS_VOLATILE (current_function_decl))
1259 type |= ARM_FT_VOLATILE;
1261 if (cfun->static_chain_decl != NULL)
1262 type |= ARM_FT_NESTED;
1264 attr = DECL_ATTRIBUTES (current_function_decl);
1266 a = lookup_attribute ("naked", attr);
1267 if (a != NULL_TREE)
1268 type |= ARM_FT_NAKED;
1270 a = lookup_attribute ("isr", attr);
1271 if (a == NULL_TREE)
1272 a = lookup_attribute ("interrupt", attr);
1274 if (a == NULL_TREE)
1275 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1276 else
1277 type |= arm_isr_value (TREE_VALUE (a));
1279 return type;
1282 /* Returns the type of the current function. */
1284 unsigned long
1285 arm_current_func_type (void)
1287 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1288 cfun->machine->func_type = arm_compute_func_type ();
1290 return cfun->machine->func_type;
1293 /* Return 1 if it is possible to return using a single instruction.
1294 If SIBLING is non-null, this is a test for a return before a sibling
1295 call. SIBLING is the call insn, so we can examine its register usage. */
1298 use_return_insn (int iscond, rtx sibling)
1300 int regno;
1301 unsigned int func_type;
1302 unsigned long saved_int_regs;
1303 unsigned HOST_WIDE_INT stack_adjust;
1304 arm_stack_offsets *offsets;
1306 /* Never use a return instruction before reload has run. */
1307 if (!reload_completed)
1308 return 0;
1310 func_type = arm_current_func_type ();
1312 /* Naked functions and volatile functions need special
1313 consideration. */
1314 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1315 return 0;
1317 /* So do interrupt functions that use the frame pointer. */
1318 if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1319 return 0;
1321 offsets = arm_get_frame_offsets ();
1322 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1324 /* As do variadic functions. */
1325 if (current_function_pretend_args_size
1326 || cfun->machine->uses_anonymous_args
1327 /* Or if the function calls __builtin_eh_return () */
1328 || current_function_calls_eh_return
1329 /* Or if the function calls alloca */
1330 || current_function_calls_alloca
1331 /* Or if there is a stack adjustment. However, if the stack pointer
1332 is saved on the stack, we can use a pre-incrementing stack load. */
1333 || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1334 return 0;
1336 saved_int_regs = arm_compute_save_reg_mask ();
1338 /* Unfortunately, the insn
1340 ldmib sp, {..., sp, ...}
1342 triggers a bug on most SA-110 based devices, such that the stack
1343 pointer won't be correctly restored if the instruction takes a
1344 page fault. We work around this problem by popping r3 along with
1345 the other registers, since that is never slower than executing
1346 another instruction.
1348 We test for !arm_arch5 here, because code for any architecture
1349 less than this could potentially be run on one of the buggy
1350 chips. */
1351 if (stack_adjust == 4 && !arm_arch5)
1353 /* Validate that r3 is a call-clobbered register (always true in
1354 the default abi) ... */
1355 if (!call_used_regs[3])
1356 return 0;
1358 /* ... that it isn't being used for a return value (always true
1359 until we implement return-in-regs), or for a tail-call
1360 argument ... */
1361 if (sibling)
1363 if (GET_CODE (sibling) != CALL_INSN)
1364 abort ();
1366 if (find_regno_fusage (sibling, USE, 3))
1367 return 0;
1370 /* ... and that there are no call-saved registers in r0-r2
1371 (always true in the default ABI). */
1372 if (saved_int_regs & 0x7)
1373 return 0;
1376 /* Can't be done if interworking with Thumb, and any registers have been
1377 stacked. */
1378 if (TARGET_INTERWORK && saved_int_regs != 0)
1379 return 0;
1381 /* On StrongARM, conditional returns are expensive if they aren't
1382 taken and multiple registers have been stacked. */
1383 if (iscond && arm_is_strong)
1385 /* Conditional return when just the LR is stored is a simple
1386 conditional-load instruction, that's not expensive. */
1387 if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1388 return 0;
1390 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1391 return 0;
1394 /* If there are saved registers but the LR isn't saved, then we need
1395 two instructions for the return. */
1396 if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1397 return 0;
1399 /* Can't be done if any of the FPA regs are pushed,
1400 since this also requires an insn. */
1401 if (TARGET_HARD_FLOAT && TARGET_FPA)
1402 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1403 if (regs_ever_live[regno] && !call_used_regs[regno])
1404 return 0;
1406 /* Likewise VFP regs. */
1407 if (TARGET_HARD_FLOAT && TARGET_VFP)
1408 for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1409 if (regs_ever_live[regno] && !call_used_regs[regno])
1410 return 0;
1412 if (TARGET_REALLY_IWMMXT)
1413 for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1414 if (regs_ever_live[regno] && ! call_used_regs [regno])
1415 return 0;
1417 return 1;
1420 /* Return TRUE if int I is a valid immediate ARM constant. */
1423 const_ok_for_arm (HOST_WIDE_INT i)
1425 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1427 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1428 be all zero, or all one. */
1429 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1430 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1431 != ((~(unsigned HOST_WIDE_INT) 0)
1432 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1433 return FALSE;
1435 /* Fast return for 0 and powers of 2 */
1436 if ((i & (i - 1)) == 0)
1437 return TRUE;
1441 if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1442 return TRUE;
1443 mask =
1444 (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1445 >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1447 while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1449 return FALSE;
1452 /* Return true if I is a valid constant for the operation CODE. */
1453 static int
1454 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1456 if (const_ok_for_arm (i))
1457 return 1;
1459 switch (code)
1461 case PLUS:
1462 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1464 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
1465 case XOR:
1466 case IOR:
1467 return 0;
1469 case AND:
1470 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1472 default:
1473 abort ();
1477 /* Emit a sequence of insns to handle a large constant.
1478 CODE is the code of the operation required, it can be any of SET, PLUS,
1479 IOR, AND, XOR, MINUS;
1480 MODE is the mode in which the operation is being performed;
1481 VAL is the integer to operate on;
1482 SOURCE is the other operand (a register, or a null-pointer for SET);
1483 SUBTARGETS means it is safe to create scratch registers if that will
1484 either produce a simpler sequence, or we will want to cse the values.
1485 Return value is the number of insns emitted. */
1488 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1489 HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1491 rtx cond;
1493 if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1494 cond = COND_EXEC_TEST (PATTERN (insn));
1495 else
1496 cond = NULL_RTX;
1498 if (subtargets || code == SET
1499 || (GET_CODE (target) == REG && GET_CODE (source) == REG
1500 && REGNO (target) != REGNO (source)))
1502 /* After arm_reorg has been called, we can't fix up expensive
1503 constants by pushing them into memory so we must synthesize
1504 them in-line, regardless of the cost. This is only likely to
1505 be more costly on chips that have load delay slots and we are
1506 compiling without running the scheduler (so no splitting
1507 occurred before the final instruction emission).
1509 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1511 if (!after_arm_reorg
1512 && !cond
1513 && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1514 1, 0)
1515 > arm_constant_limit + (code != SET)))
1517 if (code == SET)
1519 /* Currently SET is the only monadic value for CODE, all
1520 the rest are diadic. */
1521 emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1522 return 1;
1524 else
1526 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1528 emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1529 /* For MINUS, the value is subtracted from, since we never
1530 have subtraction of a constant. */
1531 if (code == MINUS)
1532 emit_insn (gen_rtx_SET (VOIDmode, target,
1533 gen_rtx_MINUS (mode, temp, source)));
1534 else
1535 emit_insn (gen_rtx_SET (VOIDmode, target,
1536 gen_rtx_fmt_ee (code, mode, source, temp)));
1537 return 2;
1542 return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1546 static int
1547 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1549 HOST_WIDE_INT temp1;
1550 int num_insns = 0;
1553 int end;
1555 if (i <= 0)
1556 i += 32;
1557 if (remainder & (3 << (i - 2)))
1559 end = i - 8;
1560 if (end < 0)
1561 end += 32;
1562 temp1 = remainder & ((0x0ff << end)
1563 | ((i < end) ? (0xff >> (32 - end)) : 0));
1564 remainder &= ~temp1;
1565 num_insns++;
1566 i -= 6;
1568 i -= 2;
1569 } while (remainder);
1570 return num_insns;
1573 /* Emit an instruction with the indicated PATTERN. If COND is
1574 non-NULL, conditionalize the execution of the instruction on COND
1575 being true. */
1577 static void
1578 emit_constant_insn (rtx cond, rtx pattern)
1580 if (cond)
1581 pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1582 emit_insn (pattern);
1585 /* As above, but extra parameter GENERATE which, if clear, suppresses
1586 RTL generation. */
1588 static int
1589 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1590 HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1591 int generate)
1593 int can_invert = 0;
1594 int can_negate = 0;
1595 int can_negate_initial = 0;
1596 int can_shift = 0;
1597 int i;
1598 int num_bits_set = 0;
1599 int set_sign_bit_copies = 0;
1600 int clear_sign_bit_copies = 0;
1601 int clear_zero_bit_copies = 0;
1602 int set_zero_bit_copies = 0;
1603 int insns = 0;
1604 unsigned HOST_WIDE_INT temp1, temp2;
1605 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1607 /* Find out which operations are safe for a given CODE. Also do a quick
1608 check for degenerate cases; these can occur when DImode operations
1609 are split. */
1610 switch (code)
1612 case SET:
1613 can_invert = 1;
1614 can_shift = 1;
1615 can_negate = 1;
1616 break;
1618 case PLUS:
1619 can_negate = 1;
1620 can_negate_initial = 1;
1621 break;
1623 case IOR:
1624 if (remainder == 0xffffffff)
1626 if (generate)
1627 emit_constant_insn (cond,
1628 gen_rtx_SET (VOIDmode, target,
1629 GEN_INT (ARM_SIGN_EXTEND (val))));
1630 return 1;
1632 if (remainder == 0)
1634 if (reload_completed && rtx_equal_p (target, source))
1635 return 0;
1636 if (generate)
1637 emit_constant_insn (cond,
1638 gen_rtx_SET (VOIDmode, target, source));
1639 return 1;
1641 break;
1643 case AND:
1644 if (remainder == 0)
1646 if (generate)
1647 emit_constant_insn (cond,
1648 gen_rtx_SET (VOIDmode, target, const0_rtx));
1649 return 1;
1651 if (remainder == 0xffffffff)
1653 if (reload_completed && rtx_equal_p (target, source))
1654 return 0;
1655 if (generate)
1656 emit_constant_insn (cond,
1657 gen_rtx_SET (VOIDmode, target, source));
1658 return 1;
1660 can_invert = 1;
1661 break;
1663 case XOR:
1664 if (remainder == 0)
1666 if (reload_completed && rtx_equal_p (target, source))
1667 return 0;
1668 if (generate)
1669 emit_constant_insn (cond,
1670 gen_rtx_SET (VOIDmode, target, source));
1671 return 1;
1673 if (remainder == 0xffffffff)
1675 if (generate)
1676 emit_constant_insn (cond,
1677 gen_rtx_SET (VOIDmode, target,
1678 gen_rtx_NOT (mode, source)));
1679 return 1;
1682 /* We don't know how to handle this yet below. */
1683 abort ();
1685 case MINUS:
1686 /* We treat MINUS as (val - source), since (source - val) is always
1687 passed as (source + (-val)). */
1688 if (remainder == 0)
1690 if (generate)
1691 emit_constant_insn (cond,
1692 gen_rtx_SET (VOIDmode, target,
1693 gen_rtx_NEG (mode, source)));
1694 return 1;
1696 if (const_ok_for_arm (val))
1698 if (generate)
1699 emit_constant_insn (cond,
1700 gen_rtx_SET (VOIDmode, target,
1701 gen_rtx_MINUS (mode, GEN_INT (val),
1702 source)));
1703 return 1;
1705 can_negate = 1;
1707 break;
1709 default:
1710 abort ();
1713 /* If we can do it in one insn get out quickly. */
1714 if (const_ok_for_arm (val)
1715 || (can_negate_initial && const_ok_for_arm (-val))
1716 || (can_invert && const_ok_for_arm (~val)))
1718 if (generate)
1719 emit_constant_insn (cond,
1720 gen_rtx_SET (VOIDmode, target,
1721 (source
1722 ? gen_rtx_fmt_ee (code, mode, source,
1723 GEN_INT (val))
1724 : GEN_INT (val))));
1725 return 1;
1728 /* Calculate a few attributes that may be useful for specific
1729 optimizations. */
1730 for (i = 31; i >= 0; i--)
1732 if ((remainder & (1 << i)) == 0)
1733 clear_sign_bit_copies++;
1734 else
1735 break;
1738 for (i = 31; i >= 0; i--)
1740 if ((remainder & (1 << i)) != 0)
1741 set_sign_bit_copies++;
1742 else
1743 break;
1746 for (i = 0; i <= 31; i++)
1748 if ((remainder & (1 << i)) == 0)
1749 clear_zero_bit_copies++;
1750 else
1751 break;
1754 for (i = 0; i <= 31; i++)
1756 if ((remainder & (1 << i)) != 0)
1757 set_zero_bit_copies++;
1758 else
1759 break;
1762 switch (code)
1764 case SET:
1765 /* See if we can do this by sign_extending a constant that is known
1766 to be negative. This is a good, way of doing it, since the shift
1767 may well merge into a subsequent insn. */
1768 if (set_sign_bit_copies > 1)
1770 if (const_ok_for_arm
1771 (temp1 = ARM_SIGN_EXTEND (remainder
1772 << (set_sign_bit_copies - 1))))
1774 if (generate)
1776 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1777 emit_constant_insn (cond,
1778 gen_rtx_SET (VOIDmode, new_src,
1779 GEN_INT (temp1)));
1780 emit_constant_insn (cond,
1781 gen_ashrsi3 (target, new_src,
1782 GEN_INT (set_sign_bit_copies - 1)));
1784 return 2;
1786 /* For an inverted constant, we will need to set the low bits,
1787 these will be shifted out of harm's way. */
1788 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1789 if (const_ok_for_arm (~temp1))
1791 if (generate)
1793 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1794 emit_constant_insn (cond,
1795 gen_rtx_SET (VOIDmode, new_src,
1796 GEN_INT (temp1)));
1797 emit_constant_insn (cond,
1798 gen_ashrsi3 (target, new_src,
1799 GEN_INT (set_sign_bit_copies - 1)));
1801 return 2;
1805 /* See if we can generate this by setting the bottom (or the top)
1806 16 bits, and then shifting these into the other half of the
1807 word. We only look for the simplest cases, to do more would cost
1808 too much. Be careful, however, not to generate this when the
1809 alternative would take fewer insns. */
1810 if (val & 0xffff0000)
1812 temp1 = remainder & 0xffff0000;
1813 temp2 = remainder & 0x0000ffff;
1815 /* Overlaps outside this range are best done using other methods. */
1816 for (i = 9; i < 24; i++)
1818 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1819 && !const_ok_for_arm (temp2))
1821 rtx new_src = (subtargets
1822 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1823 : target);
1824 insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1825 source, subtargets, generate);
1826 source = new_src;
1827 if (generate)
1828 emit_constant_insn
1829 (cond,
1830 gen_rtx_SET
1831 (VOIDmode, target,
1832 gen_rtx_IOR (mode,
1833 gen_rtx_ASHIFT (mode, source,
1834 GEN_INT (i)),
1835 source)));
1836 return insns + 1;
1840 /* Don't duplicate cases already considered. */
1841 for (i = 17; i < 24; i++)
1843 if (((temp1 | (temp1 >> i)) == remainder)
1844 && !const_ok_for_arm (temp1))
1846 rtx new_src = (subtargets
1847 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1848 : target);
1849 insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1850 source, subtargets, generate);
1851 source = new_src;
1852 if (generate)
1853 emit_constant_insn
1854 (cond,
1855 gen_rtx_SET (VOIDmode, target,
1856 gen_rtx_IOR
1857 (mode,
1858 gen_rtx_LSHIFTRT (mode, source,
1859 GEN_INT (i)),
1860 source)));
1861 return insns + 1;
1865 break;
1867 case IOR:
1868 case XOR:
1869 /* If we have IOR or XOR, and the constant can be loaded in a
1870 single instruction, and we can find a temporary to put it in,
1871 then this can be done in two instructions instead of 3-4. */
1872 if (subtargets
1873 /* TARGET can't be NULL if SUBTARGETS is 0 */
1874 || (reload_completed && !reg_mentioned_p (target, source)))
1876 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1878 if (generate)
1880 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1882 emit_constant_insn (cond,
1883 gen_rtx_SET (VOIDmode, sub,
1884 GEN_INT (val)));
1885 emit_constant_insn (cond,
1886 gen_rtx_SET (VOIDmode, target,
1887 gen_rtx_fmt_ee (code, mode,
1888 source, sub)));
1890 return 2;
1894 if (code == XOR)
1895 break;
1897 if (set_sign_bit_copies > 8
1898 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1900 if (generate)
1902 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1903 rtx shift = GEN_INT (set_sign_bit_copies);
1905 emit_constant_insn
1906 (cond,
1907 gen_rtx_SET (VOIDmode, sub,
1908 gen_rtx_NOT (mode,
1909 gen_rtx_ASHIFT (mode,
1910 source,
1911 shift))));
1912 emit_constant_insn
1913 (cond,
1914 gen_rtx_SET (VOIDmode, target,
1915 gen_rtx_NOT (mode,
1916 gen_rtx_LSHIFTRT (mode, sub,
1917 shift))));
1919 return 2;
1922 if (set_zero_bit_copies > 8
1923 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1925 if (generate)
1927 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1928 rtx shift = GEN_INT (set_zero_bit_copies);
1930 emit_constant_insn
1931 (cond,
1932 gen_rtx_SET (VOIDmode, sub,
1933 gen_rtx_NOT (mode,
1934 gen_rtx_LSHIFTRT (mode,
1935 source,
1936 shift))));
1937 emit_constant_insn
1938 (cond,
1939 gen_rtx_SET (VOIDmode, target,
1940 gen_rtx_NOT (mode,
1941 gen_rtx_ASHIFT (mode, sub,
1942 shift))));
1944 return 2;
1947 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1949 if (generate)
1951 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1952 emit_constant_insn (cond,
1953 gen_rtx_SET (VOIDmode, sub,
1954 gen_rtx_NOT (mode, source)));
1955 source = sub;
1956 if (subtargets)
1957 sub = gen_reg_rtx (mode);
1958 emit_constant_insn (cond,
1959 gen_rtx_SET (VOIDmode, sub,
1960 gen_rtx_AND (mode, source,
1961 GEN_INT (temp1))));
1962 emit_constant_insn (cond,
1963 gen_rtx_SET (VOIDmode, target,
1964 gen_rtx_NOT (mode, sub)));
1966 return 3;
1968 break;
1970 case AND:
1971 /* See if two shifts will do 2 or more insn's worth of work. */
1972 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1974 HOST_WIDE_INT shift_mask = ((0xffffffff
1975 << (32 - clear_sign_bit_copies))
1976 & 0xffffffff);
1978 if ((remainder | shift_mask) != 0xffffffff)
1980 if (generate)
1982 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1983 insns = arm_gen_constant (AND, mode, cond,
1984 remainder | shift_mask,
1985 new_src, source, subtargets, 1);
1986 source = new_src;
1988 else
1990 rtx targ = subtargets ? NULL_RTX : target;
1991 insns = arm_gen_constant (AND, mode, cond,
1992 remainder | shift_mask,
1993 targ, source, subtargets, 0);
1997 if (generate)
1999 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2000 rtx shift = GEN_INT (clear_sign_bit_copies);
2002 emit_insn (gen_ashlsi3 (new_src, source, shift));
2003 emit_insn (gen_lshrsi3 (target, new_src, shift));
2006 return insns + 2;
2009 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2011 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2013 if ((remainder | shift_mask) != 0xffffffff)
2015 if (generate)
2017 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2019 insns = arm_gen_constant (AND, mode, cond,
2020 remainder | shift_mask,
2021 new_src, source, subtargets, 1);
2022 source = new_src;
2024 else
2026 rtx targ = subtargets ? NULL_RTX : target;
2028 insns = arm_gen_constant (AND, mode, cond,
2029 remainder | shift_mask,
2030 targ, source, subtargets, 0);
2034 if (generate)
2036 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2037 rtx shift = GEN_INT (clear_zero_bit_copies);
2039 emit_insn (gen_lshrsi3 (new_src, source, shift));
2040 emit_insn (gen_ashlsi3 (target, new_src, shift));
2043 return insns + 2;
2046 break;
2048 default:
2049 break;
2052 for (i = 0; i < 32; i++)
2053 if (remainder & (1 << i))
2054 num_bits_set++;
2056 if (code == AND || (can_invert && num_bits_set > 16))
2057 remainder = (~remainder) & 0xffffffff;
2058 else if (code == PLUS && num_bits_set > 16)
2059 remainder = (-remainder) & 0xffffffff;
2060 else
2062 can_invert = 0;
2063 can_negate = 0;
2066 /* Now try and find a way of doing the job in either two or three
2067 instructions.
2068 We start by looking for the largest block of zeros that are aligned on
2069 a 2-bit boundary, we then fill up the temps, wrapping around to the
2070 top of the word when we drop off the bottom.
2071 In the worst case this code should produce no more than four insns. */
2073 int best_start = 0;
2074 int best_consecutive_zeros = 0;
2076 for (i = 0; i < 32; i += 2)
2078 int consecutive_zeros = 0;
2080 if (!(remainder & (3 << i)))
2082 while ((i < 32) && !(remainder & (3 << i)))
2084 consecutive_zeros += 2;
2085 i += 2;
2087 if (consecutive_zeros > best_consecutive_zeros)
2089 best_consecutive_zeros = consecutive_zeros;
2090 best_start = i - consecutive_zeros;
2092 i -= 2;
2096 /* So long as it won't require any more insns to do so, it's
2097 desirable to emit a small constant (in bits 0...9) in the last
2098 insn. This way there is more chance that it can be combined with
2099 a later addressing insn to form a pre-indexed load or store
2100 operation. Consider:
2102 *((volatile int *)0xe0000100) = 1;
2103 *((volatile int *)0xe0000110) = 2;
2105 We want this to wind up as:
2107 mov rA, #0xe0000000
2108 mov rB, #1
2109 str rB, [rA, #0x100]
2110 mov rB, #2
2111 str rB, [rA, #0x110]
2113 rather than having to synthesize both large constants from scratch.
2115 Therefore, we calculate how many insns would be required to emit
2116 the constant starting from `best_start', and also starting from
2117 zero (i.e. with bit 31 first to be output). If `best_start' doesn't
2118 yield a shorter sequence, we may as well use zero. */
2119 if (best_start != 0
2120 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2121 && (count_insns_for_constant (remainder, 0) <=
2122 count_insns_for_constant (remainder, best_start)))
2123 best_start = 0;
2125 /* Now start emitting the insns. */
2126 i = best_start;
2129 int end;
2131 if (i <= 0)
2132 i += 32;
2133 if (remainder & (3 << (i - 2)))
2135 end = i - 8;
2136 if (end < 0)
2137 end += 32;
2138 temp1 = remainder & ((0x0ff << end)
2139 | ((i < end) ? (0xff >> (32 - end)) : 0));
2140 remainder &= ~temp1;
2142 if (generate)
2144 rtx new_src, temp1_rtx;
2146 if (code == SET || code == MINUS)
2148 new_src = (subtargets ? gen_reg_rtx (mode) : target);
2149 if (can_invert && code != MINUS)
2150 temp1 = ~temp1;
2152 else
2154 if (remainder && subtargets)
2155 new_src = gen_reg_rtx (mode);
2156 else
2157 new_src = target;
2158 if (can_invert)
2159 temp1 = ~temp1;
2160 else if (can_negate)
2161 temp1 = -temp1;
2164 temp1 = trunc_int_for_mode (temp1, mode);
2165 temp1_rtx = GEN_INT (temp1);
2167 if (code == SET)
2169 else if (code == MINUS)
2170 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2171 else
2172 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2174 emit_constant_insn (cond,
2175 gen_rtx_SET (VOIDmode, new_src,
2176 temp1_rtx));
2177 source = new_src;
2180 if (code == SET)
2182 can_invert = 0;
2183 code = PLUS;
2185 else if (code == MINUS)
2186 code = PLUS;
2188 insns++;
2189 i -= 6;
2191 i -= 2;
2193 while (remainder);
2196 return insns;
2199 /* Canonicalize a comparison so that we are more likely to recognize it.
2200 This can be done for a few constant compares, where we can make the
2201 immediate value easier to load. */
2203 enum rtx_code
2204 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2206 unsigned HOST_WIDE_INT i = INTVAL (*op1);
2208 switch (code)
2210 case EQ:
2211 case NE:
2212 return code;
2214 case GT:
2215 case LE:
2216 if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2217 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2219 *op1 = GEN_INT (i + 1);
2220 return code == GT ? GE : LT;
2222 break;
2224 case GE:
2225 case LT:
2226 if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2227 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2229 *op1 = GEN_INT (i - 1);
2230 return code == GE ? GT : LE;
2232 break;
2234 case GTU:
2235 case LEU:
2236 if (i != ~((unsigned HOST_WIDE_INT) 0)
2237 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2239 *op1 = GEN_INT (i + 1);
2240 return code == GTU ? GEU : LTU;
2242 break;
2244 case GEU:
2245 case LTU:
2246 if (i != 0
2247 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2249 *op1 = GEN_INT (i - 1);
2250 return code == GEU ? GTU : LEU;
2252 break;
2254 default:
2255 abort ();
2258 return code;
2262 /* Define how to find the value returned by a function. */
2264 rtx arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2266 enum machine_mode mode;
2267 int unsignedp ATTRIBUTE_UNUSED;
2268 rtx r ATTRIBUTE_UNUSED;
2271 mode = TYPE_MODE (type);
2272 /* Promote integer types. */
2273 if (INTEGRAL_TYPE_P (type))
2274 PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2275 return LIBCALL_VALUE(mode);
2279 /* Decide whether a type should be returned in memory (true)
2280 or in a register (false). This is called by the macro
2281 RETURN_IN_MEMORY. */
2283 arm_return_in_memory (tree type)
2285 HOST_WIDE_INT size;
2287 if (!AGGREGATE_TYPE_P (type))
2288 /* All simple types are returned in registers. */
2289 return 0;
2291 size = int_size_in_bytes (type);
2293 if (arm_abi != ARM_ABI_APCS)
2295 /* ATPCS and later return aggregate types in memory only if they are
2296 larger than a word (or are variable size). */
2297 return (size < 0 || size > UNITS_PER_WORD);
2300 /* For the arm-wince targets we choose to be compatible with Microsoft's
2301 ARM and Thumb compilers, which always return aggregates in memory. */
2302 #ifndef ARM_WINCE
2303 /* All structures/unions bigger than one word are returned in memory.
2304 Also catch the case where int_size_in_bytes returns -1. In this case
2305 the aggregate is either huge or of variable size, and in either case
2306 we will want to return it via memory and not in a register. */
2307 if (size < 0 || size > UNITS_PER_WORD)
2308 return 1;
2310 if (TREE_CODE (type) == RECORD_TYPE)
2312 tree field;
2314 /* For a struct the APCS says that we only return in a register
2315 if the type is 'integer like' and every addressable element
2316 has an offset of zero. For practical purposes this means
2317 that the structure can have at most one non bit-field element
2318 and that this element must be the first one in the structure. */
2320 /* Find the first field, ignoring non FIELD_DECL things which will
2321 have been created by C++. */
2322 for (field = TYPE_FIELDS (type);
2323 field && TREE_CODE (field) != FIELD_DECL;
2324 field = TREE_CHAIN (field))
2325 continue;
2327 if (field == NULL)
2328 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
2330 /* Check that the first field is valid for returning in a register. */
2332 /* ... Floats are not allowed */
2333 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2334 return 1;
2336 /* ... Aggregates that are not themselves valid for returning in
2337 a register are not allowed. */
2338 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2339 return 1;
2341 /* Now check the remaining fields, if any. Only bitfields are allowed,
2342 since they are not addressable. */
2343 for (field = TREE_CHAIN (field);
2344 field;
2345 field = TREE_CHAIN (field))
2347 if (TREE_CODE (field) != FIELD_DECL)
2348 continue;
2350 if (!DECL_BIT_FIELD_TYPE (field))
2351 return 1;
2354 return 0;
2357 if (TREE_CODE (type) == UNION_TYPE)
2359 tree field;
2361 /* Unions can be returned in registers if every element is
2362 integral, or can be returned in an integer register. */
2363 for (field = TYPE_FIELDS (type);
2364 field;
2365 field = TREE_CHAIN (field))
2367 if (TREE_CODE (field) != FIELD_DECL)
2368 continue;
2370 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2371 return 1;
2373 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2374 return 1;
2377 return 0;
2379 #endif /* not ARM_WINCE */
2381 /* Return all other types in memory. */
2382 return 1;
2385 /* Indicate whether or not words of a double are in big-endian order. */
2388 arm_float_words_big_endian (void)
2390 if (TARGET_MAVERICK)
2391 return 0;
2393 /* For FPA, float words are always big-endian. For VFP, floats words
2394 follow the memory system mode. */
2396 if (TARGET_FPA)
2398 return 1;
2401 if (TARGET_VFP)
2402 return (TARGET_BIG_END ? 1 : 0);
2404 return 1;
2407 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2408 for a call to a function whose data type is FNTYPE.
2409 For a library call, FNTYPE is NULL. */
2410 void
2411 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2412 rtx libname ATTRIBUTE_UNUSED,
2413 tree fndecl ATTRIBUTE_UNUSED)
2415 /* On the ARM, the offset starts at 0. */
2416 pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2417 pcum->iwmmxt_nregs = 0;
2418 pcum->can_split = true;
2420 pcum->call_cookie = CALL_NORMAL;
2422 if (TARGET_LONG_CALLS)
2423 pcum->call_cookie = CALL_LONG;
2425 /* Check for long call/short call attributes. The attributes
2426 override any command line option. */
2427 if (fntype)
2429 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2430 pcum->call_cookie = CALL_SHORT;
2431 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2432 pcum->call_cookie = CALL_LONG;
2435 /* Varargs vectors are treated the same as long long.
2436 named_count avoids having to change the way arm handles 'named' */
2437 pcum->named_count = 0;
2438 pcum->nargs = 0;
2440 if (TARGET_REALLY_IWMMXT && fntype)
2442 tree fn_arg;
2444 for (fn_arg = TYPE_ARG_TYPES (fntype);
2445 fn_arg;
2446 fn_arg = TREE_CHAIN (fn_arg))
2447 pcum->named_count += 1;
2449 if (! pcum->named_count)
2450 pcum->named_count = INT_MAX;
2455 /* Return true if mode/type need doubleword alignment. */
2456 bool
2457 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2459 return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2460 || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2464 /* Determine where to put an argument to a function.
2465 Value is zero to push the argument on the stack,
2466 or a hard register in which to store the argument.
2468 MODE is the argument's machine mode.
2469 TYPE is the data type of the argument (as a tree).
2470 This is null for libcalls where that information may
2471 not be available.
2472 CUM is a variable of type CUMULATIVE_ARGS which gives info about
2473 the preceding args and about the function being called.
2474 NAMED is nonzero if this argument is a named parameter
2475 (otherwise it is an extra parameter matching an ellipsis). */
2478 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2479 tree type, int named)
2481 int nregs;
2483 /* Varargs vectors are treated the same as long long.
2484 named_count avoids having to change the way arm handles 'named' */
2485 if (TARGET_IWMMXT_ABI
2486 && arm_vector_mode_supported_p (mode)
2487 && pcum->named_count > pcum->nargs + 1)
2489 if (pcum->iwmmxt_nregs <= 9)
2490 return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2491 else
2493 pcum->can_split = false;
2494 return NULL_RTX;
2498 /* Put doubleword aligned quantities in even register pairs. */
2499 if (pcum->nregs & 1
2500 && ARM_DOUBLEWORD_ALIGN
2501 && arm_needs_doubleword_align (mode, type))
2502 pcum->nregs++;
2504 if (mode == VOIDmode)
2505 /* Compute operand 2 of the call insn. */
2506 return GEN_INT (pcum->call_cookie);
2508 /* Only allow splitting an arg between regs and memory if all preceding
2509 args were allocated to regs. For args passed by reference we only count
2510 the reference pointer. */
2511 if (pcum->can_split)
2512 nregs = 1;
2513 else
2514 nregs = ARM_NUM_REGS2 (mode, type);
2516 if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2517 return NULL_RTX;
2519 return gen_rtx_REG (mode, pcum->nregs);
2522 /* Variable sized types are passed by reference. This is a GCC
2523 extension to the ARM ABI. */
2525 static bool
2526 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2527 enum machine_mode mode ATTRIBUTE_UNUSED,
2528 tree type, bool named ATTRIBUTE_UNUSED)
2530 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2533 /* Encode the current state of the #pragma [no_]long_calls. */
2534 typedef enum
2536 OFF, /* No #pramgma [no_]long_calls is in effect. */
2537 LONG, /* #pragma long_calls is in effect. */
2538 SHORT /* #pragma no_long_calls is in effect. */
2539 } arm_pragma_enum;
2541 static arm_pragma_enum arm_pragma_long_calls = OFF;
2543 void
2544 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2546 arm_pragma_long_calls = LONG;
2549 void
2550 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2552 arm_pragma_long_calls = SHORT;
2555 void
2556 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2558 arm_pragma_long_calls = OFF;
2561 /* Table of machine attributes. */
2562 const struct attribute_spec arm_attribute_table[] =
2564 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2565 /* Function calls made to this symbol must be done indirectly, because
2566 it may lie outside of the 26 bit addressing range of a normal function
2567 call. */
2568 { "long_call", 0, 0, false, true, true, NULL },
2569 /* Whereas these functions are always known to reside within the 26 bit
2570 addressing range. */
2571 { "short_call", 0, 0, false, true, true, NULL },
2572 /* Interrupt Service Routines have special prologue and epilogue requirements. */
2573 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
2574 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
2575 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2576 #ifdef ARM_PE
2577 /* ARM/PE has three new attributes:
2578 interfacearm - ?
2579 dllexport - for exporting a function/variable that will live in a dll
2580 dllimport - for importing a function/variable from a dll
2582 Microsoft allows multiple declspecs in one __declspec, separating
2583 them with spaces. We do NOT support this. Instead, use __declspec
2584 multiple times.
2586 { "dllimport", 0, 0, true, false, false, NULL },
2587 { "dllexport", 0, 0, true, false, false, NULL },
2588 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2589 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2590 { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
2591 { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
2592 #endif
2593 { NULL, 0, 0, false, false, false, NULL }
2596 /* Handle an attribute requiring a FUNCTION_DECL;
2597 arguments as in struct attribute_spec.handler. */
2598 static tree
2599 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2600 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2602 if (TREE_CODE (*node) != FUNCTION_DECL)
2604 warning ("`%s' attribute only applies to functions",
2605 IDENTIFIER_POINTER (name));
2606 *no_add_attrs = true;
2609 return NULL_TREE;
2612 /* Handle an "interrupt" or "isr" attribute;
2613 arguments as in struct attribute_spec.handler. */
2614 static tree
2615 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2616 bool *no_add_attrs)
2618 if (DECL_P (*node))
2620 if (TREE_CODE (*node) != FUNCTION_DECL)
2622 warning ("`%s' attribute only applies to functions",
2623 IDENTIFIER_POINTER (name));
2624 *no_add_attrs = true;
2626 /* FIXME: the argument if any is checked for type attributes;
2627 should it be checked for decl ones? */
2629 else
2631 if (TREE_CODE (*node) == FUNCTION_TYPE
2632 || TREE_CODE (*node) == METHOD_TYPE)
2634 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2636 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2637 *no_add_attrs = true;
2640 else if (TREE_CODE (*node) == POINTER_TYPE
2641 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2642 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2643 && arm_isr_value (args) != ARM_FT_UNKNOWN)
2645 *node = build_variant_type_copy (*node);
2646 TREE_TYPE (*node) = build_type_attribute_variant
2647 (TREE_TYPE (*node),
2648 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2649 *no_add_attrs = true;
2651 else
2653 /* Possibly pass this attribute on from the type to a decl. */
2654 if (flags & ((int) ATTR_FLAG_DECL_NEXT
2655 | (int) ATTR_FLAG_FUNCTION_NEXT
2656 | (int) ATTR_FLAG_ARRAY_NEXT))
2658 *no_add_attrs = true;
2659 return tree_cons (name, args, NULL_TREE);
2661 else
2663 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
2668 return NULL_TREE;
2671 /* Return 0 if the attributes for two types are incompatible, 1 if they
2672 are compatible, and 2 if they are nearly compatible (which causes a
2673 warning to be generated). */
2674 static int
2675 arm_comp_type_attributes (tree type1, tree type2)
2677 int l1, l2, s1, s2;
2679 /* Check for mismatch of non-default calling convention. */
2680 if (TREE_CODE (type1) != FUNCTION_TYPE)
2681 return 1;
2683 /* Check for mismatched call attributes. */
2684 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2685 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2686 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2687 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2689 /* Only bother to check if an attribute is defined. */
2690 if (l1 | l2 | s1 | s2)
2692 /* If one type has an attribute, the other must have the same attribute. */
2693 if ((l1 != l2) || (s1 != s2))
2694 return 0;
2696 /* Disallow mixed attributes. */
2697 if ((l1 & s2) || (l2 & s1))
2698 return 0;
2701 /* Check for mismatched ISR attribute. */
2702 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2703 if (! l1)
2704 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2705 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2706 if (! l2)
2707 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2708 if (l1 != l2)
2709 return 0;
2711 return 1;
2714 /* Encode long_call or short_call attribute by prefixing
2715 symbol name in DECL with a special character FLAG. */
2716 void
2717 arm_encode_call_attribute (tree decl, int flag)
2719 const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2720 int len = strlen (str);
2721 char * newstr;
2723 /* Do not allow weak functions to be treated as short call. */
2724 if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2725 return;
2727 newstr = alloca (len + 2);
2728 newstr[0] = flag;
2729 strcpy (newstr + 1, str);
2731 newstr = (char *) ggc_alloc_string (newstr, len + 1);
2732 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2735 /* Assigns default attributes to newly defined type. This is used to
2736 set short_call/long_call attributes for function types of
2737 functions defined inside corresponding #pragma scopes. */
2738 static void
2739 arm_set_default_type_attributes (tree type)
2741 /* Add __attribute__ ((long_call)) to all functions, when
2742 inside #pragma long_calls or __attribute__ ((short_call)),
2743 when inside #pragma no_long_calls. */
2744 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2746 tree type_attr_list, attr_name;
2747 type_attr_list = TYPE_ATTRIBUTES (type);
2749 if (arm_pragma_long_calls == LONG)
2750 attr_name = get_identifier ("long_call");
2751 else if (arm_pragma_long_calls == SHORT)
2752 attr_name = get_identifier ("short_call");
2753 else
2754 return;
2756 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2757 TYPE_ATTRIBUTES (type) = type_attr_list;
2761 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2762 defined within the current compilation unit. If this cannot be
2763 determined, then 0 is returned. */
2764 static int
2765 current_file_function_operand (rtx sym_ref)
2767 /* This is a bit of a fib. A function will have a short call flag
2768 applied to its name if it has the short call attribute, or it has
2769 already been defined within the current compilation unit. */
2770 if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2771 return 1;
2773 /* The current function is always defined within the current compilation
2774 unit. If it s a weak definition however, then this may not be the real
2775 definition of the function, and so we have to say no. */
2776 if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2777 && !DECL_WEAK (current_function_decl))
2778 return 1;
2780 /* We cannot make the determination - default to returning 0. */
2781 return 0;
2784 /* Return nonzero if a 32 bit "long_call" should be generated for
2785 this call. We generate a long_call if the function:
2787 a. has an __attribute__((long call))
2788 or b. is within the scope of a #pragma long_calls
2789 or c. the -mlong-calls command line switch has been specified
2790 . and either:
2791 1. -ffunction-sections is in effect
2792 or 2. the current function has __attribute__ ((section))
2793 or 3. the target function has __attribute__ ((section))
2795 However we do not generate a long call if the function:
2797 d. has an __attribute__ ((short_call))
2798 or e. is inside the scope of a #pragma no_long_calls
2799 or f. is defined within the current compilation unit.
2801 This function will be called by C fragments contained in the machine
2802 description file. SYM_REF and CALL_COOKIE correspond to the matched
2803 rtl operands. CALL_SYMBOL is used to distinguish between
2804 two different callers of the function. It is set to 1 in the
2805 "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2806 and "call_value" patterns. This is because of the difference in the
2807 SYM_REFs passed by these patterns. */
2809 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2811 if (!call_symbol)
2813 if (GET_CODE (sym_ref) != MEM)
2814 return 0;
2816 sym_ref = XEXP (sym_ref, 0);
2819 if (GET_CODE (sym_ref) != SYMBOL_REF)
2820 return 0;
2822 if (call_cookie & CALL_SHORT)
2823 return 0;
2825 if (TARGET_LONG_CALLS)
2827 if (flag_function_sections
2828 || DECL_SECTION_NAME (current_function_decl))
2829 /* c.3 is handled by the definition of the
2830 ARM_DECLARE_FUNCTION_SIZE macro. */
2831 return 1;
2834 if (current_file_function_operand (sym_ref))
2835 return 0;
2837 return (call_cookie & CALL_LONG)
2838 || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2839 || TARGET_LONG_CALLS;
2842 /* Return nonzero if it is ok to make a tail-call to DECL. */
2843 static bool
2844 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
2846 int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2848 if (cfun->machine->sibcall_blocked)
2849 return false;
2851 /* Never tailcall something for which we have no decl, or if we
2852 are in Thumb mode. */
2853 if (decl == NULL || TARGET_THUMB)
2854 return false;
2856 /* Get the calling method. */
2857 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2858 call_type = CALL_SHORT;
2859 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2860 call_type = CALL_LONG;
2862 /* Cannot tail-call to long calls, since these are out of range of
2863 a branch instruction. However, if not compiling PIC, we know
2864 we can reach the symbol if it is in this compilation unit. */
2865 if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2866 return false;
2868 /* If we are interworking and the function is not declared static
2869 then we can't tail-call it unless we know that it exists in this
2870 compilation unit (since it might be a Thumb routine). */
2871 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2872 return false;
2874 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
2875 if (IS_INTERRUPT (arm_current_func_type ()))
2876 return false;
2878 /* Everything else is ok. */
2879 return true;
2883 /* Addressing mode support functions. */
2885 /* Return nonzero if X is a legitimate immediate operand when compiling
2886 for PIC. */
2888 legitimate_pic_operand_p (rtx x)
2890 if (CONSTANT_P (x)
2891 && flag_pic
2892 && (GET_CODE (x) == SYMBOL_REF
2893 || (GET_CODE (x) == CONST
2894 && GET_CODE (XEXP (x, 0)) == PLUS
2895 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2896 return 0;
2898 return 1;
2902 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
2904 if (GET_CODE (orig) == SYMBOL_REF
2905 || GET_CODE (orig) == LABEL_REF)
2907 #ifndef AOF_ASSEMBLER
2908 rtx pic_ref, address;
2909 #endif
2910 rtx insn;
2911 int subregs = 0;
2913 if (reg == 0)
2915 if (no_new_pseudos)
2916 abort ();
2917 else
2918 reg = gen_reg_rtx (Pmode);
2920 subregs = 1;
2923 #ifdef AOF_ASSEMBLER
2924 /* The AOF assembler can generate relocations for these directly, and
2925 understands that the PIC register has to be added into the offset. */
2926 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
2927 #else
2928 if (subregs)
2929 address = gen_reg_rtx (Pmode);
2930 else
2931 address = reg;
2933 if (TARGET_ARM)
2934 emit_insn (gen_pic_load_addr_arm (address, orig));
2935 else
2936 emit_insn (gen_pic_load_addr_thumb (address, orig));
2938 if ((GET_CODE (orig) == LABEL_REF
2939 || (GET_CODE (orig) == SYMBOL_REF &&
2940 SYMBOL_REF_LOCAL_P (orig)))
2941 && NEED_GOT_RELOC)
2942 pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
2943 else
2945 pic_ref = gen_const_mem (Pmode,
2946 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
2947 address));
2950 insn = emit_move_insn (reg, pic_ref);
2951 #endif
2952 current_function_uses_pic_offset_table = 1;
2953 /* Put a REG_EQUAL note on this insn, so that it can be optimized
2954 by loop. */
2955 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
2956 REG_NOTES (insn));
2957 return reg;
2959 else if (GET_CODE (orig) == CONST)
2961 rtx base, offset;
2963 if (GET_CODE (XEXP (orig, 0)) == PLUS
2964 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
2965 return orig;
2967 if (reg == 0)
2969 if (no_new_pseudos)
2970 abort ();
2971 else
2972 reg = gen_reg_rtx (Pmode);
2975 if (GET_CODE (XEXP (orig, 0)) == PLUS)
2977 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
2978 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
2979 base == reg ? 0 : reg);
2981 else
2982 abort ();
2984 if (GET_CODE (offset) == CONST_INT)
2986 /* The base register doesn't really matter, we only want to
2987 test the index for the appropriate mode. */
2988 if (!arm_legitimate_index_p (mode, offset, SET, 0))
2990 if (!no_new_pseudos)
2991 offset = force_reg (Pmode, offset);
2992 else
2993 abort ();
2996 if (GET_CODE (offset) == CONST_INT)
2997 return plus_constant (base, INTVAL (offset));
3000 if (GET_MODE_SIZE (mode) > 4
3001 && (GET_MODE_CLASS (mode) == MODE_INT
3002 || TARGET_SOFT_FLOAT))
3004 emit_insn (gen_addsi3 (reg, base, offset));
3005 return reg;
3008 return gen_rtx_PLUS (Pmode, base, offset);
3011 return orig;
3015 /* Find a spare low register. */
3017 static int
3018 thumb_find_work_register (int live_regs_mask)
3020 int reg;
3022 /* Use a spare arg register. */
3023 if (!regs_ever_live[LAST_ARG_REGNUM])
3024 return LAST_ARG_REGNUM;
3026 /* Look for a pushed register. This is used before the frame pointer is
3027 setup, so r7 is a candidate. */
3028 for (reg = LAST_LO_REGNUM; reg >=0; reg--)
3029 if (live_regs_mask & (1 << reg))
3030 return reg;
3032 /* Something went wrong. */
3033 abort ();
3037 /* Generate code to load the PIC register. In thumb mode SCRATCH is a
3038 low register. */
3040 void
3041 arm_load_pic_register (unsigned int scratch)
3043 #ifndef AOF_ASSEMBLER
3044 rtx l1, pic_tmp, pic_tmp2, pic_rtx;
3045 rtx global_offset_table;
3047 if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3048 return;
3050 if (!flag_pic)
3051 abort ();
3053 l1 = gen_label_rtx ();
3055 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3056 /* On the ARM the PC register contains 'dot + 8' at the time of the
3057 addition, on the Thumb it is 'dot + 4'. */
3058 pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
3059 if (GOT_PCREL)
3060 pic_tmp2 = gen_rtx_CONST (VOIDmode,
3061 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3062 else
3063 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3065 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3067 if (TARGET_ARM)
3069 emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3070 emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
3072 else
3074 if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3076 /* We will have pushed the pic register, so should always be
3077 able to find a work register. */
3078 pic_tmp = gen_rtx_REG (SImode, scratch);
3079 emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3080 emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3082 else
3083 emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3084 emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
3087 /* Need to emit this whether or not we obey regdecls,
3088 since setjmp/longjmp can cause life info to screw up. */
3089 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3090 #endif /* AOF_ASSEMBLER */
3094 /* Return nonzero if X is valid as an ARM state addressing register. */
3095 static int
3096 arm_address_register_rtx_p (rtx x, int strict_p)
3098 int regno;
3100 if (GET_CODE (x) != REG)
3101 return 0;
3103 regno = REGNO (x);
3105 if (strict_p)
3106 return ARM_REGNO_OK_FOR_BASE_P (regno);
3108 return (regno <= LAST_ARM_REGNUM
3109 || regno >= FIRST_PSEUDO_REGISTER
3110 || regno == FRAME_POINTER_REGNUM
3111 || regno == ARG_POINTER_REGNUM);
3114 /* Return nonzero if X is a valid ARM state address operand. */
3116 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3117 int strict_p)
3119 bool use_ldrd;
3120 enum rtx_code code = GET_CODE (x);
3122 if (arm_address_register_rtx_p (x, strict_p))
3123 return 1;
3125 use_ldrd = (TARGET_LDRD
3126 && (mode == DImode
3127 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3129 if (code == POST_INC || code == PRE_DEC
3130 || ((code == PRE_INC || code == POST_DEC)
3131 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3132 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3134 else if ((code == POST_MODIFY || code == PRE_MODIFY)
3135 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3136 && GET_CODE (XEXP (x, 1)) == PLUS
3137 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3139 rtx addend = XEXP (XEXP (x, 1), 1);
3141 /* Don't allow ldrd post increment by register because it's hard
3142 to fixup invalid register choices. */
3143 if (use_ldrd
3144 && GET_CODE (x) == POST_MODIFY
3145 && GET_CODE (addend) == REG)
3146 return 0;
3148 return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3149 && arm_legitimate_index_p (mode, addend, outer, strict_p));
3152 /* After reload constants split into minipools will have addresses
3153 from a LABEL_REF. */
3154 else if (reload_completed
3155 && (code == LABEL_REF
3156 || (code == CONST
3157 && GET_CODE (XEXP (x, 0)) == PLUS
3158 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3159 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3160 return 1;
3162 else if (mode == TImode)
3163 return 0;
3165 else if (code == PLUS)
3167 rtx xop0 = XEXP (x, 0);
3168 rtx xop1 = XEXP (x, 1);
3170 return ((arm_address_register_rtx_p (xop0, strict_p)
3171 && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3172 || (arm_address_register_rtx_p (xop1, strict_p)
3173 && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3176 #if 0
3177 /* Reload currently can't handle MINUS, so disable this for now */
3178 else if (GET_CODE (x) == MINUS)
3180 rtx xop0 = XEXP (x, 0);
3181 rtx xop1 = XEXP (x, 1);
3183 return (arm_address_register_rtx_p (xop0, strict_p)
3184 && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3186 #endif
3188 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3189 && code == SYMBOL_REF
3190 && CONSTANT_POOL_ADDRESS_P (x)
3191 && ! (flag_pic
3192 && symbol_mentioned_p (get_pool_constant (x))))
3193 return 1;
3195 return 0;
3198 /* Return nonzero if INDEX is valid for an address index operand in
3199 ARM state. */
3200 static int
3201 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3202 int strict_p)
3204 HOST_WIDE_INT range;
3205 enum rtx_code code = GET_CODE (index);
3207 /* Standard coprocessor addressing modes. */
3208 if (TARGET_HARD_FLOAT
3209 && (TARGET_FPA || TARGET_MAVERICK)
3210 && (GET_MODE_CLASS (mode) == MODE_FLOAT
3211 || (TARGET_MAVERICK && mode == DImode)))
3212 return (code == CONST_INT && INTVAL (index) < 1024
3213 && INTVAL (index) > -1024
3214 && (INTVAL (index) & 3) == 0);
3216 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3217 return (code == CONST_INT
3218 && INTVAL (index) < 1024
3219 && INTVAL (index) > -1024
3220 && (INTVAL (index) & 3) == 0);
3222 if (arm_address_register_rtx_p (index, strict_p)
3223 && (GET_MODE_SIZE (mode) <= 4))
3224 return 1;
3226 if (mode == DImode || mode == DFmode)
3228 if (code == CONST_INT)
3230 HOST_WIDE_INT val = INTVAL (index);
3232 if (TARGET_LDRD)
3233 return val > -256 && val < 256;
3234 else
3235 return val > -4096 && val < 4092;
3238 return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3241 if (GET_MODE_SIZE (mode) <= 4
3242 && ! (arm_arch4
3243 && (mode == HImode
3244 || (mode == QImode && outer == SIGN_EXTEND))))
3246 if (code == MULT)
3248 rtx xiop0 = XEXP (index, 0);
3249 rtx xiop1 = XEXP (index, 1);
3251 return ((arm_address_register_rtx_p (xiop0, strict_p)
3252 && power_of_two_operand (xiop1, SImode))
3253 || (arm_address_register_rtx_p (xiop1, strict_p)
3254 && power_of_two_operand (xiop0, SImode)));
3256 else if (code == LSHIFTRT || code == ASHIFTRT
3257 || code == ASHIFT || code == ROTATERT)
3259 rtx op = XEXP (index, 1);
3261 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3262 && GET_CODE (op) == CONST_INT
3263 && INTVAL (op) > 0
3264 && INTVAL (op) <= 31);
3268 /* For ARM v4 we may be doing a sign-extend operation during the
3269 load. */
3270 if (arm_arch4)
3272 if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3273 range = 256;
3274 else
3275 range = 4096;
3277 else
3278 range = (mode == HImode) ? 4095 : 4096;
3280 return (code == CONST_INT
3281 && INTVAL (index) < range
3282 && INTVAL (index) > -range);
3285 /* Return nonzero if X is valid as a Thumb state base register. */
3286 static int
3287 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3289 int regno;
3291 if (GET_CODE (x) != REG)
3292 return 0;
3294 regno = REGNO (x);
3296 if (strict_p)
3297 return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3299 return (regno <= LAST_LO_REGNUM
3300 || regno > LAST_VIRTUAL_REGISTER
3301 || regno == FRAME_POINTER_REGNUM
3302 || (GET_MODE_SIZE (mode) >= 4
3303 && (regno == STACK_POINTER_REGNUM
3304 || regno >= FIRST_PSEUDO_REGISTER
3305 || x == hard_frame_pointer_rtx
3306 || x == arg_pointer_rtx)));
3309 /* Return nonzero if x is a legitimate index register. This is the case
3310 for any base register that can access a QImode object. */
3311 inline static int
3312 thumb_index_register_rtx_p (rtx x, int strict_p)
3314 return thumb_base_register_rtx_p (x, QImode, strict_p);
3317 /* Return nonzero if x is a legitimate Thumb-state address.
3319 The AP may be eliminated to either the SP or the FP, so we use the
3320 least common denominator, e.g. SImode, and offsets from 0 to 64.
3322 ??? Verify whether the above is the right approach.
3324 ??? Also, the FP may be eliminated to the SP, so perhaps that
3325 needs special handling also.
3327 ??? Look at how the mips16 port solves this problem. It probably uses
3328 better ways to solve some of these problems.
3330 Although it is not incorrect, we don't accept QImode and HImode
3331 addresses based on the frame pointer or arg pointer until the
3332 reload pass starts. This is so that eliminating such addresses
3333 into stack based ones won't produce impossible code. */
3335 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3337 /* ??? Not clear if this is right. Experiment. */
3338 if (GET_MODE_SIZE (mode) < 4
3339 && !(reload_in_progress || reload_completed)
3340 && (reg_mentioned_p (frame_pointer_rtx, x)
3341 || reg_mentioned_p (arg_pointer_rtx, x)
3342 || reg_mentioned_p (virtual_incoming_args_rtx, x)
3343 || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3344 || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3345 || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3346 return 0;
3348 /* Accept any base register. SP only in SImode or larger. */
3349 else if (thumb_base_register_rtx_p (x, mode, strict_p))
3350 return 1;
3352 /* This is PC relative data before arm_reorg runs. */
3353 else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3354 && GET_CODE (x) == SYMBOL_REF
3355 && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3356 return 1;
3358 /* This is PC relative data after arm_reorg runs. */
3359 else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3360 && (GET_CODE (x) == LABEL_REF
3361 || (GET_CODE (x) == CONST
3362 && GET_CODE (XEXP (x, 0)) == PLUS
3363 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3364 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3365 return 1;
3367 /* Post-inc indexing only supported for SImode and larger. */
3368 else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3369 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3370 return 1;
3372 else if (GET_CODE (x) == PLUS)
3374 /* REG+REG address can be any two index registers. */
3375 /* We disallow FRAME+REG addressing since we know that FRAME
3376 will be replaced with STACK, and SP relative addressing only
3377 permits SP+OFFSET. */
3378 if (GET_MODE_SIZE (mode) <= 4
3379 && XEXP (x, 0) != frame_pointer_rtx
3380 && XEXP (x, 1) != frame_pointer_rtx
3381 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3382 && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3383 return 1;
3385 /* REG+const has 5-7 bit offset for non-SP registers. */
3386 else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3387 || XEXP (x, 0) == arg_pointer_rtx)
3388 && GET_CODE (XEXP (x, 1)) == CONST_INT
3389 && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3390 return 1;
3392 /* REG+const has 10 bit offset for SP, but only SImode and
3393 larger is supported. */
3394 /* ??? Should probably check for DI/DFmode overflow here
3395 just like GO_IF_LEGITIMATE_OFFSET does. */
3396 else if (GET_CODE (XEXP (x, 0)) == REG
3397 && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3398 && GET_MODE_SIZE (mode) >= 4
3399 && GET_CODE (XEXP (x, 1)) == CONST_INT
3400 && INTVAL (XEXP (x, 1)) >= 0
3401 && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3402 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3403 return 1;
3405 else if (GET_CODE (XEXP (x, 0)) == REG
3406 && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3407 && GET_MODE_SIZE (mode) >= 4
3408 && GET_CODE (XEXP (x, 1)) == CONST_INT
3409 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3410 return 1;
3413 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3414 && GET_MODE_SIZE (mode) == 4
3415 && GET_CODE (x) == SYMBOL_REF
3416 && CONSTANT_POOL_ADDRESS_P (x)
3417 && !(flag_pic
3418 && symbol_mentioned_p (get_pool_constant (x))))
3419 return 1;
3421 return 0;
3424 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3425 instruction of mode MODE. */
3427 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3429 switch (GET_MODE_SIZE (mode))
3431 case 1:
3432 return val >= 0 && val < 32;
3434 case 2:
3435 return val >= 0 && val < 64 && (val & 1) == 0;
3437 default:
3438 return (val >= 0
3439 && (val + GET_MODE_SIZE (mode)) <= 128
3440 && (val & 3) == 0);
3444 /* Try machine-dependent ways of modifying an illegitimate address
3445 to be legitimate. If we find one, return the new, valid address. */
3447 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3449 if (GET_CODE (x) == PLUS)
3451 rtx xop0 = XEXP (x, 0);
3452 rtx xop1 = XEXP (x, 1);
3454 if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3455 xop0 = force_reg (SImode, xop0);
3457 if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3458 xop1 = force_reg (SImode, xop1);
3460 if (ARM_BASE_REGISTER_RTX_P (xop0)
3461 && GET_CODE (xop1) == CONST_INT)
3463 HOST_WIDE_INT n, low_n;
3464 rtx base_reg, val;
3465 n = INTVAL (xop1);
3467 /* VFP addressing modes actually allow greater offsets, but for
3468 now we just stick with the lowest common denominator. */
3469 if (mode == DImode
3470 || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3472 low_n = n & 0x0f;
3473 n &= ~0x0f;
3474 if (low_n > 4)
3476 n += 16;
3477 low_n -= 16;
3480 else
3482 low_n = ((mode) == TImode ? 0
3483 : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3484 n -= low_n;
3487 base_reg = gen_reg_rtx (SImode);
3488 val = force_operand (gen_rtx_PLUS (SImode, xop0,
3489 GEN_INT (n)), NULL_RTX);
3490 emit_move_insn (base_reg, val);
3491 x = (low_n == 0 ? base_reg
3492 : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3494 else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3495 x = gen_rtx_PLUS (SImode, xop0, xop1);
3498 /* XXX We don't allow MINUS any more -- see comment in
3499 arm_legitimate_address_p (). */
3500 else if (GET_CODE (x) == MINUS)
3502 rtx xop0 = XEXP (x, 0);
3503 rtx xop1 = XEXP (x, 1);
3505 if (CONSTANT_P (xop0))
3506 xop0 = force_reg (SImode, xop0);
3508 if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3509 xop1 = force_reg (SImode, xop1);
3511 if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3512 x = gen_rtx_MINUS (SImode, xop0, xop1);
3515 if (flag_pic)
3517 /* We need to find and carefully transform any SYMBOL and LABEL
3518 references; so go back to the original address expression. */
3519 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3521 if (new_x != orig_x)
3522 x = new_x;
3525 return x;
3529 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3530 to be legitimate. If we find one, return the new, valid address. */
3532 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3534 if (GET_CODE (x) == PLUS
3535 && GET_CODE (XEXP (x, 1)) == CONST_INT
3536 && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3537 || INTVAL (XEXP (x, 1)) < 0))
3539 rtx xop0 = XEXP (x, 0);
3540 rtx xop1 = XEXP (x, 1);
3541 HOST_WIDE_INT offset = INTVAL (xop1);
3543 /* Try and fold the offset into a biasing of the base register and
3544 then offsetting that. Don't do this when optimizing for space
3545 since it can cause too many CSEs. */
3546 if (optimize_size && offset >= 0
3547 && offset < 256 + 31 * GET_MODE_SIZE (mode))
3549 HOST_WIDE_INT delta;
3551 if (offset >= 256)
3552 delta = offset - (256 - GET_MODE_SIZE (mode));
3553 else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3554 delta = 31 * GET_MODE_SIZE (mode);
3555 else
3556 delta = offset & (~31 * GET_MODE_SIZE (mode));
3558 xop0 = force_operand (plus_constant (xop0, offset - delta),
3559 NULL_RTX);
3560 x = plus_constant (xop0, delta);
3562 else if (offset < 0 && offset > -256)
3563 /* Small negative offsets are best done with a subtract before the
3564 dereference, forcing these into a register normally takes two
3565 instructions. */
3566 x = force_operand (x, NULL_RTX);
3567 else
3569 /* For the remaining cases, force the constant into a register. */
3570 xop1 = force_reg (SImode, xop1);
3571 x = gen_rtx_PLUS (SImode, xop0, xop1);
3574 else if (GET_CODE (x) == PLUS
3575 && s_register_operand (XEXP (x, 1), SImode)
3576 && !s_register_operand (XEXP (x, 0), SImode))
3578 rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3580 x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3583 if (flag_pic)
3585 /* We need to find and carefully transform any SYMBOL and LABEL
3586 references; so go back to the original address expression. */
3587 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3589 if (new_x != orig_x)
3590 x = new_x;
3593 return x;
3598 #define REG_OR_SUBREG_REG(X) \
3599 (GET_CODE (X) == REG \
3600 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3602 #define REG_OR_SUBREG_RTX(X) \
3603 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3605 #ifndef COSTS_N_INSNS
3606 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3607 #endif
3608 static inline int
3609 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3611 enum machine_mode mode = GET_MODE (x);
3613 switch (code)
3615 case ASHIFT:
3616 case ASHIFTRT:
3617 case LSHIFTRT:
3618 case ROTATERT:
3619 case PLUS:
3620 case MINUS:
3621 case COMPARE:
3622 case NEG:
3623 case NOT:
3624 return COSTS_N_INSNS (1);
3626 case MULT:
3627 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3629 int cycles = 0;
3630 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3632 while (i)
3634 i >>= 2;
3635 cycles++;
3637 return COSTS_N_INSNS (2) + cycles;
3639 return COSTS_N_INSNS (1) + 16;
3641 case SET:
3642 return (COSTS_N_INSNS (1)
3643 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3644 + GET_CODE (SET_DEST (x)) == MEM));
3646 case CONST_INT:
3647 if (outer == SET)
3649 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3650 return 0;
3651 if (thumb_shiftable_const (INTVAL (x)))
3652 return COSTS_N_INSNS (2);
3653 return COSTS_N_INSNS (3);
3655 else if ((outer == PLUS || outer == COMPARE)
3656 && INTVAL (x) < 256 && INTVAL (x) > -256)
3657 return 0;
3658 else if (outer == AND
3659 && INTVAL (x) < 256 && INTVAL (x) >= -256)
3660 return COSTS_N_INSNS (1);
3661 else if (outer == ASHIFT || outer == ASHIFTRT
3662 || outer == LSHIFTRT)
3663 return 0;
3664 return COSTS_N_INSNS (2);
3666 case CONST:
3667 case CONST_DOUBLE:
3668 case LABEL_REF:
3669 case SYMBOL_REF:
3670 return COSTS_N_INSNS (3);
3672 case UDIV:
3673 case UMOD:
3674 case DIV:
3675 case MOD:
3676 return 100;
3678 case TRUNCATE:
3679 return 99;
3681 case AND:
3682 case XOR:
3683 case IOR:
3684 /* XXX guess. */
3685 return 8;
3687 case MEM:
3688 /* XXX another guess. */
3689 /* Memory costs quite a lot for the first word, but subsequent words
3690 load at the equivalent of a single insn each. */
3691 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3692 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3693 ? 4 : 0));
3695 case IF_THEN_ELSE:
3696 /* XXX a guess. */
3697 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3698 return 14;
3699 return 2;
3701 case ZERO_EXTEND:
3702 /* XXX still guessing. */
3703 switch (GET_MODE (XEXP (x, 0)))
3705 case QImode:
3706 return (1 + (mode == DImode ? 4 : 0)
3707 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3709 case HImode:
3710 return (4 + (mode == DImode ? 4 : 0)
3711 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3713 case SImode:
3714 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3716 default:
3717 return 99;
3720 default:
3721 return 99;
3726 /* Worker routine for arm_rtx_costs. */
3727 static inline int
3728 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3730 enum machine_mode mode = GET_MODE (x);
3731 enum rtx_code subcode;
3732 int extra_cost;
3734 switch (code)
3736 case MEM:
3737 /* Memory costs quite a lot for the first word, but subsequent words
3738 load at the equivalent of a single insn each. */
3739 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3740 + (GET_CODE (x) == SYMBOL_REF
3741 && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3743 case DIV:
3744 case MOD:
3745 case UDIV:
3746 case UMOD:
3747 return optimize_size ? COSTS_N_INSNS (2) : 100;
3749 case ROTATE:
3750 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3751 return 4;
3752 /* Fall through */
3753 case ROTATERT:
3754 if (mode != SImode)
3755 return 8;
3756 /* Fall through */
3757 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3758 if (mode == DImode)
3759 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3760 + ((GET_CODE (XEXP (x, 0)) == REG
3761 || (GET_CODE (XEXP (x, 0)) == SUBREG
3762 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3763 ? 0 : 8));
3764 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3765 || (GET_CODE (XEXP (x, 0)) == SUBREG
3766 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3767 ? 0 : 4)
3768 + ((GET_CODE (XEXP (x, 1)) == REG
3769 || (GET_CODE (XEXP (x, 1)) == SUBREG
3770 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3771 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3772 ? 0 : 4));
3774 case MINUS:
3775 if (mode == DImode)
3776 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3777 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3778 || (GET_CODE (XEXP (x, 0)) == CONST_INT
3779 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3780 ? 0 : 8));
3782 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3783 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3784 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3785 && arm_const_double_rtx (XEXP (x, 1))))
3786 ? 0 : 8)
3787 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3788 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3789 && arm_const_double_rtx (XEXP (x, 0))))
3790 ? 0 : 8));
3792 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3793 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3794 && REG_OR_SUBREG_REG (XEXP (x, 1))))
3795 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3796 || subcode == ASHIFTRT || subcode == LSHIFTRT
3797 || subcode == ROTATE || subcode == ROTATERT
3798 || (subcode == MULT
3799 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3800 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3801 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3802 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3803 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3804 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3805 && REG_OR_SUBREG_REG (XEXP (x, 0))))
3806 return 1;
3807 /* Fall through */
3809 case PLUS:
3810 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3811 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3812 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3813 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3814 && arm_const_double_rtx (XEXP (x, 1))))
3815 ? 0 : 8));
3817 /* Fall through */
3818 case AND: case XOR: case IOR:
3819 extra_cost = 0;
3821 /* Normally the frame registers will be spilt into reg+const during
3822 reload, so it is a bad idea to combine them with other instructions,
3823 since then they might not be moved outside of loops. As a compromise
3824 we allow integration with ops that have a constant as their second
3825 operand. */
3826 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3827 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3828 && GET_CODE (XEXP (x, 1)) != CONST_INT)
3829 || (REG_OR_SUBREG_REG (XEXP (x, 0))
3830 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3831 extra_cost = 4;
3833 if (mode == DImode)
3834 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3835 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3836 || (GET_CODE (XEXP (x, 1)) == CONST_INT
3837 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3838 ? 0 : 8));
3840 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3841 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3842 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3843 || (GET_CODE (XEXP (x, 1)) == CONST_INT
3844 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3845 ? 0 : 4));
3847 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3848 return (1 + extra_cost
3849 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3850 || subcode == LSHIFTRT || subcode == ASHIFTRT
3851 || subcode == ROTATE || subcode == ROTATERT
3852 || (subcode == MULT
3853 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3854 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3855 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3856 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3857 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3858 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3859 ? 0 : 4));
3861 return 8;
3863 case MULT:
3864 /* This should have been handled by the CPU specific routines. */
3865 abort ();
3867 case TRUNCATE:
3868 if (arm_arch3m && mode == SImode
3869 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
3870 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
3871 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
3872 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
3873 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
3874 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
3875 return 8;
3876 return 99;
3878 case NEG:
3879 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3880 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
3881 /* Fall through */
3882 case NOT:
3883 if (mode == DImode)
3884 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3886 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3888 case IF_THEN_ELSE:
3889 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3890 return 14;
3891 return 2;
3893 case COMPARE:
3894 return 1;
3896 case ABS:
3897 return 4 + (mode == DImode ? 4 : 0);
3899 case SIGN_EXTEND:
3900 if (GET_MODE (XEXP (x, 0)) == QImode)
3901 return (4 + (mode == DImode ? 4 : 0)
3902 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3903 /* Fall through */
3904 case ZERO_EXTEND:
3905 switch (GET_MODE (XEXP (x, 0)))
3907 case QImode:
3908 return (1 + (mode == DImode ? 4 : 0)
3909 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3911 case HImode:
3912 return (4 + (mode == DImode ? 4 : 0)
3913 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3915 case SImode:
3916 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3918 case V8QImode:
3919 case V4HImode:
3920 case V2SImode:
3921 case V4QImode:
3922 case V2HImode:
3923 return 1;
3925 default:
3926 break;
3928 abort ();
3930 case CONST_INT:
3931 if (const_ok_for_arm (INTVAL (x)))
3932 return outer == SET ? 2 : -1;
3933 else if (outer == AND
3934 && const_ok_for_arm (~INTVAL (x)))
3935 return -1;
3936 else if ((outer == COMPARE
3937 || outer == PLUS || outer == MINUS)
3938 && const_ok_for_arm (-INTVAL (x)))
3939 return -1;
3940 else
3941 return 5;
3943 case CONST:
3944 case LABEL_REF:
3945 case SYMBOL_REF:
3946 return 6;
3948 case CONST_DOUBLE:
3949 if (arm_const_double_rtx (x))
3950 return outer == SET ? 2 : -1;
3951 else if ((outer == COMPARE || outer == PLUS)
3952 && neg_const_double_rtx_ok_for_fpa (x))
3953 return -1;
3954 return 7;
3956 default:
3957 return 99;
3961 /* RTX costs when optimizing for size. */
3962 static bool
3963 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
3965 enum machine_mode mode = GET_MODE (x);
3967 if (TARGET_THUMB)
3969 /* XXX TBD. For now, use the standard costs. */
3970 *total = thumb_rtx_costs (x, code, outer_code);
3971 return true;
3974 switch (code)
3976 case MEM:
3977 /* A memory access costs 1 insn if the mode is small, or the address is
3978 a single register, otherwise it costs one insn per word. */
3979 if (REG_P (XEXP (x, 0)))
3980 *total = COSTS_N_INSNS (1);
3981 else
3982 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
3983 return true;
3985 case DIV:
3986 case MOD:
3987 case UDIV:
3988 case UMOD:
3989 /* Needs a libcall, so it costs about this. */
3990 *total = COSTS_N_INSNS (2);
3991 return false;
3993 case ROTATE:
3994 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3996 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
3997 return true;
3999 /* Fall through */
4000 case ROTATERT:
4001 case ASHIFT:
4002 case LSHIFTRT:
4003 case ASHIFTRT:
4004 if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4006 *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4007 return true;
4009 else if (mode == SImode)
4011 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4012 /* Slightly disparage register shifts, but not by much. */
4013 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4014 *total += 1 + rtx_cost (XEXP (x, 1), code);
4015 return true;
4018 /* Needs a libcall. */
4019 *total = COSTS_N_INSNS (2);
4020 return false;
4022 case MINUS:
4023 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4025 *total = COSTS_N_INSNS (1);
4026 return false;
4029 if (mode == SImode)
4031 enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4032 enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4034 if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4035 || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4036 || subcode1 == ROTATE || subcode1 == ROTATERT
4037 || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4038 || subcode1 == ASHIFTRT)
4040 /* It's just the cost of the two operands. */
4041 *total = 0;
4042 return false;
4045 *total = COSTS_N_INSNS (1);
4046 return false;
4049 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4050 return false;
4052 case PLUS:
4053 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4055 *total = COSTS_N_INSNS (1);
4056 return false;
4059 /* Fall through */
4060 case AND: case XOR: case IOR:
4061 if (mode == SImode)
4063 enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4065 if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4066 || subcode == LSHIFTRT || subcode == ASHIFTRT
4067 || (code == AND && subcode == NOT))
4069 /* It's just the cost of the two operands. */
4070 *total = 0;
4071 return false;
4075 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4076 return false;
4078 case MULT:
4079 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4080 return false;
4082 case NEG:
4083 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4084 *total = COSTS_N_INSNS (1);
4085 /* Fall through */
4086 case NOT:
4087 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4089 return false;
4091 case IF_THEN_ELSE:
4092 *total = 0;
4093 return false;
4095 case COMPARE:
4096 if (cc_register (XEXP (x, 0), VOIDmode))
4097 * total = 0;
4098 else
4099 *total = COSTS_N_INSNS (1);
4100 return false;
4102 case ABS:
4103 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4104 *total = COSTS_N_INSNS (1);
4105 else
4106 *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4107 return false;
4109 case SIGN_EXTEND:
4110 *total = 0;
4111 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4113 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4114 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4116 if (mode == DImode)
4117 *total += COSTS_N_INSNS (1);
4118 return false;
4120 case ZERO_EXTEND:
4121 *total = 0;
4122 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4124 switch (GET_MODE (XEXP (x, 0)))
4126 case QImode:
4127 *total += COSTS_N_INSNS (1);
4128 break;
4130 case HImode:
4131 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4133 case SImode:
4134 break;
4136 default:
4137 *total += COSTS_N_INSNS (2);
4141 if (mode == DImode)
4142 *total += COSTS_N_INSNS (1);
4144 return false;
4146 case CONST_INT:
4147 if (const_ok_for_arm (INTVAL (x)))
4148 *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4149 else if (const_ok_for_arm (~INTVAL (x)))
4150 *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4151 else if (const_ok_for_arm (-INTVAL (x)))
4153 if (outer_code == COMPARE || outer_code == PLUS
4154 || outer_code == MINUS)
4155 *total = 0;
4156 else
4157 *total = COSTS_N_INSNS (1);
4159 else
4160 *total = COSTS_N_INSNS (2);
4161 return true;
4163 case CONST:
4164 case LABEL_REF:
4165 case SYMBOL_REF:
4166 *total = COSTS_N_INSNS (2);
4167 return true;
4169 case CONST_DOUBLE:
4170 *total = COSTS_N_INSNS (4);
4171 return true;
4173 default:
4174 if (mode != VOIDmode)
4175 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4176 else
4177 *total = COSTS_N_INSNS (4); /* How knows? */
4178 return false;
4182 /* RTX costs for cores with a slow MUL implementation. */
4184 static bool
4185 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4187 enum machine_mode mode = GET_MODE (x);
4189 if (TARGET_THUMB)
4191 *total = thumb_rtx_costs (x, code, outer_code);
4192 return true;
4195 switch (code)
4197 case MULT:
4198 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4199 || mode == DImode)
4201 *total = 30;
4202 return true;
4205 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4207 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4208 & (unsigned HOST_WIDE_INT) 0xffffffff);
4209 int cost, const_ok = const_ok_for_arm (i);
4210 int j, booth_unit_size;
4212 /* Tune as appropriate. */
4213 cost = const_ok ? 4 : 8;
4214 booth_unit_size = 2;
4215 for (j = 0; i && j < 32; j += booth_unit_size)
4217 i >>= booth_unit_size;
4218 cost += 2;
4221 *total = cost;
4222 return true;
4225 *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4226 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4227 return true;
4229 default:
4230 *total = arm_rtx_costs_1 (x, code, outer_code);
4231 return true;
4236 /* RTX cost for cores with a fast multiply unit (M variants). */
4238 static bool
4239 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4241 enum machine_mode mode = GET_MODE (x);
4243 if (TARGET_THUMB)
4245 *total = thumb_rtx_costs (x, code, outer_code);
4246 return true;
4249 switch (code)
4251 case MULT:
4252 /* There is no point basing this on the tuning, since it is always the
4253 fast variant if it exists at all. */
4254 if (mode == DImode
4255 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4256 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4257 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4259 *total = 8;
4260 return true;
4264 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4265 || mode == DImode)
4267 *total = 30;
4268 return true;
4271 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4273 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4274 & (unsigned HOST_WIDE_INT) 0xffffffff);
4275 int cost, const_ok = const_ok_for_arm (i);
4276 int j, booth_unit_size;
4278 /* Tune as appropriate. */
4279 cost = const_ok ? 4 : 8;
4280 booth_unit_size = 8;
4281 for (j = 0; i && j < 32; j += booth_unit_size)
4283 i >>= booth_unit_size;
4284 cost += 2;
4287 *total = cost;
4288 return true;
4291 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4292 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4293 return true;
4295 default:
4296 *total = arm_rtx_costs_1 (x, code, outer_code);
4297 return true;
4302 /* RTX cost for XScale CPUs. */
4304 static bool
4305 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4307 enum machine_mode mode = GET_MODE (x);
4309 if (TARGET_THUMB)
4311 *total = thumb_rtx_costs (x, code, outer_code);
4312 return true;
4315 switch (code)
4317 case MULT:
4318 /* There is no point basing this on the tuning, since it is always the
4319 fast variant if it exists at all. */
4320 if (mode == DImode
4321 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4322 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4323 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4325 *total = 8;
4326 return true;
4330 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4331 || mode == DImode)
4333 *total = 30;
4334 return true;
4337 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4339 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4340 & (unsigned HOST_WIDE_INT) 0xffffffff);
4341 int cost, const_ok = const_ok_for_arm (i);
4342 unsigned HOST_WIDE_INT masked_const;
4344 /* The cost will be related to two insns.
4345 First a load of the constant (MOV or LDR), then a multiply. */
4346 cost = 2;
4347 if (! const_ok)
4348 cost += 1; /* LDR is probably more expensive because
4349 of longer result latency. */
4350 masked_const = i & 0xffff8000;
4351 if (masked_const != 0 && masked_const != 0xffff8000)
4353 masked_const = i & 0xf8000000;
4354 if (masked_const == 0 || masked_const == 0xf8000000)
4355 cost += 1;
4356 else
4357 cost += 2;
4359 *total = cost;
4360 return true;
4363 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4364 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4365 return true;
4367 default:
4368 *total = arm_rtx_costs_1 (x, code, outer_code);
4369 return true;
4374 /* RTX costs for 9e (and later) cores. */
4376 static bool
4377 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4379 enum machine_mode mode = GET_MODE (x);
4380 int nonreg_cost;
4381 int cost;
4383 if (TARGET_THUMB)
4385 switch (code)
4387 case MULT:
4388 *total = COSTS_N_INSNS (3);
4389 return true;
4391 default:
4392 *total = thumb_rtx_costs (x, code, outer_code);
4393 return true;
4397 switch (code)
4399 case MULT:
4400 /* There is no point basing this on the tuning, since it is always the
4401 fast variant if it exists at all. */
4402 if (mode == DImode
4403 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4404 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4405 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4407 *total = 3;
4408 return true;
4412 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4414 *total = 30;
4415 return true;
4417 if (mode == DImode)
4419 cost = 7;
4420 nonreg_cost = 8;
4422 else
4424 cost = 2;
4425 nonreg_cost = 4;
4429 *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4430 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4431 return true;
4433 default:
4434 *total = arm_rtx_costs_1 (x, code, outer_code);
4435 return true;
4438 /* All address computations that can be done are free, but rtx cost returns
4439 the same for practically all of them. So we weight the different types
4440 of address here in the order (most pref first):
4441 PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
4442 static inline int
4443 arm_arm_address_cost (rtx x)
4445 enum rtx_code c = GET_CODE (x);
4447 if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4448 return 0;
4449 if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4450 return 10;
4452 if (c == PLUS || c == MINUS)
4454 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4455 return 2;
4457 if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4458 return 3;
4460 return 4;
4463 return 6;
4466 static inline int
4467 arm_thumb_address_cost (rtx x)
4469 enum rtx_code c = GET_CODE (x);
4471 if (c == REG)
4472 return 1;
4473 if (c == PLUS
4474 && GET_CODE (XEXP (x, 0)) == REG
4475 && GET_CODE (XEXP (x, 1)) == CONST_INT)
4476 return 1;
4478 return 2;
4481 static int
4482 arm_address_cost (rtx x)
4484 return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4487 static int
4488 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4490 rtx i_pat, d_pat;
4492 /* Some true dependencies can have a higher cost depending
4493 on precisely how certain input operands are used. */
4494 if (arm_tune_xscale
4495 && REG_NOTE_KIND (link) == 0
4496 && recog_memoized (insn) >= 0
4497 && recog_memoized (dep) >= 0)
4499 int shift_opnum = get_attr_shift (insn);
4500 enum attr_type attr_type = get_attr_type (dep);
4502 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4503 operand for INSN. If we have a shifted input operand and the
4504 instruction we depend on is another ALU instruction, then we may
4505 have to account for an additional stall. */
4506 if (shift_opnum != 0
4507 && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4509 rtx shifted_operand;
4510 int opno;
4512 /* Get the shifted operand. */
4513 extract_insn (insn);
4514 shifted_operand = recog_data.operand[shift_opnum];
4516 /* Iterate over all the operands in DEP. If we write an operand
4517 that overlaps with SHIFTED_OPERAND, then we have increase the
4518 cost of this dependency. */
4519 extract_insn (dep);
4520 preprocess_constraints ();
4521 for (opno = 0; opno < recog_data.n_operands; opno++)
4523 /* We can ignore strict inputs. */
4524 if (recog_data.operand_type[opno] == OP_IN)
4525 continue;
4527 if (reg_overlap_mentioned_p (recog_data.operand[opno],
4528 shifted_operand))
4529 return 2;
4534 /* XXX This is not strictly true for the FPA. */
4535 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4536 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4537 return 0;
4539 /* Call insns don't incur a stall, even if they follow a load. */
4540 if (REG_NOTE_KIND (link) == 0
4541 && GET_CODE (insn) == CALL_INSN)
4542 return 1;
4544 if ((i_pat = single_set (insn)) != NULL
4545 && GET_CODE (SET_SRC (i_pat)) == MEM
4546 && (d_pat = single_set (dep)) != NULL
4547 && GET_CODE (SET_DEST (d_pat)) == MEM)
4549 rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4550 /* This is a load after a store, there is no conflict if the load reads
4551 from a cached area. Assume that loads from the stack, and from the
4552 constant pool are cached, and that others will miss. This is a
4553 hack. */
4555 if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4556 || reg_mentioned_p (stack_pointer_rtx, src_mem)
4557 || reg_mentioned_p (frame_pointer_rtx, src_mem)
4558 || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4559 return 1;
4562 return cost;
4565 static int fp_consts_inited = 0;
4567 /* Only zero is valid for VFP. Other values are also valid for FPA. */
4568 static const char * const strings_fp[8] =
4570 "0", "1", "2", "3",
4571 "4", "5", "0.5", "10"
4574 static REAL_VALUE_TYPE values_fp[8];
4576 static void
4577 init_fp_table (void)
4579 int i;
4580 REAL_VALUE_TYPE r;
4582 if (TARGET_VFP)
4583 fp_consts_inited = 1;
4584 else
4585 fp_consts_inited = 8;
4587 for (i = 0; i < fp_consts_inited; i++)
4589 r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4590 values_fp[i] = r;
4594 /* Return TRUE if rtx X is a valid immediate FP constant. */
4596 arm_const_double_rtx (rtx x)
4598 REAL_VALUE_TYPE r;
4599 int i;
4601 if (!fp_consts_inited)
4602 init_fp_table ();
4604 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4605 if (REAL_VALUE_MINUS_ZERO (r))
4606 return 0;
4608 for (i = 0; i < fp_consts_inited; i++)
4609 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4610 return 1;
4612 return 0;
4615 /* Return TRUE if rtx X is a valid immediate FPA constant. */
4617 neg_const_double_rtx_ok_for_fpa (rtx x)
4619 REAL_VALUE_TYPE r;
4620 int i;
4622 if (!fp_consts_inited)
4623 init_fp_table ();
4625 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4626 r = REAL_VALUE_NEGATE (r);
4627 if (REAL_VALUE_MINUS_ZERO (r))
4628 return 0;
4630 for (i = 0; i < 8; i++)
4631 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4632 return 1;
4634 return 0;
4637 /* Predicates for `match_operand' and `match_operator'. */
4639 /* Return nonzero if OP is a valid Cirrus memory address pattern. */
4641 cirrus_memory_offset (rtx op)
4643 /* Reject eliminable registers. */
4644 if (! (reload_in_progress || reload_completed)
4645 && ( reg_mentioned_p (frame_pointer_rtx, op)
4646 || reg_mentioned_p (arg_pointer_rtx, op)
4647 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4648 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4649 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4650 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4651 return 0;
4653 if (GET_CODE (op) == MEM)
4655 rtx ind;
4657 ind = XEXP (op, 0);
4659 /* Match: (mem (reg)). */
4660 if (GET_CODE (ind) == REG)
4661 return 1;
4663 /* Match:
4664 (mem (plus (reg)
4665 (const))). */
4666 if (GET_CODE (ind) == PLUS
4667 && GET_CODE (XEXP (ind, 0)) == REG
4668 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4669 && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4670 return 1;
4673 return 0;
4676 /* Return TRUE if OP is a valid VFP memory address pattern.
4677 WB if true if writeback address modes are allowed. */
4680 arm_coproc_mem_operand (rtx op, bool wb)
4682 rtx ind;
4684 /* Reject eliminable registers. */
4685 if (! (reload_in_progress || reload_completed)
4686 && ( reg_mentioned_p (frame_pointer_rtx, op)
4687 || reg_mentioned_p (arg_pointer_rtx, op)
4688 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4689 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4690 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4691 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4692 return FALSE;
4694 /* Constants are converted into offsets from labels. */
4695 if (GET_CODE (op) != MEM)
4696 return FALSE;
4698 ind = XEXP (op, 0);
4700 if (reload_completed
4701 && (GET_CODE (ind) == LABEL_REF
4702 || (GET_CODE (ind) == CONST
4703 && GET_CODE (XEXP (ind, 0)) == PLUS
4704 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4705 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4706 return TRUE;
4708 /* Match: (mem (reg)). */
4709 if (GET_CODE (ind) == REG)
4710 return arm_address_register_rtx_p (ind, 0);
4712 /* Autoincremment addressing modes. */
4713 if (wb
4714 && (GET_CODE (ind) == PRE_INC
4715 || GET_CODE (ind) == POST_INC
4716 || GET_CODE (ind) == PRE_DEC
4717 || GET_CODE (ind) == POST_DEC))
4718 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
4720 if (wb
4721 && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
4722 && arm_address_register_rtx_p (XEXP (ind, 0), 0)
4723 && GET_CODE (XEXP (ind, 1)) == PLUS
4724 && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
4725 ind = XEXP (ind, 1);
4727 /* Match:
4728 (plus (reg)
4729 (const)). */
4730 if (GET_CODE (ind) == PLUS
4731 && GET_CODE (XEXP (ind, 0)) == REG
4732 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4733 && GET_CODE (XEXP (ind, 1)) == CONST_INT
4734 && INTVAL (XEXP (ind, 1)) > -1024
4735 && INTVAL (XEXP (ind, 1)) < 1024
4736 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
4737 return TRUE;
4739 return FALSE;
4743 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4744 VFP registers. Otherwise return NO_REGS. */
4746 enum reg_class
4747 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4749 if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
4750 return NO_REGS;
4752 return GENERAL_REGS;
4756 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4757 Use by the Cirrus Maverick code which has to workaround
4758 a hardware bug triggered by such instructions. */
4759 static bool
4760 arm_memory_load_p (rtx insn)
4762 rtx body, lhs, rhs;;
4764 if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4765 return false;
4767 body = PATTERN (insn);
4769 if (GET_CODE (body) != SET)
4770 return false;
4772 lhs = XEXP (body, 0);
4773 rhs = XEXP (body, 1);
4775 lhs = REG_OR_SUBREG_RTX (lhs);
4777 /* If the destination is not a general purpose
4778 register we do not have to worry. */
4779 if (GET_CODE (lhs) != REG
4780 || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4781 return false;
4783 /* As well as loads from memory we also have to react
4784 to loads of invalid constants which will be turned
4785 into loads from the minipool. */
4786 return (GET_CODE (rhs) == MEM
4787 || GET_CODE (rhs) == SYMBOL_REF
4788 || note_invalid_constants (insn, -1, false));
4791 /* Return TRUE if INSN is a Cirrus instruction. */
4792 static bool
4793 arm_cirrus_insn_p (rtx insn)
4795 enum attr_cirrus attr;
4797 /* get_attr aborts on USE and CLOBBER. */
4798 if (!insn
4799 || GET_CODE (insn) != INSN
4800 || GET_CODE (PATTERN (insn)) == USE
4801 || GET_CODE (PATTERN (insn)) == CLOBBER)
4802 return 0;
4804 attr = get_attr_cirrus (insn);
4806 return attr != CIRRUS_NOT;
4809 /* Cirrus reorg for invalid instruction combinations. */
4810 static void
4811 cirrus_reorg (rtx first)
4813 enum attr_cirrus attr;
4814 rtx body = PATTERN (first);
4815 rtx t;
4816 int nops;
4818 /* Any branch must be followed by 2 non Cirrus instructions. */
4819 if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4821 nops = 0;
4822 t = next_nonnote_insn (first);
4824 if (arm_cirrus_insn_p (t))
4825 ++ nops;
4827 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4828 ++ nops;
4830 while (nops --)
4831 emit_insn_after (gen_nop (), first);
4833 return;
4836 /* (float (blah)) is in parallel with a clobber. */
4837 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4838 body = XVECEXP (body, 0, 0);
4840 if (GET_CODE (body) == SET)
4842 rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4844 /* cfldrd, cfldr64, cfstrd, cfstr64 must
4845 be followed by a non Cirrus insn. */
4846 if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
4848 if (arm_cirrus_insn_p (next_nonnote_insn (first)))
4849 emit_insn_after (gen_nop (), first);
4851 return;
4853 else if (arm_memory_load_p (first))
4855 unsigned int arm_regno;
4857 /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
4858 ldr/cfmv64hr combination where the Rd field is the same
4859 in both instructions must be split with a non Cirrus
4860 insn. Example:
4862 ldr r0, blah
4864 cfmvsr mvf0, r0. */
4866 /* Get Arm register number for ldr insn. */
4867 if (GET_CODE (lhs) == REG)
4868 arm_regno = REGNO (lhs);
4869 else if (GET_CODE (rhs) == REG)
4870 arm_regno = REGNO (rhs);
4871 else
4872 abort ();
4874 /* Next insn. */
4875 first = next_nonnote_insn (first);
4877 if (! arm_cirrus_insn_p (first))
4878 return;
4880 body = PATTERN (first);
4882 /* (float (blah)) is in parallel with a clobber. */
4883 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
4884 body = XVECEXP (body, 0, 0);
4886 if (GET_CODE (body) == FLOAT)
4887 body = XEXP (body, 0);
4889 if (get_attr_cirrus (first) == CIRRUS_MOVE
4890 && GET_CODE (XEXP (body, 1)) == REG
4891 && arm_regno == REGNO (XEXP (body, 1)))
4892 emit_insn_after (gen_nop (), first);
4894 return;
4898 /* get_attr aborts on USE and CLOBBER. */
4899 if (!first
4900 || GET_CODE (first) != INSN
4901 || GET_CODE (PATTERN (first)) == USE
4902 || GET_CODE (PATTERN (first)) == CLOBBER)
4903 return;
4905 attr = get_attr_cirrus (first);
4907 /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
4908 must be followed by a non-coprocessor instruction. */
4909 if (attr == CIRRUS_COMPARE)
4911 nops = 0;
4913 t = next_nonnote_insn (first);
4915 if (arm_cirrus_insn_p (t))
4916 ++ nops;
4918 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4919 ++ nops;
4921 while (nops --)
4922 emit_insn_after (gen_nop (), first);
4924 return;
4928 /* Return TRUE if X references a SYMBOL_REF. */
4930 symbol_mentioned_p (rtx x)
4932 const char * fmt;
4933 int i;
4935 if (GET_CODE (x) == SYMBOL_REF)
4936 return 1;
4938 fmt = GET_RTX_FORMAT (GET_CODE (x));
4940 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4942 if (fmt[i] == 'E')
4944 int j;
4946 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4947 if (symbol_mentioned_p (XVECEXP (x, i, j)))
4948 return 1;
4950 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
4951 return 1;
4954 return 0;
4957 /* Return TRUE if X references a LABEL_REF. */
4959 label_mentioned_p (rtx x)
4961 const char * fmt;
4962 int i;
4964 if (GET_CODE (x) == LABEL_REF)
4965 return 1;
4967 fmt = GET_RTX_FORMAT (GET_CODE (x));
4968 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4970 if (fmt[i] == 'E')
4972 int j;
4974 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4975 if (label_mentioned_p (XVECEXP (x, i, j)))
4976 return 1;
4978 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
4979 return 1;
4982 return 0;
4985 enum rtx_code
4986 minmax_code (rtx x)
4988 enum rtx_code code = GET_CODE (x);
4990 if (code == SMAX)
4991 return GE;
4992 else if (code == SMIN)
4993 return LE;
4994 else if (code == UMIN)
4995 return LEU;
4996 else if (code == UMAX)
4997 return GEU;
4999 abort ();
5002 /* Return 1 if memory locations are adjacent. */
5004 adjacent_mem_locations (rtx a, rtx b)
5006 if ((GET_CODE (XEXP (a, 0)) == REG
5007 || (GET_CODE (XEXP (a, 0)) == PLUS
5008 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5009 && (GET_CODE (XEXP (b, 0)) == REG
5010 || (GET_CODE (XEXP (b, 0)) == PLUS
5011 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5013 int val0 = 0, val1 = 0;
5014 int reg0, reg1;
5016 if (GET_CODE (XEXP (a, 0)) == PLUS)
5018 reg0 = REGNO (XEXP (XEXP (a, 0), 0));
5019 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5021 else
5022 reg0 = REGNO (XEXP (a, 0));
5024 if (GET_CODE (XEXP (b, 0)) == PLUS)
5026 reg1 = REGNO (XEXP (XEXP (b, 0), 0));
5027 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5029 else
5030 reg1 = REGNO (XEXP (b, 0));
5032 /* Don't accept any offset that will require multiple
5033 instructions to handle, since this would cause the
5034 arith_adjacentmem pattern to output an overlong sequence. */
5035 if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5036 return 0;
5038 return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
5040 return 0;
5044 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5045 HOST_WIDE_INT *load_offset)
5047 int unsorted_regs[4];
5048 HOST_WIDE_INT unsorted_offsets[4];
5049 int order[4];
5050 int base_reg = -1;
5051 int i;
5053 /* Can only handle 2, 3, or 4 insns at present,
5054 though could be easily extended if required. */
5055 if (nops < 2 || nops > 4)
5056 abort ();
5058 /* Loop over the operands and check that the memory references are
5059 suitable (i.e. immediate offsets from the same base register). At
5060 the same time, extract the target register, and the memory
5061 offsets. */
5062 for (i = 0; i < nops; i++)
5064 rtx reg;
5065 rtx offset;
5067 /* Convert a subreg of a mem into the mem itself. */
5068 if (GET_CODE (operands[nops + i]) == SUBREG)
5069 operands[nops + i] = alter_subreg (operands + (nops + i));
5071 if (GET_CODE (operands[nops + i]) != MEM)
5072 abort ();
5074 /* Don't reorder volatile memory references; it doesn't seem worth
5075 looking for the case where the order is ok anyway. */
5076 if (MEM_VOLATILE_P (operands[nops + i]))
5077 return 0;
5079 offset = const0_rtx;
5081 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5082 || (GET_CODE (reg) == SUBREG
5083 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5084 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5085 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5086 == REG)
5087 || (GET_CODE (reg) == SUBREG
5088 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5089 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5090 == CONST_INT)))
5092 if (i == 0)
5094 base_reg = REGNO (reg);
5095 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5096 ? REGNO (operands[i])
5097 : REGNO (SUBREG_REG (operands[i])));
5098 order[0] = 0;
5100 else
5102 if (base_reg != (int) REGNO (reg))
5103 /* Not addressed from the same base register. */
5104 return 0;
5106 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5107 ? REGNO (operands[i])
5108 : REGNO (SUBREG_REG (operands[i])));
5109 if (unsorted_regs[i] < unsorted_regs[order[0]])
5110 order[0] = i;
5113 /* If it isn't an integer register, or if it overwrites the
5114 base register but isn't the last insn in the list, then
5115 we can't do this. */
5116 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5117 || (i != nops - 1 && unsorted_regs[i] == base_reg))
5118 return 0;
5120 unsorted_offsets[i] = INTVAL (offset);
5122 else
5123 /* Not a suitable memory address. */
5124 return 0;
5127 /* All the useful information has now been extracted from the
5128 operands into unsorted_regs and unsorted_offsets; additionally,
5129 order[0] has been set to the lowest numbered register in the
5130 list. Sort the registers into order, and check that the memory
5131 offsets are ascending and adjacent. */
5133 for (i = 1; i < nops; i++)
5135 int j;
5137 order[i] = order[i - 1];
5138 for (j = 0; j < nops; j++)
5139 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5140 && (order[i] == order[i - 1]
5141 || unsorted_regs[j] < unsorted_regs[order[i]]))
5142 order[i] = j;
5144 /* Have we found a suitable register? if not, one must be used more
5145 than once. */
5146 if (order[i] == order[i - 1])
5147 return 0;
5149 /* Is the memory address adjacent and ascending? */
5150 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5151 return 0;
5154 if (base)
5156 *base = base_reg;
5158 for (i = 0; i < nops; i++)
5159 regs[i] = unsorted_regs[order[i]];
5161 *load_offset = unsorted_offsets[order[0]];
5164 if (unsorted_offsets[order[0]] == 0)
5165 return 1; /* ldmia */
5167 if (unsorted_offsets[order[0]] == 4)
5168 return 2; /* ldmib */
5170 if (unsorted_offsets[order[nops - 1]] == 0)
5171 return 3; /* ldmda */
5173 if (unsorted_offsets[order[nops - 1]] == -4)
5174 return 4; /* ldmdb */
5176 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5177 if the offset isn't small enough. The reason 2 ldrs are faster
5178 is because these ARMs are able to do more than one cache access
5179 in a single cycle. The ARM9 and StrongARM have Harvard caches,
5180 whilst the ARM8 has a double bandwidth cache. This means that
5181 these cores can do both an instruction fetch and a data fetch in
5182 a single cycle, so the trick of calculating the address into a
5183 scratch register (one of the result regs) and then doing a load
5184 multiple actually becomes slower (and no smaller in code size).
5185 That is the transformation
5187 ldr rd1, [rbase + offset]
5188 ldr rd2, [rbase + offset + 4]
5192 add rd1, rbase, offset
5193 ldmia rd1, {rd1, rd2}
5195 produces worse code -- '3 cycles + any stalls on rd2' instead of
5196 '2 cycles + any stalls on rd2'. On ARMs with only one cache
5197 access per cycle, the first sequence could never complete in less
5198 than 6 cycles, whereas the ldm sequence would only take 5 and
5199 would make better use of sequential accesses if not hitting the
5200 cache.
5202 We cheat here and test 'arm_ld_sched' which we currently know to
5203 only be true for the ARM8, ARM9 and StrongARM. If this ever
5204 changes, then the test below needs to be reworked. */
5205 if (nops == 2 && arm_ld_sched)
5206 return 0;
5208 /* Can't do it without setting up the offset, only do this if it takes
5209 no more than one insn. */
5210 return (const_ok_for_arm (unsorted_offsets[order[0]])
5211 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5214 const char *
5215 emit_ldm_seq (rtx *operands, int nops)
5217 int regs[4];
5218 int base_reg;
5219 HOST_WIDE_INT offset;
5220 char buf[100];
5221 int i;
5223 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5225 case 1:
5226 strcpy (buf, "ldm%?ia\t");
5227 break;
5229 case 2:
5230 strcpy (buf, "ldm%?ib\t");
5231 break;
5233 case 3:
5234 strcpy (buf, "ldm%?da\t");
5235 break;
5237 case 4:
5238 strcpy (buf, "ldm%?db\t");
5239 break;
5241 case 5:
5242 if (offset >= 0)
5243 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5244 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5245 (long) offset);
5246 else
5247 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5248 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5249 (long) -offset);
5250 output_asm_insn (buf, operands);
5251 base_reg = regs[0];
5252 strcpy (buf, "ldm%?ia\t");
5253 break;
5255 default:
5256 abort ();
5259 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5260 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5262 for (i = 1; i < nops; i++)
5263 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5264 reg_names[regs[i]]);
5266 strcat (buf, "}\t%@ phole ldm");
5268 output_asm_insn (buf, operands);
5269 return "";
5273 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5274 HOST_WIDE_INT * load_offset)
5276 int unsorted_regs[4];
5277 HOST_WIDE_INT unsorted_offsets[4];
5278 int order[4];
5279 int base_reg = -1;
5280 int i;
5282 /* Can only handle 2, 3, or 4 insns at present, though could be easily
5283 extended if required. */
5284 if (nops < 2 || nops > 4)
5285 abort ();
5287 /* Loop over the operands and check that the memory references are
5288 suitable (i.e. immediate offsets from the same base register). At
5289 the same time, extract the target register, and the memory
5290 offsets. */
5291 for (i = 0; i < nops; i++)
5293 rtx reg;
5294 rtx offset;
5296 /* Convert a subreg of a mem into the mem itself. */
5297 if (GET_CODE (operands[nops + i]) == SUBREG)
5298 operands[nops + i] = alter_subreg (operands + (nops + i));
5300 if (GET_CODE (operands[nops + i]) != MEM)
5301 abort ();
5303 /* Don't reorder volatile memory references; it doesn't seem worth
5304 looking for the case where the order is ok anyway. */
5305 if (MEM_VOLATILE_P (operands[nops + i]))
5306 return 0;
5308 offset = const0_rtx;
5310 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5311 || (GET_CODE (reg) == SUBREG
5312 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5313 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5314 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5315 == REG)
5316 || (GET_CODE (reg) == SUBREG
5317 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5318 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5319 == CONST_INT)))
5321 if (i == 0)
5323 base_reg = REGNO (reg);
5324 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5325 ? REGNO (operands[i])
5326 : REGNO (SUBREG_REG (operands[i])));
5327 order[0] = 0;
5329 else
5331 if (base_reg != (int) REGNO (reg))
5332 /* Not addressed from the same base register. */
5333 return 0;
5335 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5336 ? REGNO (operands[i])
5337 : REGNO (SUBREG_REG (operands[i])));
5338 if (unsorted_regs[i] < unsorted_regs[order[0]])
5339 order[0] = i;
5342 /* If it isn't an integer register, then we can't do this. */
5343 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5344 return 0;
5346 unsorted_offsets[i] = INTVAL (offset);
5348 else
5349 /* Not a suitable memory address. */
5350 return 0;
5353 /* All the useful information has now been extracted from the
5354 operands into unsorted_regs and unsorted_offsets; additionally,
5355 order[0] has been set to the lowest numbered register in the
5356 list. Sort the registers into order, and check that the memory
5357 offsets are ascending and adjacent. */
5359 for (i = 1; i < nops; i++)
5361 int j;
5363 order[i] = order[i - 1];
5364 for (j = 0; j < nops; j++)
5365 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5366 && (order[i] == order[i - 1]
5367 || unsorted_regs[j] < unsorted_regs[order[i]]))
5368 order[i] = j;
5370 /* Have we found a suitable register? if not, one must be used more
5371 than once. */
5372 if (order[i] == order[i - 1])
5373 return 0;
5375 /* Is the memory address adjacent and ascending? */
5376 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5377 return 0;
5380 if (base)
5382 *base = base_reg;
5384 for (i = 0; i < nops; i++)
5385 regs[i] = unsorted_regs[order[i]];
5387 *load_offset = unsorted_offsets[order[0]];
5390 if (unsorted_offsets[order[0]] == 0)
5391 return 1; /* stmia */
5393 if (unsorted_offsets[order[0]] == 4)
5394 return 2; /* stmib */
5396 if (unsorted_offsets[order[nops - 1]] == 0)
5397 return 3; /* stmda */
5399 if (unsorted_offsets[order[nops - 1]] == -4)
5400 return 4; /* stmdb */
5402 return 0;
5405 const char *
5406 emit_stm_seq (rtx *operands, int nops)
5408 int regs[4];
5409 int base_reg;
5410 HOST_WIDE_INT offset;
5411 char buf[100];
5412 int i;
5414 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5416 case 1:
5417 strcpy (buf, "stm%?ia\t");
5418 break;
5420 case 2:
5421 strcpy (buf, "stm%?ib\t");
5422 break;
5424 case 3:
5425 strcpy (buf, "stm%?da\t");
5426 break;
5428 case 4:
5429 strcpy (buf, "stm%?db\t");
5430 break;
5432 default:
5433 abort ();
5436 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5437 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5439 for (i = 1; i < nops; i++)
5440 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5441 reg_names[regs[i]]);
5443 strcat (buf, "}\t%@ phole stm");
5445 output_asm_insn (buf, operands);
5446 return "";
5450 /* Routines for use in generating RTL. */
5453 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5454 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5456 HOST_WIDE_INT offset = *offsetp;
5457 int i = 0, j;
5458 rtx result;
5459 int sign = up ? 1 : -1;
5460 rtx mem, addr;
5462 /* XScale has load-store double instructions, but they have stricter
5463 alignment requirements than load-store multiple, so we cannot
5464 use them.
5466 For XScale ldm requires 2 + NREGS cycles to complete and blocks
5467 the pipeline until completion.
5469 NREGS CYCLES
5475 An ldr instruction takes 1-3 cycles, but does not block the
5476 pipeline.
5478 NREGS CYCLES
5479 1 1-3
5480 2 2-6
5481 3 3-9
5482 4 4-12
5484 Best case ldr will always win. However, the more ldr instructions
5485 we issue, the less likely we are to be able to schedule them well.
5486 Using ldr instructions also increases code size.
5488 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5489 for counts of 3 or 4 regs. */
5490 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5492 rtx seq;
5494 start_sequence ();
5496 for (i = 0; i < count; i++)
5498 addr = plus_constant (from, i * 4 * sign);
5499 mem = adjust_automodify_address (basemem, SImode, addr, offset);
5500 emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5501 offset += 4 * sign;
5504 if (write_back)
5506 emit_move_insn (from, plus_constant (from, count * 4 * sign));
5507 *offsetp = offset;
5510 seq = get_insns ();
5511 end_sequence ();
5513 return seq;
5516 result = gen_rtx_PARALLEL (VOIDmode,
5517 rtvec_alloc (count + (write_back ? 1 : 0)));
5518 if (write_back)
5520 XVECEXP (result, 0, 0)
5521 = gen_rtx_SET (GET_MODE (from), from,
5522 plus_constant (from, count * 4 * sign));
5523 i = 1;
5524 count++;
5527 for (j = 0; i < count; i++, j++)
5529 addr = plus_constant (from, j * 4 * sign);
5530 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5531 XVECEXP (result, 0, i)
5532 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5533 offset += 4 * sign;
5536 if (write_back)
5537 *offsetp = offset;
5539 return result;
5543 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5544 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5546 HOST_WIDE_INT offset = *offsetp;
5547 int i = 0, j;
5548 rtx result;
5549 int sign = up ? 1 : -1;
5550 rtx mem, addr;
5552 /* See arm_gen_load_multiple for discussion of
5553 the pros/cons of ldm/stm usage for XScale. */
5554 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5556 rtx seq;
5558 start_sequence ();
5560 for (i = 0; i < count; i++)
5562 addr = plus_constant (to, i * 4 * sign);
5563 mem = adjust_automodify_address (basemem, SImode, addr, offset);
5564 emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5565 offset += 4 * sign;
5568 if (write_back)
5570 emit_move_insn (to, plus_constant (to, count * 4 * sign));
5571 *offsetp = offset;
5574 seq = get_insns ();
5575 end_sequence ();
5577 return seq;
5580 result = gen_rtx_PARALLEL (VOIDmode,
5581 rtvec_alloc (count + (write_back ? 1 : 0)));
5582 if (write_back)
5584 XVECEXP (result, 0, 0)
5585 = gen_rtx_SET (GET_MODE (to), to,
5586 plus_constant (to, count * 4 * sign));
5587 i = 1;
5588 count++;
5591 for (j = 0; i < count; i++, j++)
5593 addr = plus_constant (to, j * 4 * sign);
5594 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5595 XVECEXP (result, 0, i)
5596 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5597 offset += 4 * sign;
5600 if (write_back)
5601 *offsetp = offset;
5603 return result;
5607 arm_gen_movmemqi (rtx *operands)
5609 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5610 HOST_WIDE_INT srcoffset, dstoffset;
5611 int i;
5612 rtx src, dst, srcbase, dstbase;
5613 rtx part_bytes_reg = NULL;
5614 rtx mem;
5616 if (GET_CODE (operands[2]) != CONST_INT
5617 || GET_CODE (operands[3]) != CONST_INT
5618 || INTVAL (operands[2]) > 64
5619 || INTVAL (operands[3]) & 3)
5620 return 0;
5622 dstbase = operands[0];
5623 srcbase = operands[1];
5625 dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
5626 src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
5628 in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5629 out_words_to_go = INTVAL (operands[2]) / 4;
5630 last_bytes = INTVAL (operands[2]) & 3;
5631 dstoffset = srcoffset = 0;
5633 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5634 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5636 for (i = 0; in_words_to_go >= 2; i+=4)
5638 if (in_words_to_go > 4)
5639 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5640 srcbase, &srcoffset));
5641 else
5642 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
5643 FALSE, srcbase, &srcoffset));
5645 if (out_words_to_go)
5647 if (out_words_to_go > 4)
5648 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5649 dstbase, &dstoffset));
5650 else if (out_words_to_go != 1)
5651 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5652 dst, TRUE,
5653 (last_bytes == 0
5654 ? FALSE : TRUE),
5655 dstbase, &dstoffset));
5656 else
5658 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5659 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5660 if (last_bytes != 0)
5662 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5663 dstoffset += 4;
5668 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5669 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
5672 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
5673 if (out_words_to_go)
5675 rtx sreg;
5677 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5678 sreg = copy_to_reg (mem);
5680 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5681 emit_move_insn (mem, sreg);
5682 in_words_to_go--;
5684 if (in_words_to_go) /* Sanity check */
5685 abort ();
5688 if (in_words_to_go)
5690 if (in_words_to_go < 0)
5691 abort ();
5693 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5694 part_bytes_reg = copy_to_mode_reg (SImode, mem);
5697 if (last_bytes && part_bytes_reg == NULL)
5698 abort ();
5700 if (BYTES_BIG_ENDIAN && last_bytes)
5702 rtx tmp = gen_reg_rtx (SImode);
5704 /* The bytes we want are in the top end of the word. */
5705 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
5706 GEN_INT (8 * (4 - last_bytes))));
5707 part_bytes_reg = tmp;
5709 while (last_bytes)
5711 mem = adjust_automodify_address (dstbase, QImode,
5712 plus_constant (dst, last_bytes - 1),
5713 dstoffset + last_bytes - 1);
5714 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5716 if (--last_bytes)
5718 tmp = gen_reg_rtx (SImode);
5719 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
5720 part_bytes_reg = tmp;
5725 else
5727 if (last_bytes > 1)
5729 mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
5730 emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
5731 last_bytes -= 2;
5732 if (last_bytes)
5734 rtx tmp = gen_reg_rtx (SImode);
5735 emit_insn (gen_addsi3 (dst, dst, const2_rtx));
5736 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
5737 part_bytes_reg = tmp;
5738 dstoffset += 2;
5742 if (last_bytes)
5744 mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
5745 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5749 return 1;
5752 /* Generate a memory reference for a half word, such that it will be loaded
5753 into the top 16 bits of the word. We can assume that the address is
5754 known to be alignable and of the form reg, or plus (reg, const). */
5757 arm_gen_rotated_half_load (rtx memref)
5759 HOST_WIDE_INT offset = 0;
5760 rtx base = XEXP (memref, 0);
5762 if (GET_CODE (base) == PLUS)
5764 offset = INTVAL (XEXP (base, 1));
5765 base = XEXP (base, 0);
5768 /* If we aren't allowed to generate unaligned addresses, then fail. */
5769 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0))
5770 return NULL;
5772 base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
5774 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
5775 return base;
5777 return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
5780 /* Select a dominance comparison mode if possible for a test of the general
5781 form (OP (COND_OR (X) (Y)) (const_int 0)). We support three forms.
5782 COND_OR == DOM_CC_X_AND_Y => (X && Y)
5783 COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
5784 COND_OR == DOM_CC_X_OR_Y => (X || Y)
5785 In all cases OP will be either EQ or NE, but we don't need to know which
5786 here. If we are unable to support a dominance comparison we return
5787 CC mode. This will then fail to match for the RTL expressions that
5788 generate this call. */
5789 enum machine_mode
5790 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
5792 enum rtx_code cond1, cond2;
5793 int swapped = 0;
5795 /* Currently we will probably get the wrong result if the individual
5796 comparisons are not simple. This also ensures that it is safe to
5797 reverse a comparison if necessary. */
5798 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
5799 != CCmode)
5800 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
5801 != CCmode))
5802 return CCmode;
5804 /* The if_then_else variant of this tests the second condition if the
5805 first passes, but is true if the first fails. Reverse the first
5806 condition to get a true "inclusive-or" expression. */
5807 if (cond_or == DOM_CC_NX_OR_Y)
5808 cond1 = reverse_condition (cond1);
5810 /* If the comparisons are not equal, and one doesn't dominate the other,
5811 then we can't do this. */
5812 if (cond1 != cond2
5813 && !comparison_dominates_p (cond1, cond2)
5814 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
5815 return CCmode;
5817 if (swapped)
5819 enum rtx_code temp = cond1;
5820 cond1 = cond2;
5821 cond2 = temp;
5824 switch (cond1)
5826 case EQ:
5827 if (cond2 == EQ || cond_or == DOM_CC_X_AND_Y)
5828 return CC_DEQmode;
5830 switch (cond2)
5832 case LE: return CC_DLEmode;
5833 case LEU: return CC_DLEUmode;
5834 case GE: return CC_DGEmode;
5835 case GEU: return CC_DGEUmode;
5836 default: break;
5839 break;
5841 case LT:
5842 if (cond2 == LT || cond_or == DOM_CC_X_AND_Y)
5843 return CC_DLTmode;
5844 if (cond2 == LE)
5845 return CC_DLEmode;
5846 if (cond2 == NE)
5847 return CC_DNEmode;
5848 break;
5850 case GT:
5851 if (cond2 == GT || cond_or == DOM_CC_X_AND_Y)
5852 return CC_DGTmode;
5853 if (cond2 == GE)
5854 return CC_DGEmode;
5855 if (cond2 == NE)
5856 return CC_DNEmode;
5857 break;
5859 case LTU:
5860 if (cond2 == LTU || cond_or == DOM_CC_X_AND_Y)
5861 return CC_DLTUmode;
5862 if (cond2 == LEU)
5863 return CC_DLEUmode;
5864 if (cond2 == NE)
5865 return CC_DNEmode;
5866 break;
5868 case GTU:
5869 if (cond2 == GTU || cond_or == DOM_CC_X_AND_Y)
5870 return CC_DGTUmode;
5871 if (cond2 == GEU)
5872 return CC_DGEUmode;
5873 if (cond2 == NE)
5874 return CC_DNEmode;
5875 break;
5877 /* The remaining cases only occur when both comparisons are the
5878 same. */
5879 case NE:
5880 return CC_DNEmode;
5882 case LE:
5883 return CC_DLEmode;
5885 case GE:
5886 return CC_DGEmode;
5888 case LEU:
5889 return CC_DLEUmode;
5891 case GEU:
5892 return CC_DGEUmode;
5894 default:
5895 break;
5898 abort ();
5901 enum machine_mode
5902 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
5904 /* All floating point compares return CCFP if it is an equality
5905 comparison, and CCFPE otherwise. */
5906 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
5908 switch (op)
5910 case EQ:
5911 case NE:
5912 case UNORDERED:
5913 case ORDERED:
5914 case UNLT:
5915 case UNLE:
5916 case UNGT:
5917 case UNGE:
5918 case UNEQ:
5919 case LTGT:
5920 return CCFPmode;
5922 case LT:
5923 case LE:
5924 case GT:
5925 case GE:
5926 if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
5927 return CCFPmode;
5928 return CCFPEmode;
5930 default:
5931 abort ();
5935 /* A compare with a shifted operand. Because of canonicalization, the
5936 comparison will have to be swapped when we emit the assembler. */
5937 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
5938 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
5939 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
5940 || GET_CODE (x) == ROTATERT))
5941 return CC_SWPmode;
5943 /* This is a special case that is used by combine to allow a
5944 comparison of a shifted byte load to be split into a zero-extend
5945 followed by a comparison of the shifted integer (only valid for
5946 equalities and unsigned inequalities). */
5947 if (GET_MODE (x) == SImode
5948 && GET_CODE (x) == ASHIFT
5949 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
5950 && GET_CODE (XEXP (x, 0)) == SUBREG
5951 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
5952 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
5953 && (op == EQ || op == NE
5954 || op == GEU || op == GTU || op == LTU || op == LEU)
5955 && GET_CODE (y) == CONST_INT)
5956 return CC_Zmode;
5958 /* A construct for a conditional compare, if the false arm contains
5959 0, then both conditions must be true, otherwise either condition
5960 must be true. Not all conditions are possible, so CCmode is
5961 returned if it can't be done. */
5962 if (GET_CODE (x) == IF_THEN_ELSE
5963 && (XEXP (x, 2) == const0_rtx
5964 || XEXP (x, 2) == const1_rtx)
5965 && COMPARISON_P (XEXP (x, 0))
5966 && COMPARISON_P (XEXP (x, 1)))
5967 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
5968 INTVAL (XEXP (x, 2)));
5970 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
5971 if (GET_CODE (x) == AND
5972 && COMPARISON_P (XEXP (x, 0))
5973 && COMPARISON_P (XEXP (x, 1)))
5974 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
5975 DOM_CC_X_AND_Y);
5977 if (GET_CODE (x) == IOR
5978 && COMPARISON_P (XEXP (x, 0))
5979 && COMPARISON_P (XEXP (x, 1)))
5980 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
5981 DOM_CC_X_OR_Y);
5983 /* An operation (on Thumb) where we want to test for a single bit.
5984 This is done by shifting that bit up into the top bit of a
5985 scratch register; we can then branch on the sign bit. */
5986 if (TARGET_THUMB
5987 && GET_MODE (x) == SImode
5988 && (op == EQ || op == NE)
5989 && (GET_CODE (x) == ZERO_EXTRACT))
5990 return CC_Nmode;
5992 /* An operation that sets the condition codes as a side-effect, the
5993 V flag is not set correctly, so we can only use comparisons where
5994 this doesn't matter. (For LT and GE we can use "mi" and "pl"
5995 instead.) */
5996 if (GET_MODE (x) == SImode
5997 && y == const0_rtx
5998 && (op == EQ || op == NE || op == LT || op == GE)
5999 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6000 || GET_CODE (x) == AND || GET_CODE (x) == IOR
6001 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6002 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6003 || GET_CODE (x) == LSHIFTRT
6004 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6005 || GET_CODE (x) == ROTATERT
6006 || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6007 return CC_NOOVmode;
6009 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6010 return CC_Zmode;
6012 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6013 && GET_CODE (x) == PLUS
6014 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6015 return CC_Cmode;
6017 return CCmode;
6020 /* X and Y are two things to compare using CODE. Emit the compare insn and
6021 return the rtx for register 0 in the proper mode. FP means this is a
6022 floating point compare: I don't think that it is needed on the arm. */
6024 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6026 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6027 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6029 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6030 gen_rtx_COMPARE (mode, x, y)));
6032 return cc_reg;
6035 /* Generate a sequence of insns that will generate the correct return
6036 address mask depending on the physical architecture that the program
6037 is running on. */
6039 arm_gen_return_addr_mask (void)
6041 rtx reg = gen_reg_rtx (Pmode);
6043 emit_insn (gen_return_addr_mask (reg));
6044 return reg;
6047 void
6048 arm_reload_in_hi (rtx *operands)
6050 rtx ref = operands[1];
6051 rtx base, scratch;
6052 HOST_WIDE_INT offset = 0;
6054 if (GET_CODE (ref) == SUBREG)
6056 offset = SUBREG_BYTE (ref);
6057 ref = SUBREG_REG (ref);
6060 if (GET_CODE (ref) == REG)
6062 /* We have a pseudo which has been spilt onto the stack; there
6063 are two cases here: the first where there is a simple
6064 stack-slot replacement and a second where the stack-slot is
6065 out of range, or is used as a subreg. */
6066 if (reg_equiv_mem[REGNO (ref)])
6068 ref = reg_equiv_mem[REGNO (ref)];
6069 base = find_replacement (&XEXP (ref, 0));
6071 else
6072 /* The slot is out of range, or was dressed up in a SUBREG. */
6073 base = reg_equiv_address[REGNO (ref)];
6075 else
6076 base = find_replacement (&XEXP (ref, 0));
6078 /* Handle the case where the address is too complex to be offset by 1. */
6079 if (GET_CODE (base) == MINUS
6080 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6082 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6084 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6085 base = base_plus;
6087 else if (GET_CODE (base) == PLUS)
6089 /* The addend must be CONST_INT, or we would have dealt with it above. */
6090 HOST_WIDE_INT hi, lo;
6092 offset += INTVAL (XEXP (base, 1));
6093 base = XEXP (base, 0);
6095 /* Rework the address into a legal sequence of insns. */
6096 /* Valid range for lo is -4095 -> 4095 */
6097 lo = (offset >= 0
6098 ? (offset & 0xfff)
6099 : -((-offset) & 0xfff));
6101 /* Corner case, if lo is the max offset then we would be out of range
6102 once we have added the additional 1 below, so bump the msb into the
6103 pre-loading insn(s). */
6104 if (lo == 4095)
6105 lo &= 0x7ff;
6107 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6108 ^ (HOST_WIDE_INT) 0x80000000)
6109 - (HOST_WIDE_INT) 0x80000000);
6111 if (hi + lo != offset)
6112 abort ();
6114 if (hi != 0)
6116 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6118 /* Get the base address; addsi3 knows how to handle constants
6119 that require more than one insn. */
6120 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6121 base = base_plus;
6122 offset = lo;
6126 /* Operands[2] may overlap operands[0] (though it won't overlap
6127 operands[1]), that's why we asked for a DImode reg -- so we can
6128 use the bit that does not overlap. */
6129 if (REGNO (operands[2]) == REGNO (operands[0]))
6130 scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6131 else
6132 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6134 emit_insn (gen_zero_extendqisi2 (scratch,
6135 gen_rtx_MEM (QImode,
6136 plus_constant (base,
6137 offset))));
6138 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6139 gen_rtx_MEM (QImode,
6140 plus_constant (base,
6141 offset + 1))));
6142 if (!BYTES_BIG_ENDIAN)
6143 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6144 gen_rtx_IOR (SImode,
6145 gen_rtx_ASHIFT
6146 (SImode,
6147 gen_rtx_SUBREG (SImode, operands[0], 0),
6148 GEN_INT (8)),
6149 scratch)));
6150 else
6151 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6152 gen_rtx_IOR (SImode,
6153 gen_rtx_ASHIFT (SImode, scratch,
6154 GEN_INT (8)),
6155 gen_rtx_SUBREG (SImode, operands[0],
6156 0))));
6159 /* Handle storing a half-word to memory during reload by synthesizing as two
6160 byte stores. Take care not to clobber the input values until after we
6161 have moved them somewhere safe. This code assumes that if the DImode
6162 scratch in operands[2] overlaps either the input value or output address
6163 in some way, then that value must die in this insn (we absolutely need
6164 two scratch registers for some corner cases). */
6165 void
6166 arm_reload_out_hi (rtx *operands)
6168 rtx ref = operands[0];
6169 rtx outval = operands[1];
6170 rtx base, scratch;
6171 HOST_WIDE_INT offset = 0;
6173 if (GET_CODE (ref) == SUBREG)
6175 offset = SUBREG_BYTE (ref);
6176 ref = SUBREG_REG (ref);
6179 if (GET_CODE (ref) == REG)
6181 /* We have a pseudo which has been spilt onto the stack; there
6182 are two cases here: the first where there is a simple
6183 stack-slot replacement and a second where the stack-slot is
6184 out of range, or is used as a subreg. */
6185 if (reg_equiv_mem[REGNO (ref)])
6187 ref = reg_equiv_mem[REGNO (ref)];
6188 base = find_replacement (&XEXP (ref, 0));
6190 else
6191 /* The slot is out of range, or was dressed up in a SUBREG. */
6192 base = reg_equiv_address[REGNO (ref)];
6194 else
6195 base = find_replacement (&XEXP (ref, 0));
6197 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6199 /* Handle the case where the address is too complex to be offset by 1. */
6200 if (GET_CODE (base) == MINUS
6201 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6203 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6205 /* Be careful not to destroy OUTVAL. */
6206 if (reg_overlap_mentioned_p (base_plus, outval))
6208 /* Updating base_plus might destroy outval, see if we can
6209 swap the scratch and base_plus. */
6210 if (!reg_overlap_mentioned_p (scratch, outval))
6212 rtx tmp = scratch;
6213 scratch = base_plus;
6214 base_plus = tmp;
6216 else
6218 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6220 /* Be conservative and copy OUTVAL into the scratch now,
6221 this should only be necessary if outval is a subreg
6222 of something larger than a word. */
6223 /* XXX Might this clobber base? I can't see how it can,
6224 since scratch is known to overlap with OUTVAL, and
6225 must be wider than a word. */
6226 emit_insn (gen_movhi (scratch_hi, outval));
6227 outval = scratch_hi;
6231 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6232 base = base_plus;
6234 else if (GET_CODE (base) == PLUS)
6236 /* The addend must be CONST_INT, or we would have dealt with it above. */
6237 HOST_WIDE_INT hi, lo;
6239 offset += INTVAL (XEXP (base, 1));
6240 base = XEXP (base, 0);
6242 /* Rework the address into a legal sequence of insns. */
6243 /* Valid range for lo is -4095 -> 4095 */
6244 lo = (offset >= 0
6245 ? (offset & 0xfff)
6246 : -((-offset) & 0xfff));
6248 /* Corner case, if lo is the max offset then we would be out of range
6249 once we have added the additional 1 below, so bump the msb into the
6250 pre-loading insn(s). */
6251 if (lo == 4095)
6252 lo &= 0x7ff;
6254 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6255 ^ (HOST_WIDE_INT) 0x80000000)
6256 - (HOST_WIDE_INT) 0x80000000);
6258 if (hi + lo != offset)
6259 abort ();
6261 if (hi != 0)
6263 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6265 /* Be careful not to destroy OUTVAL. */
6266 if (reg_overlap_mentioned_p (base_plus, outval))
6268 /* Updating base_plus might destroy outval, see if we
6269 can swap the scratch and base_plus. */
6270 if (!reg_overlap_mentioned_p (scratch, outval))
6272 rtx tmp = scratch;
6273 scratch = base_plus;
6274 base_plus = tmp;
6276 else
6278 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6280 /* Be conservative and copy outval into scratch now,
6281 this should only be necessary if outval is a
6282 subreg of something larger than a word. */
6283 /* XXX Might this clobber base? I can't see how it
6284 can, since scratch is known to overlap with
6285 outval. */
6286 emit_insn (gen_movhi (scratch_hi, outval));
6287 outval = scratch_hi;
6291 /* Get the base address; addsi3 knows how to handle constants
6292 that require more than one insn. */
6293 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6294 base = base_plus;
6295 offset = lo;
6299 if (BYTES_BIG_ENDIAN)
6301 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6302 plus_constant (base, offset + 1)),
6303 gen_lowpart (QImode, outval)));
6304 emit_insn (gen_lshrsi3 (scratch,
6305 gen_rtx_SUBREG (SImode, outval, 0),
6306 GEN_INT (8)));
6307 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6308 gen_lowpart (QImode, scratch)));
6310 else
6312 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6313 gen_lowpart (QImode, outval)));
6314 emit_insn (gen_lshrsi3 (scratch,
6315 gen_rtx_SUBREG (SImode, outval, 0),
6316 GEN_INT (8)));
6317 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6318 plus_constant (base, offset + 1)),
6319 gen_lowpart (QImode, scratch)));
6323 /* Print a symbolic form of X to the debug file, F. */
6324 static void
6325 arm_print_value (FILE *f, rtx x)
6327 switch (GET_CODE (x))
6329 case CONST_INT:
6330 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6331 return;
6333 case CONST_DOUBLE:
6334 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6335 return;
6337 case CONST_VECTOR:
6339 int i;
6341 fprintf (f, "<");
6342 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6344 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6345 if (i < (CONST_VECTOR_NUNITS (x) - 1))
6346 fputc (',', f);
6348 fprintf (f, ">");
6350 return;
6352 case CONST_STRING:
6353 fprintf (f, "\"%s\"", XSTR (x, 0));
6354 return;
6356 case SYMBOL_REF:
6357 fprintf (f, "`%s'", XSTR (x, 0));
6358 return;
6360 case LABEL_REF:
6361 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6362 return;
6364 case CONST:
6365 arm_print_value (f, XEXP (x, 0));
6366 return;
6368 case PLUS:
6369 arm_print_value (f, XEXP (x, 0));
6370 fprintf (f, "+");
6371 arm_print_value (f, XEXP (x, 1));
6372 return;
6374 case PC:
6375 fprintf (f, "pc");
6376 return;
6378 default:
6379 fprintf (f, "????");
6380 return;
6384 /* Routines for manipulation of the constant pool. */
6386 /* Arm instructions cannot load a large constant directly into a
6387 register; they have to come from a pc relative load. The constant
6388 must therefore be placed in the addressable range of the pc
6389 relative load. Depending on the precise pc relative load
6390 instruction the range is somewhere between 256 bytes and 4k. This
6391 means that we often have to dump a constant inside a function, and
6392 generate code to branch around it.
6394 It is important to minimize this, since the branches will slow
6395 things down and make the code larger.
6397 Normally we can hide the table after an existing unconditional
6398 branch so that there is no interruption of the flow, but in the
6399 worst case the code looks like this:
6401 ldr rn, L1
6403 b L2
6404 align
6405 L1: .long value
6409 ldr rn, L3
6411 b L4
6412 align
6413 L3: .long value
6417 We fix this by performing a scan after scheduling, which notices
6418 which instructions need to have their operands fetched from the
6419 constant table and builds the table.
6421 The algorithm starts by building a table of all the constants that
6422 need fixing up and all the natural barriers in the function (places
6423 where a constant table can be dropped without breaking the flow).
6424 For each fixup we note how far the pc-relative replacement will be
6425 able to reach and the offset of the instruction into the function.
6427 Having built the table we then group the fixes together to form
6428 tables that are as large as possible (subject to addressing
6429 constraints) and emit each table of constants after the last
6430 barrier that is within range of all the instructions in the group.
6431 If a group does not contain a barrier, then we forcibly create one
6432 by inserting a jump instruction into the flow. Once the table has
6433 been inserted, the insns are then modified to reference the
6434 relevant entry in the pool.
6436 Possible enhancements to the algorithm (not implemented) are:
6438 1) For some processors and object formats, there may be benefit in
6439 aligning the pools to the start of cache lines; this alignment
6440 would need to be taken into account when calculating addressability
6441 of a pool. */
6443 /* These typedefs are located at the start of this file, so that
6444 they can be used in the prototypes there. This comment is to
6445 remind readers of that fact so that the following structures
6446 can be understood more easily.
6448 typedef struct minipool_node Mnode;
6449 typedef struct minipool_fixup Mfix; */
6451 struct minipool_node
6453 /* Doubly linked chain of entries. */
6454 Mnode * next;
6455 Mnode * prev;
6456 /* The maximum offset into the code that this entry can be placed. While
6457 pushing fixes for forward references, all entries are sorted in order
6458 of increasing max_address. */
6459 HOST_WIDE_INT max_address;
6460 /* Similarly for an entry inserted for a backwards ref. */
6461 HOST_WIDE_INT min_address;
6462 /* The number of fixes referencing this entry. This can become zero
6463 if we "unpush" an entry. In this case we ignore the entry when we
6464 come to emit the code. */
6465 int refcount;
6466 /* The offset from the start of the minipool. */
6467 HOST_WIDE_INT offset;
6468 /* The value in table. */
6469 rtx value;
6470 /* The mode of value. */
6471 enum machine_mode mode;
6472 /* The size of the value. With iWMMXt enabled
6473 sizes > 4 also imply an alignment of 8-bytes. */
6474 int fix_size;
6477 struct minipool_fixup
6479 Mfix * next;
6480 rtx insn;
6481 HOST_WIDE_INT address;
6482 rtx * loc;
6483 enum machine_mode mode;
6484 int fix_size;
6485 rtx value;
6486 Mnode * minipool;
6487 HOST_WIDE_INT forwards;
6488 HOST_WIDE_INT backwards;
6491 /* Fixes less than a word need padding out to a word boundary. */
6492 #define MINIPOOL_FIX_SIZE(mode) \
6493 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6495 static Mnode * minipool_vector_head;
6496 static Mnode * minipool_vector_tail;
6497 static rtx minipool_vector_label;
6499 /* The linked list of all minipool fixes required for this function. */
6500 Mfix * minipool_fix_head;
6501 Mfix * minipool_fix_tail;
6502 /* The fix entry for the current minipool, once it has been placed. */
6503 Mfix * minipool_barrier;
6505 /* Determines if INSN is the start of a jump table. Returns the end
6506 of the TABLE or NULL_RTX. */
6507 static rtx
6508 is_jump_table (rtx insn)
6510 rtx table;
6512 if (GET_CODE (insn) == JUMP_INSN
6513 && JUMP_LABEL (insn) != NULL
6514 && ((table = next_real_insn (JUMP_LABEL (insn)))
6515 == next_real_insn (insn))
6516 && table != NULL
6517 && GET_CODE (table) == JUMP_INSN
6518 && (GET_CODE (PATTERN (table)) == ADDR_VEC
6519 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6520 return table;
6522 return NULL_RTX;
6525 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6526 #define JUMP_TABLES_IN_TEXT_SECTION 0
6527 #endif
6529 static HOST_WIDE_INT
6530 get_jump_table_size (rtx insn)
6532 /* ADDR_VECs only take room if read-only data does into the text
6533 section. */
6534 if (JUMP_TABLES_IN_TEXT_SECTION
6535 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6536 || 1
6537 #endif
6540 rtx body = PATTERN (insn);
6541 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6543 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6546 return 0;
6549 /* Move a minipool fix MP from its current location to before MAX_MP.
6550 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6551 constraints may need updating. */
6552 static Mnode *
6553 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6554 HOST_WIDE_INT max_address)
6556 /* This should never be true and the code below assumes these are
6557 different. */
6558 if (mp == max_mp)
6559 abort ();
6561 if (max_mp == NULL)
6563 if (max_address < mp->max_address)
6564 mp->max_address = max_address;
6566 else
6568 if (max_address > max_mp->max_address - mp->fix_size)
6569 mp->max_address = max_mp->max_address - mp->fix_size;
6570 else
6571 mp->max_address = max_address;
6573 /* Unlink MP from its current position. Since max_mp is non-null,
6574 mp->prev must be non-null. */
6575 mp->prev->next = mp->next;
6576 if (mp->next != NULL)
6577 mp->next->prev = mp->prev;
6578 else
6579 minipool_vector_tail = mp->prev;
6581 /* Re-insert it before MAX_MP. */
6582 mp->next = max_mp;
6583 mp->prev = max_mp->prev;
6584 max_mp->prev = mp;
6586 if (mp->prev != NULL)
6587 mp->prev->next = mp;
6588 else
6589 minipool_vector_head = mp;
6592 /* Save the new entry. */
6593 max_mp = mp;
6595 /* Scan over the preceding entries and adjust their addresses as
6596 required. */
6597 while (mp->prev != NULL
6598 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6600 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6601 mp = mp->prev;
6604 return max_mp;
6607 /* Add a constant to the minipool for a forward reference. Returns the
6608 node added or NULL if the constant will not fit in this pool. */
6609 static Mnode *
6610 add_minipool_forward_ref (Mfix *fix)
6612 /* If set, max_mp is the first pool_entry that has a lower
6613 constraint than the one we are trying to add. */
6614 Mnode * max_mp = NULL;
6615 HOST_WIDE_INT max_address = fix->address + fix->forwards;
6616 Mnode * mp;
6618 /* If this fix's address is greater than the address of the first
6619 entry, then we can't put the fix in this pool. We subtract the
6620 size of the current fix to ensure that if the table is fully
6621 packed we still have enough room to insert this value by suffling
6622 the other fixes forwards. */
6623 if (minipool_vector_head &&
6624 fix->address >= minipool_vector_head->max_address - fix->fix_size)
6625 return NULL;
6627 /* Scan the pool to see if a constant with the same value has
6628 already been added. While we are doing this, also note the
6629 location where we must insert the constant if it doesn't already
6630 exist. */
6631 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6633 if (GET_CODE (fix->value) == GET_CODE (mp->value)
6634 && fix->mode == mp->mode
6635 && (GET_CODE (fix->value) != CODE_LABEL
6636 || (CODE_LABEL_NUMBER (fix->value)
6637 == CODE_LABEL_NUMBER (mp->value)))
6638 && rtx_equal_p (fix->value, mp->value))
6640 /* More than one fix references this entry. */
6641 mp->refcount++;
6642 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6645 /* Note the insertion point if necessary. */
6646 if (max_mp == NULL
6647 && mp->max_address > max_address)
6648 max_mp = mp;
6650 /* If we are inserting an 8-bytes aligned quantity and
6651 we have not already found an insertion point, then
6652 make sure that all such 8-byte aligned quantities are
6653 placed at the start of the pool. */
6654 if (ARM_DOUBLEWORD_ALIGN
6655 && max_mp == NULL
6656 && fix->fix_size == 8
6657 && mp->fix_size != 8)
6659 max_mp = mp;
6660 max_address = mp->max_address;
6664 /* The value is not currently in the minipool, so we need to create
6665 a new entry for it. If MAX_MP is NULL, the entry will be put on
6666 the end of the list since the placement is less constrained than
6667 any existing entry. Otherwise, we insert the new fix before
6668 MAX_MP and, if necessary, adjust the constraints on the other
6669 entries. */
6670 mp = xmalloc (sizeof (* mp));
6671 mp->fix_size = fix->fix_size;
6672 mp->mode = fix->mode;
6673 mp->value = fix->value;
6674 mp->refcount = 1;
6675 /* Not yet required for a backwards ref. */
6676 mp->min_address = -65536;
6678 if (max_mp == NULL)
6680 mp->max_address = max_address;
6681 mp->next = NULL;
6682 mp->prev = minipool_vector_tail;
6684 if (mp->prev == NULL)
6686 minipool_vector_head = mp;
6687 minipool_vector_label = gen_label_rtx ();
6689 else
6690 mp->prev->next = mp;
6692 minipool_vector_tail = mp;
6694 else
6696 if (max_address > max_mp->max_address - mp->fix_size)
6697 mp->max_address = max_mp->max_address - mp->fix_size;
6698 else
6699 mp->max_address = max_address;
6701 mp->next = max_mp;
6702 mp->prev = max_mp->prev;
6703 max_mp->prev = mp;
6704 if (mp->prev != NULL)
6705 mp->prev->next = mp;
6706 else
6707 minipool_vector_head = mp;
6710 /* Save the new entry. */
6711 max_mp = mp;
6713 /* Scan over the preceding entries and adjust their addresses as
6714 required. */
6715 while (mp->prev != NULL
6716 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6718 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6719 mp = mp->prev;
6722 return max_mp;
6725 static Mnode *
6726 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
6727 HOST_WIDE_INT min_address)
6729 HOST_WIDE_INT offset;
6731 /* This should never be true, and the code below assumes these are
6732 different. */
6733 if (mp == min_mp)
6734 abort ();
6736 if (min_mp == NULL)
6738 if (min_address > mp->min_address)
6739 mp->min_address = min_address;
6741 else
6743 /* We will adjust this below if it is too loose. */
6744 mp->min_address = min_address;
6746 /* Unlink MP from its current position. Since min_mp is non-null,
6747 mp->next must be non-null. */
6748 mp->next->prev = mp->prev;
6749 if (mp->prev != NULL)
6750 mp->prev->next = mp->next;
6751 else
6752 minipool_vector_head = mp->next;
6754 /* Reinsert it after MIN_MP. */
6755 mp->prev = min_mp;
6756 mp->next = min_mp->next;
6757 min_mp->next = mp;
6758 if (mp->next != NULL)
6759 mp->next->prev = mp;
6760 else
6761 minipool_vector_tail = mp;
6764 min_mp = mp;
6766 offset = 0;
6767 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6769 mp->offset = offset;
6770 if (mp->refcount > 0)
6771 offset += mp->fix_size;
6773 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
6774 mp->next->min_address = mp->min_address + mp->fix_size;
6777 return min_mp;
6780 /* Add a constant to the minipool for a backward reference. Returns the
6781 node added or NULL if the constant will not fit in this pool.
6783 Note that the code for insertion for a backwards reference can be
6784 somewhat confusing because the calculated offsets for each fix do
6785 not take into account the size of the pool (which is still under
6786 construction. */
6787 static Mnode *
6788 add_minipool_backward_ref (Mfix *fix)
6790 /* If set, min_mp is the last pool_entry that has a lower constraint
6791 than the one we are trying to add. */
6792 Mnode *min_mp = NULL;
6793 /* This can be negative, since it is only a constraint. */
6794 HOST_WIDE_INT min_address = fix->address - fix->backwards;
6795 Mnode *mp;
6797 /* If we can't reach the current pool from this insn, or if we can't
6798 insert this entry at the end of the pool without pushing other
6799 fixes out of range, then we don't try. This ensures that we
6800 can't fail later on. */
6801 if (min_address >= minipool_barrier->address
6802 || (minipool_vector_tail->min_address + fix->fix_size
6803 >= minipool_barrier->address))
6804 return NULL;
6806 /* Scan the pool to see if a constant with the same value has
6807 already been added. While we are doing this, also note the
6808 location where we must insert the constant if it doesn't already
6809 exist. */
6810 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
6812 if (GET_CODE (fix->value) == GET_CODE (mp->value)
6813 && fix->mode == mp->mode
6814 && (GET_CODE (fix->value) != CODE_LABEL
6815 || (CODE_LABEL_NUMBER (fix->value)
6816 == CODE_LABEL_NUMBER (mp->value)))
6817 && rtx_equal_p (fix->value, mp->value)
6818 /* Check that there is enough slack to move this entry to the
6819 end of the table (this is conservative). */
6820 && (mp->max_address
6821 > (minipool_barrier->address
6822 + minipool_vector_tail->offset
6823 + minipool_vector_tail->fix_size)))
6825 mp->refcount++;
6826 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
6829 if (min_mp != NULL)
6830 mp->min_address += fix->fix_size;
6831 else
6833 /* Note the insertion point if necessary. */
6834 if (mp->min_address < min_address)
6836 /* For now, we do not allow the insertion of 8-byte alignment
6837 requiring nodes anywhere but at the start of the pool. */
6838 if (ARM_DOUBLEWORD_ALIGN
6839 && fix->fix_size == 8 && mp->fix_size != 8)
6840 return NULL;
6841 else
6842 min_mp = mp;
6844 else if (mp->max_address
6845 < minipool_barrier->address + mp->offset + fix->fix_size)
6847 /* Inserting before this entry would push the fix beyond
6848 its maximum address (which can happen if we have
6849 re-located a forwards fix); force the new fix to come
6850 after it. */
6851 min_mp = mp;
6852 min_address = mp->min_address + fix->fix_size;
6854 /* If we are inserting an 8-bytes aligned quantity and
6855 we have not already found an insertion point, then
6856 make sure that all such 8-byte aligned quantities are
6857 placed at the start of the pool. */
6858 else if (ARM_DOUBLEWORD_ALIGN
6859 && min_mp == NULL
6860 && fix->fix_size == 8
6861 && mp->fix_size < 8)
6863 min_mp = mp;
6864 min_address = mp->min_address + fix->fix_size;
6869 /* We need to create a new entry. */
6870 mp = xmalloc (sizeof (* mp));
6871 mp->fix_size = fix->fix_size;
6872 mp->mode = fix->mode;
6873 mp->value = fix->value;
6874 mp->refcount = 1;
6875 mp->max_address = minipool_barrier->address + 65536;
6877 mp->min_address = min_address;
6879 if (min_mp == NULL)
6881 mp->prev = NULL;
6882 mp->next = minipool_vector_head;
6884 if (mp->next == NULL)
6886 minipool_vector_tail = mp;
6887 minipool_vector_label = gen_label_rtx ();
6889 else
6890 mp->next->prev = mp;
6892 minipool_vector_head = mp;
6894 else
6896 mp->next = min_mp->next;
6897 mp->prev = min_mp;
6898 min_mp->next = mp;
6900 if (mp->next != NULL)
6901 mp->next->prev = mp;
6902 else
6903 minipool_vector_tail = mp;
6906 /* Save the new entry. */
6907 min_mp = mp;
6909 if (mp->prev)
6910 mp = mp->prev;
6911 else
6912 mp->offset = 0;
6914 /* Scan over the following entries and adjust their offsets. */
6915 while (mp->next != NULL)
6917 if (mp->next->min_address < mp->min_address + mp->fix_size)
6918 mp->next->min_address = mp->min_address + mp->fix_size;
6920 if (mp->refcount)
6921 mp->next->offset = mp->offset + mp->fix_size;
6922 else
6923 mp->next->offset = mp->offset;
6925 mp = mp->next;
6928 return min_mp;
6931 static void
6932 assign_minipool_offsets (Mfix *barrier)
6934 HOST_WIDE_INT offset = 0;
6935 Mnode *mp;
6937 minipool_barrier = barrier;
6939 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6941 mp->offset = offset;
6943 if (mp->refcount > 0)
6944 offset += mp->fix_size;
6948 /* Output the literal table */
6949 static void
6950 dump_minipool (rtx scan)
6952 Mnode * mp;
6953 Mnode * nmp;
6954 int align64 = 0;
6956 if (ARM_DOUBLEWORD_ALIGN)
6957 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6958 if (mp->refcount > 0 && mp->fix_size == 8)
6960 align64 = 1;
6961 break;
6964 if (dump_file)
6965 fprintf (dump_file,
6966 ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
6967 INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
6969 scan = emit_label_after (gen_label_rtx (), scan);
6970 scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
6971 scan = emit_label_after (minipool_vector_label, scan);
6973 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
6975 if (mp->refcount > 0)
6977 if (dump_file)
6979 fprintf (dump_file,
6980 ";; Offset %u, min %ld, max %ld ",
6981 (unsigned) mp->offset, (unsigned long) mp->min_address,
6982 (unsigned long) mp->max_address);
6983 arm_print_value (dump_file, mp->value);
6984 fputc ('\n', dump_file);
6987 switch (mp->fix_size)
6989 #ifdef HAVE_consttable_1
6990 case 1:
6991 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
6992 break;
6994 #endif
6995 #ifdef HAVE_consttable_2
6996 case 2:
6997 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
6998 break;
7000 #endif
7001 #ifdef HAVE_consttable_4
7002 case 4:
7003 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7004 break;
7006 #endif
7007 #ifdef HAVE_consttable_8
7008 case 8:
7009 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7010 break;
7012 #endif
7013 default:
7014 abort ();
7015 break;
7019 nmp = mp->next;
7020 free (mp);
7023 minipool_vector_head = minipool_vector_tail = NULL;
7024 scan = emit_insn_after (gen_consttable_end (), scan);
7025 scan = emit_barrier_after (scan);
7028 /* Return the cost of forcibly inserting a barrier after INSN. */
7029 static int
7030 arm_barrier_cost (rtx insn)
7032 /* Basing the location of the pool on the loop depth is preferable,
7033 but at the moment, the basic block information seems to be
7034 corrupt by this stage of the compilation. */
7035 int base_cost = 50;
7036 rtx next = next_nonnote_insn (insn);
7038 if (next != NULL && GET_CODE (next) == CODE_LABEL)
7039 base_cost -= 20;
7041 switch (GET_CODE (insn))
7043 case CODE_LABEL:
7044 /* It will always be better to place the table before the label, rather
7045 than after it. */
7046 return 50;
7048 case INSN:
7049 case CALL_INSN:
7050 return base_cost;
7052 case JUMP_INSN:
7053 return base_cost - 10;
7055 default:
7056 return base_cost + 10;
7060 /* Find the best place in the insn stream in the range
7061 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7062 Create the barrier by inserting a jump and add a new fix entry for
7063 it. */
7064 static Mfix *
7065 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7067 HOST_WIDE_INT count = 0;
7068 rtx barrier;
7069 rtx from = fix->insn;
7070 rtx selected = from;
7071 int selected_cost;
7072 HOST_WIDE_INT selected_address;
7073 Mfix * new_fix;
7074 HOST_WIDE_INT max_count = max_address - fix->address;
7075 rtx label = gen_label_rtx ();
7077 selected_cost = arm_barrier_cost (from);
7078 selected_address = fix->address;
7080 while (from && count < max_count)
7082 rtx tmp;
7083 int new_cost;
7085 /* This code shouldn't have been called if there was a natural barrier
7086 within range. */
7087 if (GET_CODE (from) == BARRIER)
7088 abort ();
7090 /* Count the length of this insn. */
7091 count += get_attr_length (from);
7093 /* If there is a jump table, add its length. */
7094 tmp = is_jump_table (from);
7095 if (tmp != NULL)
7097 count += get_jump_table_size (tmp);
7099 /* Jump tables aren't in a basic block, so base the cost on
7100 the dispatch insn. If we select this location, we will
7101 still put the pool after the table. */
7102 new_cost = arm_barrier_cost (from);
7104 if (count < max_count && new_cost <= selected_cost)
7106 selected = tmp;
7107 selected_cost = new_cost;
7108 selected_address = fix->address + count;
7111 /* Continue after the dispatch table. */
7112 from = NEXT_INSN (tmp);
7113 continue;
7116 new_cost = arm_barrier_cost (from);
7118 if (count < max_count && new_cost <= selected_cost)
7120 selected = from;
7121 selected_cost = new_cost;
7122 selected_address = fix->address + count;
7125 from = NEXT_INSN (from);
7128 /* Create a new JUMP_INSN that branches around a barrier. */
7129 from = emit_jump_insn_after (gen_jump (label), selected);
7130 JUMP_LABEL (from) = label;
7131 barrier = emit_barrier_after (from);
7132 emit_label_after (label, barrier);
7134 /* Create a minipool barrier entry for the new barrier. */
7135 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7136 new_fix->insn = barrier;
7137 new_fix->address = selected_address;
7138 new_fix->next = fix->next;
7139 fix->next = new_fix;
7141 return new_fix;
7144 /* Record that there is a natural barrier in the insn stream at
7145 ADDRESS. */
7146 static void
7147 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7149 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7151 fix->insn = insn;
7152 fix->address = address;
7154 fix->next = NULL;
7155 if (minipool_fix_head != NULL)
7156 minipool_fix_tail->next = fix;
7157 else
7158 minipool_fix_head = fix;
7160 minipool_fix_tail = fix;
7163 /* Record INSN, which will need fixing up to load a value from the
7164 minipool. ADDRESS is the offset of the insn since the start of the
7165 function; LOC is a pointer to the part of the insn which requires
7166 fixing; VALUE is the constant that must be loaded, which is of type
7167 MODE. */
7168 static void
7169 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7170 enum machine_mode mode, rtx value)
7172 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7174 #ifdef AOF_ASSEMBLER
7175 /* PIC symbol references need to be converted into offsets into the
7176 based area. */
7177 /* XXX This shouldn't be done here. */
7178 if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7179 value = aof_pic_entry (value);
7180 #endif /* AOF_ASSEMBLER */
7182 fix->insn = insn;
7183 fix->address = address;
7184 fix->loc = loc;
7185 fix->mode = mode;
7186 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7187 fix->value = value;
7188 fix->forwards = get_attr_pool_range (insn);
7189 fix->backwards = get_attr_neg_pool_range (insn);
7190 fix->minipool = NULL;
7192 /* If an insn doesn't have a range defined for it, then it isn't
7193 expecting to be reworked by this code. Better to abort now than
7194 to generate duff assembly code. */
7195 if (fix->forwards == 0 && fix->backwards == 0)
7196 abort ();
7198 /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7199 So there might be an empty word before the start of the pool.
7200 Hence we reduce the forward range by 4 to allow for this
7201 possibility. */
7202 if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7203 fix->forwards -= 4;
7205 if (dump_file)
7207 fprintf (dump_file,
7208 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7209 GET_MODE_NAME (mode),
7210 INSN_UID (insn), (unsigned long) address,
7211 -1 * (long)fix->backwards, (long)fix->forwards);
7212 arm_print_value (dump_file, fix->value);
7213 fprintf (dump_file, "\n");
7216 /* Add it to the chain of fixes. */
7217 fix->next = NULL;
7219 if (minipool_fix_head != NULL)
7220 minipool_fix_tail->next = fix;
7221 else
7222 minipool_fix_head = fix;
7224 minipool_fix_tail = fix;
7227 /* Scan INSN and note any of its operands that need fixing.
7228 If DO_PUSHES is false we do not actually push any of the fixups
7229 needed. The function returns TRUE is any fixups were needed/pushed.
7230 This is used by arm_memory_load_p() which needs to know about loads
7231 of constants that will be converted into minipool loads. */
7232 static bool
7233 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7235 bool result = false;
7236 int opno;
7238 extract_insn (insn);
7240 if (!constrain_operands (1))
7241 fatal_insn_not_found (insn);
7243 if (recog_data.n_alternatives == 0)
7244 return false;
7246 /* Fill in recog_op_alt with information about the constraints of this insn. */
7247 preprocess_constraints ();
7249 for (opno = 0; opno < recog_data.n_operands; opno++)
7251 /* Things we need to fix can only occur in inputs. */
7252 if (recog_data.operand_type[opno] != OP_IN)
7253 continue;
7255 /* If this alternative is a memory reference, then any mention
7256 of constants in this alternative is really to fool reload
7257 into allowing us to accept one there. We need to fix them up
7258 now so that we output the right code. */
7259 if (recog_op_alt[opno][which_alternative].memory_ok)
7261 rtx op = recog_data.operand[opno];
7263 if (CONSTANT_P (op))
7265 if (do_pushes)
7266 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7267 recog_data.operand_mode[opno], op);
7268 result = true;
7270 else if (GET_CODE (op) == MEM
7271 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7272 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7274 if (do_pushes)
7276 rtx cop = avoid_constant_pool_reference (op);
7278 /* Casting the address of something to a mode narrower
7279 than a word can cause avoid_constant_pool_reference()
7280 to return the pool reference itself. That's no good to
7281 us here. Lets just hope that we can use the
7282 constant pool value directly. */
7283 if (op == cop)
7284 cop = get_pool_constant (XEXP (op, 0));
7286 push_minipool_fix (insn, address,
7287 recog_data.operand_loc[opno],
7288 recog_data.operand_mode[opno], cop);
7291 result = true;
7296 return result;
7299 /* Gcc puts the pool in the wrong place for ARM, since we can only
7300 load addresses a limited distance around the pc. We do some
7301 special munging to move the constant pool values to the correct
7302 point in the code. */
7303 static void
7304 arm_reorg (void)
7306 rtx insn;
7307 HOST_WIDE_INT address = 0;
7308 Mfix * fix;
7310 minipool_fix_head = minipool_fix_tail = NULL;
7312 /* The first insn must always be a note, or the code below won't
7313 scan it properly. */
7314 insn = get_insns ();
7315 if (GET_CODE (insn) != NOTE)
7316 abort ();
7318 /* Scan all the insns and record the operands that will need fixing. */
7319 for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7321 if (TARGET_CIRRUS_FIX_INVALID_INSNS
7322 && (arm_cirrus_insn_p (insn)
7323 || GET_CODE (insn) == JUMP_INSN
7324 || arm_memory_load_p (insn)))
7325 cirrus_reorg (insn);
7327 if (GET_CODE (insn) == BARRIER)
7328 push_minipool_barrier (insn, address);
7329 else if (INSN_P (insn))
7331 rtx table;
7333 note_invalid_constants (insn, address, true);
7334 address += get_attr_length (insn);
7336 /* If the insn is a vector jump, add the size of the table
7337 and skip the table. */
7338 if ((table = is_jump_table (insn)) != NULL)
7340 address += get_jump_table_size (table);
7341 insn = table;
7346 fix = minipool_fix_head;
7348 /* Now scan the fixups and perform the required changes. */
7349 while (fix)
7351 Mfix * ftmp;
7352 Mfix * fdel;
7353 Mfix * last_added_fix;
7354 Mfix * last_barrier = NULL;
7355 Mfix * this_fix;
7357 /* Skip any further barriers before the next fix. */
7358 while (fix && GET_CODE (fix->insn) == BARRIER)
7359 fix = fix->next;
7361 /* No more fixes. */
7362 if (fix == NULL)
7363 break;
7365 last_added_fix = NULL;
7367 for (ftmp = fix; ftmp; ftmp = ftmp->next)
7369 if (GET_CODE (ftmp->insn) == BARRIER)
7371 if (ftmp->address >= minipool_vector_head->max_address)
7372 break;
7374 last_barrier = ftmp;
7376 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7377 break;
7379 last_added_fix = ftmp; /* Keep track of the last fix added. */
7382 /* If we found a barrier, drop back to that; any fixes that we
7383 could have reached but come after the barrier will now go in
7384 the next mini-pool. */
7385 if (last_barrier != NULL)
7387 /* Reduce the refcount for those fixes that won't go into this
7388 pool after all. */
7389 for (fdel = last_barrier->next;
7390 fdel && fdel != ftmp;
7391 fdel = fdel->next)
7393 fdel->minipool->refcount--;
7394 fdel->minipool = NULL;
7397 ftmp = last_barrier;
7399 else
7401 /* ftmp is first fix that we can't fit into this pool and
7402 there no natural barriers that we could use. Insert a
7403 new barrier in the code somewhere between the previous
7404 fix and this one, and arrange to jump around it. */
7405 HOST_WIDE_INT max_address;
7407 /* The last item on the list of fixes must be a barrier, so
7408 we can never run off the end of the list of fixes without
7409 last_barrier being set. */
7410 if (ftmp == NULL)
7411 abort ();
7413 max_address = minipool_vector_head->max_address;
7414 /* Check that there isn't another fix that is in range that
7415 we couldn't fit into this pool because the pool was
7416 already too large: we need to put the pool before such an
7417 instruction. */
7418 if (ftmp->address < max_address)
7419 max_address = ftmp->address;
7421 last_barrier = create_fix_barrier (last_added_fix, max_address);
7424 assign_minipool_offsets (last_barrier);
7426 while (ftmp)
7428 if (GET_CODE (ftmp->insn) != BARRIER
7429 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7430 == NULL))
7431 break;
7433 ftmp = ftmp->next;
7436 /* Scan over the fixes we have identified for this pool, fixing them
7437 up and adding the constants to the pool itself. */
7438 for (this_fix = fix; this_fix && ftmp != this_fix;
7439 this_fix = this_fix->next)
7440 if (GET_CODE (this_fix->insn) != BARRIER)
7442 rtx addr
7443 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
7444 minipool_vector_label),
7445 this_fix->minipool->offset);
7446 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7449 dump_minipool (last_barrier->insn);
7450 fix = ftmp;
7453 /* From now on we must synthesize any constants that we can't handle
7454 directly. This can happen if the RTL gets split during final
7455 instruction generation. */
7456 after_arm_reorg = 1;
7458 /* Free the minipool memory. */
7459 obstack_free (&minipool_obstack, minipool_startobj);
7462 /* Routines to output assembly language. */
7464 /* If the rtx is the correct value then return the string of the number.
7465 In this way we can ensure that valid double constants are generated even
7466 when cross compiling. */
7467 const char *
7468 fp_immediate_constant (rtx x)
7470 REAL_VALUE_TYPE r;
7471 int i;
7473 if (!fp_consts_inited)
7474 init_fp_table ();
7476 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7477 for (i = 0; i < 8; i++)
7478 if (REAL_VALUES_EQUAL (r, values_fp[i]))
7479 return strings_fp[i];
7481 abort ();
7484 /* As for fp_immediate_constant, but value is passed directly, not in rtx. */
7485 static const char *
7486 fp_const_from_val (REAL_VALUE_TYPE *r)
7488 int i;
7490 if (!fp_consts_inited)
7491 init_fp_table ();
7493 for (i = 0; i < 8; i++)
7494 if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7495 return strings_fp[i];
7497 abort ();
7500 /* Output the operands of a LDM/STM instruction to STREAM.
7501 MASK is the ARM register set mask of which only bits 0-15 are important.
7502 REG is the base register, either the frame pointer or the stack pointer,
7503 INSTR is the possibly suffixed load or store instruction. */
7504 static void
7505 print_multi_reg (FILE *stream, const char *instr, int reg, int mask)
7507 int i;
7508 int not_first = FALSE;
7510 fputc ('\t', stream);
7511 asm_fprintf (stream, instr, reg);
7512 fputs (", {", stream);
7514 for (i = 0; i <= LAST_ARM_REGNUM; i++)
7515 if (mask & (1 << i))
7517 if (not_first)
7518 fprintf (stream, ", ");
7520 asm_fprintf (stream, "%r", i);
7521 not_first = TRUE;
7524 fprintf (stream, "}\n");
7528 /* Output a FLDMX instruction to STREAM.
7529 BASE if the register containing the address.
7530 REG and COUNT specify the register range.
7531 Extra registers may be added to avoid hardware bugs. */
7533 static void
7534 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7536 int i;
7538 /* Workaround ARM10 VFPr1 bug. */
7539 if (count == 2 && !arm_arch6)
7541 if (reg == 15)
7542 reg--;
7543 count++;
7546 fputc ('\t', stream);
7547 asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7549 for (i = reg; i < reg + count; i++)
7551 if (i > reg)
7552 fputs (", ", stream);
7553 asm_fprintf (stream, "d%d", i);
7555 fputs ("}\n", stream);
7560 /* Output the assembly for a store multiple. */
7562 const char *
7563 vfp_output_fstmx (rtx * operands)
7565 char pattern[100];
7566 int p;
7567 int base;
7568 int i;
7570 strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7571 p = strlen (pattern);
7573 if (GET_CODE (operands[1]) != REG)
7574 abort ();
7576 base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7577 for (i = 1; i < XVECLEN (operands[2], 0); i++)
7579 p += sprintf (&pattern[p], ", d%d", base + i);
7581 strcpy (&pattern[p], "}");
7583 output_asm_insn (pattern, operands);
7584 return "";
7588 /* Emit RTL to save block of VFP register pairs to the stack. Returns the
7589 number of bytes pushed. */
7591 static int
7592 vfp_emit_fstmx (int base_reg, int count)
7594 rtx par;
7595 rtx dwarf;
7596 rtx tmp, reg;
7597 int i;
7599 /* Workaround ARM10 VFPr1 bug. Data corruption can occur when exactly two
7600 register pairs are stored by a store multiple insn. We avoid this
7601 by pushing an extra pair. */
7602 if (count == 2 && !arm_arch6)
7604 if (base_reg == LAST_VFP_REGNUM - 3)
7605 base_reg -= 2;
7606 count++;
7609 /* ??? The frame layout is implementation defined. We describe
7610 standard format 1 (equivalent to a FSTMD insn and unused pad word).
7611 We really need some way of representing the whole block so that the
7612 unwinder can figure it out at runtime. */
7613 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7614 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
7616 reg = gen_rtx_REG (DFmode, base_reg);
7617 base_reg += 2;
7619 XVECEXP (par, 0, 0)
7620 = gen_rtx_SET (VOIDmode,
7621 gen_rtx_MEM (BLKmode,
7622 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7623 gen_rtx_UNSPEC (BLKmode,
7624 gen_rtvec (1, reg),
7625 UNSPEC_PUSH_MULT));
7627 tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7628 gen_rtx_PLUS (SImode, stack_pointer_rtx,
7629 GEN_INT (-(count * 8 + 4))));
7630 RTX_FRAME_RELATED_P (tmp) = 1;
7631 XVECEXP (dwarf, 0, 0) = tmp;
7633 tmp = gen_rtx_SET (VOIDmode,
7634 gen_rtx_MEM (DFmode, stack_pointer_rtx),
7635 reg);
7636 RTX_FRAME_RELATED_P (tmp) = 1;
7637 XVECEXP (dwarf, 0, 1) = tmp;
7639 for (i = 1; i < count; i++)
7641 reg = gen_rtx_REG (DFmode, base_reg);
7642 base_reg += 2;
7643 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
7645 tmp = gen_rtx_SET (VOIDmode,
7646 gen_rtx_MEM (DFmode,
7647 gen_rtx_PLUS (SImode,
7648 stack_pointer_rtx,
7649 GEN_INT (i * 8))),
7650 reg);
7651 RTX_FRAME_RELATED_P (tmp) = 1;
7652 XVECEXP (dwarf, 0, i + 1) = tmp;
7655 par = emit_insn (par);
7656 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7657 REG_NOTES (par));
7658 RTX_FRAME_RELATED_P (par) = 1;
7660 return count * 8 + 4;
7664 /* Output a 'call' insn. */
7665 const char *
7666 output_call (rtx *operands)
7668 if (arm_arch5)
7669 abort (); /* Patterns should call blx <reg> directly. */
7671 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
7672 if (REGNO (operands[0]) == LR_REGNUM)
7674 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
7675 output_asm_insn ("mov%?\t%0, %|lr", operands);
7678 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7680 if (TARGET_INTERWORK || arm_arch4t)
7681 output_asm_insn ("bx%?\t%0", operands);
7682 else
7683 output_asm_insn ("mov%?\t%|pc, %0", operands);
7685 return "";
7688 /* Output a 'call' insn that is a reference in memory. */
7689 const char *
7690 output_call_mem (rtx *operands)
7692 if (TARGET_INTERWORK && !arm_arch5)
7694 output_asm_insn ("ldr%?\t%|ip, %0", operands);
7695 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7696 output_asm_insn ("bx%?\t%|ip", operands);
7698 else if (regno_use_in (LR_REGNUM, operands[0]))
7700 /* LR is used in the memory address. We load the address in the
7701 first instruction. It's safe to use IP as the target of the
7702 load since the call will kill it anyway. */
7703 output_asm_insn ("ldr%?\t%|ip, %0", operands);
7704 if (arm_arch5)
7705 output_asm_insn ("blx%?\t%|ip", operands);
7706 else
7708 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7709 if (arm_arch4t)
7710 output_asm_insn ("bx%?\t%|ip", operands);
7711 else
7712 output_asm_insn ("mov%?\t%|pc, %|ip", operands);
7715 else
7717 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7718 output_asm_insn ("ldr%?\t%|pc, %0", operands);
7721 return "";
7725 /* Output a move from arm registers to an fpa registers.
7726 OPERANDS[0] is an fpa register.
7727 OPERANDS[1] is the first registers of an arm register pair. */
7728 const char *
7729 output_mov_long_double_fpa_from_arm (rtx *operands)
7731 int arm_reg0 = REGNO (operands[1]);
7732 rtx ops[3];
7734 if (arm_reg0 == IP_REGNUM)
7735 abort ();
7737 ops[0] = gen_rtx_REG (SImode, arm_reg0);
7738 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7739 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
7741 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
7742 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
7744 return "";
7747 /* Output a move from an fpa register to arm registers.
7748 OPERANDS[0] is the first registers of an arm register pair.
7749 OPERANDS[1] is an fpa register. */
7750 const char *
7751 output_mov_long_double_arm_from_fpa (rtx *operands)
7753 int arm_reg0 = REGNO (operands[0]);
7754 rtx ops[3];
7756 if (arm_reg0 == IP_REGNUM)
7757 abort ();
7759 ops[0] = gen_rtx_REG (SImode, arm_reg0);
7760 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7761 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
7763 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
7764 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
7765 return "";
7768 /* Output a move from arm registers to arm registers of a long double
7769 OPERANDS[0] is the destination.
7770 OPERANDS[1] is the source. */
7771 const char *
7772 output_mov_long_double_arm_from_arm (rtx *operands)
7774 /* We have to be careful here because the two might overlap. */
7775 int dest_start = REGNO (operands[0]);
7776 int src_start = REGNO (operands[1]);
7777 rtx ops[2];
7778 int i;
7780 if (dest_start < src_start)
7782 for (i = 0; i < 3; i++)
7784 ops[0] = gen_rtx_REG (SImode, dest_start + i);
7785 ops[1] = gen_rtx_REG (SImode, src_start + i);
7786 output_asm_insn ("mov%?\t%0, %1", ops);
7789 else
7791 for (i = 2; i >= 0; i--)
7793 ops[0] = gen_rtx_REG (SImode, dest_start + i);
7794 ops[1] = gen_rtx_REG (SImode, src_start + i);
7795 output_asm_insn ("mov%?\t%0, %1", ops);
7799 return "";
7803 /* Output a move from arm registers to an fpa registers.
7804 OPERANDS[0] is an fpa register.
7805 OPERANDS[1] is the first registers of an arm register pair. */
7806 const char *
7807 output_mov_double_fpa_from_arm (rtx *operands)
7809 int arm_reg0 = REGNO (operands[1]);
7810 rtx ops[2];
7812 if (arm_reg0 == IP_REGNUM)
7813 abort ();
7815 ops[0] = gen_rtx_REG (SImode, arm_reg0);
7816 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7817 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
7818 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
7819 return "";
7822 /* Output a move from an fpa register to arm registers.
7823 OPERANDS[0] is the first registers of an arm register pair.
7824 OPERANDS[1] is an fpa register. */
7825 const char *
7826 output_mov_double_arm_from_fpa (rtx *operands)
7828 int arm_reg0 = REGNO (operands[0]);
7829 rtx ops[2];
7831 if (arm_reg0 == IP_REGNUM)
7832 abort ();
7834 ops[0] = gen_rtx_REG (SImode, arm_reg0);
7835 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7836 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
7837 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
7838 return "";
7841 /* Output a move between double words.
7842 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
7843 or MEM<-REG and all MEMs must be offsettable addresses. */
7844 const char *
7845 output_move_double (rtx *operands)
7847 enum rtx_code code0 = GET_CODE (operands[0]);
7848 enum rtx_code code1 = GET_CODE (operands[1]);
7849 rtx otherops[3];
7851 if (code0 == REG)
7853 int reg0 = REGNO (operands[0]);
7855 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
7857 if (code1 == REG)
7859 int reg1 = REGNO (operands[1]);
7860 if (reg1 == IP_REGNUM)
7861 abort ();
7863 /* Ensure the second source is not overwritten. */
7864 if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
7865 output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
7866 else
7867 output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
7869 else if (code1 == CONST_VECTOR)
7871 HOST_WIDE_INT hint = 0;
7873 switch (GET_MODE (operands[1]))
7875 case V2SImode:
7876 otherops[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 1)));
7877 operands[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 0)));
7878 break;
7880 case V4HImode:
7881 if (BYTES_BIG_ENDIAN)
7883 hint = INTVAL (CONST_VECTOR_ELT (operands[1], 2));
7884 hint <<= 16;
7885 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
7887 else
7889 hint = INTVAL (CONST_VECTOR_ELT (operands[1], 3));
7890 hint <<= 16;
7891 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
7894 otherops[1] = GEN_INT (hint);
7895 hint = 0;
7897 if (BYTES_BIG_ENDIAN)
7899 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
7900 hint <<= 16;
7901 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
7903 else
7905 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
7906 hint <<= 16;
7907 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
7910 operands[1] = GEN_INT (hint);
7911 break;
7913 case V8QImode:
7914 if (BYTES_BIG_ENDIAN)
7916 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
7917 hint <<= 8;
7918 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
7919 hint <<= 8;
7920 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
7921 hint <<= 8;
7922 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
7924 else
7926 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
7927 hint <<= 8;
7928 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
7929 hint <<= 8;
7930 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
7931 hint <<= 8;
7932 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
7935 otherops[1] = GEN_INT (hint);
7936 hint = 0;
7938 if (BYTES_BIG_ENDIAN)
7940 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
7941 hint <<= 8;
7942 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
7943 hint <<= 8;
7944 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
7945 hint <<= 8;
7946 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
7948 else
7950 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
7951 hint <<= 8;
7952 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
7953 hint <<= 8;
7954 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
7955 hint <<= 8;
7956 hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
7959 operands[1] = GEN_INT (hint);
7960 break;
7962 default:
7963 abort ();
7965 output_mov_immediate (operands);
7966 output_mov_immediate (otherops);
7968 else if (code1 == CONST_DOUBLE)
7970 if (GET_MODE (operands[1]) == DFmode)
7972 REAL_VALUE_TYPE r;
7973 long l[2];
7975 REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
7976 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
7977 otherops[1] = GEN_INT (l[1]);
7978 operands[1] = GEN_INT (l[0]);
7980 else if (GET_MODE (operands[1]) != VOIDmode)
7981 abort ();
7982 else if (WORDS_BIG_ENDIAN)
7984 otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
7985 operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
7987 else
7989 otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
7990 operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
7993 output_mov_immediate (operands);
7994 output_mov_immediate (otherops);
7996 else if (code1 == CONST_INT)
7998 #if HOST_BITS_PER_WIDE_INT > 32
7999 /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
8000 what the upper word is. */
8001 if (WORDS_BIG_ENDIAN)
8003 otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8004 operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8006 else
8008 otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8009 operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8011 #else
8012 /* Sign extend the intval into the high-order word. */
8013 if (WORDS_BIG_ENDIAN)
8015 otherops[1] = operands[1];
8016 operands[1] = (INTVAL (operands[1]) < 0
8017 ? constm1_rtx : const0_rtx);
8019 else
8020 otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
8021 #endif
8022 output_mov_immediate (otherops);
8023 output_mov_immediate (operands);
8025 else if (code1 == MEM)
8027 switch (GET_CODE (XEXP (operands[1], 0)))
8029 case REG:
8030 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8031 break;
8033 case PRE_INC:
8034 if (!TARGET_LDRD)
8035 abort (); /* Should never happen now. */
8036 output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8037 break;
8039 case PRE_DEC:
8040 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8041 break;
8043 case POST_INC:
8044 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8045 break;
8047 case POST_DEC:
8048 if (!TARGET_LDRD)
8049 abort (); /* Should never happen now. */
8050 output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8051 break;
8053 case PRE_MODIFY:
8054 case POST_MODIFY:
8055 otherops[0] = operands[0];
8056 otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8057 otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8059 if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8061 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8063 /* Registers overlap so split out the increment. */
8064 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8065 output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8067 else
8068 output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8070 else
8072 /* We only allow constant increments, so this is safe. */
8073 output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8075 break;
8077 case LABEL_REF:
8078 case CONST:
8079 output_asm_insn ("adr%?\t%0, %1", operands);
8080 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8081 break;
8083 default:
8084 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8085 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8087 otherops[0] = operands[0];
8088 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8089 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8091 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8093 if (GET_CODE (otherops[2]) == CONST_INT)
8095 switch ((int) INTVAL (otherops[2]))
8097 case -8:
8098 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8099 return "";
8100 case -4:
8101 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8102 return "";
8103 case 4:
8104 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8105 return "";
8108 if (TARGET_LDRD
8109 && (GET_CODE (otherops[2]) == REG
8110 || (GET_CODE (otherops[2]) == CONST_INT
8111 && INTVAL (otherops[2]) > -256
8112 && INTVAL (otherops[2]) < 256)))
8114 if (reg_overlap_mentioned_p (otherops[0],
8115 otherops[2]))
8117 /* Swap base and index registers over to
8118 avoid a conflict. */
8119 otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8120 otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8123 /* If both registers conflict, it will usually
8124 have been fixed by a splitter. */
8125 if (reg_overlap_mentioned_p (otherops[0],
8126 otherops[2]))
8128 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8129 output_asm_insn ("ldr%?d\t%0, [%1]",
8130 otherops);
8131 return "";
8133 else
8135 output_asm_insn ("ldr%?d\t%0, [%1, %2]",
8136 otherops);
8137 return "";
8140 if (GET_CODE (otherops[2]) == CONST_INT)
8142 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8143 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8144 else
8145 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8147 else
8148 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8150 else
8151 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8153 return "ldm%?ia\t%0, %M0";
8155 else
8157 otherops[1] = adjust_address (operands[1], SImode, 4);
8158 /* Take care of overlapping base/data reg. */
8159 if (reg_mentioned_p (operands[0], operands[1]))
8161 output_asm_insn ("ldr%?\t%0, %1", otherops);
8162 output_asm_insn ("ldr%?\t%0, %1", operands);
8164 else
8166 output_asm_insn ("ldr%?\t%0, %1", operands);
8167 output_asm_insn ("ldr%?\t%0, %1", otherops);
8172 else
8173 abort (); /* Constraints should prevent this. */
8175 else if (code0 == MEM && code1 == REG)
8177 if (REGNO (operands[1]) == IP_REGNUM)
8178 abort ();
8180 switch (GET_CODE (XEXP (operands[0], 0)))
8182 case REG:
8183 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8184 break;
8186 case PRE_INC:
8187 if (!TARGET_LDRD)
8188 abort (); /* Should never happen now. */
8189 output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8190 break;
8192 case PRE_DEC:
8193 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8194 break;
8196 case POST_INC:
8197 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8198 break;
8200 case POST_DEC:
8201 if (!TARGET_LDRD)
8202 abort (); /* Should never happen now. */
8203 output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8204 break;
8206 case PRE_MODIFY:
8207 case POST_MODIFY:
8208 otherops[0] = operands[1];
8209 otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8210 otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8212 if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8213 output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8214 else
8215 output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8216 break;
8218 case PLUS:
8219 otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8220 if (GET_CODE (otherops[2]) == CONST_INT)
8222 switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8224 case -8:
8225 output_asm_insn ("stm%?db\t%m0, %M1", operands);
8226 return "";
8228 case -4:
8229 output_asm_insn ("stm%?da\t%m0, %M1", operands);
8230 return "";
8232 case 4:
8233 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8234 return "";
8237 if (TARGET_LDRD
8238 && (GET_CODE (otherops[2]) == REG
8239 || (GET_CODE (otherops[2]) == CONST_INT
8240 && INTVAL (otherops[2]) > -256
8241 && INTVAL (otherops[2]) < 256)))
8243 otherops[0] = operands[1];
8244 otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8245 output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8246 return "";
8248 /* Fall through */
8250 default:
8251 otherops[0] = adjust_address (operands[0], SImode, 4);
8252 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8253 output_asm_insn ("str%?\t%1, %0", operands);
8254 output_asm_insn ("str%?\t%1, %0", otherops);
8257 else
8258 /* Constraints should prevent this. */
8259 abort ();
8261 return "";
8265 /* Output an arbitrary MOV reg, #n.
8266 OPERANDS[0] is a register. OPERANDS[1] is a const_int. */
8267 const char *
8268 output_mov_immediate (rtx *operands)
8270 HOST_WIDE_INT n = INTVAL (operands[1]);
8272 /* Try to use one MOV. */
8273 if (const_ok_for_arm (n))
8274 output_asm_insn ("mov%?\t%0, %1", operands);
8276 /* Try to use one MVN. */
8277 else if (const_ok_for_arm (~n))
8279 operands[1] = GEN_INT (~n);
8280 output_asm_insn ("mvn%?\t%0, %1", operands);
8282 else
8284 int n_ones = 0;
8285 int i;
8287 /* If all else fails, make it out of ORRs or BICs as appropriate. */
8288 for (i = 0; i < 32; i++)
8289 if (n & 1 << i)
8290 n_ones++;
8292 if (n_ones > 16) /* Shorter to use MVN with BIC in this case. */
8293 output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1, ~ n);
8294 else
8295 output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
8298 return "";
8301 /* Output an ADD r, s, #n where n may be too big for one instruction.
8302 If adding zero to one register, output nothing. */
8303 const char *
8304 output_add_immediate (rtx *operands)
8306 HOST_WIDE_INT n = INTVAL (operands[2]);
8308 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8310 if (n < 0)
8311 output_multi_immediate (operands,
8312 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8313 -n);
8314 else
8315 output_multi_immediate (operands,
8316 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8320 return "";
8323 /* Output a multiple immediate operation.
8324 OPERANDS is the vector of operands referred to in the output patterns.
8325 INSTR1 is the output pattern to use for the first constant.
8326 INSTR2 is the output pattern to use for subsequent constants.
8327 IMMED_OP is the index of the constant slot in OPERANDS.
8328 N is the constant value. */
8329 static const char *
8330 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8331 int immed_op, HOST_WIDE_INT n)
8333 #if HOST_BITS_PER_WIDE_INT > 32
8334 n &= 0xffffffff;
8335 #endif
8337 if (n == 0)
8339 /* Quick and easy output. */
8340 operands[immed_op] = const0_rtx;
8341 output_asm_insn (instr1, operands);
8343 else
8345 int i;
8346 const char * instr = instr1;
8348 /* Note that n is never zero here (which would give no output). */
8349 for (i = 0; i < 32; i += 2)
8351 if (n & (3 << i))
8353 operands[immed_op] = GEN_INT (n & (255 << i));
8354 output_asm_insn (instr, operands);
8355 instr = instr2;
8356 i += 6;
8361 return "";
8364 /* Return the appropriate ARM instruction for the operation code.
8365 The returned result should not be overwritten. OP is the rtx of the
8366 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8367 was shifted. */
8368 const char *
8369 arithmetic_instr (rtx op, int shift_first_arg)
8371 switch (GET_CODE (op))
8373 case PLUS:
8374 return "add";
8376 case MINUS:
8377 return shift_first_arg ? "rsb" : "sub";
8379 case IOR:
8380 return "orr";
8382 case XOR:
8383 return "eor";
8385 case AND:
8386 return "and";
8388 default:
8389 abort ();
8393 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8394 for the operation code. The returned result should not be overwritten.
8395 OP is the rtx code of the shift.
8396 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8397 shift. */
8398 static const char *
8399 shift_op (rtx op, HOST_WIDE_INT *amountp)
8401 const char * mnem;
8402 enum rtx_code code = GET_CODE (op);
8404 if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
8405 *amountp = -1;
8406 else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
8407 *amountp = INTVAL (XEXP (op, 1));
8408 else
8409 abort ();
8411 switch (code)
8413 case ASHIFT:
8414 mnem = "asl";
8415 break;
8417 case ASHIFTRT:
8418 mnem = "asr";
8419 break;
8421 case LSHIFTRT:
8422 mnem = "lsr";
8423 break;
8425 case ROTATE:
8426 if (*amountp == -1)
8427 abort ();
8428 *amountp = 32 - *amountp;
8430 /* Fall through. */
8432 case ROTATERT:
8433 mnem = "ror";
8434 break;
8436 case MULT:
8437 /* We never have to worry about the amount being other than a
8438 power of 2, since this case can never be reloaded from a reg. */
8439 if (*amountp != -1)
8440 *amountp = int_log2 (*amountp);
8441 else
8442 abort ();
8443 return "asl";
8445 default:
8446 abort ();
8449 if (*amountp != -1)
8451 /* This is not 100% correct, but follows from the desire to merge
8452 multiplication by a power of 2 with the recognizer for a
8453 shift. >=32 is not a valid shift for "asl", so we must try and
8454 output a shift that produces the correct arithmetical result.
8455 Using lsr #32 is identical except for the fact that the carry bit
8456 is not set correctly if we set the flags; but we never use the
8457 carry bit from such an operation, so we can ignore that. */
8458 if (code == ROTATERT)
8459 /* Rotate is just modulo 32. */
8460 *amountp &= 31;
8461 else if (*amountp != (*amountp & 31))
8463 if (code == ASHIFT)
8464 mnem = "lsr";
8465 *amountp = 32;
8468 /* Shifts of 0 are no-ops. */
8469 if (*amountp == 0)
8470 return NULL;
8473 return mnem;
8476 /* Obtain the shift from the POWER of two. */
8478 static HOST_WIDE_INT
8479 int_log2 (HOST_WIDE_INT power)
8481 HOST_WIDE_INT shift = 0;
8483 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8485 if (shift > 31)
8486 abort ();
8487 shift++;
8490 return shift;
8493 /* Output a .ascii pseudo-op, keeping track of lengths. This is because
8494 /bin/as is horribly restrictive. */
8495 #define MAX_ASCII_LEN 51
8497 void
8498 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8500 int i;
8501 int len_so_far = 0;
8503 fputs ("\t.ascii\t\"", stream);
8505 for (i = 0; i < len; i++)
8507 int c = p[i];
8509 if (len_so_far >= MAX_ASCII_LEN)
8511 fputs ("\"\n\t.ascii\t\"", stream);
8512 len_so_far = 0;
8515 switch (c)
8517 case TARGET_TAB:
8518 fputs ("\\t", stream);
8519 len_so_far += 2;
8520 break;
8522 case TARGET_FF:
8523 fputs ("\\f", stream);
8524 len_so_far += 2;
8525 break;
8527 case TARGET_BS:
8528 fputs ("\\b", stream);
8529 len_so_far += 2;
8530 break;
8532 case TARGET_CR:
8533 fputs ("\\r", stream);
8534 len_so_far += 2;
8535 break;
8537 case TARGET_NEWLINE:
8538 fputs ("\\n", stream);
8539 c = p [i + 1];
8540 if ((c >= ' ' && c <= '~')
8541 || c == TARGET_TAB)
8542 /* This is a good place for a line break. */
8543 len_so_far = MAX_ASCII_LEN;
8544 else
8545 len_so_far += 2;
8546 break;
8548 case '\"':
8549 case '\\':
8550 putc ('\\', stream);
8551 len_so_far++;
8552 /* Drop through. */
8554 default:
8555 if (c >= ' ' && c <= '~')
8557 putc (c, stream);
8558 len_so_far++;
8560 else
8562 fprintf (stream, "\\%03o", c);
8563 len_so_far += 4;
8565 break;
8569 fputs ("\"\n", stream);
8572 /* Compute the register save mask for registers 0 through 12
8573 inclusive. This code is used by arm_compute_save_reg_mask. */
8574 static unsigned long
8575 arm_compute_save_reg0_reg12_mask (void)
8577 unsigned long func_type = arm_current_func_type ();
8578 unsigned int save_reg_mask = 0;
8579 unsigned int reg;
8581 if (IS_INTERRUPT (func_type))
8583 unsigned int max_reg;
8584 /* Interrupt functions must not corrupt any registers,
8585 even call clobbered ones. If this is a leaf function
8586 we can just examine the registers used by the RTL, but
8587 otherwise we have to assume that whatever function is
8588 called might clobber anything, and so we have to save
8589 all the call-clobbered registers as well. */
8590 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8591 /* FIQ handlers have registers r8 - r12 banked, so
8592 we only need to check r0 - r7, Normal ISRs only
8593 bank r14 and r15, so we must check up to r12.
8594 r13 is the stack pointer which is always preserved,
8595 so we do not need to consider it here. */
8596 max_reg = 7;
8597 else
8598 max_reg = 12;
8600 for (reg = 0; reg <= max_reg; reg++)
8601 if (regs_ever_live[reg]
8602 || (! current_function_is_leaf && call_used_regs [reg]))
8603 save_reg_mask |= (1 << reg);
8605 else
8607 /* In the normal case we only need to save those registers
8608 which are call saved and which are used by this function. */
8609 for (reg = 0; reg <= 10; reg++)
8610 if (regs_ever_live[reg] && ! call_used_regs [reg])
8611 save_reg_mask |= (1 << reg);
8613 /* Handle the frame pointer as a special case. */
8614 if (! TARGET_APCS_FRAME
8615 && ! frame_pointer_needed
8616 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8617 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8618 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8620 /* If we aren't loading the PIC register,
8621 don't stack it even though it may be live. */
8622 if (flag_pic
8623 && ! TARGET_SINGLE_PIC_BASE
8624 && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
8625 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8628 /* Save registers so the exception handler can modify them. */
8629 if (current_function_calls_eh_return)
8631 unsigned int i;
8633 for (i = 0; ; i++)
8635 reg = EH_RETURN_DATA_REGNO (i);
8636 if (reg == INVALID_REGNUM)
8637 break;
8638 save_reg_mask |= 1 << reg;
8642 return save_reg_mask;
8645 /* Compute a bit mask of which registers need to be
8646 saved on the stack for the current function. */
8648 static unsigned long
8649 arm_compute_save_reg_mask (void)
8651 unsigned int save_reg_mask = 0;
8652 unsigned long func_type = arm_current_func_type ();
8654 if (IS_NAKED (func_type))
8655 /* This should never really happen. */
8656 return 0;
8658 /* If we are creating a stack frame, then we must save the frame pointer,
8659 IP (which will hold the old stack pointer), LR and the PC. */
8660 if (frame_pointer_needed)
8661 save_reg_mask |=
8662 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8663 | (1 << IP_REGNUM)
8664 | (1 << LR_REGNUM)
8665 | (1 << PC_REGNUM);
8667 /* Volatile functions do not return, so there
8668 is no need to save any other registers. */
8669 if (IS_VOLATILE (func_type))
8670 return save_reg_mask;
8672 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8674 /* Decide if we need to save the link register.
8675 Interrupt routines have their own banked link register,
8676 so they never need to save it.
8677 Otherwise if we do not use the link register we do not need to save
8678 it. If we are pushing other registers onto the stack however, we
8679 can save an instruction in the epilogue by pushing the link register
8680 now and then popping it back into the PC. This incurs extra memory
8681 accesses though, so we only do it when optimizing for size, and only
8682 if we know that we will not need a fancy return sequence. */
8683 if (regs_ever_live [LR_REGNUM]
8684 || (save_reg_mask
8685 && optimize_size
8686 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
8687 && !current_function_calls_eh_return))
8688 save_reg_mask |= 1 << LR_REGNUM;
8690 if (cfun->machine->lr_save_eliminated)
8691 save_reg_mask &= ~ (1 << LR_REGNUM);
8693 if (TARGET_REALLY_IWMMXT
8694 && ((bit_count (save_reg_mask)
8695 + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8697 unsigned int reg;
8699 /* The total number of registers that are going to be pushed
8700 onto the stack is odd. We need to ensure that the stack
8701 is 64-bit aligned before we start to save iWMMXt registers,
8702 and also before we start to create locals. (A local variable
8703 might be a double or long long which we will load/store using
8704 an iWMMXt instruction). Therefore we need to push another
8705 ARM register, so that the stack will be 64-bit aligned. We
8706 try to avoid using the arg registers (r0 -r3) as they might be
8707 used to pass values in a tail call. */
8708 for (reg = 4; reg <= 12; reg++)
8709 if ((save_reg_mask & (1 << reg)) == 0)
8710 break;
8712 if (reg <= 12)
8713 save_reg_mask |= (1 << reg);
8714 else
8716 cfun->machine->sibcall_blocked = 1;
8717 save_reg_mask |= (1 << 3);
8721 return save_reg_mask;
8725 /* Compute a bit mask of which registers need to be
8726 saved on the stack for the current function. */
8727 static unsigned long
8728 thumb_compute_save_reg_mask (void)
8730 unsigned long mask;
8731 int reg;
8733 mask = 0;
8734 for (reg = 0; reg < 12; reg ++)
8736 if (regs_ever_live[reg] && !call_used_regs[reg])
8737 mask |= 1 << reg;
8740 if (flag_pic && !TARGET_SINGLE_PIC_BASE)
8741 mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
8742 if (TARGET_SINGLE_PIC_BASE)
8743 mask &= ~(1 << arm_pic_register);
8745 /* lr will also be pushed if any lo regs are pushed. */
8746 if (mask & 0xff || thumb_force_lr_save ())
8747 mask |= (1 << LR_REGNUM);
8749 /* Make sure we have a low work register if we need one. */
8750 if (((mask & 0xff) == 0 && regs_ever_live[LAST_ARG_REGNUM])
8751 && ((mask & 0x0f00) || TARGET_BACKTRACE))
8752 mask |= 1 << LAST_LO_REGNUM;
8754 return mask;
8758 /* Return the number of bytes required to save VFP registers. */
8759 static int
8760 arm_get_vfp_saved_size (void)
8762 unsigned int regno;
8763 int count;
8764 int saved;
8766 saved = 0;
8767 /* Space for saved VFP registers. */
8768 if (TARGET_HARD_FLOAT && TARGET_VFP)
8770 count = 0;
8771 for (regno = FIRST_VFP_REGNUM;
8772 regno < LAST_VFP_REGNUM;
8773 regno += 2)
8775 if ((!regs_ever_live[regno] || call_used_regs[regno])
8776 && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8778 if (count > 0)
8780 /* Workaround ARM10 VFPr1 bug. */
8781 if (count == 2 && !arm_arch6)
8782 count++;
8783 saved += count * 8 + 4;
8785 count = 0;
8787 else
8788 count++;
8790 if (count > 0)
8792 if (count == 2 && !arm_arch6)
8793 count++;
8794 saved += count * 8 + 4;
8797 return saved;
8801 /* Generate a function exit sequence. If REALLY_RETURN is false, then do
8802 everything bar the final return instruction. */
8803 const char *
8804 output_return_instruction (rtx operand, int really_return, int reverse)
8806 char conditional[10];
8807 char instr[100];
8808 int reg;
8809 unsigned long live_regs_mask;
8810 unsigned long func_type;
8811 arm_stack_offsets *offsets;
8813 func_type = arm_current_func_type ();
8815 if (IS_NAKED (func_type))
8816 return "";
8818 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
8820 /* If this function was declared non-returning, and we have
8821 found a tail call, then we have to trust that the called
8822 function won't return. */
8823 if (really_return)
8825 rtx ops[2];
8827 /* Otherwise, trap an attempted return by aborting. */
8828 ops[0] = operand;
8829 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
8830 : "abort");
8831 assemble_external_libcall (ops[1]);
8832 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
8835 return "";
8838 if (current_function_calls_alloca && !really_return)
8839 abort ();
8841 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
8843 return_used_this_function = 1;
8845 live_regs_mask = arm_compute_save_reg_mask ();
8847 if (live_regs_mask)
8849 const char * return_reg;
8851 /* If we do not have any special requirements for function exit
8852 (e.g. interworking, or ISR) then we can load the return address
8853 directly into the PC. Otherwise we must load it into LR. */
8854 if (really_return
8855 && ! TARGET_INTERWORK)
8856 return_reg = reg_names[PC_REGNUM];
8857 else
8858 return_reg = reg_names[LR_REGNUM];
8860 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
8862 /* There are three possible reasons for the IP register
8863 being saved. 1) a stack frame was created, in which case
8864 IP contains the old stack pointer, or 2) an ISR routine
8865 corrupted it, or 3) it was saved to align the stack on
8866 iWMMXt. In case 1, restore IP into SP, otherwise just
8867 restore IP. */
8868 if (frame_pointer_needed)
8870 live_regs_mask &= ~ (1 << IP_REGNUM);
8871 live_regs_mask |= (1 << SP_REGNUM);
8873 else
8875 if (! IS_INTERRUPT (func_type)
8876 && ! TARGET_REALLY_IWMMXT)
8877 abort ();
8881 /* On some ARM architectures it is faster to use LDR rather than
8882 LDM to load a single register. On other architectures, the
8883 cost is the same. In 26 bit mode, or for exception handlers,
8884 we have to use LDM to load the PC so that the CPSR is also
8885 restored. */
8886 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
8888 if (live_regs_mask == (unsigned int)(1 << reg))
8889 break;
8891 if (reg <= LAST_ARM_REGNUM
8892 && (reg != LR_REGNUM
8893 || ! really_return
8894 || ! IS_INTERRUPT (func_type)))
8896 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
8897 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
8899 else
8901 char *p;
8902 int first = 1;
8904 /* Generate the load multiple instruction to restore the
8905 registers. Note we can get here, even if
8906 frame_pointer_needed is true, but only if sp already
8907 points to the base of the saved core registers. */
8908 if (live_regs_mask & (1 << SP_REGNUM))
8910 unsigned HOST_WIDE_INT stack_adjust;
8912 offsets = arm_get_frame_offsets ();
8913 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
8914 if (stack_adjust != 0 && stack_adjust != 4)
8915 abort ();
8917 if (stack_adjust && arm_arch5)
8918 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
8919 else
8921 /* If we can't use ldmib (SA110 bug), then try to pop r3
8922 instead. */
8923 if (stack_adjust)
8924 live_regs_mask |= 1 << 3;
8925 sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
8928 else
8929 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
8931 p = instr + strlen (instr);
8933 for (reg = 0; reg <= SP_REGNUM; reg++)
8934 if (live_regs_mask & (1 << reg))
8936 int l = strlen (reg_names[reg]);
8938 if (first)
8939 first = 0;
8940 else
8942 memcpy (p, ", ", 2);
8943 p += 2;
8946 memcpy (p, "%|", 2);
8947 memcpy (p + 2, reg_names[reg], l);
8948 p += l + 2;
8951 if (live_regs_mask & (1 << LR_REGNUM))
8953 sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
8954 /* If returning from an interrupt, restore the CPSR. */
8955 if (IS_INTERRUPT (func_type))
8956 strcat (p, "^");
8958 else
8959 strcpy (p, "}");
8962 output_asm_insn (instr, & operand);
8964 /* See if we need to generate an extra instruction to
8965 perform the actual function return. */
8966 if (really_return
8967 && func_type != ARM_FT_INTERWORKED
8968 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
8970 /* The return has already been handled
8971 by loading the LR into the PC. */
8972 really_return = 0;
8976 if (really_return)
8978 switch ((int) ARM_FUNC_TYPE (func_type))
8980 case ARM_FT_ISR:
8981 case ARM_FT_FIQ:
8982 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
8983 break;
8985 case ARM_FT_INTERWORKED:
8986 sprintf (instr, "bx%s\t%%|lr", conditional);
8987 break;
8989 case ARM_FT_EXCEPTION:
8990 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
8991 break;
8993 default:
8994 /* Use bx if it's available. */
8995 if (arm_arch5 || arm_arch4t)
8996 sprintf (instr, "bx%s\t%%|lr", conditional);
8997 else
8998 sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
8999 break;
9002 output_asm_insn (instr, & operand);
9005 return "";
9008 /* Write the function name into the code section, directly preceding
9009 the function prologue.
9011 Code will be output similar to this:
9013 .ascii "arm_poke_function_name", 0
9014 .align
9016 .word 0xff000000 + (t1 - t0)
9017 arm_poke_function_name
9018 mov ip, sp
9019 stmfd sp!, {fp, ip, lr, pc}
9020 sub fp, ip, #4
9022 When performing a stack backtrace, code can inspect the value
9023 of 'pc' stored at 'fp' + 0. If the trace function then looks
9024 at location pc - 12 and the top 8 bits are set, then we know
9025 that there is a function name embedded immediately preceding this
9026 location and has length ((pc[-3]) & 0xff000000).
9028 We assume that pc is declared as a pointer to an unsigned long.
9030 It is of no benefit to output the function name if we are assembling
9031 a leaf function. These function types will not contain a stack
9032 backtrace structure, therefore it is not possible to determine the
9033 function name. */
9034 void
9035 arm_poke_function_name (FILE *stream, const char *name)
9037 unsigned long alignlength;
9038 unsigned long length;
9039 rtx x;
9041 length = strlen (name) + 1;
9042 alignlength = ROUND_UP_WORD (length);
9044 ASM_OUTPUT_ASCII (stream, name, length);
9045 ASM_OUTPUT_ALIGN (stream, 2);
9046 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9047 assemble_aligned_integer (UNITS_PER_WORD, x);
9050 /* Place some comments into the assembler stream
9051 describing the current function. */
9052 static void
9053 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9055 unsigned long func_type;
9057 if (!TARGET_ARM)
9059 thumb_output_function_prologue (f, frame_size);
9060 return;
9063 /* Sanity check. */
9064 if (arm_ccfsm_state || arm_target_insn)
9065 abort ();
9067 func_type = arm_current_func_type ();
9069 switch ((int) ARM_FUNC_TYPE (func_type))
9071 default:
9072 case ARM_FT_NORMAL:
9073 break;
9074 case ARM_FT_INTERWORKED:
9075 asm_fprintf (f, "\t%@ Function supports interworking.\n");
9076 break;
9077 case ARM_FT_ISR:
9078 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9079 break;
9080 case ARM_FT_FIQ:
9081 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9082 break;
9083 case ARM_FT_EXCEPTION:
9084 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9085 break;
9088 if (IS_NAKED (func_type))
9089 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9091 if (IS_VOLATILE (func_type))
9092 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9094 if (IS_NESTED (func_type))
9095 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9097 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9098 current_function_args_size,
9099 current_function_pretend_args_size, frame_size);
9101 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9102 frame_pointer_needed,
9103 cfun->machine->uses_anonymous_args);
9105 if (cfun->machine->lr_save_eliminated)
9106 asm_fprintf (f, "\t%@ link register save eliminated.\n");
9108 if (current_function_calls_eh_return)
9109 asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9111 #ifdef AOF_ASSEMBLER
9112 if (flag_pic)
9113 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9114 #endif
9116 return_used_this_function = 0;
9119 const char *
9120 arm_output_epilogue (rtx sibling)
9122 int reg;
9123 unsigned long saved_regs_mask;
9124 unsigned long func_type;
9125 /* Floats_offset is the offset from the "virtual" frame. In an APCS
9126 frame that is $fp + 4 for a non-variadic function. */
9127 int floats_offset = 0;
9128 rtx operands[3];
9129 FILE * f = asm_out_file;
9130 unsigned int lrm_count = 0;
9131 int really_return = (sibling == NULL);
9132 int start_reg;
9133 arm_stack_offsets *offsets;
9135 /* If we have already generated the return instruction
9136 then it is futile to generate anything else. */
9137 if (use_return_insn (FALSE, sibling) && return_used_this_function)
9138 return "";
9140 func_type = arm_current_func_type ();
9142 if (IS_NAKED (func_type))
9143 /* Naked functions don't have epilogues. */
9144 return "";
9146 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9148 rtx op;
9150 /* A volatile function should never return. Call abort. */
9151 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9152 assemble_external_libcall (op);
9153 output_asm_insn ("bl\t%a0", &op);
9155 return "";
9158 if (current_function_calls_eh_return
9159 && ! really_return)
9160 /* If we are throwing an exception, then we really must
9161 be doing a return, so we can't tail-call. */
9162 abort ();
9164 offsets = arm_get_frame_offsets ();
9165 saved_regs_mask = arm_compute_save_reg_mask ();
9167 if (TARGET_IWMMXT)
9168 lrm_count = bit_count (saved_regs_mask);
9170 floats_offset = offsets->saved_args;
9171 /* Compute how far away the floats will be. */
9172 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9173 if (saved_regs_mask & (1 << reg))
9174 floats_offset += 4;
9176 if (frame_pointer_needed)
9178 /* This variable is for the Virtual Frame Pointer, not VFP regs. */
9179 int vfp_offset = offsets->frame;
9181 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9183 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9184 if (regs_ever_live[reg] && !call_used_regs[reg])
9186 floats_offset += 12;
9187 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9188 reg, FP_REGNUM, floats_offset - vfp_offset);
9191 else
9193 start_reg = LAST_FPA_REGNUM;
9195 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9197 if (regs_ever_live[reg] && !call_used_regs[reg])
9199 floats_offset += 12;
9201 /* We can't unstack more than four registers at once. */
9202 if (start_reg - reg == 3)
9204 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9205 reg, FP_REGNUM, floats_offset - vfp_offset);
9206 start_reg = reg - 1;
9209 else
9211 if (reg != start_reg)
9212 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9213 reg + 1, start_reg - reg,
9214 FP_REGNUM, floats_offset - vfp_offset);
9215 start_reg = reg - 1;
9219 /* Just in case the last register checked also needs unstacking. */
9220 if (reg != start_reg)
9221 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9222 reg + 1, start_reg - reg,
9223 FP_REGNUM, floats_offset - vfp_offset);
9226 if (TARGET_HARD_FLOAT && TARGET_VFP)
9228 int saved_size;
9230 /* The fldmx insn does not have base+offset addressing modes,
9231 so we use IP to hold the address. */
9232 saved_size = arm_get_vfp_saved_size ();
9234 if (saved_size > 0)
9236 floats_offset += saved_size;
9237 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9238 FP_REGNUM, floats_offset - vfp_offset);
9240 start_reg = FIRST_VFP_REGNUM;
9241 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9243 if ((!regs_ever_live[reg] || call_used_regs[reg])
9244 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9246 if (start_reg != reg)
9247 arm_output_fldmx (f, IP_REGNUM,
9248 (start_reg - FIRST_VFP_REGNUM) / 2,
9249 (reg - start_reg) / 2);
9250 start_reg = reg + 2;
9253 if (start_reg != reg)
9254 arm_output_fldmx (f, IP_REGNUM,
9255 (start_reg - FIRST_VFP_REGNUM) / 2,
9256 (reg - start_reg) / 2);
9259 if (TARGET_IWMMXT)
9261 /* The frame pointer is guaranteed to be non-double-word aligned.
9262 This is because it is set to (old_stack_pointer - 4) and the
9263 old_stack_pointer was double word aligned. Thus the offset to
9264 the iWMMXt registers to be loaded must also be non-double-word
9265 sized, so that the resultant address *is* double-word aligned.
9266 We can ignore floats_offset since that was already included in
9267 the live_regs_mask. */
9268 lrm_count += (lrm_count % 2 ? 2 : 1);
9270 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9271 if (regs_ever_live[reg] && !call_used_regs[reg])
9273 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9274 reg, FP_REGNUM, lrm_count * 4);
9275 lrm_count += 2;
9279 /* saved_regs_mask should contain the IP, which at the time of stack
9280 frame generation actually contains the old stack pointer. So a
9281 quick way to unwind the stack is just pop the IP register directly
9282 into the stack pointer. */
9283 if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
9284 abort ();
9285 saved_regs_mask &= ~ (1 << IP_REGNUM);
9286 saved_regs_mask |= (1 << SP_REGNUM);
9288 /* There are two registers left in saved_regs_mask - LR and PC. We
9289 only need to restore the LR register (the return address), but to
9290 save time we can load it directly into the PC, unless we need a
9291 special function exit sequence, or we are not really returning. */
9292 if (really_return
9293 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9294 && !current_function_calls_eh_return)
9295 /* Delete the LR from the register mask, so that the LR on
9296 the stack is loaded into the PC in the register mask. */
9297 saved_regs_mask &= ~ (1 << LR_REGNUM);
9298 else
9299 saved_regs_mask &= ~ (1 << PC_REGNUM);
9301 /* We must use SP as the base register, because SP is one of the
9302 registers being restored. If an interrupt or page fault
9303 happens in the ldm instruction, the SP might or might not
9304 have been restored. That would be bad, as then SP will no
9305 longer indicate the safe area of stack, and we can get stack
9306 corruption. Using SP as the base register means that it will
9307 be reset correctly to the original value, should an interrupt
9308 occur. If the stack pointer already points at the right
9309 place, then omit the subtraction. */
9310 if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9311 || current_function_calls_alloca)
9312 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9313 4 * bit_count (saved_regs_mask));
9314 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9316 if (IS_INTERRUPT (func_type))
9317 /* Interrupt handlers will have pushed the
9318 IP onto the stack, so restore it now. */
9319 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9321 else
9323 /* Restore stack pointer if necessary. */
9324 if (offsets->outgoing_args != offsets->saved_regs)
9326 operands[0] = operands[1] = stack_pointer_rtx;
9327 operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9328 output_add_immediate (operands);
9331 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9333 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9334 if (regs_ever_live[reg] && !call_used_regs[reg])
9335 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9336 reg, SP_REGNUM);
9338 else
9340 start_reg = FIRST_FPA_REGNUM;
9342 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9344 if (regs_ever_live[reg] && !call_used_regs[reg])
9346 if (reg - start_reg == 3)
9348 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9349 start_reg, SP_REGNUM);
9350 start_reg = reg + 1;
9353 else
9355 if (reg != start_reg)
9356 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9357 start_reg, reg - start_reg,
9358 SP_REGNUM);
9360 start_reg = reg + 1;
9364 /* Just in case the last register checked also needs unstacking. */
9365 if (reg != start_reg)
9366 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9367 start_reg, reg - start_reg, SP_REGNUM);
9370 if (TARGET_HARD_FLOAT && TARGET_VFP)
9372 start_reg = FIRST_VFP_REGNUM;
9373 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9375 if ((!regs_ever_live[reg] || call_used_regs[reg])
9376 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9378 if (start_reg != reg)
9379 arm_output_fldmx (f, SP_REGNUM,
9380 (start_reg - FIRST_VFP_REGNUM) / 2,
9381 (reg - start_reg) / 2);
9382 start_reg = reg + 2;
9385 if (start_reg != reg)
9386 arm_output_fldmx (f, SP_REGNUM,
9387 (start_reg - FIRST_VFP_REGNUM) / 2,
9388 (reg - start_reg) / 2);
9390 if (TARGET_IWMMXT)
9391 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9392 if (regs_ever_live[reg] && !call_used_regs[reg])
9393 asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9395 /* If we can, restore the LR into the PC. */
9396 if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9397 && really_return
9398 && current_function_pretend_args_size == 0
9399 && saved_regs_mask & (1 << LR_REGNUM)
9400 && !current_function_calls_eh_return)
9402 saved_regs_mask &= ~ (1 << LR_REGNUM);
9403 saved_regs_mask |= (1 << PC_REGNUM);
9406 /* Load the registers off the stack. If we only have one register
9407 to load use the LDR instruction - it is faster. */
9408 if (saved_regs_mask == (1 << LR_REGNUM))
9410 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9412 else if (saved_regs_mask)
9414 if (saved_regs_mask & (1 << SP_REGNUM))
9415 /* Note - write back to the stack register is not enabled
9416 (i.e. "ldmfd sp!..."). We know that the stack pointer is
9417 in the list of registers and if we add writeback the
9418 instruction becomes UNPREDICTABLE. */
9419 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9420 else
9421 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9424 if (current_function_pretend_args_size)
9426 /* Unwind the pre-pushed regs. */
9427 operands[0] = operands[1] = stack_pointer_rtx;
9428 operands[2] = GEN_INT (current_function_pretend_args_size);
9429 output_add_immediate (operands);
9433 /* We may have already restored PC directly from the stack. */
9434 if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
9435 return "";
9437 /* Stack adjustment for exception handler. */
9438 if (current_function_calls_eh_return)
9439 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
9440 ARM_EH_STACKADJ_REGNUM);
9442 /* Generate the return instruction. */
9443 switch ((int) ARM_FUNC_TYPE (func_type))
9445 case ARM_FT_ISR:
9446 case ARM_FT_FIQ:
9447 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9448 break;
9450 case ARM_FT_EXCEPTION:
9451 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9452 break;
9454 case ARM_FT_INTERWORKED:
9455 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9456 break;
9458 default:
9459 if (arm_arch5 || arm_arch4t)
9460 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9461 else
9462 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9463 break;
9466 return "";
9469 static void
9470 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9471 HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9473 arm_stack_offsets *offsets;
9475 if (TARGET_THUMB)
9477 /* ??? Probably not safe to set this here, since it assumes that a
9478 function will be emitted as assembly immediately after we generate
9479 RTL for it. This does not happen for inline functions. */
9480 return_used_this_function = 0;
9482 else
9484 /* We need to take into account any stack-frame rounding. */
9485 offsets = arm_get_frame_offsets ();
9487 if (use_return_insn (FALSE, NULL)
9488 && return_used_this_function
9489 && offsets->saved_regs != offsets->outgoing_args
9490 && !frame_pointer_needed)
9491 abort ();
9493 /* Reset the ARM-specific per-function variables. */
9494 after_arm_reorg = 0;
9498 /* Generate and emit an insn that we will recognize as a push_multi.
9499 Unfortunately, since this insn does not reflect very well the actual
9500 semantics of the operation, we need to annotate the insn for the benefit
9501 of DWARF2 frame unwind information. */
9502 static rtx
9503 emit_multi_reg_push (int mask)
9505 int num_regs = 0;
9506 int num_dwarf_regs;
9507 int i, j;
9508 rtx par;
9509 rtx dwarf;
9510 int dwarf_par_index;
9511 rtx tmp, reg;
9513 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9514 if (mask & (1 << i))
9515 num_regs++;
9517 if (num_regs == 0 || num_regs > 16)
9518 abort ();
9520 /* We don't record the PC in the dwarf frame information. */
9521 num_dwarf_regs = num_regs;
9522 if (mask & (1 << PC_REGNUM))
9523 num_dwarf_regs--;
9525 /* For the body of the insn we are going to generate an UNSPEC in
9526 parallel with several USEs. This allows the insn to be recognized
9527 by the push_multi pattern in the arm.md file. The insn looks
9528 something like this:
9530 (parallel [
9531 (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9532 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9533 (use (reg:SI 11 fp))
9534 (use (reg:SI 12 ip))
9535 (use (reg:SI 14 lr))
9536 (use (reg:SI 15 pc))
9539 For the frame note however, we try to be more explicit and actually
9540 show each register being stored into the stack frame, plus a (single)
9541 decrement of the stack pointer. We do it this way in order to be
9542 friendly to the stack unwinding code, which only wants to see a single
9543 stack decrement per instruction. The RTL we generate for the note looks
9544 something like this:
9546 (sequence [
9547 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9548 (set (mem:SI (reg:SI sp)) (reg:SI r4))
9549 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9550 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9551 (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9554 This sequence is used both by the code to support stack unwinding for
9555 exceptions handlers and the code to generate dwarf2 frame debugging. */
9557 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9558 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9559 dwarf_par_index = 1;
9561 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9563 if (mask & (1 << i))
9565 reg = gen_rtx_REG (SImode, i);
9567 XVECEXP (par, 0, 0)
9568 = gen_rtx_SET (VOIDmode,
9569 gen_rtx_MEM (BLKmode,
9570 gen_rtx_PRE_DEC (BLKmode,
9571 stack_pointer_rtx)),
9572 gen_rtx_UNSPEC (BLKmode,
9573 gen_rtvec (1, reg),
9574 UNSPEC_PUSH_MULT));
9576 if (i != PC_REGNUM)
9578 tmp = gen_rtx_SET (VOIDmode,
9579 gen_rtx_MEM (SImode, stack_pointer_rtx),
9580 reg);
9581 RTX_FRAME_RELATED_P (tmp) = 1;
9582 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9583 dwarf_par_index++;
9586 break;
9590 for (j = 1, i++; j < num_regs; i++)
9592 if (mask & (1 << i))
9594 reg = gen_rtx_REG (SImode, i);
9596 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9598 if (i != PC_REGNUM)
9600 tmp = gen_rtx_SET (VOIDmode,
9601 gen_rtx_MEM (SImode,
9602 plus_constant (stack_pointer_rtx,
9603 4 * j)),
9604 reg);
9605 RTX_FRAME_RELATED_P (tmp) = 1;
9606 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9609 j++;
9613 par = emit_insn (par);
9615 tmp = gen_rtx_SET (SImode,
9616 stack_pointer_rtx,
9617 gen_rtx_PLUS (SImode,
9618 stack_pointer_rtx,
9619 GEN_INT (-4 * num_regs)));
9620 RTX_FRAME_RELATED_P (tmp) = 1;
9621 XVECEXP (dwarf, 0, 0) = tmp;
9623 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9624 REG_NOTES (par));
9625 return par;
9628 static rtx
9629 emit_sfm (int base_reg, int count)
9631 rtx par;
9632 rtx dwarf;
9633 rtx tmp, reg;
9634 int i;
9636 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9637 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9639 reg = gen_rtx_REG (XFmode, base_reg++);
9641 XVECEXP (par, 0, 0)
9642 = gen_rtx_SET (VOIDmode,
9643 gen_rtx_MEM (BLKmode,
9644 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9645 gen_rtx_UNSPEC (BLKmode,
9646 gen_rtvec (1, reg),
9647 UNSPEC_PUSH_MULT));
9648 tmp = gen_rtx_SET (VOIDmode,
9649 gen_rtx_MEM (XFmode, stack_pointer_rtx), reg);
9650 RTX_FRAME_RELATED_P (tmp) = 1;
9651 XVECEXP (dwarf, 0, 1) = tmp;
9653 for (i = 1; i < count; i++)
9655 reg = gen_rtx_REG (XFmode, base_reg++);
9656 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9658 tmp = gen_rtx_SET (VOIDmode,
9659 gen_rtx_MEM (XFmode,
9660 plus_constant (stack_pointer_rtx,
9661 i * 12)),
9662 reg);
9663 RTX_FRAME_RELATED_P (tmp) = 1;
9664 XVECEXP (dwarf, 0, i + 1) = tmp;
9667 tmp = gen_rtx_SET (VOIDmode,
9668 stack_pointer_rtx,
9669 gen_rtx_PLUS (SImode,
9670 stack_pointer_rtx,
9671 GEN_INT (-12 * count)));
9672 RTX_FRAME_RELATED_P (tmp) = 1;
9673 XVECEXP (dwarf, 0, 0) = tmp;
9675 par = emit_insn (par);
9676 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9677 REG_NOTES (par));
9678 return par;
9682 /* Return true if the current function needs to save/restore LR. */
9684 static bool
9685 thumb_force_lr_save (void)
9687 return !cfun->machine->lr_save_eliminated
9688 && (!leaf_function_p ()
9689 || thumb_far_jump_used_p ()
9690 || regs_ever_live [LR_REGNUM]);
9694 /* Compute the distance from register FROM to register TO.
9695 These can be the arg pointer (26), the soft frame pointer (25),
9696 the stack pointer (13) or the hard frame pointer (11).
9697 In thumb mode r7 is used as the soft frame pointer, if needed.
9698 Typical stack layout looks like this:
9700 old stack pointer -> | |
9701 ----
9702 | | \
9703 | | saved arguments for
9704 | | vararg functions
9705 | | /
9707 hard FP & arg pointer -> | | \
9708 | | stack
9709 | | frame
9710 | | /
9712 | | \
9713 | | call saved
9714 | | registers
9715 soft frame pointer -> | | /
9717 | | \
9718 | | local
9719 | | variables
9720 | | /
9722 | | \
9723 | | outgoing
9724 | | arguments
9725 current stack pointer -> | | /
9728 For a given function some or all of these stack components
9729 may not be needed, giving rise to the possibility of
9730 eliminating some of the registers.
9732 The values returned by this function must reflect the behavior
9733 of arm_expand_prologue() and arm_compute_save_reg_mask().
9735 The sign of the number returned reflects the direction of stack
9736 growth, so the values are positive for all eliminations except
9737 from the soft frame pointer to the hard frame pointer.
9739 SFP may point just inside the local variables block to ensure correct
9740 alignment. */
9743 /* Calculate stack offsets. These are used to calculate register elimination
9744 offsets and in prologue/epilogue code. */
9746 static arm_stack_offsets *
9747 arm_get_frame_offsets (void)
9749 struct arm_stack_offsets *offsets;
9750 unsigned long func_type;
9751 int leaf;
9752 int saved;
9753 HOST_WIDE_INT frame_size;
9755 offsets = &cfun->machine->stack_offsets;
9757 /* We need to know if we are a leaf function. Unfortunately, it
9758 is possible to be called after start_sequence has been called,
9759 which causes get_insns to return the insns for the sequence,
9760 not the function, which will cause leaf_function_p to return
9761 the incorrect result.
9763 to know about leaf functions once reload has completed, and the
9764 frame size cannot be changed after that time, so we can safely
9765 use the cached value. */
9767 if (reload_completed)
9768 return offsets;
9770 /* Initially this is the size of the local variables. It will translated
9771 into an offset once we have determined the size of preceding data. */
9772 frame_size = ROUND_UP_WORD (get_frame_size ());
9774 leaf = leaf_function_p ();
9776 /* Space for variadic functions. */
9777 offsets->saved_args = current_function_pretend_args_size;
9779 offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
9781 if (TARGET_ARM)
9783 unsigned int regno;
9785 saved = bit_count (arm_compute_save_reg_mask ()) * 4;
9787 /* We know that SP will be doubleword aligned on entry, and we must
9788 preserve that condition at any subroutine call. We also require the
9789 soft frame pointer to be doubleword aligned. */
9791 if (TARGET_REALLY_IWMMXT)
9793 /* Check for the call-saved iWMMXt registers. */
9794 for (regno = FIRST_IWMMXT_REGNUM;
9795 regno <= LAST_IWMMXT_REGNUM;
9796 regno++)
9797 if (regs_ever_live [regno] && ! call_used_regs [regno])
9798 saved += 8;
9801 func_type = arm_current_func_type ();
9802 if (! IS_VOLATILE (func_type))
9804 /* Space for saved FPA registers. */
9805 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
9806 if (regs_ever_live[regno] && ! call_used_regs[regno])
9807 saved += 12;
9809 /* Space for saved VFP registers. */
9810 if (TARGET_HARD_FLOAT && TARGET_VFP)
9811 saved += arm_get_vfp_saved_size ();
9814 else /* TARGET_THUMB */
9816 saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
9817 if (TARGET_BACKTRACE)
9818 saved += 16;
9821 /* Saved registers include the stack frame. */
9822 offsets->saved_regs = offsets->saved_args + saved;
9823 offsets->soft_frame = offsets->saved_regs;
9824 /* A leaf function does not need any stack alignment if it has nothing
9825 on the stack. */
9826 if (leaf && frame_size == 0)
9828 offsets->outgoing_args = offsets->soft_frame;
9829 return offsets;
9832 /* Ensure SFP has the correct alignment. */
9833 if (ARM_DOUBLEWORD_ALIGN
9834 && (offsets->soft_frame & 7))
9835 offsets->soft_frame += 4;
9837 offsets->outgoing_args = offsets->soft_frame + frame_size
9838 + current_function_outgoing_args_size;
9840 if (ARM_DOUBLEWORD_ALIGN)
9842 /* Ensure SP remains doubleword aligned. */
9843 if (offsets->outgoing_args & 7)
9844 offsets->outgoing_args += 4;
9845 if (offsets->outgoing_args & 7)
9846 abort ();
9849 return offsets;
9853 /* Calculate the relative offsets for the different stack pointers. Positive
9854 offsets are in the direction of stack growth. */
9856 HOST_WIDE_INT
9857 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
9859 arm_stack_offsets *offsets;
9861 offsets = arm_get_frame_offsets ();
9863 /* OK, now we have enough information to compute the distances.
9864 There must be an entry in these switch tables for each pair
9865 of registers in ELIMINABLE_REGS, even if some of the entries
9866 seem to be redundant or useless. */
9867 switch (from)
9869 case ARG_POINTER_REGNUM:
9870 switch (to)
9872 case THUMB_HARD_FRAME_POINTER_REGNUM:
9873 return 0;
9875 case FRAME_POINTER_REGNUM:
9876 /* This is the reverse of the soft frame pointer
9877 to hard frame pointer elimination below. */
9878 return offsets->soft_frame - offsets->saved_args;
9880 case ARM_HARD_FRAME_POINTER_REGNUM:
9881 /* If there is no stack frame then the hard
9882 frame pointer and the arg pointer coincide. */
9883 if (offsets->frame == offsets->saved_regs)
9884 return 0;
9885 /* FIXME: Not sure about this. Maybe we should always return 0 ? */
9886 return (frame_pointer_needed
9887 && cfun->static_chain_decl != NULL
9888 && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
9890 case STACK_POINTER_REGNUM:
9891 /* If nothing has been pushed on the stack at all
9892 then this will return -4. This *is* correct! */
9893 return offsets->outgoing_args - (offsets->saved_args + 4);
9895 default:
9896 abort ();
9898 break;
9900 case FRAME_POINTER_REGNUM:
9901 switch (to)
9903 case THUMB_HARD_FRAME_POINTER_REGNUM:
9904 return 0;
9906 case ARM_HARD_FRAME_POINTER_REGNUM:
9907 /* The hard frame pointer points to the top entry in the
9908 stack frame. The soft frame pointer to the bottom entry
9909 in the stack frame. If there is no stack frame at all,
9910 then they are identical. */
9912 return offsets->frame - offsets->soft_frame;
9914 case STACK_POINTER_REGNUM:
9915 return offsets->outgoing_args - offsets->soft_frame;
9917 default:
9918 abort ();
9920 break;
9922 default:
9923 /* You cannot eliminate from the stack pointer.
9924 In theory you could eliminate from the hard frame
9925 pointer to the stack pointer, but this will never
9926 happen, since if a stack frame is not needed the
9927 hard frame pointer will never be used. */
9928 abort ();
9933 /* Generate the prologue instructions for entry into an ARM function. */
9934 void
9935 arm_expand_prologue (void)
9937 int reg;
9938 rtx amount;
9939 rtx insn;
9940 rtx ip_rtx;
9941 unsigned long live_regs_mask;
9942 unsigned long func_type;
9943 int fp_offset = 0;
9944 int saved_pretend_args = 0;
9945 int saved_regs = 0;
9946 unsigned HOST_WIDE_INT args_to_push;
9947 arm_stack_offsets *offsets;
9949 func_type = arm_current_func_type ();
9951 /* Naked functions don't have prologues. */
9952 if (IS_NAKED (func_type))
9953 return;
9955 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
9956 args_to_push = current_function_pretend_args_size;
9958 /* Compute which register we will have to save onto the stack. */
9959 live_regs_mask = arm_compute_save_reg_mask ();
9961 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
9963 if (frame_pointer_needed)
9965 if (IS_INTERRUPT (func_type))
9967 /* Interrupt functions must not corrupt any registers.
9968 Creating a frame pointer however, corrupts the IP
9969 register, so we must push it first. */
9970 insn = emit_multi_reg_push (1 << IP_REGNUM);
9972 /* Do not set RTX_FRAME_RELATED_P on this insn.
9973 The dwarf stack unwinding code only wants to see one
9974 stack decrement per function, and this is not it. If
9975 this instruction is labeled as being part of the frame
9976 creation sequence then dwarf2out_frame_debug_expr will
9977 abort when it encounters the assignment of IP to FP
9978 later on, since the use of SP here establishes SP as
9979 the CFA register and not IP.
9981 Anyway this instruction is not really part of the stack
9982 frame creation although it is part of the prologue. */
9984 else if (IS_NESTED (func_type))
9986 /* The Static chain register is the same as the IP register
9987 used as a scratch register during stack frame creation.
9988 To get around this need to find somewhere to store IP
9989 whilst the frame is being created. We try the following
9990 places in order:
9992 1. The last argument register.
9993 2. A slot on the stack above the frame. (This only
9994 works if the function is not a varargs function).
9995 3. Register r3, after pushing the argument registers
9996 onto the stack.
9998 Note - we only need to tell the dwarf2 backend about the SP
9999 adjustment in the second variant; the static chain register
10000 doesn't need to be unwound, as it doesn't contain a value
10001 inherited from the caller. */
10003 if (regs_ever_live[3] == 0)
10005 insn = gen_rtx_REG (SImode, 3);
10006 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10007 insn = emit_insn (insn);
10009 else if (args_to_push == 0)
10011 rtx dwarf;
10012 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10013 insn = gen_rtx_MEM (SImode, insn);
10014 insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10015 insn = emit_insn (insn);
10017 fp_offset = 4;
10019 /* Just tell the dwarf backend that we adjusted SP. */
10020 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10021 gen_rtx_PLUS (SImode, stack_pointer_rtx,
10022 GEN_INT (-fp_offset)));
10023 RTX_FRAME_RELATED_P (insn) = 1;
10024 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10025 dwarf, REG_NOTES (insn));
10027 else
10029 /* Store the args on the stack. */
10030 if (cfun->machine->uses_anonymous_args)
10031 insn = emit_multi_reg_push
10032 ((0xf0 >> (args_to_push / 4)) & 0xf);
10033 else
10034 insn = emit_insn
10035 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10036 GEN_INT (- args_to_push)));
10038 RTX_FRAME_RELATED_P (insn) = 1;
10040 saved_pretend_args = 1;
10041 fp_offset = args_to_push;
10042 args_to_push = 0;
10044 /* Now reuse r3 to preserve IP. */
10045 insn = gen_rtx_REG (SImode, 3);
10046 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10047 (void) emit_insn (insn);
10051 if (fp_offset)
10053 insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10054 insn = gen_rtx_SET (SImode, ip_rtx, insn);
10056 else
10057 insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10059 insn = emit_insn (insn);
10060 RTX_FRAME_RELATED_P (insn) = 1;
10063 if (args_to_push)
10065 /* Push the argument registers, or reserve space for them. */
10066 if (cfun->machine->uses_anonymous_args)
10067 insn = emit_multi_reg_push
10068 ((0xf0 >> (args_to_push / 4)) & 0xf);
10069 else
10070 insn = emit_insn
10071 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10072 GEN_INT (- args_to_push)));
10073 RTX_FRAME_RELATED_P (insn) = 1;
10076 /* If this is an interrupt service routine, and the link register
10077 is going to be pushed, and we are not creating a stack frame,
10078 (which would involve an extra push of IP and a pop in the epilogue)
10079 subtracting four from LR now will mean that the function return
10080 can be done with a single instruction. */
10081 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10082 && (live_regs_mask & (1 << LR_REGNUM)) != 0
10083 && ! frame_pointer_needed)
10084 emit_insn (gen_rtx_SET (SImode,
10085 gen_rtx_REG (SImode, LR_REGNUM),
10086 gen_rtx_PLUS (SImode,
10087 gen_rtx_REG (SImode, LR_REGNUM),
10088 GEN_INT (-4))));
10090 if (live_regs_mask)
10092 insn = emit_multi_reg_push (live_regs_mask);
10093 saved_regs += bit_count (live_regs_mask) * 4;
10094 RTX_FRAME_RELATED_P (insn) = 1;
10097 if (TARGET_IWMMXT)
10098 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10099 if (regs_ever_live[reg] && ! call_used_regs [reg])
10101 insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10102 insn = gen_rtx_MEM (V2SImode, insn);
10103 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10104 gen_rtx_REG (V2SImode, reg)));
10105 RTX_FRAME_RELATED_P (insn) = 1;
10106 saved_regs += 8;
10109 if (! IS_VOLATILE (func_type))
10111 int start_reg;
10113 /* Save any floating point call-saved registers used by this
10114 function. */
10115 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10117 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10118 if (regs_ever_live[reg] && !call_used_regs[reg])
10120 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10121 insn = gen_rtx_MEM (XFmode, insn);
10122 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10123 gen_rtx_REG (XFmode, reg)));
10124 RTX_FRAME_RELATED_P (insn) = 1;
10125 saved_regs += 12;
10128 else
10130 start_reg = LAST_FPA_REGNUM;
10132 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10134 if (regs_ever_live[reg] && !call_used_regs[reg])
10136 if (start_reg - reg == 3)
10138 insn = emit_sfm (reg, 4);
10139 RTX_FRAME_RELATED_P (insn) = 1;
10140 saved_regs += 48;
10141 start_reg = reg - 1;
10144 else
10146 if (start_reg != reg)
10148 insn = emit_sfm (reg + 1, start_reg - reg);
10149 RTX_FRAME_RELATED_P (insn) = 1;
10150 saved_regs += (start_reg - reg) * 12;
10152 start_reg = reg - 1;
10156 if (start_reg != reg)
10158 insn = emit_sfm (reg + 1, start_reg - reg);
10159 saved_regs += (start_reg - reg) * 12;
10160 RTX_FRAME_RELATED_P (insn) = 1;
10163 if (TARGET_HARD_FLOAT && TARGET_VFP)
10165 start_reg = FIRST_VFP_REGNUM;
10167 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10169 if ((!regs_ever_live[reg] || call_used_regs[reg])
10170 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10172 if (start_reg != reg)
10173 saved_regs += vfp_emit_fstmx (start_reg,
10174 (reg - start_reg) / 2);
10175 start_reg = reg + 2;
10178 if (start_reg != reg)
10179 saved_regs += vfp_emit_fstmx (start_reg,
10180 (reg - start_reg) / 2);
10184 if (frame_pointer_needed)
10186 /* Create the new frame pointer. */
10187 insn = GEN_INT (-(4 + args_to_push + fp_offset));
10188 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10189 RTX_FRAME_RELATED_P (insn) = 1;
10191 if (IS_NESTED (func_type))
10193 /* Recover the static chain register. */
10194 if (regs_ever_live [3] == 0
10195 || saved_pretend_args)
10196 insn = gen_rtx_REG (SImode, 3);
10197 else /* if (current_function_pretend_args_size == 0) */
10199 insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10200 GEN_INT (4));
10201 insn = gen_rtx_MEM (SImode, insn);
10204 emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10205 /* Add a USE to stop propagate_one_insn() from barfing. */
10206 emit_insn (gen_prologue_use (ip_rtx));
10210 offsets = arm_get_frame_offsets ();
10211 if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10213 /* This add can produce multiple insns for a large constant, so we
10214 need to get tricky. */
10215 rtx last = get_last_insn ();
10217 amount = GEN_INT (offsets->saved_args + saved_regs
10218 - offsets->outgoing_args);
10220 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10221 amount));
10224 last = last ? NEXT_INSN (last) : get_insns ();
10225 RTX_FRAME_RELATED_P (last) = 1;
10227 while (last != insn);
10229 /* If the frame pointer is needed, emit a special barrier that
10230 will prevent the scheduler from moving stores to the frame
10231 before the stack adjustment. */
10232 if (frame_pointer_needed)
10233 insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10234 hard_frame_pointer_rtx));
10238 if (flag_pic)
10239 arm_load_pic_register (INVALID_REGNUM);
10241 /* If we are profiling, make sure no instructions are scheduled before
10242 the call to mcount. Similarly if the user has requested no
10243 scheduling in the prolog. */
10244 if (current_function_profile || TARGET_NO_SCHED_PRO)
10245 emit_insn (gen_blockage ());
10247 /* If the link register is being kept alive, with the return address in it,
10248 then make sure that it does not get reused by the ce2 pass. */
10249 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10251 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10252 cfun->machine->lr_save_eliminated = 1;
10256 /* If CODE is 'd', then the X is a condition operand and the instruction
10257 should only be executed if the condition is true.
10258 if CODE is 'D', then the X is a condition operand and the instruction
10259 should only be executed if the condition is false: however, if the mode
10260 of the comparison is CCFPEmode, then always execute the instruction -- we
10261 do this because in these circumstances !GE does not necessarily imply LT;
10262 in these cases the instruction pattern will take care to make sure that
10263 an instruction containing %d will follow, thereby undoing the effects of
10264 doing this instruction unconditionally.
10265 If CODE is 'N' then X is a floating point operand that must be negated
10266 before output.
10267 If CODE is 'B' then output a bitwise inverted value of X (a const int).
10268 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
10269 void
10270 arm_print_operand (FILE *stream, rtx x, int code)
10272 switch (code)
10274 case '@':
10275 fputs (ASM_COMMENT_START, stream);
10276 return;
10278 case '_':
10279 fputs (user_label_prefix, stream);
10280 return;
10282 case '|':
10283 fputs (REGISTER_PREFIX, stream);
10284 return;
10286 case '?':
10287 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10289 if (TARGET_THUMB)
10291 output_operand_lossage ("predicated Thumb instruction");
10292 break;
10294 if (current_insn_predicate != NULL)
10296 output_operand_lossage
10297 ("predicated instruction in conditional sequence");
10298 break;
10301 fputs (arm_condition_codes[arm_current_cc], stream);
10303 else if (current_insn_predicate)
10305 enum arm_cond_code code;
10307 if (TARGET_THUMB)
10309 output_operand_lossage ("predicated Thumb instruction");
10310 break;
10313 code = get_arm_condition_code (current_insn_predicate);
10314 fputs (arm_condition_codes[code], stream);
10316 return;
10318 case 'N':
10320 REAL_VALUE_TYPE r;
10321 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10322 r = REAL_VALUE_NEGATE (r);
10323 fprintf (stream, "%s", fp_const_from_val (&r));
10325 return;
10327 case 'B':
10328 if (GET_CODE (x) == CONST_INT)
10330 HOST_WIDE_INT val;
10331 val = ARM_SIGN_EXTEND (~INTVAL (x));
10332 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10334 else
10336 putc ('~', stream);
10337 output_addr_const (stream, x);
10339 return;
10341 case 'i':
10342 fprintf (stream, "%s", arithmetic_instr (x, 1));
10343 return;
10345 /* Truncate Cirrus shift counts. */
10346 case 's':
10347 if (GET_CODE (x) == CONST_INT)
10349 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10350 return;
10352 arm_print_operand (stream, x, 0);
10353 return;
10355 case 'I':
10356 fprintf (stream, "%s", arithmetic_instr (x, 0));
10357 return;
10359 case 'S':
10361 HOST_WIDE_INT val;
10362 const char * shift = shift_op (x, &val);
10364 if (shift)
10366 fprintf (stream, ", %s ", shift_op (x, &val));
10367 if (val == -1)
10368 arm_print_operand (stream, XEXP (x, 1), 0);
10369 else
10370 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10373 return;
10375 /* An explanation of the 'Q', 'R' and 'H' register operands:
10377 In a pair of registers containing a DI or DF value the 'Q'
10378 operand returns the register number of the register containing
10379 the least significant part of the value. The 'R' operand returns
10380 the register number of the register containing the most
10381 significant part of the value.
10383 The 'H' operand returns the higher of the two register numbers.
10384 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10385 same as the 'Q' operand, since the most significant part of the
10386 value is held in the lower number register. The reverse is true
10387 on systems where WORDS_BIG_ENDIAN is false.
10389 The purpose of these operands is to distinguish between cases
10390 where the endian-ness of the values is important (for example
10391 when they are added together), and cases where the endian-ness
10392 is irrelevant, but the order of register operations is important.
10393 For example when loading a value from memory into a register
10394 pair, the endian-ness does not matter. Provided that the value
10395 from the lower memory address is put into the lower numbered
10396 register, and the value from the higher address is put into the
10397 higher numbered register, the load will work regardless of whether
10398 the value being loaded is big-wordian or little-wordian. The
10399 order of the two register loads can matter however, if the address
10400 of the memory location is actually held in one of the registers
10401 being overwritten by the load. */
10402 case 'Q':
10403 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10405 output_operand_lossage ("invalid operand for code '%c'", code);
10406 return;
10409 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10410 return;
10412 case 'R':
10413 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10415 output_operand_lossage ("invalid operand for code '%c'", code);
10416 return;
10419 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10420 return;
10422 case 'H':
10423 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10425 output_operand_lossage ("invalid operand for code '%c'", code);
10426 return;
10429 asm_fprintf (stream, "%r", REGNO (x) + 1);
10430 return;
10432 case 'm':
10433 asm_fprintf (stream, "%r",
10434 GET_CODE (XEXP (x, 0)) == REG
10435 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10436 return;
10438 case 'M':
10439 asm_fprintf (stream, "{%r-%r}",
10440 REGNO (x),
10441 REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10442 return;
10444 case 'd':
10445 /* CONST_TRUE_RTX means always -- that's the default. */
10446 if (x == const_true_rtx)
10447 return;
10449 if (!COMPARISON_P (x))
10451 output_operand_lossage ("invalid operand for code '%c'", code);
10452 return;
10455 fputs (arm_condition_codes[get_arm_condition_code (x)],
10456 stream);
10457 return;
10459 case 'D':
10460 /* CONST_TRUE_RTX means not always -- i.e. never. We shouldn't ever
10461 want to do that. */
10462 if (x == const_true_rtx)
10464 output_operand_lossage ("instruction never exectued");
10465 return;
10467 if (!COMPARISON_P (x))
10469 output_operand_lossage ("invalid operand for code '%c'", code);
10470 return;
10473 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10474 (get_arm_condition_code (x))],
10475 stream);
10476 return;
10478 /* Cirrus registers can be accessed in a variety of ways:
10479 single floating point (f)
10480 double floating point (d)
10481 32bit integer (fx)
10482 64bit integer (dx). */
10483 case 'W': /* Cirrus register in F mode. */
10484 case 'X': /* Cirrus register in D mode. */
10485 case 'Y': /* Cirrus register in FX mode. */
10486 case 'Z': /* Cirrus register in DX mode. */
10487 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10488 abort ();
10490 fprintf (stream, "mv%s%s",
10491 code == 'W' ? "f"
10492 : code == 'X' ? "d"
10493 : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10495 return;
10497 /* Print cirrus register in the mode specified by the register's mode. */
10498 case 'V':
10500 int mode = GET_MODE (x);
10502 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10504 output_operand_lossage ("invalid operand for code '%c'", code);
10505 return;
10508 fprintf (stream, "mv%s%s",
10509 mode == DFmode ? "d"
10510 : mode == SImode ? "fx"
10511 : mode == DImode ? "dx"
10512 : "f", reg_names[REGNO (x)] + 2);
10514 return;
10517 case 'U':
10518 if (GET_CODE (x) != REG
10519 || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10520 || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10521 /* Bad value for wCG register number. */
10523 output_operand_lossage ("invalid operand for code '%c'", code);
10524 return;
10527 else
10528 fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10529 return;
10531 /* Print an iWMMXt control register name. */
10532 case 'w':
10533 if (GET_CODE (x) != CONST_INT
10534 || INTVAL (x) < 0
10535 || INTVAL (x) >= 16)
10536 /* Bad value for wC register number. */
10538 output_operand_lossage ("invalid operand for code '%c'", code);
10539 return;
10542 else
10544 static const char * wc_reg_names [16] =
10546 "wCID", "wCon", "wCSSF", "wCASF",
10547 "wC4", "wC5", "wC6", "wC7",
10548 "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10549 "wC12", "wC13", "wC14", "wC15"
10552 fprintf (stream, wc_reg_names [INTVAL (x)]);
10554 return;
10556 /* Print a VFP double precision register name. */
10557 case 'P':
10559 int mode = GET_MODE (x);
10560 int num;
10562 if (mode != DImode && mode != DFmode)
10564 output_operand_lossage ("invalid operand for code '%c'", code);
10565 return;
10568 if (GET_CODE (x) != REG
10569 || !IS_VFP_REGNUM (REGNO (x)))
10571 output_operand_lossage ("invalid operand for code '%c'", code);
10572 return;
10575 num = REGNO(x) - FIRST_VFP_REGNUM;
10576 if (num & 1)
10578 output_operand_lossage ("invalid operand for code '%c'", code);
10579 return;
10582 fprintf (stream, "d%d", num >> 1);
10584 return;
10586 default:
10587 if (x == 0)
10589 output_operand_lossage ("missing operand");
10590 return;
10593 if (GET_CODE (x) == REG)
10594 asm_fprintf (stream, "%r", REGNO (x));
10595 else if (GET_CODE (x) == MEM)
10597 output_memory_reference_mode = GET_MODE (x);
10598 output_address (XEXP (x, 0));
10600 else if (GET_CODE (x) == CONST_DOUBLE)
10601 fprintf (stream, "#%s", fp_immediate_constant (x));
10602 else if (GET_CODE (x) == NEG)
10603 abort (); /* This should never happen now. */
10604 else
10606 fputc ('#', stream);
10607 output_addr_const (stream, x);
10612 #ifndef AOF_ASSEMBLER
10613 /* Target hook for assembling integer objects. The ARM version needs to
10614 handle word-sized values specially. */
10615 static bool
10616 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10618 if (size == UNITS_PER_WORD && aligned_p)
10620 fputs ("\t.word\t", asm_out_file);
10621 output_addr_const (asm_out_file, x);
10623 /* Mark symbols as position independent. We only do this in the
10624 .text segment, not in the .data segment. */
10625 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10626 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10628 if (GET_CODE (x) == SYMBOL_REF
10629 && (CONSTANT_POOL_ADDRESS_P (x)
10630 || SYMBOL_REF_LOCAL_P (x)))
10631 fputs ("(GOTOFF)", asm_out_file);
10632 else if (GET_CODE (x) == LABEL_REF)
10633 fputs ("(GOTOFF)", asm_out_file);
10634 else
10635 fputs ("(GOT)", asm_out_file);
10637 fputc ('\n', asm_out_file);
10638 return true;
10641 if (arm_vector_mode_supported_p (GET_MODE (x)))
10643 int i, units;
10645 if (GET_CODE (x) != CONST_VECTOR)
10646 abort ();
10648 units = CONST_VECTOR_NUNITS (x);
10650 switch (GET_MODE (x))
10652 case V2SImode: size = 4; break;
10653 case V4HImode: size = 2; break;
10654 case V8QImode: size = 1; break;
10655 default:
10656 abort ();
10659 for (i = 0; i < units; i++)
10661 rtx elt;
10663 elt = CONST_VECTOR_ELT (x, i);
10664 assemble_integer
10665 (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10668 return true;
10671 return default_assemble_integer (x, size, aligned_p);
10673 #endif
10675 /* A finite state machine takes care of noticing whether or not instructions
10676 can be conditionally executed, and thus decrease execution time and code
10677 size by deleting branch instructions. The fsm is controlled by
10678 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
10680 /* The state of the fsm controlling condition codes are:
10681 0: normal, do nothing special
10682 1: make ASM_OUTPUT_OPCODE not output this instruction
10683 2: make ASM_OUTPUT_OPCODE not output this instruction
10684 3: make instructions conditional
10685 4: make instructions conditional
10687 State transitions (state->state by whom under condition):
10688 0 -> 1 final_prescan_insn if the `target' is a label
10689 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10690 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10691 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10692 3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10693 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10694 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10695 (the target insn is arm_target_insn).
10697 If the jump clobbers the conditions then we use states 2 and 4.
10699 A similar thing can be done with conditional return insns.
10701 XXX In case the `target' is an unconditional branch, this conditionalising
10702 of the instructions always reduces code size, but not always execution
10703 time. But then, I want to reduce the code size to somewhere near what
10704 /bin/cc produces. */
10706 /* Returns the index of the ARM condition code string in
10707 `arm_condition_codes'. COMPARISON should be an rtx like
10708 `(eq (...) (...))'. */
10709 static enum arm_cond_code
10710 get_arm_condition_code (rtx comparison)
10712 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10713 int code;
10714 enum rtx_code comp_code = GET_CODE (comparison);
10716 if (GET_MODE_CLASS (mode) != MODE_CC)
10717 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10718 XEXP (comparison, 1));
10720 switch (mode)
10722 case CC_DNEmode: code = ARM_NE; goto dominance;
10723 case CC_DEQmode: code = ARM_EQ; goto dominance;
10724 case CC_DGEmode: code = ARM_GE; goto dominance;
10725 case CC_DGTmode: code = ARM_GT; goto dominance;
10726 case CC_DLEmode: code = ARM_LE; goto dominance;
10727 case CC_DLTmode: code = ARM_LT; goto dominance;
10728 case CC_DGEUmode: code = ARM_CS; goto dominance;
10729 case CC_DGTUmode: code = ARM_HI; goto dominance;
10730 case CC_DLEUmode: code = ARM_LS; goto dominance;
10731 case CC_DLTUmode: code = ARM_CC;
10733 dominance:
10734 if (comp_code != EQ && comp_code != NE)
10735 abort ();
10737 if (comp_code == EQ)
10738 return ARM_INVERSE_CONDITION_CODE (code);
10739 return code;
10741 case CC_NOOVmode:
10742 switch (comp_code)
10744 case NE: return ARM_NE;
10745 case EQ: return ARM_EQ;
10746 case GE: return ARM_PL;
10747 case LT: return ARM_MI;
10748 default: abort ();
10751 case CC_Zmode:
10752 switch (comp_code)
10754 case NE: return ARM_NE;
10755 case EQ: return ARM_EQ;
10756 default: abort ();
10759 case CC_Nmode:
10760 switch (comp_code)
10762 case NE: return ARM_MI;
10763 case EQ: return ARM_PL;
10764 default: abort ();
10767 case CCFPEmode:
10768 case CCFPmode:
10769 /* These encodings assume that AC=1 in the FPA system control
10770 byte. This allows us to handle all cases except UNEQ and
10771 LTGT. */
10772 switch (comp_code)
10774 case GE: return ARM_GE;
10775 case GT: return ARM_GT;
10776 case LE: return ARM_LS;
10777 case LT: return ARM_MI;
10778 case NE: return ARM_NE;
10779 case EQ: return ARM_EQ;
10780 case ORDERED: return ARM_VC;
10781 case UNORDERED: return ARM_VS;
10782 case UNLT: return ARM_LT;
10783 case UNLE: return ARM_LE;
10784 case UNGT: return ARM_HI;
10785 case UNGE: return ARM_PL;
10786 /* UNEQ and LTGT do not have a representation. */
10787 case UNEQ: /* Fall through. */
10788 case LTGT: /* Fall through. */
10789 default: abort ();
10792 case CC_SWPmode:
10793 switch (comp_code)
10795 case NE: return ARM_NE;
10796 case EQ: return ARM_EQ;
10797 case GE: return ARM_LE;
10798 case GT: return ARM_LT;
10799 case LE: return ARM_GE;
10800 case LT: return ARM_GT;
10801 case GEU: return ARM_LS;
10802 case GTU: return ARM_CC;
10803 case LEU: return ARM_CS;
10804 case LTU: return ARM_HI;
10805 default: abort ();
10808 case CC_Cmode:
10809 switch (comp_code)
10811 case LTU: return ARM_CS;
10812 case GEU: return ARM_CC;
10813 default: abort ();
10816 case CCmode:
10817 switch (comp_code)
10819 case NE: return ARM_NE;
10820 case EQ: return ARM_EQ;
10821 case GE: return ARM_GE;
10822 case GT: return ARM_GT;
10823 case LE: return ARM_LE;
10824 case LT: return ARM_LT;
10825 case GEU: return ARM_CS;
10826 case GTU: return ARM_HI;
10827 case LEU: return ARM_LS;
10828 case LTU: return ARM_CC;
10829 default: abort ();
10832 default: abort ();
10835 abort ();
10838 void
10839 arm_final_prescan_insn (rtx insn)
10841 /* BODY will hold the body of INSN. */
10842 rtx body = PATTERN (insn);
10844 /* This will be 1 if trying to repeat the trick, and things need to be
10845 reversed if it appears to fail. */
10846 int reverse = 0;
10848 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
10849 taken are clobbered, even if the rtl suggests otherwise. It also
10850 means that we have to grub around within the jump expression to find
10851 out what the conditions are when the jump isn't taken. */
10852 int jump_clobbers = 0;
10854 /* If we start with a return insn, we only succeed if we find another one. */
10855 int seeking_return = 0;
10857 /* START_INSN will hold the insn from where we start looking. This is the
10858 first insn after the following code_label if REVERSE is true. */
10859 rtx start_insn = insn;
10861 /* If in state 4, check if the target branch is reached, in order to
10862 change back to state 0. */
10863 if (arm_ccfsm_state == 4)
10865 if (insn == arm_target_insn)
10867 arm_target_insn = NULL;
10868 arm_ccfsm_state = 0;
10870 return;
10873 /* If in state 3, it is possible to repeat the trick, if this insn is an
10874 unconditional branch to a label, and immediately following this branch
10875 is the previous target label which is only used once, and the label this
10876 branch jumps to is not too far off. */
10877 if (arm_ccfsm_state == 3)
10879 if (simplejump_p (insn))
10881 start_insn = next_nonnote_insn (start_insn);
10882 if (GET_CODE (start_insn) == BARRIER)
10884 /* XXX Isn't this always a barrier? */
10885 start_insn = next_nonnote_insn (start_insn);
10887 if (GET_CODE (start_insn) == CODE_LABEL
10888 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
10889 && LABEL_NUSES (start_insn) == 1)
10890 reverse = TRUE;
10891 else
10892 return;
10894 else if (GET_CODE (body) == RETURN)
10896 start_insn = next_nonnote_insn (start_insn);
10897 if (GET_CODE (start_insn) == BARRIER)
10898 start_insn = next_nonnote_insn (start_insn);
10899 if (GET_CODE (start_insn) == CODE_LABEL
10900 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
10901 && LABEL_NUSES (start_insn) == 1)
10903 reverse = TRUE;
10904 seeking_return = 1;
10906 else
10907 return;
10909 else
10910 return;
10913 if (arm_ccfsm_state != 0 && !reverse)
10914 abort ();
10915 if (GET_CODE (insn) != JUMP_INSN)
10916 return;
10918 /* This jump might be paralleled with a clobber of the condition codes
10919 the jump should always come first */
10920 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
10921 body = XVECEXP (body, 0, 0);
10923 if (reverse
10924 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
10925 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
10927 int insns_skipped;
10928 int fail = FALSE, succeed = FALSE;
10929 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
10930 int then_not_else = TRUE;
10931 rtx this_insn = start_insn, label = 0;
10933 /* If the jump cannot be done with one instruction, we cannot
10934 conditionally execute the instruction in the inverse case. */
10935 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
10937 jump_clobbers = 1;
10938 return;
10941 /* Register the insn jumped to. */
10942 if (reverse)
10944 if (!seeking_return)
10945 label = XEXP (SET_SRC (body), 0);
10947 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
10948 label = XEXP (XEXP (SET_SRC (body), 1), 0);
10949 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
10951 label = XEXP (XEXP (SET_SRC (body), 2), 0);
10952 then_not_else = FALSE;
10954 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
10955 seeking_return = 1;
10956 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
10958 seeking_return = 1;
10959 then_not_else = FALSE;
10961 else
10962 abort ();
10964 /* See how many insns this branch skips, and what kind of insns. If all
10965 insns are okay, and the label or unconditional branch to the same
10966 label is not too far away, succeed. */
10967 for (insns_skipped = 0;
10968 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
10970 rtx scanbody;
10972 this_insn = next_nonnote_insn (this_insn);
10973 if (!this_insn)
10974 break;
10976 switch (GET_CODE (this_insn))
10978 case CODE_LABEL:
10979 /* Succeed if it is the target label, otherwise fail since
10980 control falls in from somewhere else. */
10981 if (this_insn == label)
10983 if (jump_clobbers)
10985 arm_ccfsm_state = 2;
10986 this_insn = next_nonnote_insn (this_insn);
10988 else
10989 arm_ccfsm_state = 1;
10990 succeed = TRUE;
10992 else
10993 fail = TRUE;
10994 break;
10996 case BARRIER:
10997 /* Succeed if the following insn is the target label.
10998 Otherwise fail.
10999 If return insns are used then the last insn in a function
11000 will be a barrier. */
11001 this_insn = next_nonnote_insn (this_insn);
11002 if (this_insn && this_insn == label)
11004 if (jump_clobbers)
11006 arm_ccfsm_state = 2;
11007 this_insn = next_nonnote_insn (this_insn);
11009 else
11010 arm_ccfsm_state = 1;
11011 succeed = TRUE;
11013 else
11014 fail = TRUE;
11015 break;
11017 case CALL_INSN:
11018 /* The AAPCS says that conditional calls should not be
11019 used since they make interworking inefficient (the
11020 linker can't transform BL<cond> into BLX). That's
11021 only a problem if the machine has BLX. */
11022 if (arm_arch5)
11024 fail = TRUE;
11025 break;
11028 /* Succeed if the following insn is the target label, or
11029 if the following two insns are a barrier and the
11030 target label. */
11031 this_insn = next_nonnote_insn (this_insn);
11032 if (this_insn && GET_CODE (this_insn) == BARRIER)
11033 this_insn = next_nonnote_insn (this_insn);
11035 if (this_insn && this_insn == label
11036 && insns_skipped < max_insns_skipped)
11038 if (jump_clobbers)
11040 arm_ccfsm_state = 2;
11041 this_insn = next_nonnote_insn (this_insn);
11043 else
11044 arm_ccfsm_state = 1;
11045 succeed = TRUE;
11047 else
11048 fail = TRUE;
11049 break;
11051 case JUMP_INSN:
11052 /* If this is an unconditional branch to the same label, succeed.
11053 If it is to another label, do nothing. If it is conditional,
11054 fail. */
11055 /* XXX Probably, the tests for SET and the PC are
11056 unnecessary. */
11058 scanbody = PATTERN (this_insn);
11059 if (GET_CODE (scanbody) == SET
11060 && GET_CODE (SET_DEST (scanbody)) == PC)
11062 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11063 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11065 arm_ccfsm_state = 2;
11066 succeed = TRUE;
11068 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11069 fail = TRUE;
11071 /* Fail if a conditional return is undesirable (e.g. on a
11072 StrongARM), but still allow this if optimizing for size. */
11073 else if (GET_CODE (scanbody) == RETURN
11074 && !use_return_insn (TRUE, NULL)
11075 && !optimize_size)
11076 fail = TRUE;
11077 else if (GET_CODE (scanbody) == RETURN
11078 && seeking_return)
11080 arm_ccfsm_state = 2;
11081 succeed = TRUE;
11083 else if (GET_CODE (scanbody) == PARALLEL)
11085 switch (get_attr_conds (this_insn))
11087 case CONDS_NOCOND:
11088 break;
11089 default:
11090 fail = TRUE;
11091 break;
11094 else
11095 fail = TRUE; /* Unrecognized jump (e.g. epilogue). */
11097 break;
11099 case INSN:
11100 /* Instructions using or affecting the condition codes make it
11101 fail. */
11102 scanbody = PATTERN (this_insn);
11103 if (!(GET_CODE (scanbody) == SET
11104 || GET_CODE (scanbody) == PARALLEL)
11105 || get_attr_conds (this_insn) != CONDS_NOCOND)
11106 fail = TRUE;
11108 /* A conditional cirrus instruction must be followed by
11109 a non Cirrus instruction. However, since we
11110 conditionalize instructions in this function and by
11111 the time we get here we can't add instructions
11112 (nops), because shorten_branches() has already been
11113 called, we will disable conditionalizing Cirrus
11114 instructions to be safe. */
11115 if (GET_CODE (scanbody) != USE
11116 && GET_CODE (scanbody) != CLOBBER
11117 && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11118 fail = TRUE;
11119 break;
11121 default:
11122 break;
11125 if (succeed)
11127 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11128 arm_target_label = CODE_LABEL_NUMBER (label);
11129 else if (seeking_return || arm_ccfsm_state == 2)
11131 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11133 this_insn = next_nonnote_insn (this_insn);
11134 if (this_insn && (GET_CODE (this_insn) == BARRIER
11135 || GET_CODE (this_insn) == CODE_LABEL))
11136 abort ();
11138 if (!this_insn)
11140 /* Oh, dear! we ran off the end.. give up. */
11141 recog (PATTERN (insn), insn, NULL);
11142 arm_ccfsm_state = 0;
11143 arm_target_insn = NULL;
11144 return;
11146 arm_target_insn = this_insn;
11148 else
11149 abort ();
11150 if (jump_clobbers)
11152 if (reverse)
11153 abort ();
11154 arm_current_cc =
11155 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11156 0), 0), 1));
11157 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11158 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11159 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11160 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11162 else
11164 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11165 what it was. */
11166 if (!reverse)
11167 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11168 0));
11171 if (reverse || then_not_else)
11172 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11175 /* Restore recog_data (getting the attributes of other insns can
11176 destroy this array, but final.c assumes that it remains intact
11177 across this call; since the insn has been recognized already we
11178 call recog direct). */
11179 recog (PATTERN (insn), insn, NULL);
11183 /* Returns true if REGNO is a valid register
11184 for holding a quantity of type MODE. */
11186 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11188 if (GET_MODE_CLASS (mode) == MODE_CC)
11189 return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11191 if (TARGET_THUMB)
11192 /* For the Thumb we only allow values bigger than SImode in
11193 registers 0 - 6, so that there is always a second low
11194 register available to hold the upper part of the value.
11195 We probably we ought to ensure that the register is the
11196 start of an even numbered register pair. */
11197 return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11199 if (IS_CIRRUS_REGNUM (regno))
11200 /* We have outlawed SI values in Cirrus registers because they
11201 reside in the lower 32 bits, but SF values reside in the
11202 upper 32 bits. This causes gcc all sorts of grief. We can't
11203 even split the registers into pairs because Cirrus SI values
11204 get sign extended to 64bits-- aldyh. */
11205 return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11207 if (IS_VFP_REGNUM (regno))
11209 if (mode == SFmode || mode == SImode)
11210 return TRUE;
11212 /* DFmode values are only valid in even register pairs. */
11213 if (mode == DFmode)
11214 return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11215 return FALSE;
11218 if (IS_IWMMXT_GR_REGNUM (regno))
11219 return mode == SImode;
11221 if (IS_IWMMXT_REGNUM (regno))
11222 return VALID_IWMMXT_REG_MODE (mode);
11224 /* We allow any value to be stored in the general registers.
11225 Restrict doubleword quantities to even register pairs so that we can
11226 use ldrd. */
11227 if (regno <= LAST_ARM_REGNUM)
11228 return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11230 if ( regno == FRAME_POINTER_REGNUM
11231 || regno == ARG_POINTER_REGNUM)
11232 /* We only allow integers in the fake hard registers. */
11233 return GET_MODE_CLASS (mode) == MODE_INT;
11235 /* The only registers left are the FPA registers
11236 which we only allow to hold FP values. */
11237 return GET_MODE_CLASS (mode) == MODE_FLOAT
11238 && regno >= FIRST_FPA_REGNUM
11239 && regno <= LAST_FPA_REGNUM;
11243 arm_regno_class (int regno)
11245 if (TARGET_THUMB)
11247 if (regno == STACK_POINTER_REGNUM)
11248 return STACK_REG;
11249 if (regno == CC_REGNUM)
11250 return CC_REG;
11251 if (regno < 8)
11252 return LO_REGS;
11253 return HI_REGS;
11256 if ( regno <= LAST_ARM_REGNUM
11257 || regno == FRAME_POINTER_REGNUM
11258 || regno == ARG_POINTER_REGNUM)
11259 return GENERAL_REGS;
11261 if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11262 return NO_REGS;
11264 if (IS_CIRRUS_REGNUM (regno))
11265 return CIRRUS_REGS;
11267 if (IS_VFP_REGNUM (regno))
11268 return VFP_REGS;
11270 if (IS_IWMMXT_REGNUM (regno))
11271 return IWMMXT_REGS;
11273 if (IS_IWMMXT_GR_REGNUM (regno))
11274 return IWMMXT_GR_REGS;
11276 return FPA_REGS;
11279 /* Handle a special case when computing the offset
11280 of an argument from the frame pointer. */
11282 arm_debugger_arg_offset (int value, rtx addr)
11284 rtx insn;
11286 /* We are only interested if dbxout_parms() failed to compute the offset. */
11287 if (value != 0)
11288 return 0;
11290 /* We can only cope with the case where the address is held in a register. */
11291 if (GET_CODE (addr) != REG)
11292 return 0;
11294 /* If we are using the frame pointer to point at the argument, then
11295 an offset of 0 is correct. */
11296 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11297 return 0;
11299 /* If we are using the stack pointer to point at the
11300 argument, then an offset of 0 is correct. */
11301 if ((TARGET_THUMB || !frame_pointer_needed)
11302 && REGNO (addr) == SP_REGNUM)
11303 return 0;
11305 /* Oh dear. The argument is pointed to by a register rather
11306 than being held in a register, or being stored at a known
11307 offset from the frame pointer. Since GDB only understands
11308 those two kinds of argument we must translate the address
11309 held in the register into an offset from the frame pointer.
11310 We do this by searching through the insns for the function
11311 looking to see where this register gets its value. If the
11312 register is initialized from the frame pointer plus an offset
11313 then we are in luck and we can continue, otherwise we give up.
11315 This code is exercised by producing debugging information
11316 for a function with arguments like this:
11318 double func (double a, double b, int c, double d) {return d;}
11320 Without this code the stab for parameter 'd' will be set to
11321 an offset of 0 from the frame pointer, rather than 8. */
11323 /* The if() statement says:
11325 If the insn is a normal instruction
11326 and if the insn is setting the value in a register
11327 and if the register being set is the register holding the address of the argument
11328 and if the address is computing by an addition
11329 that involves adding to a register
11330 which is the frame pointer
11331 a constant integer
11333 then... */
11335 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11337 if ( GET_CODE (insn) == INSN
11338 && GET_CODE (PATTERN (insn)) == SET
11339 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11340 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11341 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11342 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11343 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11346 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11348 break;
11352 if (value == 0)
11354 debug_rtx (addr);
11355 warning ("unable to compute real location of stacked parameter");
11356 value = 8; /* XXX magic hack */
11359 return value;
11362 #define def_mbuiltin(MASK, NAME, TYPE, CODE) \
11363 do \
11365 if ((MASK) & insn_flags) \
11366 lang_hooks.builtin_function ((NAME), (TYPE), (CODE), \
11367 BUILT_IN_MD, NULL, NULL_TREE); \
11369 while (0)
11371 struct builtin_description
11373 const unsigned int mask;
11374 const enum insn_code icode;
11375 const char * const name;
11376 const enum arm_builtins code;
11377 const enum rtx_code comparison;
11378 const unsigned int flag;
11381 static const struct builtin_description bdesc_2arg[] =
11383 #define IWMMXT_BUILTIN(code, string, builtin) \
11384 { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11385 ARM_BUILTIN_##builtin, 0, 0 },
11387 IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11388 IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11389 IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11390 IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11391 IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11392 IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11393 IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11394 IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11395 IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11396 IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11397 IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11398 IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11399 IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11400 IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11401 IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11402 IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11403 IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11404 IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11405 IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11406 IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11407 IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11408 IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11409 IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11410 IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11411 IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11412 IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11413 IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11414 IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11415 IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11416 IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11417 IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11418 IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11419 IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11420 IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11421 IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11422 IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11423 IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11424 IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11425 IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11426 IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11427 IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11428 IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11429 IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11430 IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11431 IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11432 IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11433 IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11434 IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11435 IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11436 IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11437 IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11438 IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11439 IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11440 IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11441 IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11442 IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11443 IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11444 IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11446 #define IWMMXT_BUILTIN2(code, builtin) \
11447 { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11449 IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11450 IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11451 IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11452 IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11453 IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11454 IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11455 IWMMXT_BUILTIN2 (ashlv4hi3_di, WSLLH)
11456 IWMMXT_BUILTIN2 (ashlv4hi3, WSLLHI)
11457 IWMMXT_BUILTIN2 (ashlv2si3_di, WSLLW)
11458 IWMMXT_BUILTIN2 (ashlv2si3, WSLLWI)
11459 IWMMXT_BUILTIN2 (ashldi3_di, WSLLD)
11460 IWMMXT_BUILTIN2 (ashldi3_iwmmxt, WSLLDI)
11461 IWMMXT_BUILTIN2 (lshrv4hi3_di, WSRLH)
11462 IWMMXT_BUILTIN2 (lshrv4hi3, WSRLHI)
11463 IWMMXT_BUILTIN2 (lshrv2si3_di, WSRLW)
11464 IWMMXT_BUILTIN2 (lshrv2si3, WSRLWI)
11465 IWMMXT_BUILTIN2 (lshrdi3_di, WSRLD)
11466 IWMMXT_BUILTIN2 (lshrdi3_iwmmxt, WSRLDI)
11467 IWMMXT_BUILTIN2 (ashrv4hi3_di, WSRAH)
11468 IWMMXT_BUILTIN2 (ashrv4hi3, WSRAHI)
11469 IWMMXT_BUILTIN2 (ashrv2si3_di, WSRAW)
11470 IWMMXT_BUILTIN2 (ashrv2si3, WSRAWI)
11471 IWMMXT_BUILTIN2 (ashrdi3_di, WSRAD)
11472 IWMMXT_BUILTIN2 (ashrdi3_iwmmxt, WSRADI)
11473 IWMMXT_BUILTIN2 (rorv4hi3_di, WRORH)
11474 IWMMXT_BUILTIN2 (rorv4hi3, WRORHI)
11475 IWMMXT_BUILTIN2 (rorv2si3_di, WRORW)
11476 IWMMXT_BUILTIN2 (rorv2si3, WRORWI)
11477 IWMMXT_BUILTIN2 (rordi3_di, WRORD)
11478 IWMMXT_BUILTIN2 (rordi3, WRORDI)
11479 IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
11480 IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
11483 static const struct builtin_description bdesc_1arg[] =
11485 IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11486 IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11487 IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11488 IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11489 IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11490 IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11491 IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11492 IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11493 IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11494 IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11495 IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11496 IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11497 IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11498 IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11499 IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11500 IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11501 IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11502 IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11505 /* Set up all the iWMMXt builtins. This is
11506 not called if TARGET_IWMMXT is zero. */
11508 static void
11509 arm_init_iwmmxt_builtins (void)
11511 const struct builtin_description * d;
11512 size_t i;
11513 tree endlink = void_list_node;
11515 tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11516 tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11517 tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11519 tree int_ftype_int
11520 = build_function_type (integer_type_node,
11521 tree_cons (NULL_TREE, integer_type_node, endlink));
11522 tree v8qi_ftype_v8qi_v8qi_int
11523 = build_function_type (V8QI_type_node,
11524 tree_cons (NULL_TREE, V8QI_type_node,
11525 tree_cons (NULL_TREE, V8QI_type_node,
11526 tree_cons (NULL_TREE,
11527 integer_type_node,
11528 endlink))));
11529 tree v4hi_ftype_v4hi_int
11530 = build_function_type (V4HI_type_node,
11531 tree_cons (NULL_TREE, V4HI_type_node,
11532 tree_cons (NULL_TREE, integer_type_node,
11533 endlink)));
11534 tree v2si_ftype_v2si_int
11535 = build_function_type (V2SI_type_node,
11536 tree_cons (NULL_TREE, V2SI_type_node,
11537 tree_cons (NULL_TREE, integer_type_node,
11538 endlink)));
11539 tree v2si_ftype_di_di
11540 = build_function_type (V2SI_type_node,
11541 tree_cons (NULL_TREE, long_long_integer_type_node,
11542 tree_cons (NULL_TREE, long_long_integer_type_node,
11543 endlink)));
11544 tree di_ftype_di_int
11545 = build_function_type (long_long_integer_type_node,
11546 tree_cons (NULL_TREE, long_long_integer_type_node,
11547 tree_cons (NULL_TREE, integer_type_node,
11548 endlink)));
11549 tree di_ftype_di_int_int
11550 = build_function_type (long_long_integer_type_node,
11551 tree_cons (NULL_TREE, long_long_integer_type_node,
11552 tree_cons (NULL_TREE, integer_type_node,
11553 tree_cons (NULL_TREE,
11554 integer_type_node,
11555 endlink))));
11556 tree int_ftype_v8qi
11557 = build_function_type (integer_type_node,
11558 tree_cons (NULL_TREE, V8QI_type_node,
11559 endlink));
11560 tree int_ftype_v4hi
11561 = build_function_type (integer_type_node,
11562 tree_cons (NULL_TREE, V4HI_type_node,
11563 endlink));
11564 tree int_ftype_v2si
11565 = build_function_type (integer_type_node,
11566 tree_cons (NULL_TREE, V2SI_type_node,
11567 endlink));
11568 tree int_ftype_v8qi_int
11569 = build_function_type (integer_type_node,
11570 tree_cons (NULL_TREE, V8QI_type_node,
11571 tree_cons (NULL_TREE, integer_type_node,
11572 endlink)));
11573 tree int_ftype_v4hi_int
11574 = build_function_type (integer_type_node,
11575 tree_cons (NULL_TREE, V4HI_type_node,
11576 tree_cons (NULL_TREE, integer_type_node,
11577 endlink)));
11578 tree int_ftype_v2si_int
11579 = build_function_type (integer_type_node,
11580 tree_cons (NULL_TREE, V2SI_type_node,
11581 tree_cons (NULL_TREE, integer_type_node,
11582 endlink)));
11583 tree v8qi_ftype_v8qi_int_int
11584 = build_function_type (V8QI_type_node,
11585 tree_cons (NULL_TREE, V8QI_type_node,
11586 tree_cons (NULL_TREE, integer_type_node,
11587 tree_cons (NULL_TREE,
11588 integer_type_node,
11589 endlink))));
11590 tree v4hi_ftype_v4hi_int_int
11591 = build_function_type (V4HI_type_node,
11592 tree_cons (NULL_TREE, V4HI_type_node,
11593 tree_cons (NULL_TREE, integer_type_node,
11594 tree_cons (NULL_TREE,
11595 integer_type_node,
11596 endlink))));
11597 tree v2si_ftype_v2si_int_int
11598 = build_function_type (V2SI_type_node,
11599 tree_cons (NULL_TREE, V2SI_type_node,
11600 tree_cons (NULL_TREE, integer_type_node,
11601 tree_cons (NULL_TREE,
11602 integer_type_node,
11603 endlink))));
11604 /* Miscellaneous. */
11605 tree v8qi_ftype_v4hi_v4hi
11606 = build_function_type (V8QI_type_node,
11607 tree_cons (NULL_TREE, V4HI_type_node,
11608 tree_cons (NULL_TREE, V4HI_type_node,
11609 endlink)));
11610 tree v4hi_ftype_v2si_v2si
11611 = build_function_type (V4HI_type_node,
11612 tree_cons (NULL_TREE, V2SI_type_node,
11613 tree_cons (NULL_TREE, V2SI_type_node,
11614 endlink)));
11615 tree v2si_ftype_v4hi_v4hi
11616 = build_function_type (V2SI_type_node,
11617 tree_cons (NULL_TREE, V4HI_type_node,
11618 tree_cons (NULL_TREE, V4HI_type_node,
11619 endlink)));
11620 tree v2si_ftype_v8qi_v8qi
11621 = build_function_type (V2SI_type_node,
11622 tree_cons (NULL_TREE, V8QI_type_node,
11623 tree_cons (NULL_TREE, V8QI_type_node,
11624 endlink)));
11625 tree v4hi_ftype_v4hi_di
11626 = build_function_type (V4HI_type_node,
11627 tree_cons (NULL_TREE, V4HI_type_node,
11628 tree_cons (NULL_TREE,
11629 long_long_integer_type_node,
11630 endlink)));
11631 tree v2si_ftype_v2si_di
11632 = build_function_type (V2SI_type_node,
11633 tree_cons (NULL_TREE, V2SI_type_node,
11634 tree_cons (NULL_TREE,
11635 long_long_integer_type_node,
11636 endlink)));
11637 tree void_ftype_int_int
11638 = build_function_type (void_type_node,
11639 tree_cons (NULL_TREE, integer_type_node,
11640 tree_cons (NULL_TREE, integer_type_node,
11641 endlink)));
11642 tree di_ftype_void
11643 = build_function_type (long_long_unsigned_type_node, endlink);
11644 tree di_ftype_v8qi
11645 = build_function_type (long_long_integer_type_node,
11646 tree_cons (NULL_TREE, V8QI_type_node,
11647 endlink));
11648 tree di_ftype_v4hi
11649 = build_function_type (long_long_integer_type_node,
11650 tree_cons (NULL_TREE, V4HI_type_node,
11651 endlink));
11652 tree di_ftype_v2si
11653 = build_function_type (long_long_integer_type_node,
11654 tree_cons (NULL_TREE, V2SI_type_node,
11655 endlink));
11656 tree v2si_ftype_v4hi
11657 = build_function_type (V2SI_type_node,
11658 tree_cons (NULL_TREE, V4HI_type_node,
11659 endlink));
11660 tree v4hi_ftype_v8qi
11661 = build_function_type (V4HI_type_node,
11662 tree_cons (NULL_TREE, V8QI_type_node,
11663 endlink));
11665 tree di_ftype_di_v4hi_v4hi
11666 = build_function_type (long_long_unsigned_type_node,
11667 tree_cons (NULL_TREE,
11668 long_long_unsigned_type_node,
11669 tree_cons (NULL_TREE, V4HI_type_node,
11670 tree_cons (NULL_TREE,
11671 V4HI_type_node,
11672 endlink))));
11674 tree di_ftype_v4hi_v4hi
11675 = build_function_type (long_long_unsigned_type_node,
11676 tree_cons (NULL_TREE, V4HI_type_node,
11677 tree_cons (NULL_TREE, V4HI_type_node,
11678 endlink)));
11680 /* Normal vector binops. */
11681 tree v8qi_ftype_v8qi_v8qi
11682 = build_function_type (V8QI_type_node,
11683 tree_cons (NULL_TREE, V8QI_type_node,
11684 tree_cons (NULL_TREE, V8QI_type_node,
11685 endlink)));
11686 tree v4hi_ftype_v4hi_v4hi
11687 = build_function_type (V4HI_type_node,
11688 tree_cons (NULL_TREE, V4HI_type_node,
11689 tree_cons (NULL_TREE, V4HI_type_node,
11690 endlink)));
11691 tree v2si_ftype_v2si_v2si
11692 = build_function_type (V2SI_type_node,
11693 tree_cons (NULL_TREE, V2SI_type_node,
11694 tree_cons (NULL_TREE, V2SI_type_node,
11695 endlink)));
11696 tree di_ftype_di_di
11697 = build_function_type (long_long_unsigned_type_node,
11698 tree_cons (NULL_TREE, long_long_unsigned_type_node,
11699 tree_cons (NULL_TREE,
11700 long_long_unsigned_type_node,
11701 endlink)));
11703 /* Add all builtins that are more or less simple operations on two
11704 operands. */
11705 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11707 /* Use one of the operands; the target can have a different mode for
11708 mask-generating compares. */
11709 enum machine_mode mode;
11710 tree type;
11712 if (d->name == 0)
11713 continue;
11715 mode = insn_data[d->icode].operand[1].mode;
11717 switch (mode)
11719 case V8QImode:
11720 type = v8qi_ftype_v8qi_v8qi;
11721 break;
11722 case V4HImode:
11723 type = v4hi_ftype_v4hi_v4hi;
11724 break;
11725 case V2SImode:
11726 type = v2si_ftype_v2si_v2si;
11727 break;
11728 case DImode:
11729 type = di_ftype_di_di;
11730 break;
11732 default:
11733 abort ();
11736 def_mbuiltin (d->mask, d->name, type, d->code);
11739 /* Add the remaining MMX insns with somewhat more complicated types. */
11740 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11741 def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11742 def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11744 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11745 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11746 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11747 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11748 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11749 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11751 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11752 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11753 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11754 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11755 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11756 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11758 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11759 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11760 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11761 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11762 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11763 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11765 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11766 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11767 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11768 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11769 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11770 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11772 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11774 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11775 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11776 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11777 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11779 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11780 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11781 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11782 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11783 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11784 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11785 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11786 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11787 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11789 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11790 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11791 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11793 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11794 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11795 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
11797 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
11798 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
11799 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
11800 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
11801 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
11802 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
11804 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
11805 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
11806 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
11807 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
11808 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
11809 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
11810 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
11811 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
11812 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
11813 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
11814 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
11815 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
11817 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
11818 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
11819 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
11820 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
11822 def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
11823 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
11824 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
11825 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
11826 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
11827 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
11828 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
11831 static void
11832 arm_init_builtins (void)
11834 if (TARGET_REALLY_IWMMXT)
11835 arm_init_iwmmxt_builtins ();
11838 /* Errors in the source file can cause expand_expr to return const0_rtx
11839 where we expect a vector. To avoid crashing, use one of the vector
11840 clear instructions. */
11842 static rtx
11843 safe_vector_operand (rtx x, enum machine_mode mode)
11845 if (x != const0_rtx)
11846 return x;
11847 x = gen_reg_rtx (mode);
11849 emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
11850 : gen_rtx_SUBREG (DImode, x, 0)));
11851 return x;
11854 /* Subroutine of arm_expand_builtin to take care of binop insns. */
11856 static rtx
11857 arm_expand_binop_builtin (enum insn_code icode,
11858 tree arglist, rtx target)
11860 rtx pat;
11861 tree arg0 = TREE_VALUE (arglist);
11862 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11863 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11864 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11865 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11866 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11867 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11869 if (VECTOR_MODE_P (mode0))
11870 op0 = safe_vector_operand (op0, mode0);
11871 if (VECTOR_MODE_P (mode1))
11872 op1 = safe_vector_operand (op1, mode1);
11874 if (! target
11875 || GET_MODE (target) != tmode
11876 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11877 target = gen_reg_rtx (tmode);
11879 /* In case the insn wants input operands in modes different from
11880 the result, abort. */
11881 if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
11882 abort ();
11884 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11885 op0 = copy_to_mode_reg (mode0, op0);
11886 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11887 op1 = copy_to_mode_reg (mode1, op1);
11889 pat = GEN_FCN (icode) (target, op0, op1);
11890 if (! pat)
11891 return 0;
11892 emit_insn (pat);
11893 return target;
11896 /* Subroutine of arm_expand_builtin to take care of unop insns. */
11898 static rtx
11899 arm_expand_unop_builtin (enum insn_code icode,
11900 tree arglist, rtx target, int do_load)
11902 rtx pat;
11903 tree arg0 = TREE_VALUE (arglist);
11904 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11905 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11906 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11908 if (! target
11909 || GET_MODE (target) != tmode
11910 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11911 target = gen_reg_rtx (tmode);
11912 if (do_load)
11913 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11914 else
11916 if (VECTOR_MODE_P (mode0))
11917 op0 = safe_vector_operand (op0, mode0);
11919 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11920 op0 = copy_to_mode_reg (mode0, op0);
11923 pat = GEN_FCN (icode) (target, op0);
11924 if (! pat)
11925 return 0;
11926 emit_insn (pat);
11927 return target;
11930 /* Expand an expression EXP that calls a built-in function,
11931 with result going to TARGET if that's convenient
11932 (and in mode MODE if that's convenient).
11933 SUBTARGET may be used as the target for computing one of EXP's operands.
11934 IGNORE is nonzero if the value is to be ignored. */
11936 static rtx
11937 arm_expand_builtin (tree exp,
11938 rtx target,
11939 rtx subtarget ATTRIBUTE_UNUSED,
11940 enum machine_mode mode ATTRIBUTE_UNUSED,
11941 int ignore ATTRIBUTE_UNUSED)
11943 const struct builtin_description * d;
11944 enum insn_code icode;
11945 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
11946 tree arglist = TREE_OPERAND (exp, 1);
11947 tree arg0;
11948 tree arg1;
11949 tree arg2;
11950 rtx op0;
11951 rtx op1;
11952 rtx op2;
11953 rtx pat;
11954 int fcode = DECL_FUNCTION_CODE (fndecl);
11955 size_t i;
11956 enum machine_mode tmode;
11957 enum machine_mode mode0;
11958 enum machine_mode mode1;
11959 enum machine_mode mode2;
11961 switch (fcode)
11963 case ARM_BUILTIN_TEXTRMSB:
11964 case ARM_BUILTIN_TEXTRMUB:
11965 case ARM_BUILTIN_TEXTRMSH:
11966 case ARM_BUILTIN_TEXTRMUH:
11967 case ARM_BUILTIN_TEXTRMSW:
11968 case ARM_BUILTIN_TEXTRMUW:
11969 icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
11970 : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
11971 : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
11972 : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
11973 : CODE_FOR_iwmmxt_textrmw);
11975 arg0 = TREE_VALUE (arglist);
11976 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11977 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11978 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11979 tmode = insn_data[icode].operand[0].mode;
11980 mode0 = insn_data[icode].operand[1].mode;
11981 mode1 = insn_data[icode].operand[2].mode;
11983 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11984 op0 = copy_to_mode_reg (mode0, op0);
11985 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11987 /* @@@ better error message */
11988 error ("selector must be an immediate");
11989 return gen_reg_rtx (tmode);
11991 if (target == 0
11992 || GET_MODE (target) != tmode
11993 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11994 target = gen_reg_rtx (tmode);
11995 pat = GEN_FCN (icode) (target, op0, op1);
11996 if (! pat)
11997 return 0;
11998 emit_insn (pat);
11999 return target;
12001 case ARM_BUILTIN_TINSRB:
12002 case ARM_BUILTIN_TINSRH:
12003 case ARM_BUILTIN_TINSRW:
12004 icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12005 : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12006 : CODE_FOR_iwmmxt_tinsrw);
12007 arg0 = TREE_VALUE (arglist);
12008 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12009 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12010 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12011 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12012 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12013 tmode = insn_data[icode].operand[0].mode;
12014 mode0 = insn_data[icode].operand[1].mode;
12015 mode1 = insn_data[icode].operand[2].mode;
12016 mode2 = insn_data[icode].operand[3].mode;
12018 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12019 op0 = copy_to_mode_reg (mode0, op0);
12020 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12021 op1 = copy_to_mode_reg (mode1, op1);
12022 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12024 /* @@@ better error message */
12025 error ("selector must be an immediate");
12026 return const0_rtx;
12028 if (target == 0
12029 || GET_MODE (target) != tmode
12030 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12031 target = gen_reg_rtx (tmode);
12032 pat = GEN_FCN (icode) (target, op0, op1, op2);
12033 if (! pat)
12034 return 0;
12035 emit_insn (pat);
12036 return target;
12038 case ARM_BUILTIN_SETWCX:
12039 arg0 = TREE_VALUE (arglist);
12040 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12041 op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12042 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12043 emit_insn (gen_iwmmxt_tmcr (op1, op0));
12044 return 0;
12046 case ARM_BUILTIN_GETWCX:
12047 arg0 = TREE_VALUE (arglist);
12048 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12049 target = gen_reg_rtx (SImode);
12050 emit_insn (gen_iwmmxt_tmrc (target, op0));
12051 return target;
12053 case ARM_BUILTIN_WSHUFH:
12054 icode = CODE_FOR_iwmmxt_wshufh;
12055 arg0 = TREE_VALUE (arglist);
12056 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12057 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12058 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12059 tmode = insn_data[icode].operand[0].mode;
12060 mode1 = insn_data[icode].operand[1].mode;
12061 mode2 = insn_data[icode].operand[2].mode;
12063 if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12064 op0 = copy_to_mode_reg (mode1, op0);
12065 if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12067 /* @@@ better error message */
12068 error ("mask must be an immediate");
12069 return const0_rtx;
12071 if (target == 0
12072 || GET_MODE (target) != tmode
12073 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12074 target = gen_reg_rtx (tmode);
12075 pat = GEN_FCN (icode) (target, op0, op1);
12076 if (! pat)
12077 return 0;
12078 emit_insn (pat);
12079 return target;
12081 case ARM_BUILTIN_WSADB:
12082 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12083 case ARM_BUILTIN_WSADH:
12084 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12085 case ARM_BUILTIN_WSADBZ:
12086 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12087 case ARM_BUILTIN_WSADHZ:
12088 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12090 /* Several three-argument builtins. */
12091 case ARM_BUILTIN_WMACS:
12092 case ARM_BUILTIN_WMACU:
12093 case ARM_BUILTIN_WALIGN:
12094 case ARM_BUILTIN_TMIA:
12095 case ARM_BUILTIN_TMIAPH:
12096 case ARM_BUILTIN_TMIATT:
12097 case ARM_BUILTIN_TMIATB:
12098 case ARM_BUILTIN_TMIABT:
12099 case ARM_BUILTIN_TMIABB:
12100 icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12101 : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12102 : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12103 : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12104 : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12105 : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12106 : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12107 : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12108 : CODE_FOR_iwmmxt_walign);
12109 arg0 = TREE_VALUE (arglist);
12110 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12111 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12112 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12113 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12114 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12115 tmode = insn_data[icode].operand[0].mode;
12116 mode0 = insn_data[icode].operand[1].mode;
12117 mode1 = insn_data[icode].operand[2].mode;
12118 mode2 = insn_data[icode].operand[3].mode;
12120 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12121 op0 = copy_to_mode_reg (mode0, op0);
12122 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12123 op1 = copy_to_mode_reg (mode1, op1);
12124 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12125 op2 = copy_to_mode_reg (mode2, op2);
12126 if (target == 0
12127 || GET_MODE (target) != tmode
12128 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12129 target = gen_reg_rtx (tmode);
12130 pat = GEN_FCN (icode) (target, op0, op1, op2);
12131 if (! pat)
12132 return 0;
12133 emit_insn (pat);
12134 return target;
12136 case ARM_BUILTIN_WZERO:
12137 target = gen_reg_rtx (DImode);
12138 emit_insn (gen_iwmmxt_clrdi (target));
12139 return target;
12141 default:
12142 break;
12145 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12146 if (d->code == (const enum arm_builtins) fcode)
12147 return arm_expand_binop_builtin (d->icode, arglist, target);
12149 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12150 if (d->code == (const enum arm_builtins) fcode)
12151 return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12153 /* @@@ Should really do something sensible here. */
12154 return NULL_RTX;
12157 /* Recursively search through all of the blocks in a function
12158 checking to see if any of the variables created in that
12159 function match the RTX called 'orig'. If they do then
12160 replace them with the RTX called 'new'. */
12161 static void
12162 replace_symbols_in_block (tree block, rtx orig, rtx new)
12164 for (; block; block = BLOCK_CHAIN (block))
12166 tree sym;
12168 if (!TREE_USED (block))
12169 continue;
12171 for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
12173 if ( (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
12174 || DECL_IGNORED_P (sym)
12175 || TREE_CODE (sym) != VAR_DECL
12176 || DECL_EXTERNAL (sym)
12177 || !rtx_equal_p (DECL_RTL (sym), orig)
12179 continue;
12181 SET_DECL_RTL (sym, new);
12184 replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
12188 /* Return the number (counting from 0) of
12189 the least significant set bit in MASK. */
12191 inline static int
12192 number_of_first_bit_set (int mask)
12194 int bit;
12196 for (bit = 0;
12197 (mask & (1 << bit)) == 0;
12198 ++bit)
12199 continue;
12201 return bit;
12204 /* Generate code to return from a thumb function.
12205 If 'reg_containing_return_addr' is -1, then the return address is
12206 actually on the stack, at the stack pointer. */
12207 static void
12208 thumb_exit (FILE *f, int reg_containing_return_addr)
12210 unsigned regs_available_for_popping;
12211 unsigned regs_to_pop;
12212 int pops_needed;
12213 unsigned available;
12214 unsigned required;
12215 int mode;
12216 int size;
12217 int restore_a4 = FALSE;
12219 /* Compute the registers we need to pop. */
12220 regs_to_pop = 0;
12221 pops_needed = 0;
12223 if (reg_containing_return_addr == -1)
12225 regs_to_pop |= 1 << LR_REGNUM;
12226 ++pops_needed;
12229 if (TARGET_BACKTRACE)
12231 /* Restore the (ARM) frame pointer and stack pointer. */
12232 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12233 pops_needed += 2;
12236 /* If there is nothing to pop then just emit the BX instruction and
12237 return. */
12238 if (pops_needed == 0)
12240 if (current_function_calls_eh_return)
12241 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12243 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12244 return;
12246 /* Otherwise if we are not supporting interworking and we have not created
12247 a backtrace structure and the function was not entered in ARM mode then
12248 just pop the return address straight into the PC. */
12249 else if (!TARGET_INTERWORK
12250 && !TARGET_BACKTRACE
12251 && !is_called_in_ARM_mode (current_function_decl)
12252 && !current_function_calls_eh_return)
12254 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12255 return;
12258 /* Find out how many of the (return) argument registers we can corrupt. */
12259 regs_available_for_popping = 0;
12261 /* If returning via __builtin_eh_return, the bottom three registers
12262 all contain information needed for the return. */
12263 if (current_function_calls_eh_return)
12264 size = 12;
12265 else
12267 /* If we can deduce the registers used from the function's
12268 return value. This is more reliable that examining
12269 regs_ever_live[] because that will be set if the register is
12270 ever used in the function, not just if the register is used
12271 to hold a return value. */
12273 if (current_function_return_rtx != 0)
12274 mode = GET_MODE (current_function_return_rtx);
12275 else
12276 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12278 size = GET_MODE_SIZE (mode);
12280 if (size == 0)
12282 /* In a void function we can use any argument register.
12283 In a function that returns a structure on the stack
12284 we can use the second and third argument registers. */
12285 if (mode == VOIDmode)
12286 regs_available_for_popping =
12287 (1 << ARG_REGISTER (1))
12288 | (1 << ARG_REGISTER (2))
12289 | (1 << ARG_REGISTER (3));
12290 else
12291 regs_available_for_popping =
12292 (1 << ARG_REGISTER (2))
12293 | (1 << ARG_REGISTER (3));
12295 else if (size <= 4)
12296 regs_available_for_popping =
12297 (1 << ARG_REGISTER (2))
12298 | (1 << ARG_REGISTER (3));
12299 else if (size <= 8)
12300 regs_available_for_popping =
12301 (1 << ARG_REGISTER (3));
12304 /* Match registers to be popped with registers into which we pop them. */
12305 for (available = regs_available_for_popping,
12306 required = regs_to_pop;
12307 required != 0 && available != 0;
12308 available &= ~(available & - available),
12309 required &= ~(required & - required))
12310 -- pops_needed;
12312 /* If we have any popping registers left over, remove them. */
12313 if (available > 0)
12314 regs_available_for_popping &= ~available;
12316 /* Otherwise if we need another popping register we can use
12317 the fourth argument register. */
12318 else if (pops_needed)
12320 /* If we have not found any free argument registers and
12321 reg a4 contains the return address, we must move it. */
12322 if (regs_available_for_popping == 0
12323 && reg_containing_return_addr == LAST_ARG_REGNUM)
12325 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12326 reg_containing_return_addr = LR_REGNUM;
12328 else if (size > 12)
12330 /* Register a4 is being used to hold part of the return value,
12331 but we have dire need of a free, low register. */
12332 restore_a4 = TRUE;
12334 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12337 if (reg_containing_return_addr != LAST_ARG_REGNUM)
12339 /* The fourth argument register is available. */
12340 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12342 --pops_needed;
12346 /* Pop as many registers as we can. */
12347 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12348 regs_available_for_popping);
12350 /* Process the registers we popped. */
12351 if (reg_containing_return_addr == -1)
12353 /* The return address was popped into the lowest numbered register. */
12354 regs_to_pop &= ~(1 << LR_REGNUM);
12356 reg_containing_return_addr =
12357 number_of_first_bit_set (regs_available_for_popping);
12359 /* Remove this register for the mask of available registers, so that
12360 the return address will not be corrupted by further pops. */
12361 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12364 /* If we popped other registers then handle them here. */
12365 if (regs_available_for_popping)
12367 int frame_pointer;
12369 /* Work out which register currently contains the frame pointer. */
12370 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12372 /* Move it into the correct place. */
12373 asm_fprintf (f, "\tmov\t%r, %r\n",
12374 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12376 /* (Temporarily) remove it from the mask of popped registers. */
12377 regs_available_for_popping &= ~(1 << frame_pointer);
12378 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12380 if (regs_available_for_popping)
12382 int stack_pointer;
12384 /* We popped the stack pointer as well,
12385 find the register that contains it. */
12386 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12388 /* Move it into the stack register. */
12389 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12391 /* At this point we have popped all necessary registers, so
12392 do not worry about restoring regs_available_for_popping
12393 to its correct value:
12395 assert (pops_needed == 0)
12396 assert (regs_available_for_popping == (1 << frame_pointer))
12397 assert (regs_to_pop == (1 << STACK_POINTER)) */
12399 else
12401 /* Since we have just move the popped value into the frame
12402 pointer, the popping register is available for reuse, and
12403 we know that we still have the stack pointer left to pop. */
12404 regs_available_for_popping |= (1 << frame_pointer);
12408 /* If we still have registers left on the stack, but we no longer have
12409 any registers into which we can pop them, then we must move the return
12410 address into the link register and make available the register that
12411 contained it. */
12412 if (regs_available_for_popping == 0 && pops_needed > 0)
12414 regs_available_for_popping |= 1 << reg_containing_return_addr;
12416 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12417 reg_containing_return_addr);
12419 reg_containing_return_addr = LR_REGNUM;
12422 /* If we have registers left on the stack then pop some more.
12423 We know that at most we will want to pop FP and SP. */
12424 if (pops_needed > 0)
12426 int popped_into;
12427 int move_to;
12429 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12430 regs_available_for_popping);
12432 /* We have popped either FP or SP.
12433 Move whichever one it is into the correct register. */
12434 popped_into = number_of_first_bit_set (regs_available_for_popping);
12435 move_to = number_of_first_bit_set (regs_to_pop);
12437 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12439 regs_to_pop &= ~(1 << move_to);
12441 --pops_needed;
12444 /* If we still have not popped everything then we must have only
12445 had one register available to us and we are now popping the SP. */
12446 if (pops_needed > 0)
12448 int popped_into;
12450 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12451 regs_available_for_popping);
12453 popped_into = number_of_first_bit_set (regs_available_for_popping);
12455 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12457 assert (regs_to_pop == (1 << STACK_POINTER))
12458 assert (pops_needed == 1)
12462 /* If necessary restore the a4 register. */
12463 if (restore_a4)
12465 if (reg_containing_return_addr != LR_REGNUM)
12467 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12468 reg_containing_return_addr = LR_REGNUM;
12471 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12474 if (current_function_calls_eh_return)
12475 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12477 /* Return to caller. */
12478 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12481 /* Emit code to push or pop registers to or from the stack. F is the
12482 assembly file. MASK is the registers to push or pop. PUSH is
12483 nonzero if we should push, and zero if we should pop. For debugging
12484 output, if pushing, adjust CFA_OFFSET by the amount of space added
12485 to the stack. REAL_REGS should have the same number of bits set as
12486 MASK, and will be used instead (in the same order) to describe which
12487 registers were saved - this is used to mark the save slots when we
12488 push high registers after moving them to low registers. */
12489 static void
12490 thumb_pushpop (FILE *f, int mask, int push, int *cfa_offset, int real_regs)
12492 int regno;
12493 int lo_mask = mask & 0xFF;
12494 int pushed_words = 0;
12496 if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12498 /* Special case. Do not generate a POP PC statement here, do it in
12499 thumb_exit() */
12500 thumb_exit (f, -1);
12501 return;
12504 fprintf (f, "\t%s\t{", push ? "push" : "pop");
12506 /* Look at the low registers first. */
12507 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12509 if (lo_mask & 1)
12511 asm_fprintf (f, "%r", regno);
12513 if ((lo_mask & ~1) != 0)
12514 fprintf (f, ", ");
12516 pushed_words++;
12520 if (push && (mask & (1 << LR_REGNUM)))
12522 /* Catch pushing the LR. */
12523 if (mask & 0xFF)
12524 fprintf (f, ", ");
12526 asm_fprintf (f, "%r", LR_REGNUM);
12528 pushed_words++;
12530 else if (!push && (mask & (1 << PC_REGNUM)))
12532 /* Catch popping the PC. */
12533 if (TARGET_INTERWORK || TARGET_BACKTRACE
12534 || current_function_calls_eh_return)
12536 /* The PC is never poped directly, instead
12537 it is popped into r3 and then BX is used. */
12538 fprintf (f, "}\n");
12540 thumb_exit (f, -1);
12542 return;
12544 else
12546 if (mask & 0xFF)
12547 fprintf (f, ", ");
12549 asm_fprintf (f, "%r", PC_REGNUM);
12553 fprintf (f, "}\n");
12555 if (push && pushed_words && dwarf2out_do_frame ())
12557 char *l = dwarf2out_cfi_label ();
12558 int pushed_mask = real_regs;
12560 *cfa_offset += pushed_words * 4;
12561 dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12563 pushed_words = 0;
12564 pushed_mask = real_regs;
12565 for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12567 if (pushed_mask & 1)
12568 dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12573 void
12574 thumb_final_prescan_insn (rtx insn)
12576 if (flag_print_asm_name)
12577 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12578 INSN_ADDRESSES (INSN_UID (insn)));
12582 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12584 unsigned HOST_WIDE_INT mask = 0xff;
12585 int i;
12587 if (val == 0) /* XXX */
12588 return 0;
12590 for (i = 0; i < 25; i++)
12591 if ((val & (mask << i)) == val)
12592 return 1;
12594 return 0;
12597 /* Returns nonzero if the current function contains,
12598 or might contain a far jump. */
12599 static int
12600 thumb_far_jump_used_p (void)
12602 rtx insn;
12604 /* This test is only important for leaf functions. */
12605 /* assert (!leaf_function_p ()); */
12607 /* If we have already decided that far jumps may be used,
12608 do not bother checking again, and always return true even if
12609 it turns out that they are not being used. Once we have made
12610 the decision that far jumps are present (and that hence the link
12611 register will be pushed onto the stack) we cannot go back on it. */
12612 if (cfun->machine->far_jump_used)
12613 return 1;
12615 /* If this function is not being called from the prologue/epilogue
12616 generation code then it must be being called from the
12617 INITIAL_ELIMINATION_OFFSET macro. */
12618 if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12620 /* In this case we know that we are being asked about the elimination
12621 of the arg pointer register. If that register is not being used,
12622 then there are no arguments on the stack, and we do not have to
12623 worry that a far jump might force the prologue to push the link
12624 register, changing the stack offsets. In this case we can just
12625 return false, since the presence of far jumps in the function will
12626 not affect stack offsets.
12628 If the arg pointer is live (or if it was live, but has now been
12629 eliminated and so set to dead) then we do have to test to see if
12630 the function might contain a far jump. This test can lead to some
12631 false negatives, since before reload is completed, then length of
12632 branch instructions is not known, so gcc defaults to returning their
12633 longest length, which in turn sets the far jump attribute to true.
12635 A false negative will not result in bad code being generated, but it
12636 will result in a needless push and pop of the link register. We
12637 hope that this does not occur too often.
12639 If we need doubleword stack alignment this could affect the other
12640 elimination offsets so we can't risk getting it wrong. */
12641 if (regs_ever_live [ARG_POINTER_REGNUM])
12642 cfun->machine->arg_pointer_live = 1;
12643 else if (!cfun->machine->arg_pointer_live)
12644 return 0;
12647 /* Check to see if the function contains a branch
12648 insn with the far jump attribute set. */
12649 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12651 if (GET_CODE (insn) == JUMP_INSN
12652 /* Ignore tablejump patterns. */
12653 && GET_CODE (PATTERN (insn)) != ADDR_VEC
12654 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12655 && get_attr_far_jump (insn) == FAR_JUMP_YES
12658 /* Record the fact that we have decided that
12659 the function does use far jumps. */
12660 cfun->machine->far_jump_used = 1;
12661 return 1;
12665 return 0;
12668 /* Return nonzero if FUNC must be entered in ARM mode. */
12670 is_called_in_ARM_mode (tree func)
12672 if (TREE_CODE (func) != FUNCTION_DECL)
12673 abort ();
12675 /* Ignore the problem about functions whoes address is taken. */
12676 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12677 return TRUE;
12679 #ifdef ARM_PE
12680 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12681 #else
12682 return FALSE;
12683 #endif
12686 /* The bits which aren't usefully expanded as rtl. */
12687 const char *
12688 thumb_unexpanded_epilogue (void)
12690 int regno;
12691 int live_regs_mask = 0;
12692 int high_regs_pushed = 0;
12693 int had_to_push_lr;
12694 int size;
12695 int mode;
12697 if (return_used_this_function)
12698 return "";
12700 if (IS_NAKED (arm_current_func_type ()))
12701 return "";
12703 live_regs_mask = thumb_compute_save_reg_mask ();
12704 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
12706 /* If we can deduce the registers used from the function's return value.
12707 This is more reliable that examining regs_ever_live[] because that
12708 will be set if the register is ever used in the function, not just if
12709 the register is used to hold a return value. */
12711 if (current_function_return_rtx != 0)
12712 mode = GET_MODE (current_function_return_rtx);
12713 else
12714 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12716 size = GET_MODE_SIZE (mode);
12718 /* The prolog may have pushed some high registers to use as
12719 work registers. e.g. the testsuite file:
12720 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12721 compiles to produce:
12722 push {r4, r5, r6, r7, lr}
12723 mov r7, r9
12724 mov r6, r8
12725 push {r6, r7}
12726 as part of the prolog. We have to undo that pushing here. */
12728 if (high_regs_pushed)
12730 int mask = live_regs_mask & 0xff;
12731 int next_hi_reg;
12733 /* The available low registers depend on the size of the value we are
12734 returning. */
12735 if (size <= 12)
12736 mask |= 1 << 3;
12737 if (size <= 8)
12738 mask |= 1 << 2;
12740 if (mask == 0)
12741 /* Oh dear! We have no low registers into which we can pop
12742 high registers! */
12743 internal_error
12744 ("no low registers available for popping high registers");
12746 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12747 if (live_regs_mask & (1 << next_hi_reg))
12748 break;
12750 while (high_regs_pushed)
12752 /* Find lo register(s) into which the high register(s) can
12753 be popped. */
12754 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12756 if (mask & (1 << regno))
12757 high_regs_pushed--;
12758 if (high_regs_pushed == 0)
12759 break;
12762 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
12764 /* Pop the values into the low register(s). */
12765 thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
12767 /* Move the value(s) into the high registers. */
12768 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12770 if (mask & (1 << regno))
12772 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12773 regno);
12775 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
12776 if (live_regs_mask & (1 << next_hi_reg))
12777 break;
12781 live_regs_mask &= ~0x0f00;
12784 had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
12785 live_regs_mask &= 0xff;
12787 if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12789 /* Pop the return address into the PC. */
12790 if (had_to_push_lr)
12791 live_regs_mask |= 1 << PC_REGNUM;
12793 /* Either no argument registers were pushed or a backtrace
12794 structure was created which includes an adjusted stack
12795 pointer, so just pop everything. */
12796 if (live_regs_mask)
12797 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12798 live_regs_mask);
12800 /* We have either just popped the return address into the
12801 PC or it is was kept in LR for the entire function. */
12802 if (!had_to_push_lr)
12803 thumb_exit (asm_out_file, LR_REGNUM);
12805 else
12807 /* Pop everything but the return address. */
12808 if (live_regs_mask)
12809 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12810 live_regs_mask);
12812 if (had_to_push_lr)
12814 if (size > 12)
12816 /* We have no free low regs, so save one. */
12817 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
12818 LAST_ARG_REGNUM);
12821 /* Get the return address into a temporary register. */
12822 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
12823 1 << LAST_ARG_REGNUM);
12825 if (size > 12)
12827 /* Move the return address to lr. */
12828 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
12829 LAST_ARG_REGNUM);
12830 /* Restore the low register. */
12831 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
12832 IP_REGNUM);
12833 regno = LR_REGNUM;
12835 else
12836 regno = LAST_ARG_REGNUM;
12838 else
12839 regno = LR_REGNUM;
12841 /* Remove the argument registers that were pushed onto the stack. */
12842 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
12843 SP_REGNUM, SP_REGNUM,
12844 current_function_pretend_args_size);
12846 thumb_exit (asm_out_file, regno);
12849 return "";
12852 /* Functions to save and restore machine-specific function data. */
12853 static struct machine_function *
12854 arm_init_machine_status (void)
12856 struct machine_function *machine;
12857 machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
12859 #if ARM_FT_UNKNOWN != 0
12860 machine->func_type = ARM_FT_UNKNOWN;
12861 #endif
12862 return machine;
12865 /* Return an RTX indicating where the return address to the
12866 calling function can be found. */
12868 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
12870 if (count != 0)
12871 return NULL_RTX;
12873 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
12876 /* Do anything needed before RTL is emitted for each function. */
12877 void
12878 arm_init_expanders (void)
12880 /* Arrange to initialize and mark the machine per-function status. */
12881 init_machine_status = arm_init_machine_status;
12883 /* This is to stop the combine pass optimizing away the alignment
12884 adjustment of va_arg. */
12885 /* ??? It is claimed that this should not be necessary. */
12886 if (cfun)
12887 mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
12891 /* Like arm_compute_initial_elimination offset. Simpler because
12892 THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer. */
12894 HOST_WIDE_INT
12895 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
12897 arm_stack_offsets *offsets;
12899 offsets = arm_get_frame_offsets ();
12901 switch (from)
12903 case ARG_POINTER_REGNUM:
12904 switch (to)
12906 case STACK_POINTER_REGNUM:
12907 return offsets->outgoing_args - offsets->saved_args;
12909 case FRAME_POINTER_REGNUM:
12910 return offsets->soft_frame - offsets->saved_args;
12912 case THUMB_HARD_FRAME_POINTER_REGNUM:
12913 case ARM_HARD_FRAME_POINTER_REGNUM:
12914 return offsets->saved_regs - offsets->saved_args;
12916 default:
12917 abort();
12919 break;
12921 case FRAME_POINTER_REGNUM:
12922 switch (to)
12924 case STACK_POINTER_REGNUM:
12925 return offsets->outgoing_args - offsets->soft_frame;
12927 case THUMB_HARD_FRAME_POINTER_REGNUM:
12928 case ARM_HARD_FRAME_POINTER_REGNUM:
12929 return offsets->saved_regs - offsets->soft_frame;
12931 default:
12932 abort();
12934 break;
12936 default:
12937 abort ();
12942 /* Generate the rest of a function's prologue. */
12943 void
12944 thumb_expand_prologue (void)
12946 rtx insn, dwarf;
12948 HOST_WIDE_INT amount;
12949 arm_stack_offsets *offsets;
12950 unsigned long func_type;
12951 int regno;
12952 unsigned long live_regs_mask;
12954 func_type = arm_current_func_type ();
12956 /* Naked functions don't have prologues. */
12957 if (IS_NAKED (func_type))
12958 return;
12960 if (IS_INTERRUPT (func_type))
12962 error ("interrupt Service Routines cannot be coded in Thumb mode");
12963 return;
12966 live_regs_mask = thumb_compute_save_reg_mask ();
12967 /* Load the pic register before setting the frame pointer, so we can use r7
12968 as a temporary work register. */
12969 if (flag_pic)
12970 arm_load_pic_register (thumb_find_work_register (live_regs_mask));
12972 offsets = arm_get_frame_offsets ();
12974 if (frame_pointer_needed)
12976 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
12977 stack_pointer_rtx));
12978 RTX_FRAME_RELATED_P (insn) = 1;
12981 amount = offsets->outgoing_args - offsets->saved_regs;
12982 if (amount)
12984 if (amount < 512)
12986 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
12987 GEN_INT (- amount)));
12988 RTX_FRAME_RELATED_P (insn) = 1;
12990 else
12992 rtx reg;
12994 /* The stack decrement is too big for an immediate value in a single
12995 insn. In theory we could issue multiple subtracts, but after
12996 three of them it becomes more space efficient to place the full
12997 value in the constant pool and load into a register. (Also the
12998 ARM debugger really likes to see only one stack decrement per
12999 function). So instead we look for a scratch register into which
13000 we can load the decrement, and then we subtract this from the
13001 stack pointer. Unfortunately on the thumb the only available
13002 scratch registers are the argument registers, and we cannot use
13003 these as they may hold arguments to the function. Instead we
13004 attempt to locate a call preserved register which is used by this
13005 function. If we can find one, then we know that it will have
13006 been pushed at the start of the prologue and so we can corrupt
13007 it now. */
13008 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13009 if (live_regs_mask & (1 << regno)
13010 && !(frame_pointer_needed
13011 && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13012 break;
13014 if (regno > LAST_LO_REGNUM) /* Very unlikely. */
13016 rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13018 /* Choose an arbitrary, non-argument low register. */
13019 reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13021 /* Save it by copying it into a high, scratch register. */
13022 emit_insn (gen_movsi (spare, reg));
13023 /* Add a USE to stop propagate_one_insn() from barfing. */
13024 emit_insn (gen_prologue_use (spare));
13026 /* Decrement the stack. */
13027 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13028 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13029 stack_pointer_rtx, reg));
13030 RTX_FRAME_RELATED_P (insn) = 1;
13031 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13032 plus_constant (stack_pointer_rtx,
13033 -amount));
13034 RTX_FRAME_RELATED_P (dwarf) = 1;
13035 REG_NOTES (insn)
13036 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13037 REG_NOTES (insn));
13039 /* Restore the low register's original value. */
13040 emit_insn (gen_movsi (reg, spare));
13042 /* Emit a USE of the restored scratch register, so that flow
13043 analysis will not consider the restore redundant. The
13044 register won't be used again in this function and isn't
13045 restored by the epilogue. */
13046 emit_insn (gen_prologue_use (reg));
13048 else
13050 reg = gen_rtx_REG (SImode, regno);
13052 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13054 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13055 stack_pointer_rtx, reg));
13056 RTX_FRAME_RELATED_P (insn) = 1;
13057 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13058 plus_constant (stack_pointer_rtx,
13059 -amount));
13060 RTX_FRAME_RELATED_P (dwarf) = 1;
13061 REG_NOTES (insn)
13062 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13063 REG_NOTES (insn));
13066 /* If the frame pointer is needed, emit a special barrier that
13067 will prevent the scheduler from moving stores to the frame
13068 before the stack adjustment. */
13069 if (frame_pointer_needed)
13070 emit_insn (gen_stack_tie (stack_pointer_rtx,
13071 hard_frame_pointer_rtx));
13074 if (current_function_profile || TARGET_NO_SCHED_PRO)
13075 emit_insn (gen_blockage ());
13077 cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13078 if (live_regs_mask & 0xff)
13079 cfun->machine->lr_save_eliminated = 0;
13081 /* If the link register is being kept alive, with the return address in it,
13082 then make sure that it does not get reused by the ce2 pass. */
13083 if (cfun->machine->lr_save_eliminated)
13084 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13088 void
13089 thumb_expand_epilogue (void)
13091 HOST_WIDE_INT amount;
13092 arm_stack_offsets *offsets;
13093 int regno;
13095 /* Naked functions don't have prologues. */
13096 if (IS_NAKED (arm_current_func_type ()))
13097 return;
13099 offsets = arm_get_frame_offsets ();
13100 amount = offsets->outgoing_args - offsets->saved_regs;
13102 if (frame_pointer_needed)
13103 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13104 else if (amount)
13106 if (amount < 512)
13107 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13108 GEN_INT (amount)));
13109 else
13111 /* r3 is always free in the epilogue. */
13112 rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13114 emit_insn (gen_movsi (reg, GEN_INT (amount)));
13115 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13119 /* Emit a USE (stack_pointer_rtx), so that
13120 the stack adjustment will not be deleted. */
13121 emit_insn (gen_prologue_use (stack_pointer_rtx));
13123 if (current_function_profile || TARGET_NO_SCHED_PRO)
13124 emit_insn (gen_blockage ());
13126 /* Emit a clobber for each insn that will be restored in the epilogue,
13127 so that flow2 will get register lifetimes correct. */
13128 for (regno = 0; regno < 13; regno++)
13129 if (regs_ever_live[regno] && !call_used_regs[regno])
13130 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13132 if (! regs_ever_live[LR_REGNUM])
13133 emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13136 static void
13137 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13139 int live_regs_mask = 0;
13140 int l_mask;
13141 int high_regs_pushed = 0;
13142 int cfa_offset = 0;
13143 int regno;
13145 if (IS_NAKED (arm_current_func_type ()))
13146 return;
13148 if (is_called_in_ARM_mode (current_function_decl))
13150 const char * name;
13152 if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
13153 abort ();
13154 if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
13155 abort ();
13156 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
13158 /* Generate code sequence to switch us into Thumb mode. */
13159 /* The .code 32 directive has already been emitted by
13160 ASM_DECLARE_FUNCTION_NAME. */
13161 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13162 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13164 /* Generate a label, so that the debugger will notice the
13165 change in instruction sets. This label is also used by
13166 the assembler to bypass the ARM code when this function
13167 is called from a Thumb encoded function elsewhere in the
13168 same file. Hence the definition of STUB_NAME here must
13169 agree with the definition in gas/config/tc-arm.c. */
13171 #define STUB_NAME ".real_start_of"
13173 fprintf (f, "\t.code\t16\n");
13174 #ifdef ARM_PE
13175 if (arm_dllexport_name_p (name))
13176 name = arm_strip_name_encoding (name);
13177 #endif
13178 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13179 fprintf (f, "\t.thumb_func\n");
13180 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13183 if (current_function_pretend_args_size)
13185 if (cfun->machine->uses_anonymous_args)
13187 int num_pushes;
13189 fprintf (f, "\tpush\t{");
13191 num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13193 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13194 regno <= LAST_ARG_REGNUM;
13195 regno++)
13196 asm_fprintf (f, "%r%s", regno,
13197 regno == LAST_ARG_REGNUM ? "" : ", ");
13199 fprintf (f, "}\n");
13201 else
13202 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13203 SP_REGNUM, SP_REGNUM,
13204 current_function_pretend_args_size);
13206 /* We don't need to record the stores for unwinding (would it
13207 help the debugger any if we did?), but record the change in
13208 the stack pointer. */
13209 if (dwarf2out_do_frame ())
13211 char *l = dwarf2out_cfi_label ();
13212 cfa_offset = cfa_offset + current_function_pretend_args_size;
13213 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13217 live_regs_mask = thumb_compute_save_reg_mask ();
13218 /* Just low regs and lr. */
13219 l_mask = live_regs_mask & 0x40ff;
13221 if (TARGET_BACKTRACE)
13223 int offset;
13224 int work_register;
13226 /* We have been asked to create a stack backtrace structure.
13227 The code looks like this:
13229 0 .align 2
13230 0 func:
13231 0 sub SP, #16 Reserve space for 4 registers.
13232 2 push {R7} Push low registers.
13233 4 add R7, SP, #20 Get the stack pointer before the push.
13234 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
13235 8 mov R7, PC Get hold of the start of this code plus 12.
13236 10 str R7, [SP, #16] Store it.
13237 12 mov R7, FP Get hold of the current frame pointer.
13238 14 str R7, [SP, #4] Store it.
13239 16 mov R7, LR Get hold of the current return address.
13240 18 str R7, [SP, #12] Store it.
13241 20 add R7, SP, #16 Point at the start of the backtrace structure.
13242 22 mov FP, R7 Put this value into the frame pointer. */
13244 work_register = thumb_find_work_register (live_regs_mask);
13246 asm_fprintf
13247 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13248 SP_REGNUM, SP_REGNUM);
13250 if (dwarf2out_do_frame ())
13252 char *l = dwarf2out_cfi_label ();
13253 cfa_offset = cfa_offset + 16;
13254 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13257 if (l_mask)
13259 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13260 offset = bit_count (l_mask);
13262 else
13263 offset = 0;
13265 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13266 offset + 16 + current_function_pretend_args_size);
13268 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13269 offset + 4);
13271 /* Make sure that the instruction fetching the PC is in the right place
13272 to calculate "start of backtrace creation code + 12". */
13273 if (l_mask)
13275 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13276 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13277 offset + 12);
13278 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13279 ARM_HARD_FRAME_POINTER_REGNUM);
13280 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13281 offset);
13283 else
13285 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13286 ARM_HARD_FRAME_POINTER_REGNUM);
13287 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13288 offset);
13289 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13290 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13291 offset + 12);
13294 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13295 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13296 offset + 8);
13297 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13298 offset + 12);
13299 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13300 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13302 else if (l_mask)
13303 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13305 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13307 if (high_regs_pushed)
13309 int pushable_regs = 0;
13310 int next_hi_reg;
13312 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13313 if (live_regs_mask & (1 << next_hi_reg))
13314 break;
13316 pushable_regs = l_mask & 0xff;
13318 if (pushable_regs == 0)
13319 pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
13321 while (high_regs_pushed > 0)
13323 int real_regs_mask = 0;
13325 for (regno = LAST_LO_REGNUM; regno >= 0; regno--)
13327 if (pushable_regs & (1 << regno))
13329 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13331 high_regs_pushed--;
13332 real_regs_mask |= (1 << next_hi_reg);
13334 if (high_regs_pushed)
13336 for (next_hi_reg--; next_hi_reg > LAST_LO_REGNUM;
13337 next_hi_reg--)
13338 if (live_regs_mask & (1 << next_hi_reg))
13339 break;
13341 else
13343 pushable_regs &= ~((1 << regno) - 1);
13344 break;
13349 thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
13354 /* Handle the case of a double word load into a low register from
13355 a computed memory address. The computed address may involve a
13356 register which is overwritten by the load. */
13357 const char *
13358 thumb_load_double_from_address (rtx *operands)
13360 rtx addr;
13361 rtx base;
13362 rtx offset;
13363 rtx arg1;
13364 rtx arg2;
13366 if (GET_CODE (operands[0]) != REG)
13367 abort ();
13369 if (GET_CODE (operands[1]) != MEM)
13370 abort ();
13372 /* Get the memory address. */
13373 addr = XEXP (operands[1], 0);
13375 /* Work out how the memory address is computed. */
13376 switch (GET_CODE (addr))
13378 case REG:
13379 operands[2] = gen_rtx_MEM (SImode,
13380 plus_constant (XEXP (operands[1], 0), 4));
13382 if (REGNO (operands[0]) == REGNO (addr))
13384 output_asm_insn ("ldr\t%H0, %2", operands);
13385 output_asm_insn ("ldr\t%0, %1", operands);
13387 else
13389 output_asm_insn ("ldr\t%0, %1", operands);
13390 output_asm_insn ("ldr\t%H0, %2", operands);
13392 break;
13394 case CONST:
13395 /* Compute <address> + 4 for the high order load. */
13396 operands[2] = gen_rtx_MEM (SImode,
13397 plus_constant (XEXP (operands[1], 0), 4));
13399 output_asm_insn ("ldr\t%0, %1", operands);
13400 output_asm_insn ("ldr\t%H0, %2", operands);
13401 break;
13403 case PLUS:
13404 arg1 = XEXP (addr, 0);
13405 arg2 = XEXP (addr, 1);
13407 if (CONSTANT_P (arg1))
13408 base = arg2, offset = arg1;
13409 else
13410 base = arg1, offset = arg2;
13412 if (GET_CODE (base) != REG)
13413 abort ();
13415 /* Catch the case of <address> = <reg> + <reg> */
13416 if (GET_CODE (offset) == REG)
13418 int reg_offset = REGNO (offset);
13419 int reg_base = REGNO (base);
13420 int reg_dest = REGNO (operands[0]);
13422 /* Add the base and offset registers together into the
13423 higher destination register. */
13424 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13425 reg_dest + 1, reg_base, reg_offset);
13427 /* Load the lower destination register from the address in
13428 the higher destination register. */
13429 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13430 reg_dest, reg_dest + 1);
13432 /* Load the higher destination register from its own address
13433 plus 4. */
13434 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13435 reg_dest + 1, reg_dest + 1);
13437 else
13439 /* Compute <address> + 4 for the high order load. */
13440 operands[2] = gen_rtx_MEM (SImode,
13441 plus_constant (XEXP (operands[1], 0), 4));
13443 /* If the computed address is held in the low order register
13444 then load the high order register first, otherwise always
13445 load the low order register first. */
13446 if (REGNO (operands[0]) == REGNO (base))
13448 output_asm_insn ("ldr\t%H0, %2", operands);
13449 output_asm_insn ("ldr\t%0, %1", operands);
13451 else
13453 output_asm_insn ("ldr\t%0, %1", operands);
13454 output_asm_insn ("ldr\t%H0, %2", operands);
13457 break;
13459 case LABEL_REF:
13460 /* With no registers to worry about we can just load the value
13461 directly. */
13462 operands[2] = gen_rtx_MEM (SImode,
13463 plus_constant (XEXP (operands[1], 0), 4));
13465 output_asm_insn ("ldr\t%H0, %2", operands);
13466 output_asm_insn ("ldr\t%0, %1", operands);
13467 break;
13469 default:
13470 abort ();
13471 break;
13474 return "";
13477 const char *
13478 thumb_output_move_mem_multiple (int n, rtx *operands)
13480 rtx tmp;
13482 switch (n)
13484 case 2:
13485 if (REGNO (operands[4]) > REGNO (operands[5]))
13487 tmp = operands[4];
13488 operands[4] = operands[5];
13489 operands[5] = tmp;
13491 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13492 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13493 break;
13495 case 3:
13496 if (REGNO (operands[4]) > REGNO (operands[5]))
13498 tmp = operands[4];
13499 operands[4] = operands[5];
13500 operands[5] = tmp;
13502 if (REGNO (operands[5]) > REGNO (operands[6]))
13504 tmp = operands[5];
13505 operands[5] = operands[6];
13506 operands[6] = tmp;
13508 if (REGNO (operands[4]) > REGNO (operands[5]))
13510 tmp = operands[4];
13511 operands[4] = operands[5];
13512 operands[5] = tmp;
13515 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13516 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13517 break;
13519 default:
13520 abort ();
13523 return "";
13526 /* Routines for generating rtl. */
13527 void
13528 thumb_expand_movmemqi (rtx *operands)
13530 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13531 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13532 HOST_WIDE_INT len = INTVAL (operands[2]);
13533 HOST_WIDE_INT offset = 0;
13535 while (len >= 12)
13537 emit_insn (gen_movmem12b (out, in, out, in));
13538 len -= 12;
13541 if (len >= 8)
13543 emit_insn (gen_movmem8b (out, in, out, in));
13544 len -= 8;
13547 if (len >= 4)
13549 rtx reg = gen_reg_rtx (SImode);
13550 emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13551 emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13552 len -= 4;
13553 offset += 4;
13556 if (len >= 2)
13558 rtx reg = gen_reg_rtx (HImode);
13559 emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13560 plus_constant (in, offset))));
13561 emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13562 reg));
13563 len -= 2;
13564 offset += 2;
13567 if (len)
13569 rtx reg = gen_reg_rtx (QImode);
13570 emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13571 plus_constant (in, offset))));
13572 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13573 reg));
13577 void
13578 thumb_reload_out_hi (rtx *operands)
13580 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13583 /* Handle reading a half-word from memory during reload. */
13584 void
13585 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13587 abort ();
13590 /* Return the length of a function name prefix
13591 that starts with the character 'c'. */
13592 static int
13593 arm_get_strip_length (int c)
13595 switch (c)
13597 ARM_NAME_ENCODING_LENGTHS
13598 default: return 0;
13602 /* Return a pointer to a function's name with any
13603 and all prefix encodings stripped from it. */
13604 const char *
13605 arm_strip_name_encoding (const char *name)
13607 int skip;
13609 while ((skip = arm_get_strip_length (* name)))
13610 name += skip;
13612 return name;
13615 /* If there is a '*' anywhere in the name's prefix, then
13616 emit the stripped name verbatim, otherwise prepend an
13617 underscore if leading underscores are being used. */
13618 void
13619 arm_asm_output_labelref (FILE *stream, const char *name)
13621 int skip;
13622 int verbatim = 0;
13624 while ((skip = arm_get_strip_length (* name)))
13626 verbatim |= (*name == '*');
13627 name += skip;
13630 if (verbatim)
13631 fputs (name, stream);
13632 else
13633 asm_fprintf (stream, "%U%s", name);
13636 rtx aof_pic_label;
13638 #ifdef AOF_ASSEMBLER
13639 /* Special functions only needed when producing AOF syntax assembler. */
13641 struct pic_chain
13643 struct pic_chain * next;
13644 const char * symname;
13647 static struct pic_chain * aof_pic_chain = NULL;
13650 aof_pic_entry (rtx x)
13652 struct pic_chain ** chainp;
13653 int offset;
13655 if (aof_pic_label == NULL_RTX)
13657 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13660 for (offset = 0, chainp = &aof_pic_chain; *chainp;
13661 offset += 4, chainp = &(*chainp)->next)
13662 if ((*chainp)->symname == XSTR (x, 0))
13663 return plus_constant (aof_pic_label, offset);
13665 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13666 (*chainp)->next = NULL;
13667 (*chainp)->symname = XSTR (x, 0);
13668 return plus_constant (aof_pic_label, offset);
13671 void
13672 aof_dump_pic_table (FILE *f)
13674 struct pic_chain * chain;
13676 if (aof_pic_chain == NULL)
13677 return;
13679 asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13680 PIC_OFFSET_TABLE_REGNUM,
13681 PIC_OFFSET_TABLE_REGNUM);
13682 fputs ("|x$adcons|\n", f);
13684 for (chain = aof_pic_chain; chain; chain = chain->next)
13686 fputs ("\tDCD\t", f);
13687 assemble_name (f, chain->symname);
13688 fputs ("\n", f);
13692 int arm_text_section_count = 1;
13694 char *
13695 aof_text_section (void )
13697 static char buf[100];
13698 sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13699 arm_text_section_count++);
13700 if (flag_pic)
13701 strcat (buf, ", PIC, REENTRANT");
13702 return buf;
13705 static int arm_data_section_count = 1;
13707 char *
13708 aof_data_section (void)
13710 static char buf[100];
13711 sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13712 return buf;
13715 /* The AOF assembler is religiously strict about declarations of
13716 imported and exported symbols, so that it is impossible to declare
13717 a function as imported near the beginning of the file, and then to
13718 export it later on. It is, however, possible to delay the decision
13719 until all the functions in the file have been compiled. To get
13720 around this, we maintain a list of the imports and exports, and
13721 delete from it any that are subsequently defined. At the end of
13722 compilation we spit the remainder of the list out before the END
13723 directive. */
13725 struct import
13727 struct import * next;
13728 const char * name;
13731 static struct import * imports_list = NULL;
13733 void
13734 aof_add_import (const char *name)
13736 struct import * new;
13738 for (new = imports_list; new; new = new->next)
13739 if (new->name == name)
13740 return;
13742 new = (struct import *) xmalloc (sizeof (struct import));
13743 new->next = imports_list;
13744 imports_list = new;
13745 new->name = name;
13748 void
13749 aof_delete_import (const char *name)
13751 struct import ** old;
13753 for (old = &imports_list; *old; old = & (*old)->next)
13755 if ((*old)->name == name)
13757 *old = (*old)->next;
13758 return;
13763 int arm_main_function = 0;
13765 static void
13766 aof_dump_imports (FILE *f)
13768 /* The AOF assembler needs this to cause the startup code to be extracted
13769 from the library. Brining in __main causes the whole thing to work
13770 automagically. */
13771 if (arm_main_function)
13773 text_section ();
13774 fputs ("\tIMPORT __main\n", f);
13775 fputs ("\tDCD __main\n", f);
13778 /* Now dump the remaining imports. */
13779 while (imports_list)
13781 fprintf (f, "\tIMPORT\t");
13782 assemble_name (f, imports_list->name);
13783 fputc ('\n', f);
13784 imports_list = imports_list->next;
13788 static void
13789 aof_globalize_label (FILE *stream, const char *name)
13791 default_globalize_label (stream, name);
13792 if (! strcmp (name, "main"))
13793 arm_main_function = 1;
13796 static void
13797 aof_file_start (void)
13799 fputs ("__r0\tRN\t0\n", asm_out_file);
13800 fputs ("__a1\tRN\t0\n", asm_out_file);
13801 fputs ("__a2\tRN\t1\n", asm_out_file);
13802 fputs ("__a3\tRN\t2\n", asm_out_file);
13803 fputs ("__a4\tRN\t3\n", asm_out_file);
13804 fputs ("__v1\tRN\t4\n", asm_out_file);
13805 fputs ("__v2\tRN\t5\n", asm_out_file);
13806 fputs ("__v3\tRN\t6\n", asm_out_file);
13807 fputs ("__v4\tRN\t7\n", asm_out_file);
13808 fputs ("__v5\tRN\t8\n", asm_out_file);
13809 fputs ("__v6\tRN\t9\n", asm_out_file);
13810 fputs ("__sl\tRN\t10\n", asm_out_file);
13811 fputs ("__fp\tRN\t11\n", asm_out_file);
13812 fputs ("__ip\tRN\t12\n", asm_out_file);
13813 fputs ("__sp\tRN\t13\n", asm_out_file);
13814 fputs ("__lr\tRN\t14\n", asm_out_file);
13815 fputs ("__pc\tRN\t15\n", asm_out_file);
13816 fputs ("__f0\tFN\t0\n", asm_out_file);
13817 fputs ("__f1\tFN\t1\n", asm_out_file);
13818 fputs ("__f2\tFN\t2\n", asm_out_file);
13819 fputs ("__f3\tFN\t3\n", asm_out_file);
13820 fputs ("__f4\tFN\t4\n", asm_out_file);
13821 fputs ("__f5\tFN\t5\n", asm_out_file);
13822 fputs ("__f6\tFN\t6\n", asm_out_file);
13823 fputs ("__f7\tFN\t7\n", asm_out_file);
13824 text_section ();
13827 static void
13828 aof_file_end (void)
13830 if (flag_pic)
13831 aof_dump_pic_table (asm_out_file);
13832 aof_dump_imports (asm_out_file);
13833 fputs ("\tEND\n", asm_out_file);
13835 #endif /* AOF_ASSEMBLER */
13837 #ifndef ARM_PE
13838 /* Symbols in the text segment can be accessed without indirecting via the
13839 constant pool; it may take an extra binary operation, but this is still
13840 faster than indirecting via memory. Don't do this when not optimizing,
13841 since we won't be calculating al of the offsets necessary to do this
13842 simplification. */
13844 static void
13845 arm_encode_section_info (tree decl, rtx rtl, int first)
13847 /* This doesn't work with AOF syntax, since the string table may be in
13848 a different AREA. */
13849 #ifndef AOF_ASSEMBLER
13850 if (optimize > 0 && TREE_CONSTANT (decl))
13851 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
13852 #endif
13854 /* If we are referencing a function that is weak then encode a long call
13855 flag in the function name, otherwise if the function is static or
13856 or known to be defined in this file then encode a short call flag. */
13857 if (first && DECL_P (decl))
13859 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
13860 arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
13861 else if (! TREE_PUBLIC (decl))
13862 arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
13865 #endif /* !ARM_PE */
13867 static void
13868 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
13870 if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
13871 && !strcmp (prefix, "L"))
13873 arm_ccfsm_state = 0;
13874 arm_target_insn = NULL;
13876 default_internal_label (stream, prefix, labelno);
13879 /* Output code to add DELTA to the first argument, and then jump
13880 to FUNCTION. Used for C++ multiple inheritance. */
13881 static void
13882 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
13883 HOST_WIDE_INT delta,
13884 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
13885 tree function)
13887 static int thunk_label = 0;
13888 char label[256];
13889 int mi_delta = delta;
13890 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
13891 int shift = 0;
13892 int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
13893 ? 1 : 0);
13894 if (mi_delta < 0)
13895 mi_delta = - mi_delta;
13896 if (TARGET_THUMB)
13898 int labelno = thunk_label++;
13899 ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
13900 fputs ("\tldr\tr12, ", file);
13901 assemble_name (file, label);
13902 fputc ('\n', file);
13904 while (mi_delta != 0)
13906 if ((mi_delta & (3 << shift)) == 0)
13907 shift += 2;
13908 else
13910 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
13911 mi_op, this_regno, this_regno,
13912 mi_delta & (0xff << shift));
13913 mi_delta &= ~(0xff << shift);
13914 shift += 8;
13917 if (TARGET_THUMB)
13919 fprintf (file, "\tbx\tr12\n");
13920 ASM_OUTPUT_ALIGN (file, 2);
13921 assemble_name (file, label);
13922 fputs (":\n", file);
13923 assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
13925 else
13927 fputs ("\tb\t", file);
13928 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
13929 if (NEED_PLT_RELOC)
13930 fputs ("(PLT)", file);
13931 fputc ('\n', file);
13936 arm_emit_vector_const (FILE *file, rtx x)
13938 int i;
13939 const char * pattern;
13941 if (GET_CODE (x) != CONST_VECTOR)
13942 abort ();
13944 switch (GET_MODE (x))
13946 case V2SImode: pattern = "%08x"; break;
13947 case V4HImode: pattern = "%04x"; break;
13948 case V8QImode: pattern = "%02x"; break;
13949 default: abort ();
13952 fprintf (file, "0x");
13953 for (i = CONST_VECTOR_NUNITS (x); i--;)
13955 rtx element;
13957 element = CONST_VECTOR_ELT (x, i);
13958 fprintf (file, pattern, INTVAL (element));
13961 return 1;
13964 const char *
13965 arm_output_load_gr (rtx *operands)
13967 rtx reg;
13968 rtx offset;
13969 rtx wcgr;
13970 rtx sum;
13972 if (GET_CODE (operands [1]) != MEM
13973 || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
13974 || GET_CODE (reg = XEXP (sum, 0)) != REG
13975 || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
13976 || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
13977 return "wldrw%?\t%0, %1";
13979 /* Fix up an out-of-range load of a GR register. */
13980 output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
13981 wcgr = operands[0];
13982 operands[0] = reg;
13983 output_asm_insn ("ldr%?\t%0, %1", operands);
13985 operands[0] = wcgr;
13986 operands[1] = reg;
13987 output_asm_insn ("tmcr%?\t%0, %1", operands);
13988 output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
13990 return "";
13993 static rtx
13994 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
13995 int incoming ATTRIBUTE_UNUSED)
13997 #if 0
13998 /* FIXME: The ARM backend has special code to handle structure
13999 returns, and will reserve its own hidden first argument. So
14000 if this macro is enabled a *second* hidden argument will be
14001 reserved, which will break binary compatibility with old
14002 toolchains and also thunk handling. One day this should be
14003 fixed. */
14004 return 0;
14005 #else
14006 /* Register in which address to store a structure value
14007 is passed to a function. */
14008 return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14009 #endif
14012 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14014 On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14015 named arg and all anonymous args onto the stack.
14016 XXX I know the prologue shouldn't be pushing registers, but it is faster
14017 that way. */
14019 static void
14020 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14021 enum machine_mode mode ATTRIBUTE_UNUSED,
14022 tree type ATTRIBUTE_UNUSED,
14023 int *pretend_size,
14024 int second_time ATTRIBUTE_UNUSED)
14026 cfun->machine->uses_anonymous_args = 1;
14027 if (cum->nregs < NUM_ARG_REGS)
14028 *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14031 /* Return nonzero if the CONSUMER instruction (a store) does not need
14032 PRODUCER's value to calculate the address. */
14035 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14037 rtx value = PATTERN (producer);
14038 rtx addr = PATTERN (consumer);
14040 if (GET_CODE (value) == COND_EXEC)
14041 value = COND_EXEC_CODE (value);
14042 if (GET_CODE (value) == PARALLEL)
14043 value = XVECEXP (value, 0, 0);
14044 value = XEXP (value, 0);
14045 if (GET_CODE (addr) == COND_EXEC)
14046 addr = COND_EXEC_CODE (addr);
14047 if (GET_CODE (addr) == PARALLEL)
14048 addr = XVECEXP (addr, 0, 0);
14049 addr = XEXP (addr, 0);
14051 return !reg_overlap_mentioned_p (value, addr);
14054 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14055 have an early register shift value or amount dependency on the
14056 result of PRODUCER. */
14059 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14061 rtx value = PATTERN (producer);
14062 rtx op = PATTERN (consumer);
14063 rtx early_op;
14065 if (GET_CODE (value) == COND_EXEC)
14066 value = COND_EXEC_CODE (value);
14067 if (GET_CODE (value) == PARALLEL)
14068 value = XVECEXP (value, 0, 0);
14069 value = XEXP (value, 0);
14070 if (GET_CODE (op) == COND_EXEC)
14071 op = COND_EXEC_CODE (op);
14072 if (GET_CODE (op) == PARALLEL)
14073 op = XVECEXP (op, 0, 0);
14074 op = XEXP (op, 1);
14076 early_op = XEXP (op, 0);
14077 /* This is either an actual independent shift, or a shift applied to
14078 the first operand of another operation. We want the whole shift
14079 operation. */
14080 if (GET_CODE (early_op) == REG)
14081 early_op = op;
14083 return !reg_overlap_mentioned_p (value, early_op);
14086 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14087 have an early register shift value dependency on the result of
14088 PRODUCER. */
14091 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14093 rtx value = PATTERN (producer);
14094 rtx op = PATTERN (consumer);
14095 rtx early_op;
14097 if (GET_CODE (value) == COND_EXEC)
14098 value = COND_EXEC_CODE (value);
14099 if (GET_CODE (value) == PARALLEL)
14100 value = XVECEXP (value, 0, 0);
14101 value = XEXP (value, 0);
14102 if (GET_CODE (op) == COND_EXEC)
14103 op = COND_EXEC_CODE (op);
14104 if (GET_CODE (op) == PARALLEL)
14105 op = XVECEXP (op, 0, 0);
14106 op = XEXP (op, 1);
14108 early_op = XEXP (op, 0);
14110 /* This is either an actual independent shift, or a shift applied to
14111 the first operand of another operation. We want the value being
14112 shifted, in either case. */
14113 if (GET_CODE (early_op) != REG)
14114 early_op = XEXP (early_op, 0);
14116 return !reg_overlap_mentioned_p (value, early_op);
14119 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14120 have an early register mult dependency on the result of
14121 PRODUCER. */
14124 arm_no_early_mul_dep (rtx producer, rtx consumer)
14126 rtx value = PATTERN (producer);
14127 rtx op = PATTERN (consumer);
14129 if (GET_CODE (value) == COND_EXEC)
14130 value = COND_EXEC_CODE (value);
14131 if (GET_CODE (value) == PARALLEL)
14132 value = XVECEXP (value, 0, 0);
14133 value = XEXP (value, 0);
14134 if (GET_CODE (op) == COND_EXEC)
14135 op = COND_EXEC_CODE (op);
14136 if (GET_CODE (op) == PARALLEL)
14137 op = XVECEXP (op, 0, 0);
14138 op = XEXP (op, 1);
14140 return (GET_CODE (op) == PLUS
14141 && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14145 /* We can't rely on the caller doing the proper promotion when
14146 using APCS or ATPCS. */
14148 static bool
14149 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14151 return !TARGET_AAPCS_BASED;
14155 /* AAPCS based ABIs use short enums by default. */
14157 static bool
14158 arm_default_short_enums (void)
14160 return TARGET_AAPCS_BASED;
14164 /* AAPCS requires that anonymous bitfields affect structure alignment. */
14166 static bool
14167 arm_align_anon_bitfield (void)
14169 return TARGET_AAPCS_BASED;
14173 /* The generic C++ ABI says 64-bit (long long). The EABI says 32-bit. */
14175 static tree
14176 arm_cxx_guard_type (void)
14178 return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14182 /* The EABI says test the least significan bit of a guard variable. */
14184 static bool
14185 arm_cxx_guard_mask_bit (void)
14187 return TARGET_AAPCS_BASED;
14191 /* The EABI specifies that all array cookies are 8 bytes long. */
14193 static tree
14194 arm_get_cookie_size (tree type)
14196 tree size;
14198 if (!TARGET_AAPCS_BASED)
14199 return default_cxx_get_cookie_size (type);
14201 size = build_int_cst (sizetype, 8);
14202 return size;
14206 /* The EABI says that array cookies should also contain the element size. */
14208 static bool
14209 arm_cookie_has_size (void)
14211 return TARGET_AAPCS_BASED;
14215 /* The EABI says constructors and destructors should return a pointer to
14216 the object constructed/destroyed. */
14218 static bool
14219 arm_cxx_cdtor_returns_this (void)
14221 return TARGET_AAPCS_BASED;
14224 /* The EABI says that an inline function may never be the key
14225 method. */
14227 static bool
14228 arm_cxx_key_method_may_be_inline (void)
14230 return !TARGET_AAPCS_BASED;
14233 /* The EABI says that the virtual table, etc., for a class must be
14234 exported if it has a key method. The EABI does not specific the
14235 behavior if there is no key method, but there is no harm in
14236 exporting the class data in that case too. */
14238 static bool
14239 arm_cxx_export_class_data (void)
14241 return TARGET_AAPCS_BASED;
14244 void
14245 arm_set_return_address (rtx source, rtx scratch)
14247 arm_stack_offsets *offsets;
14248 HOST_WIDE_INT delta;
14249 rtx addr;
14250 unsigned long saved_regs;
14252 saved_regs = arm_compute_save_reg_mask ();
14254 if ((saved_regs & (1 << LR_REGNUM)) == 0)
14255 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14256 else
14258 if (frame_pointer_needed)
14259 addr = plus_constant(hard_frame_pointer_rtx, -4);
14260 else
14262 /* LR will be the first saved register. */
14263 offsets = arm_get_frame_offsets ();
14264 delta = offsets->outgoing_args - (offsets->frame + 4);
14267 if (delta >= 4096)
14269 emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
14270 GEN_INT (delta & ~4095)));
14271 addr = scratch;
14272 delta &= 4095;
14274 else
14275 addr = stack_pointer_rtx;
14277 addr = plus_constant (addr, delta);
14279 emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14284 void
14285 thumb_set_return_address (rtx source, rtx scratch)
14287 arm_stack_offsets *offsets;
14288 HOST_WIDE_INT delta;
14289 int reg;
14290 rtx addr;
14291 unsigned long mask;
14293 emit_insn (gen_rtx_USE (VOIDmode, source));
14295 mask = thumb_compute_save_reg_mask ();
14296 if (mask & (1 << LR_REGNUM))
14298 offsets = arm_get_frame_offsets ();
14300 /* Find the saved regs. */
14301 if (frame_pointer_needed)
14303 delta = offsets->soft_frame - offsets->saved_args;
14304 reg = THUMB_HARD_FRAME_POINTER_REGNUM;
14306 else
14308 delta = offsets->outgoing_args - offsets->saved_args;
14309 reg = SP_REGNUM;
14311 /* Allow for the stack frame. */
14312 if (TARGET_BACKTRACE)
14313 delta -= 16;
14314 /* The link register is always the first saved register. */
14315 delta -= 4;
14317 /* Construct the address. */
14318 addr = gen_rtx_REG (SImode, reg);
14319 if ((reg != SP_REGNUM && delta >= 128)
14320 || delta >= 1024)
14322 emit_insn (gen_movsi (scratch, GEN_INT (delta)));
14323 emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
14324 addr = scratch;
14326 else
14327 addr = plus_constant (addr, delta);
14329 emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14331 else
14332 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14335 /* Implements target hook vector_mode_supported_p. */
14336 bool
14337 arm_vector_mode_supported_p (enum machine_mode mode)
14339 if ((mode == V2SImode)
14340 || (mode == V4HImode)
14341 || (mode == V8QImode))
14342 return true;
14344 return false;
14347 /* Implement TARGET_SHIFT_TRUNCATION_MASK. SImode shifts use normal
14348 ARM insns and therefore guarantee that the shift count is modulo 256.
14349 DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
14350 guarantee no particular behavior for out-of-range counts. */
14352 static unsigned HOST_WIDE_INT
14353 arm_shift_truncation_mask (enum machine_mode mode)
14355 return mode == SImode ? 255 : 0;