* target-def.h (TARGET_CXX_EXPORT_CLASS_DATA): Remove.
[official-gcc.git] / gcc / config / arm / arm.c
blobb713bff9a3076ce4e0807c1215f7c4a55df94042
1 /* Output routines for GCC for ARM.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5 and Martin Simmons (@harleqn.co.uk).
6 More major hacks by Richard Earnshaw (rearnsha@arm.com).
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 int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
75 static rtx emit_sfm (int, int);
76 #ifndef AOF_ASSEMBLER
77 static bool arm_assemble_integer (rtx, unsigned int, int);
78 #endif
79 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
80 static arm_cc get_arm_condition_code (rtx);
81 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
82 static rtx is_jump_table (rtx);
83 static const char *output_multi_immediate (rtx *, const char *, const char *,
84 int, HOST_WIDE_INT);
85 static const char *shift_op (rtx, HOST_WIDE_INT *);
86 static struct machine_function *arm_init_machine_status (void);
87 static void thumb_exit (FILE *, int);
88 static rtx is_jump_table (rtx);
89 static HOST_WIDE_INT get_jump_table_size (rtx);
90 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
91 static Mnode *add_minipool_forward_ref (Mfix *);
92 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
93 static Mnode *add_minipool_backward_ref (Mfix *);
94 static void assign_minipool_offsets (Mfix *);
95 static void arm_print_value (FILE *, rtx);
96 static void dump_minipool (rtx);
97 static int arm_barrier_cost (rtx);
98 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
99 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
100 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
101 rtx);
102 static void arm_reorg (void);
103 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
104 static int current_file_function_operand (rtx);
105 static unsigned long arm_compute_save_reg0_reg12_mask (void);
106 static unsigned long arm_compute_save_reg_mask (void);
107 static unsigned long arm_isr_value (tree);
108 static unsigned long arm_compute_func_type (void);
109 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
110 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
111 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
112 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
113 #endif
114 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
115 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
116 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
117 static int arm_comp_type_attributes (tree, tree);
118 static void arm_set_default_type_attributes (tree);
119 static int arm_adjust_cost (rtx, rtx, rtx, int);
120 static int count_insns_for_constant (HOST_WIDE_INT, int);
121 static int arm_get_strip_length (int);
122 static bool arm_function_ok_for_sibcall (tree, tree);
123 static void arm_internal_label (FILE *, const char *, unsigned long);
124 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
125 tree);
126 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
127 static bool arm_size_rtx_costs (rtx, int, int, int *);
128 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
129 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
130 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
131 static bool arm_9e_rtx_costs (rtx, int, int, int *);
132 static int arm_address_cost (rtx);
133 static bool arm_memory_load_p (rtx);
134 static bool arm_cirrus_insn_p (rtx);
135 static void cirrus_reorg (rtx);
136 static void arm_init_builtins (void);
137 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
138 static void arm_init_iwmmxt_builtins (void);
139 static rtx safe_vector_operand (rtx, enum machine_mode);
140 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
141 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
142 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
143 static void emit_constant_insn (rtx cond, rtx pattern);
144 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
145 tree, bool);
147 #ifndef ARM_PE
148 static void arm_encode_section_info (tree, rtx, int);
149 #endif
151 static void arm_file_end (void);
153 #ifdef AOF_ASSEMBLER
154 static void aof_globalize_label (FILE *, const char *);
155 static void aof_dump_imports (FILE *);
156 static void aof_dump_pic_table (FILE *);
157 static void aof_file_start (void);
158 static void aof_file_end (void);
159 #endif
160 static rtx arm_struct_value_rtx (tree, int);
161 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
162 tree, int *, int);
163 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
164 enum machine_mode, tree, bool);
165 static bool arm_promote_prototypes (tree);
166 static bool arm_default_short_enums (void);
167 static bool arm_align_anon_bitfield (void);
169 static tree arm_cxx_guard_type (void);
170 static bool arm_cxx_guard_mask_bit (void);
171 static tree arm_get_cookie_size (tree);
172 static bool arm_cookie_has_size (void);
173 static bool arm_cxx_cdtor_returns_this (void);
174 static bool arm_cxx_key_method_may_be_inline (void);
175 static void arm_cxx_determine_class_data_visibility (tree);
176 static bool arm_cxx_class_data_always_comdat (void);
177 static void arm_init_libfuncs (void);
178 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
180 /* Initialize the GCC target structure. */
181 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
182 #undef TARGET_MERGE_DECL_ATTRIBUTES
183 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
184 #endif
186 #undef TARGET_ATTRIBUTE_TABLE
187 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
189 #undef TARGET_ASM_FILE_END
190 #define TARGET_ASM_FILE_END arm_file_end
192 #ifdef AOF_ASSEMBLER
193 #undef TARGET_ASM_BYTE_OP
194 #define TARGET_ASM_BYTE_OP "\tDCB\t"
195 #undef TARGET_ASM_ALIGNED_HI_OP
196 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
197 #undef TARGET_ASM_ALIGNED_SI_OP
198 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
199 #undef TARGET_ASM_GLOBALIZE_LABEL
200 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
201 #undef TARGET_ASM_FILE_START
202 #define TARGET_ASM_FILE_START aof_file_start
203 #undef TARGET_ASM_FILE_END
204 #define TARGET_ASM_FILE_END aof_file_end
205 #else
206 #undef TARGET_ASM_ALIGNED_SI_OP
207 #define TARGET_ASM_ALIGNED_SI_OP NULL
208 #undef TARGET_ASM_INTEGER
209 #define TARGET_ASM_INTEGER arm_assemble_integer
210 #endif
212 #undef TARGET_ASM_FUNCTION_PROLOGUE
213 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
215 #undef TARGET_ASM_FUNCTION_EPILOGUE
216 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
218 #undef TARGET_COMP_TYPE_ATTRIBUTES
219 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
221 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
222 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
224 #undef TARGET_SCHED_ADJUST_COST
225 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
227 #undef TARGET_ENCODE_SECTION_INFO
228 #ifdef ARM_PE
229 #define TARGET_ENCODE_SECTION_INFO arm_pe_encode_section_info
230 #else
231 #define TARGET_ENCODE_SECTION_INFO arm_encode_section_info
232 #endif
234 #undef TARGET_STRIP_NAME_ENCODING
235 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
237 #undef TARGET_ASM_INTERNAL_LABEL
238 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
240 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
241 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
243 #undef TARGET_ASM_OUTPUT_MI_THUNK
244 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
245 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
246 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
248 /* This will be overridden in arm_override_options. */
249 #undef TARGET_RTX_COSTS
250 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
251 #undef TARGET_ADDRESS_COST
252 #define TARGET_ADDRESS_COST arm_address_cost
254 #undef TARGET_SHIFT_TRUNCATION_MASK
255 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
256 #undef TARGET_VECTOR_MODE_SUPPORTED_P
257 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
259 #undef TARGET_MACHINE_DEPENDENT_REORG
260 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
262 #undef TARGET_INIT_BUILTINS
263 #define TARGET_INIT_BUILTINS arm_init_builtins
264 #undef TARGET_EXPAND_BUILTIN
265 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
267 #undef TARGET_INIT_LIBFUNCS
268 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
270 #undef TARGET_PROMOTE_FUNCTION_ARGS
271 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
272 #undef TARGET_PROMOTE_FUNCTION_RETURN
273 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
274 #undef TARGET_PROMOTE_PROTOTYPES
275 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
276 #undef TARGET_PASS_BY_REFERENCE
277 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
278 #undef TARGET_ARG_PARTIAL_BYTES
279 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
281 #undef TARGET_STRUCT_VALUE_RTX
282 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
284 #undef TARGET_SETUP_INCOMING_VARARGS
285 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
287 #undef TARGET_DEFAULT_SHORT_ENUMS
288 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
290 #undef TARGET_ALIGN_ANON_BITFIELD
291 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
293 #undef TARGET_CXX_GUARD_TYPE
294 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
296 #undef TARGET_CXX_GUARD_MASK_BIT
297 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
299 #undef TARGET_CXX_GET_COOKIE_SIZE
300 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
302 #undef TARGET_CXX_COOKIE_HAS_SIZE
303 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
305 #undef TARGET_CXX_CDTOR_RETURNS_THIS
306 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
308 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
309 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
311 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
312 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
313 arm_cxx_determine_class_data_visibility
315 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
316 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
318 struct gcc_target targetm = TARGET_INITIALIZER;
320 /* Obstack for minipool constant handling. */
321 static struct obstack minipool_obstack;
322 static char * minipool_startobj;
324 /* The maximum number of insns skipped which
325 will be conditionalised if possible. */
326 static int max_insns_skipped = 5;
328 extern FILE * asm_out_file;
330 /* True if we are currently building a constant table. */
331 int making_const_table;
333 /* Define the information needed to generate branch insns. This is
334 stored from the compare operation. */
335 rtx arm_compare_op0, arm_compare_op1;
337 /* The processor for which instructions should be scheduled. */
338 enum processor_type arm_tune = arm_none;
340 /* Which floating point model to use. */
341 enum arm_fp_model arm_fp_model;
343 /* Which floating point hardware is available. */
344 enum fputype arm_fpu_arch;
346 /* Which floating point hardware to schedule for. */
347 enum fputype arm_fpu_tune;
349 /* Whether to use floating point hardware. */
350 enum float_abi_type arm_float_abi;
352 /* Which ABI to use. */
353 enum arm_abi_type arm_abi;
355 /* Set by the -mfpu=... option. */
356 const char * target_fpu_name = NULL;
358 /* Set by the -mfpe=... option. */
359 const char * target_fpe_name = NULL;
361 /* Set by the -mfloat-abi=... option. */
362 const char * target_float_abi_name = NULL;
364 /* Set by the legacy -mhard-float and -msoft-float options. */
365 const char * target_float_switch = NULL;
367 /* Set by the -mabi=... option. */
368 const char * target_abi_name = NULL;
370 /* Used to parse -mstructure_size_boundary command line option. */
371 const char * structure_size_string = NULL;
372 int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
374 /* Used for Thumb call_via trampolines. */
375 rtx thumb_call_via_label[14];
376 static int thumb_call_reg_needed;
378 /* Bit values used to identify processor capabilities. */
379 #define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
380 #define FL_ARCH3M (1 << 1) /* Extended multiply */
381 #define FL_MODE26 (1 << 2) /* 26-bit mode support */
382 #define FL_MODE32 (1 << 3) /* 32-bit mode support */
383 #define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
384 #define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
385 #define FL_THUMB (1 << 6) /* Thumb aware */
386 #define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
387 #define FL_STRONG (1 << 8) /* StrongARM */
388 #define FL_ARCH5E (1 << 9) /* DSP extensions to v5 */
389 #define FL_XSCALE (1 << 10) /* XScale */
390 #define FL_CIRRUS (1 << 11) /* Cirrus/DSP. */
391 #define FL_ARCH6 (1 << 12) /* Architecture rel 6. Adds
392 media instructions. */
393 #define FL_VFPV2 (1 << 13) /* Vector Floating Point V2. */
394 #define FL_WBUF (1 << 14) /* Schedule for write buffer ops.
395 Note: ARM6 & 7 derivatives only. */
397 #define FL_IWMMXT (1 << 29) /* XScale v2 or "Intel Wireless MMX technology". */
399 #define FL_FOR_ARCH2 0
400 #define FL_FOR_ARCH3 FL_MODE32
401 #define FL_FOR_ARCH3M (FL_FOR_ARCH3 | FL_ARCH3M)
402 #define FL_FOR_ARCH4 (FL_FOR_ARCH3M | FL_ARCH4)
403 #define FL_FOR_ARCH4T (FL_FOR_ARCH4 | FL_THUMB)
404 #define FL_FOR_ARCH5 (FL_FOR_ARCH4 | FL_ARCH5)
405 #define FL_FOR_ARCH5T (FL_FOR_ARCH5 | FL_THUMB)
406 #define FL_FOR_ARCH5E (FL_FOR_ARCH5 | FL_ARCH5E)
407 #define FL_FOR_ARCH5TE (FL_FOR_ARCH5E | FL_THUMB)
408 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
409 #define FL_FOR_ARCH6 (FL_FOR_ARCH5TE | FL_ARCH6)
410 #define FL_FOR_ARCH6J FL_FOR_ARCH6
411 #define FL_FOR_ARCH6K FL_FOR_ARCH6
412 #define FL_FOR_ARCH6Z FL_FOR_ARCH6
413 #define FL_FOR_ARCH6ZK FL_FOR_ARCH6
415 /* The bits in this mask specify which
416 instructions we are allowed to generate. */
417 static unsigned long insn_flags = 0;
419 /* The bits in this mask specify which instruction scheduling options should
420 be used. */
421 static unsigned long tune_flags = 0;
423 /* The following are used in the arm.md file as equivalents to bits
424 in the above two flag variables. */
426 /* Nonzero if this chip supports the ARM Architecture 3M extensions. */
427 int arm_arch3m = 0;
429 /* Nonzero if this chip supports the ARM Architecture 4 extensions. */
430 int arm_arch4 = 0;
432 /* Nonzero if this chip supports the ARM Architecture 4t extensions. */
433 int arm_arch4t = 0;
435 /* Nonzero if this chip supports the ARM Architecture 5 extensions. */
436 int arm_arch5 = 0;
438 /* Nonzero if this chip supports the ARM Architecture 5E extensions. */
439 int arm_arch5e = 0;
441 /* Nonzero if this chip supports the ARM Architecture 6 extensions. */
442 int arm_arch6 = 0;
444 /* Nonzero if this chip can benefit from load scheduling. */
445 int arm_ld_sched = 0;
447 /* Nonzero if this chip is a StrongARM. */
448 int arm_tune_strongarm = 0;
450 /* Nonzero if this chip is a Cirrus variant. */
451 int arm_arch_cirrus = 0;
453 /* Nonzero if this chip supports Intel Wireless MMX technology. */
454 int arm_arch_iwmmxt = 0;
456 /* Nonzero if this chip is an XScale. */
457 int arm_arch_xscale = 0;
459 /* Nonzero if tuning for XScale */
460 int arm_tune_xscale = 0;
462 /* Nonzero if we want to tune for stores that access the write-buffer.
463 This typically means an ARM6 or ARM7 with MMU or MPU. */
464 int arm_tune_wbuf = 0;
466 /* Nonzero if generating Thumb instructions. */
467 int thumb_code = 0;
469 /* Nonzero if we should define __THUMB_INTERWORK__ in the
470 preprocessor.
471 XXX This is a bit of a hack, it's intended to help work around
472 problems in GLD which doesn't understand that armv5t code is
473 interworking clean. */
474 int arm_cpp_interwork = 0;
476 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
477 must report the mode of the memory reference from PRINT_OPERAND to
478 PRINT_OPERAND_ADDRESS. */
479 enum machine_mode output_memory_reference_mode;
481 /* The register number to be used for the PIC offset register. */
482 const char * arm_pic_register_string = NULL;
483 int arm_pic_register = INVALID_REGNUM;
485 /* Set to 1 when a return insn is output, this means that the epilogue
486 is not needed. */
487 int return_used_this_function;
489 /* Set to 1 after arm_reorg has started. Reset to start at the start of
490 the next function. */
491 static int after_arm_reorg = 0;
493 /* The maximum number of insns to be used when loading a constant. */
494 static int arm_constant_limit = 3;
496 /* For an explanation of these variables, see final_prescan_insn below. */
497 int arm_ccfsm_state;
498 enum arm_cond_code arm_current_cc;
499 rtx arm_target_insn;
500 int arm_target_label;
502 /* The condition codes of the ARM, and the inverse function. */
503 static const char * const arm_condition_codes[] =
505 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
506 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
509 #define streq(string1, string2) (strcmp (string1, string2) == 0)
511 /* Initialization code. */
513 struct processors
515 const char *const name;
516 enum processor_type core;
517 const char *arch;
518 const unsigned long flags;
519 bool (* rtx_costs) (rtx, int, int, int *);
522 /* Not all of these give usefully different compilation alternatives,
523 but there is no simple way of generalizing them. */
524 static const struct processors all_cores[] =
526 /* ARM Cores */
527 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
528 {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
529 #include "arm-cores.def"
530 #undef ARM_CORE
531 {NULL, arm_none, NULL, 0, NULL}
534 static const struct processors all_architectures[] =
536 /* ARM Architectures */
537 /* We don't specify rtx_costs here as it will be figured out
538 from the core. */
540 {"armv2", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
541 {"armv2a", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
542 {"armv3", arm6, "3", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
543 {"armv3m", arm7m, "3M", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
544 {"armv4", arm7tdmi, "4", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
545 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
546 implementations that support it, so we will leave it out for now. */
547 {"armv4t", arm7tdmi, "4T", FL_CO_PROC | FL_FOR_ARCH4T, NULL},
548 {"armv5", arm10tdmi, "5", FL_CO_PROC | FL_FOR_ARCH5, NULL},
549 {"armv5t", arm10tdmi, "5T", FL_CO_PROC | FL_FOR_ARCH5T, NULL},
550 {"armv5e", arm1026ejs, "5E", FL_CO_PROC | FL_FOR_ARCH5E, NULL},
551 {"armv5te", arm1026ejs, "5TE", FL_CO_PROC | FL_FOR_ARCH5TE, NULL},
552 {"armv6", arm1136js, "6", FL_CO_PROC | FL_FOR_ARCH6, NULL},
553 {"armv6j", arm1136js, "6J", FL_CO_PROC | FL_FOR_ARCH6J, NULL},
554 {"armv6k", mpcore, "6K", FL_CO_PROC | FL_FOR_ARCH6K, NULL},
555 {"armv6z", arm1176jzs, "6Z", FL_CO_PROC | FL_FOR_ARCH6Z, NULL},
556 {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC | FL_FOR_ARCH6ZK, NULL},
557 {"ep9312", ep9312, "4T", FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
558 {"iwmmxt", iwmmxt, "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
559 {NULL, arm_none, NULL, 0 , NULL}
562 /* This is a magic structure. The 'string' field is magically filled in
563 with a pointer to the value specified by the user on the command line
564 assuming that the user has specified such a value. */
566 struct arm_cpu_select arm_select[] =
568 /* string name processors */
569 { NULL, "-mcpu=", all_cores },
570 { NULL, "-march=", all_architectures },
571 { NULL, "-mtune=", all_cores }
575 /* The name of the proprocessor macro to define for this architecture. */
577 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
579 struct fpu_desc
581 const char * name;
582 enum fputype fpu;
586 /* Available values for for -mfpu=. */
588 static const struct fpu_desc all_fpus[] =
590 {"fpa", FPUTYPE_FPA},
591 {"fpe2", FPUTYPE_FPA_EMU2},
592 {"fpe3", FPUTYPE_FPA_EMU2},
593 {"maverick", FPUTYPE_MAVERICK},
594 {"vfp", FPUTYPE_VFP}
598 /* Floating point models used by the different hardware.
599 See fputype in arm.h. */
601 static const enum fputype fp_model_for_fpu[] =
603 /* No FP hardware. */
604 ARM_FP_MODEL_UNKNOWN, /* FPUTYPE_NONE */
605 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA */
606 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU2 */
607 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU3 */
608 ARM_FP_MODEL_MAVERICK, /* FPUTYPE_MAVERICK */
609 ARM_FP_MODEL_VFP /* FPUTYPE_VFP */
613 struct float_abi
615 const char * name;
616 enum float_abi_type abi_type;
620 /* Available values for -mfloat-abi=. */
622 static const struct float_abi all_float_abis[] =
624 {"soft", ARM_FLOAT_ABI_SOFT},
625 {"softfp", ARM_FLOAT_ABI_SOFTFP},
626 {"hard", ARM_FLOAT_ABI_HARD}
630 struct abi_name
632 const char *name;
633 enum arm_abi_type abi_type;
637 /* Available values for -mabi=. */
639 static const struct abi_name arm_all_abis[] =
641 {"apcs-gnu", ARM_ABI_APCS},
642 {"atpcs", ARM_ABI_ATPCS},
643 {"aapcs", ARM_ABI_AAPCS},
644 {"iwmmxt", ARM_ABI_IWMMXT}
647 /* Return the number of bits set in VALUE. */
648 static unsigned
649 bit_count (unsigned long value)
651 unsigned long count = 0;
653 while (value)
655 count++;
656 value &= value - 1; /* Clear the least-significant set bit. */
659 return count;
662 /* Set up library functions unique to ARM. */
664 static void
665 arm_init_libfuncs (void)
667 /* There are no special library functions unless we are using the
668 ARM BPABI. */
669 if (!TARGET_BPABI)
670 return;
672 /* The functions below are described in Section 4 of the "Run-Time
673 ABI for the ARM architecture", Version 1.0. */
675 /* Double-precision floating-point arithmetic. Table 2. */
676 set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
677 set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
678 set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
679 set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
680 set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
682 /* Double-precision comparisons. Table 3. */
683 set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
684 set_optab_libfunc (ne_optab, DFmode, NULL);
685 set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
686 set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
687 set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
688 set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
689 set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
691 /* Single-precision floating-point arithmetic. Table 4. */
692 set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
693 set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
694 set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
695 set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
696 set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
698 /* Single-precision comparisons. Table 5. */
699 set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
700 set_optab_libfunc (ne_optab, SFmode, NULL);
701 set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
702 set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
703 set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
704 set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
705 set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
707 /* Floating-point to integer conversions. Table 6. */
708 set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
709 set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
710 set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
711 set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
712 set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
713 set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
714 set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
715 set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
717 /* Conversions between floating types. Table 7. */
718 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
719 set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
721 /* Integer to floating-point conversions. Table 8. */
722 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
723 set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
724 set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
725 set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
726 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
727 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
728 set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
729 set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
731 /* Long long. Table 9. */
732 set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
733 set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
734 set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
735 set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
736 set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
737 set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
738 set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
739 set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
741 /* Integer (32/32->32) division. \S 4.3.1. */
742 set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
743 set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
745 /* The divmod functions are designed so that they can be used for
746 plain division, even though they return both the quotient and the
747 remainder. The quotient is returned in the usual location (i.e.,
748 r0 for SImode, {r0, r1} for DImode), just as would be expected
749 for an ordinary division routine. Because the AAPCS calling
750 conventions specify that all of { r0, r1, r2, r3 } are
751 callee-saved registers, there is no need to tell the compiler
752 explicitly that those registers are clobbered by these
753 routines. */
754 set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
755 set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
756 set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idivmod");
757 set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidivmod");
760 /* Fix up any incompatible options that the user has specified.
761 This has now turned into a maze. */
762 void
763 arm_override_options (void)
765 unsigned i;
767 /* Set up the flags based on the cpu/architecture selected by the user. */
768 for (i = ARRAY_SIZE (arm_select); i--;)
770 struct arm_cpu_select * ptr = arm_select + i;
772 if (ptr->string != NULL && ptr->string[0] != '\0')
774 const struct processors * sel;
776 for (sel = ptr->processors; sel->name != NULL; sel++)
777 if (streq (ptr->string, sel->name))
779 /* Set the architecture define. */
780 if (i != 2)
781 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
783 /* Determine the processor core for which we should
784 tune code-generation. */
785 if (/* -mcpu= is a sensible default. */
786 i == 0
787 /* If -march= is used, and -mcpu= has not been used,
788 assume that we should tune for a representative
789 CPU from that architecture. */
790 || i == 1
791 /* -mtune= overrides -mcpu= and -march=. */
792 || i == 2)
793 arm_tune = (enum processor_type) (sel - ptr->processors);
795 if (i != 2)
797 /* If we have been given an architecture and a processor
798 make sure that they are compatible. We only generate
799 a warning though, and we prefer the CPU over the
800 architecture. */
801 if (insn_flags != 0 && (insn_flags ^ sel->flags))
802 warning ("switch -mcpu=%s conflicts with -march= switch",
803 ptr->string);
805 insn_flags = sel->flags;
808 break;
811 if (sel->name == NULL)
812 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
816 /* If the user did not specify a processor, choose one for them. */
817 if (insn_flags == 0)
819 const struct processors * sel;
820 unsigned int sought;
821 enum processor_type cpu;
823 cpu = TARGET_CPU_DEFAULT;
824 if (cpu == arm_none)
826 #ifdef SUBTARGET_CPU_DEFAULT
827 /* Use the subtarget default CPU if none was specified by
828 configure. */
829 cpu = SUBTARGET_CPU_DEFAULT;
830 #endif
831 /* Default to ARM6. */
832 if (cpu == arm_none)
833 cpu = arm6;
835 sel = &all_cores[cpu];
837 insn_flags = sel->flags;
839 /* Now check to see if the user has specified some command line
840 switch that require certain abilities from the cpu. */
841 sought = 0;
843 if (TARGET_INTERWORK || TARGET_THUMB)
845 sought |= (FL_THUMB | FL_MODE32);
847 /* There are no ARM processors that support both APCS-26 and
848 interworking. Therefore we force FL_MODE26 to be removed
849 from insn_flags here (if it was set), so that the search
850 below will always be able to find a compatible processor. */
851 insn_flags &= ~FL_MODE26;
854 if (sought != 0 && ((sought & insn_flags) != sought))
856 /* Try to locate a CPU type that supports all of the abilities
857 of the default CPU, plus the extra abilities requested by
858 the user. */
859 for (sel = all_cores; sel->name != NULL; sel++)
860 if ((sel->flags & sought) == (sought | insn_flags))
861 break;
863 if (sel->name == NULL)
865 unsigned current_bit_count = 0;
866 const struct processors * best_fit = NULL;
868 /* Ideally we would like to issue an error message here
869 saying that it was not possible to find a CPU compatible
870 with the default CPU, but which also supports the command
871 line options specified by the programmer, and so they
872 ought to use the -mcpu=<name> command line option to
873 override the default CPU type.
875 If we cannot find a cpu that has both the
876 characteristics of the default cpu and the given
877 command line options we scan the array again looking
878 for a best match. */
879 for (sel = all_cores; sel->name != NULL; sel++)
880 if ((sel->flags & sought) == sought)
882 unsigned count;
884 count = bit_count (sel->flags & insn_flags);
886 if (count >= current_bit_count)
888 best_fit = sel;
889 current_bit_count = count;
893 if (best_fit == NULL)
894 abort ();
895 else
896 sel = best_fit;
899 insn_flags = sel->flags;
901 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
902 if (arm_tune == arm_none)
903 arm_tune = (enum processor_type) (sel - all_cores);
906 /* The processor for which we should tune should now have been
907 chosen. */
908 if (arm_tune == arm_none)
909 abort ();
911 tune_flags = all_cores[(int)arm_tune].flags;
912 if (optimize_size)
913 targetm.rtx_costs = arm_size_rtx_costs;
914 else
915 targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
917 /* Make sure that the processor choice does not conflict with any of the
918 other command line choices. */
919 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
921 warning ("target CPU does not support interworking" );
922 target_flags &= ~ARM_FLAG_INTERWORK;
925 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
927 warning ("target CPU does not support THUMB instructions");
928 target_flags &= ~ARM_FLAG_THUMB;
931 if (TARGET_APCS_FRAME && TARGET_THUMB)
933 /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
934 target_flags &= ~ARM_FLAG_APCS_FRAME;
937 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
938 from here where no function is being compiled currently. */
939 if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
940 && TARGET_ARM)
941 warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
943 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
944 warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
946 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
947 warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
949 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
951 warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
952 target_flags |= ARM_FLAG_APCS_FRAME;
955 if (TARGET_POKE_FUNCTION_NAME)
956 target_flags |= ARM_FLAG_APCS_FRAME;
958 if (TARGET_APCS_REENT && flag_pic)
959 error ("-fpic and -mapcs-reent are incompatible");
961 if (TARGET_APCS_REENT)
962 warning ("APCS reentrant code not supported. Ignored");
964 /* If this target is normally configured to use APCS frames, warn if they
965 are turned off and debugging is turned on. */
966 if (TARGET_ARM
967 && write_symbols != NO_DEBUG
968 && !TARGET_APCS_FRAME
969 && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
970 warning ("-g with -mno-apcs-frame may not give sensible debugging");
972 /* If stack checking is disabled, we can use r10 as the PIC register,
973 which keeps r9 available. */
974 if (flag_pic)
975 arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
977 if (TARGET_APCS_FLOAT)
978 warning ("passing floating point arguments in fp regs not yet supported");
980 /* Initialize boolean versions of the flags, for use in the arm.md file. */
981 arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
982 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
983 arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
984 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
985 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
986 arm_arch6 = (insn_flags & FL_ARCH6) != 0;
987 arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
988 arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
990 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
991 arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
992 thumb_code = (TARGET_ARM == 0);
993 arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
994 arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
995 arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
997 /* V5 code we generate is completely interworking capable, so we turn off
998 TARGET_INTERWORK here to avoid many tests later on. */
1000 /* XXX However, we must pass the right pre-processor defines to CPP
1001 or GLD can get confused. This is a hack. */
1002 if (TARGET_INTERWORK)
1003 arm_cpp_interwork = 1;
1005 if (arm_arch5)
1006 target_flags &= ~ARM_FLAG_INTERWORK;
1008 if (target_abi_name)
1010 for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1012 if (streq (arm_all_abis[i].name, target_abi_name))
1014 arm_abi = arm_all_abis[i].abi_type;
1015 break;
1018 if (i == ARRAY_SIZE (arm_all_abis))
1019 error ("invalid ABI option: -mabi=%s", target_abi_name);
1021 else
1022 arm_abi = ARM_DEFAULT_ABI;
1024 if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1025 error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1027 if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1028 error ("iwmmxt abi requires an iwmmxt capable cpu");
1030 arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1031 if (target_fpu_name == NULL && target_fpe_name != NULL)
1033 if (streq (target_fpe_name, "2"))
1034 target_fpu_name = "fpe2";
1035 else if (streq (target_fpe_name, "3"))
1036 target_fpu_name = "fpe3";
1037 else
1038 error ("invalid floating point emulation option: -mfpe=%s",
1039 target_fpe_name);
1041 if (target_fpu_name != NULL)
1043 /* The user specified a FPU. */
1044 for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1046 if (streq (all_fpus[i].name, target_fpu_name))
1048 arm_fpu_arch = all_fpus[i].fpu;
1049 arm_fpu_tune = arm_fpu_arch;
1050 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1051 break;
1054 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1055 error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1057 else
1059 #ifdef FPUTYPE_DEFAULT
1060 /* Use the default if it is specified for this platform. */
1061 arm_fpu_arch = FPUTYPE_DEFAULT;
1062 arm_fpu_tune = FPUTYPE_DEFAULT;
1063 #else
1064 /* Pick one based on CPU type. */
1065 /* ??? Some targets assume FPA is the default.
1066 if ((insn_flags & FL_VFP) != 0)
1067 arm_fpu_arch = FPUTYPE_VFP;
1068 else
1070 if (arm_arch_cirrus)
1071 arm_fpu_arch = FPUTYPE_MAVERICK;
1072 else
1073 arm_fpu_arch = FPUTYPE_FPA_EMU2;
1074 #endif
1075 if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1076 arm_fpu_tune = FPUTYPE_FPA;
1077 else
1078 arm_fpu_tune = arm_fpu_arch;
1079 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1080 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1081 abort ();
1084 if (target_float_abi_name != NULL)
1086 /* The user specified a FP ABI. */
1087 for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1089 if (streq (all_float_abis[i].name, target_float_abi_name))
1091 arm_float_abi = all_float_abis[i].abi_type;
1092 break;
1095 if (i == ARRAY_SIZE (all_float_abis))
1096 error ("invalid floating point abi: -mfloat-abi=%s",
1097 target_float_abi_name);
1099 else if (target_float_switch)
1101 /* This is a bit of a hack to avoid needing target flags for these. */
1102 if (target_float_switch[0] == 'h')
1103 arm_float_abi = ARM_FLOAT_ABI_HARD;
1104 else
1105 arm_float_abi = ARM_FLOAT_ABI_SOFT;
1107 else
1108 arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1110 if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1111 sorry ("-mfloat-abi=hard and VFP");
1113 /* If soft-float is specified then don't use FPU. */
1114 if (TARGET_SOFT_FLOAT)
1115 arm_fpu_arch = FPUTYPE_NONE;
1117 /* For arm2/3 there is no need to do any scheduling if there is only
1118 a floating point emulator, or we are doing software floating-point. */
1119 if ((TARGET_SOFT_FLOAT
1120 || arm_fpu_tune == FPUTYPE_FPA_EMU2
1121 || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1122 && (tune_flags & FL_MODE32) == 0)
1123 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1125 /* Override the default structure alignment for AAPCS ABI. */
1126 if (arm_abi == ARM_ABI_AAPCS)
1127 arm_structure_size_boundary = 8;
1129 if (structure_size_string != NULL)
1131 int size = strtol (structure_size_string, NULL, 0);
1133 if (size == 8 || size == 32
1134 || (ARM_DOUBLEWORD_ALIGN && size == 64))
1135 arm_structure_size_boundary = size;
1136 else
1137 warning ("structure size boundary can only be set to %s",
1138 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1141 if (arm_pic_register_string != NULL)
1143 int pic_register = decode_reg_name (arm_pic_register_string);
1145 if (!flag_pic)
1146 warning ("-mpic-register= is useless without -fpic");
1148 /* Prevent the user from choosing an obviously stupid PIC register. */
1149 else if (pic_register < 0 || call_used_regs[pic_register]
1150 || pic_register == HARD_FRAME_POINTER_REGNUM
1151 || pic_register == STACK_POINTER_REGNUM
1152 || pic_register >= PC_REGNUM)
1153 error ("unable to use '%s' for PIC register", arm_pic_register_string);
1154 else
1155 arm_pic_register = pic_register;
1158 if (TARGET_THUMB && flag_schedule_insns)
1160 /* Don't warn since it's on by default in -O2. */
1161 flag_schedule_insns = 0;
1164 if (optimize_size)
1166 /* There's some dispute as to whether this should be 1 or 2. However,
1167 experiments seem to show that in pathological cases a setting of
1168 1 degrades less severely than a setting of 2. This could change if
1169 other parts of the compiler change their behavior. */
1170 arm_constant_limit = 1;
1172 /* If optimizing for size, bump the number of instructions that we
1173 are prepared to conditionally execute (even on a StrongARM). */
1174 max_insns_skipped = 6;
1176 else
1178 /* For processors with load scheduling, it never costs more than
1179 2 cycles to load a constant, and the load scheduler may well
1180 reduce that to 1. */
1181 if (arm_ld_sched)
1182 arm_constant_limit = 1;
1184 /* On XScale the longer latency of a load makes it more difficult
1185 to achieve a good schedule, so it's faster to synthesize
1186 constants that can be done in two insns. */
1187 if (arm_tune_xscale)
1188 arm_constant_limit = 2;
1190 /* StrongARM has early execution of branches, so a sequence
1191 that is worth skipping is shorter. */
1192 if (arm_tune_strongarm)
1193 max_insns_skipped = 3;
1196 /* Register global variables with the garbage collector. */
1197 arm_add_gc_roots ();
1200 static void
1201 arm_add_gc_roots (void)
1203 gcc_obstack_init(&minipool_obstack);
1204 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1207 /* A table of known ARM exception types.
1208 For use with the interrupt function attribute. */
1210 typedef struct
1212 const char *const arg;
1213 const unsigned long return_value;
1215 isr_attribute_arg;
1217 static const isr_attribute_arg isr_attribute_args [] =
1219 { "IRQ", ARM_FT_ISR },
1220 { "irq", ARM_FT_ISR },
1221 { "FIQ", ARM_FT_FIQ },
1222 { "fiq", ARM_FT_FIQ },
1223 { "ABORT", ARM_FT_ISR },
1224 { "abort", ARM_FT_ISR },
1225 { "ABORT", ARM_FT_ISR },
1226 { "abort", ARM_FT_ISR },
1227 { "UNDEF", ARM_FT_EXCEPTION },
1228 { "undef", ARM_FT_EXCEPTION },
1229 { "SWI", ARM_FT_EXCEPTION },
1230 { "swi", ARM_FT_EXCEPTION },
1231 { NULL, ARM_FT_NORMAL }
1234 /* Returns the (interrupt) function type of the current
1235 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
1237 static unsigned long
1238 arm_isr_value (tree argument)
1240 const isr_attribute_arg * ptr;
1241 const char * arg;
1243 /* No argument - default to IRQ. */
1244 if (argument == NULL_TREE)
1245 return ARM_FT_ISR;
1247 /* Get the value of the argument. */
1248 if (TREE_VALUE (argument) == NULL_TREE
1249 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1250 return ARM_FT_UNKNOWN;
1252 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1254 /* Check it against the list of known arguments. */
1255 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1256 if (streq (arg, ptr->arg))
1257 return ptr->return_value;
1259 /* An unrecognized interrupt type. */
1260 return ARM_FT_UNKNOWN;
1263 /* Computes the type of the current function. */
1265 static unsigned long
1266 arm_compute_func_type (void)
1268 unsigned long type = ARM_FT_UNKNOWN;
1269 tree a;
1270 tree attr;
1272 if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
1273 abort ();
1275 /* Decide if the current function is volatile. Such functions
1276 never return, and many memory cycles can be saved by not storing
1277 register values that will never be needed again. This optimization
1278 was added to speed up context switching in a kernel application. */
1279 if (optimize > 0
1280 && TREE_NOTHROW (current_function_decl)
1281 && TREE_THIS_VOLATILE (current_function_decl))
1282 type |= ARM_FT_VOLATILE;
1284 if (cfun->static_chain_decl != NULL)
1285 type |= ARM_FT_NESTED;
1287 attr = DECL_ATTRIBUTES (current_function_decl);
1289 a = lookup_attribute ("naked", attr);
1290 if (a != NULL_TREE)
1291 type |= ARM_FT_NAKED;
1293 a = lookup_attribute ("isr", attr);
1294 if (a == NULL_TREE)
1295 a = lookup_attribute ("interrupt", attr);
1297 if (a == NULL_TREE)
1298 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1299 else
1300 type |= arm_isr_value (TREE_VALUE (a));
1302 return type;
1305 /* Returns the type of the current function. */
1307 unsigned long
1308 arm_current_func_type (void)
1310 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1311 cfun->machine->func_type = arm_compute_func_type ();
1313 return cfun->machine->func_type;
1316 /* Return 1 if it is possible to return using a single instruction.
1317 If SIBLING is non-null, this is a test for a return before a sibling
1318 call. SIBLING is the call insn, so we can examine its register usage. */
1321 use_return_insn (int iscond, rtx sibling)
1323 int regno;
1324 unsigned int func_type;
1325 unsigned long saved_int_regs;
1326 unsigned HOST_WIDE_INT stack_adjust;
1327 arm_stack_offsets *offsets;
1329 /* Never use a return instruction before reload has run. */
1330 if (!reload_completed)
1331 return 0;
1333 func_type = arm_current_func_type ();
1335 /* Naked functions and volatile functions need special
1336 consideration. */
1337 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1338 return 0;
1340 /* So do interrupt functions that use the frame pointer. */
1341 if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1342 return 0;
1344 offsets = arm_get_frame_offsets ();
1345 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1347 /* As do variadic functions. */
1348 if (current_function_pretend_args_size
1349 || cfun->machine->uses_anonymous_args
1350 /* Or if the function calls __builtin_eh_return () */
1351 || current_function_calls_eh_return
1352 /* Or if the function calls alloca */
1353 || current_function_calls_alloca
1354 /* Or if there is a stack adjustment. However, if the stack pointer
1355 is saved on the stack, we can use a pre-incrementing stack load. */
1356 || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1357 return 0;
1359 saved_int_regs = arm_compute_save_reg_mask ();
1361 /* Unfortunately, the insn
1363 ldmib sp, {..., sp, ...}
1365 triggers a bug on most SA-110 based devices, such that the stack
1366 pointer won't be correctly restored if the instruction takes a
1367 page fault. We work around this problem by popping r3 along with
1368 the other registers, since that is never slower than executing
1369 another instruction.
1371 We test for !arm_arch5 here, because code for any architecture
1372 less than this could potentially be run on one of the buggy
1373 chips. */
1374 if (stack_adjust == 4 && !arm_arch5)
1376 /* Validate that r3 is a call-clobbered register (always true in
1377 the default abi) ... */
1378 if (!call_used_regs[3])
1379 return 0;
1381 /* ... that it isn't being used for a return value (always true
1382 until we implement return-in-regs), or for a tail-call
1383 argument ... */
1384 if (sibling)
1386 if (GET_CODE (sibling) != CALL_INSN)
1387 abort ();
1389 if (find_regno_fusage (sibling, USE, 3))
1390 return 0;
1393 /* ... and that there are no call-saved registers in r0-r2
1394 (always true in the default ABI). */
1395 if (saved_int_regs & 0x7)
1396 return 0;
1399 /* Can't be done if interworking with Thumb, and any registers have been
1400 stacked. */
1401 if (TARGET_INTERWORK && saved_int_regs != 0)
1402 return 0;
1404 /* On StrongARM, conditional returns are expensive if they aren't
1405 taken and multiple registers have been stacked. */
1406 if (iscond && arm_tune_strongarm)
1408 /* Conditional return when just the LR is stored is a simple
1409 conditional-load instruction, that's not expensive. */
1410 if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1411 return 0;
1413 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1414 return 0;
1417 /* If there are saved registers but the LR isn't saved, then we need
1418 two instructions for the return. */
1419 if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1420 return 0;
1422 /* Can't be done if any of the FPA regs are pushed,
1423 since this also requires an insn. */
1424 if (TARGET_HARD_FLOAT && TARGET_FPA)
1425 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1426 if (regs_ever_live[regno] && !call_used_regs[regno])
1427 return 0;
1429 /* Likewise VFP regs. */
1430 if (TARGET_HARD_FLOAT && TARGET_VFP)
1431 for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1432 if (regs_ever_live[regno] && !call_used_regs[regno])
1433 return 0;
1435 if (TARGET_REALLY_IWMMXT)
1436 for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1437 if (regs_ever_live[regno] && ! call_used_regs [regno])
1438 return 0;
1440 return 1;
1443 /* Return TRUE if int I is a valid immediate ARM constant. */
1446 const_ok_for_arm (HOST_WIDE_INT i)
1448 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1450 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1451 be all zero, or all one. */
1452 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1453 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1454 != ((~(unsigned HOST_WIDE_INT) 0)
1455 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1456 return FALSE;
1458 /* Fast return for 0 and powers of 2 */
1459 if ((i & (i - 1)) == 0)
1460 return TRUE;
1464 if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1465 return TRUE;
1466 mask =
1467 (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1468 >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1470 while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1472 return FALSE;
1475 /* Return true if I is a valid constant for the operation CODE. */
1476 static int
1477 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1479 if (const_ok_for_arm (i))
1480 return 1;
1482 switch (code)
1484 case PLUS:
1485 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1487 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
1488 case XOR:
1489 case IOR:
1490 return 0;
1492 case AND:
1493 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1495 default:
1496 abort ();
1500 /* Emit a sequence of insns to handle a large constant.
1501 CODE is the code of the operation required, it can be any of SET, PLUS,
1502 IOR, AND, XOR, MINUS;
1503 MODE is the mode in which the operation is being performed;
1504 VAL is the integer to operate on;
1505 SOURCE is the other operand (a register, or a null-pointer for SET);
1506 SUBTARGETS means it is safe to create scratch registers if that will
1507 either produce a simpler sequence, or we will want to cse the values.
1508 Return value is the number of insns emitted. */
1511 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1512 HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1514 rtx cond;
1516 if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1517 cond = COND_EXEC_TEST (PATTERN (insn));
1518 else
1519 cond = NULL_RTX;
1521 if (subtargets || code == SET
1522 || (GET_CODE (target) == REG && GET_CODE (source) == REG
1523 && REGNO (target) != REGNO (source)))
1525 /* After arm_reorg has been called, we can't fix up expensive
1526 constants by pushing them into memory so we must synthesize
1527 them in-line, regardless of the cost. This is only likely to
1528 be more costly on chips that have load delay slots and we are
1529 compiling without running the scheduler (so no splitting
1530 occurred before the final instruction emission).
1532 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1534 if (!after_arm_reorg
1535 && !cond
1536 && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1537 1, 0)
1538 > arm_constant_limit + (code != SET)))
1540 if (code == SET)
1542 /* Currently SET is the only monadic value for CODE, all
1543 the rest are diadic. */
1544 emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1545 return 1;
1547 else
1549 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1551 emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1552 /* For MINUS, the value is subtracted from, since we never
1553 have subtraction of a constant. */
1554 if (code == MINUS)
1555 emit_insn (gen_rtx_SET (VOIDmode, target,
1556 gen_rtx_MINUS (mode, temp, source)));
1557 else
1558 emit_insn (gen_rtx_SET (VOIDmode, target,
1559 gen_rtx_fmt_ee (code, mode, source, temp)));
1560 return 2;
1565 return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1569 static int
1570 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1572 HOST_WIDE_INT temp1;
1573 int num_insns = 0;
1576 int end;
1578 if (i <= 0)
1579 i += 32;
1580 if (remainder & (3 << (i - 2)))
1582 end = i - 8;
1583 if (end < 0)
1584 end += 32;
1585 temp1 = remainder & ((0x0ff << end)
1586 | ((i < end) ? (0xff >> (32 - end)) : 0));
1587 remainder &= ~temp1;
1588 num_insns++;
1589 i -= 6;
1591 i -= 2;
1592 } while (remainder);
1593 return num_insns;
1596 /* Emit an instruction with the indicated PATTERN. If COND is
1597 non-NULL, conditionalize the execution of the instruction on COND
1598 being true. */
1600 static void
1601 emit_constant_insn (rtx cond, rtx pattern)
1603 if (cond)
1604 pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1605 emit_insn (pattern);
1608 /* As above, but extra parameter GENERATE which, if clear, suppresses
1609 RTL generation. */
1611 static int
1612 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1613 HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1614 int generate)
1616 int can_invert = 0;
1617 int can_negate = 0;
1618 int can_negate_initial = 0;
1619 int can_shift = 0;
1620 int i;
1621 int num_bits_set = 0;
1622 int set_sign_bit_copies = 0;
1623 int clear_sign_bit_copies = 0;
1624 int clear_zero_bit_copies = 0;
1625 int set_zero_bit_copies = 0;
1626 int insns = 0;
1627 unsigned HOST_WIDE_INT temp1, temp2;
1628 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1630 /* Find out which operations are safe for a given CODE. Also do a quick
1631 check for degenerate cases; these can occur when DImode operations
1632 are split. */
1633 switch (code)
1635 case SET:
1636 can_invert = 1;
1637 can_shift = 1;
1638 can_negate = 1;
1639 break;
1641 case PLUS:
1642 can_negate = 1;
1643 can_negate_initial = 1;
1644 break;
1646 case IOR:
1647 if (remainder == 0xffffffff)
1649 if (generate)
1650 emit_constant_insn (cond,
1651 gen_rtx_SET (VOIDmode, target,
1652 GEN_INT (ARM_SIGN_EXTEND (val))));
1653 return 1;
1655 if (remainder == 0)
1657 if (reload_completed && rtx_equal_p (target, source))
1658 return 0;
1659 if (generate)
1660 emit_constant_insn (cond,
1661 gen_rtx_SET (VOIDmode, target, source));
1662 return 1;
1664 break;
1666 case AND:
1667 if (remainder == 0)
1669 if (generate)
1670 emit_constant_insn (cond,
1671 gen_rtx_SET (VOIDmode, target, const0_rtx));
1672 return 1;
1674 if (remainder == 0xffffffff)
1676 if (reload_completed && rtx_equal_p (target, source))
1677 return 0;
1678 if (generate)
1679 emit_constant_insn (cond,
1680 gen_rtx_SET (VOIDmode, target, source));
1681 return 1;
1683 can_invert = 1;
1684 break;
1686 case XOR:
1687 if (remainder == 0)
1689 if (reload_completed && rtx_equal_p (target, source))
1690 return 0;
1691 if (generate)
1692 emit_constant_insn (cond,
1693 gen_rtx_SET (VOIDmode, target, source));
1694 return 1;
1696 if (remainder == 0xffffffff)
1698 if (generate)
1699 emit_constant_insn (cond,
1700 gen_rtx_SET (VOIDmode, target,
1701 gen_rtx_NOT (mode, source)));
1702 return 1;
1705 /* We don't know how to handle this yet below. */
1706 abort ();
1708 case MINUS:
1709 /* We treat MINUS as (val - source), since (source - val) is always
1710 passed as (source + (-val)). */
1711 if (remainder == 0)
1713 if (generate)
1714 emit_constant_insn (cond,
1715 gen_rtx_SET (VOIDmode, target,
1716 gen_rtx_NEG (mode, source)));
1717 return 1;
1719 if (const_ok_for_arm (val))
1721 if (generate)
1722 emit_constant_insn (cond,
1723 gen_rtx_SET (VOIDmode, target,
1724 gen_rtx_MINUS (mode, GEN_INT (val),
1725 source)));
1726 return 1;
1728 can_negate = 1;
1730 break;
1732 default:
1733 abort ();
1736 /* If we can do it in one insn get out quickly. */
1737 if (const_ok_for_arm (val)
1738 || (can_negate_initial && const_ok_for_arm (-val))
1739 || (can_invert && const_ok_for_arm (~val)))
1741 if (generate)
1742 emit_constant_insn (cond,
1743 gen_rtx_SET (VOIDmode, target,
1744 (source
1745 ? gen_rtx_fmt_ee (code, mode, source,
1746 GEN_INT (val))
1747 : GEN_INT (val))));
1748 return 1;
1751 /* Calculate a few attributes that may be useful for specific
1752 optimizations. */
1753 for (i = 31; i >= 0; i--)
1755 if ((remainder & (1 << i)) == 0)
1756 clear_sign_bit_copies++;
1757 else
1758 break;
1761 for (i = 31; i >= 0; i--)
1763 if ((remainder & (1 << i)) != 0)
1764 set_sign_bit_copies++;
1765 else
1766 break;
1769 for (i = 0; i <= 31; i++)
1771 if ((remainder & (1 << i)) == 0)
1772 clear_zero_bit_copies++;
1773 else
1774 break;
1777 for (i = 0; i <= 31; i++)
1779 if ((remainder & (1 << i)) != 0)
1780 set_zero_bit_copies++;
1781 else
1782 break;
1785 switch (code)
1787 case SET:
1788 /* See if we can do this by sign_extending a constant that is known
1789 to be negative. This is a good, way of doing it, since the shift
1790 may well merge into a subsequent insn. */
1791 if (set_sign_bit_copies > 1)
1793 if (const_ok_for_arm
1794 (temp1 = ARM_SIGN_EXTEND (remainder
1795 << (set_sign_bit_copies - 1))))
1797 if (generate)
1799 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1800 emit_constant_insn (cond,
1801 gen_rtx_SET (VOIDmode, new_src,
1802 GEN_INT (temp1)));
1803 emit_constant_insn (cond,
1804 gen_ashrsi3 (target, new_src,
1805 GEN_INT (set_sign_bit_copies - 1)));
1807 return 2;
1809 /* For an inverted constant, we will need to set the low bits,
1810 these will be shifted out of harm's way. */
1811 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1812 if (const_ok_for_arm (~temp1))
1814 if (generate)
1816 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1817 emit_constant_insn (cond,
1818 gen_rtx_SET (VOIDmode, new_src,
1819 GEN_INT (temp1)));
1820 emit_constant_insn (cond,
1821 gen_ashrsi3 (target, new_src,
1822 GEN_INT (set_sign_bit_copies - 1)));
1824 return 2;
1828 /* See if we can generate this by setting the bottom (or the top)
1829 16 bits, and then shifting these into the other half of the
1830 word. We only look for the simplest cases, to do more would cost
1831 too much. Be careful, however, not to generate this when the
1832 alternative would take fewer insns. */
1833 if (val & 0xffff0000)
1835 temp1 = remainder & 0xffff0000;
1836 temp2 = remainder & 0x0000ffff;
1838 /* Overlaps outside this range are best done using other methods. */
1839 for (i = 9; i < 24; i++)
1841 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1842 && !const_ok_for_arm (temp2))
1844 rtx new_src = (subtargets
1845 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1846 : target);
1847 insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1848 source, subtargets, generate);
1849 source = new_src;
1850 if (generate)
1851 emit_constant_insn
1852 (cond,
1853 gen_rtx_SET
1854 (VOIDmode, target,
1855 gen_rtx_IOR (mode,
1856 gen_rtx_ASHIFT (mode, source,
1857 GEN_INT (i)),
1858 source)));
1859 return insns + 1;
1863 /* Don't duplicate cases already considered. */
1864 for (i = 17; i < 24; i++)
1866 if (((temp1 | (temp1 >> i)) == remainder)
1867 && !const_ok_for_arm (temp1))
1869 rtx new_src = (subtargets
1870 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1871 : target);
1872 insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1873 source, subtargets, generate);
1874 source = new_src;
1875 if (generate)
1876 emit_constant_insn
1877 (cond,
1878 gen_rtx_SET (VOIDmode, target,
1879 gen_rtx_IOR
1880 (mode,
1881 gen_rtx_LSHIFTRT (mode, source,
1882 GEN_INT (i)),
1883 source)));
1884 return insns + 1;
1888 break;
1890 case IOR:
1891 case XOR:
1892 /* If we have IOR or XOR, and the constant can be loaded in a
1893 single instruction, and we can find a temporary to put it in,
1894 then this can be done in two instructions instead of 3-4. */
1895 if (subtargets
1896 /* TARGET can't be NULL if SUBTARGETS is 0 */
1897 || (reload_completed && !reg_mentioned_p (target, source)))
1899 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1901 if (generate)
1903 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1905 emit_constant_insn (cond,
1906 gen_rtx_SET (VOIDmode, sub,
1907 GEN_INT (val)));
1908 emit_constant_insn (cond,
1909 gen_rtx_SET (VOIDmode, target,
1910 gen_rtx_fmt_ee (code, mode,
1911 source, sub)));
1913 return 2;
1917 if (code == XOR)
1918 break;
1920 if (set_sign_bit_copies > 8
1921 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1923 if (generate)
1925 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1926 rtx shift = GEN_INT (set_sign_bit_copies);
1928 emit_constant_insn
1929 (cond,
1930 gen_rtx_SET (VOIDmode, sub,
1931 gen_rtx_NOT (mode,
1932 gen_rtx_ASHIFT (mode,
1933 source,
1934 shift))));
1935 emit_constant_insn
1936 (cond,
1937 gen_rtx_SET (VOIDmode, target,
1938 gen_rtx_NOT (mode,
1939 gen_rtx_LSHIFTRT (mode, sub,
1940 shift))));
1942 return 2;
1945 if (set_zero_bit_copies > 8
1946 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1948 if (generate)
1950 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1951 rtx shift = GEN_INT (set_zero_bit_copies);
1953 emit_constant_insn
1954 (cond,
1955 gen_rtx_SET (VOIDmode, sub,
1956 gen_rtx_NOT (mode,
1957 gen_rtx_LSHIFTRT (mode,
1958 source,
1959 shift))));
1960 emit_constant_insn
1961 (cond,
1962 gen_rtx_SET (VOIDmode, target,
1963 gen_rtx_NOT (mode,
1964 gen_rtx_ASHIFT (mode, sub,
1965 shift))));
1967 return 2;
1970 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1972 if (generate)
1974 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1975 emit_constant_insn (cond,
1976 gen_rtx_SET (VOIDmode, sub,
1977 gen_rtx_NOT (mode, source)));
1978 source = sub;
1979 if (subtargets)
1980 sub = gen_reg_rtx (mode);
1981 emit_constant_insn (cond,
1982 gen_rtx_SET (VOIDmode, sub,
1983 gen_rtx_AND (mode, source,
1984 GEN_INT (temp1))));
1985 emit_constant_insn (cond,
1986 gen_rtx_SET (VOIDmode, target,
1987 gen_rtx_NOT (mode, sub)));
1989 return 3;
1991 break;
1993 case AND:
1994 /* See if two shifts will do 2 or more insn's worth of work. */
1995 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1997 HOST_WIDE_INT shift_mask = ((0xffffffff
1998 << (32 - clear_sign_bit_copies))
1999 & 0xffffffff);
2001 if ((remainder | shift_mask) != 0xffffffff)
2003 if (generate)
2005 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2006 insns = arm_gen_constant (AND, mode, cond,
2007 remainder | shift_mask,
2008 new_src, source, subtargets, 1);
2009 source = new_src;
2011 else
2013 rtx targ = subtargets ? NULL_RTX : target;
2014 insns = arm_gen_constant (AND, mode, cond,
2015 remainder | shift_mask,
2016 targ, source, subtargets, 0);
2020 if (generate)
2022 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2023 rtx shift = GEN_INT (clear_sign_bit_copies);
2025 emit_insn (gen_ashlsi3 (new_src, source, shift));
2026 emit_insn (gen_lshrsi3 (target, new_src, shift));
2029 return insns + 2;
2032 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2034 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2036 if ((remainder | shift_mask) != 0xffffffff)
2038 if (generate)
2040 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2042 insns = arm_gen_constant (AND, mode, cond,
2043 remainder | shift_mask,
2044 new_src, source, subtargets, 1);
2045 source = new_src;
2047 else
2049 rtx targ = subtargets ? NULL_RTX : target;
2051 insns = arm_gen_constant (AND, mode, cond,
2052 remainder | shift_mask,
2053 targ, source, subtargets, 0);
2057 if (generate)
2059 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2060 rtx shift = GEN_INT (clear_zero_bit_copies);
2062 emit_insn (gen_lshrsi3 (new_src, source, shift));
2063 emit_insn (gen_ashlsi3 (target, new_src, shift));
2066 return insns + 2;
2069 break;
2071 default:
2072 break;
2075 for (i = 0; i < 32; i++)
2076 if (remainder & (1 << i))
2077 num_bits_set++;
2079 if (code == AND || (can_invert && num_bits_set > 16))
2080 remainder = (~remainder) & 0xffffffff;
2081 else if (code == PLUS && num_bits_set > 16)
2082 remainder = (-remainder) & 0xffffffff;
2083 else
2085 can_invert = 0;
2086 can_negate = 0;
2089 /* Now try and find a way of doing the job in either two or three
2090 instructions.
2091 We start by looking for the largest block of zeros that are aligned on
2092 a 2-bit boundary, we then fill up the temps, wrapping around to the
2093 top of the word when we drop off the bottom.
2094 In the worst case this code should produce no more than four insns. */
2096 int best_start = 0;
2097 int best_consecutive_zeros = 0;
2099 for (i = 0; i < 32; i += 2)
2101 int consecutive_zeros = 0;
2103 if (!(remainder & (3 << i)))
2105 while ((i < 32) && !(remainder & (3 << i)))
2107 consecutive_zeros += 2;
2108 i += 2;
2110 if (consecutive_zeros > best_consecutive_zeros)
2112 best_consecutive_zeros = consecutive_zeros;
2113 best_start = i - consecutive_zeros;
2115 i -= 2;
2119 /* So long as it won't require any more insns to do so, it's
2120 desirable to emit a small constant (in bits 0...9) in the last
2121 insn. This way there is more chance that it can be combined with
2122 a later addressing insn to form a pre-indexed load or store
2123 operation. Consider:
2125 *((volatile int *)0xe0000100) = 1;
2126 *((volatile int *)0xe0000110) = 2;
2128 We want this to wind up as:
2130 mov rA, #0xe0000000
2131 mov rB, #1
2132 str rB, [rA, #0x100]
2133 mov rB, #2
2134 str rB, [rA, #0x110]
2136 rather than having to synthesize both large constants from scratch.
2138 Therefore, we calculate how many insns would be required to emit
2139 the constant starting from `best_start', and also starting from
2140 zero (i.e. with bit 31 first to be output). If `best_start' doesn't
2141 yield a shorter sequence, we may as well use zero. */
2142 if (best_start != 0
2143 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2144 && (count_insns_for_constant (remainder, 0) <=
2145 count_insns_for_constant (remainder, best_start)))
2146 best_start = 0;
2148 /* Now start emitting the insns. */
2149 i = best_start;
2152 int end;
2154 if (i <= 0)
2155 i += 32;
2156 if (remainder & (3 << (i - 2)))
2158 end = i - 8;
2159 if (end < 0)
2160 end += 32;
2161 temp1 = remainder & ((0x0ff << end)
2162 | ((i < end) ? (0xff >> (32 - end)) : 0));
2163 remainder &= ~temp1;
2165 if (generate)
2167 rtx new_src, temp1_rtx;
2169 if (code == SET || code == MINUS)
2171 new_src = (subtargets ? gen_reg_rtx (mode) : target);
2172 if (can_invert && code != MINUS)
2173 temp1 = ~temp1;
2175 else
2177 if (remainder && subtargets)
2178 new_src = gen_reg_rtx (mode);
2179 else
2180 new_src = target;
2181 if (can_invert)
2182 temp1 = ~temp1;
2183 else if (can_negate)
2184 temp1 = -temp1;
2187 temp1 = trunc_int_for_mode (temp1, mode);
2188 temp1_rtx = GEN_INT (temp1);
2190 if (code == SET)
2192 else if (code == MINUS)
2193 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2194 else
2195 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2197 emit_constant_insn (cond,
2198 gen_rtx_SET (VOIDmode, new_src,
2199 temp1_rtx));
2200 source = new_src;
2203 if (code == SET)
2205 can_invert = 0;
2206 code = PLUS;
2208 else if (code == MINUS)
2209 code = PLUS;
2211 insns++;
2212 i -= 6;
2214 i -= 2;
2216 while (remainder);
2219 return insns;
2222 /* Canonicalize a comparison so that we are more likely to recognize it.
2223 This can be done for a few constant compares, where we can make the
2224 immediate value easier to load. */
2226 enum rtx_code
2227 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2229 unsigned HOST_WIDE_INT i = INTVAL (*op1);
2231 switch (code)
2233 case EQ:
2234 case NE:
2235 return code;
2237 case GT:
2238 case LE:
2239 if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2240 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2242 *op1 = GEN_INT (i + 1);
2243 return code == GT ? GE : LT;
2245 break;
2247 case GE:
2248 case LT:
2249 if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2250 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2252 *op1 = GEN_INT (i - 1);
2253 return code == GE ? GT : LE;
2255 break;
2257 case GTU:
2258 case LEU:
2259 if (i != ~((unsigned HOST_WIDE_INT) 0)
2260 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2262 *op1 = GEN_INT (i + 1);
2263 return code == GTU ? GEU : LTU;
2265 break;
2267 case GEU:
2268 case LTU:
2269 if (i != 0
2270 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2272 *op1 = GEN_INT (i - 1);
2273 return code == GEU ? GTU : LEU;
2275 break;
2277 default:
2278 abort ();
2281 return code;
2285 /* Define how to find the value returned by a function. */
2288 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2290 enum machine_mode mode;
2291 int unsignedp ATTRIBUTE_UNUSED;
2292 rtx r ATTRIBUTE_UNUSED;
2295 mode = TYPE_MODE (type);
2296 /* Promote integer types. */
2297 if (INTEGRAL_TYPE_P (type))
2298 PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2299 return LIBCALL_VALUE(mode);
2302 /* Determine the amount of memory needed to store the possible return
2303 registers of an untyped call. */
2305 arm_apply_result_size (void)
2307 int size = 16;
2309 if (TARGET_ARM)
2311 if (TARGET_HARD_FLOAT_ABI)
2313 if (TARGET_FPA)
2314 size += 12;
2315 if (TARGET_MAVERICK)
2316 size += 8;
2318 if (TARGET_IWMMXT_ABI)
2319 size += 8;
2322 return size;
2325 /* Decide whether a type should be returned in memory (true)
2326 or in a register (false). This is called by the macro
2327 RETURN_IN_MEMORY. */
2329 arm_return_in_memory (tree type)
2331 HOST_WIDE_INT size;
2333 if (!AGGREGATE_TYPE_P (type) &&
2334 !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2335 /* All simple types are returned in registers.
2336 For AAPCS, complex types are treated the same as aggregates. */
2337 return 0;
2339 size = int_size_in_bytes (type);
2341 if (arm_abi != ARM_ABI_APCS)
2343 /* ATPCS and later return aggregate types in memory only if they are
2344 larger than a word (or are variable size). */
2345 return (size < 0 || size > UNITS_PER_WORD);
2348 /* For the arm-wince targets we choose to be compatible with Microsoft's
2349 ARM and Thumb compilers, which always return aggregates in memory. */
2350 #ifndef ARM_WINCE
2351 /* All structures/unions bigger than one word are returned in memory.
2352 Also catch the case where int_size_in_bytes returns -1. In this case
2353 the aggregate is either huge or of variable size, and in either case
2354 we will want to return it via memory and not in a register. */
2355 if (size < 0 || size > UNITS_PER_WORD)
2356 return 1;
2358 if (TREE_CODE (type) == RECORD_TYPE)
2360 tree field;
2362 /* For a struct the APCS says that we only return in a register
2363 if the type is 'integer like' and every addressable element
2364 has an offset of zero. For practical purposes this means
2365 that the structure can have at most one non bit-field element
2366 and that this element must be the first one in the structure. */
2368 /* Find the first field, ignoring non FIELD_DECL things which will
2369 have been created by C++. */
2370 for (field = TYPE_FIELDS (type);
2371 field && TREE_CODE (field) != FIELD_DECL;
2372 field = TREE_CHAIN (field))
2373 continue;
2375 if (field == NULL)
2376 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
2378 /* Check that the first field is valid for returning in a register. */
2380 /* ... Floats are not allowed */
2381 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2382 return 1;
2384 /* ... Aggregates that are not themselves valid for returning in
2385 a register are not allowed. */
2386 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2387 return 1;
2389 /* Now check the remaining fields, if any. Only bitfields are allowed,
2390 since they are not addressable. */
2391 for (field = TREE_CHAIN (field);
2392 field;
2393 field = TREE_CHAIN (field))
2395 if (TREE_CODE (field) != FIELD_DECL)
2396 continue;
2398 if (!DECL_BIT_FIELD_TYPE (field))
2399 return 1;
2402 return 0;
2405 if (TREE_CODE (type) == UNION_TYPE)
2407 tree field;
2409 /* Unions can be returned in registers if every element is
2410 integral, or can be returned in an integer register. */
2411 for (field = TYPE_FIELDS (type);
2412 field;
2413 field = TREE_CHAIN (field))
2415 if (TREE_CODE (field) != FIELD_DECL)
2416 continue;
2418 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2419 return 1;
2421 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2422 return 1;
2425 return 0;
2427 #endif /* not ARM_WINCE */
2429 /* Return all other types in memory. */
2430 return 1;
2433 /* Indicate whether or not words of a double are in big-endian order. */
2436 arm_float_words_big_endian (void)
2438 if (TARGET_MAVERICK)
2439 return 0;
2441 /* For FPA, float words are always big-endian. For VFP, floats words
2442 follow the memory system mode. */
2444 if (TARGET_FPA)
2446 return 1;
2449 if (TARGET_VFP)
2450 return (TARGET_BIG_END ? 1 : 0);
2452 return 1;
2455 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2456 for a call to a function whose data type is FNTYPE.
2457 For a library call, FNTYPE is NULL. */
2458 void
2459 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2460 rtx libname ATTRIBUTE_UNUSED,
2461 tree fndecl ATTRIBUTE_UNUSED)
2463 /* On the ARM, the offset starts at 0. */
2464 pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2465 pcum->iwmmxt_nregs = 0;
2466 pcum->can_split = true;
2468 pcum->call_cookie = CALL_NORMAL;
2470 if (TARGET_LONG_CALLS)
2471 pcum->call_cookie = CALL_LONG;
2473 /* Check for long call/short call attributes. The attributes
2474 override any command line option. */
2475 if (fntype)
2477 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2478 pcum->call_cookie = CALL_SHORT;
2479 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2480 pcum->call_cookie = CALL_LONG;
2483 /* Varargs vectors are treated the same as long long.
2484 named_count avoids having to change the way arm handles 'named' */
2485 pcum->named_count = 0;
2486 pcum->nargs = 0;
2488 if (TARGET_REALLY_IWMMXT && fntype)
2490 tree fn_arg;
2492 for (fn_arg = TYPE_ARG_TYPES (fntype);
2493 fn_arg;
2494 fn_arg = TREE_CHAIN (fn_arg))
2495 pcum->named_count += 1;
2497 if (! pcum->named_count)
2498 pcum->named_count = INT_MAX;
2503 /* Return true if mode/type need doubleword alignment. */
2504 bool
2505 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2507 return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2508 || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2512 /* Determine where to put an argument to a function.
2513 Value is zero to push the argument on the stack,
2514 or a hard register in which to store the argument.
2516 MODE is the argument's machine mode.
2517 TYPE is the data type of the argument (as a tree).
2518 This is null for libcalls where that information may
2519 not be available.
2520 CUM is a variable of type CUMULATIVE_ARGS which gives info about
2521 the preceding args and about the function being called.
2522 NAMED is nonzero if this argument is a named parameter
2523 (otherwise it is an extra parameter matching an ellipsis). */
2526 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2527 tree type, int named)
2529 int nregs;
2531 /* Varargs vectors are treated the same as long long.
2532 named_count avoids having to change the way arm handles 'named' */
2533 if (TARGET_IWMMXT_ABI
2534 && arm_vector_mode_supported_p (mode)
2535 && pcum->named_count > pcum->nargs + 1)
2537 if (pcum->iwmmxt_nregs <= 9)
2538 return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2539 else
2541 pcum->can_split = false;
2542 return NULL_RTX;
2546 /* Put doubleword aligned quantities in even register pairs. */
2547 if (pcum->nregs & 1
2548 && ARM_DOUBLEWORD_ALIGN
2549 && arm_needs_doubleword_align (mode, type))
2550 pcum->nregs++;
2552 if (mode == VOIDmode)
2553 /* Compute operand 2 of the call insn. */
2554 return GEN_INT (pcum->call_cookie);
2556 /* Only allow splitting an arg between regs and memory if all preceding
2557 args were allocated to regs. For args passed by reference we only count
2558 the reference pointer. */
2559 if (pcum->can_split)
2560 nregs = 1;
2561 else
2562 nregs = ARM_NUM_REGS2 (mode, type);
2564 if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2565 return NULL_RTX;
2567 return gen_rtx_REG (mode, pcum->nregs);
2570 static int
2571 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2572 tree type, bool named ATTRIBUTE_UNUSED)
2574 int nregs = pcum->nregs;
2576 if (arm_vector_mode_supported_p (mode))
2577 return 0;
2579 if (NUM_ARG_REGS > nregs
2580 && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2581 && pcum->can_split)
2582 return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2584 return 0;
2587 /* Variable sized types are passed by reference. This is a GCC
2588 extension to the ARM ABI. */
2590 static bool
2591 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2592 enum machine_mode mode ATTRIBUTE_UNUSED,
2593 tree type, bool named ATTRIBUTE_UNUSED)
2595 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2598 /* Encode the current state of the #pragma [no_]long_calls. */
2599 typedef enum
2601 OFF, /* No #pramgma [no_]long_calls is in effect. */
2602 LONG, /* #pragma long_calls is in effect. */
2603 SHORT /* #pragma no_long_calls is in effect. */
2604 } arm_pragma_enum;
2606 static arm_pragma_enum arm_pragma_long_calls = OFF;
2608 void
2609 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2611 arm_pragma_long_calls = LONG;
2614 void
2615 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2617 arm_pragma_long_calls = SHORT;
2620 void
2621 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2623 arm_pragma_long_calls = OFF;
2626 /* Table of machine attributes. */
2627 const struct attribute_spec arm_attribute_table[] =
2629 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2630 /* Function calls made to this symbol must be done indirectly, because
2631 it may lie outside of the 26 bit addressing range of a normal function
2632 call. */
2633 { "long_call", 0, 0, false, true, true, NULL },
2634 /* Whereas these functions are always known to reside within the 26 bit
2635 addressing range. */
2636 { "short_call", 0, 0, false, true, true, NULL },
2637 /* Interrupt Service Routines have special prologue and epilogue requirements. */
2638 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
2639 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
2640 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2641 #ifdef ARM_PE
2642 /* ARM/PE has three new attributes:
2643 interfacearm - ?
2644 dllexport - for exporting a function/variable that will live in a dll
2645 dllimport - for importing a function/variable from a dll
2647 Microsoft allows multiple declspecs in one __declspec, separating
2648 them with spaces. We do NOT support this. Instead, use __declspec
2649 multiple times.
2651 { "dllimport", 0, 0, true, false, false, NULL },
2652 { "dllexport", 0, 0, true, false, false, NULL },
2653 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2654 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2655 { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
2656 { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
2657 { "notshared", 0, 0, false, true, false, arm_handle_notshared_attribute },
2658 #endif
2659 { NULL, 0, 0, false, false, false, NULL }
2662 /* Handle an attribute requiring a FUNCTION_DECL;
2663 arguments as in struct attribute_spec.handler. */
2664 static tree
2665 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2666 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2668 if (TREE_CODE (*node) != FUNCTION_DECL)
2670 warning ("%qs attribute only applies to functions",
2671 IDENTIFIER_POINTER (name));
2672 *no_add_attrs = true;
2675 return NULL_TREE;
2678 /* Handle an "interrupt" or "isr" attribute;
2679 arguments as in struct attribute_spec.handler. */
2680 static tree
2681 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2682 bool *no_add_attrs)
2684 if (DECL_P (*node))
2686 if (TREE_CODE (*node) != FUNCTION_DECL)
2688 warning ("%qs attribute only applies to functions",
2689 IDENTIFIER_POINTER (name));
2690 *no_add_attrs = true;
2692 /* FIXME: the argument if any is checked for type attributes;
2693 should it be checked for decl ones? */
2695 else
2697 if (TREE_CODE (*node) == FUNCTION_TYPE
2698 || TREE_CODE (*node) == METHOD_TYPE)
2700 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2702 warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
2703 *no_add_attrs = true;
2706 else if (TREE_CODE (*node) == POINTER_TYPE
2707 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2708 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2709 && arm_isr_value (args) != ARM_FT_UNKNOWN)
2711 *node = build_variant_type_copy (*node);
2712 TREE_TYPE (*node) = build_type_attribute_variant
2713 (TREE_TYPE (*node),
2714 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2715 *no_add_attrs = true;
2717 else
2719 /* Possibly pass this attribute on from the type to a decl. */
2720 if (flags & ((int) ATTR_FLAG_DECL_NEXT
2721 | (int) ATTR_FLAG_FUNCTION_NEXT
2722 | (int) ATTR_FLAG_ARRAY_NEXT))
2724 *no_add_attrs = true;
2725 return tree_cons (name, args, NULL_TREE);
2727 else
2729 warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
2734 return NULL_TREE;
2737 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2738 /* Handle the "notshared" attribute. This attribute is another way of
2739 requesting hidden visibility. ARM's compiler supports
2740 "__declspec(notshared)"; we support the same thing via an
2741 attribute. */
2743 static tree
2744 arm_handle_notshared_attribute (tree *node,
2745 tree name ATTRIBUTE_UNUSED,
2746 tree args ATTRIBUTE_UNUSED,
2747 int flags ATTRIBUTE_UNUSED,
2748 bool *no_add_attrs)
2750 tree decl = TYPE_NAME (*node);
2752 if (decl)
2754 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2755 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2756 *no_add_attrs = false;
2758 return NULL_TREE;
2760 #endif
2762 /* Return 0 if the attributes for two types are incompatible, 1 if they
2763 are compatible, and 2 if they are nearly compatible (which causes a
2764 warning to be generated). */
2765 static int
2766 arm_comp_type_attributes (tree type1, tree type2)
2768 int l1, l2, s1, s2;
2770 /* Check for mismatch of non-default calling convention. */
2771 if (TREE_CODE (type1) != FUNCTION_TYPE)
2772 return 1;
2774 /* Check for mismatched call attributes. */
2775 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2776 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2777 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2778 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2780 /* Only bother to check if an attribute is defined. */
2781 if (l1 | l2 | s1 | s2)
2783 /* If one type has an attribute, the other must have the same attribute. */
2784 if ((l1 != l2) || (s1 != s2))
2785 return 0;
2787 /* Disallow mixed attributes. */
2788 if ((l1 & s2) || (l2 & s1))
2789 return 0;
2792 /* Check for mismatched ISR attribute. */
2793 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2794 if (! l1)
2795 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2796 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2797 if (! l2)
2798 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2799 if (l1 != l2)
2800 return 0;
2802 return 1;
2805 /* Encode long_call or short_call attribute by prefixing
2806 symbol name in DECL with a special character FLAG. */
2807 void
2808 arm_encode_call_attribute (tree decl, int flag)
2810 const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2811 int len = strlen (str);
2812 char * newstr;
2814 /* Do not allow weak functions to be treated as short call. */
2815 if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2816 return;
2818 newstr = alloca (len + 2);
2819 newstr[0] = flag;
2820 strcpy (newstr + 1, str);
2822 newstr = (char *) ggc_alloc_string (newstr, len + 1);
2823 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2826 /* Assigns default attributes to newly defined type. This is used to
2827 set short_call/long_call attributes for function types of
2828 functions defined inside corresponding #pragma scopes. */
2829 static void
2830 arm_set_default_type_attributes (tree type)
2832 /* Add __attribute__ ((long_call)) to all functions, when
2833 inside #pragma long_calls or __attribute__ ((short_call)),
2834 when inside #pragma no_long_calls. */
2835 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2837 tree type_attr_list, attr_name;
2838 type_attr_list = TYPE_ATTRIBUTES (type);
2840 if (arm_pragma_long_calls == LONG)
2841 attr_name = get_identifier ("long_call");
2842 else if (arm_pragma_long_calls == SHORT)
2843 attr_name = get_identifier ("short_call");
2844 else
2845 return;
2847 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2848 TYPE_ATTRIBUTES (type) = type_attr_list;
2852 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2853 defined within the current compilation unit. If this cannot be
2854 determined, then 0 is returned. */
2855 static int
2856 current_file_function_operand (rtx sym_ref)
2858 /* This is a bit of a fib. A function will have a short call flag
2859 applied to its name if it has the short call attribute, or it has
2860 already been defined within the current compilation unit. */
2861 if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2862 return 1;
2864 /* The current function is always defined within the current compilation
2865 unit. If it s a weak definition however, then this may not be the real
2866 definition of the function, and so we have to say no. */
2867 if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2868 && !DECL_WEAK (current_function_decl))
2869 return 1;
2871 /* We cannot make the determination - default to returning 0. */
2872 return 0;
2875 /* Return nonzero if a 32 bit "long_call" should be generated for
2876 this call. We generate a long_call if the function:
2878 a. has an __attribute__((long call))
2879 or b. is within the scope of a #pragma long_calls
2880 or c. the -mlong-calls command line switch has been specified
2881 . and either:
2882 1. -ffunction-sections is in effect
2883 or 2. the current function has __attribute__ ((section))
2884 or 3. the target function has __attribute__ ((section))
2886 However we do not generate a long call if the function:
2888 d. has an __attribute__ ((short_call))
2889 or e. is inside the scope of a #pragma no_long_calls
2890 or f. is defined within the current compilation unit.
2892 This function will be called by C fragments contained in the machine
2893 description file. SYM_REF and CALL_COOKIE correspond to the matched
2894 rtl operands. CALL_SYMBOL is used to distinguish between
2895 two different callers of the function. It is set to 1 in the
2896 "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2897 and "call_value" patterns. This is because of the difference in the
2898 SYM_REFs passed by these patterns. */
2900 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2902 if (!call_symbol)
2904 if (GET_CODE (sym_ref) != MEM)
2905 return 0;
2907 sym_ref = XEXP (sym_ref, 0);
2910 if (GET_CODE (sym_ref) != SYMBOL_REF)
2911 return 0;
2913 if (call_cookie & CALL_SHORT)
2914 return 0;
2916 if (TARGET_LONG_CALLS)
2918 if (flag_function_sections
2919 || DECL_SECTION_NAME (current_function_decl))
2920 /* c.3 is handled by the definition of the
2921 ARM_DECLARE_FUNCTION_SIZE macro. */
2922 return 1;
2925 if (current_file_function_operand (sym_ref))
2926 return 0;
2928 return (call_cookie & CALL_LONG)
2929 || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2930 || TARGET_LONG_CALLS;
2933 /* Return nonzero if it is ok to make a tail-call to DECL. */
2934 static bool
2935 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
2937 int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2939 if (cfun->machine->sibcall_blocked)
2940 return false;
2942 /* Never tailcall something for which we have no decl, or if we
2943 are in Thumb mode. */
2944 if (decl == NULL || TARGET_THUMB)
2945 return false;
2947 /* Get the calling method. */
2948 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2949 call_type = CALL_SHORT;
2950 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2951 call_type = CALL_LONG;
2953 /* Cannot tail-call to long calls, since these are out of range of
2954 a branch instruction. However, if not compiling PIC, we know
2955 we can reach the symbol if it is in this compilation unit. */
2956 if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2957 return false;
2959 /* If we are interworking and the function is not declared static
2960 then we can't tail-call it unless we know that it exists in this
2961 compilation unit (since it might be a Thumb routine). */
2962 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2963 return false;
2965 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
2966 if (IS_INTERRUPT (arm_current_func_type ()))
2967 return false;
2969 /* Everything else is ok. */
2970 return true;
2974 /* Addressing mode support functions. */
2976 /* Return nonzero if X is a legitimate immediate operand when compiling
2977 for PIC. */
2979 legitimate_pic_operand_p (rtx x)
2981 if (CONSTANT_P (x)
2982 && flag_pic
2983 && (GET_CODE (x) == SYMBOL_REF
2984 || (GET_CODE (x) == CONST
2985 && GET_CODE (XEXP (x, 0)) == PLUS
2986 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2987 return 0;
2989 return 1;
2993 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
2995 if (GET_CODE (orig) == SYMBOL_REF
2996 || GET_CODE (orig) == LABEL_REF)
2998 #ifndef AOF_ASSEMBLER
2999 rtx pic_ref, address;
3000 #endif
3001 rtx insn;
3002 int subregs = 0;
3004 if (reg == 0)
3006 if (no_new_pseudos)
3007 abort ();
3008 else
3009 reg = gen_reg_rtx (Pmode);
3011 subregs = 1;
3014 #ifdef AOF_ASSEMBLER
3015 /* The AOF assembler can generate relocations for these directly, and
3016 understands that the PIC register has to be added into the offset. */
3017 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3018 #else
3019 if (subregs)
3020 address = gen_reg_rtx (Pmode);
3021 else
3022 address = reg;
3024 if (TARGET_ARM)
3025 emit_insn (gen_pic_load_addr_arm (address, orig));
3026 else
3027 emit_insn (gen_pic_load_addr_thumb (address, orig));
3029 if ((GET_CODE (orig) == LABEL_REF
3030 || (GET_CODE (orig) == SYMBOL_REF &&
3031 SYMBOL_REF_LOCAL_P (orig)))
3032 && NEED_GOT_RELOC)
3033 pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
3034 else
3036 pic_ref = gen_const_mem (Pmode,
3037 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3038 address));
3041 insn = emit_move_insn (reg, pic_ref);
3042 #endif
3043 current_function_uses_pic_offset_table = 1;
3044 /* Put a REG_EQUAL note on this insn, so that it can be optimized
3045 by loop. */
3046 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3047 REG_NOTES (insn));
3048 return reg;
3050 else if (GET_CODE (orig) == CONST)
3052 rtx base, offset;
3054 if (GET_CODE (XEXP (orig, 0)) == PLUS
3055 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3056 return orig;
3058 if (reg == 0)
3060 if (no_new_pseudos)
3061 abort ();
3062 else
3063 reg = gen_reg_rtx (Pmode);
3066 if (GET_CODE (XEXP (orig, 0)) == PLUS)
3068 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3069 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3070 base == reg ? 0 : reg);
3072 else
3073 abort ();
3075 if (GET_CODE (offset) == CONST_INT)
3077 /* The base register doesn't really matter, we only want to
3078 test the index for the appropriate mode. */
3079 if (!arm_legitimate_index_p (mode, offset, SET, 0))
3081 if (!no_new_pseudos)
3082 offset = force_reg (Pmode, offset);
3083 else
3084 abort ();
3087 if (GET_CODE (offset) == CONST_INT)
3088 return plus_constant (base, INTVAL (offset));
3091 if (GET_MODE_SIZE (mode) > 4
3092 && (GET_MODE_CLASS (mode) == MODE_INT
3093 || TARGET_SOFT_FLOAT))
3095 emit_insn (gen_addsi3 (reg, base, offset));
3096 return reg;
3099 return gen_rtx_PLUS (Pmode, base, offset);
3102 return orig;
3106 /* Find a spare low register to use during the prolog of a function. */
3108 static int
3109 thumb_find_work_register (unsigned long pushed_regs_mask)
3111 int reg;
3113 /* Check the argument registers first as these are call-used. The
3114 register allocation order means that sometimes r3 might be used
3115 but earlier argument registers might not, so check them all. */
3116 for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3117 if (!regs_ever_live[reg])
3118 return reg;
3120 /* Before going on to check the call-saved registers we can try a couple
3121 more ways of deducing that r3 is available. The first is when we are
3122 pushing anonymous arguments onto the stack and we have less than 4
3123 registers worth of fixed arguments(*). In this case r3 will be part of
3124 the variable argument list and so we can be sure that it will be
3125 pushed right at the start of the function. Hence it will be available
3126 for the rest of the prologue.
3127 (*): ie current_function_pretend_args_size is greater than 0. */
3128 if (cfun->machine->uses_anonymous_args
3129 && current_function_pretend_args_size > 0)
3130 return LAST_ARG_REGNUM;
3132 /* The other case is when we have fixed arguments but less than 4 registers
3133 worth. In this case r3 might be used in the body of the function, but
3134 it is not being used to convey an argument into the function. In theory
3135 we could just check current_function_args_size to see how many bytes are
3136 being passed in argument registers, but it seems that it is unreliable.
3137 Sometimes it will have the value 0 when in fact arguments are being
3138 passed. (See testcase execute/20021111-1.c for an example). So we also
3139 check the args_info.nregs field as well. The problem with this field is
3140 that it makes no allowances for arguments that are passed to the
3141 function but which are not used. Hence we could miss an opportunity
3142 when a function has an unused argument in r3. But it is better to be
3143 safe than to be sorry. */
3144 if (! cfun->machine->uses_anonymous_args
3145 && current_function_args_size >= 0
3146 && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3147 && cfun->args_info.nregs < 4)
3148 return LAST_ARG_REGNUM;
3150 /* Otherwise look for a call-saved register that is going to be pushed. */
3151 for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3152 if (pushed_regs_mask & (1 << reg))
3153 return reg;
3155 /* Something went wrong - thumb_compute_save_reg_mask()
3156 should have arranged for a suitable register to be pushed. */
3157 abort ();
3161 /* Generate code to load the PIC register. In thumb mode SCRATCH is a
3162 low register. */
3164 void
3165 arm_load_pic_register (unsigned int scratch)
3167 #ifndef AOF_ASSEMBLER
3168 rtx l1, pic_tmp, pic_tmp2, pic_rtx;
3169 rtx global_offset_table;
3171 if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3172 return;
3174 if (!flag_pic)
3175 abort ();
3177 l1 = gen_label_rtx ();
3179 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3180 /* On the ARM the PC register contains 'dot + 8' at the time of the
3181 addition, on the Thumb it is 'dot + 4'. */
3182 pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
3183 if (GOT_PCREL)
3184 pic_tmp2 = gen_rtx_CONST (VOIDmode,
3185 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3186 else
3187 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3189 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3191 if (TARGET_ARM)
3193 emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3194 emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
3196 else
3198 if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3200 /* We will have pushed the pic register, so should always be
3201 able to find a work register. */
3202 pic_tmp = gen_rtx_REG (SImode, scratch);
3203 emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3204 emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3206 else
3207 emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3208 emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
3211 /* Need to emit this whether or not we obey regdecls,
3212 since setjmp/longjmp can cause life info to screw up. */
3213 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3214 #endif /* AOF_ASSEMBLER */
3218 /* Return nonzero if X is valid as an ARM state addressing register. */
3219 static int
3220 arm_address_register_rtx_p (rtx x, int strict_p)
3222 int regno;
3224 if (GET_CODE (x) != REG)
3225 return 0;
3227 regno = REGNO (x);
3229 if (strict_p)
3230 return ARM_REGNO_OK_FOR_BASE_P (regno);
3232 return (regno <= LAST_ARM_REGNUM
3233 || regno >= FIRST_PSEUDO_REGISTER
3234 || regno == FRAME_POINTER_REGNUM
3235 || regno == ARG_POINTER_REGNUM);
3238 /* Return nonzero if X is a valid ARM state address operand. */
3240 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3241 int strict_p)
3243 bool use_ldrd;
3244 enum rtx_code code = GET_CODE (x);
3246 if (arm_address_register_rtx_p (x, strict_p))
3247 return 1;
3249 use_ldrd = (TARGET_LDRD
3250 && (mode == DImode
3251 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3253 if (code == POST_INC || code == PRE_DEC
3254 || ((code == PRE_INC || code == POST_DEC)
3255 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3256 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3258 else if ((code == POST_MODIFY || code == PRE_MODIFY)
3259 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3260 && GET_CODE (XEXP (x, 1)) == PLUS
3261 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3263 rtx addend = XEXP (XEXP (x, 1), 1);
3265 /* Don't allow ldrd post increment by register because it's hard
3266 to fixup invalid register choices. */
3267 if (use_ldrd
3268 && GET_CODE (x) == POST_MODIFY
3269 && GET_CODE (addend) == REG)
3270 return 0;
3272 return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3273 && arm_legitimate_index_p (mode, addend, outer, strict_p));
3276 /* After reload constants split into minipools will have addresses
3277 from a LABEL_REF. */
3278 else if (reload_completed
3279 && (code == LABEL_REF
3280 || (code == CONST
3281 && GET_CODE (XEXP (x, 0)) == PLUS
3282 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3283 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3284 return 1;
3286 else if (mode == TImode)
3287 return 0;
3289 else if (code == PLUS)
3291 rtx xop0 = XEXP (x, 0);
3292 rtx xop1 = XEXP (x, 1);
3294 return ((arm_address_register_rtx_p (xop0, strict_p)
3295 && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3296 || (arm_address_register_rtx_p (xop1, strict_p)
3297 && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3300 #if 0
3301 /* Reload currently can't handle MINUS, so disable this for now */
3302 else if (GET_CODE (x) == MINUS)
3304 rtx xop0 = XEXP (x, 0);
3305 rtx xop1 = XEXP (x, 1);
3307 return (arm_address_register_rtx_p (xop0, strict_p)
3308 && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3310 #endif
3312 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3313 && code == SYMBOL_REF
3314 && CONSTANT_POOL_ADDRESS_P (x)
3315 && ! (flag_pic
3316 && symbol_mentioned_p (get_pool_constant (x))))
3317 return 1;
3319 return 0;
3322 /* Return nonzero if INDEX is valid for an address index operand in
3323 ARM state. */
3324 static int
3325 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3326 int strict_p)
3328 HOST_WIDE_INT range;
3329 enum rtx_code code = GET_CODE (index);
3331 /* Standard coprocessor addressing modes. */
3332 if (TARGET_HARD_FLOAT
3333 && (TARGET_FPA || TARGET_MAVERICK)
3334 && (GET_MODE_CLASS (mode) == MODE_FLOAT
3335 || (TARGET_MAVERICK && mode == DImode)))
3336 return (code == CONST_INT && INTVAL (index) < 1024
3337 && INTVAL (index) > -1024
3338 && (INTVAL (index) & 3) == 0);
3340 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3341 return (code == CONST_INT
3342 && INTVAL (index) < 1024
3343 && INTVAL (index) > -1024
3344 && (INTVAL (index) & 3) == 0);
3346 if (arm_address_register_rtx_p (index, strict_p)
3347 && (GET_MODE_SIZE (mode) <= 4))
3348 return 1;
3350 if (mode == DImode || mode == DFmode)
3352 if (code == CONST_INT)
3354 HOST_WIDE_INT val = INTVAL (index);
3356 if (TARGET_LDRD)
3357 return val > -256 && val < 256;
3358 else
3359 return val > -4096 && val < 4092;
3362 return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3365 if (GET_MODE_SIZE (mode) <= 4
3366 && ! (arm_arch4
3367 && (mode == HImode
3368 || (mode == QImode && outer == SIGN_EXTEND))))
3370 if (code == MULT)
3372 rtx xiop0 = XEXP (index, 0);
3373 rtx xiop1 = XEXP (index, 1);
3375 return ((arm_address_register_rtx_p (xiop0, strict_p)
3376 && power_of_two_operand (xiop1, SImode))
3377 || (arm_address_register_rtx_p (xiop1, strict_p)
3378 && power_of_two_operand (xiop0, SImode)));
3380 else if (code == LSHIFTRT || code == ASHIFTRT
3381 || code == ASHIFT || code == ROTATERT)
3383 rtx op = XEXP (index, 1);
3385 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3386 && GET_CODE (op) == CONST_INT
3387 && INTVAL (op) > 0
3388 && INTVAL (op) <= 31);
3392 /* For ARM v4 we may be doing a sign-extend operation during the
3393 load. */
3394 if (arm_arch4)
3396 if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3397 range = 256;
3398 else
3399 range = 4096;
3401 else
3402 range = (mode == HImode) ? 4095 : 4096;
3404 return (code == CONST_INT
3405 && INTVAL (index) < range
3406 && INTVAL (index) > -range);
3409 /* Return nonzero if X is valid as a Thumb state base register. */
3410 static int
3411 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3413 int regno;
3415 if (GET_CODE (x) != REG)
3416 return 0;
3418 regno = REGNO (x);
3420 if (strict_p)
3421 return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3423 return (regno <= LAST_LO_REGNUM
3424 || regno > LAST_VIRTUAL_REGISTER
3425 || regno == FRAME_POINTER_REGNUM
3426 || (GET_MODE_SIZE (mode) >= 4
3427 && (regno == STACK_POINTER_REGNUM
3428 || regno >= FIRST_PSEUDO_REGISTER
3429 || x == hard_frame_pointer_rtx
3430 || x == arg_pointer_rtx)));
3433 /* Return nonzero if x is a legitimate index register. This is the case
3434 for any base register that can access a QImode object. */
3435 inline static int
3436 thumb_index_register_rtx_p (rtx x, int strict_p)
3438 return thumb_base_register_rtx_p (x, QImode, strict_p);
3441 /* Return nonzero if x is a legitimate Thumb-state address.
3443 The AP may be eliminated to either the SP or the FP, so we use the
3444 least common denominator, e.g. SImode, and offsets from 0 to 64.
3446 ??? Verify whether the above is the right approach.
3448 ??? Also, the FP may be eliminated to the SP, so perhaps that
3449 needs special handling also.
3451 ??? Look at how the mips16 port solves this problem. It probably uses
3452 better ways to solve some of these problems.
3454 Although it is not incorrect, we don't accept QImode and HImode
3455 addresses based on the frame pointer or arg pointer until the
3456 reload pass starts. This is so that eliminating such addresses
3457 into stack based ones won't produce impossible code. */
3459 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3461 /* ??? Not clear if this is right. Experiment. */
3462 if (GET_MODE_SIZE (mode) < 4
3463 && !(reload_in_progress || reload_completed)
3464 && (reg_mentioned_p (frame_pointer_rtx, x)
3465 || reg_mentioned_p (arg_pointer_rtx, x)
3466 || reg_mentioned_p (virtual_incoming_args_rtx, x)
3467 || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3468 || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3469 || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3470 return 0;
3472 /* Accept any base register. SP only in SImode or larger. */
3473 else if (thumb_base_register_rtx_p (x, mode, strict_p))
3474 return 1;
3476 /* This is PC relative data before arm_reorg runs. */
3477 else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3478 && GET_CODE (x) == SYMBOL_REF
3479 && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3480 return 1;
3482 /* This is PC relative data after arm_reorg runs. */
3483 else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3484 && (GET_CODE (x) == LABEL_REF
3485 || (GET_CODE (x) == CONST
3486 && GET_CODE (XEXP (x, 0)) == PLUS
3487 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3488 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3489 return 1;
3491 /* Post-inc indexing only supported for SImode and larger. */
3492 else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3493 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3494 return 1;
3496 else if (GET_CODE (x) == PLUS)
3498 /* REG+REG address can be any two index registers. */
3499 /* We disallow FRAME+REG addressing since we know that FRAME
3500 will be replaced with STACK, and SP relative addressing only
3501 permits SP+OFFSET. */
3502 if (GET_MODE_SIZE (mode) <= 4
3503 && XEXP (x, 0) != frame_pointer_rtx
3504 && XEXP (x, 1) != frame_pointer_rtx
3505 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3506 && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3507 return 1;
3509 /* REG+const has 5-7 bit offset for non-SP registers. */
3510 else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3511 || XEXP (x, 0) == arg_pointer_rtx)
3512 && GET_CODE (XEXP (x, 1)) == CONST_INT
3513 && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3514 return 1;
3516 /* REG+const has 10 bit offset for SP, but only SImode and
3517 larger is supported. */
3518 /* ??? Should probably check for DI/DFmode overflow here
3519 just like GO_IF_LEGITIMATE_OFFSET does. */
3520 else if (GET_CODE (XEXP (x, 0)) == REG
3521 && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3522 && GET_MODE_SIZE (mode) >= 4
3523 && GET_CODE (XEXP (x, 1)) == CONST_INT
3524 && INTVAL (XEXP (x, 1)) >= 0
3525 && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3526 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3527 return 1;
3529 else if (GET_CODE (XEXP (x, 0)) == REG
3530 && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3531 && GET_MODE_SIZE (mode) >= 4
3532 && GET_CODE (XEXP (x, 1)) == CONST_INT
3533 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3534 return 1;
3537 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3538 && GET_MODE_SIZE (mode) == 4
3539 && GET_CODE (x) == SYMBOL_REF
3540 && CONSTANT_POOL_ADDRESS_P (x)
3541 && !(flag_pic
3542 && symbol_mentioned_p (get_pool_constant (x))))
3543 return 1;
3545 return 0;
3548 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3549 instruction of mode MODE. */
3551 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3553 switch (GET_MODE_SIZE (mode))
3555 case 1:
3556 return val >= 0 && val < 32;
3558 case 2:
3559 return val >= 0 && val < 64 && (val & 1) == 0;
3561 default:
3562 return (val >= 0
3563 && (val + GET_MODE_SIZE (mode)) <= 128
3564 && (val & 3) == 0);
3568 /* Try machine-dependent ways of modifying an illegitimate address
3569 to be legitimate. If we find one, return the new, valid address. */
3571 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3573 if (GET_CODE (x) == PLUS)
3575 rtx xop0 = XEXP (x, 0);
3576 rtx xop1 = XEXP (x, 1);
3578 if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3579 xop0 = force_reg (SImode, xop0);
3581 if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3582 xop1 = force_reg (SImode, xop1);
3584 if (ARM_BASE_REGISTER_RTX_P (xop0)
3585 && GET_CODE (xop1) == CONST_INT)
3587 HOST_WIDE_INT n, low_n;
3588 rtx base_reg, val;
3589 n = INTVAL (xop1);
3591 /* VFP addressing modes actually allow greater offsets, but for
3592 now we just stick with the lowest common denominator. */
3593 if (mode == DImode
3594 || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3596 low_n = n & 0x0f;
3597 n &= ~0x0f;
3598 if (low_n > 4)
3600 n += 16;
3601 low_n -= 16;
3604 else
3606 low_n = ((mode) == TImode ? 0
3607 : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3608 n -= low_n;
3611 base_reg = gen_reg_rtx (SImode);
3612 val = force_operand (gen_rtx_PLUS (SImode, xop0,
3613 GEN_INT (n)), NULL_RTX);
3614 emit_move_insn (base_reg, val);
3615 x = (low_n == 0 ? base_reg
3616 : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3618 else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3619 x = gen_rtx_PLUS (SImode, xop0, xop1);
3622 /* XXX We don't allow MINUS any more -- see comment in
3623 arm_legitimate_address_p (). */
3624 else if (GET_CODE (x) == MINUS)
3626 rtx xop0 = XEXP (x, 0);
3627 rtx xop1 = XEXP (x, 1);
3629 if (CONSTANT_P (xop0))
3630 xop0 = force_reg (SImode, xop0);
3632 if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3633 xop1 = force_reg (SImode, xop1);
3635 if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3636 x = gen_rtx_MINUS (SImode, xop0, xop1);
3639 if (flag_pic)
3641 /* We need to find and carefully transform any SYMBOL and LABEL
3642 references; so go back to the original address expression. */
3643 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3645 if (new_x != orig_x)
3646 x = new_x;
3649 return x;
3653 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3654 to be legitimate. If we find one, return the new, valid address. */
3656 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3658 if (GET_CODE (x) == PLUS
3659 && GET_CODE (XEXP (x, 1)) == CONST_INT
3660 && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3661 || INTVAL (XEXP (x, 1)) < 0))
3663 rtx xop0 = XEXP (x, 0);
3664 rtx xop1 = XEXP (x, 1);
3665 HOST_WIDE_INT offset = INTVAL (xop1);
3667 /* Try and fold the offset into a biasing of the base register and
3668 then offsetting that. Don't do this when optimizing for space
3669 since it can cause too many CSEs. */
3670 if (optimize_size && offset >= 0
3671 && offset < 256 + 31 * GET_MODE_SIZE (mode))
3673 HOST_WIDE_INT delta;
3675 if (offset >= 256)
3676 delta = offset - (256 - GET_MODE_SIZE (mode));
3677 else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3678 delta = 31 * GET_MODE_SIZE (mode);
3679 else
3680 delta = offset & (~31 * GET_MODE_SIZE (mode));
3682 xop0 = force_operand (plus_constant (xop0, offset - delta),
3683 NULL_RTX);
3684 x = plus_constant (xop0, delta);
3686 else if (offset < 0 && offset > -256)
3687 /* Small negative offsets are best done with a subtract before the
3688 dereference, forcing these into a register normally takes two
3689 instructions. */
3690 x = force_operand (x, NULL_RTX);
3691 else
3693 /* For the remaining cases, force the constant into a register. */
3694 xop1 = force_reg (SImode, xop1);
3695 x = gen_rtx_PLUS (SImode, xop0, xop1);
3698 else if (GET_CODE (x) == PLUS
3699 && s_register_operand (XEXP (x, 1), SImode)
3700 && !s_register_operand (XEXP (x, 0), SImode))
3702 rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3704 x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3707 if (flag_pic)
3709 /* We need to find and carefully transform any SYMBOL and LABEL
3710 references; so go back to the original address expression. */
3711 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3713 if (new_x != orig_x)
3714 x = new_x;
3717 return x;
3722 #define REG_OR_SUBREG_REG(X) \
3723 (GET_CODE (X) == REG \
3724 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3726 #define REG_OR_SUBREG_RTX(X) \
3727 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3729 #ifndef COSTS_N_INSNS
3730 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3731 #endif
3732 static inline int
3733 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3735 enum machine_mode mode = GET_MODE (x);
3737 switch (code)
3739 case ASHIFT:
3740 case ASHIFTRT:
3741 case LSHIFTRT:
3742 case ROTATERT:
3743 case PLUS:
3744 case MINUS:
3745 case COMPARE:
3746 case NEG:
3747 case NOT:
3748 return COSTS_N_INSNS (1);
3750 case MULT:
3751 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3753 int cycles = 0;
3754 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3756 while (i)
3758 i >>= 2;
3759 cycles++;
3761 return COSTS_N_INSNS (2) + cycles;
3763 return COSTS_N_INSNS (1) + 16;
3765 case SET:
3766 return (COSTS_N_INSNS (1)
3767 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3768 + GET_CODE (SET_DEST (x)) == MEM));
3770 case CONST_INT:
3771 if (outer == SET)
3773 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3774 return 0;
3775 if (thumb_shiftable_const (INTVAL (x)))
3776 return COSTS_N_INSNS (2);
3777 return COSTS_N_INSNS (3);
3779 else if ((outer == PLUS || outer == COMPARE)
3780 && INTVAL (x) < 256 && INTVAL (x) > -256)
3781 return 0;
3782 else if (outer == AND
3783 && INTVAL (x) < 256 && INTVAL (x) >= -256)
3784 return COSTS_N_INSNS (1);
3785 else if (outer == ASHIFT || outer == ASHIFTRT
3786 || outer == LSHIFTRT)
3787 return 0;
3788 return COSTS_N_INSNS (2);
3790 case CONST:
3791 case CONST_DOUBLE:
3792 case LABEL_REF:
3793 case SYMBOL_REF:
3794 return COSTS_N_INSNS (3);
3796 case UDIV:
3797 case UMOD:
3798 case DIV:
3799 case MOD:
3800 return 100;
3802 case TRUNCATE:
3803 return 99;
3805 case AND:
3806 case XOR:
3807 case IOR:
3808 /* XXX guess. */
3809 return 8;
3811 case MEM:
3812 /* XXX another guess. */
3813 /* Memory costs quite a lot for the first word, but subsequent words
3814 load at the equivalent of a single insn each. */
3815 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3816 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3817 ? 4 : 0));
3819 case IF_THEN_ELSE:
3820 /* XXX a guess. */
3821 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3822 return 14;
3823 return 2;
3825 case ZERO_EXTEND:
3826 /* XXX still guessing. */
3827 switch (GET_MODE (XEXP (x, 0)))
3829 case QImode:
3830 return (1 + (mode == DImode ? 4 : 0)
3831 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3833 case HImode:
3834 return (4 + (mode == DImode ? 4 : 0)
3835 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3837 case SImode:
3838 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3840 default:
3841 return 99;
3844 default:
3845 return 99;
3850 /* Worker routine for arm_rtx_costs. */
3851 static inline int
3852 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3854 enum machine_mode mode = GET_MODE (x);
3855 enum rtx_code subcode;
3856 int extra_cost;
3858 switch (code)
3860 case MEM:
3861 /* Memory costs quite a lot for the first word, but subsequent words
3862 load at the equivalent of a single insn each. */
3863 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3864 + (GET_CODE (x) == SYMBOL_REF
3865 && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3867 case DIV:
3868 case MOD:
3869 case UDIV:
3870 case UMOD:
3871 return optimize_size ? COSTS_N_INSNS (2) : 100;
3873 case ROTATE:
3874 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3875 return 4;
3876 /* Fall through */
3877 case ROTATERT:
3878 if (mode != SImode)
3879 return 8;
3880 /* Fall through */
3881 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3882 if (mode == DImode)
3883 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3884 + ((GET_CODE (XEXP (x, 0)) == REG
3885 || (GET_CODE (XEXP (x, 0)) == SUBREG
3886 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3887 ? 0 : 8));
3888 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3889 || (GET_CODE (XEXP (x, 0)) == SUBREG
3890 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3891 ? 0 : 4)
3892 + ((GET_CODE (XEXP (x, 1)) == REG
3893 || (GET_CODE (XEXP (x, 1)) == SUBREG
3894 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3895 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3896 ? 0 : 4));
3898 case MINUS:
3899 if (mode == DImode)
3900 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3901 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3902 || (GET_CODE (XEXP (x, 0)) == CONST_INT
3903 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3904 ? 0 : 8));
3906 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3907 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3908 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3909 && arm_const_double_rtx (XEXP (x, 1))))
3910 ? 0 : 8)
3911 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3912 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3913 && arm_const_double_rtx (XEXP (x, 0))))
3914 ? 0 : 8));
3916 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3917 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3918 && REG_OR_SUBREG_REG (XEXP (x, 1))))
3919 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3920 || subcode == ASHIFTRT || subcode == LSHIFTRT
3921 || subcode == ROTATE || subcode == ROTATERT
3922 || (subcode == MULT
3923 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3924 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3925 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3926 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3927 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3928 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3929 && REG_OR_SUBREG_REG (XEXP (x, 0))))
3930 return 1;
3931 /* Fall through */
3933 case PLUS:
3934 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3935 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3936 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3937 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3938 && arm_const_double_rtx (XEXP (x, 1))))
3939 ? 0 : 8));
3941 /* Fall through */
3942 case AND: case XOR: case IOR:
3943 extra_cost = 0;
3945 /* Normally the frame registers will be spilt into reg+const during
3946 reload, so it is a bad idea to combine them with other instructions,
3947 since then they might not be moved outside of loops. As a compromise
3948 we allow integration with ops that have a constant as their second
3949 operand. */
3950 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3951 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3952 && GET_CODE (XEXP (x, 1)) != CONST_INT)
3953 || (REG_OR_SUBREG_REG (XEXP (x, 0))
3954 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3955 extra_cost = 4;
3957 if (mode == DImode)
3958 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3959 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3960 || (GET_CODE (XEXP (x, 1)) == CONST_INT
3961 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3962 ? 0 : 8));
3964 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3965 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3966 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3967 || (GET_CODE (XEXP (x, 1)) == CONST_INT
3968 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3969 ? 0 : 4));
3971 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3972 return (1 + extra_cost
3973 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3974 || subcode == LSHIFTRT || subcode == ASHIFTRT
3975 || subcode == ROTATE || subcode == ROTATERT
3976 || (subcode == MULT
3977 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3978 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3979 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3980 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3981 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3982 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3983 ? 0 : 4));
3985 return 8;
3987 case MULT:
3988 /* This should have been handled by the CPU specific routines. */
3989 abort ();
3991 case TRUNCATE:
3992 if (arm_arch3m && mode == SImode
3993 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
3994 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
3995 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
3996 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
3997 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
3998 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
3999 return 8;
4000 return 99;
4002 case NEG:
4003 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4004 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4005 /* Fall through */
4006 case NOT:
4007 if (mode == DImode)
4008 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4010 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4012 case IF_THEN_ELSE:
4013 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4014 return 14;
4015 return 2;
4017 case COMPARE:
4018 return 1;
4020 case ABS:
4021 return 4 + (mode == DImode ? 4 : 0);
4023 case SIGN_EXTEND:
4024 if (GET_MODE (XEXP (x, 0)) == QImode)
4025 return (4 + (mode == DImode ? 4 : 0)
4026 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4027 /* Fall through */
4028 case ZERO_EXTEND:
4029 switch (GET_MODE (XEXP (x, 0)))
4031 case QImode:
4032 return (1 + (mode == DImode ? 4 : 0)
4033 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4035 case HImode:
4036 return (4 + (mode == DImode ? 4 : 0)
4037 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4039 case SImode:
4040 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4042 case V8QImode:
4043 case V4HImode:
4044 case V2SImode:
4045 case V4QImode:
4046 case V2HImode:
4047 return 1;
4049 default:
4050 break;
4052 abort ();
4054 case CONST_INT:
4055 if (const_ok_for_arm (INTVAL (x)))
4056 return outer == SET ? 2 : -1;
4057 else if (outer == AND
4058 && const_ok_for_arm (~INTVAL (x)))
4059 return -1;
4060 else if ((outer == COMPARE
4061 || outer == PLUS || outer == MINUS)
4062 && const_ok_for_arm (-INTVAL (x)))
4063 return -1;
4064 else
4065 return 5;
4067 case CONST:
4068 case LABEL_REF:
4069 case SYMBOL_REF:
4070 return 6;
4072 case CONST_DOUBLE:
4073 if (arm_const_double_rtx (x))
4074 return outer == SET ? 2 : -1;
4075 else if ((outer == COMPARE || outer == PLUS)
4076 && neg_const_double_rtx_ok_for_fpa (x))
4077 return -1;
4078 return 7;
4080 default:
4081 return 99;
4085 /* RTX costs when optimizing for size. */
4086 static bool
4087 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4089 enum machine_mode mode = GET_MODE (x);
4091 if (TARGET_THUMB)
4093 /* XXX TBD. For now, use the standard costs. */
4094 *total = thumb_rtx_costs (x, code, outer_code);
4095 return true;
4098 switch (code)
4100 case MEM:
4101 /* A memory access costs 1 insn if the mode is small, or the address is
4102 a single register, otherwise it costs one insn per word. */
4103 if (REG_P (XEXP (x, 0)))
4104 *total = COSTS_N_INSNS (1);
4105 else
4106 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4107 return true;
4109 case DIV:
4110 case MOD:
4111 case UDIV:
4112 case UMOD:
4113 /* Needs a libcall, so it costs about this. */
4114 *total = COSTS_N_INSNS (2);
4115 return false;
4117 case ROTATE:
4118 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4120 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4121 return true;
4123 /* Fall through */
4124 case ROTATERT:
4125 case ASHIFT:
4126 case LSHIFTRT:
4127 case ASHIFTRT:
4128 if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4130 *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4131 return true;
4133 else if (mode == SImode)
4135 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4136 /* Slightly disparage register shifts, but not by much. */
4137 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4138 *total += 1 + rtx_cost (XEXP (x, 1), code);
4139 return true;
4142 /* Needs a libcall. */
4143 *total = COSTS_N_INSNS (2);
4144 return false;
4146 case MINUS:
4147 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4149 *total = COSTS_N_INSNS (1);
4150 return false;
4153 if (mode == SImode)
4155 enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4156 enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4158 if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4159 || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4160 || subcode1 == ROTATE || subcode1 == ROTATERT
4161 || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4162 || subcode1 == ASHIFTRT)
4164 /* It's just the cost of the two operands. */
4165 *total = 0;
4166 return false;
4169 *total = COSTS_N_INSNS (1);
4170 return false;
4173 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4174 return false;
4176 case PLUS:
4177 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4179 *total = COSTS_N_INSNS (1);
4180 return false;
4183 /* Fall through */
4184 case AND: case XOR: case IOR:
4185 if (mode == SImode)
4187 enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4189 if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4190 || subcode == LSHIFTRT || subcode == ASHIFTRT
4191 || (code == AND && subcode == NOT))
4193 /* It's just the cost of the two operands. */
4194 *total = 0;
4195 return false;
4199 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4200 return false;
4202 case MULT:
4203 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4204 return false;
4206 case NEG:
4207 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4208 *total = COSTS_N_INSNS (1);
4209 /* Fall through */
4210 case NOT:
4211 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4213 return false;
4215 case IF_THEN_ELSE:
4216 *total = 0;
4217 return false;
4219 case COMPARE:
4220 if (cc_register (XEXP (x, 0), VOIDmode))
4221 * total = 0;
4222 else
4223 *total = COSTS_N_INSNS (1);
4224 return false;
4226 case ABS:
4227 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4228 *total = COSTS_N_INSNS (1);
4229 else
4230 *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4231 return false;
4233 case SIGN_EXTEND:
4234 *total = 0;
4235 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4237 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4238 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4240 if (mode == DImode)
4241 *total += COSTS_N_INSNS (1);
4242 return false;
4244 case ZERO_EXTEND:
4245 *total = 0;
4246 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4248 switch (GET_MODE (XEXP (x, 0)))
4250 case QImode:
4251 *total += COSTS_N_INSNS (1);
4252 break;
4254 case HImode:
4255 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4257 case SImode:
4258 break;
4260 default:
4261 *total += COSTS_N_INSNS (2);
4265 if (mode == DImode)
4266 *total += COSTS_N_INSNS (1);
4268 return false;
4270 case CONST_INT:
4271 if (const_ok_for_arm (INTVAL (x)))
4272 *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4273 else if (const_ok_for_arm (~INTVAL (x)))
4274 *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4275 else if (const_ok_for_arm (-INTVAL (x)))
4277 if (outer_code == COMPARE || outer_code == PLUS
4278 || outer_code == MINUS)
4279 *total = 0;
4280 else
4281 *total = COSTS_N_INSNS (1);
4283 else
4284 *total = COSTS_N_INSNS (2);
4285 return true;
4287 case CONST:
4288 case LABEL_REF:
4289 case SYMBOL_REF:
4290 *total = COSTS_N_INSNS (2);
4291 return true;
4293 case CONST_DOUBLE:
4294 *total = COSTS_N_INSNS (4);
4295 return true;
4297 default:
4298 if (mode != VOIDmode)
4299 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4300 else
4301 *total = COSTS_N_INSNS (4); /* How knows? */
4302 return false;
4306 /* RTX costs for cores with a slow MUL implementation. */
4308 static bool
4309 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4311 enum machine_mode mode = GET_MODE (x);
4313 if (TARGET_THUMB)
4315 *total = thumb_rtx_costs (x, code, outer_code);
4316 return true;
4319 switch (code)
4321 case MULT:
4322 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4323 || mode == DImode)
4325 *total = 30;
4326 return true;
4329 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4331 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4332 & (unsigned HOST_WIDE_INT) 0xffffffff);
4333 int cost, const_ok = const_ok_for_arm (i);
4334 int j, booth_unit_size;
4336 /* Tune as appropriate. */
4337 cost = const_ok ? 4 : 8;
4338 booth_unit_size = 2;
4339 for (j = 0; i && j < 32; j += booth_unit_size)
4341 i >>= booth_unit_size;
4342 cost += 2;
4345 *total = cost;
4346 return true;
4349 *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4350 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4351 return true;
4353 default:
4354 *total = arm_rtx_costs_1 (x, code, outer_code);
4355 return true;
4360 /* RTX cost for cores with a fast multiply unit (M variants). */
4362 static bool
4363 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4365 enum machine_mode mode = GET_MODE (x);
4367 if (TARGET_THUMB)
4369 *total = thumb_rtx_costs (x, code, outer_code);
4370 return true;
4373 switch (code)
4375 case MULT:
4376 /* There is no point basing this on the tuning, since it is always the
4377 fast variant if it exists at all. */
4378 if (mode == DImode
4379 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4380 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4381 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4383 *total = 8;
4384 return true;
4388 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4389 || mode == DImode)
4391 *total = 30;
4392 return true;
4395 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4397 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4398 & (unsigned HOST_WIDE_INT) 0xffffffff);
4399 int cost, const_ok = const_ok_for_arm (i);
4400 int j, booth_unit_size;
4402 /* Tune as appropriate. */
4403 cost = const_ok ? 4 : 8;
4404 booth_unit_size = 8;
4405 for (j = 0; i && j < 32; j += booth_unit_size)
4407 i >>= booth_unit_size;
4408 cost += 2;
4411 *total = cost;
4412 return true;
4415 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4416 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4417 return true;
4419 default:
4420 *total = arm_rtx_costs_1 (x, code, outer_code);
4421 return true;
4426 /* RTX cost for XScale CPUs. */
4428 static bool
4429 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4431 enum machine_mode mode = GET_MODE (x);
4433 if (TARGET_THUMB)
4435 *total = thumb_rtx_costs (x, code, outer_code);
4436 return true;
4439 switch (code)
4441 case MULT:
4442 /* There is no point basing this on the tuning, since it is always the
4443 fast variant if it exists at all. */
4444 if (mode == DImode
4445 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4446 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4447 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4449 *total = 8;
4450 return true;
4454 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4455 || mode == DImode)
4457 *total = 30;
4458 return true;
4461 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4463 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4464 & (unsigned HOST_WIDE_INT) 0xffffffff);
4465 int cost, const_ok = const_ok_for_arm (i);
4466 unsigned HOST_WIDE_INT masked_const;
4468 /* The cost will be related to two insns.
4469 First a load of the constant (MOV or LDR), then a multiply. */
4470 cost = 2;
4471 if (! const_ok)
4472 cost += 1; /* LDR is probably more expensive because
4473 of longer result latency. */
4474 masked_const = i & 0xffff8000;
4475 if (masked_const != 0 && masked_const != 0xffff8000)
4477 masked_const = i & 0xf8000000;
4478 if (masked_const == 0 || masked_const == 0xf8000000)
4479 cost += 1;
4480 else
4481 cost += 2;
4483 *total = cost;
4484 return true;
4487 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4488 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4489 return true;
4491 case COMPARE:
4492 /* A COMPARE of a MULT is slow on XScale; the muls instruction
4493 will stall until the multiplication is complete. */
4494 if (GET_CODE (XEXP (x, 0)) == MULT)
4495 *total = 4 + rtx_cost (XEXP (x, 0), code);
4496 else
4497 *total = arm_rtx_costs_1 (x, code, outer_code);
4498 return true;
4500 default:
4501 *total = arm_rtx_costs_1 (x, code, outer_code);
4502 return true;
4507 /* RTX costs for 9e (and later) cores. */
4509 static bool
4510 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4512 enum machine_mode mode = GET_MODE (x);
4513 int nonreg_cost;
4514 int cost;
4516 if (TARGET_THUMB)
4518 switch (code)
4520 case MULT:
4521 *total = COSTS_N_INSNS (3);
4522 return true;
4524 default:
4525 *total = thumb_rtx_costs (x, code, outer_code);
4526 return true;
4530 switch (code)
4532 case MULT:
4533 /* There is no point basing this on the tuning, since it is always the
4534 fast variant if it exists at all. */
4535 if (mode == DImode
4536 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4537 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4538 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4540 *total = 3;
4541 return true;
4545 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4547 *total = 30;
4548 return true;
4550 if (mode == DImode)
4552 cost = 7;
4553 nonreg_cost = 8;
4555 else
4557 cost = 2;
4558 nonreg_cost = 4;
4562 *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4563 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4564 return true;
4566 default:
4567 *total = arm_rtx_costs_1 (x, code, outer_code);
4568 return true;
4571 /* All address computations that can be done are free, but rtx cost returns
4572 the same for practically all of them. So we weight the different types
4573 of address here in the order (most pref first):
4574 PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
4575 static inline int
4576 arm_arm_address_cost (rtx x)
4578 enum rtx_code c = GET_CODE (x);
4580 if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4581 return 0;
4582 if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4583 return 10;
4585 if (c == PLUS || c == MINUS)
4587 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4588 return 2;
4590 if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4591 return 3;
4593 return 4;
4596 return 6;
4599 static inline int
4600 arm_thumb_address_cost (rtx x)
4602 enum rtx_code c = GET_CODE (x);
4604 if (c == REG)
4605 return 1;
4606 if (c == PLUS
4607 && GET_CODE (XEXP (x, 0)) == REG
4608 && GET_CODE (XEXP (x, 1)) == CONST_INT)
4609 return 1;
4611 return 2;
4614 static int
4615 arm_address_cost (rtx x)
4617 return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4620 static int
4621 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4623 rtx i_pat, d_pat;
4625 /* Some true dependencies can have a higher cost depending
4626 on precisely how certain input operands are used. */
4627 if (arm_tune_xscale
4628 && REG_NOTE_KIND (link) == 0
4629 && recog_memoized (insn) >= 0
4630 && recog_memoized (dep) >= 0)
4632 int shift_opnum = get_attr_shift (insn);
4633 enum attr_type attr_type = get_attr_type (dep);
4635 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4636 operand for INSN. If we have a shifted input operand and the
4637 instruction we depend on is another ALU instruction, then we may
4638 have to account for an additional stall. */
4639 if (shift_opnum != 0
4640 && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4642 rtx shifted_operand;
4643 int opno;
4645 /* Get the shifted operand. */
4646 extract_insn (insn);
4647 shifted_operand = recog_data.operand[shift_opnum];
4649 /* Iterate over all the operands in DEP. If we write an operand
4650 that overlaps with SHIFTED_OPERAND, then we have increase the
4651 cost of this dependency. */
4652 extract_insn (dep);
4653 preprocess_constraints ();
4654 for (opno = 0; opno < recog_data.n_operands; opno++)
4656 /* We can ignore strict inputs. */
4657 if (recog_data.operand_type[opno] == OP_IN)
4658 continue;
4660 if (reg_overlap_mentioned_p (recog_data.operand[opno],
4661 shifted_operand))
4662 return 2;
4667 /* XXX This is not strictly true for the FPA. */
4668 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4669 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4670 return 0;
4672 /* Call insns don't incur a stall, even if they follow a load. */
4673 if (REG_NOTE_KIND (link) == 0
4674 && GET_CODE (insn) == CALL_INSN)
4675 return 1;
4677 if ((i_pat = single_set (insn)) != NULL
4678 && GET_CODE (SET_SRC (i_pat)) == MEM
4679 && (d_pat = single_set (dep)) != NULL
4680 && GET_CODE (SET_DEST (d_pat)) == MEM)
4682 rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4683 /* This is a load after a store, there is no conflict if the load reads
4684 from a cached area. Assume that loads from the stack, and from the
4685 constant pool are cached, and that others will miss. This is a
4686 hack. */
4688 if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4689 || reg_mentioned_p (stack_pointer_rtx, src_mem)
4690 || reg_mentioned_p (frame_pointer_rtx, src_mem)
4691 || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4692 return 1;
4695 return cost;
4698 static int fp_consts_inited = 0;
4700 /* Only zero is valid for VFP. Other values are also valid for FPA. */
4701 static const char * const strings_fp[8] =
4703 "0", "1", "2", "3",
4704 "4", "5", "0.5", "10"
4707 static REAL_VALUE_TYPE values_fp[8];
4709 static void
4710 init_fp_table (void)
4712 int i;
4713 REAL_VALUE_TYPE r;
4715 if (TARGET_VFP)
4716 fp_consts_inited = 1;
4717 else
4718 fp_consts_inited = 8;
4720 for (i = 0; i < fp_consts_inited; i++)
4722 r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4723 values_fp[i] = r;
4727 /* Return TRUE if rtx X is a valid immediate FP constant. */
4729 arm_const_double_rtx (rtx x)
4731 REAL_VALUE_TYPE r;
4732 int i;
4734 if (!fp_consts_inited)
4735 init_fp_table ();
4737 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4738 if (REAL_VALUE_MINUS_ZERO (r))
4739 return 0;
4741 for (i = 0; i < fp_consts_inited; i++)
4742 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4743 return 1;
4745 return 0;
4748 /* Return TRUE if rtx X is a valid immediate FPA constant. */
4750 neg_const_double_rtx_ok_for_fpa (rtx x)
4752 REAL_VALUE_TYPE r;
4753 int i;
4755 if (!fp_consts_inited)
4756 init_fp_table ();
4758 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4759 r = REAL_VALUE_NEGATE (r);
4760 if (REAL_VALUE_MINUS_ZERO (r))
4761 return 0;
4763 for (i = 0; i < 8; i++)
4764 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4765 return 1;
4767 return 0;
4770 /* Predicates for `match_operand' and `match_operator'. */
4772 /* Return nonzero if OP is a valid Cirrus memory address pattern. */
4774 cirrus_memory_offset (rtx op)
4776 /* Reject eliminable registers. */
4777 if (! (reload_in_progress || reload_completed)
4778 && ( reg_mentioned_p (frame_pointer_rtx, op)
4779 || reg_mentioned_p (arg_pointer_rtx, op)
4780 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4781 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4782 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4783 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4784 return 0;
4786 if (GET_CODE (op) == MEM)
4788 rtx ind;
4790 ind = XEXP (op, 0);
4792 /* Match: (mem (reg)). */
4793 if (GET_CODE (ind) == REG)
4794 return 1;
4796 /* Match:
4797 (mem (plus (reg)
4798 (const))). */
4799 if (GET_CODE (ind) == PLUS
4800 && GET_CODE (XEXP (ind, 0)) == REG
4801 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4802 && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4803 return 1;
4806 return 0;
4809 /* Return TRUE if OP is a valid VFP memory address pattern.
4810 WB if true if writeback address modes are allowed. */
4813 arm_coproc_mem_operand (rtx op, bool wb)
4815 rtx ind;
4817 /* Reject eliminable registers. */
4818 if (! (reload_in_progress || reload_completed)
4819 && ( reg_mentioned_p (frame_pointer_rtx, op)
4820 || reg_mentioned_p (arg_pointer_rtx, op)
4821 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4822 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4823 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4824 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4825 return FALSE;
4827 /* Constants are converted into offsets from labels. */
4828 if (GET_CODE (op) != MEM)
4829 return FALSE;
4831 ind = XEXP (op, 0);
4833 if (reload_completed
4834 && (GET_CODE (ind) == LABEL_REF
4835 || (GET_CODE (ind) == CONST
4836 && GET_CODE (XEXP (ind, 0)) == PLUS
4837 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4838 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4839 return TRUE;
4841 /* Match: (mem (reg)). */
4842 if (GET_CODE (ind) == REG)
4843 return arm_address_register_rtx_p (ind, 0);
4845 /* Autoincremment addressing modes. */
4846 if (wb
4847 && (GET_CODE (ind) == PRE_INC
4848 || GET_CODE (ind) == POST_INC
4849 || GET_CODE (ind) == PRE_DEC
4850 || GET_CODE (ind) == POST_DEC))
4851 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
4853 if (wb
4854 && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
4855 && arm_address_register_rtx_p (XEXP (ind, 0), 0)
4856 && GET_CODE (XEXP (ind, 1)) == PLUS
4857 && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
4858 ind = XEXP (ind, 1);
4860 /* Match:
4861 (plus (reg)
4862 (const)). */
4863 if (GET_CODE (ind) == PLUS
4864 && GET_CODE (XEXP (ind, 0)) == REG
4865 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4866 && GET_CODE (XEXP (ind, 1)) == CONST_INT
4867 && INTVAL (XEXP (ind, 1)) > -1024
4868 && INTVAL (XEXP (ind, 1)) < 1024
4869 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
4870 return TRUE;
4872 return FALSE;
4875 /* Return true if X is a register that will be eliminated later on. */
4877 arm_eliminable_register (rtx x)
4879 return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
4880 || REGNO (x) == ARG_POINTER_REGNUM
4881 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
4882 && REGNO (x) <= LAST_VIRTUAL_REGISTER));
4885 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4886 VFP registers. Otherwise return NO_REGS. */
4888 enum reg_class
4889 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4891 if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
4892 return NO_REGS;
4894 return GENERAL_REGS;
4898 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4899 Use by the Cirrus Maverick code which has to workaround
4900 a hardware bug triggered by such instructions. */
4901 static bool
4902 arm_memory_load_p (rtx insn)
4904 rtx body, lhs, rhs;;
4906 if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4907 return false;
4909 body = PATTERN (insn);
4911 if (GET_CODE (body) != SET)
4912 return false;
4914 lhs = XEXP (body, 0);
4915 rhs = XEXP (body, 1);
4917 lhs = REG_OR_SUBREG_RTX (lhs);
4919 /* If the destination is not a general purpose
4920 register we do not have to worry. */
4921 if (GET_CODE (lhs) != REG
4922 || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4923 return false;
4925 /* As well as loads from memory we also have to react
4926 to loads of invalid constants which will be turned
4927 into loads from the minipool. */
4928 return (GET_CODE (rhs) == MEM
4929 || GET_CODE (rhs) == SYMBOL_REF
4930 || note_invalid_constants (insn, -1, false));
4933 /* Return TRUE if INSN is a Cirrus instruction. */
4934 static bool
4935 arm_cirrus_insn_p (rtx insn)
4937 enum attr_cirrus attr;
4939 /* get_attr aborts on USE and CLOBBER. */
4940 if (!insn
4941 || GET_CODE (insn) != INSN
4942 || GET_CODE (PATTERN (insn)) == USE
4943 || GET_CODE (PATTERN (insn)) == CLOBBER)
4944 return 0;
4946 attr = get_attr_cirrus (insn);
4948 return attr != CIRRUS_NOT;
4951 /* Cirrus reorg for invalid instruction combinations. */
4952 static void
4953 cirrus_reorg (rtx first)
4955 enum attr_cirrus attr;
4956 rtx body = PATTERN (first);
4957 rtx t;
4958 int nops;
4960 /* Any branch must be followed by 2 non Cirrus instructions. */
4961 if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4963 nops = 0;
4964 t = next_nonnote_insn (first);
4966 if (arm_cirrus_insn_p (t))
4967 ++ nops;
4969 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4970 ++ nops;
4972 while (nops --)
4973 emit_insn_after (gen_nop (), first);
4975 return;
4978 /* (float (blah)) is in parallel with a clobber. */
4979 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4980 body = XVECEXP (body, 0, 0);
4982 if (GET_CODE (body) == SET)
4984 rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4986 /* cfldrd, cfldr64, cfstrd, cfstr64 must
4987 be followed by a non Cirrus insn. */
4988 if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
4990 if (arm_cirrus_insn_p (next_nonnote_insn (first)))
4991 emit_insn_after (gen_nop (), first);
4993 return;
4995 else if (arm_memory_load_p (first))
4997 unsigned int arm_regno;
4999 /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
5000 ldr/cfmv64hr combination where the Rd field is the same
5001 in both instructions must be split with a non Cirrus
5002 insn. Example:
5004 ldr r0, blah
5006 cfmvsr mvf0, r0. */
5008 /* Get Arm register number for ldr insn. */
5009 if (GET_CODE (lhs) == REG)
5010 arm_regno = REGNO (lhs);
5011 else if (GET_CODE (rhs) == REG)
5012 arm_regno = REGNO (rhs);
5013 else
5014 abort ();
5016 /* Next insn. */
5017 first = next_nonnote_insn (first);
5019 if (! arm_cirrus_insn_p (first))
5020 return;
5022 body = PATTERN (first);
5024 /* (float (blah)) is in parallel with a clobber. */
5025 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5026 body = XVECEXP (body, 0, 0);
5028 if (GET_CODE (body) == FLOAT)
5029 body = XEXP (body, 0);
5031 if (get_attr_cirrus (first) == CIRRUS_MOVE
5032 && GET_CODE (XEXP (body, 1)) == REG
5033 && arm_regno == REGNO (XEXP (body, 1)))
5034 emit_insn_after (gen_nop (), first);
5036 return;
5040 /* get_attr aborts on USE and CLOBBER. */
5041 if (!first
5042 || GET_CODE (first) != INSN
5043 || GET_CODE (PATTERN (first)) == USE
5044 || GET_CODE (PATTERN (first)) == CLOBBER)
5045 return;
5047 attr = get_attr_cirrus (first);
5049 /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5050 must be followed by a non-coprocessor instruction. */
5051 if (attr == CIRRUS_COMPARE)
5053 nops = 0;
5055 t = next_nonnote_insn (first);
5057 if (arm_cirrus_insn_p (t))
5058 ++ nops;
5060 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5061 ++ nops;
5063 while (nops --)
5064 emit_insn_after (gen_nop (), first);
5066 return;
5070 /* Return TRUE if X references a SYMBOL_REF. */
5072 symbol_mentioned_p (rtx x)
5074 const char * fmt;
5075 int i;
5077 if (GET_CODE (x) == SYMBOL_REF)
5078 return 1;
5080 fmt = GET_RTX_FORMAT (GET_CODE (x));
5082 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5084 if (fmt[i] == 'E')
5086 int j;
5088 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5089 if (symbol_mentioned_p (XVECEXP (x, i, j)))
5090 return 1;
5092 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5093 return 1;
5096 return 0;
5099 /* Return TRUE if X references a LABEL_REF. */
5101 label_mentioned_p (rtx x)
5103 const char * fmt;
5104 int i;
5106 if (GET_CODE (x) == LABEL_REF)
5107 return 1;
5109 fmt = GET_RTX_FORMAT (GET_CODE (x));
5110 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5112 if (fmt[i] == 'E')
5114 int j;
5116 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5117 if (label_mentioned_p (XVECEXP (x, i, j)))
5118 return 1;
5120 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5121 return 1;
5124 return 0;
5127 enum rtx_code
5128 minmax_code (rtx x)
5130 enum rtx_code code = GET_CODE (x);
5132 if (code == SMAX)
5133 return GE;
5134 else if (code == SMIN)
5135 return LE;
5136 else if (code == UMIN)
5137 return LEU;
5138 else if (code == UMAX)
5139 return GEU;
5141 abort ();
5144 /* Return 1 if memory locations are adjacent. */
5146 adjacent_mem_locations (rtx a, rtx b)
5148 /* We don't guarantee to preserve the order of these memory refs. */
5149 if (volatile_refs_p (a) || volatile_refs_p (b))
5150 return 0;
5152 if ((GET_CODE (XEXP (a, 0)) == REG
5153 || (GET_CODE (XEXP (a, 0)) == PLUS
5154 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5155 && (GET_CODE (XEXP (b, 0)) == REG
5156 || (GET_CODE (XEXP (b, 0)) == PLUS
5157 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5159 HOST_WIDE_INT val0 = 0, val1 = 0;
5160 rtx reg0, reg1;
5161 int val_diff;
5163 if (GET_CODE (XEXP (a, 0)) == PLUS)
5165 reg0 = XEXP (XEXP (a, 0), 0);
5166 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5168 else
5169 reg0 = XEXP (a, 0);
5171 if (GET_CODE (XEXP (b, 0)) == PLUS)
5173 reg1 = XEXP (XEXP (b, 0), 0);
5174 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5176 else
5177 reg1 = XEXP (b, 0);
5179 /* Don't accept any offset that will require multiple
5180 instructions to handle, since this would cause the
5181 arith_adjacentmem pattern to output an overlong sequence. */
5182 if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5183 return 0;
5185 /* Don't allow an eliminable register: register elimination can make
5186 the offset too large. */
5187 if (arm_eliminable_register (reg0))
5188 return 0;
5190 val_diff = val1 - val0;
5192 if (arm_ld_sched)
5194 /* If the target has load delay slots, then there's no benefit
5195 to using an ldm instruction unless the offset is zero and
5196 we are optimizing for size. */
5197 return (optimize_size && (REGNO (reg0) == REGNO (reg1))
5198 && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
5199 && (val_diff == 4 || val_diff == -4));
5202 return ((REGNO (reg0) == REGNO (reg1))
5203 && (val_diff == 4 || val_diff == -4));
5206 return 0;
5210 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5211 HOST_WIDE_INT *load_offset)
5213 int unsorted_regs[4];
5214 HOST_WIDE_INT unsorted_offsets[4];
5215 int order[4];
5216 int base_reg = -1;
5217 int i;
5219 /* Can only handle 2, 3, or 4 insns at present,
5220 though could be easily extended if required. */
5221 if (nops < 2 || nops > 4)
5222 abort ();
5224 /* Loop over the operands and check that the memory references are
5225 suitable (i.e. immediate offsets from the same base register). At
5226 the same time, extract the target register, and the memory
5227 offsets. */
5228 for (i = 0; i < nops; i++)
5230 rtx reg;
5231 rtx offset;
5233 /* Convert a subreg of a mem into the mem itself. */
5234 if (GET_CODE (operands[nops + i]) == SUBREG)
5235 operands[nops + i] = alter_subreg (operands + (nops + i));
5237 if (GET_CODE (operands[nops + i]) != MEM)
5238 abort ();
5240 /* Don't reorder volatile memory references; it doesn't seem worth
5241 looking for the case where the order is ok anyway. */
5242 if (MEM_VOLATILE_P (operands[nops + i]))
5243 return 0;
5245 offset = const0_rtx;
5247 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5248 || (GET_CODE (reg) == SUBREG
5249 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5250 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5251 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5252 == REG)
5253 || (GET_CODE (reg) == SUBREG
5254 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5255 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5256 == CONST_INT)))
5258 if (i == 0)
5260 base_reg = REGNO (reg);
5261 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5262 ? REGNO (operands[i])
5263 : REGNO (SUBREG_REG (operands[i])));
5264 order[0] = 0;
5266 else
5268 if (base_reg != (int) REGNO (reg))
5269 /* Not addressed from the same base register. */
5270 return 0;
5272 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5273 ? REGNO (operands[i])
5274 : REGNO (SUBREG_REG (operands[i])));
5275 if (unsorted_regs[i] < unsorted_regs[order[0]])
5276 order[0] = i;
5279 /* If it isn't an integer register, or if it overwrites the
5280 base register but isn't the last insn in the list, then
5281 we can't do this. */
5282 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5283 || (i != nops - 1 && unsorted_regs[i] == base_reg))
5284 return 0;
5286 unsorted_offsets[i] = INTVAL (offset);
5288 else
5289 /* Not a suitable memory address. */
5290 return 0;
5293 /* All the useful information has now been extracted from the
5294 operands into unsorted_regs and unsorted_offsets; additionally,
5295 order[0] has been set to the lowest numbered register in the
5296 list. Sort the registers into order, and check that the memory
5297 offsets are ascending and adjacent. */
5299 for (i = 1; i < nops; i++)
5301 int j;
5303 order[i] = order[i - 1];
5304 for (j = 0; j < nops; j++)
5305 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5306 && (order[i] == order[i - 1]
5307 || unsorted_regs[j] < unsorted_regs[order[i]]))
5308 order[i] = j;
5310 /* Have we found a suitable register? if not, one must be used more
5311 than once. */
5312 if (order[i] == order[i - 1])
5313 return 0;
5315 /* Is the memory address adjacent and ascending? */
5316 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5317 return 0;
5320 if (base)
5322 *base = base_reg;
5324 for (i = 0; i < nops; i++)
5325 regs[i] = unsorted_regs[order[i]];
5327 *load_offset = unsorted_offsets[order[0]];
5330 if (unsorted_offsets[order[0]] == 0)
5331 return 1; /* ldmia */
5333 if (unsorted_offsets[order[0]] == 4)
5334 return 2; /* ldmib */
5336 if (unsorted_offsets[order[nops - 1]] == 0)
5337 return 3; /* ldmda */
5339 if (unsorted_offsets[order[nops - 1]] == -4)
5340 return 4; /* ldmdb */
5342 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5343 if the offset isn't small enough. The reason 2 ldrs are faster
5344 is because these ARMs are able to do more than one cache access
5345 in a single cycle. The ARM9 and StrongARM have Harvard caches,
5346 whilst the ARM8 has a double bandwidth cache. This means that
5347 these cores can do both an instruction fetch and a data fetch in
5348 a single cycle, so the trick of calculating the address into a
5349 scratch register (one of the result regs) and then doing a load
5350 multiple actually becomes slower (and no smaller in code size).
5351 That is the transformation
5353 ldr rd1, [rbase + offset]
5354 ldr rd2, [rbase + offset + 4]
5358 add rd1, rbase, offset
5359 ldmia rd1, {rd1, rd2}
5361 produces worse code -- '3 cycles + any stalls on rd2' instead of
5362 '2 cycles + any stalls on rd2'. On ARMs with only one cache
5363 access per cycle, the first sequence could never complete in less
5364 than 6 cycles, whereas the ldm sequence would only take 5 and
5365 would make better use of sequential accesses if not hitting the
5366 cache.
5368 We cheat here and test 'arm_ld_sched' which we currently know to
5369 only be true for the ARM8, ARM9 and StrongARM. If this ever
5370 changes, then the test below needs to be reworked. */
5371 if (nops == 2 && arm_ld_sched)
5372 return 0;
5374 /* Can't do it without setting up the offset, only do this if it takes
5375 no more than one insn. */
5376 return (const_ok_for_arm (unsorted_offsets[order[0]])
5377 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5380 const char *
5381 emit_ldm_seq (rtx *operands, int nops)
5383 int regs[4];
5384 int base_reg;
5385 HOST_WIDE_INT offset;
5386 char buf[100];
5387 int i;
5389 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5391 case 1:
5392 strcpy (buf, "ldm%?ia\t");
5393 break;
5395 case 2:
5396 strcpy (buf, "ldm%?ib\t");
5397 break;
5399 case 3:
5400 strcpy (buf, "ldm%?da\t");
5401 break;
5403 case 4:
5404 strcpy (buf, "ldm%?db\t");
5405 break;
5407 case 5:
5408 if (offset >= 0)
5409 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5410 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5411 (long) offset);
5412 else
5413 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5414 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5415 (long) -offset);
5416 output_asm_insn (buf, operands);
5417 base_reg = regs[0];
5418 strcpy (buf, "ldm%?ia\t");
5419 break;
5421 default:
5422 abort ();
5425 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5426 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5428 for (i = 1; i < nops; i++)
5429 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5430 reg_names[regs[i]]);
5432 strcat (buf, "}\t%@ phole ldm");
5434 output_asm_insn (buf, operands);
5435 return "";
5439 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5440 HOST_WIDE_INT * load_offset)
5442 int unsorted_regs[4];
5443 HOST_WIDE_INT unsorted_offsets[4];
5444 int order[4];
5445 int base_reg = -1;
5446 int i;
5448 /* Can only handle 2, 3, or 4 insns at present, though could be easily
5449 extended if required. */
5450 if (nops < 2 || nops > 4)
5451 abort ();
5453 /* Loop over the operands and check that the memory references are
5454 suitable (i.e. immediate offsets from the same base register). At
5455 the same time, extract the target register, and the memory
5456 offsets. */
5457 for (i = 0; i < nops; i++)
5459 rtx reg;
5460 rtx offset;
5462 /* Convert a subreg of a mem into the mem itself. */
5463 if (GET_CODE (operands[nops + i]) == SUBREG)
5464 operands[nops + i] = alter_subreg (operands + (nops + i));
5466 if (GET_CODE (operands[nops + i]) != MEM)
5467 abort ();
5469 /* Don't reorder volatile memory references; it doesn't seem worth
5470 looking for the case where the order is ok anyway. */
5471 if (MEM_VOLATILE_P (operands[nops + i]))
5472 return 0;
5474 offset = const0_rtx;
5476 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5477 || (GET_CODE (reg) == SUBREG
5478 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5479 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5480 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5481 == REG)
5482 || (GET_CODE (reg) == SUBREG
5483 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5484 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5485 == CONST_INT)))
5487 if (i == 0)
5489 base_reg = REGNO (reg);
5490 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5491 ? REGNO (operands[i])
5492 : REGNO (SUBREG_REG (operands[i])));
5493 order[0] = 0;
5495 else
5497 if (base_reg != (int) REGNO (reg))
5498 /* Not addressed from the same base register. */
5499 return 0;
5501 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5502 ? REGNO (operands[i])
5503 : REGNO (SUBREG_REG (operands[i])));
5504 if (unsorted_regs[i] < unsorted_regs[order[0]])
5505 order[0] = i;
5508 /* If it isn't an integer register, then we can't do this. */
5509 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5510 return 0;
5512 unsorted_offsets[i] = INTVAL (offset);
5514 else
5515 /* Not a suitable memory address. */
5516 return 0;
5519 /* All the useful information has now been extracted from the
5520 operands into unsorted_regs and unsorted_offsets; additionally,
5521 order[0] has been set to the lowest numbered register in the
5522 list. Sort the registers into order, and check that the memory
5523 offsets are ascending and adjacent. */
5525 for (i = 1; i < nops; i++)
5527 int j;
5529 order[i] = order[i - 1];
5530 for (j = 0; j < nops; j++)
5531 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5532 && (order[i] == order[i - 1]
5533 || unsorted_regs[j] < unsorted_regs[order[i]]))
5534 order[i] = j;
5536 /* Have we found a suitable register? if not, one must be used more
5537 than once. */
5538 if (order[i] == order[i - 1])
5539 return 0;
5541 /* Is the memory address adjacent and ascending? */
5542 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5543 return 0;
5546 if (base)
5548 *base = base_reg;
5550 for (i = 0; i < nops; i++)
5551 regs[i] = unsorted_regs[order[i]];
5553 *load_offset = unsorted_offsets[order[0]];
5556 if (unsorted_offsets[order[0]] == 0)
5557 return 1; /* stmia */
5559 if (unsorted_offsets[order[0]] == 4)
5560 return 2; /* stmib */
5562 if (unsorted_offsets[order[nops - 1]] == 0)
5563 return 3; /* stmda */
5565 if (unsorted_offsets[order[nops - 1]] == -4)
5566 return 4; /* stmdb */
5568 return 0;
5571 const char *
5572 emit_stm_seq (rtx *operands, int nops)
5574 int regs[4];
5575 int base_reg;
5576 HOST_WIDE_INT offset;
5577 char buf[100];
5578 int i;
5580 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5582 case 1:
5583 strcpy (buf, "stm%?ia\t");
5584 break;
5586 case 2:
5587 strcpy (buf, "stm%?ib\t");
5588 break;
5590 case 3:
5591 strcpy (buf, "stm%?da\t");
5592 break;
5594 case 4:
5595 strcpy (buf, "stm%?db\t");
5596 break;
5598 default:
5599 abort ();
5602 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5603 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5605 for (i = 1; i < nops; i++)
5606 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5607 reg_names[regs[i]]);
5609 strcat (buf, "}\t%@ phole stm");
5611 output_asm_insn (buf, operands);
5612 return "";
5616 /* Routines for use in generating RTL. */
5619 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5620 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5622 HOST_WIDE_INT offset = *offsetp;
5623 int i = 0, j;
5624 rtx result;
5625 int sign = up ? 1 : -1;
5626 rtx mem, addr;
5628 /* XScale has load-store double instructions, but they have stricter
5629 alignment requirements than load-store multiple, so we cannot
5630 use them.
5632 For XScale ldm requires 2 + NREGS cycles to complete and blocks
5633 the pipeline until completion.
5635 NREGS CYCLES
5641 An ldr instruction takes 1-3 cycles, but does not block the
5642 pipeline.
5644 NREGS CYCLES
5645 1 1-3
5646 2 2-6
5647 3 3-9
5648 4 4-12
5650 Best case ldr will always win. However, the more ldr instructions
5651 we issue, the less likely we are to be able to schedule them well.
5652 Using ldr instructions also increases code size.
5654 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5655 for counts of 3 or 4 regs. */
5656 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5658 rtx seq;
5660 start_sequence ();
5662 for (i = 0; i < count; i++)
5664 addr = plus_constant (from, i * 4 * sign);
5665 mem = adjust_automodify_address (basemem, SImode, addr, offset);
5666 emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5667 offset += 4 * sign;
5670 if (write_back)
5672 emit_move_insn (from, plus_constant (from, count * 4 * sign));
5673 *offsetp = offset;
5676 seq = get_insns ();
5677 end_sequence ();
5679 return seq;
5682 result = gen_rtx_PARALLEL (VOIDmode,
5683 rtvec_alloc (count + (write_back ? 1 : 0)));
5684 if (write_back)
5686 XVECEXP (result, 0, 0)
5687 = gen_rtx_SET (GET_MODE (from), from,
5688 plus_constant (from, count * 4 * sign));
5689 i = 1;
5690 count++;
5693 for (j = 0; i < count; i++, j++)
5695 addr = plus_constant (from, j * 4 * sign);
5696 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5697 XVECEXP (result, 0, i)
5698 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5699 offset += 4 * sign;
5702 if (write_back)
5703 *offsetp = offset;
5705 return result;
5709 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5710 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5712 HOST_WIDE_INT offset = *offsetp;
5713 int i = 0, j;
5714 rtx result;
5715 int sign = up ? 1 : -1;
5716 rtx mem, addr;
5718 /* See arm_gen_load_multiple for discussion of
5719 the pros/cons of ldm/stm usage for XScale. */
5720 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5722 rtx seq;
5724 start_sequence ();
5726 for (i = 0; i < count; i++)
5728 addr = plus_constant (to, i * 4 * sign);
5729 mem = adjust_automodify_address (basemem, SImode, addr, offset);
5730 emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5731 offset += 4 * sign;
5734 if (write_back)
5736 emit_move_insn (to, plus_constant (to, count * 4 * sign));
5737 *offsetp = offset;
5740 seq = get_insns ();
5741 end_sequence ();
5743 return seq;
5746 result = gen_rtx_PARALLEL (VOIDmode,
5747 rtvec_alloc (count + (write_back ? 1 : 0)));
5748 if (write_back)
5750 XVECEXP (result, 0, 0)
5751 = gen_rtx_SET (GET_MODE (to), to,
5752 plus_constant (to, count * 4 * sign));
5753 i = 1;
5754 count++;
5757 for (j = 0; i < count; i++, j++)
5759 addr = plus_constant (to, j * 4 * sign);
5760 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5761 XVECEXP (result, 0, i)
5762 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5763 offset += 4 * sign;
5766 if (write_back)
5767 *offsetp = offset;
5769 return result;
5773 arm_gen_movmemqi (rtx *operands)
5775 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5776 HOST_WIDE_INT srcoffset, dstoffset;
5777 int i;
5778 rtx src, dst, srcbase, dstbase;
5779 rtx part_bytes_reg = NULL;
5780 rtx mem;
5782 if (GET_CODE (operands[2]) != CONST_INT
5783 || GET_CODE (operands[3]) != CONST_INT
5784 || INTVAL (operands[2]) > 64
5785 || INTVAL (operands[3]) & 3)
5786 return 0;
5788 dstbase = operands[0];
5789 srcbase = operands[1];
5791 dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
5792 src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
5794 in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5795 out_words_to_go = INTVAL (operands[2]) / 4;
5796 last_bytes = INTVAL (operands[2]) & 3;
5797 dstoffset = srcoffset = 0;
5799 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5800 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5802 for (i = 0; in_words_to_go >= 2; i+=4)
5804 if (in_words_to_go > 4)
5805 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5806 srcbase, &srcoffset));
5807 else
5808 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
5809 FALSE, srcbase, &srcoffset));
5811 if (out_words_to_go)
5813 if (out_words_to_go > 4)
5814 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5815 dstbase, &dstoffset));
5816 else if (out_words_to_go != 1)
5817 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5818 dst, TRUE,
5819 (last_bytes == 0
5820 ? FALSE : TRUE),
5821 dstbase, &dstoffset));
5822 else
5824 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5825 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5826 if (last_bytes != 0)
5828 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5829 dstoffset += 4;
5834 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5835 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
5838 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
5839 if (out_words_to_go)
5841 rtx sreg;
5843 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5844 sreg = copy_to_reg (mem);
5846 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5847 emit_move_insn (mem, sreg);
5848 in_words_to_go--;
5850 if (in_words_to_go) /* Sanity check */
5851 abort ();
5854 if (in_words_to_go)
5856 if (in_words_to_go < 0)
5857 abort ();
5859 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5860 part_bytes_reg = copy_to_mode_reg (SImode, mem);
5863 if (last_bytes && part_bytes_reg == NULL)
5864 abort ();
5866 if (BYTES_BIG_ENDIAN && last_bytes)
5868 rtx tmp = gen_reg_rtx (SImode);
5870 /* The bytes we want are in the top end of the word. */
5871 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
5872 GEN_INT (8 * (4 - last_bytes))));
5873 part_bytes_reg = tmp;
5875 while (last_bytes)
5877 mem = adjust_automodify_address (dstbase, QImode,
5878 plus_constant (dst, last_bytes - 1),
5879 dstoffset + last_bytes - 1);
5880 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5882 if (--last_bytes)
5884 tmp = gen_reg_rtx (SImode);
5885 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
5886 part_bytes_reg = tmp;
5891 else
5893 if (last_bytes > 1)
5895 mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
5896 emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
5897 last_bytes -= 2;
5898 if (last_bytes)
5900 rtx tmp = gen_reg_rtx (SImode);
5901 emit_insn (gen_addsi3 (dst, dst, const2_rtx));
5902 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
5903 part_bytes_reg = tmp;
5904 dstoffset += 2;
5908 if (last_bytes)
5910 mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
5911 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5915 return 1;
5918 /* Generate a memory reference for a half word, such that it will be loaded
5919 into the top 16 bits of the word. We can assume that the address is
5920 known to be alignable and of the form reg, or plus (reg, const). */
5923 arm_gen_rotated_half_load (rtx memref)
5925 HOST_WIDE_INT offset = 0;
5926 rtx base = XEXP (memref, 0);
5928 if (GET_CODE (base) == PLUS)
5930 offset = INTVAL (XEXP (base, 1));
5931 base = XEXP (base, 0);
5934 /* If we aren't allowed to generate unaligned addresses, then fail. */
5935 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0))
5936 return NULL;
5938 base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
5940 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
5941 return base;
5943 return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
5946 /* Select a dominance comparison mode if possible for a test of the general
5947 form (OP (COND_OR (X) (Y)) (const_int 0)). We support three forms.
5948 COND_OR == DOM_CC_X_AND_Y => (X && Y)
5949 COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
5950 COND_OR == DOM_CC_X_OR_Y => (X || Y)
5951 In all cases OP will be either EQ or NE, but we don't need to know which
5952 here. If we are unable to support a dominance comparison we return
5953 CC mode. This will then fail to match for the RTL expressions that
5954 generate this call. */
5955 enum machine_mode
5956 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
5958 enum rtx_code cond1, cond2;
5959 int swapped = 0;
5961 /* Currently we will probably get the wrong result if the individual
5962 comparisons are not simple. This also ensures that it is safe to
5963 reverse a comparison if necessary. */
5964 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
5965 != CCmode)
5966 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
5967 != CCmode))
5968 return CCmode;
5970 /* The if_then_else variant of this tests the second condition if the
5971 first passes, but is true if the first fails. Reverse the first
5972 condition to get a true "inclusive-or" expression. */
5973 if (cond_or == DOM_CC_NX_OR_Y)
5974 cond1 = reverse_condition (cond1);
5976 /* If the comparisons are not equal, and one doesn't dominate the other,
5977 then we can't do this. */
5978 if (cond1 != cond2
5979 && !comparison_dominates_p (cond1, cond2)
5980 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
5981 return CCmode;
5983 if (swapped)
5985 enum rtx_code temp = cond1;
5986 cond1 = cond2;
5987 cond2 = temp;
5990 switch (cond1)
5992 case EQ:
5993 if (cond2 == EQ || cond_or == DOM_CC_X_AND_Y)
5994 return CC_DEQmode;
5996 switch (cond2)
5998 case LE: return CC_DLEmode;
5999 case LEU: return CC_DLEUmode;
6000 case GE: return CC_DGEmode;
6001 case GEU: return CC_DGEUmode;
6002 default: break;
6005 break;
6007 case LT:
6008 if (cond2 == LT || cond_or == DOM_CC_X_AND_Y)
6009 return CC_DLTmode;
6010 if (cond2 == LE)
6011 return CC_DLEmode;
6012 if (cond2 == NE)
6013 return CC_DNEmode;
6014 break;
6016 case GT:
6017 if (cond2 == GT || cond_or == DOM_CC_X_AND_Y)
6018 return CC_DGTmode;
6019 if (cond2 == GE)
6020 return CC_DGEmode;
6021 if (cond2 == NE)
6022 return CC_DNEmode;
6023 break;
6025 case LTU:
6026 if (cond2 == LTU || cond_or == DOM_CC_X_AND_Y)
6027 return CC_DLTUmode;
6028 if (cond2 == LEU)
6029 return CC_DLEUmode;
6030 if (cond2 == NE)
6031 return CC_DNEmode;
6032 break;
6034 case GTU:
6035 if (cond2 == GTU || cond_or == DOM_CC_X_AND_Y)
6036 return CC_DGTUmode;
6037 if (cond2 == GEU)
6038 return CC_DGEUmode;
6039 if (cond2 == NE)
6040 return CC_DNEmode;
6041 break;
6043 /* The remaining cases only occur when both comparisons are the
6044 same. */
6045 case NE:
6046 return CC_DNEmode;
6048 case LE:
6049 return CC_DLEmode;
6051 case GE:
6052 return CC_DGEmode;
6054 case LEU:
6055 return CC_DLEUmode;
6057 case GEU:
6058 return CC_DGEUmode;
6060 default:
6061 break;
6064 abort ();
6067 enum machine_mode
6068 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6070 /* All floating point compares return CCFP if it is an equality
6071 comparison, and CCFPE otherwise. */
6072 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6074 switch (op)
6076 case EQ:
6077 case NE:
6078 case UNORDERED:
6079 case ORDERED:
6080 case UNLT:
6081 case UNLE:
6082 case UNGT:
6083 case UNGE:
6084 case UNEQ:
6085 case LTGT:
6086 return CCFPmode;
6088 case LT:
6089 case LE:
6090 case GT:
6091 case GE:
6092 if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6093 return CCFPmode;
6094 return CCFPEmode;
6096 default:
6097 abort ();
6101 /* A compare with a shifted operand. Because of canonicalization, the
6102 comparison will have to be swapped when we emit the assembler. */
6103 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6104 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6105 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6106 || GET_CODE (x) == ROTATERT))
6107 return CC_SWPmode;
6109 /* This operation is performed swapped, but since we only rely on the Z
6110 flag we don't need an additional mode. */
6111 if (GET_MODE (y) == SImode && REG_P (y)
6112 && GET_CODE (x) == NEG
6113 && (op == EQ || op == NE))
6114 return CC_Zmode;
6116 /* This is a special case that is used by combine to allow a
6117 comparison of a shifted byte load to be split into a zero-extend
6118 followed by a comparison of the shifted integer (only valid for
6119 equalities and unsigned inequalities). */
6120 if (GET_MODE (x) == SImode
6121 && GET_CODE (x) == ASHIFT
6122 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6123 && GET_CODE (XEXP (x, 0)) == SUBREG
6124 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6125 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6126 && (op == EQ || op == NE
6127 || op == GEU || op == GTU || op == LTU || op == LEU)
6128 && GET_CODE (y) == CONST_INT)
6129 return CC_Zmode;
6131 /* A construct for a conditional compare, if the false arm contains
6132 0, then both conditions must be true, otherwise either condition
6133 must be true. Not all conditions are possible, so CCmode is
6134 returned if it can't be done. */
6135 if (GET_CODE (x) == IF_THEN_ELSE
6136 && (XEXP (x, 2) == const0_rtx
6137 || XEXP (x, 2) == const1_rtx)
6138 && COMPARISON_P (XEXP (x, 0))
6139 && COMPARISON_P (XEXP (x, 1)))
6140 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6141 INTVAL (XEXP (x, 2)));
6143 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
6144 if (GET_CODE (x) == AND
6145 && COMPARISON_P (XEXP (x, 0))
6146 && COMPARISON_P (XEXP (x, 1)))
6147 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6148 DOM_CC_X_AND_Y);
6150 if (GET_CODE (x) == IOR
6151 && COMPARISON_P (XEXP (x, 0))
6152 && COMPARISON_P (XEXP (x, 1)))
6153 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6154 DOM_CC_X_OR_Y);
6156 /* An operation (on Thumb) where we want to test for a single bit.
6157 This is done by shifting that bit up into the top bit of a
6158 scratch register; we can then branch on the sign bit. */
6159 if (TARGET_THUMB
6160 && GET_MODE (x) == SImode
6161 && (op == EQ || op == NE)
6162 && (GET_CODE (x) == ZERO_EXTRACT))
6163 return CC_Nmode;
6165 /* An operation that sets the condition codes as a side-effect, the
6166 V flag is not set correctly, so we can only use comparisons where
6167 this doesn't matter. (For LT and GE we can use "mi" and "pl"
6168 instead.) */
6169 if (GET_MODE (x) == SImode
6170 && y == const0_rtx
6171 && (op == EQ || op == NE || op == LT || op == GE)
6172 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6173 || GET_CODE (x) == AND || GET_CODE (x) == IOR
6174 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6175 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6176 || GET_CODE (x) == LSHIFTRT
6177 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6178 || GET_CODE (x) == ROTATERT
6179 || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6180 return CC_NOOVmode;
6182 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6183 return CC_Zmode;
6185 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6186 && GET_CODE (x) == PLUS
6187 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6188 return CC_Cmode;
6190 return CCmode;
6193 /* X and Y are two things to compare using CODE. Emit the compare insn and
6194 return the rtx for register 0 in the proper mode. FP means this is a
6195 floating point compare: I don't think that it is needed on the arm. */
6197 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6199 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6200 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6202 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6203 gen_rtx_COMPARE (mode, x, y)));
6205 return cc_reg;
6208 /* Generate a sequence of insns that will generate the correct return
6209 address mask depending on the physical architecture that the program
6210 is running on. */
6212 arm_gen_return_addr_mask (void)
6214 rtx reg = gen_reg_rtx (Pmode);
6216 emit_insn (gen_return_addr_mask (reg));
6217 return reg;
6220 void
6221 arm_reload_in_hi (rtx *operands)
6223 rtx ref = operands[1];
6224 rtx base, scratch;
6225 HOST_WIDE_INT offset = 0;
6227 if (GET_CODE (ref) == SUBREG)
6229 offset = SUBREG_BYTE (ref);
6230 ref = SUBREG_REG (ref);
6233 if (GET_CODE (ref) == REG)
6235 /* We have a pseudo which has been spilt onto the stack; there
6236 are two cases here: the first where there is a simple
6237 stack-slot replacement and a second where the stack-slot is
6238 out of range, or is used as a subreg. */
6239 if (reg_equiv_mem[REGNO (ref)])
6241 ref = reg_equiv_mem[REGNO (ref)];
6242 base = find_replacement (&XEXP (ref, 0));
6244 else
6245 /* The slot is out of range, or was dressed up in a SUBREG. */
6246 base = reg_equiv_address[REGNO (ref)];
6248 else
6249 base = find_replacement (&XEXP (ref, 0));
6251 /* Handle the case where the address is too complex to be offset by 1. */
6252 if (GET_CODE (base) == MINUS
6253 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6255 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6257 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6258 base = base_plus;
6260 else if (GET_CODE (base) == PLUS)
6262 /* The addend must be CONST_INT, or we would have dealt with it above. */
6263 HOST_WIDE_INT hi, lo;
6265 offset += INTVAL (XEXP (base, 1));
6266 base = XEXP (base, 0);
6268 /* Rework the address into a legal sequence of insns. */
6269 /* Valid range for lo is -4095 -> 4095 */
6270 lo = (offset >= 0
6271 ? (offset & 0xfff)
6272 : -((-offset) & 0xfff));
6274 /* Corner case, if lo is the max offset then we would be out of range
6275 once we have added the additional 1 below, so bump the msb into the
6276 pre-loading insn(s). */
6277 if (lo == 4095)
6278 lo &= 0x7ff;
6280 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6281 ^ (HOST_WIDE_INT) 0x80000000)
6282 - (HOST_WIDE_INT) 0x80000000);
6284 if (hi + lo != offset)
6285 abort ();
6287 if (hi != 0)
6289 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
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 /* Operands[2] may overlap operands[0] (though it won't overlap
6300 operands[1]), that's why we asked for a DImode reg -- so we can
6301 use the bit that does not overlap. */
6302 if (REGNO (operands[2]) == REGNO (operands[0]))
6303 scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6304 else
6305 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6307 emit_insn (gen_zero_extendqisi2 (scratch,
6308 gen_rtx_MEM (QImode,
6309 plus_constant (base,
6310 offset))));
6311 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6312 gen_rtx_MEM (QImode,
6313 plus_constant (base,
6314 offset + 1))));
6315 if (!BYTES_BIG_ENDIAN)
6316 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6317 gen_rtx_IOR (SImode,
6318 gen_rtx_ASHIFT
6319 (SImode,
6320 gen_rtx_SUBREG (SImode, operands[0], 0),
6321 GEN_INT (8)),
6322 scratch)));
6323 else
6324 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6325 gen_rtx_IOR (SImode,
6326 gen_rtx_ASHIFT (SImode, scratch,
6327 GEN_INT (8)),
6328 gen_rtx_SUBREG (SImode, operands[0],
6329 0))));
6332 /* Handle storing a half-word to memory during reload by synthesizing as two
6333 byte stores. Take care not to clobber the input values until after we
6334 have moved them somewhere safe. This code assumes that if the DImode
6335 scratch in operands[2] overlaps either the input value or output address
6336 in some way, then that value must die in this insn (we absolutely need
6337 two scratch registers for some corner cases). */
6338 void
6339 arm_reload_out_hi (rtx *operands)
6341 rtx ref = operands[0];
6342 rtx outval = operands[1];
6343 rtx base, scratch;
6344 HOST_WIDE_INT offset = 0;
6346 if (GET_CODE (ref) == SUBREG)
6348 offset = SUBREG_BYTE (ref);
6349 ref = SUBREG_REG (ref);
6352 if (GET_CODE (ref) == REG)
6354 /* We have a pseudo which has been spilt onto the stack; there
6355 are two cases here: the first where there is a simple
6356 stack-slot replacement and a second where the stack-slot is
6357 out of range, or is used as a subreg. */
6358 if (reg_equiv_mem[REGNO (ref)])
6360 ref = reg_equiv_mem[REGNO (ref)];
6361 base = find_replacement (&XEXP (ref, 0));
6363 else
6364 /* The slot is out of range, or was dressed up in a SUBREG. */
6365 base = reg_equiv_address[REGNO (ref)];
6367 else
6368 base = find_replacement (&XEXP (ref, 0));
6370 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6372 /* Handle the case where the address is too complex to be offset by 1. */
6373 if (GET_CODE (base) == MINUS
6374 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6376 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6378 /* Be careful not to destroy OUTVAL. */
6379 if (reg_overlap_mentioned_p (base_plus, outval))
6381 /* Updating base_plus might destroy outval, see if we can
6382 swap the scratch and base_plus. */
6383 if (!reg_overlap_mentioned_p (scratch, outval))
6385 rtx tmp = scratch;
6386 scratch = base_plus;
6387 base_plus = tmp;
6389 else
6391 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6393 /* Be conservative and copy OUTVAL into the scratch now,
6394 this should only be necessary if outval is a subreg
6395 of something larger than a word. */
6396 /* XXX Might this clobber base? I can't see how it can,
6397 since scratch is known to overlap with OUTVAL, and
6398 must be wider than a word. */
6399 emit_insn (gen_movhi (scratch_hi, outval));
6400 outval = scratch_hi;
6404 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6405 base = base_plus;
6407 else if (GET_CODE (base) == PLUS)
6409 /* The addend must be CONST_INT, or we would have dealt with it above. */
6410 HOST_WIDE_INT hi, lo;
6412 offset += INTVAL (XEXP (base, 1));
6413 base = XEXP (base, 0);
6415 /* Rework the address into a legal sequence of insns. */
6416 /* Valid range for lo is -4095 -> 4095 */
6417 lo = (offset >= 0
6418 ? (offset & 0xfff)
6419 : -((-offset) & 0xfff));
6421 /* Corner case, if lo is the max offset then we would be out of range
6422 once we have added the additional 1 below, so bump the msb into the
6423 pre-loading insn(s). */
6424 if (lo == 4095)
6425 lo &= 0x7ff;
6427 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6428 ^ (HOST_WIDE_INT) 0x80000000)
6429 - (HOST_WIDE_INT) 0x80000000);
6431 if (hi + lo != offset)
6432 abort ();
6434 if (hi != 0)
6436 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6438 /* Be careful not to destroy OUTVAL. */
6439 if (reg_overlap_mentioned_p (base_plus, outval))
6441 /* Updating base_plus might destroy outval, see if we
6442 can swap the scratch and base_plus. */
6443 if (!reg_overlap_mentioned_p (scratch, outval))
6445 rtx tmp = scratch;
6446 scratch = base_plus;
6447 base_plus = tmp;
6449 else
6451 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6453 /* Be conservative and copy outval into scratch now,
6454 this should only be necessary if outval is a
6455 subreg of something larger than a word. */
6456 /* XXX Might this clobber base? I can't see how it
6457 can, since scratch is known to overlap with
6458 outval. */
6459 emit_insn (gen_movhi (scratch_hi, outval));
6460 outval = scratch_hi;
6464 /* Get the base address; addsi3 knows how to handle constants
6465 that require more than one insn. */
6466 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6467 base = base_plus;
6468 offset = lo;
6472 if (BYTES_BIG_ENDIAN)
6474 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6475 plus_constant (base, offset + 1)),
6476 gen_lowpart (QImode, outval)));
6477 emit_insn (gen_lshrsi3 (scratch,
6478 gen_rtx_SUBREG (SImode, outval, 0),
6479 GEN_INT (8)));
6480 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6481 gen_lowpart (QImode, scratch)));
6483 else
6485 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6486 gen_lowpart (QImode, outval)));
6487 emit_insn (gen_lshrsi3 (scratch,
6488 gen_rtx_SUBREG (SImode, outval, 0),
6489 GEN_INT (8)));
6490 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6491 plus_constant (base, offset + 1)),
6492 gen_lowpart (QImode, scratch)));
6496 /* Print a symbolic form of X to the debug file, F. */
6497 static void
6498 arm_print_value (FILE *f, rtx x)
6500 switch (GET_CODE (x))
6502 case CONST_INT:
6503 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6504 return;
6506 case CONST_DOUBLE:
6507 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6508 return;
6510 case CONST_VECTOR:
6512 int i;
6514 fprintf (f, "<");
6515 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6517 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6518 if (i < (CONST_VECTOR_NUNITS (x) - 1))
6519 fputc (',', f);
6521 fprintf (f, ">");
6523 return;
6525 case CONST_STRING:
6526 fprintf (f, "\"%s\"", XSTR (x, 0));
6527 return;
6529 case SYMBOL_REF:
6530 fprintf (f, "`%s'", XSTR (x, 0));
6531 return;
6533 case LABEL_REF:
6534 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6535 return;
6537 case CONST:
6538 arm_print_value (f, XEXP (x, 0));
6539 return;
6541 case PLUS:
6542 arm_print_value (f, XEXP (x, 0));
6543 fprintf (f, "+");
6544 arm_print_value (f, XEXP (x, 1));
6545 return;
6547 case PC:
6548 fprintf (f, "pc");
6549 return;
6551 default:
6552 fprintf (f, "????");
6553 return;
6557 /* Routines for manipulation of the constant pool. */
6559 /* Arm instructions cannot load a large constant directly into a
6560 register; they have to come from a pc relative load. The constant
6561 must therefore be placed in the addressable range of the pc
6562 relative load. Depending on the precise pc relative load
6563 instruction the range is somewhere between 256 bytes and 4k. This
6564 means that we often have to dump a constant inside a function, and
6565 generate code to branch around it.
6567 It is important to minimize this, since the branches will slow
6568 things down and make the code larger.
6570 Normally we can hide the table after an existing unconditional
6571 branch so that there is no interruption of the flow, but in the
6572 worst case the code looks like this:
6574 ldr rn, L1
6576 b L2
6577 align
6578 L1: .long value
6582 ldr rn, L3
6584 b L4
6585 align
6586 L3: .long value
6590 We fix this by performing a scan after scheduling, which notices
6591 which instructions need to have their operands fetched from the
6592 constant table and builds the table.
6594 The algorithm starts by building a table of all the constants that
6595 need fixing up and all the natural barriers in the function (places
6596 where a constant table can be dropped without breaking the flow).
6597 For each fixup we note how far the pc-relative replacement will be
6598 able to reach and the offset of the instruction into the function.
6600 Having built the table we then group the fixes together to form
6601 tables that are as large as possible (subject to addressing
6602 constraints) and emit each table of constants after the last
6603 barrier that is within range of all the instructions in the group.
6604 If a group does not contain a barrier, then we forcibly create one
6605 by inserting a jump instruction into the flow. Once the table has
6606 been inserted, the insns are then modified to reference the
6607 relevant entry in the pool.
6609 Possible enhancements to the algorithm (not implemented) are:
6611 1) For some processors and object formats, there may be benefit in
6612 aligning the pools to the start of cache lines; this alignment
6613 would need to be taken into account when calculating addressability
6614 of a pool. */
6616 /* These typedefs are located at the start of this file, so that
6617 they can be used in the prototypes there. This comment is to
6618 remind readers of that fact so that the following structures
6619 can be understood more easily.
6621 typedef struct minipool_node Mnode;
6622 typedef struct minipool_fixup Mfix; */
6624 struct minipool_node
6626 /* Doubly linked chain of entries. */
6627 Mnode * next;
6628 Mnode * prev;
6629 /* The maximum offset into the code that this entry can be placed. While
6630 pushing fixes for forward references, all entries are sorted in order
6631 of increasing max_address. */
6632 HOST_WIDE_INT max_address;
6633 /* Similarly for an entry inserted for a backwards ref. */
6634 HOST_WIDE_INT min_address;
6635 /* The number of fixes referencing this entry. This can become zero
6636 if we "unpush" an entry. In this case we ignore the entry when we
6637 come to emit the code. */
6638 int refcount;
6639 /* The offset from the start of the minipool. */
6640 HOST_WIDE_INT offset;
6641 /* The value in table. */
6642 rtx value;
6643 /* The mode of value. */
6644 enum machine_mode mode;
6645 /* The size of the value. With iWMMXt enabled
6646 sizes > 4 also imply an alignment of 8-bytes. */
6647 int fix_size;
6650 struct minipool_fixup
6652 Mfix * next;
6653 rtx insn;
6654 HOST_WIDE_INT address;
6655 rtx * loc;
6656 enum machine_mode mode;
6657 int fix_size;
6658 rtx value;
6659 Mnode * minipool;
6660 HOST_WIDE_INT forwards;
6661 HOST_WIDE_INT backwards;
6664 /* Fixes less than a word need padding out to a word boundary. */
6665 #define MINIPOOL_FIX_SIZE(mode) \
6666 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6668 static Mnode * minipool_vector_head;
6669 static Mnode * minipool_vector_tail;
6670 static rtx minipool_vector_label;
6672 /* The linked list of all minipool fixes required for this function. */
6673 Mfix * minipool_fix_head;
6674 Mfix * minipool_fix_tail;
6675 /* The fix entry for the current minipool, once it has been placed. */
6676 Mfix * minipool_barrier;
6678 /* Determines if INSN is the start of a jump table. Returns the end
6679 of the TABLE or NULL_RTX. */
6680 static rtx
6681 is_jump_table (rtx insn)
6683 rtx table;
6685 if (GET_CODE (insn) == JUMP_INSN
6686 && JUMP_LABEL (insn) != NULL
6687 && ((table = next_real_insn (JUMP_LABEL (insn)))
6688 == next_real_insn (insn))
6689 && table != NULL
6690 && GET_CODE (table) == JUMP_INSN
6691 && (GET_CODE (PATTERN (table)) == ADDR_VEC
6692 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6693 return table;
6695 return NULL_RTX;
6698 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6699 #define JUMP_TABLES_IN_TEXT_SECTION 0
6700 #endif
6702 static HOST_WIDE_INT
6703 get_jump_table_size (rtx insn)
6705 /* ADDR_VECs only take room if read-only data does into the text
6706 section. */
6707 if (JUMP_TABLES_IN_TEXT_SECTION
6708 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6709 || 1
6710 #endif
6713 rtx body = PATTERN (insn);
6714 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6716 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6719 return 0;
6722 /* Move a minipool fix MP from its current location to before MAX_MP.
6723 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6724 constraints may need updating. */
6725 static Mnode *
6726 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6727 HOST_WIDE_INT max_address)
6729 /* This should never be true and the code below assumes these are
6730 different. */
6731 if (mp == max_mp)
6732 abort ();
6734 if (max_mp == NULL)
6736 if (max_address < mp->max_address)
6737 mp->max_address = max_address;
6739 else
6741 if (max_address > max_mp->max_address - mp->fix_size)
6742 mp->max_address = max_mp->max_address - mp->fix_size;
6743 else
6744 mp->max_address = max_address;
6746 /* Unlink MP from its current position. Since max_mp is non-null,
6747 mp->prev must be non-null. */
6748 mp->prev->next = mp->next;
6749 if (mp->next != NULL)
6750 mp->next->prev = mp->prev;
6751 else
6752 minipool_vector_tail = mp->prev;
6754 /* Re-insert it before MAX_MP. */
6755 mp->next = max_mp;
6756 mp->prev = max_mp->prev;
6757 max_mp->prev = mp;
6759 if (mp->prev != NULL)
6760 mp->prev->next = mp;
6761 else
6762 minipool_vector_head = mp;
6765 /* Save the new entry. */
6766 max_mp = mp;
6768 /* Scan over the preceding entries and adjust their addresses as
6769 required. */
6770 while (mp->prev != NULL
6771 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6773 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6774 mp = mp->prev;
6777 return max_mp;
6780 /* Add a constant to the minipool for a forward reference. Returns the
6781 node added or NULL if the constant will not fit in this pool. */
6782 static Mnode *
6783 add_minipool_forward_ref (Mfix *fix)
6785 /* If set, max_mp is the first pool_entry that has a lower
6786 constraint than the one we are trying to add. */
6787 Mnode * max_mp = NULL;
6788 HOST_WIDE_INT max_address = fix->address + fix->forwards;
6789 Mnode * mp;
6791 /* If this fix's address is greater than the address of the first
6792 entry, then we can't put the fix in this pool. We subtract the
6793 size of the current fix to ensure that if the table is fully
6794 packed we still have enough room to insert this value by suffling
6795 the other fixes forwards. */
6796 if (minipool_vector_head &&
6797 fix->address >= minipool_vector_head->max_address - fix->fix_size)
6798 return NULL;
6800 /* Scan the pool to see if a constant with the same value has
6801 already been added. While we are doing this, also note the
6802 location where we must insert the constant if it doesn't already
6803 exist. */
6804 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6806 if (GET_CODE (fix->value) == GET_CODE (mp->value)
6807 && fix->mode == mp->mode
6808 && (GET_CODE (fix->value) != CODE_LABEL
6809 || (CODE_LABEL_NUMBER (fix->value)
6810 == CODE_LABEL_NUMBER (mp->value)))
6811 && rtx_equal_p (fix->value, mp->value))
6813 /* More than one fix references this entry. */
6814 mp->refcount++;
6815 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6818 /* Note the insertion point if necessary. */
6819 if (max_mp == NULL
6820 && mp->max_address > max_address)
6821 max_mp = mp;
6823 /* If we are inserting an 8-bytes aligned quantity and
6824 we have not already found an insertion point, then
6825 make sure that all such 8-byte aligned quantities are
6826 placed at the start of the pool. */
6827 if (ARM_DOUBLEWORD_ALIGN
6828 && max_mp == NULL
6829 && fix->fix_size == 8
6830 && mp->fix_size != 8)
6832 max_mp = mp;
6833 max_address = mp->max_address;
6837 /* The value is not currently in the minipool, so we need to create
6838 a new entry for it. If MAX_MP is NULL, the entry will be put on
6839 the end of the list since the placement is less constrained than
6840 any existing entry. Otherwise, we insert the new fix before
6841 MAX_MP and, if necessary, adjust the constraints on the other
6842 entries. */
6843 mp = xmalloc (sizeof (* mp));
6844 mp->fix_size = fix->fix_size;
6845 mp->mode = fix->mode;
6846 mp->value = fix->value;
6847 mp->refcount = 1;
6848 /* Not yet required for a backwards ref. */
6849 mp->min_address = -65536;
6851 if (max_mp == NULL)
6853 mp->max_address = max_address;
6854 mp->next = NULL;
6855 mp->prev = minipool_vector_tail;
6857 if (mp->prev == NULL)
6859 minipool_vector_head = mp;
6860 minipool_vector_label = gen_label_rtx ();
6862 else
6863 mp->prev->next = mp;
6865 minipool_vector_tail = mp;
6867 else
6869 if (max_address > max_mp->max_address - mp->fix_size)
6870 mp->max_address = max_mp->max_address - mp->fix_size;
6871 else
6872 mp->max_address = max_address;
6874 mp->next = max_mp;
6875 mp->prev = max_mp->prev;
6876 max_mp->prev = mp;
6877 if (mp->prev != NULL)
6878 mp->prev->next = mp;
6879 else
6880 minipool_vector_head = mp;
6883 /* Save the new entry. */
6884 max_mp = mp;
6886 /* Scan over the preceding entries and adjust their addresses as
6887 required. */
6888 while (mp->prev != NULL
6889 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6891 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6892 mp = mp->prev;
6895 return max_mp;
6898 static Mnode *
6899 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
6900 HOST_WIDE_INT min_address)
6902 HOST_WIDE_INT offset;
6904 /* This should never be true, and the code below assumes these are
6905 different. */
6906 if (mp == min_mp)
6907 abort ();
6909 if (min_mp == NULL)
6911 if (min_address > mp->min_address)
6912 mp->min_address = min_address;
6914 else
6916 /* We will adjust this below if it is too loose. */
6917 mp->min_address = min_address;
6919 /* Unlink MP from its current position. Since min_mp is non-null,
6920 mp->next must be non-null. */
6921 mp->next->prev = mp->prev;
6922 if (mp->prev != NULL)
6923 mp->prev->next = mp->next;
6924 else
6925 minipool_vector_head = mp->next;
6927 /* Reinsert it after MIN_MP. */
6928 mp->prev = min_mp;
6929 mp->next = min_mp->next;
6930 min_mp->next = mp;
6931 if (mp->next != NULL)
6932 mp->next->prev = mp;
6933 else
6934 minipool_vector_tail = mp;
6937 min_mp = mp;
6939 offset = 0;
6940 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6942 mp->offset = offset;
6943 if (mp->refcount > 0)
6944 offset += mp->fix_size;
6946 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
6947 mp->next->min_address = mp->min_address + mp->fix_size;
6950 return min_mp;
6953 /* Add a constant to the minipool for a backward reference. Returns the
6954 node added or NULL if the constant will not fit in this pool.
6956 Note that the code for insertion for a backwards reference can be
6957 somewhat confusing because the calculated offsets for each fix do
6958 not take into account the size of the pool (which is still under
6959 construction. */
6960 static Mnode *
6961 add_minipool_backward_ref (Mfix *fix)
6963 /* If set, min_mp is the last pool_entry that has a lower constraint
6964 than the one we are trying to add. */
6965 Mnode *min_mp = NULL;
6966 /* This can be negative, since it is only a constraint. */
6967 HOST_WIDE_INT min_address = fix->address - fix->backwards;
6968 Mnode *mp;
6970 /* If we can't reach the current pool from this insn, or if we can't
6971 insert this entry at the end of the pool without pushing other
6972 fixes out of range, then we don't try. This ensures that we
6973 can't fail later on. */
6974 if (min_address >= minipool_barrier->address
6975 || (minipool_vector_tail->min_address + fix->fix_size
6976 >= minipool_barrier->address))
6977 return NULL;
6979 /* Scan the pool to see if a constant with the same value has
6980 already been added. While we are doing this, also note the
6981 location where we must insert the constant if it doesn't already
6982 exist. */
6983 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
6985 if (GET_CODE (fix->value) == GET_CODE (mp->value)
6986 && fix->mode == mp->mode
6987 && (GET_CODE (fix->value) != CODE_LABEL
6988 || (CODE_LABEL_NUMBER (fix->value)
6989 == CODE_LABEL_NUMBER (mp->value)))
6990 && rtx_equal_p (fix->value, mp->value)
6991 /* Check that there is enough slack to move this entry to the
6992 end of the table (this is conservative). */
6993 && (mp->max_address
6994 > (minipool_barrier->address
6995 + minipool_vector_tail->offset
6996 + minipool_vector_tail->fix_size)))
6998 mp->refcount++;
6999 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7002 if (min_mp != NULL)
7003 mp->min_address += fix->fix_size;
7004 else
7006 /* Note the insertion point if necessary. */
7007 if (mp->min_address < min_address)
7009 /* For now, we do not allow the insertion of 8-byte alignment
7010 requiring nodes anywhere but at the start of the pool. */
7011 if (ARM_DOUBLEWORD_ALIGN
7012 && fix->fix_size == 8 && mp->fix_size != 8)
7013 return NULL;
7014 else
7015 min_mp = mp;
7017 else if (mp->max_address
7018 < minipool_barrier->address + mp->offset + fix->fix_size)
7020 /* Inserting before this entry would push the fix beyond
7021 its maximum address (which can happen if we have
7022 re-located a forwards fix); force the new fix to come
7023 after it. */
7024 min_mp = mp;
7025 min_address = mp->min_address + fix->fix_size;
7027 /* If we are inserting an 8-bytes aligned quantity and
7028 we have not already found an insertion point, then
7029 make sure that all such 8-byte aligned quantities are
7030 placed at the start of the pool. */
7031 else if (ARM_DOUBLEWORD_ALIGN
7032 && min_mp == NULL
7033 && fix->fix_size == 8
7034 && mp->fix_size < 8)
7036 min_mp = mp;
7037 min_address = mp->min_address + fix->fix_size;
7042 /* We need to create a new entry. */
7043 mp = xmalloc (sizeof (* mp));
7044 mp->fix_size = fix->fix_size;
7045 mp->mode = fix->mode;
7046 mp->value = fix->value;
7047 mp->refcount = 1;
7048 mp->max_address = minipool_barrier->address + 65536;
7050 mp->min_address = min_address;
7052 if (min_mp == NULL)
7054 mp->prev = NULL;
7055 mp->next = minipool_vector_head;
7057 if (mp->next == NULL)
7059 minipool_vector_tail = mp;
7060 minipool_vector_label = gen_label_rtx ();
7062 else
7063 mp->next->prev = mp;
7065 minipool_vector_head = mp;
7067 else
7069 mp->next = min_mp->next;
7070 mp->prev = min_mp;
7071 min_mp->next = mp;
7073 if (mp->next != NULL)
7074 mp->next->prev = mp;
7075 else
7076 minipool_vector_tail = mp;
7079 /* Save the new entry. */
7080 min_mp = mp;
7082 if (mp->prev)
7083 mp = mp->prev;
7084 else
7085 mp->offset = 0;
7087 /* Scan over the following entries and adjust their offsets. */
7088 while (mp->next != NULL)
7090 if (mp->next->min_address < mp->min_address + mp->fix_size)
7091 mp->next->min_address = mp->min_address + mp->fix_size;
7093 if (mp->refcount)
7094 mp->next->offset = mp->offset + mp->fix_size;
7095 else
7096 mp->next->offset = mp->offset;
7098 mp = mp->next;
7101 return min_mp;
7104 static void
7105 assign_minipool_offsets (Mfix *barrier)
7107 HOST_WIDE_INT offset = 0;
7108 Mnode *mp;
7110 minipool_barrier = barrier;
7112 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7114 mp->offset = offset;
7116 if (mp->refcount > 0)
7117 offset += mp->fix_size;
7121 /* Output the literal table */
7122 static void
7123 dump_minipool (rtx scan)
7125 Mnode * mp;
7126 Mnode * nmp;
7127 int align64 = 0;
7129 if (ARM_DOUBLEWORD_ALIGN)
7130 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7131 if (mp->refcount > 0 && mp->fix_size == 8)
7133 align64 = 1;
7134 break;
7137 if (dump_file)
7138 fprintf (dump_file,
7139 ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7140 INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7142 scan = emit_label_after (gen_label_rtx (), scan);
7143 scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7144 scan = emit_label_after (minipool_vector_label, scan);
7146 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7148 if (mp->refcount > 0)
7150 if (dump_file)
7152 fprintf (dump_file,
7153 ";; Offset %u, min %ld, max %ld ",
7154 (unsigned) mp->offset, (unsigned long) mp->min_address,
7155 (unsigned long) mp->max_address);
7156 arm_print_value (dump_file, mp->value);
7157 fputc ('\n', dump_file);
7160 switch (mp->fix_size)
7162 #ifdef HAVE_consttable_1
7163 case 1:
7164 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7165 break;
7167 #endif
7168 #ifdef HAVE_consttable_2
7169 case 2:
7170 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7171 break;
7173 #endif
7174 #ifdef HAVE_consttable_4
7175 case 4:
7176 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7177 break;
7179 #endif
7180 #ifdef HAVE_consttable_8
7181 case 8:
7182 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7183 break;
7185 #endif
7186 default:
7187 abort ();
7188 break;
7192 nmp = mp->next;
7193 free (mp);
7196 minipool_vector_head = minipool_vector_tail = NULL;
7197 scan = emit_insn_after (gen_consttable_end (), scan);
7198 scan = emit_barrier_after (scan);
7201 /* Return the cost of forcibly inserting a barrier after INSN. */
7202 static int
7203 arm_barrier_cost (rtx insn)
7205 /* Basing the location of the pool on the loop depth is preferable,
7206 but at the moment, the basic block information seems to be
7207 corrupt by this stage of the compilation. */
7208 int base_cost = 50;
7209 rtx next = next_nonnote_insn (insn);
7211 if (next != NULL && GET_CODE (next) == CODE_LABEL)
7212 base_cost -= 20;
7214 switch (GET_CODE (insn))
7216 case CODE_LABEL:
7217 /* It will always be better to place the table before the label, rather
7218 than after it. */
7219 return 50;
7221 case INSN:
7222 case CALL_INSN:
7223 return base_cost;
7225 case JUMP_INSN:
7226 return base_cost - 10;
7228 default:
7229 return base_cost + 10;
7233 /* Find the best place in the insn stream in the range
7234 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7235 Create the barrier by inserting a jump and add a new fix entry for
7236 it. */
7237 static Mfix *
7238 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7240 HOST_WIDE_INT count = 0;
7241 rtx barrier;
7242 rtx from = fix->insn;
7243 rtx selected = from;
7244 int selected_cost;
7245 HOST_WIDE_INT selected_address;
7246 Mfix * new_fix;
7247 HOST_WIDE_INT max_count = max_address - fix->address;
7248 rtx label = gen_label_rtx ();
7250 selected_cost = arm_barrier_cost (from);
7251 selected_address = fix->address;
7253 while (from && count < max_count)
7255 rtx tmp;
7256 int new_cost;
7258 /* This code shouldn't have been called if there was a natural barrier
7259 within range. */
7260 if (GET_CODE (from) == BARRIER)
7261 abort ();
7263 /* Count the length of this insn. */
7264 count += get_attr_length (from);
7266 /* If there is a jump table, add its length. */
7267 tmp = is_jump_table (from);
7268 if (tmp != NULL)
7270 count += get_jump_table_size (tmp);
7272 /* Jump tables aren't in a basic block, so base the cost on
7273 the dispatch insn. If we select this location, we will
7274 still put the pool after the table. */
7275 new_cost = arm_barrier_cost (from);
7277 if (count < max_count && new_cost <= selected_cost)
7279 selected = tmp;
7280 selected_cost = new_cost;
7281 selected_address = fix->address + count;
7284 /* Continue after the dispatch table. */
7285 from = NEXT_INSN (tmp);
7286 continue;
7289 new_cost = arm_barrier_cost (from);
7291 if (count < max_count && new_cost <= selected_cost)
7293 selected = from;
7294 selected_cost = new_cost;
7295 selected_address = fix->address + count;
7298 from = NEXT_INSN (from);
7301 /* Create a new JUMP_INSN that branches around a barrier. */
7302 from = emit_jump_insn_after (gen_jump (label), selected);
7303 JUMP_LABEL (from) = label;
7304 barrier = emit_barrier_after (from);
7305 emit_label_after (label, barrier);
7307 /* Create a minipool barrier entry for the new barrier. */
7308 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7309 new_fix->insn = barrier;
7310 new_fix->address = selected_address;
7311 new_fix->next = fix->next;
7312 fix->next = new_fix;
7314 return new_fix;
7317 /* Record that there is a natural barrier in the insn stream at
7318 ADDRESS. */
7319 static void
7320 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7322 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7324 fix->insn = insn;
7325 fix->address = address;
7327 fix->next = NULL;
7328 if (minipool_fix_head != NULL)
7329 minipool_fix_tail->next = fix;
7330 else
7331 minipool_fix_head = fix;
7333 minipool_fix_tail = fix;
7336 /* Record INSN, which will need fixing up to load a value from the
7337 minipool. ADDRESS is the offset of the insn since the start of the
7338 function; LOC is a pointer to the part of the insn which requires
7339 fixing; VALUE is the constant that must be loaded, which is of type
7340 MODE. */
7341 static void
7342 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7343 enum machine_mode mode, rtx value)
7345 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7347 #ifdef AOF_ASSEMBLER
7348 /* PIC symbol references need to be converted into offsets into the
7349 based area. */
7350 /* XXX This shouldn't be done here. */
7351 if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7352 value = aof_pic_entry (value);
7353 #endif /* AOF_ASSEMBLER */
7355 fix->insn = insn;
7356 fix->address = address;
7357 fix->loc = loc;
7358 fix->mode = mode;
7359 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7360 fix->value = value;
7361 fix->forwards = get_attr_pool_range (insn);
7362 fix->backwards = get_attr_neg_pool_range (insn);
7363 fix->minipool = NULL;
7365 /* If an insn doesn't have a range defined for it, then it isn't
7366 expecting to be reworked by this code. Better to abort now than
7367 to generate duff assembly code. */
7368 if (fix->forwards == 0 && fix->backwards == 0)
7369 abort ();
7371 /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7372 So there might be an empty word before the start of the pool.
7373 Hence we reduce the forward range by 4 to allow for this
7374 possibility. */
7375 if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7376 fix->forwards -= 4;
7378 if (dump_file)
7380 fprintf (dump_file,
7381 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7382 GET_MODE_NAME (mode),
7383 INSN_UID (insn), (unsigned long) address,
7384 -1 * (long)fix->backwards, (long)fix->forwards);
7385 arm_print_value (dump_file, fix->value);
7386 fprintf (dump_file, "\n");
7389 /* Add it to the chain of fixes. */
7390 fix->next = NULL;
7392 if (minipool_fix_head != NULL)
7393 minipool_fix_tail->next = fix;
7394 else
7395 minipool_fix_head = fix;
7397 minipool_fix_tail = fix;
7400 /* Return the cost of synthesizing a 64-bit constant VAL inline.
7401 Returns the number of insns needed, or 99 if we don't know how to
7402 do it. */
7404 arm_const_double_inline_cost (rtx val)
7406 rtx lowpart, highpart;
7407 enum machine_mode mode;
7409 mode = GET_MODE (val);
7411 if (mode == VOIDmode)
7412 mode = DImode;
7414 gcc_assert (GET_MODE_SIZE (mode) == 8);
7416 lowpart = gen_lowpart (SImode, val);
7417 highpart = gen_highpart_mode (SImode, mode, val);
7419 gcc_assert (GET_CODE (lowpart) == CONST_INT);
7420 gcc_assert (GET_CODE (highpart) == CONST_INT);
7422 return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
7423 NULL_RTX, NULL_RTX, 0, 0)
7424 + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
7425 NULL_RTX, NULL_RTX, 0, 0));
7428 /* Return true if it is worthwhile to split a 64-bit constant into two
7429 32-bit operations. This is the case if optimizing for size, or
7430 if we have load delay slots, or if one 32-bit part can be done with
7431 a single data operation. */
7432 bool
7433 arm_const_double_by_parts (rtx val)
7435 enum machine_mode mode = GET_MODE (val);
7436 rtx part;
7438 if (optimize_size || arm_ld_sched)
7439 return true;
7441 if (mode == VOIDmode)
7442 mode = DImode;
7444 part = gen_highpart_mode (SImode, mode, val);
7446 gcc_assert (GET_CODE (part) == CONST_INT);
7448 if (const_ok_for_arm (INTVAL (part))
7449 || const_ok_for_arm (~INTVAL (part)))
7450 return true;
7452 part = gen_lowpart (SImode, val);
7454 gcc_assert (GET_CODE (part) == CONST_INT);
7456 if (const_ok_for_arm (INTVAL (part))
7457 || const_ok_for_arm (~INTVAL (part)))
7458 return true;
7460 return false;
7463 /* Scan INSN and note any of its operands that need fixing.
7464 If DO_PUSHES is false we do not actually push any of the fixups
7465 needed. The function returns TRUE if any fixups were needed/pushed.
7466 This is used by arm_memory_load_p() which needs to know about loads
7467 of constants that will be converted into minipool loads. */
7468 static bool
7469 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7471 bool result = false;
7472 int opno;
7474 extract_insn (insn);
7476 if (!constrain_operands (1))
7477 fatal_insn_not_found (insn);
7479 if (recog_data.n_alternatives == 0)
7480 return false;
7482 /* Fill in recog_op_alt with information about the constraints of
7483 this insn. */
7484 preprocess_constraints ();
7486 for (opno = 0; opno < recog_data.n_operands; opno++)
7488 /* Things we need to fix can only occur in inputs. */
7489 if (recog_data.operand_type[opno] != OP_IN)
7490 continue;
7492 /* If this alternative is a memory reference, then any mention
7493 of constants in this alternative is really to fool reload
7494 into allowing us to accept one there. We need to fix them up
7495 now so that we output the right code. */
7496 if (recog_op_alt[opno][which_alternative].memory_ok)
7498 rtx op = recog_data.operand[opno];
7500 if (CONSTANT_P (op))
7502 if (do_pushes)
7503 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7504 recog_data.operand_mode[opno], op);
7505 result = true;
7507 else if (GET_CODE (op) == MEM
7508 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7509 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7511 if (do_pushes)
7513 rtx cop = avoid_constant_pool_reference (op);
7515 /* Casting the address of something to a mode narrower
7516 than a word can cause avoid_constant_pool_reference()
7517 to return the pool reference itself. That's no good to
7518 us here. Lets just hope that we can use the
7519 constant pool value directly. */
7520 if (op == cop)
7521 cop = get_pool_constant (XEXP (op, 0));
7523 push_minipool_fix (insn, address,
7524 recog_data.operand_loc[opno],
7525 recog_data.operand_mode[opno], cop);
7528 result = true;
7533 return result;
7536 /* Gcc puts the pool in the wrong place for ARM, since we can only
7537 load addresses a limited distance around the pc. We do some
7538 special munging to move the constant pool values to the correct
7539 point in the code. */
7540 static void
7541 arm_reorg (void)
7543 rtx insn;
7544 HOST_WIDE_INT address = 0;
7545 Mfix * fix;
7547 minipool_fix_head = minipool_fix_tail = NULL;
7549 /* The first insn must always be a note, or the code below won't
7550 scan it properly. */
7551 insn = get_insns ();
7552 if (GET_CODE (insn) != NOTE)
7553 abort ();
7555 /* Scan all the insns and record the operands that will need fixing. */
7556 for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7558 if (TARGET_CIRRUS_FIX_INVALID_INSNS
7559 && (arm_cirrus_insn_p (insn)
7560 || GET_CODE (insn) == JUMP_INSN
7561 || arm_memory_load_p (insn)))
7562 cirrus_reorg (insn);
7564 if (GET_CODE (insn) == BARRIER)
7565 push_minipool_barrier (insn, address);
7566 else if (INSN_P (insn))
7568 rtx table;
7570 note_invalid_constants (insn, address, true);
7571 address += get_attr_length (insn);
7573 /* If the insn is a vector jump, add the size of the table
7574 and skip the table. */
7575 if ((table = is_jump_table (insn)) != NULL)
7577 address += get_jump_table_size (table);
7578 insn = table;
7583 fix = minipool_fix_head;
7585 /* Now scan the fixups and perform the required changes. */
7586 while (fix)
7588 Mfix * ftmp;
7589 Mfix * fdel;
7590 Mfix * last_added_fix;
7591 Mfix * last_barrier = NULL;
7592 Mfix * this_fix;
7594 /* Skip any further barriers before the next fix. */
7595 while (fix && GET_CODE (fix->insn) == BARRIER)
7596 fix = fix->next;
7598 /* No more fixes. */
7599 if (fix == NULL)
7600 break;
7602 last_added_fix = NULL;
7604 for (ftmp = fix; ftmp; ftmp = ftmp->next)
7606 if (GET_CODE (ftmp->insn) == BARRIER)
7608 if (ftmp->address >= minipool_vector_head->max_address)
7609 break;
7611 last_barrier = ftmp;
7613 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7614 break;
7616 last_added_fix = ftmp; /* Keep track of the last fix added. */
7619 /* If we found a barrier, drop back to that; any fixes that we
7620 could have reached but come after the barrier will now go in
7621 the next mini-pool. */
7622 if (last_barrier != NULL)
7624 /* Reduce the refcount for those fixes that won't go into this
7625 pool after all. */
7626 for (fdel = last_barrier->next;
7627 fdel && fdel != ftmp;
7628 fdel = fdel->next)
7630 fdel->minipool->refcount--;
7631 fdel->minipool = NULL;
7634 ftmp = last_barrier;
7636 else
7638 /* ftmp is first fix that we can't fit into this pool and
7639 there no natural barriers that we could use. Insert a
7640 new barrier in the code somewhere between the previous
7641 fix and this one, and arrange to jump around it. */
7642 HOST_WIDE_INT max_address;
7644 /* The last item on the list of fixes must be a barrier, so
7645 we can never run off the end of the list of fixes without
7646 last_barrier being set. */
7647 if (ftmp == NULL)
7648 abort ();
7650 max_address = minipool_vector_head->max_address;
7651 /* Check that there isn't another fix that is in range that
7652 we couldn't fit into this pool because the pool was
7653 already too large: we need to put the pool before such an
7654 instruction. */
7655 if (ftmp->address < max_address)
7656 max_address = ftmp->address;
7658 last_barrier = create_fix_barrier (last_added_fix, max_address);
7661 assign_minipool_offsets (last_barrier);
7663 while (ftmp)
7665 if (GET_CODE (ftmp->insn) != BARRIER
7666 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7667 == NULL))
7668 break;
7670 ftmp = ftmp->next;
7673 /* Scan over the fixes we have identified for this pool, fixing them
7674 up and adding the constants to the pool itself. */
7675 for (this_fix = fix; this_fix && ftmp != this_fix;
7676 this_fix = this_fix->next)
7677 if (GET_CODE (this_fix->insn) != BARRIER)
7679 rtx addr
7680 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
7681 minipool_vector_label),
7682 this_fix->minipool->offset);
7683 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7686 dump_minipool (last_barrier->insn);
7687 fix = ftmp;
7690 /* From now on we must synthesize any constants that we can't handle
7691 directly. This can happen if the RTL gets split during final
7692 instruction generation. */
7693 after_arm_reorg = 1;
7695 /* Free the minipool memory. */
7696 obstack_free (&minipool_obstack, minipool_startobj);
7699 /* Routines to output assembly language. */
7701 /* If the rtx is the correct value then return the string of the number.
7702 In this way we can ensure that valid double constants are generated even
7703 when cross compiling. */
7704 const char *
7705 fp_immediate_constant (rtx x)
7707 REAL_VALUE_TYPE r;
7708 int i;
7710 if (!fp_consts_inited)
7711 init_fp_table ();
7713 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7714 for (i = 0; i < 8; i++)
7715 if (REAL_VALUES_EQUAL (r, values_fp[i]))
7716 return strings_fp[i];
7718 abort ();
7721 /* As for fp_immediate_constant, but value is passed directly, not in rtx. */
7722 static const char *
7723 fp_const_from_val (REAL_VALUE_TYPE *r)
7725 int i;
7727 if (!fp_consts_inited)
7728 init_fp_table ();
7730 for (i = 0; i < 8; i++)
7731 if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7732 return strings_fp[i];
7734 abort ();
7737 /* Output the operands of a LDM/STM instruction to STREAM.
7738 MASK is the ARM register set mask of which only bits 0-15 are important.
7739 REG is the base register, either the frame pointer or the stack pointer,
7740 INSTR is the possibly suffixed load or store instruction. */
7742 static void
7743 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
7744 unsigned long mask)
7746 unsigned i;
7747 bool not_first = FALSE;
7749 fputc ('\t', stream);
7750 asm_fprintf (stream, instr, reg);
7751 fputs (", {", stream);
7753 for (i = 0; i <= LAST_ARM_REGNUM; i++)
7754 if (mask & (1 << i))
7756 if (not_first)
7757 fprintf (stream, ", ");
7759 asm_fprintf (stream, "%r", i);
7760 not_first = TRUE;
7763 fprintf (stream, "}\n");
7767 /* Output a FLDMX instruction to STREAM.
7768 BASE if the register containing the address.
7769 REG and COUNT specify the register range.
7770 Extra registers may be added to avoid hardware bugs. */
7772 static void
7773 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7775 int i;
7777 /* Workaround ARM10 VFPr1 bug. */
7778 if (count == 2 && !arm_arch6)
7780 if (reg == 15)
7781 reg--;
7782 count++;
7785 fputc ('\t', stream);
7786 asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7788 for (i = reg; i < reg + count; i++)
7790 if (i > reg)
7791 fputs (", ", stream);
7792 asm_fprintf (stream, "d%d", i);
7794 fputs ("}\n", stream);
7799 /* Output the assembly for a store multiple. */
7801 const char *
7802 vfp_output_fstmx (rtx * operands)
7804 char pattern[100];
7805 int p;
7806 int base;
7807 int i;
7809 strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7810 p = strlen (pattern);
7812 if (GET_CODE (operands[1]) != REG)
7813 abort ();
7815 base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7816 for (i = 1; i < XVECLEN (operands[2], 0); i++)
7818 p += sprintf (&pattern[p], ", d%d", base + i);
7820 strcpy (&pattern[p], "}");
7822 output_asm_insn (pattern, operands);
7823 return "";
7827 /* Emit RTL to save block of VFP register pairs to the stack. Returns the
7828 number of bytes pushed. */
7830 static int
7831 vfp_emit_fstmx (int base_reg, int count)
7833 rtx par;
7834 rtx dwarf;
7835 rtx tmp, reg;
7836 int i;
7838 /* Workaround ARM10 VFPr1 bug. Data corruption can occur when exactly two
7839 register pairs are stored by a store multiple insn. We avoid this
7840 by pushing an extra pair. */
7841 if (count == 2 && !arm_arch6)
7843 if (base_reg == LAST_VFP_REGNUM - 3)
7844 base_reg -= 2;
7845 count++;
7848 /* ??? The frame layout is implementation defined. We describe
7849 standard format 1 (equivalent to a FSTMD insn and unused pad word).
7850 We really need some way of representing the whole block so that the
7851 unwinder can figure it out at runtime. */
7852 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7853 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
7855 reg = gen_rtx_REG (DFmode, base_reg);
7856 base_reg += 2;
7858 XVECEXP (par, 0, 0)
7859 = gen_rtx_SET (VOIDmode,
7860 gen_rtx_MEM (BLKmode,
7861 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7862 gen_rtx_UNSPEC (BLKmode,
7863 gen_rtvec (1, reg),
7864 UNSPEC_PUSH_MULT));
7866 tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7867 gen_rtx_PLUS (SImode, stack_pointer_rtx,
7868 GEN_INT (-(count * 8 + 4))));
7869 RTX_FRAME_RELATED_P (tmp) = 1;
7870 XVECEXP (dwarf, 0, 0) = tmp;
7872 tmp = gen_rtx_SET (VOIDmode,
7873 gen_rtx_MEM (DFmode, stack_pointer_rtx),
7874 reg);
7875 RTX_FRAME_RELATED_P (tmp) = 1;
7876 XVECEXP (dwarf, 0, 1) = tmp;
7878 for (i = 1; i < count; i++)
7880 reg = gen_rtx_REG (DFmode, base_reg);
7881 base_reg += 2;
7882 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
7884 tmp = gen_rtx_SET (VOIDmode,
7885 gen_rtx_MEM (DFmode,
7886 gen_rtx_PLUS (SImode,
7887 stack_pointer_rtx,
7888 GEN_INT (i * 8))),
7889 reg);
7890 RTX_FRAME_RELATED_P (tmp) = 1;
7891 XVECEXP (dwarf, 0, i + 1) = tmp;
7894 par = emit_insn (par);
7895 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7896 REG_NOTES (par));
7897 RTX_FRAME_RELATED_P (par) = 1;
7899 return count * 8 + 4;
7903 /* Output a 'call' insn. */
7904 const char *
7905 output_call (rtx *operands)
7907 if (arm_arch5)
7908 abort (); /* Patterns should call blx <reg> directly. */
7910 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
7911 if (REGNO (operands[0]) == LR_REGNUM)
7913 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
7914 output_asm_insn ("mov%?\t%0, %|lr", operands);
7917 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7919 if (TARGET_INTERWORK || arm_arch4t)
7920 output_asm_insn ("bx%?\t%0", operands);
7921 else
7922 output_asm_insn ("mov%?\t%|pc, %0", operands);
7924 return "";
7927 /* Output a 'call' insn that is a reference in memory. */
7928 const char *
7929 output_call_mem (rtx *operands)
7931 if (TARGET_INTERWORK && !arm_arch5)
7933 output_asm_insn ("ldr%?\t%|ip, %0", operands);
7934 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7935 output_asm_insn ("bx%?\t%|ip", operands);
7937 else if (regno_use_in (LR_REGNUM, operands[0]))
7939 /* LR is used in the memory address. We load the address in the
7940 first instruction. It's safe to use IP as the target of the
7941 load since the call will kill it anyway. */
7942 output_asm_insn ("ldr%?\t%|ip, %0", operands);
7943 if (arm_arch5)
7944 output_asm_insn ("blx%?\t%|ip", operands);
7945 else
7947 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7948 if (arm_arch4t)
7949 output_asm_insn ("bx%?\t%|ip", operands);
7950 else
7951 output_asm_insn ("mov%?\t%|pc, %|ip", operands);
7954 else
7956 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7957 output_asm_insn ("ldr%?\t%|pc, %0", operands);
7960 return "";
7964 /* Output a move from arm registers to an fpa registers.
7965 OPERANDS[0] is an fpa register.
7966 OPERANDS[1] is the first registers of an arm register pair. */
7967 const char *
7968 output_mov_long_double_fpa_from_arm (rtx *operands)
7970 int arm_reg0 = REGNO (operands[1]);
7971 rtx ops[3];
7973 if (arm_reg0 == IP_REGNUM)
7974 abort ();
7976 ops[0] = gen_rtx_REG (SImode, arm_reg0);
7977 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7978 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
7980 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
7981 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
7983 return "";
7986 /* Output a move from an fpa register to arm registers.
7987 OPERANDS[0] is the first registers of an arm register pair.
7988 OPERANDS[1] is an fpa register. */
7989 const char *
7990 output_mov_long_double_arm_from_fpa (rtx *operands)
7992 int arm_reg0 = REGNO (operands[0]);
7993 rtx ops[3];
7995 if (arm_reg0 == IP_REGNUM)
7996 abort ();
7998 ops[0] = gen_rtx_REG (SImode, arm_reg0);
7999 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8000 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8002 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8003 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8004 return "";
8007 /* Output a move from arm registers to arm registers of a long double
8008 OPERANDS[0] is the destination.
8009 OPERANDS[1] is the source. */
8010 const char *
8011 output_mov_long_double_arm_from_arm (rtx *operands)
8013 /* We have to be careful here because the two might overlap. */
8014 int dest_start = REGNO (operands[0]);
8015 int src_start = REGNO (operands[1]);
8016 rtx ops[2];
8017 int i;
8019 if (dest_start < src_start)
8021 for (i = 0; i < 3; i++)
8023 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8024 ops[1] = gen_rtx_REG (SImode, src_start + i);
8025 output_asm_insn ("mov%?\t%0, %1", ops);
8028 else
8030 for (i = 2; i >= 0; i--)
8032 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8033 ops[1] = gen_rtx_REG (SImode, src_start + i);
8034 output_asm_insn ("mov%?\t%0, %1", ops);
8038 return "";
8042 /* Output a move from arm registers to an fpa registers.
8043 OPERANDS[0] is an fpa register.
8044 OPERANDS[1] is the first registers of an arm register pair. */
8045 const char *
8046 output_mov_double_fpa_from_arm (rtx *operands)
8048 int arm_reg0 = REGNO (operands[1]);
8049 rtx ops[2];
8051 if (arm_reg0 == IP_REGNUM)
8052 abort ();
8054 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8055 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8056 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8057 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8058 return "";
8061 /* Output a move from an fpa register to arm registers.
8062 OPERANDS[0] is the first registers of an arm register pair.
8063 OPERANDS[1] is an fpa register. */
8064 const char *
8065 output_mov_double_arm_from_fpa (rtx *operands)
8067 int arm_reg0 = REGNO (operands[0]);
8068 rtx ops[2];
8070 if (arm_reg0 == IP_REGNUM)
8071 abort ();
8073 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8074 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8075 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8076 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8077 return "";
8080 /* Output a move between double words.
8081 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8082 or MEM<-REG and all MEMs must be offsettable addresses. */
8083 const char *
8084 output_move_double (rtx *operands)
8086 enum rtx_code code0 = GET_CODE (operands[0]);
8087 enum rtx_code code1 = GET_CODE (operands[1]);
8088 rtx otherops[3];
8090 if (code0 == REG)
8092 int reg0 = REGNO (operands[0]);
8094 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8096 if (code1 == MEM)
8098 switch (GET_CODE (XEXP (operands[1], 0)))
8100 case REG:
8101 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8102 break;
8104 case PRE_INC:
8105 if (!TARGET_LDRD)
8106 abort (); /* Should never happen now. */
8107 output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8108 break;
8110 case PRE_DEC:
8111 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8112 break;
8114 case POST_INC:
8115 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8116 break;
8118 case POST_DEC:
8119 if (!TARGET_LDRD)
8120 abort (); /* Should never happen now. */
8121 output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8122 break;
8124 case PRE_MODIFY:
8125 case POST_MODIFY:
8126 otherops[0] = operands[0];
8127 otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8128 otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8130 if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8132 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8134 /* Registers overlap so split out the increment. */
8135 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8136 output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8138 else
8139 output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8141 else
8143 /* We only allow constant increments, so this is safe. */
8144 output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8146 break;
8148 case LABEL_REF:
8149 case CONST:
8150 output_asm_insn ("adr%?\t%0, %1", operands);
8151 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8152 break;
8154 default:
8155 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8156 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8158 otherops[0] = operands[0];
8159 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8160 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8162 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8164 if (GET_CODE (otherops[2]) == CONST_INT)
8166 switch ((int) INTVAL (otherops[2]))
8168 case -8:
8169 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8170 return "";
8171 case -4:
8172 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8173 return "";
8174 case 4:
8175 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8176 return "";
8179 if (TARGET_LDRD
8180 && (GET_CODE (otherops[2]) == REG
8181 || (GET_CODE (otherops[2]) == CONST_INT
8182 && INTVAL (otherops[2]) > -256
8183 && INTVAL (otherops[2]) < 256)))
8185 if (reg_overlap_mentioned_p (otherops[0],
8186 otherops[2]))
8188 /* Swap base and index registers over to
8189 avoid a conflict. */
8190 otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8191 otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8194 /* If both registers conflict, it will usually
8195 have been fixed by a splitter. */
8196 if (reg_overlap_mentioned_p (otherops[0],
8197 otherops[2]))
8199 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8200 output_asm_insn ("ldr%?d\t%0, [%1]",
8201 otherops);
8202 return "";
8204 else
8206 output_asm_insn ("ldr%?d\t%0, [%1, %2]",
8207 otherops);
8208 return "";
8211 if (GET_CODE (otherops[2]) == CONST_INT)
8213 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8214 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8215 else
8216 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8218 else
8219 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8221 else
8222 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8224 return "ldm%?ia\t%0, %M0";
8226 else
8228 otherops[1] = adjust_address (operands[1], SImode, 4);
8229 /* Take care of overlapping base/data reg. */
8230 if (reg_mentioned_p (operands[0], operands[1]))
8232 output_asm_insn ("ldr%?\t%0, %1", otherops);
8233 output_asm_insn ("ldr%?\t%0, %1", operands);
8235 else
8237 output_asm_insn ("ldr%?\t%0, %1", operands);
8238 output_asm_insn ("ldr%?\t%0, %1", otherops);
8243 else
8244 abort (); /* Constraints should prevent this. */
8246 else if (code0 == MEM && code1 == REG)
8248 if (REGNO (operands[1]) == IP_REGNUM)
8249 abort ();
8251 switch (GET_CODE (XEXP (operands[0], 0)))
8253 case REG:
8254 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8255 break;
8257 case PRE_INC:
8258 if (!TARGET_LDRD)
8259 abort (); /* Should never happen now. */
8260 output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8261 break;
8263 case PRE_DEC:
8264 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8265 break;
8267 case POST_INC:
8268 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8269 break;
8271 case POST_DEC:
8272 if (!TARGET_LDRD)
8273 abort (); /* Should never happen now. */
8274 output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8275 break;
8277 case PRE_MODIFY:
8278 case POST_MODIFY:
8279 otherops[0] = operands[1];
8280 otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8281 otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8283 if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8284 output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8285 else
8286 output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8287 break;
8289 case PLUS:
8290 otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8291 if (GET_CODE (otherops[2]) == CONST_INT)
8293 switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8295 case -8:
8296 output_asm_insn ("stm%?db\t%m0, %M1", operands);
8297 return "";
8299 case -4:
8300 output_asm_insn ("stm%?da\t%m0, %M1", operands);
8301 return "";
8303 case 4:
8304 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8305 return "";
8308 if (TARGET_LDRD
8309 && (GET_CODE (otherops[2]) == REG
8310 || (GET_CODE (otherops[2]) == CONST_INT
8311 && INTVAL (otherops[2]) > -256
8312 && INTVAL (otherops[2]) < 256)))
8314 otherops[0] = operands[1];
8315 otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8316 output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8317 return "";
8319 /* Fall through */
8321 default:
8322 otherops[0] = adjust_address (operands[0], SImode, 4);
8323 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8324 output_asm_insn ("str%?\t%1, %0", operands);
8325 output_asm_insn ("str%?\t%1, %0", otherops);
8328 else
8329 /* Constraints should prevent this. */
8330 abort ();
8332 return "";
8335 /* Output an ADD r, s, #n where n may be too big for one instruction.
8336 If adding zero to one register, output nothing. */
8337 const char *
8338 output_add_immediate (rtx *operands)
8340 HOST_WIDE_INT n = INTVAL (operands[2]);
8342 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8344 if (n < 0)
8345 output_multi_immediate (operands,
8346 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8347 -n);
8348 else
8349 output_multi_immediate (operands,
8350 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8354 return "";
8357 /* Output a multiple immediate operation.
8358 OPERANDS is the vector of operands referred to in the output patterns.
8359 INSTR1 is the output pattern to use for the first constant.
8360 INSTR2 is the output pattern to use for subsequent constants.
8361 IMMED_OP is the index of the constant slot in OPERANDS.
8362 N is the constant value. */
8363 static const char *
8364 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8365 int immed_op, HOST_WIDE_INT n)
8367 #if HOST_BITS_PER_WIDE_INT > 32
8368 n &= 0xffffffff;
8369 #endif
8371 if (n == 0)
8373 /* Quick and easy output. */
8374 operands[immed_op] = const0_rtx;
8375 output_asm_insn (instr1, operands);
8377 else
8379 int i;
8380 const char * instr = instr1;
8382 /* Note that n is never zero here (which would give no output). */
8383 for (i = 0; i < 32; i += 2)
8385 if (n & (3 << i))
8387 operands[immed_op] = GEN_INT (n & (255 << i));
8388 output_asm_insn (instr, operands);
8389 instr = instr2;
8390 i += 6;
8395 return "";
8398 /* Return the appropriate ARM instruction for the operation code.
8399 The returned result should not be overwritten. OP is the rtx of the
8400 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8401 was shifted. */
8402 const char *
8403 arithmetic_instr (rtx op, int shift_first_arg)
8405 switch (GET_CODE (op))
8407 case PLUS:
8408 return "add";
8410 case MINUS:
8411 return shift_first_arg ? "rsb" : "sub";
8413 case IOR:
8414 return "orr";
8416 case XOR:
8417 return "eor";
8419 case AND:
8420 return "and";
8422 default:
8423 abort ();
8427 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8428 for the operation code. The returned result should not be overwritten.
8429 OP is the rtx code of the shift.
8430 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8431 shift. */
8432 static const char *
8433 shift_op (rtx op, HOST_WIDE_INT *amountp)
8435 const char * mnem;
8436 enum rtx_code code = GET_CODE (op);
8438 if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
8439 *amountp = -1;
8440 else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
8441 *amountp = INTVAL (XEXP (op, 1));
8442 else
8443 abort ();
8445 switch (code)
8447 case ASHIFT:
8448 mnem = "asl";
8449 break;
8451 case ASHIFTRT:
8452 mnem = "asr";
8453 break;
8455 case LSHIFTRT:
8456 mnem = "lsr";
8457 break;
8459 case ROTATE:
8460 if (*amountp == -1)
8461 abort ();
8462 *amountp = 32 - *amountp;
8464 /* Fall through. */
8466 case ROTATERT:
8467 mnem = "ror";
8468 break;
8470 case MULT:
8471 /* We never have to worry about the amount being other than a
8472 power of 2, since this case can never be reloaded from a reg. */
8473 if (*amountp != -1)
8474 *amountp = int_log2 (*amountp);
8475 else
8476 abort ();
8477 return "asl";
8479 default:
8480 abort ();
8483 if (*amountp != -1)
8485 /* This is not 100% correct, but follows from the desire to merge
8486 multiplication by a power of 2 with the recognizer for a
8487 shift. >=32 is not a valid shift for "asl", so we must try and
8488 output a shift that produces the correct arithmetical result.
8489 Using lsr #32 is identical except for the fact that the carry bit
8490 is not set correctly if we set the flags; but we never use the
8491 carry bit from such an operation, so we can ignore that. */
8492 if (code == ROTATERT)
8493 /* Rotate is just modulo 32. */
8494 *amountp &= 31;
8495 else if (*amountp != (*amountp & 31))
8497 if (code == ASHIFT)
8498 mnem = "lsr";
8499 *amountp = 32;
8502 /* Shifts of 0 are no-ops. */
8503 if (*amountp == 0)
8504 return NULL;
8507 return mnem;
8510 /* Obtain the shift from the POWER of two. */
8512 static HOST_WIDE_INT
8513 int_log2 (HOST_WIDE_INT power)
8515 HOST_WIDE_INT shift = 0;
8517 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8519 if (shift > 31)
8520 abort ();
8521 shift++;
8524 return shift;
8527 /* Output a .ascii pseudo-op, keeping track of lengths. This is
8528 because /bin/as is horribly restrictive. The judgement about
8529 whether or not each character is 'printable' (and can be output as
8530 is) or not (and must be printed with an octal escape) must be made
8531 with reference to the *host* character set -- the situation is
8532 similar to that discussed in the comments above pp_c_char in
8533 c-pretty-print.c. */
8535 #define MAX_ASCII_LEN 51
8537 void
8538 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8540 int i;
8541 int len_so_far = 0;
8543 fputs ("\t.ascii\t\"", stream);
8545 for (i = 0; i < len; i++)
8547 int c = p[i];
8549 if (len_so_far >= MAX_ASCII_LEN)
8551 fputs ("\"\n\t.ascii\t\"", stream);
8552 len_so_far = 0;
8555 if (ISPRINT (c))
8557 if (c == '\\' || c == '\"')
8559 putc ('\\', stream);
8560 len_so_far++;
8562 putc (c, stream);
8563 len_so_far++;
8565 else
8567 fprintf (stream, "\\%03o", c);
8568 len_so_far += 4;
8572 fputs ("\"\n", stream);
8575 /* Compute the register save mask for registers 0 through 12
8576 inclusive. This code is used by arm_compute_save_reg_mask. */
8578 static unsigned long
8579 arm_compute_save_reg0_reg12_mask (void)
8581 unsigned long func_type = arm_current_func_type ();
8582 unsigned long save_reg_mask = 0;
8583 unsigned int reg;
8585 if (IS_INTERRUPT (func_type))
8587 unsigned int max_reg;
8588 /* Interrupt functions must not corrupt any registers,
8589 even call clobbered ones. If this is a leaf function
8590 we can just examine the registers used by the RTL, but
8591 otherwise we have to assume that whatever function is
8592 called might clobber anything, and so we have to save
8593 all the call-clobbered registers as well. */
8594 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8595 /* FIQ handlers have registers r8 - r12 banked, so
8596 we only need to check r0 - r7, Normal ISRs only
8597 bank r14 and r15, so we must check up to r12.
8598 r13 is the stack pointer which is always preserved,
8599 so we do not need to consider it here. */
8600 max_reg = 7;
8601 else
8602 max_reg = 12;
8604 for (reg = 0; reg <= max_reg; reg++)
8605 if (regs_ever_live[reg]
8606 || (! current_function_is_leaf && call_used_regs [reg]))
8607 save_reg_mask |= (1 << reg);
8609 /* Also save the pic base register if necessary. */
8610 if (flag_pic
8611 && !TARGET_SINGLE_PIC_BASE
8612 && current_function_uses_pic_offset_table)
8613 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8615 else
8617 /* In the normal case we only need to save those registers
8618 which are call saved and which are used by this function. */
8619 for (reg = 0; reg <= 10; reg++)
8620 if (regs_ever_live[reg] && ! call_used_regs [reg])
8621 save_reg_mask |= (1 << reg);
8623 /* Handle the frame pointer as a special case. */
8624 if (! TARGET_APCS_FRAME
8625 && ! frame_pointer_needed
8626 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8627 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8628 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8630 /* If we aren't loading the PIC register,
8631 don't stack it even though it may be live. */
8632 if (flag_pic
8633 && !TARGET_SINGLE_PIC_BASE
8634 && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
8635 || current_function_uses_pic_offset_table))
8636 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8639 /* Save registers so the exception handler can modify them. */
8640 if (current_function_calls_eh_return)
8642 unsigned int i;
8644 for (i = 0; ; i++)
8646 reg = EH_RETURN_DATA_REGNO (i);
8647 if (reg == INVALID_REGNUM)
8648 break;
8649 save_reg_mask |= 1 << reg;
8653 return save_reg_mask;
8656 /* Compute a bit mask of which registers need to be
8657 saved on the stack for the current function. */
8659 static unsigned long
8660 arm_compute_save_reg_mask (void)
8662 unsigned int save_reg_mask = 0;
8663 unsigned long func_type = arm_current_func_type ();
8665 if (IS_NAKED (func_type))
8666 /* This should never really happen. */
8667 return 0;
8669 /* If we are creating a stack frame, then we must save the frame pointer,
8670 IP (which will hold the old stack pointer), LR and the PC. */
8671 if (frame_pointer_needed)
8672 save_reg_mask |=
8673 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8674 | (1 << IP_REGNUM)
8675 | (1 << LR_REGNUM)
8676 | (1 << PC_REGNUM);
8678 /* Volatile functions do not return, so there
8679 is no need to save any other registers. */
8680 if (IS_VOLATILE (func_type))
8681 return save_reg_mask;
8683 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8685 /* Decide if we need to save the link register.
8686 Interrupt routines have their own banked link register,
8687 so they never need to save it.
8688 Otherwise if we do not use the link register we do not need to save
8689 it. If we are pushing other registers onto the stack however, we
8690 can save an instruction in the epilogue by pushing the link register
8691 now and then popping it back into the PC. This incurs extra memory
8692 accesses though, so we only do it when optimizing for size, and only
8693 if we know that we will not need a fancy return sequence. */
8694 if (regs_ever_live [LR_REGNUM]
8695 || (save_reg_mask
8696 && optimize_size
8697 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
8698 && !current_function_calls_eh_return))
8699 save_reg_mask |= 1 << LR_REGNUM;
8701 if (cfun->machine->lr_save_eliminated)
8702 save_reg_mask &= ~ (1 << LR_REGNUM);
8704 if (TARGET_REALLY_IWMMXT
8705 && ((bit_count (save_reg_mask)
8706 + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8708 unsigned int reg;
8710 /* The total number of registers that are going to be pushed
8711 onto the stack is odd. We need to ensure that the stack
8712 is 64-bit aligned before we start to save iWMMXt registers,
8713 and also before we start to create locals. (A local variable
8714 might be a double or long long which we will load/store using
8715 an iWMMXt instruction). Therefore we need to push another
8716 ARM register, so that the stack will be 64-bit aligned. We
8717 try to avoid using the arg registers (r0 -r3) as they might be
8718 used to pass values in a tail call. */
8719 for (reg = 4; reg <= 12; reg++)
8720 if ((save_reg_mask & (1 << reg)) == 0)
8721 break;
8723 if (reg <= 12)
8724 save_reg_mask |= (1 << reg);
8725 else
8727 cfun->machine->sibcall_blocked = 1;
8728 save_reg_mask |= (1 << 3);
8732 return save_reg_mask;
8736 /* Compute a bit mask of which registers need to be
8737 saved on the stack for the current function. */
8738 static unsigned long
8739 thumb_compute_save_reg_mask (void)
8741 unsigned long mask;
8742 unsigned reg;
8744 mask = 0;
8745 for (reg = 0; reg < 12; reg ++)
8746 if (regs_ever_live[reg] && !call_used_regs[reg])
8747 mask |= 1 << reg;
8749 if (flag_pic && !TARGET_SINGLE_PIC_BASE)
8750 mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
8752 if (TARGET_SINGLE_PIC_BASE)
8753 mask &= ~(1 << arm_pic_register);
8755 /* See if we might need r11 for calls to _interwork_r11_call_via_rN(). */
8756 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
8757 mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
8759 /* LR will also be pushed if any lo regs are pushed. */
8760 if (mask & 0xff || thumb_force_lr_save ())
8761 mask |= (1 << LR_REGNUM);
8763 /* Make sure we have a low work register if we need one.
8764 We will need one if we are going to push a high register,
8765 but we are not currently intending to push a low register. */
8766 if ((mask & 0xff) == 0
8767 && ((mask & 0x0f00) || TARGET_BACKTRACE))
8769 /* Use thumb_find_work_register to choose which register
8770 we will use. If the register is live then we will
8771 have to push it. Use LAST_LO_REGNUM as our fallback
8772 choice for the register to select. */
8773 reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
8775 if (! call_used_regs[reg])
8776 mask |= 1 << reg;
8779 return mask;
8783 /* Return the number of bytes required to save VFP registers. */
8784 static int
8785 arm_get_vfp_saved_size (void)
8787 unsigned int regno;
8788 int count;
8789 int saved;
8791 saved = 0;
8792 /* Space for saved VFP registers. */
8793 if (TARGET_HARD_FLOAT && TARGET_VFP)
8795 count = 0;
8796 for (regno = FIRST_VFP_REGNUM;
8797 regno < LAST_VFP_REGNUM;
8798 regno += 2)
8800 if ((!regs_ever_live[regno] || call_used_regs[regno])
8801 && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8803 if (count > 0)
8805 /* Workaround ARM10 VFPr1 bug. */
8806 if (count == 2 && !arm_arch6)
8807 count++;
8808 saved += count * 8 + 4;
8810 count = 0;
8812 else
8813 count++;
8815 if (count > 0)
8817 if (count == 2 && !arm_arch6)
8818 count++;
8819 saved += count * 8 + 4;
8822 return saved;
8826 /* Generate a function exit sequence. If REALLY_RETURN is false, then do
8827 everything bar the final return instruction. */
8828 const char *
8829 output_return_instruction (rtx operand, int really_return, int reverse)
8831 char conditional[10];
8832 char instr[100];
8833 unsigned reg;
8834 unsigned long live_regs_mask;
8835 unsigned long func_type;
8836 arm_stack_offsets *offsets;
8838 func_type = arm_current_func_type ();
8840 if (IS_NAKED (func_type))
8841 return "";
8843 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
8845 /* If this function was declared non-returning, and we have
8846 found a tail call, then we have to trust that the called
8847 function won't return. */
8848 if (really_return)
8850 rtx ops[2];
8852 /* Otherwise, trap an attempted return by aborting. */
8853 ops[0] = operand;
8854 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
8855 : "abort");
8856 assemble_external_libcall (ops[1]);
8857 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
8860 return "";
8863 if (current_function_calls_alloca && !really_return)
8864 abort ();
8866 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
8868 return_used_this_function = 1;
8870 live_regs_mask = arm_compute_save_reg_mask ();
8872 if (live_regs_mask)
8874 const char * return_reg;
8876 /* If we do not have any special requirements for function exit
8877 (e.g. interworking, or ISR) then we can load the return address
8878 directly into the PC. Otherwise we must load it into LR. */
8879 if (really_return
8880 && ! TARGET_INTERWORK)
8881 return_reg = reg_names[PC_REGNUM];
8882 else
8883 return_reg = reg_names[LR_REGNUM];
8885 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
8887 /* There are three possible reasons for the IP register
8888 being saved. 1) a stack frame was created, in which case
8889 IP contains the old stack pointer, or 2) an ISR routine
8890 corrupted it, or 3) it was saved to align the stack on
8891 iWMMXt. In case 1, restore IP into SP, otherwise just
8892 restore IP. */
8893 if (frame_pointer_needed)
8895 live_regs_mask &= ~ (1 << IP_REGNUM);
8896 live_regs_mask |= (1 << SP_REGNUM);
8898 else
8900 if (! IS_INTERRUPT (func_type)
8901 && ! TARGET_REALLY_IWMMXT)
8902 abort ();
8906 /* On some ARM architectures it is faster to use LDR rather than
8907 LDM to load a single register. On other architectures, the
8908 cost is the same. In 26 bit mode, or for exception handlers,
8909 we have to use LDM to load the PC so that the CPSR is also
8910 restored. */
8911 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
8912 if (live_regs_mask == (1U << reg))
8913 break;
8915 if (reg <= LAST_ARM_REGNUM
8916 && (reg != LR_REGNUM
8917 || ! really_return
8918 || ! IS_INTERRUPT (func_type)))
8920 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
8921 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
8923 else
8925 char *p;
8926 int first = 1;
8928 /* Generate the load multiple instruction to restore the
8929 registers. Note we can get here, even if
8930 frame_pointer_needed is true, but only if sp already
8931 points to the base of the saved core registers. */
8932 if (live_regs_mask & (1 << SP_REGNUM))
8934 unsigned HOST_WIDE_INT stack_adjust;
8936 offsets = arm_get_frame_offsets ();
8937 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
8938 if (stack_adjust != 0 && stack_adjust != 4)
8939 abort ();
8941 if (stack_adjust && arm_arch5)
8942 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
8943 else
8945 /* If we can't use ldmib (SA110 bug),
8946 then try to pop r3 instead. */
8947 if (stack_adjust)
8948 live_regs_mask |= 1 << 3;
8949 sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
8952 else
8953 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
8955 p = instr + strlen (instr);
8957 for (reg = 0; reg <= SP_REGNUM; reg++)
8958 if (live_regs_mask & (1 << reg))
8960 int l = strlen (reg_names[reg]);
8962 if (first)
8963 first = 0;
8964 else
8966 memcpy (p, ", ", 2);
8967 p += 2;
8970 memcpy (p, "%|", 2);
8971 memcpy (p + 2, reg_names[reg], l);
8972 p += l + 2;
8975 if (live_regs_mask & (1 << LR_REGNUM))
8977 sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
8978 /* If returning from an interrupt, restore the CPSR. */
8979 if (IS_INTERRUPT (func_type))
8980 strcat (p, "^");
8982 else
8983 strcpy (p, "}");
8986 output_asm_insn (instr, & operand);
8988 /* See if we need to generate an extra instruction to
8989 perform the actual function return. */
8990 if (really_return
8991 && func_type != ARM_FT_INTERWORKED
8992 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
8994 /* The return has already been handled
8995 by loading the LR into the PC. */
8996 really_return = 0;
9000 if (really_return)
9002 switch ((int) ARM_FUNC_TYPE (func_type))
9004 case ARM_FT_ISR:
9005 case ARM_FT_FIQ:
9006 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9007 break;
9009 case ARM_FT_INTERWORKED:
9010 sprintf (instr, "bx%s\t%%|lr", conditional);
9011 break;
9013 case ARM_FT_EXCEPTION:
9014 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9015 break;
9017 default:
9018 /* Use bx if it's available. */
9019 if (arm_arch5 || arm_arch4t)
9020 sprintf (instr, "bx%s\t%%|lr", conditional);
9021 else
9022 sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9023 break;
9026 output_asm_insn (instr, & operand);
9029 return "";
9032 /* Write the function name into the code section, directly preceding
9033 the function prologue.
9035 Code will be output similar to this:
9037 .ascii "arm_poke_function_name", 0
9038 .align
9040 .word 0xff000000 + (t1 - t0)
9041 arm_poke_function_name
9042 mov ip, sp
9043 stmfd sp!, {fp, ip, lr, pc}
9044 sub fp, ip, #4
9046 When performing a stack backtrace, code can inspect the value
9047 of 'pc' stored at 'fp' + 0. If the trace function then looks
9048 at location pc - 12 and the top 8 bits are set, then we know
9049 that there is a function name embedded immediately preceding this
9050 location and has length ((pc[-3]) & 0xff000000).
9052 We assume that pc is declared as a pointer to an unsigned long.
9054 It is of no benefit to output the function name if we are assembling
9055 a leaf function. These function types will not contain a stack
9056 backtrace structure, therefore it is not possible to determine the
9057 function name. */
9058 void
9059 arm_poke_function_name (FILE *stream, const char *name)
9061 unsigned long alignlength;
9062 unsigned long length;
9063 rtx x;
9065 length = strlen (name) + 1;
9066 alignlength = ROUND_UP_WORD (length);
9068 ASM_OUTPUT_ASCII (stream, name, length);
9069 ASM_OUTPUT_ALIGN (stream, 2);
9070 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9071 assemble_aligned_integer (UNITS_PER_WORD, x);
9074 /* Place some comments into the assembler stream
9075 describing the current function. */
9076 static void
9077 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9079 unsigned long func_type;
9081 if (!TARGET_ARM)
9083 thumb_output_function_prologue (f, frame_size);
9084 return;
9087 /* Sanity check. */
9088 if (arm_ccfsm_state || arm_target_insn)
9089 abort ();
9091 func_type = arm_current_func_type ();
9093 switch ((int) ARM_FUNC_TYPE (func_type))
9095 default:
9096 case ARM_FT_NORMAL:
9097 break;
9098 case ARM_FT_INTERWORKED:
9099 asm_fprintf (f, "\t%@ Function supports interworking.\n");
9100 break;
9101 case ARM_FT_ISR:
9102 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9103 break;
9104 case ARM_FT_FIQ:
9105 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9106 break;
9107 case ARM_FT_EXCEPTION:
9108 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9109 break;
9112 if (IS_NAKED (func_type))
9113 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9115 if (IS_VOLATILE (func_type))
9116 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9118 if (IS_NESTED (func_type))
9119 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9121 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9122 current_function_args_size,
9123 current_function_pretend_args_size, frame_size);
9125 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9126 frame_pointer_needed,
9127 cfun->machine->uses_anonymous_args);
9129 if (cfun->machine->lr_save_eliminated)
9130 asm_fprintf (f, "\t%@ link register save eliminated.\n");
9132 if (current_function_calls_eh_return)
9133 asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9135 #ifdef AOF_ASSEMBLER
9136 if (flag_pic)
9137 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9138 #endif
9140 return_used_this_function = 0;
9143 const char *
9144 arm_output_epilogue (rtx sibling)
9146 int reg;
9147 unsigned long saved_regs_mask;
9148 unsigned long func_type;
9149 /* Floats_offset is the offset from the "virtual" frame. In an APCS
9150 frame that is $fp + 4 for a non-variadic function. */
9151 int floats_offset = 0;
9152 rtx operands[3];
9153 FILE * f = asm_out_file;
9154 unsigned int lrm_count = 0;
9155 int really_return = (sibling == NULL);
9156 int start_reg;
9157 arm_stack_offsets *offsets;
9159 /* If we have already generated the return instruction
9160 then it is futile to generate anything else. */
9161 if (use_return_insn (FALSE, sibling) && return_used_this_function)
9162 return "";
9164 func_type = arm_current_func_type ();
9166 if (IS_NAKED (func_type))
9167 /* Naked functions don't have epilogues. */
9168 return "";
9170 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9172 rtx op;
9174 /* A volatile function should never return. Call abort. */
9175 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9176 assemble_external_libcall (op);
9177 output_asm_insn ("bl\t%a0", &op);
9179 return "";
9182 if (current_function_calls_eh_return
9183 && ! really_return)
9184 /* If we are throwing an exception, then we really must
9185 be doing a return, so we can't tail-call. */
9186 abort ();
9188 offsets = arm_get_frame_offsets ();
9189 saved_regs_mask = arm_compute_save_reg_mask ();
9191 if (TARGET_IWMMXT)
9192 lrm_count = bit_count (saved_regs_mask);
9194 floats_offset = offsets->saved_args;
9195 /* Compute how far away the floats will be. */
9196 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9197 if (saved_regs_mask & (1 << reg))
9198 floats_offset += 4;
9200 if (frame_pointer_needed)
9202 /* This variable is for the Virtual Frame Pointer, not VFP regs. */
9203 int vfp_offset = offsets->frame;
9205 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9207 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9208 if (regs_ever_live[reg] && !call_used_regs[reg])
9210 floats_offset += 12;
9211 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9212 reg, FP_REGNUM, floats_offset - vfp_offset);
9215 else
9217 start_reg = LAST_FPA_REGNUM;
9219 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9221 if (regs_ever_live[reg] && !call_used_regs[reg])
9223 floats_offset += 12;
9225 /* We can't unstack more than four registers at once. */
9226 if (start_reg - reg == 3)
9228 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9229 reg, FP_REGNUM, floats_offset - vfp_offset);
9230 start_reg = reg - 1;
9233 else
9235 if (reg != start_reg)
9236 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9237 reg + 1, start_reg - reg,
9238 FP_REGNUM, floats_offset - vfp_offset);
9239 start_reg = reg - 1;
9243 /* Just in case the last register checked also needs unstacking. */
9244 if (reg != start_reg)
9245 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9246 reg + 1, start_reg - reg,
9247 FP_REGNUM, floats_offset - vfp_offset);
9250 if (TARGET_HARD_FLOAT && TARGET_VFP)
9252 int saved_size;
9254 /* The fldmx insn does not have base+offset addressing modes,
9255 so we use IP to hold the address. */
9256 saved_size = arm_get_vfp_saved_size ();
9258 if (saved_size > 0)
9260 floats_offset += saved_size;
9261 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9262 FP_REGNUM, floats_offset - vfp_offset);
9264 start_reg = FIRST_VFP_REGNUM;
9265 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9267 if ((!regs_ever_live[reg] || call_used_regs[reg])
9268 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9270 if (start_reg != reg)
9271 arm_output_fldmx (f, IP_REGNUM,
9272 (start_reg - FIRST_VFP_REGNUM) / 2,
9273 (reg - start_reg) / 2);
9274 start_reg = reg + 2;
9277 if (start_reg != reg)
9278 arm_output_fldmx (f, IP_REGNUM,
9279 (start_reg - FIRST_VFP_REGNUM) / 2,
9280 (reg - start_reg) / 2);
9283 if (TARGET_IWMMXT)
9285 /* The frame pointer is guaranteed to be non-double-word aligned.
9286 This is because it is set to (old_stack_pointer - 4) and the
9287 old_stack_pointer was double word aligned. Thus the offset to
9288 the iWMMXt registers to be loaded must also be non-double-word
9289 sized, so that the resultant address *is* double-word aligned.
9290 We can ignore floats_offset since that was already included in
9291 the live_regs_mask. */
9292 lrm_count += (lrm_count % 2 ? 2 : 1);
9294 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9295 if (regs_ever_live[reg] && !call_used_regs[reg])
9297 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9298 reg, FP_REGNUM, lrm_count * 4);
9299 lrm_count += 2;
9303 /* saved_regs_mask should contain the IP, which at the time of stack
9304 frame generation actually contains the old stack pointer. So a
9305 quick way to unwind the stack is just pop the IP register directly
9306 into the stack pointer. */
9307 if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
9308 abort ();
9309 saved_regs_mask &= ~ (1 << IP_REGNUM);
9310 saved_regs_mask |= (1 << SP_REGNUM);
9312 /* There are two registers left in saved_regs_mask - LR and PC. We
9313 only need to restore the LR register (the return address), but to
9314 save time we can load it directly into the PC, unless we need a
9315 special function exit sequence, or we are not really returning. */
9316 if (really_return
9317 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9318 && !current_function_calls_eh_return)
9319 /* Delete the LR from the register mask, so that the LR on
9320 the stack is loaded into the PC in the register mask. */
9321 saved_regs_mask &= ~ (1 << LR_REGNUM);
9322 else
9323 saved_regs_mask &= ~ (1 << PC_REGNUM);
9325 /* We must use SP as the base register, because SP is one of the
9326 registers being restored. If an interrupt or page fault
9327 happens in the ldm instruction, the SP might or might not
9328 have been restored. That would be bad, as then SP will no
9329 longer indicate the safe area of stack, and we can get stack
9330 corruption. Using SP as the base register means that it will
9331 be reset correctly to the original value, should an interrupt
9332 occur. If the stack pointer already points at the right
9333 place, then omit the subtraction. */
9334 if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9335 || current_function_calls_alloca)
9336 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9337 4 * bit_count (saved_regs_mask));
9338 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9340 if (IS_INTERRUPT (func_type))
9341 /* Interrupt handlers will have pushed the
9342 IP onto the stack, so restore it now. */
9343 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9345 else
9347 /* Restore stack pointer if necessary. */
9348 if (offsets->outgoing_args != offsets->saved_regs)
9350 operands[0] = operands[1] = stack_pointer_rtx;
9351 operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9352 output_add_immediate (operands);
9355 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9357 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9358 if (regs_ever_live[reg] && !call_used_regs[reg])
9359 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9360 reg, SP_REGNUM);
9362 else
9364 start_reg = FIRST_FPA_REGNUM;
9366 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9368 if (regs_ever_live[reg] && !call_used_regs[reg])
9370 if (reg - start_reg == 3)
9372 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9373 start_reg, SP_REGNUM);
9374 start_reg = reg + 1;
9377 else
9379 if (reg != start_reg)
9380 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9381 start_reg, reg - start_reg,
9382 SP_REGNUM);
9384 start_reg = reg + 1;
9388 /* Just in case the last register checked also needs unstacking. */
9389 if (reg != start_reg)
9390 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9391 start_reg, reg - start_reg, SP_REGNUM);
9394 if (TARGET_HARD_FLOAT && TARGET_VFP)
9396 start_reg = FIRST_VFP_REGNUM;
9397 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9399 if ((!regs_ever_live[reg] || call_used_regs[reg])
9400 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9402 if (start_reg != reg)
9403 arm_output_fldmx (f, SP_REGNUM,
9404 (start_reg - FIRST_VFP_REGNUM) / 2,
9405 (reg - start_reg) / 2);
9406 start_reg = reg + 2;
9409 if (start_reg != reg)
9410 arm_output_fldmx (f, SP_REGNUM,
9411 (start_reg - FIRST_VFP_REGNUM) / 2,
9412 (reg - start_reg) / 2);
9414 if (TARGET_IWMMXT)
9415 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9416 if (regs_ever_live[reg] && !call_used_regs[reg])
9417 asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9419 /* If we can, restore the LR into the PC. */
9420 if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9421 && really_return
9422 && current_function_pretend_args_size == 0
9423 && saved_regs_mask & (1 << LR_REGNUM)
9424 && !current_function_calls_eh_return)
9426 saved_regs_mask &= ~ (1 << LR_REGNUM);
9427 saved_regs_mask |= (1 << PC_REGNUM);
9430 /* Load the registers off the stack. If we only have one register
9431 to load use the LDR instruction - it is faster. */
9432 if (saved_regs_mask == (1 << LR_REGNUM))
9434 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9436 else if (saved_regs_mask)
9438 if (saved_regs_mask & (1 << SP_REGNUM))
9439 /* Note - write back to the stack register is not enabled
9440 (i.e. "ldmfd sp!..."). We know that the stack pointer is
9441 in the list of registers and if we add writeback the
9442 instruction becomes UNPREDICTABLE. */
9443 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9444 else
9445 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9448 if (current_function_pretend_args_size)
9450 /* Unwind the pre-pushed regs. */
9451 operands[0] = operands[1] = stack_pointer_rtx;
9452 operands[2] = GEN_INT (current_function_pretend_args_size);
9453 output_add_immediate (operands);
9457 /* We may have already restored PC directly from the stack. */
9458 if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
9459 return "";
9461 /* Stack adjustment for exception handler. */
9462 if (current_function_calls_eh_return)
9463 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
9464 ARM_EH_STACKADJ_REGNUM);
9466 /* Generate the return instruction. */
9467 switch ((int) ARM_FUNC_TYPE (func_type))
9469 case ARM_FT_ISR:
9470 case ARM_FT_FIQ:
9471 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9472 break;
9474 case ARM_FT_EXCEPTION:
9475 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9476 break;
9478 case ARM_FT_INTERWORKED:
9479 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9480 break;
9482 default:
9483 if (arm_arch5 || arm_arch4t)
9484 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9485 else
9486 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9487 break;
9490 return "";
9493 static void
9494 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9495 HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9497 arm_stack_offsets *offsets;
9499 if (TARGET_THUMB)
9501 int regno;
9503 /* Emit any call-via-reg trampolines that are needed for v4t support
9504 of call_reg and call_value_reg type insns. */
9505 for (regno = 0; regno < LR_REGNUM; regno++)
9507 rtx label = cfun->machine->call_via[regno];
9509 if (label != NULL)
9511 function_section (current_function_decl);
9512 targetm.asm_out.internal_label (asm_out_file, "L",
9513 CODE_LABEL_NUMBER (label));
9514 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
9518 /* ??? Probably not safe to set this here, since it assumes that a
9519 function will be emitted as assembly immediately after we generate
9520 RTL for it. This does not happen for inline functions. */
9521 return_used_this_function = 0;
9523 else
9525 /* We need to take into account any stack-frame rounding. */
9526 offsets = arm_get_frame_offsets ();
9528 if (use_return_insn (FALSE, NULL)
9529 && return_used_this_function
9530 && offsets->saved_regs != offsets->outgoing_args
9531 && !frame_pointer_needed)
9532 abort ();
9534 /* Reset the ARM-specific per-function variables. */
9535 after_arm_reorg = 0;
9539 /* Generate and emit an insn that we will recognize as a push_multi.
9540 Unfortunately, since this insn does not reflect very well the actual
9541 semantics of the operation, we need to annotate the insn for the benefit
9542 of DWARF2 frame unwind information. */
9543 static rtx
9544 emit_multi_reg_push (unsigned long mask)
9546 int num_regs = 0;
9547 int num_dwarf_regs;
9548 int i, j;
9549 rtx par;
9550 rtx dwarf;
9551 int dwarf_par_index;
9552 rtx tmp, reg;
9554 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9555 if (mask & (1 << i))
9556 num_regs++;
9558 if (num_regs == 0 || num_regs > 16)
9559 abort ();
9561 /* We don't record the PC in the dwarf frame information. */
9562 num_dwarf_regs = num_regs;
9563 if (mask & (1 << PC_REGNUM))
9564 num_dwarf_regs--;
9566 /* For the body of the insn we are going to generate an UNSPEC in
9567 parallel with several USEs. This allows the insn to be recognized
9568 by the push_multi pattern in the arm.md file. The insn looks
9569 something like this:
9571 (parallel [
9572 (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9573 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9574 (use (reg:SI 11 fp))
9575 (use (reg:SI 12 ip))
9576 (use (reg:SI 14 lr))
9577 (use (reg:SI 15 pc))
9580 For the frame note however, we try to be more explicit and actually
9581 show each register being stored into the stack frame, plus a (single)
9582 decrement of the stack pointer. We do it this way in order to be
9583 friendly to the stack unwinding code, which only wants to see a single
9584 stack decrement per instruction. The RTL we generate for the note looks
9585 something like this:
9587 (sequence [
9588 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9589 (set (mem:SI (reg:SI sp)) (reg:SI r4))
9590 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9591 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9592 (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9595 This sequence is used both by the code to support stack unwinding for
9596 exceptions handlers and the code to generate dwarf2 frame debugging. */
9598 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9599 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9600 dwarf_par_index = 1;
9602 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9604 if (mask & (1 << i))
9606 reg = gen_rtx_REG (SImode, i);
9608 XVECEXP (par, 0, 0)
9609 = gen_rtx_SET (VOIDmode,
9610 gen_rtx_MEM (BLKmode,
9611 gen_rtx_PRE_DEC (BLKmode,
9612 stack_pointer_rtx)),
9613 gen_rtx_UNSPEC (BLKmode,
9614 gen_rtvec (1, reg),
9615 UNSPEC_PUSH_MULT));
9617 if (i != PC_REGNUM)
9619 tmp = gen_rtx_SET (VOIDmode,
9620 gen_rtx_MEM (SImode, stack_pointer_rtx),
9621 reg);
9622 RTX_FRAME_RELATED_P (tmp) = 1;
9623 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9624 dwarf_par_index++;
9627 break;
9631 for (j = 1, i++; j < num_regs; i++)
9633 if (mask & (1 << i))
9635 reg = gen_rtx_REG (SImode, i);
9637 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9639 if (i != PC_REGNUM)
9641 tmp = gen_rtx_SET (VOIDmode,
9642 gen_rtx_MEM (SImode,
9643 plus_constant (stack_pointer_rtx,
9644 4 * j)),
9645 reg);
9646 RTX_FRAME_RELATED_P (tmp) = 1;
9647 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9650 j++;
9654 par = emit_insn (par);
9656 tmp = gen_rtx_SET (SImode,
9657 stack_pointer_rtx,
9658 gen_rtx_PLUS (SImode,
9659 stack_pointer_rtx,
9660 GEN_INT (-4 * num_regs)));
9661 RTX_FRAME_RELATED_P (tmp) = 1;
9662 XVECEXP (dwarf, 0, 0) = tmp;
9664 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9665 REG_NOTES (par));
9666 return par;
9669 static rtx
9670 emit_sfm (int base_reg, int count)
9672 rtx par;
9673 rtx dwarf;
9674 rtx tmp, reg;
9675 int i;
9677 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9678 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9680 reg = gen_rtx_REG (XFmode, base_reg++);
9682 XVECEXP (par, 0, 0)
9683 = gen_rtx_SET (VOIDmode,
9684 gen_rtx_MEM (BLKmode,
9685 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9686 gen_rtx_UNSPEC (BLKmode,
9687 gen_rtvec (1, reg),
9688 UNSPEC_PUSH_MULT));
9689 tmp = gen_rtx_SET (VOIDmode,
9690 gen_rtx_MEM (XFmode, stack_pointer_rtx), reg);
9691 RTX_FRAME_RELATED_P (tmp) = 1;
9692 XVECEXP (dwarf, 0, 1) = tmp;
9694 for (i = 1; i < count; i++)
9696 reg = gen_rtx_REG (XFmode, base_reg++);
9697 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9699 tmp = gen_rtx_SET (VOIDmode,
9700 gen_rtx_MEM (XFmode,
9701 plus_constant (stack_pointer_rtx,
9702 i * 12)),
9703 reg);
9704 RTX_FRAME_RELATED_P (tmp) = 1;
9705 XVECEXP (dwarf, 0, i + 1) = tmp;
9708 tmp = gen_rtx_SET (VOIDmode,
9709 stack_pointer_rtx,
9710 gen_rtx_PLUS (SImode,
9711 stack_pointer_rtx,
9712 GEN_INT (-12 * count)));
9713 RTX_FRAME_RELATED_P (tmp) = 1;
9714 XVECEXP (dwarf, 0, 0) = tmp;
9716 par = emit_insn (par);
9717 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9718 REG_NOTES (par));
9719 return par;
9723 /* Return true if the current function needs to save/restore LR. */
9725 static bool
9726 thumb_force_lr_save (void)
9728 return !cfun->machine->lr_save_eliminated
9729 && (!leaf_function_p ()
9730 || thumb_far_jump_used_p ()
9731 || regs_ever_live [LR_REGNUM]);
9735 /* Compute the distance from register FROM to register TO.
9736 These can be the arg pointer (26), the soft frame pointer (25),
9737 the stack pointer (13) or the hard frame pointer (11).
9738 In thumb mode r7 is used as the soft frame pointer, if needed.
9739 Typical stack layout looks like this:
9741 old stack pointer -> | |
9742 ----
9743 | | \
9744 | | saved arguments for
9745 | | vararg functions
9746 | | /
9748 hard FP & arg pointer -> | | \
9749 | | stack
9750 | | frame
9751 | | /
9753 | | \
9754 | | call saved
9755 | | registers
9756 soft frame pointer -> | | /
9758 | | \
9759 | | local
9760 | | variables
9761 | | /
9763 | | \
9764 | | outgoing
9765 | | arguments
9766 current stack pointer -> | | /
9769 For a given function some or all of these stack components
9770 may not be needed, giving rise to the possibility of
9771 eliminating some of the registers.
9773 The values returned by this function must reflect the behavior
9774 of arm_expand_prologue() and arm_compute_save_reg_mask().
9776 The sign of the number returned reflects the direction of stack
9777 growth, so the values are positive for all eliminations except
9778 from the soft frame pointer to the hard frame pointer.
9780 SFP may point just inside the local variables block to ensure correct
9781 alignment. */
9784 /* Calculate stack offsets. These are used to calculate register elimination
9785 offsets and in prologue/epilogue code. */
9787 static arm_stack_offsets *
9788 arm_get_frame_offsets (void)
9790 struct arm_stack_offsets *offsets;
9791 unsigned long func_type;
9792 int leaf;
9793 int saved;
9794 HOST_WIDE_INT frame_size;
9796 offsets = &cfun->machine->stack_offsets;
9798 /* We need to know if we are a leaf function. Unfortunately, it
9799 is possible to be called after start_sequence has been called,
9800 which causes get_insns to return the insns for the sequence,
9801 not the function, which will cause leaf_function_p to return
9802 the incorrect result.
9804 to know about leaf functions once reload has completed, and the
9805 frame size cannot be changed after that time, so we can safely
9806 use the cached value. */
9808 if (reload_completed)
9809 return offsets;
9811 /* Initially this is the size of the local variables. It will translated
9812 into an offset once we have determined the size of preceding data. */
9813 frame_size = ROUND_UP_WORD (get_frame_size ());
9815 leaf = leaf_function_p ();
9817 /* Space for variadic functions. */
9818 offsets->saved_args = current_function_pretend_args_size;
9820 offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
9822 if (TARGET_ARM)
9824 unsigned int regno;
9826 saved = bit_count (arm_compute_save_reg_mask ()) * 4;
9828 /* We know that SP will be doubleword aligned on entry, and we must
9829 preserve that condition at any subroutine call. We also require the
9830 soft frame pointer to be doubleword aligned. */
9832 if (TARGET_REALLY_IWMMXT)
9834 /* Check for the call-saved iWMMXt registers. */
9835 for (regno = FIRST_IWMMXT_REGNUM;
9836 regno <= LAST_IWMMXT_REGNUM;
9837 regno++)
9838 if (regs_ever_live [regno] && ! call_used_regs [regno])
9839 saved += 8;
9842 func_type = arm_current_func_type ();
9843 if (! IS_VOLATILE (func_type))
9845 /* Space for saved FPA registers. */
9846 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
9847 if (regs_ever_live[regno] && ! call_used_regs[regno])
9848 saved += 12;
9850 /* Space for saved VFP registers. */
9851 if (TARGET_HARD_FLOAT && TARGET_VFP)
9852 saved += arm_get_vfp_saved_size ();
9855 else /* TARGET_THUMB */
9857 saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
9858 if (TARGET_BACKTRACE)
9859 saved += 16;
9862 /* Saved registers include the stack frame. */
9863 offsets->saved_regs = offsets->saved_args + saved;
9864 offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
9865 /* A leaf function does not need any stack alignment if it has nothing
9866 on the stack. */
9867 if (leaf && frame_size == 0)
9869 offsets->outgoing_args = offsets->soft_frame;
9870 return offsets;
9873 /* Ensure SFP has the correct alignment. */
9874 if (ARM_DOUBLEWORD_ALIGN
9875 && (offsets->soft_frame & 7))
9876 offsets->soft_frame += 4;
9878 offsets->outgoing_args = offsets->soft_frame + frame_size
9879 + current_function_outgoing_args_size;
9881 if (ARM_DOUBLEWORD_ALIGN)
9883 /* Ensure SP remains doubleword aligned. */
9884 if (offsets->outgoing_args & 7)
9885 offsets->outgoing_args += 4;
9886 if (offsets->outgoing_args & 7)
9887 abort ();
9890 return offsets;
9894 /* Calculate the relative offsets for the different stack pointers. Positive
9895 offsets are in the direction of stack growth. */
9897 HOST_WIDE_INT
9898 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
9900 arm_stack_offsets *offsets;
9902 offsets = arm_get_frame_offsets ();
9904 /* OK, now we have enough information to compute the distances.
9905 There must be an entry in these switch tables for each pair
9906 of registers in ELIMINABLE_REGS, even if some of the entries
9907 seem to be redundant or useless. */
9908 switch (from)
9910 case ARG_POINTER_REGNUM:
9911 switch (to)
9913 case THUMB_HARD_FRAME_POINTER_REGNUM:
9914 return 0;
9916 case FRAME_POINTER_REGNUM:
9917 /* This is the reverse of the soft frame pointer
9918 to hard frame pointer elimination below. */
9919 return offsets->soft_frame - offsets->saved_args;
9921 case ARM_HARD_FRAME_POINTER_REGNUM:
9922 /* If there is no stack frame then the hard
9923 frame pointer and the arg pointer coincide. */
9924 if (offsets->frame == offsets->saved_regs)
9925 return 0;
9926 /* FIXME: Not sure about this. Maybe we should always return 0 ? */
9927 return (frame_pointer_needed
9928 && cfun->static_chain_decl != NULL
9929 && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
9931 case STACK_POINTER_REGNUM:
9932 /* If nothing has been pushed on the stack at all
9933 then this will return -4. This *is* correct! */
9934 return offsets->outgoing_args - (offsets->saved_args + 4);
9936 default:
9937 abort ();
9939 break;
9941 case FRAME_POINTER_REGNUM:
9942 switch (to)
9944 case THUMB_HARD_FRAME_POINTER_REGNUM:
9945 return 0;
9947 case ARM_HARD_FRAME_POINTER_REGNUM:
9948 /* The hard frame pointer points to the top entry in the
9949 stack frame. The soft frame pointer to the bottom entry
9950 in the stack frame. If there is no stack frame at all,
9951 then they are identical. */
9953 return offsets->frame - offsets->soft_frame;
9955 case STACK_POINTER_REGNUM:
9956 return offsets->outgoing_args - offsets->soft_frame;
9958 default:
9959 abort ();
9961 break;
9963 default:
9964 /* You cannot eliminate from the stack pointer.
9965 In theory you could eliminate from the hard frame
9966 pointer to the stack pointer, but this will never
9967 happen, since if a stack frame is not needed the
9968 hard frame pointer will never be used. */
9969 abort ();
9974 /* Generate the prologue instructions for entry into an ARM function. */
9975 void
9976 arm_expand_prologue (void)
9978 int reg;
9979 rtx amount;
9980 rtx insn;
9981 rtx ip_rtx;
9982 unsigned long live_regs_mask;
9983 unsigned long func_type;
9984 int fp_offset = 0;
9985 int saved_pretend_args = 0;
9986 int saved_regs = 0;
9987 unsigned HOST_WIDE_INT args_to_push;
9988 arm_stack_offsets *offsets;
9990 func_type = arm_current_func_type ();
9992 /* Naked functions don't have prologues. */
9993 if (IS_NAKED (func_type))
9994 return;
9996 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
9997 args_to_push = current_function_pretend_args_size;
9999 /* Compute which register we will have to save onto the stack. */
10000 live_regs_mask = arm_compute_save_reg_mask ();
10002 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10004 if (frame_pointer_needed)
10006 if (IS_INTERRUPT (func_type))
10008 /* Interrupt functions must not corrupt any registers.
10009 Creating a frame pointer however, corrupts the IP
10010 register, so we must push it first. */
10011 insn = emit_multi_reg_push (1 << IP_REGNUM);
10013 /* Do not set RTX_FRAME_RELATED_P on this insn.
10014 The dwarf stack unwinding code only wants to see one
10015 stack decrement per function, and this is not it. If
10016 this instruction is labeled as being part of the frame
10017 creation sequence then dwarf2out_frame_debug_expr will
10018 abort when it encounters the assignment of IP to FP
10019 later on, since the use of SP here establishes SP as
10020 the CFA register and not IP.
10022 Anyway this instruction is not really part of the stack
10023 frame creation although it is part of the prologue. */
10025 else if (IS_NESTED (func_type))
10027 /* The Static chain register is the same as the IP register
10028 used as a scratch register during stack frame creation.
10029 To get around this need to find somewhere to store IP
10030 whilst the frame is being created. We try the following
10031 places in order:
10033 1. The last argument register.
10034 2. A slot on the stack above the frame. (This only
10035 works if the function is not a varargs function).
10036 3. Register r3, after pushing the argument registers
10037 onto the stack.
10039 Note - we only need to tell the dwarf2 backend about the SP
10040 adjustment in the second variant; the static chain register
10041 doesn't need to be unwound, as it doesn't contain a value
10042 inherited from the caller. */
10044 if (regs_ever_live[3] == 0)
10046 insn = gen_rtx_REG (SImode, 3);
10047 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10048 insn = emit_insn (insn);
10050 else if (args_to_push == 0)
10052 rtx dwarf;
10053 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10054 insn = gen_rtx_MEM (SImode, insn);
10055 insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10056 insn = emit_insn (insn);
10058 fp_offset = 4;
10060 /* Just tell the dwarf backend that we adjusted SP. */
10061 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10062 gen_rtx_PLUS (SImode, stack_pointer_rtx,
10063 GEN_INT (-fp_offset)));
10064 RTX_FRAME_RELATED_P (insn) = 1;
10065 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10066 dwarf, REG_NOTES (insn));
10068 else
10070 /* Store the args on the stack. */
10071 if (cfun->machine->uses_anonymous_args)
10072 insn = emit_multi_reg_push
10073 ((0xf0 >> (args_to_push / 4)) & 0xf);
10074 else
10075 insn = emit_insn
10076 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10077 GEN_INT (- args_to_push)));
10079 RTX_FRAME_RELATED_P (insn) = 1;
10081 saved_pretend_args = 1;
10082 fp_offset = args_to_push;
10083 args_to_push = 0;
10085 /* Now reuse r3 to preserve IP. */
10086 insn = gen_rtx_REG (SImode, 3);
10087 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10088 (void) emit_insn (insn);
10092 if (fp_offset)
10094 insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10095 insn = gen_rtx_SET (SImode, ip_rtx, insn);
10097 else
10098 insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10100 insn = emit_insn (insn);
10101 RTX_FRAME_RELATED_P (insn) = 1;
10104 if (args_to_push)
10106 /* Push the argument registers, or reserve space for them. */
10107 if (cfun->machine->uses_anonymous_args)
10108 insn = emit_multi_reg_push
10109 ((0xf0 >> (args_to_push / 4)) & 0xf);
10110 else
10111 insn = emit_insn
10112 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10113 GEN_INT (- args_to_push)));
10114 RTX_FRAME_RELATED_P (insn) = 1;
10117 /* If this is an interrupt service routine, and the link register
10118 is going to be pushed, and we are not creating a stack frame,
10119 (which would involve an extra push of IP and a pop in the epilogue)
10120 subtracting four from LR now will mean that the function return
10121 can be done with a single instruction. */
10122 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10123 && (live_regs_mask & (1 << LR_REGNUM)) != 0
10124 && ! frame_pointer_needed)
10125 emit_insn (gen_rtx_SET (SImode,
10126 gen_rtx_REG (SImode, LR_REGNUM),
10127 gen_rtx_PLUS (SImode,
10128 gen_rtx_REG (SImode, LR_REGNUM),
10129 GEN_INT (-4))));
10131 if (live_regs_mask)
10133 insn = emit_multi_reg_push (live_regs_mask);
10134 saved_regs += bit_count (live_regs_mask) * 4;
10135 RTX_FRAME_RELATED_P (insn) = 1;
10138 if (TARGET_IWMMXT)
10139 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10140 if (regs_ever_live[reg] && ! call_used_regs [reg])
10142 insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10143 insn = gen_rtx_MEM (V2SImode, insn);
10144 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10145 gen_rtx_REG (V2SImode, reg)));
10146 RTX_FRAME_RELATED_P (insn) = 1;
10147 saved_regs += 8;
10150 if (! IS_VOLATILE (func_type))
10152 int start_reg;
10154 /* Save any floating point call-saved registers used by this
10155 function. */
10156 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10158 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10159 if (regs_ever_live[reg] && !call_used_regs[reg])
10161 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10162 insn = gen_rtx_MEM (XFmode, insn);
10163 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10164 gen_rtx_REG (XFmode, reg)));
10165 RTX_FRAME_RELATED_P (insn) = 1;
10166 saved_regs += 12;
10169 else
10171 start_reg = LAST_FPA_REGNUM;
10173 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10175 if (regs_ever_live[reg] && !call_used_regs[reg])
10177 if (start_reg - reg == 3)
10179 insn = emit_sfm (reg, 4);
10180 RTX_FRAME_RELATED_P (insn) = 1;
10181 saved_regs += 48;
10182 start_reg = reg - 1;
10185 else
10187 if (start_reg != reg)
10189 insn = emit_sfm (reg + 1, start_reg - reg);
10190 RTX_FRAME_RELATED_P (insn) = 1;
10191 saved_regs += (start_reg - reg) * 12;
10193 start_reg = reg - 1;
10197 if (start_reg != reg)
10199 insn = emit_sfm (reg + 1, start_reg - reg);
10200 saved_regs += (start_reg - reg) * 12;
10201 RTX_FRAME_RELATED_P (insn) = 1;
10204 if (TARGET_HARD_FLOAT && TARGET_VFP)
10206 start_reg = FIRST_VFP_REGNUM;
10208 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10210 if ((!regs_ever_live[reg] || call_used_regs[reg])
10211 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10213 if (start_reg != reg)
10214 saved_regs += vfp_emit_fstmx (start_reg,
10215 (reg - start_reg) / 2);
10216 start_reg = reg + 2;
10219 if (start_reg != reg)
10220 saved_regs += vfp_emit_fstmx (start_reg,
10221 (reg - start_reg) / 2);
10225 if (frame_pointer_needed)
10227 /* Create the new frame pointer. */
10228 insn = GEN_INT (-(4 + args_to_push + fp_offset));
10229 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10230 RTX_FRAME_RELATED_P (insn) = 1;
10232 if (IS_NESTED (func_type))
10234 /* Recover the static chain register. */
10235 if (regs_ever_live [3] == 0
10236 || saved_pretend_args)
10237 insn = gen_rtx_REG (SImode, 3);
10238 else /* if (current_function_pretend_args_size == 0) */
10240 insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10241 GEN_INT (4));
10242 insn = gen_rtx_MEM (SImode, insn);
10245 emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10246 /* Add a USE to stop propagate_one_insn() from barfing. */
10247 emit_insn (gen_prologue_use (ip_rtx));
10251 offsets = arm_get_frame_offsets ();
10252 if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10254 /* This add can produce multiple insns for a large constant, so we
10255 need to get tricky. */
10256 rtx last = get_last_insn ();
10258 amount = GEN_INT (offsets->saved_args + saved_regs
10259 - offsets->outgoing_args);
10261 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10262 amount));
10265 last = last ? NEXT_INSN (last) : get_insns ();
10266 RTX_FRAME_RELATED_P (last) = 1;
10268 while (last != insn);
10270 /* If the frame pointer is needed, emit a special barrier that
10271 will prevent the scheduler from moving stores to the frame
10272 before the stack adjustment. */
10273 if (frame_pointer_needed)
10274 insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10275 hard_frame_pointer_rtx));
10279 if (flag_pic)
10280 arm_load_pic_register (INVALID_REGNUM);
10282 /* If we are profiling, make sure no instructions are scheduled before
10283 the call to mcount. Similarly if the user has requested no
10284 scheduling in the prolog. */
10285 if (current_function_profile || TARGET_NO_SCHED_PRO)
10286 emit_insn (gen_blockage ());
10288 /* If the link register is being kept alive, with the return address in it,
10289 then make sure that it does not get reused by the ce2 pass. */
10290 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10292 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10293 cfun->machine->lr_save_eliminated = 1;
10297 /* If CODE is 'd', then the X is a condition operand and the instruction
10298 should only be executed if the condition is true.
10299 if CODE is 'D', then the X is a condition operand and the instruction
10300 should only be executed if the condition is false: however, if the mode
10301 of the comparison is CCFPEmode, then always execute the instruction -- we
10302 do this because in these circumstances !GE does not necessarily imply LT;
10303 in these cases the instruction pattern will take care to make sure that
10304 an instruction containing %d will follow, thereby undoing the effects of
10305 doing this instruction unconditionally.
10306 If CODE is 'N' then X is a floating point operand that must be negated
10307 before output.
10308 If CODE is 'B' then output a bitwise inverted value of X (a const int).
10309 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
10310 void
10311 arm_print_operand (FILE *stream, rtx x, int code)
10313 switch (code)
10315 case '@':
10316 fputs (ASM_COMMENT_START, stream);
10317 return;
10319 case '_':
10320 fputs (user_label_prefix, stream);
10321 return;
10323 case '|':
10324 fputs (REGISTER_PREFIX, stream);
10325 return;
10327 case '?':
10328 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10330 if (TARGET_THUMB)
10332 output_operand_lossage ("predicated Thumb instruction");
10333 break;
10335 if (current_insn_predicate != NULL)
10337 output_operand_lossage
10338 ("predicated instruction in conditional sequence");
10339 break;
10342 fputs (arm_condition_codes[arm_current_cc], stream);
10344 else if (current_insn_predicate)
10346 enum arm_cond_code code;
10348 if (TARGET_THUMB)
10350 output_operand_lossage ("predicated Thumb instruction");
10351 break;
10354 code = get_arm_condition_code (current_insn_predicate);
10355 fputs (arm_condition_codes[code], stream);
10357 return;
10359 case 'N':
10361 REAL_VALUE_TYPE r;
10362 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10363 r = REAL_VALUE_NEGATE (r);
10364 fprintf (stream, "%s", fp_const_from_val (&r));
10366 return;
10368 case 'B':
10369 if (GET_CODE (x) == CONST_INT)
10371 HOST_WIDE_INT val;
10372 val = ARM_SIGN_EXTEND (~INTVAL (x));
10373 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10375 else
10377 putc ('~', stream);
10378 output_addr_const (stream, x);
10380 return;
10382 case 'i':
10383 fprintf (stream, "%s", arithmetic_instr (x, 1));
10384 return;
10386 /* Truncate Cirrus shift counts. */
10387 case 's':
10388 if (GET_CODE (x) == CONST_INT)
10390 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10391 return;
10393 arm_print_operand (stream, x, 0);
10394 return;
10396 case 'I':
10397 fprintf (stream, "%s", arithmetic_instr (x, 0));
10398 return;
10400 case 'S':
10402 HOST_WIDE_INT val;
10403 const char * shift = shift_op (x, &val);
10405 if (shift)
10407 fprintf (stream, ", %s ", shift_op (x, &val));
10408 if (val == -1)
10409 arm_print_operand (stream, XEXP (x, 1), 0);
10410 else
10411 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10414 return;
10416 /* An explanation of the 'Q', 'R' and 'H' register operands:
10418 In a pair of registers containing a DI or DF value the 'Q'
10419 operand returns the register number of the register containing
10420 the least significant part of the value. The 'R' operand returns
10421 the register number of the register containing the most
10422 significant part of the value.
10424 The 'H' operand returns the higher of the two register numbers.
10425 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10426 same as the 'Q' operand, since the most significant part of the
10427 value is held in the lower number register. The reverse is true
10428 on systems where WORDS_BIG_ENDIAN is false.
10430 The purpose of these operands is to distinguish between cases
10431 where the endian-ness of the values is important (for example
10432 when they are added together), and cases where the endian-ness
10433 is irrelevant, but the order of register operations is important.
10434 For example when loading a value from memory into a register
10435 pair, the endian-ness does not matter. Provided that the value
10436 from the lower memory address is put into the lower numbered
10437 register, and the value from the higher address is put into the
10438 higher numbered register, the load will work regardless of whether
10439 the value being loaded is big-wordian or little-wordian. The
10440 order of the two register loads can matter however, if the address
10441 of the memory location is actually held in one of the registers
10442 being overwritten by the load. */
10443 case 'Q':
10444 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10446 output_operand_lossage ("invalid operand for code '%c'", code);
10447 return;
10450 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10451 return;
10453 case 'R':
10454 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10456 output_operand_lossage ("invalid operand for code '%c'", code);
10457 return;
10460 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10461 return;
10463 case 'H':
10464 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10466 output_operand_lossage ("invalid operand for code '%c'", code);
10467 return;
10470 asm_fprintf (stream, "%r", REGNO (x) + 1);
10471 return;
10473 case 'm':
10474 asm_fprintf (stream, "%r",
10475 GET_CODE (XEXP (x, 0)) == REG
10476 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10477 return;
10479 case 'M':
10480 asm_fprintf (stream, "{%r-%r}",
10481 REGNO (x),
10482 REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10483 return;
10485 case 'd':
10486 /* CONST_TRUE_RTX means always -- that's the default. */
10487 if (x == const_true_rtx)
10488 return;
10490 if (!COMPARISON_P (x))
10492 output_operand_lossage ("invalid operand for code '%c'", code);
10493 return;
10496 fputs (arm_condition_codes[get_arm_condition_code (x)],
10497 stream);
10498 return;
10500 case 'D':
10501 /* CONST_TRUE_RTX means not always -- i.e. never. We shouldn't ever
10502 want to do that. */
10503 if (x == const_true_rtx)
10505 output_operand_lossage ("instruction never exectued");
10506 return;
10508 if (!COMPARISON_P (x))
10510 output_operand_lossage ("invalid operand for code '%c'", code);
10511 return;
10514 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10515 (get_arm_condition_code (x))],
10516 stream);
10517 return;
10519 /* Cirrus registers can be accessed in a variety of ways:
10520 single floating point (f)
10521 double floating point (d)
10522 32bit integer (fx)
10523 64bit integer (dx). */
10524 case 'W': /* Cirrus register in F mode. */
10525 case 'X': /* Cirrus register in D mode. */
10526 case 'Y': /* Cirrus register in FX mode. */
10527 case 'Z': /* Cirrus register in DX mode. */
10528 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10529 abort ();
10531 fprintf (stream, "mv%s%s",
10532 code == 'W' ? "f"
10533 : code == 'X' ? "d"
10534 : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10536 return;
10538 /* Print cirrus register in the mode specified by the register's mode. */
10539 case 'V':
10541 int mode = GET_MODE (x);
10543 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10545 output_operand_lossage ("invalid operand for code '%c'", code);
10546 return;
10549 fprintf (stream, "mv%s%s",
10550 mode == DFmode ? "d"
10551 : mode == SImode ? "fx"
10552 : mode == DImode ? "dx"
10553 : "f", reg_names[REGNO (x)] + 2);
10555 return;
10558 case 'U':
10559 if (GET_CODE (x) != REG
10560 || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10561 || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10562 /* Bad value for wCG register number. */
10564 output_operand_lossage ("invalid operand for code '%c'", code);
10565 return;
10568 else
10569 fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10570 return;
10572 /* Print an iWMMXt control register name. */
10573 case 'w':
10574 if (GET_CODE (x) != CONST_INT
10575 || INTVAL (x) < 0
10576 || INTVAL (x) >= 16)
10577 /* Bad value for wC register number. */
10579 output_operand_lossage ("invalid operand for code '%c'", code);
10580 return;
10583 else
10585 static const char * wc_reg_names [16] =
10587 "wCID", "wCon", "wCSSF", "wCASF",
10588 "wC4", "wC5", "wC6", "wC7",
10589 "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10590 "wC12", "wC13", "wC14", "wC15"
10593 fprintf (stream, wc_reg_names [INTVAL (x)]);
10595 return;
10597 /* Print a VFP double precision register name. */
10598 case 'P':
10600 int mode = GET_MODE (x);
10601 int num;
10603 if (mode != DImode && mode != DFmode)
10605 output_operand_lossage ("invalid operand for code '%c'", code);
10606 return;
10609 if (GET_CODE (x) != REG
10610 || !IS_VFP_REGNUM (REGNO (x)))
10612 output_operand_lossage ("invalid operand for code '%c'", code);
10613 return;
10616 num = REGNO(x) - FIRST_VFP_REGNUM;
10617 if (num & 1)
10619 output_operand_lossage ("invalid operand for code '%c'", code);
10620 return;
10623 fprintf (stream, "d%d", num >> 1);
10625 return;
10627 default:
10628 if (x == 0)
10630 output_operand_lossage ("missing operand");
10631 return;
10634 if (GET_CODE (x) == REG)
10635 asm_fprintf (stream, "%r", REGNO (x));
10636 else if (GET_CODE (x) == MEM)
10638 output_memory_reference_mode = GET_MODE (x);
10639 output_address (XEXP (x, 0));
10641 else if (GET_CODE (x) == CONST_DOUBLE)
10642 fprintf (stream, "#%s", fp_immediate_constant (x));
10643 else if (GET_CODE (x) == NEG)
10644 abort (); /* This should never happen now. */
10645 else
10647 fputc ('#', stream);
10648 output_addr_const (stream, x);
10653 #ifndef AOF_ASSEMBLER
10654 /* Target hook for assembling integer objects. The ARM version needs to
10655 handle word-sized values specially. */
10656 static bool
10657 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10659 if (size == UNITS_PER_WORD && aligned_p)
10661 fputs ("\t.word\t", asm_out_file);
10662 output_addr_const (asm_out_file, x);
10664 /* Mark symbols as position independent. We only do this in the
10665 .text segment, not in the .data segment. */
10666 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10667 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10669 if (GET_CODE (x) == SYMBOL_REF
10670 && (CONSTANT_POOL_ADDRESS_P (x)
10671 || SYMBOL_REF_LOCAL_P (x)))
10672 fputs ("(GOTOFF)", asm_out_file);
10673 else if (GET_CODE (x) == LABEL_REF)
10674 fputs ("(GOTOFF)", asm_out_file);
10675 else
10676 fputs ("(GOT)", asm_out_file);
10678 fputc ('\n', asm_out_file);
10679 return true;
10682 if (arm_vector_mode_supported_p (GET_MODE (x)))
10684 int i, units;
10686 if (GET_CODE (x) != CONST_VECTOR)
10687 abort ();
10689 units = CONST_VECTOR_NUNITS (x);
10691 switch (GET_MODE (x))
10693 case V2SImode: size = 4; break;
10694 case V4HImode: size = 2; break;
10695 case V8QImode: size = 1; break;
10696 default:
10697 abort ();
10700 for (i = 0; i < units; i++)
10702 rtx elt;
10704 elt = CONST_VECTOR_ELT (x, i);
10705 assemble_integer
10706 (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10709 return true;
10712 return default_assemble_integer (x, size, aligned_p);
10714 #endif
10716 /* A finite state machine takes care of noticing whether or not instructions
10717 can be conditionally executed, and thus decrease execution time and code
10718 size by deleting branch instructions. The fsm is controlled by
10719 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
10721 /* The state of the fsm controlling condition codes are:
10722 0: normal, do nothing special
10723 1: make ASM_OUTPUT_OPCODE not output this instruction
10724 2: make ASM_OUTPUT_OPCODE not output this instruction
10725 3: make instructions conditional
10726 4: make instructions conditional
10728 State transitions (state->state by whom under condition):
10729 0 -> 1 final_prescan_insn if the `target' is a label
10730 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10731 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10732 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10733 3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10734 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10735 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10736 (the target insn is arm_target_insn).
10738 If the jump clobbers the conditions then we use states 2 and 4.
10740 A similar thing can be done with conditional return insns.
10742 XXX In case the `target' is an unconditional branch, this conditionalising
10743 of the instructions always reduces code size, but not always execution
10744 time. But then, I want to reduce the code size to somewhere near what
10745 /bin/cc produces. */
10747 /* Returns the index of the ARM condition code string in
10748 `arm_condition_codes'. COMPARISON should be an rtx like
10749 `(eq (...) (...))'. */
10750 static enum arm_cond_code
10751 get_arm_condition_code (rtx comparison)
10753 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10754 int code;
10755 enum rtx_code comp_code = GET_CODE (comparison);
10757 if (GET_MODE_CLASS (mode) != MODE_CC)
10758 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10759 XEXP (comparison, 1));
10761 switch (mode)
10763 case CC_DNEmode: code = ARM_NE; goto dominance;
10764 case CC_DEQmode: code = ARM_EQ; goto dominance;
10765 case CC_DGEmode: code = ARM_GE; goto dominance;
10766 case CC_DGTmode: code = ARM_GT; goto dominance;
10767 case CC_DLEmode: code = ARM_LE; goto dominance;
10768 case CC_DLTmode: code = ARM_LT; goto dominance;
10769 case CC_DGEUmode: code = ARM_CS; goto dominance;
10770 case CC_DGTUmode: code = ARM_HI; goto dominance;
10771 case CC_DLEUmode: code = ARM_LS; goto dominance;
10772 case CC_DLTUmode: code = ARM_CC;
10774 dominance:
10775 if (comp_code != EQ && comp_code != NE)
10776 abort ();
10778 if (comp_code == EQ)
10779 return ARM_INVERSE_CONDITION_CODE (code);
10780 return code;
10782 case CC_NOOVmode:
10783 switch (comp_code)
10785 case NE: return ARM_NE;
10786 case EQ: return ARM_EQ;
10787 case GE: return ARM_PL;
10788 case LT: return ARM_MI;
10789 default: abort ();
10792 case CC_Zmode:
10793 switch (comp_code)
10795 case NE: return ARM_NE;
10796 case EQ: return ARM_EQ;
10797 default: abort ();
10800 case CC_Nmode:
10801 switch (comp_code)
10803 case NE: return ARM_MI;
10804 case EQ: return ARM_PL;
10805 default: abort ();
10808 case CCFPEmode:
10809 case CCFPmode:
10810 /* These encodings assume that AC=1 in the FPA system control
10811 byte. This allows us to handle all cases except UNEQ and
10812 LTGT. */
10813 switch (comp_code)
10815 case GE: return ARM_GE;
10816 case GT: return ARM_GT;
10817 case LE: return ARM_LS;
10818 case LT: return ARM_MI;
10819 case NE: return ARM_NE;
10820 case EQ: return ARM_EQ;
10821 case ORDERED: return ARM_VC;
10822 case UNORDERED: return ARM_VS;
10823 case UNLT: return ARM_LT;
10824 case UNLE: return ARM_LE;
10825 case UNGT: return ARM_HI;
10826 case UNGE: return ARM_PL;
10827 /* UNEQ and LTGT do not have a representation. */
10828 case UNEQ: /* Fall through. */
10829 case LTGT: /* Fall through. */
10830 default: abort ();
10833 case CC_SWPmode:
10834 switch (comp_code)
10836 case NE: return ARM_NE;
10837 case EQ: return ARM_EQ;
10838 case GE: return ARM_LE;
10839 case GT: return ARM_LT;
10840 case LE: return ARM_GE;
10841 case LT: return ARM_GT;
10842 case GEU: return ARM_LS;
10843 case GTU: return ARM_CC;
10844 case LEU: return ARM_CS;
10845 case LTU: return ARM_HI;
10846 default: abort ();
10849 case CC_Cmode:
10850 switch (comp_code)
10852 case LTU: return ARM_CS;
10853 case GEU: return ARM_CC;
10854 default: abort ();
10857 case CCmode:
10858 switch (comp_code)
10860 case NE: return ARM_NE;
10861 case EQ: return ARM_EQ;
10862 case GE: return ARM_GE;
10863 case GT: return ARM_GT;
10864 case LE: return ARM_LE;
10865 case LT: return ARM_LT;
10866 case GEU: return ARM_CS;
10867 case GTU: return ARM_HI;
10868 case LEU: return ARM_LS;
10869 case LTU: return ARM_CC;
10870 default: abort ();
10873 default: abort ();
10876 abort ();
10879 void
10880 arm_final_prescan_insn (rtx insn)
10882 /* BODY will hold the body of INSN. */
10883 rtx body = PATTERN (insn);
10885 /* This will be 1 if trying to repeat the trick, and things need to be
10886 reversed if it appears to fail. */
10887 int reverse = 0;
10889 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
10890 taken are clobbered, even if the rtl suggests otherwise. It also
10891 means that we have to grub around within the jump expression to find
10892 out what the conditions are when the jump isn't taken. */
10893 int jump_clobbers = 0;
10895 /* If we start with a return insn, we only succeed if we find another one. */
10896 int seeking_return = 0;
10898 /* START_INSN will hold the insn from where we start looking. This is the
10899 first insn after the following code_label if REVERSE is true. */
10900 rtx start_insn = insn;
10902 /* If in state 4, check if the target branch is reached, in order to
10903 change back to state 0. */
10904 if (arm_ccfsm_state == 4)
10906 if (insn == arm_target_insn)
10908 arm_target_insn = NULL;
10909 arm_ccfsm_state = 0;
10911 return;
10914 /* If in state 3, it is possible to repeat the trick, if this insn is an
10915 unconditional branch to a label, and immediately following this branch
10916 is the previous target label which is only used once, and the label this
10917 branch jumps to is not too far off. */
10918 if (arm_ccfsm_state == 3)
10920 if (simplejump_p (insn))
10922 start_insn = next_nonnote_insn (start_insn);
10923 if (GET_CODE (start_insn) == BARRIER)
10925 /* XXX Isn't this always a barrier? */
10926 start_insn = next_nonnote_insn (start_insn);
10928 if (GET_CODE (start_insn) == CODE_LABEL
10929 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
10930 && LABEL_NUSES (start_insn) == 1)
10931 reverse = TRUE;
10932 else
10933 return;
10935 else if (GET_CODE (body) == RETURN)
10937 start_insn = next_nonnote_insn (start_insn);
10938 if (GET_CODE (start_insn) == BARRIER)
10939 start_insn = next_nonnote_insn (start_insn);
10940 if (GET_CODE (start_insn) == CODE_LABEL
10941 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
10942 && LABEL_NUSES (start_insn) == 1)
10944 reverse = TRUE;
10945 seeking_return = 1;
10947 else
10948 return;
10950 else
10951 return;
10954 if (arm_ccfsm_state != 0 && !reverse)
10955 abort ();
10956 if (GET_CODE (insn) != JUMP_INSN)
10957 return;
10959 /* This jump might be paralleled with a clobber of the condition codes
10960 the jump should always come first */
10961 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
10962 body = XVECEXP (body, 0, 0);
10964 if (reverse
10965 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
10966 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
10968 int insns_skipped;
10969 int fail = FALSE, succeed = FALSE;
10970 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
10971 int then_not_else = TRUE;
10972 rtx this_insn = start_insn, label = 0;
10974 /* If the jump cannot be done with one instruction, we cannot
10975 conditionally execute the instruction in the inverse case. */
10976 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
10978 jump_clobbers = 1;
10979 return;
10982 /* Register the insn jumped to. */
10983 if (reverse)
10985 if (!seeking_return)
10986 label = XEXP (SET_SRC (body), 0);
10988 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
10989 label = XEXP (XEXP (SET_SRC (body), 1), 0);
10990 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
10992 label = XEXP (XEXP (SET_SRC (body), 2), 0);
10993 then_not_else = FALSE;
10995 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
10996 seeking_return = 1;
10997 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
10999 seeking_return = 1;
11000 then_not_else = FALSE;
11002 else
11003 abort ();
11005 /* See how many insns this branch skips, and what kind of insns. If all
11006 insns are okay, and the label or unconditional branch to the same
11007 label is not too far away, succeed. */
11008 for (insns_skipped = 0;
11009 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11011 rtx scanbody;
11013 this_insn = next_nonnote_insn (this_insn);
11014 if (!this_insn)
11015 break;
11017 switch (GET_CODE (this_insn))
11019 case CODE_LABEL:
11020 /* Succeed if it is the target label, otherwise fail since
11021 control falls in from somewhere else. */
11022 if (this_insn == label)
11024 if (jump_clobbers)
11026 arm_ccfsm_state = 2;
11027 this_insn = next_nonnote_insn (this_insn);
11029 else
11030 arm_ccfsm_state = 1;
11031 succeed = TRUE;
11033 else
11034 fail = TRUE;
11035 break;
11037 case BARRIER:
11038 /* Succeed if the following insn is the target label.
11039 Otherwise fail.
11040 If return insns are used then the last insn in a function
11041 will be a barrier. */
11042 this_insn = next_nonnote_insn (this_insn);
11043 if (this_insn && this_insn == label)
11045 if (jump_clobbers)
11047 arm_ccfsm_state = 2;
11048 this_insn = next_nonnote_insn (this_insn);
11050 else
11051 arm_ccfsm_state = 1;
11052 succeed = TRUE;
11054 else
11055 fail = TRUE;
11056 break;
11058 case CALL_INSN:
11059 /* The AAPCS says that conditional calls should not be
11060 used since they make interworking inefficient (the
11061 linker can't transform BL<cond> into BLX). That's
11062 only a problem if the machine has BLX. */
11063 if (arm_arch5)
11065 fail = TRUE;
11066 break;
11069 /* Succeed if the following insn is the target label, or
11070 if the following two insns are a barrier and the
11071 target label. */
11072 this_insn = next_nonnote_insn (this_insn);
11073 if (this_insn && GET_CODE (this_insn) == BARRIER)
11074 this_insn = next_nonnote_insn (this_insn);
11076 if (this_insn && this_insn == label
11077 && insns_skipped < max_insns_skipped)
11079 if (jump_clobbers)
11081 arm_ccfsm_state = 2;
11082 this_insn = next_nonnote_insn (this_insn);
11084 else
11085 arm_ccfsm_state = 1;
11086 succeed = TRUE;
11088 else
11089 fail = TRUE;
11090 break;
11092 case JUMP_INSN:
11093 /* If this is an unconditional branch to the same label, succeed.
11094 If it is to another label, do nothing. If it is conditional,
11095 fail. */
11096 /* XXX Probably, the tests for SET and the PC are
11097 unnecessary. */
11099 scanbody = PATTERN (this_insn);
11100 if (GET_CODE (scanbody) == SET
11101 && GET_CODE (SET_DEST (scanbody)) == PC)
11103 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11104 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11106 arm_ccfsm_state = 2;
11107 succeed = TRUE;
11109 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11110 fail = TRUE;
11112 /* Fail if a conditional return is undesirable (e.g. on a
11113 StrongARM), but still allow this if optimizing for size. */
11114 else if (GET_CODE (scanbody) == RETURN
11115 && !use_return_insn (TRUE, NULL)
11116 && !optimize_size)
11117 fail = TRUE;
11118 else if (GET_CODE (scanbody) == RETURN
11119 && seeking_return)
11121 arm_ccfsm_state = 2;
11122 succeed = TRUE;
11124 else if (GET_CODE (scanbody) == PARALLEL)
11126 switch (get_attr_conds (this_insn))
11128 case CONDS_NOCOND:
11129 break;
11130 default:
11131 fail = TRUE;
11132 break;
11135 else
11136 fail = TRUE; /* Unrecognized jump (e.g. epilogue). */
11138 break;
11140 case INSN:
11141 /* Instructions using or affecting the condition codes make it
11142 fail. */
11143 scanbody = PATTERN (this_insn);
11144 if (!(GET_CODE (scanbody) == SET
11145 || GET_CODE (scanbody) == PARALLEL)
11146 || get_attr_conds (this_insn) != CONDS_NOCOND)
11147 fail = TRUE;
11149 /* A conditional cirrus instruction must be followed by
11150 a non Cirrus instruction. However, since we
11151 conditionalize instructions in this function and by
11152 the time we get here we can't add instructions
11153 (nops), because shorten_branches() has already been
11154 called, we will disable conditionalizing Cirrus
11155 instructions to be safe. */
11156 if (GET_CODE (scanbody) != USE
11157 && GET_CODE (scanbody) != CLOBBER
11158 && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11159 fail = TRUE;
11160 break;
11162 default:
11163 break;
11166 if (succeed)
11168 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11169 arm_target_label = CODE_LABEL_NUMBER (label);
11170 else if (seeking_return || arm_ccfsm_state == 2)
11172 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11174 this_insn = next_nonnote_insn (this_insn);
11175 if (this_insn && (GET_CODE (this_insn) == BARRIER
11176 || GET_CODE (this_insn) == CODE_LABEL))
11177 abort ();
11179 if (!this_insn)
11181 /* Oh, dear! we ran off the end.. give up. */
11182 recog (PATTERN (insn), insn, NULL);
11183 arm_ccfsm_state = 0;
11184 arm_target_insn = NULL;
11185 return;
11187 arm_target_insn = this_insn;
11189 else
11190 abort ();
11191 if (jump_clobbers)
11193 if (reverse)
11194 abort ();
11195 arm_current_cc =
11196 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11197 0), 0), 1));
11198 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11199 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11200 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11201 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11203 else
11205 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11206 what it was. */
11207 if (!reverse)
11208 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11209 0));
11212 if (reverse || then_not_else)
11213 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11216 /* Restore recog_data (getting the attributes of other insns can
11217 destroy this array, but final.c assumes that it remains intact
11218 across this call; since the insn has been recognized already we
11219 call recog direct). */
11220 recog (PATTERN (insn), insn, NULL);
11224 /* Returns true if REGNO is a valid register
11225 for holding a quantity of type MODE. */
11227 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11229 if (GET_MODE_CLASS (mode) == MODE_CC)
11230 return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11232 if (TARGET_THUMB)
11233 /* For the Thumb we only allow values bigger than SImode in
11234 registers 0 - 6, so that there is always a second low
11235 register available to hold the upper part of the value.
11236 We probably we ought to ensure that the register is the
11237 start of an even numbered register pair. */
11238 return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11240 if (IS_CIRRUS_REGNUM (regno))
11241 /* We have outlawed SI values in Cirrus registers because they
11242 reside in the lower 32 bits, but SF values reside in the
11243 upper 32 bits. This causes gcc all sorts of grief. We can't
11244 even split the registers into pairs because Cirrus SI values
11245 get sign extended to 64bits-- aldyh. */
11246 return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11248 if (IS_VFP_REGNUM (regno))
11250 if (mode == SFmode || mode == SImode)
11251 return TRUE;
11253 /* DFmode values are only valid in even register pairs. */
11254 if (mode == DFmode)
11255 return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11256 return FALSE;
11259 if (IS_IWMMXT_GR_REGNUM (regno))
11260 return mode == SImode;
11262 if (IS_IWMMXT_REGNUM (regno))
11263 return VALID_IWMMXT_REG_MODE (mode);
11265 /* We allow any value to be stored in the general registers.
11266 Restrict doubleword quantities to even register pairs so that we can
11267 use ldrd. */
11268 if (regno <= LAST_ARM_REGNUM)
11269 return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11271 if ( regno == FRAME_POINTER_REGNUM
11272 || regno == ARG_POINTER_REGNUM)
11273 /* We only allow integers in the fake hard registers. */
11274 return GET_MODE_CLASS (mode) == MODE_INT;
11276 /* The only registers left are the FPA registers
11277 which we only allow to hold FP values. */
11278 return GET_MODE_CLASS (mode) == MODE_FLOAT
11279 && regno >= FIRST_FPA_REGNUM
11280 && regno <= LAST_FPA_REGNUM;
11284 arm_regno_class (int regno)
11286 if (TARGET_THUMB)
11288 if (regno == STACK_POINTER_REGNUM)
11289 return STACK_REG;
11290 if (regno == CC_REGNUM)
11291 return CC_REG;
11292 if (regno < 8)
11293 return LO_REGS;
11294 return HI_REGS;
11297 if ( regno <= LAST_ARM_REGNUM
11298 || regno == FRAME_POINTER_REGNUM
11299 || regno == ARG_POINTER_REGNUM)
11300 return GENERAL_REGS;
11302 if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11303 return NO_REGS;
11305 if (IS_CIRRUS_REGNUM (regno))
11306 return CIRRUS_REGS;
11308 if (IS_VFP_REGNUM (regno))
11309 return VFP_REGS;
11311 if (IS_IWMMXT_REGNUM (regno))
11312 return IWMMXT_REGS;
11314 if (IS_IWMMXT_GR_REGNUM (regno))
11315 return IWMMXT_GR_REGS;
11317 return FPA_REGS;
11320 /* Handle a special case when computing the offset
11321 of an argument from the frame pointer. */
11323 arm_debugger_arg_offset (int value, rtx addr)
11325 rtx insn;
11327 /* We are only interested if dbxout_parms() failed to compute the offset. */
11328 if (value != 0)
11329 return 0;
11331 /* We can only cope with the case where the address is held in a register. */
11332 if (GET_CODE (addr) != REG)
11333 return 0;
11335 /* If we are using the frame pointer to point at the argument, then
11336 an offset of 0 is correct. */
11337 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11338 return 0;
11340 /* If we are using the stack pointer to point at the
11341 argument, then an offset of 0 is correct. */
11342 if ((TARGET_THUMB || !frame_pointer_needed)
11343 && REGNO (addr) == SP_REGNUM)
11344 return 0;
11346 /* Oh dear. The argument is pointed to by a register rather
11347 than being held in a register, or being stored at a known
11348 offset from the frame pointer. Since GDB only understands
11349 those two kinds of argument we must translate the address
11350 held in the register into an offset from the frame pointer.
11351 We do this by searching through the insns for the function
11352 looking to see where this register gets its value. If the
11353 register is initialized from the frame pointer plus an offset
11354 then we are in luck and we can continue, otherwise we give up.
11356 This code is exercised by producing debugging information
11357 for a function with arguments like this:
11359 double func (double a, double b, int c, double d) {return d;}
11361 Without this code the stab for parameter 'd' will be set to
11362 an offset of 0 from the frame pointer, rather than 8. */
11364 /* The if() statement says:
11366 If the insn is a normal instruction
11367 and if the insn is setting the value in a register
11368 and if the register being set is the register holding the address of the argument
11369 and if the address is computing by an addition
11370 that involves adding to a register
11371 which is the frame pointer
11372 a constant integer
11374 then... */
11376 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11378 if ( GET_CODE (insn) == INSN
11379 && GET_CODE (PATTERN (insn)) == SET
11380 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11381 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11382 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11383 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11384 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11387 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11389 break;
11393 if (value == 0)
11395 debug_rtx (addr);
11396 warning ("unable to compute real location of stacked parameter");
11397 value = 8; /* XXX magic hack */
11400 return value;
11403 #define def_mbuiltin(MASK, NAME, TYPE, CODE) \
11404 do \
11406 if ((MASK) & insn_flags) \
11407 lang_hooks.builtin_function ((NAME), (TYPE), (CODE), \
11408 BUILT_IN_MD, NULL, NULL_TREE); \
11410 while (0)
11412 struct builtin_description
11414 const unsigned int mask;
11415 const enum insn_code icode;
11416 const char * const name;
11417 const enum arm_builtins code;
11418 const enum rtx_code comparison;
11419 const unsigned int flag;
11422 static const struct builtin_description bdesc_2arg[] =
11424 #define IWMMXT_BUILTIN(code, string, builtin) \
11425 { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11426 ARM_BUILTIN_##builtin, 0, 0 },
11428 IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11429 IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11430 IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11431 IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11432 IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11433 IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11434 IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11435 IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11436 IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11437 IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11438 IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11439 IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11440 IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11441 IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11442 IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11443 IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11444 IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11445 IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11446 IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11447 IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11448 IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11449 IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11450 IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11451 IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11452 IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11453 IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11454 IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11455 IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11456 IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11457 IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11458 IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11459 IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11460 IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11461 IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11462 IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11463 IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11464 IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11465 IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11466 IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11467 IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11468 IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11469 IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11470 IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11471 IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11472 IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11473 IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11474 IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11475 IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11476 IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11477 IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11478 IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11479 IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11480 IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11481 IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11482 IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11483 IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11484 IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11485 IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11487 #define IWMMXT_BUILTIN2(code, builtin) \
11488 { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11490 IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11491 IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11492 IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11493 IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11494 IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11495 IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11496 IWMMXT_BUILTIN2 (ashlv4hi3_di, WSLLH)
11497 IWMMXT_BUILTIN2 (ashlv4hi3, WSLLHI)
11498 IWMMXT_BUILTIN2 (ashlv2si3_di, WSLLW)
11499 IWMMXT_BUILTIN2 (ashlv2si3, WSLLWI)
11500 IWMMXT_BUILTIN2 (ashldi3_di, WSLLD)
11501 IWMMXT_BUILTIN2 (ashldi3_iwmmxt, WSLLDI)
11502 IWMMXT_BUILTIN2 (lshrv4hi3_di, WSRLH)
11503 IWMMXT_BUILTIN2 (lshrv4hi3, WSRLHI)
11504 IWMMXT_BUILTIN2 (lshrv2si3_di, WSRLW)
11505 IWMMXT_BUILTIN2 (lshrv2si3, WSRLWI)
11506 IWMMXT_BUILTIN2 (lshrdi3_di, WSRLD)
11507 IWMMXT_BUILTIN2 (lshrdi3_iwmmxt, WSRLDI)
11508 IWMMXT_BUILTIN2 (ashrv4hi3_di, WSRAH)
11509 IWMMXT_BUILTIN2 (ashrv4hi3, WSRAHI)
11510 IWMMXT_BUILTIN2 (ashrv2si3_di, WSRAW)
11511 IWMMXT_BUILTIN2 (ashrv2si3, WSRAWI)
11512 IWMMXT_BUILTIN2 (ashrdi3_di, WSRAD)
11513 IWMMXT_BUILTIN2 (ashrdi3_iwmmxt, WSRADI)
11514 IWMMXT_BUILTIN2 (rorv4hi3_di, WRORH)
11515 IWMMXT_BUILTIN2 (rorv4hi3, WRORHI)
11516 IWMMXT_BUILTIN2 (rorv2si3_di, WRORW)
11517 IWMMXT_BUILTIN2 (rorv2si3, WRORWI)
11518 IWMMXT_BUILTIN2 (rordi3_di, WRORD)
11519 IWMMXT_BUILTIN2 (rordi3, WRORDI)
11520 IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
11521 IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
11524 static const struct builtin_description bdesc_1arg[] =
11526 IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11527 IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11528 IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11529 IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11530 IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11531 IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11532 IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11533 IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11534 IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11535 IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11536 IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11537 IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11538 IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11539 IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11540 IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11541 IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11542 IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11543 IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11546 /* Set up all the iWMMXt builtins. This is
11547 not called if TARGET_IWMMXT is zero. */
11549 static void
11550 arm_init_iwmmxt_builtins (void)
11552 const struct builtin_description * d;
11553 size_t i;
11554 tree endlink = void_list_node;
11556 tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11557 tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11558 tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11560 tree int_ftype_int
11561 = build_function_type (integer_type_node,
11562 tree_cons (NULL_TREE, integer_type_node, endlink));
11563 tree v8qi_ftype_v8qi_v8qi_int
11564 = build_function_type (V8QI_type_node,
11565 tree_cons (NULL_TREE, V8QI_type_node,
11566 tree_cons (NULL_TREE, V8QI_type_node,
11567 tree_cons (NULL_TREE,
11568 integer_type_node,
11569 endlink))));
11570 tree v4hi_ftype_v4hi_int
11571 = build_function_type (V4HI_type_node,
11572 tree_cons (NULL_TREE, V4HI_type_node,
11573 tree_cons (NULL_TREE, integer_type_node,
11574 endlink)));
11575 tree v2si_ftype_v2si_int
11576 = build_function_type (V2SI_type_node,
11577 tree_cons (NULL_TREE, V2SI_type_node,
11578 tree_cons (NULL_TREE, integer_type_node,
11579 endlink)));
11580 tree v2si_ftype_di_di
11581 = build_function_type (V2SI_type_node,
11582 tree_cons (NULL_TREE, long_long_integer_type_node,
11583 tree_cons (NULL_TREE, long_long_integer_type_node,
11584 endlink)));
11585 tree di_ftype_di_int
11586 = build_function_type (long_long_integer_type_node,
11587 tree_cons (NULL_TREE, long_long_integer_type_node,
11588 tree_cons (NULL_TREE, integer_type_node,
11589 endlink)));
11590 tree di_ftype_di_int_int
11591 = build_function_type (long_long_integer_type_node,
11592 tree_cons (NULL_TREE, long_long_integer_type_node,
11593 tree_cons (NULL_TREE, integer_type_node,
11594 tree_cons (NULL_TREE,
11595 integer_type_node,
11596 endlink))));
11597 tree int_ftype_v8qi
11598 = build_function_type (integer_type_node,
11599 tree_cons (NULL_TREE, V8QI_type_node,
11600 endlink));
11601 tree int_ftype_v4hi
11602 = build_function_type (integer_type_node,
11603 tree_cons (NULL_TREE, V4HI_type_node,
11604 endlink));
11605 tree int_ftype_v2si
11606 = build_function_type (integer_type_node,
11607 tree_cons (NULL_TREE, V2SI_type_node,
11608 endlink));
11609 tree int_ftype_v8qi_int
11610 = build_function_type (integer_type_node,
11611 tree_cons (NULL_TREE, V8QI_type_node,
11612 tree_cons (NULL_TREE, integer_type_node,
11613 endlink)));
11614 tree int_ftype_v4hi_int
11615 = build_function_type (integer_type_node,
11616 tree_cons (NULL_TREE, V4HI_type_node,
11617 tree_cons (NULL_TREE, integer_type_node,
11618 endlink)));
11619 tree int_ftype_v2si_int
11620 = build_function_type (integer_type_node,
11621 tree_cons (NULL_TREE, V2SI_type_node,
11622 tree_cons (NULL_TREE, integer_type_node,
11623 endlink)));
11624 tree v8qi_ftype_v8qi_int_int
11625 = build_function_type (V8QI_type_node,
11626 tree_cons (NULL_TREE, V8QI_type_node,
11627 tree_cons (NULL_TREE, integer_type_node,
11628 tree_cons (NULL_TREE,
11629 integer_type_node,
11630 endlink))));
11631 tree v4hi_ftype_v4hi_int_int
11632 = build_function_type (V4HI_type_node,
11633 tree_cons (NULL_TREE, V4HI_type_node,
11634 tree_cons (NULL_TREE, integer_type_node,
11635 tree_cons (NULL_TREE,
11636 integer_type_node,
11637 endlink))));
11638 tree v2si_ftype_v2si_int_int
11639 = build_function_type (V2SI_type_node,
11640 tree_cons (NULL_TREE, V2SI_type_node,
11641 tree_cons (NULL_TREE, integer_type_node,
11642 tree_cons (NULL_TREE,
11643 integer_type_node,
11644 endlink))));
11645 /* Miscellaneous. */
11646 tree v8qi_ftype_v4hi_v4hi
11647 = build_function_type (V8QI_type_node,
11648 tree_cons (NULL_TREE, V4HI_type_node,
11649 tree_cons (NULL_TREE, V4HI_type_node,
11650 endlink)));
11651 tree v4hi_ftype_v2si_v2si
11652 = build_function_type (V4HI_type_node,
11653 tree_cons (NULL_TREE, V2SI_type_node,
11654 tree_cons (NULL_TREE, V2SI_type_node,
11655 endlink)));
11656 tree v2si_ftype_v4hi_v4hi
11657 = build_function_type (V2SI_type_node,
11658 tree_cons (NULL_TREE, V4HI_type_node,
11659 tree_cons (NULL_TREE, V4HI_type_node,
11660 endlink)));
11661 tree v2si_ftype_v8qi_v8qi
11662 = build_function_type (V2SI_type_node,
11663 tree_cons (NULL_TREE, V8QI_type_node,
11664 tree_cons (NULL_TREE, V8QI_type_node,
11665 endlink)));
11666 tree v4hi_ftype_v4hi_di
11667 = build_function_type (V4HI_type_node,
11668 tree_cons (NULL_TREE, V4HI_type_node,
11669 tree_cons (NULL_TREE,
11670 long_long_integer_type_node,
11671 endlink)));
11672 tree v2si_ftype_v2si_di
11673 = build_function_type (V2SI_type_node,
11674 tree_cons (NULL_TREE, V2SI_type_node,
11675 tree_cons (NULL_TREE,
11676 long_long_integer_type_node,
11677 endlink)));
11678 tree void_ftype_int_int
11679 = build_function_type (void_type_node,
11680 tree_cons (NULL_TREE, integer_type_node,
11681 tree_cons (NULL_TREE, integer_type_node,
11682 endlink)));
11683 tree di_ftype_void
11684 = build_function_type (long_long_unsigned_type_node, endlink);
11685 tree di_ftype_v8qi
11686 = build_function_type (long_long_integer_type_node,
11687 tree_cons (NULL_TREE, V8QI_type_node,
11688 endlink));
11689 tree di_ftype_v4hi
11690 = build_function_type (long_long_integer_type_node,
11691 tree_cons (NULL_TREE, V4HI_type_node,
11692 endlink));
11693 tree di_ftype_v2si
11694 = build_function_type (long_long_integer_type_node,
11695 tree_cons (NULL_TREE, V2SI_type_node,
11696 endlink));
11697 tree v2si_ftype_v4hi
11698 = build_function_type (V2SI_type_node,
11699 tree_cons (NULL_TREE, V4HI_type_node,
11700 endlink));
11701 tree v4hi_ftype_v8qi
11702 = build_function_type (V4HI_type_node,
11703 tree_cons (NULL_TREE, V8QI_type_node,
11704 endlink));
11706 tree di_ftype_di_v4hi_v4hi
11707 = build_function_type (long_long_unsigned_type_node,
11708 tree_cons (NULL_TREE,
11709 long_long_unsigned_type_node,
11710 tree_cons (NULL_TREE, V4HI_type_node,
11711 tree_cons (NULL_TREE,
11712 V4HI_type_node,
11713 endlink))));
11715 tree di_ftype_v4hi_v4hi
11716 = build_function_type (long_long_unsigned_type_node,
11717 tree_cons (NULL_TREE, V4HI_type_node,
11718 tree_cons (NULL_TREE, V4HI_type_node,
11719 endlink)));
11721 /* Normal vector binops. */
11722 tree v8qi_ftype_v8qi_v8qi
11723 = build_function_type (V8QI_type_node,
11724 tree_cons (NULL_TREE, V8QI_type_node,
11725 tree_cons (NULL_TREE, V8QI_type_node,
11726 endlink)));
11727 tree v4hi_ftype_v4hi_v4hi
11728 = build_function_type (V4HI_type_node,
11729 tree_cons (NULL_TREE, V4HI_type_node,
11730 tree_cons (NULL_TREE, V4HI_type_node,
11731 endlink)));
11732 tree v2si_ftype_v2si_v2si
11733 = build_function_type (V2SI_type_node,
11734 tree_cons (NULL_TREE, V2SI_type_node,
11735 tree_cons (NULL_TREE, V2SI_type_node,
11736 endlink)));
11737 tree di_ftype_di_di
11738 = build_function_type (long_long_unsigned_type_node,
11739 tree_cons (NULL_TREE, long_long_unsigned_type_node,
11740 tree_cons (NULL_TREE,
11741 long_long_unsigned_type_node,
11742 endlink)));
11744 /* Add all builtins that are more or less simple operations on two
11745 operands. */
11746 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11748 /* Use one of the operands; the target can have a different mode for
11749 mask-generating compares. */
11750 enum machine_mode mode;
11751 tree type;
11753 if (d->name == 0)
11754 continue;
11756 mode = insn_data[d->icode].operand[1].mode;
11758 switch (mode)
11760 case V8QImode:
11761 type = v8qi_ftype_v8qi_v8qi;
11762 break;
11763 case V4HImode:
11764 type = v4hi_ftype_v4hi_v4hi;
11765 break;
11766 case V2SImode:
11767 type = v2si_ftype_v2si_v2si;
11768 break;
11769 case DImode:
11770 type = di_ftype_di_di;
11771 break;
11773 default:
11774 abort ();
11777 def_mbuiltin (d->mask, d->name, type, d->code);
11780 /* Add the remaining MMX insns with somewhat more complicated types. */
11781 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11782 def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11783 def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11785 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11786 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11787 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11788 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11789 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11790 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11792 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11793 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11794 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11795 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11796 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11797 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11799 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11800 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11801 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11802 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11803 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11804 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11806 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11807 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11808 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11809 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11810 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11811 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11813 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11815 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11816 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11817 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11818 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11820 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11821 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11822 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11823 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11824 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11825 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11826 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11827 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11828 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11830 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11831 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11832 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11834 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11835 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11836 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
11838 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
11839 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
11840 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
11841 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
11842 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
11843 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
11845 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
11846 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
11847 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
11848 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
11849 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
11850 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
11851 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
11852 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
11853 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
11854 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
11855 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
11856 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
11858 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
11859 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
11860 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
11861 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
11863 def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
11864 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
11865 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
11866 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
11867 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
11868 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
11869 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
11872 static void
11873 arm_init_builtins (void)
11875 if (TARGET_REALLY_IWMMXT)
11876 arm_init_iwmmxt_builtins ();
11879 /* Errors in the source file can cause expand_expr to return const0_rtx
11880 where we expect a vector. To avoid crashing, use one of the vector
11881 clear instructions. */
11883 static rtx
11884 safe_vector_operand (rtx x, enum machine_mode mode)
11886 if (x != const0_rtx)
11887 return x;
11888 x = gen_reg_rtx (mode);
11890 emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
11891 : gen_rtx_SUBREG (DImode, x, 0)));
11892 return x;
11895 /* Subroutine of arm_expand_builtin to take care of binop insns. */
11897 static rtx
11898 arm_expand_binop_builtin (enum insn_code icode,
11899 tree arglist, rtx target)
11901 rtx pat;
11902 tree arg0 = TREE_VALUE (arglist);
11903 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11904 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11905 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11906 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11907 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11908 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11910 if (VECTOR_MODE_P (mode0))
11911 op0 = safe_vector_operand (op0, mode0);
11912 if (VECTOR_MODE_P (mode1))
11913 op1 = safe_vector_operand (op1, mode1);
11915 if (! target
11916 || GET_MODE (target) != tmode
11917 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11918 target = gen_reg_rtx (tmode);
11920 /* In case the insn wants input operands in modes different from
11921 the result, abort. */
11922 if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
11923 abort ();
11925 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11926 op0 = copy_to_mode_reg (mode0, op0);
11927 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11928 op1 = copy_to_mode_reg (mode1, op1);
11930 pat = GEN_FCN (icode) (target, op0, op1);
11931 if (! pat)
11932 return 0;
11933 emit_insn (pat);
11934 return target;
11937 /* Subroutine of arm_expand_builtin to take care of unop insns. */
11939 static rtx
11940 arm_expand_unop_builtin (enum insn_code icode,
11941 tree arglist, rtx target, int do_load)
11943 rtx pat;
11944 tree arg0 = TREE_VALUE (arglist);
11945 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11946 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11947 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11949 if (! target
11950 || GET_MODE (target) != tmode
11951 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11952 target = gen_reg_rtx (tmode);
11953 if (do_load)
11954 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11955 else
11957 if (VECTOR_MODE_P (mode0))
11958 op0 = safe_vector_operand (op0, mode0);
11960 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11961 op0 = copy_to_mode_reg (mode0, op0);
11964 pat = GEN_FCN (icode) (target, op0);
11965 if (! pat)
11966 return 0;
11967 emit_insn (pat);
11968 return target;
11971 /* Expand an expression EXP that calls a built-in function,
11972 with result going to TARGET if that's convenient
11973 (and in mode MODE if that's convenient).
11974 SUBTARGET may be used as the target for computing one of EXP's operands.
11975 IGNORE is nonzero if the value is to be ignored. */
11977 static rtx
11978 arm_expand_builtin (tree exp,
11979 rtx target,
11980 rtx subtarget ATTRIBUTE_UNUSED,
11981 enum machine_mode mode ATTRIBUTE_UNUSED,
11982 int ignore ATTRIBUTE_UNUSED)
11984 const struct builtin_description * d;
11985 enum insn_code icode;
11986 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
11987 tree arglist = TREE_OPERAND (exp, 1);
11988 tree arg0;
11989 tree arg1;
11990 tree arg2;
11991 rtx op0;
11992 rtx op1;
11993 rtx op2;
11994 rtx pat;
11995 int fcode = DECL_FUNCTION_CODE (fndecl);
11996 size_t i;
11997 enum machine_mode tmode;
11998 enum machine_mode mode0;
11999 enum machine_mode mode1;
12000 enum machine_mode mode2;
12002 switch (fcode)
12004 case ARM_BUILTIN_TEXTRMSB:
12005 case ARM_BUILTIN_TEXTRMUB:
12006 case ARM_BUILTIN_TEXTRMSH:
12007 case ARM_BUILTIN_TEXTRMUH:
12008 case ARM_BUILTIN_TEXTRMSW:
12009 case ARM_BUILTIN_TEXTRMUW:
12010 icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12011 : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12012 : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12013 : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12014 : CODE_FOR_iwmmxt_textrmw);
12016 arg0 = TREE_VALUE (arglist);
12017 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12018 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12019 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12020 tmode = insn_data[icode].operand[0].mode;
12021 mode0 = insn_data[icode].operand[1].mode;
12022 mode1 = insn_data[icode].operand[2].mode;
12024 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12025 op0 = copy_to_mode_reg (mode0, op0);
12026 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12028 /* @@@ better error message */
12029 error ("selector must be an immediate");
12030 return gen_reg_rtx (tmode);
12032 if (target == 0
12033 || GET_MODE (target) != tmode
12034 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12035 target = gen_reg_rtx (tmode);
12036 pat = GEN_FCN (icode) (target, op0, op1);
12037 if (! pat)
12038 return 0;
12039 emit_insn (pat);
12040 return target;
12042 case ARM_BUILTIN_TINSRB:
12043 case ARM_BUILTIN_TINSRH:
12044 case ARM_BUILTIN_TINSRW:
12045 icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12046 : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12047 : CODE_FOR_iwmmxt_tinsrw);
12048 arg0 = TREE_VALUE (arglist);
12049 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12050 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12051 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12052 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12053 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12054 tmode = insn_data[icode].operand[0].mode;
12055 mode0 = insn_data[icode].operand[1].mode;
12056 mode1 = insn_data[icode].operand[2].mode;
12057 mode2 = insn_data[icode].operand[3].mode;
12059 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12060 op0 = copy_to_mode_reg (mode0, op0);
12061 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12062 op1 = copy_to_mode_reg (mode1, op1);
12063 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12065 /* @@@ better error message */
12066 error ("selector must be an immediate");
12067 return const0_rtx;
12069 if (target == 0
12070 || GET_MODE (target) != tmode
12071 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12072 target = gen_reg_rtx (tmode);
12073 pat = GEN_FCN (icode) (target, op0, op1, op2);
12074 if (! pat)
12075 return 0;
12076 emit_insn (pat);
12077 return target;
12079 case ARM_BUILTIN_SETWCX:
12080 arg0 = TREE_VALUE (arglist);
12081 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12082 op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12083 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12084 emit_insn (gen_iwmmxt_tmcr (op1, op0));
12085 return 0;
12087 case ARM_BUILTIN_GETWCX:
12088 arg0 = TREE_VALUE (arglist);
12089 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12090 target = gen_reg_rtx (SImode);
12091 emit_insn (gen_iwmmxt_tmrc (target, op0));
12092 return target;
12094 case ARM_BUILTIN_WSHUFH:
12095 icode = CODE_FOR_iwmmxt_wshufh;
12096 arg0 = TREE_VALUE (arglist);
12097 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12098 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12099 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12100 tmode = insn_data[icode].operand[0].mode;
12101 mode1 = insn_data[icode].operand[1].mode;
12102 mode2 = insn_data[icode].operand[2].mode;
12104 if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12105 op0 = copy_to_mode_reg (mode1, op0);
12106 if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12108 /* @@@ better error message */
12109 error ("mask must be an immediate");
12110 return const0_rtx;
12112 if (target == 0
12113 || GET_MODE (target) != tmode
12114 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12115 target = gen_reg_rtx (tmode);
12116 pat = GEN_FCN (icode) (target, op0, op1);
12117 if (! pat)
12118 return 0;
12119 emit_insn (pat);
12120 return target;
12122 case ARM_BUILTIN_WSADB:
12123 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12124 case ARM_BUILTIN_WSADH:
12125 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12126 case ARM_BUILTIN_WSADBZ:
12127 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12128 case ARM_BUILTIN_WSADHZ:
12129 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12131 /* Several three-argument builtins. */
12132 case ARM_BUILTIN_WMACS:
12133 case ARM_BUILTIN_WMACU:
12134 case ARM_BUILTIN_WALIGN:
12135 case ARM_BUILTIN_TMIA:
12136 case ARM_BUILTIN_TMIAPH:
12137 case ARM_BUILTIN_TMIATT:
12138 case ARM_BUILTIN_TMIATB:
12139 case ARM_BUILTIN_TMIABT:
12140 case ARM_BUILTIN_TMIABB:
12141 icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12142 : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12143 : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12144 : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12145 : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12146 : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12147 : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12148 : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12149 : CODE_FOR_iwmmxt_walign);
12150 arg0 = TREE_VALUE (arglist);
12151 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12152 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12153 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12154 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12155 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12156 tmode = insn_data[icode].operand[0].mode;
12157 mode0 = insn_data[icode].operand[1].mode;
12158 mode1 = insn_data[icode].operand[2].mode;
12159 mode2 = insn_data[icode].operand[3].mode;
12161 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12162 op0 = copy_to_mode_reg (mode0, op0);
12163 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12164 op1 = copy_to_mode_reg (mode1, op1);
12165 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12166 op2 = copy_to_mode_reg (mode2, op2);
12167 if (target == 0
12168 || GET_MODE (target) != tmode
12169 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12170 target = gen_reg_rtx (tmode);
12171 pat = GEN_FCN (icode) (target, op0, op1, op2);
12172 if (! pat)
12173 return 0;
12174 emit_insn (pat);
12175 return target;
12177 case ARM_BUILTIN_WZERO:
12178 target = gen_reg_rtx (DImode);
12179 emit_insn (gen_iwmmxt_clrdi (target));
12180 return target;
12182 default:
12183 break;
12186 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12187 if (d->code == (const enum arm_builtins) fcode)
12188 return arm_expand_binop_builtin (d->icode, arglist, target);
12190 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12191 if (d->code == (const enum arm_builtins) fcode)
12192 return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12194 /* @@@ Should really do something sensible here. */
12195 return NULL_RTX;
12198 /* Return the number (counting from 0) of
12199 the least significant set bit in MASK. */
12201 inline static int
12202 number_of_first_bit_set (unsigned mask)
12204 int bit;
12206 for (bit = 0;
12207 (mask & (1 << bit)) == 0;
12208 ++bit)
12209 continue;
12211 return bit;
12214 /* Emit code to push or pop registers to or from the stack. F is the
12215 assembly file. MASK is the registers to push or pop. PUSH is
12216 nonzero if we should push, and zero if we should pop. For debugging
12217 output, if pushing, adjust CFA_OFFSET by the amount of space added
12218 to the stack. REAL_REGS should have the same number of bits set as
12219 MASK, and will be used instead (in the same order) to describe which
12220 registers were saved - this is used to mark the save slots when we
12221 push high registers after moving them to low registers. */
12222 static void
12223 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
12224 unsigned long real_regs)
12226 int regno;
12227 int lo_mask = mask & 0xFF;
12228 int pushed_words = 0;
12230 if (mask == 0)
12231 abort ();
12233 if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12235 /* Special case. Do not generate a POP PC statement here, do it in
12236 thumb_exit() */
12237 thumb_exit (f, -1);
12238 return;
12241 fprintf (f, "\t%s\t{", push ? "push" : "pop");
12243 /* Look at the low registers first. */
12244 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12246 if (lo_mask & 1)
12248 asm_fprintf (f, "%r", regno);
12250 if ((lo_mask & ~1) != 0)
12251 fprintf (f, ", ");
12253 pushed_words++;
12257 if (push && (mask & (1 << LR_REGNUM)))
12259 /* Catch pushing the LR. */
12260 if (mask & 0xFF)
12261 fprintf (f, ", ");
12263 asm_fprintf (f, "%r", LR_REGNUM);
12265 pushed_words++;
12267 else if (!push && (mask & (1 << PC_REGNUM)))
12269 /* Catch popping the PC. */
12270 if (TARGET_INTERWORK || TARGET_BACKTRACE
12271 || current_function_calls_eh_return)
12273 /* The PC is never poped directly, instead
12274 it is popped into r3 and then BX is used. */
12275 fprintf (f, "}\n");
12277 thumb_exit (f, -1);
12279 return;
12281 else
12283 if (mask & 0xFF)
12284 fprintf (f, ", ");
12286 asm_fprintf (f, "%r", PC_REGNUM);
12290 fprintf (f, "}\n");
12292 if (push && pushed_words && dwarf2out_do_frame ())
12294 char *l = dwarf2out_cfi_label ();
12295 int pushed_mask = real_regs;
12297 *cfa_offset += pushed_words * 4;
12298 dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12300 pushed_words = 0;
12301 pushed_mask = real_regs;
12302 for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12304 if (pushed_mask & 1)
12305 dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12310 /* Generate code to return from a thumb function.
12311 If 'reg_containing_return_addr' is -1, then the return address is
12312 actually on the stack, at the stack pointer. */
12313 static void
12314 thumb_exit (FILE *f, int reg_containing_return_addr)
12316 unsigned regs_available_for_popping;
12317 unsigned regs_to_pop;
12318 int pops_needed;
12319 unsigned available;
12320 unsigned required;
12321 int mode;
12322 int size;
12323 int restore_a4 = FALSE;
12325 /* Compute the registers we need to pop. */
12326 regs_to_pop = 0;
12327 pops_needed = 0;
12329 if (reg_containing_return_addr == -1)
12331 regs_to_pop |= 1 << LR_REGNUM;
12332 ++pops_needed;
12335 if (TARGET_BACKTRACE)
12337 /* Restore the (ARM) frame pointer and stack pointer. */
12338 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12339 pops_needed += 2;
12342 /* If there is nothing to pop then just emit the BX instruction and
12343 return. */
12344 if (pops_needed == 0)
12346 if (current_function_calls_eh_return)
12347 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12349 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12350 return;
12352 /* Otherwise if we are not supporting interworking and we have not created
12353 a backtrace structure and the function was not entered in ARM mode then
12354 just pop the return address straight into the PC. */
12355 else if (!TARGET_INTERWORK
12356 && !TARGET_BACKTRACE
12357 && !is_called_in_ARM_mode (current_function_decl)
12358 && !current_function_calls_eh_return)
12360 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12361 return;
12364 /* Find out how many of the (return) argument registers we can corrupt. */
12365 regs_available_for_popping = 0;
12367 /* If returning via __builtin_eh_return, the bottom three registers
12368 all contain information needed for the return. */
12369 if (current_function_calls_eh_return)
12370 size = 12;
12371 else
12373 /* If we can deduce the registers used from the function's
12374 return value. This is more reliable that examining
12375 regs_ever_live[] because that will be set if the register is
12376 ever used in the function, not just if the register is used
12377 to hold a return value. */
12379 if (current_function_return_rtx != 0)
12380 mode = GET_MODE (current_function_return_rtx);
12381 else
12382 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12384 size = GET_MODE_SIZE (mode);
12386 if (size == 0)
12388 /* In a void function we can use any argument register.
12389 In a function that returns a structure on the stack
12390 we can use the second and third argument registers. */
12391 if (mode == VOIDmode)
12392 regs_available_for_popping =
12393 (1 << ARG_REGISTER (1))
12394 | (1 << ARG_REGISTER (2))
12395 | (1 << ARG_REGISTER (3));
12396 else
12397 regs_available_for_popping =
12398 (1 << ARG_REGISTER (2))
12399 | (1 << ARG_REGISTER (3));
12401 else if (size <= 4)
12402 regs_available_for_popping =
12403 (1 << ARG_REGISTER (2))
12404 | (1 << ARG_REGISTER (3));
12405 else if (size <= 8)
12406 regs_available_for_popping =
12407 (1 << ARG_REGISTER (3));
12410 /* Match registers to be popped with registers into which we pop them. */
12411 for (available = regs_available_for_popping,
12412 required = regs_to_pop;
12413 required != 0 && available != 0;
12414 available &= ~(available & - available),
12415 required &= ~(required & - required))
12416 -- pops_needed;
12418 /* If we have any popping registers left over, remove them. */
12419 if (available > 0)
12420 regs_available_for_popping &= ~available;
12422 /* Otherwise if we need another popping register we can use
12423 the fourth argument register. */
12424 else if (pops_needed)
12426 /* If we have not found any free argument registers and
12427 reg a4 contains the return address, we must move it. */
12428 if (regs_available_for_popping == 0
12429 && reg_containing_return_addr == LAST_ARG_REGNUM)
12431 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12432 reg_containing_return_addr = LR_REGNUM;
12434 else if (size > 12)
12436 /* Register a4 is being used to hold part of the return value,
12437 but we have dire need of a free, low register. */
12438 restore_a4 = TRUE;
12440 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12443 if (reg_containing_return_addr != LAST_ARG_REGNUM)
12445 /* The fourth argument register is available. */
12446 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12448 --pops_needed;
12452 /* Pop as many registers as we can. */
12453 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12454 regs_available_for_popping);
12456 /* Process the registers we popped. */
12457 if (reg_containing_return_addr == -1)
12459 /* The return address was popped into the lowest numbered register. */
12460 regs_to_pop &= ~(1 << LR_REGNUM);
12462 reg_containing_return_addr =
12463 number_of_first_bit_set (regs_available_for_popping);
12465 /* Remove this register for the mask of available registers, so that
12466 the return address will not be corrupted by further pops. */
12467 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12470 /* If we popped other registers then handle them here. */
12471 if (regs_available_for_popping)
12473 int frame_pointer;
12475 /* Work out which register currently contains the frame pointer. */
12476 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12478 /* Move it into the correct place. */
12479 asm_fprintf (f, "\tmov\t%r, %r\n",
12480 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12482 /* (Temporarily) remove it from the mask of popped registers. */
12483 regs_available_for_popping &= ~(1 << frame_pointer);
12484 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12486 if (regs_available_for_popping)
12488 int stack_pointer;
12490 /* We popped the stack pointer as well,
12491 find the register that contains it. */
12492 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12494 /* Move it into the stack register. */
12495 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12497 /* At this point we have popped all necessary registers, so
12498 do not worry about restoring regs_available_for_popping
12499 to its correct value:
12501 assert (pops_needed == 0)
12502 assert (regs_available_for_popping == (1 << frame_pointer))
12503 assert (regs_to_pop == (1 << STACK_POINTER)) */
12505 else
12507 /* Since we have just move the popped value into the frame
12508 pointer, the popping register is available for reuse, and
12509 we know that we still have the stack pointer left to pop. */
12510 regs_available_for_popping |= (1 << frame_pointer);
12514 /* If we still have registers left on the stack, but we no longer have
12515 any registers into which we can pop them, then we must move the return
12516 address into the link register and make available the register that
12517 contained it. */
12518 if (regs_available_for_popping == 0 && pops_needed > 0)
12520 regs_available_for_popping |= 1 << reg_containing_return_addr;
12522 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12523 reg_containing_return_addr);
12525 reg_containing_return_addr = LR_REGNUM;
12528 /* If we have registers left on the stack then pop some more.
12529 We know that at most we will want to pop FP and SP. */
12530 if (pops_needed > 0)
12532 int popped_into;
12533 int move_to;
12535 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12536 regs_available_for_popping);
12538 /* We have popped either FP or SP.
12539 Move whichever one it is into the correct register. */
12540 popped_into = number_of_first_bit_set (regs_available_for_popping);
12541 move_to = number_of_first_bit_set (regs_to_pop);
12543 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12545 regs_to_pop &= ~(1 << move_to);
12547 --pops_needed;
12550 /* If we still have not popped everything then we must have only
12551 had one register available to us and we are now popping the SP. */
12552 if (pops_needed > 0)
12554 int popped_into;
12556 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12557 regs_available_for_popping);
12559 popped_into = number_of_first_bit_set (regs_available_for_popping);
12561 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12563 assert (regs_to_pop == (1 << STACK_POINTER))
12564 assert (pops_needed == 1)
12568 /* If necessary restore the a4 register. */
12569 if (restore_a4)
12571 if (reg_containing_return_addr != LR_REGNUM)
12573 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12574 reg_containing_return_addr = LR_REGNUM;
12577 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12580 if (current_function_calls_eh_return)
12581 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12583 /* Return to caller. */
12584 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12588 void
12589 thumb_final_prescan_insn (rtx insn)
12591 if (flag_print_asm_name)
12592 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12593 INSN_ADDRESSES (INSN_UID (insn)));
12597 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12599 unsigned HOST_WIDE_INT mask = 0xff;
12600 int i;
12602 if (val == 0) /* XXX */
12603 return 0;
12605 for (i = 0; i < 25; i++)
12606 if ((val & (mask << i)) == val)
12607 return 1;
12609 return 0;
12612 /* Returns nonzero if the current function contains,
12613 or might contain a far jump. */
12614 static int
12615 thumb_far_jump_used_p (void)
12617 rtx insn;
12619 /* This test is only important for leaf functions. */
12620 /* assert (!leaf_function_p ()); */
12622 /* If we have already decided that far jumps may be used,
12623 do not bother checking again, and always return true even if
12624 it turns out that they are not being used. Once we have made
12625 the decision that far jumps are present (and that hence the link
12626 register will be pushed onto the stack) we cannot go back on it. */
12627 if (cfun->machine->far_jump_used)
12628 return 1;
12630 /* If this function is not being called from the prologue/epilogue
12631 generation code then it must be being called from the
12632 INITIAL_ELIMINATION_OFFSET macro. */
12633 if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12635 /* In this case we know that we are being asked about the elimination
12636 of the arg pointer register. If that register is not being used,
12637 then there are no arguments on the stack, and we do not have to
12638 worry that a far jump might force the prologue to push the link
12639 register, changing the stack offsets. In this case we can just
12640 return false, since the presence of far jumps in the function will
12641 not affect stack offsets.
12643 If the arg pointer is live (or if it was live, but has now been
12644 eliminated and so set to dead) then we do have to test to see if
12645 the function might contain a far jump. This test can lead to some
12646 false negatives, since before reload is completed, then length of
12647 branch instructions is not known, so gcc defaults to returning their
12648 longest length, which in turn sets the far jump attribute to true.
12650 A false negative will not result in bad code being generated, but it
12651 will result in a needless push and pop of the link register. We
12652 hope that this does not occur too often.
12654 If we need doubleword stack alignment this could affect the other
12655 elimination offsets so we can't risk getting it wrong. */
12656 if (regs_ever_live [ARG_POINTER_REGNUM])
12657 cfun->machine->arg_pointer_live = 1;
12658 else if (!cfun->machine->arg_pointer_live)
12659 return 0;
12662 /* Check to see if the function contains a branch
12663 insn with the far jump attribute set. */
12664 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12666 if (GET_CODE (insn) == JUMP_INSN
12667 /* Ignore tablejump patterns. */
12668 && GET_CODE (PATTERN (insn)) != ADDR_VEC
12669 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12670 && get_attr_far_jump (insn) == FAR_JUMP_YES
12673 /* Record the fact that we have decided that
12674 the function does use far jumps. */
12675 cfun->machine->far_jump_used = 1;
12676 return 1;
12680 return 0;
12683 /* Return nonzero if FUNC must be entered in ARM mode. */
12685 is_called_in_ARM_mode (tree func)
12687 if (TREE_CODE (func) != FUNCTION_DECL)
12688 abort ();
12690 /* Ignore the problem about functions whoes address is taken. */
12691 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12692 return TRUE;
12694 #ifdef ARM_PE
12695 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12696 #else
12697 return FALSE;
12698 #endif
12701 /* The bits which aren't usefully expanded as rtl. */
12702 const char *
12703 thumb_unexpanded_epilogue (void)
12705 int regno;
12706 unsigned long live_regs_mask = 0;
12707 int high_regs_pushed = 0;
12708 int had_to_push_lr;
12709 int size;
12710 int mode;
12712 if (return_used_this_function)
12713 return "";
12715 if (IS_NAKED (arm_current_func_type ()))
12716 return "";
12718 live_regs_mask = thumb_compute_save_reg_mask ();
12719 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
12721 /* If we can deduce the registers used from the function's return value.
12722 This is more reliable that examining regs_ever_live[] because that
12723 will be set if the register is ever used in the function, not just if
12724 the register is used to hold a return value. */
12726 if (current_function_return_rtx != 0)
12727 mode = GET_MODE (current_function_return_rtx);
12728 else
12729 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12731 size = GET_MODE_SIZE (mode);
12733 /* The prolog may have pushed some high registers to use as
12734 work registers. e.g. the testsuite file:
12735 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12736 compiles to produce:
12737 push {r4, r5, r6, r7, lr}
12738 mov r7, r9
12739 mov r6, r8
12740 push {r6, r7}
12741 as part of the prolog. We have to undo that pushing here. */
12743 if (high_regs_pushed)
12745 unsigned long mask = live_regs_mask & 0xff;
12746 int next_hi_reg;
12748 /* The available low registers depend on the size of the value we are
12749 returning. */
12750 if (size <= 12)
12751 mask |= 1 << 3;
12752 if (size <= 8)
12753 mask |= 1 << 2;
12755 if (mask == 0)
12756 /* Oh dear! We have no low registers into which we can pop
12757 high registers! */
12758 internal_error
12759 ("no low registers available for popping high registers");
12761 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12762 if (live_regs_mask & (1 << next_hi_reg))
12763 break;
12765 while (high_regs_pushed)
12767 /* Find lo register(s) into which the high register(s) can
12768 be popped. */
12769 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12771 if (mask & (1 << regno))
12772 high_regs_pushed--;
12773 if (high_regs_pushed == 0)
12774 break;
12777 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
12779 /* Pop the values into the low register(s). */
12780 thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
12782 /* Move the value(s) into the high registers. */
12783 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12785 if (mask & (1 << regno))
12787 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12788 regno);
12790 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
12791 if (live_regs_mask & (1 << next_hi_reg))
12792 break;
12796 live_regs_mask &= ~0x0f00;
12799 had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
12800 live_regs_mask &= 0xff;
12802 if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12804 /* Pop the return address into the PC. */
12805 if (had_to_push_lr)
12806 live_regs_mask |= 1 << PC_REGNUM;
12808 /* Either no argument registers were pushed or a backtrace
12809 structure was created which includes an adjusted stack
12810 pointer, so just pop everything. */
12811 if (live_regs_mask)
12812 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12813 live_regs_mask);
12815 /* We have either just popped the return address into the
12816 PC or it is was kept in LR for the entire function. */
12817 if (!had_to_push_lr)
12818 thumb_exit (asm_out_file, LR_REGNUM);
12820 else
12822 /* Pop everything but the return address. */
12823 if (live_regs_mask)
12824 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12825 live_regs_mask);
12827 if (had_to_push_lr)
12829 if (size > 12)
12831 /* We have no free low regs, so save one. */
12832 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
12833 LAST_ARG_REGNUM);
12836 /* Get the return address into a temporary register. */
12837 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
12838 1 << LAST_ARG_REGNUM);
12840 if (size > 12)
12842 /* Move the return address to lr. */
12843 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
12844 LAST_ARG_REGNUM);
12845 /* Restore the low register. */
12846 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
12847 IP_REGNUM);
12848 regno = LR_REGNUM;
12850 else
12851 regno = LAST_ARG_REGNUM;
12853 else
12854 regno = LR_REGNUM;
12856 /* Remove the argument registers that were pushed onto the stack. */
12857 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
12858 SP_REGNUM, SP_REGNUM,
12859 current_function_pretend_args_size);
12861 thumb_exit (asm_out_file, regno);
12864 return "";
12867 /* Functions to save and restore machine-specific function data. */
12868 static struct machine_function *
12869 arm_init_machine_status (void)
12871 struct machine_function *machine;
12872 machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
12874 #if ARM_FT_UNKNOWN != 0
12875 machine->func_type = ARM_FT_UNKNOWN;
12876 #endif
12877 return machine;
12880 /* Return an RTX indicating where the return address to the
12881 calling function can be found. */
12883 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
12885 if (count != 0)
12886 return NULL_RTX;
12888 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
12891 /* Do anything needed before RTL is emitted for each function. */
12892 void
12893 arm_init_expanders (void)
12895 /* Arrange to initialize and mark the machine per-function status. */
12896 init_machine_status = arm_init_machine_status;
12898 /* This is to stop the combine pass optimizing away the alignment
12899 adjustment of va_arg. */
12900 /* ??? It is claimed that this should not be necessary. */
12901 if (cfun)
12902 mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
12906 /* Like arm_compute_initial_elimination offset. Simpler because
12907 THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer. */
12909 HOST_WIDE_INT
12910 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
12912 arm_stack_offsets *offsets;
12914 offsets = arm_get_frame_offsets ();
12916 switch (from)
12918 case ARG_POINTER_REGNUM:
12919 switch (to)
12921 case STACK_POINTER_REGNUM:
12922 return offsets->outgoing_args - offsets->saved_args;
12924 case FRAME_POINTER_REGNUM:
12925 return offsets->soft_frame - offsets->saved_args;
12927 case THUMB_HARD_FRAME_POINTER_REGNUM:
12928 case ARM_HARD_FRAME_POINTER_REGNUM:
12929 return offsets->saved_regs - offsets->saved_args;
12931 default:
12932 abort();
12934 break;
12936 case FRAME_POINTER_REGNUM:
12937 switch (to)
12939 case STACK_POINTER_REGNUM:
12940 return offsets->outgoing_args - offsets->soft_frame;
12942 case THUMB_HARD_FRAME_POINTER_REGNUM:
12943 case ARM_HARD_FRAME_POINTER_REGNUM:
12944 return offsets->saved_regs - offsets->soft_frame;
12946 default:
12947 abort();
12949 break;
12951 default:
12952 abort ();
12957 /* Generate the rest of a function's prologue. */
12958 void
12959 thumb_expand_prologue (void)
12961 rtx insn, dwarf;
12963 HOST_WIDE_INT amount;
12964 arm_stack_offsets *offsets;
12965 unsigned long func_type;
12966 int regno;
12967 unsigned long live_regs_mask;
12969 func_type = arm_current_func_type ();
12971 /* Naked functions don't have prologues. */
12972 if (IS_NAKED (func_type))
12973 return;
12975 if (IS_INTERRUPT (func_type))
12977 error ("interrupt Service Routines cannot be coded in Thumb mode");
12978 return;
12981 live_regs_mask = thumb_compute_save_reg_mask ();
12982 /* Load the pic register before setting the frame pointer,
12983 so we can use r7 as a temporary work register. */
12984 if (flag_pic)
12985 arm_load_pic_register (thumb_find_work_register (live_regs_mask));
12987 offsets = arm_get_frame_offsets ();
12989 if (frame_pointer_needed)
12991 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
12992 stack_pointer_rtx));
12993 RTX_FRAME_RELATED_P (insn) = 1;
12995 else if (CALLER_INTERWORKING_SLOT_SIZE > 0)
12996 emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
12997 stack_pointer_rtx);
12999 amount = offsets->outgoing_args - offsets->saved_regs;
13000 if (amount)
13002 if (amount < 512)
13004 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13005 GEN_INT (- amount)));
13006 RTX_FRAME_RELATED_P (insn) = 1;
13008 else
13010 rtx reg;
13012 /* The stack decrement is too big for an immediate value in a single
13013 insn. In theory we could issue multiple subtracts, but after
13014 three of them it becomes more space efficient to place the full
13015 value in the constant pool and load into a register. (Also the
13016 ARM debugger really likes to see only one stack decrement per
13017 function). So instead we look for a scratch register into which
13018 we can load the decrement, and then we subtract this from the
13019 stack pointer. Unfortunately on the thumb the only available
13020 scratch registers are the argument registers, and we cannot use
13021 these as they may hold arguments to the function. Instead we
13022 attempt to locate a call preserved register which is used by this
13023 function. If we can find one, then we know that it will have
13024 been pushed at the start of the prologue and so we can corrupt
13025 it now. */
13026 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13027 if (live_regs_mask & (1 << regno)
13028 && !(frame_pointer_needed
13029 && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13030 break;
13032 if (regno > LAST_LO_REGNUM) /* Very unlikely. */
13034 rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13036 /* Choose an arbitrary, non-argument low register. */
13037 reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13039 /* Save it by copying it into a high, scratch register. */
13040 emit_insn (gen_movsi (spare, reg));
13041 /* Add a USE to stop propagate_one_insn() from barfing. */
13042 emit_insn (gen_prologue_use (spare));
13044 /* Decrement the stack. */
13045 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13046 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13047 stack_pointer_rtx, reg));
13048 RTX_FRAME_RELATED_P (insn) = 1;
13049 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13050 plus_constant (stack_pointer_rtx,
13051 -amount));
13052 RTX_FRAME_RELATED_P (dwarf) = 1;
13053 REG_NOTES (insn)
13054 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13055 REG_NOTES (insn));
13057 /* Restore the low register's original value. */
13058 emit_insn (gen_movsi (reg, spare));
13060 /* Emit a USE of the restored scratch register, so that flow
13061 analysis will not consider the restore redundant. The
13062 register won't be used again in this function and isn't
13063 restored by the epilogue. */
13064 emit_insn (gen_prologue_use (reg));
13066 else
13068 reg = gen_rtx_REG (SImode, regno);
13070 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13072 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13073 stack_pointer_rtx, reg));
13074 RTX_FRAME_RELATED_P (insn) = 1;
13075 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13076 plus_constant (stack_pointer_rtx,
13077 -amount));
13078 RTX_FRAME_RELATED_P (dwarf) = 1;
13079 REG_NOTES (insn)
13080 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13081 REG_NOTES (insn));
13084 /* If the frame pointer is needed, emit a special barrier that
13085 will prevent the scheduler from moving stores to the frame
13086 before the stack adjustment. */
13087 if (frame_pointer_needed)
13088 emit_insn (gen_stack_tie (stack_pointer_rtx,
13089 hard_frame_pointer_rtx));
13092 if (current_function_profile || TARGET_NO_SCHED_PRO)
13093 emit_insn (gen_blockage ());
13095 cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13096 if (live_regs_mask & 0xff)
13097 cfun->machine->lr_save_eliminated = 0;
13099 /* If the link register is being kept alive, with the return address in it,
13100 then make sure that it does not get reused by the ce2 pass. */
13101 if (cfun->machine->lr_save_eliminated)
13102 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13106 void
13107 thumb_expand_epilogue (void)
13109 HOST_WIDE_INT amount;
13110 arm_stack_offsets *offsets;
13111 int regno;
13113 /* Naked functions don't have prologues. */
13114 if (IS_NAKED (arm_current_func_type ()))
13115 return;
13117 offsets = arm_get_frame_offsets ();
13118 amount = offsets->outgoing_args - offsets->saved_regs;
13120 if (frame_pointer_needed)
13121 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13122 else if (amount)
13124 if (amount < 512)
13125 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13126 GEN_INT (amount)));
13127 else
13129 /* r3 is always free in the epilogue. */
13130 rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13132 emit_insn (gen_movsi (reg, GEN_INT (amount)));
13133 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13137 /* Emit a USE (stack_pointer_rtx), so that
13138 the stack adjustment will not be deleted. */
13139 emit_insn (gen_prologue_use (stack_pointer_rtx));
13141 if (current_function_profile || TARGET_NO_SCHED_PRO)
13142 emit_insn (gen_blockage ());
13144 /* Emit a clobber for each insn that will be restored in the epilogue,
13145 so that flow2 will get register lifetimes correct. */
13146 for (regno = 0; regno < 13; regno++)
13147 if (regs_ever_live[regno] && !call_used_regs[regno])
13148 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13150 if (! regs_ever_live[LR_REGNUM])
13151 emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13154 static void
13155 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13157 unsigned long live_regs_mask = 0;
13158 unsigned long l_mask;
13159 unsigned high_regs_pushed = 0;
13160 int cfa_offset = 0;
13161 int regno;
13163 if (IS_NAKED (arm_current_func_type ()))
13164 return;
13166 if (is_called_in_ARM_mode (current_function_decl))
13168 const char * name;
13170 if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
13171 abort ();
13172 if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
13173 abort ();
13174 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
13176 /* Generate code sequence to switch us into Thumb mode. */
13177 /* The .code 32 directive has already been emitted by
13178 ASM_DECLARE_FUNCTION_NAME. */
13179 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13180 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13182 /* Generate a label, so that the debugger will notice the
13183 change in instruction sets. This label is also used by
13184 the assembler to bypass the ARM code when this function
13185 is called from a Thumb encoded function elsewhere in the
13186 same file. Hence the definition of STUB_NAME here must
13187 agree with the definition in gas/config/tc-arm.c. */
13189 #define STUB_NAME ".real_start_of"
13191 fprintf (f, "\t.code\t16\n");
13192 #ifdef ARM_PE
13193 if (arm_dllexport_name_p (name))
13194 name = arm_strip_name_encoding (name);
13195 #endif
13196 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13197 fprintf (f, "\t.thumb_func\n");
13198 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13201 if (current_function_pretend_args_size)
13203 if (cfun->machine->uses_anonymous_args)
13205 int num_pushes;
13207 fprintf (f, "\tpush\t{");
13209 num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13211 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13212 regno <= LAST_ARG_REGNUM;
13213 regno++)
13214 asm_fprintf (f, "%r%s", regno,
13215 regno == LAST_ARG_REGNUM ? "" : ", ");
13217 fprintf (f, "}\n");
13219 else
13220 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13221 SP_REGNUM, SP_REGNUM,
13222 current_function_pretend_args_size);
13224 /* We don't need to record the stores for unwinding (would it
13225 help the debugger any if we did?), but record the change in
13226 the stack pointer. */
13227 if (dwarf2out_do_frame ())
13229 char *l = dwarf2out_cfi_label ();
13231 cfa_offset = cfa_offset + current_function_pretend_args_size;
13232 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13236 /* Get the registers we are going to push. */
13237 live_regs_mask = thumb_compute_save_reg_mask ();
13238 /* Extract a mask of the ones we can give to the Thumb's push instruction. */
13239 l_mask = live_regs_mask & 0x40ff;
13240 /* Then count how many other high registers will need to be pushed. */
13241 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13243 if (TARGET_BACKTRACE)
13245 unsigned offset;
13246 unsigned work_register;
13248 /* We have been asked to create a stack backtrace structure.
13249 The code looks like this:
13251 0 .align 2
13252 0 func:
13253 0 sub SP, #16 Reserve space for 4 registers.
13254 2 push {R7} Push low registers.
13255 4 add R7, SP, #20 Get the stack pointer before the push.
13256 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
13257 8 mov R7, PC Get hold of the start of this code plus 12.
13258 10 str R7, [SP, #16] Store it.
13259 12 mov R7, FP Get hold of the current frame pointer.
13260 14 str R7, [SP, #4] Store it.
13261 16 mov R7, LR Get hold of the current return address.
13262 18 str R7, [SP, #12] Store it.
13263 20 add R7, SP, #16 Point at the start of the backtrace structure.
13264 22 mov FP, R7 Put this value into the frame pointer. */
13266 work_register = thumb_find_work_register (live_regs_mask);
13268 asm_fprintf
13269 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13270 SP_REGNUM, SP_REGNUM);
13272 if (dwarf2out_do_frame ())
13274 char *l = dwarf2out_cfi_label ();
13276 cfa_offset = cfa_offset + 16;
13277 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13280 if (l_mask)
13282 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13283 offset = bit_count (l_mask);
13285 else
13286 offset = 0;
13288 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13289 offset + 16 + current_function_pretend_args_size);
13291 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13292 offset + 4);
13294 /* Make sure that the instruction fetching the PC is in the right place
13295 to calculate "start of backtrace creation code + 12". */
13296 if (l_mask)
13298 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13299 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13300 offset + 12);
13301 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13302 ARM_HARD_FRAME_POINTER_REGNUM);
13303 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13304 offset);
13306 else
13308 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13309 ARM_HARD_FRAME_POINTER_REGNUM);
13310 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13311 offset);
13312 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13313 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13314 offset + 12);
13317 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13318 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13319 offset + 8);
13320 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13321 offset + 12);
13322 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13323 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13325 /* Optimisation: If we are not pushing any low registers but we are going
13326 to push some high registers then delay our first push. This will just
13327 be a push of LR and we can combine it with the push of the first high
13328 register. */
13329 else if ((l_mask & 0xff) != 0
13330 || (high_regs_pushed == 0 && l_mask))
13331 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13333 if (high_regs_pushed)
13335 unsigned pushable_regs;
13336 unsigned next_hi_reg;
13338 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13339 if (live_regs_mask & (1 << next_hi_reg))
13340 break;
13342 pushable_regs = l_mask & 0xff;
13344 if (pushable_regs == 0)
13345 pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
13347 while (high_regs_pushed > 0)
13349 unsigned long real_regs_mask = 0;
13351 for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
13353 if (pushable_regs & (1 << regno))
13355 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13357 high_regs_pushed --;
13358 real_regs_mask |= (1 << next_hi_reg);
13360 if (high_regs_pushed)
13362 for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
13363 next_hi_reg --)
13364 if (live_regs_mask & (1 << next_hi_reg))
13365 break;
13367 else
13369 pushable_regs &= ~((1 << regno) - 1);
13370 break;
13375 /* If we had to find a work register and we have not yet
13376 saved the LR then add it to the list of regs to push. */
13377 if (l_mask == (1 << LR_REGNUM))
13379 thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
13380 1, &cfa_offset,
13381 real_regs_mask | (1 << LR_REGNUM));
13382 l_mask = 0;
13384 else
13385 thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
13390 /* Handle the case of a double word load into a low register from
13391 a computed memory address. The computed address may involve a
13392 register which is overwritten by the load. */
13393 const char *
13394 thumb_load_double_from_address (rtx *operands)
13396 rtx addr;
13397 rtx base;
13398 rtx offset;
13399 rtx arg1;
13400 rtx arg2;
13402 if (GET_CODE (operands[0]) != REG)
13403 abort ();
13405 if (GET_CODE (operands[1]) != MEM)
13406 abort ();
13408 /* Get the memory address. */
13409 addr = XEXP (operands[1], 0);
13411 /* Work out how the memory address is computed. */
13412 switch (GET_CODE (addr))
13414 case REG:
13415 operands[2] = gen_rtx_MEM (SImode,
13416 plus_constant (XEXP (operands[1], 0), 4));
13418 if (REGNO (operands[0]) == REGNO (addr))
13420 output_asm_insn ("ldr\t%H0, %2", operands);
13421 output_asm_insn ("ldr\t%0, %1", operands);
13423 else
13425 output_asm_insn ("ldr\t%0, %1", operands);
13426 output_asm_insn ("ldr\t%H0, %2", operands);
13428 break;
13430 case CONST:
13431 /* Compute <address> + 4 for the high order load. */
13432 operands[2] = gen_rtx_MEM (SImode,
13433 plus_constant (XEXP (operands[1], 0), 4));
13435 output_asm_insn ("ldr\t%0, %1", operands);
13436 output_asm_insn ("ldr\t%H0, %2", operands);
13437 break;
13439 case PLUS:
13440 arg1 = XEXP (addr, 0);
13441 arg2 = XEXP (addr, 1);
13443 if (CONSTANT_P (arg1))
13444 base = arg2, offset = arg1;
13445 else
13446 base = arg1, offset = arg2;
13448 if (GET_CODE (base) != REG)
13449 abort ();
13451 /* Catch the case of <address> = <reg> + <reg> */
13452 if (GET_CODE (offset) == REG)
13454 int reg_offset = REGNO (offset);
13455 int reg_base = REGNO (base);
13456 int reg_dest = REGNO (operands[0]);
13458 /* Add the base and offset registers together into the
13459 higher destination register. */
13460 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13461 reg_dest + 1, reg_base, reg_offset);
13463 /* Load the lower destination register from the address in
13464 the higher destination register. */
13465 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13466 reg_dest, reg_dest + 1);
13468 /* Load the higher destination register from its own address
13469 plus 4. */
13470 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13471 reg_dest + 1, reg_dest + 1);
13473 else
13475 /* Compute <address> + 4 for the high order load. */
13476 operands[2] = gen_rtx_MEM (SImode,
13477 plus_constant (XEXP (operands[1], 0), 4));
13479 /* If the computed address is held in the low order register
13480 then load the high order register first, otherwise always
13481 load the low order register first. */
13482 if (REGNO (operands[0]) == REGNO (base))
13484 output_asm_insn ("ldr\t%H0, %2", operands);
13485 output_asm_insn ("ldr\t%0, %1", operands);
13487 else
13489 output_asm_insn ("ldr\t%0, %1", operands);
13490 output_asm_insn ("ldr\t%H0, %2", operands);
13493 break;
13495 case LABEL_REF:
13496 /* With no registers to worry about we can just load the value
13497 directly. */
13498 operands[2] = gen_rtx_MEM (SImode,
13499 plus_constant (XEXP (operands[1], 0), 4));
13501 output_asm_insn ("ldr\t%H0, %2", operands);
13502 output_asm_insn ("ldr\t%0, %1", operands);
13503 break;
13505 default:
13506 abort ();
13507 break;
13510 return "";
13513 const char *
13514 thumb_output_move_mem_multiple (int n, rtx *operands)
13516 rtx tmp;
13518 switch (n)
13520 case 2:
13521 if (REGNO (operands[4]) > REGNO (operands[5]))
13523 tmp = operands[4];
13524 operands[4] = operands[5];
13525 operands[5] = tmp;
13527 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13528 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13529 break;
13531 case 3:
13532 if (REGNO (operands[4]) > REGNO (operands[5]))
13534 tmp = operands[4];
13535 operands[4] = operands[5];
13536 operands[5] = tmp;
13538 if (REGNO (operands[5]) > REGNO (operands[6]))
13540 tmp = operands[5];
13541 operands[5] = operands[6];
13542 operands[6] = tmp;
13544 if (REGNO (operands[4]) > REGNO (operands[5]))
13546 tmp = operands[4];
13547 operands[4] = operands[5];
13548 operands[5] = tmp;
13551 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13552 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13553 break;
13555 default:
13556 abort ();
13559 return "";
13562 /* Output a call-via instruction for thumb state. */
13563 const char *
13564 thumb_call_via_reg (rtx reg)
13566 int regno = REGNO (reg);
13567 rtx *labelp;
13569 gcc_assert (regno < LR_REGNUM);
13571 /* If we are in the normal text section we can use a single instance
13572 per compilation unit. If we are doing function sections, then we need
13573 an entry per section, since we can't rely on reachability. */
13574 if (in_text_section ())
13576 thumb_call_reg_needed = 1;
13578 if (thumb_call_via_label[regno] == NULL)
13579 thumb_call_via_label[regno] = gen_label_rtx ();
13580 labelp = thumb_call_via_label + regno;
13582 else
13584 if (cfun->machine->call_via[regno] == NULL)
13585 cfun->machine->call_via[regno] = gen_label_rtx ();
13586 labelp = cfun->machine->call_via + regno;
13589 output_asm_insn ("bl\t%a0", labelp);
13590 return "";
13593 /* Routines for generating rtl. */
13594 void
13595 thumb_expand_movmemqi (rtx *operands)
13597 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13598 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13599 HOST_WIDE_INT len = INTVAL (operands[2]);
13600 HOST_WIDE_INT offset = 0;
13602 while (len >= 12)
13604 emit_insn (gen_movmem12b (out, in, out, in));
13605 len -= 12;
13608 if (len >= 8)
13610 emit_insn (gen_movmem8b (out, in, out, in));
13611 len -= 8;
13614 if (len >= 4)
13616 rtx reg = gen_reg_rtx (SImode);
13617 emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13618 emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13619 len -= 4;
13620 offset += 4;
13623 if (len >= 2)
13625 rtx reg = gen_reg_rtx (HImode);
13626 emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13627 plus_constant (in, offset))));
13628 emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13629 reg));
13630 len -= 2;
13631 offset += 2;
13634 if (len)
13636 rtx reg = gen_reg_rtx (QImode);
13637 emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13638 plus_constant (in, offset))));
13639 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13640 reg));
13644 void
13645 thumb_reload_out_hi (rtx *operands)
13647 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13650 /* Handle reading a half-word from memory during reload. */
13651 void
13652 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13654 abort ();
13657 /* Return the length of a function name prefix
13658 that starts with the character 'c'. */
13659 static int
13660 arm_get_strip_length (int c)
13662 switch (c)
13664 ARM_NAME_ENCODING_LENGTHS
13665 default: return 0;
13669 /* Return a pointer to a function's name with any
13670 and all prefix encodings stripped from it. */
13671 const char *
13672 arm_strip_name_encoding (const char *name)
13674 int skip;
13676 while ((skip = arm_get_strip_length (* name)))
13677 name += skip;
13679 return name;
13682 /* If there is a '*' anywhere in the name's prefix, then
13683 emit the stripped name verbatim, otherwise prepend an
13684 underscore if leading underscores are being used. */
13685 void
13686 arm_asm_output_labelref (FILE *stream, const char *name)
13688 int skip;
13689 int verbatim = 0;
13691 while ((skip = arm_get_strip_length (* name)))
13693 verbatim |= (*name == '*');
13694 name += skip;
13697 if (verbatim)
13698 fputs (name, stream);
13699 else
13700 asm_fprintf (stream, "%U%s", name);
13703 static void
13704 arm_file_end (void)
13706 int regno;
13708 if (! thumb_call_reg_needed)
13709 return;
13711 text_section ();
13712 asm_fprintf (asm_out_file, "\t.code 16\n");
13713 ASM_OUTPUT_ALIGN (asm_out_file, 1);
13715 for (regno = 0; regno < LR_REGNUM; regno++)
13717 rtx label = thumb_call_via_label[regno];
13719 if (label != 0)
13721 targetm.asm_out.internal_label (asm_out_file, "L",
13722 CODE_LABEL_NUMBER (label));
13723 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
13728 rtx aof_pic_label;
13730 #ifdef AOF_ASSEMBLER
13731 /* Special functions only needed when producing AOF syntax assembler. */
13733 struct pic_chain
13735 struct pic_chain * next;
13736 const char * symname;
13739 static struct pic_chain * aof_pic_chain = NULL;
13742 aof_pic_entry (rtx x)
13744 struct pic_chain ** chainp;
13745 int offset;
13747 if (aof_pic_label == NULL_RTX)
13749 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13752 for (offset = 0, chainp = &aof_pic_chain; *chainp;
13753 offset += 4, chainp = &(*chainp)->next)
13754 if ((*chainp)->symname == XSTR (x, 0))
13755 return plus_constant (aof_pic_label, offset);
13757 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13758 (*chainp)->next = NULL;
13759 (*chainp)->symname = XSTR (x, 0);
13760 return plus_constant (aof_pic_label, offset);
13763 void
13764 aof_dump_pic_table (FILE *f)
13766 struct pic_chain * chain;
13768 if (aof_pic_chain == NULL)
13769 return;
13771 asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13772 PIC_OFFSET_TABLE_REGNUM,
13773 PIC_OFFSET_TABLE_REGNUM);
13774 fputs ("|x$adcons|\n", f);
13776 for (chain = aof_pic_chain; chain; chain = chain->next)
13778 fputs ("\tDCD\t", f);
13779 assemble_name (f, chain->symname);
13780 fputs ("\n", f);
13784 int arm_text_section_count = 1;
13786 char *
13787 aof_text_section (void )
13789 static char buf[100];
13790 sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13791 arm_text_section_count++);
13792 if (flag_pic)
13793 strcat (buf, ", PIC, REENTRANT");
13794 return buf;
13797 static int arm_data_section_count = 1;
13799 char *
13800 aof_data_section (void)
13802 static char buf[100];
13803 sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13804 return buf;
13807 /* The AOF assembler is religiously strict about declarations of
13808 imported and exported symbols, so that it is impossible to declare
13809 a function as imported near the beginning of the file, and then to
13810 export it later on. It is, however, possible to delay the decision
13811 until all the functions in the file have been compiled. To get
13812 around this, we maintain a list of the imports and exports, and
13813 delete from it any that are subsequently defined. At the end of
13814 compilation we spit the remainder of the list out before the END
13815 directive. */
13817 struct import
13819 struct import * next;
13820 const char * name;
13823 static struct import * imports_list = NULL;
13825 void
13826 aof_add_import (const char *name)
13828 struct import * new;
13830 for (new = imports_list; new; new = new->next)
13831 if (new->name == name)
13832 return;
13834 new = (struct import *) xmalloc (sizeof (struct import));
13835 new->next = imports_list;
13836 imports_list = new;
13837 new->name = name;
13840 void
13841 aof_delete_import (const char *name)
13843 struct import ** old;
13845 for (old = &imports_list; *old; old = & (*old)->next)
13847 if ((*old)->name == name)
13849 *old = (*old)->next;
13850 return;
13855 int arm_main_function = 0;
13857 static void
13858 aof_dump_imports (FILE *f)
13860 /* The AOF assembler needs this to cause the startup code to be extracted
13861 from the library. Brining in __main causes the whole thing to work
13862 automagically. */
13863 if (arm_main_function)
13865 text_section ();
13866 fputs ("\tIMPORT __main\n", f);
13867 fputs ("\tDCD __main\n", f);
13870 /* Now dump the remaining imports. */
13871 while (imports_list)
13873 fprintf (f, "\tIMPORT\t");
13874 assemble_name (f, imports_list->name);
13875 fputc ('\n', f);
13876 imports_list = imports_list->next;
13880 static void
13881 aof_globalize_label (FILE *stream, const char *name)
13883 default_globalize_label (stream, name);
13884 if (! strcmp (name, "main"))
13885 arm_main_function = 1;
13888 static void
13889 aof_file_start (void)
13891 fputs ("__r0\tRN\t0\n", asm_out_file);
13892 fputs ("__a1\tRN\t0\n", asm_out_file);
13893 fputs ("__a2\tRN\t1\n", asm_out_file);
13894 fputs ("__a3\tRN\t2\n", asm_out_file);
13895 fputs ("__a4\tRN\t3\n", asm_out_file);
13896 fputs ("__v1\tRN\t4\n", asm_out_file);
13897 fputs ("__v2\tRN\t5\n", asm_out_file);
13898 fputs ("__v3\tRN\t6\n", asm_out_file);
13899 fputs ("__v4\tRN\t7\n", asm_out_file);
13900 fputs ("__v5\tRN\t8\n", asm_out_file);
13901 fputs ("__v6\tRN\t9\n", asm_out_file);
13902 fputs ("__sl\tRN\t10\n", asm_out_file);
13903 fputs ("__fp\tRN\t11\n", asm_out_file);
13904 fputs ("__ip\tRN\t12\n", asm_out_file);
13905 fputs ("__sp\tRN\t13\n", asm_out_file);
13906 fputs ("__lr\tRN\t14\n", asm_out_file);
13907 fputs ("__pc\tRN\t15\n", asm_out_file);
13908 fputs ("__f0\tFN\t0\n", asm_out_file);
13909 fputs ("__f1\tFN\t1\n", asm_out_file);
13910 fputs ("__f2\tFN\t2\n", asm_out_file);
13911 fputs ("__f3\tFN\t3\n", asm_out_file);
13912 fputs ("__f4\tFN\t4\n", asm_out_file);
13913 fputs ("__f5\tFN\t5\n", asm_out_file);
13914 fputs ("__f6\tFN\t6\n", asm_out_file);
13915 fputs ("__f7\tFN\t7\n", asm_out_file);
13916 text_section ();
13919 static void
13920 aof_file_end (void)
13922 if (flag_pic)
13923 aof_dump_pic_table (asm_out_file);
13924 arm_file_end ();
13925 aof_dump_imports (asm_out_file);
13926 fputs ("\tEND\n", asm_out_file);
13928 #endif /* AOF_ASSEMBLER */
13930 #ifndef ARM_PE
13931 /* Symbols in the text segment can be accessed without indirecting via the
13932 constant pool; it may take an extra binary operation, but this is still
13933 faster than indirecting via memory. Don't do this when not optimizing,
13934 since we won't be calculating al of the offsets necessary to do this
13935 simplification. */
13937 static void
13938 arm_encode_section_info (tree decl, rtx rtl, int first)
13940 /* This doesn't work with AOF syntax, since the string table may be in
13941 a different AREA. */
13942 #ifndef AOF_ASSEMBLER
13943 if (optimize > 0 && TREE_CONSTANT (decl))
13944 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
13945 #endif
13947 /* If we are referencing a function that is weak then encode a long call
13948 flag in the function name, otherwise if the function is static or
13949 or known to be defined in this file then encode a short call flag. */
13950 if (first && DECL_P (decl))
13952 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
13953 arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
13954 else if (! TREE_PUBLIC (decl))
13955 arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
13958 #endif /* !ARM_PE */
13960 static void
13961 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
13963 if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
13964 && !strcmp (prefix, "L"))
13966 arm_ccfsm_state = 0;
13967 arm_target_insn = NULL;
13969 default_internal_label (stream, prefix, labelno);
13972 /* Output code to add DELTA to the first argument, and then jump
13973 to FUNCTION. Used for C++ multiple inheritance. */
13974 static void
13975 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
13976 HOST_WIDE_INT delta,
13977 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
13978 tree function)
13980 static int thunk_label = 0;
13981 char label[256];
13982 int mi_delta = delta;
13983 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
13984 int shift = 0;
13985 int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
13986 ? 1 : 0);
13987 if (mi_delta < 0)
13988 mi_delta = - mi_delta;
13989 if (TARGET_THUMB)
13991 int labelno = thunk_label++;
13992 ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
13993 fputs ("\tldr\tr12, ", file);
13994 assemble_name (file, label);
13995 fputc ('\n', file);
13997 while (mi_delta != 0)
13999 if ((mi_delta & (3 << shift)) == 0)
14000 shift += 2;
14001 else
14003 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14004 mi_op, this_regno, this_regno,
14005 mi_delta & (0xff << shift));
14006 mi_delta &= ~(0xff << shift);
14007 shift += 8;
14010 if (TARGET_THUMB)
14012 fprintf (file, "\tbx\tr12\n");
14013 ASM_OUTPUT_ALIGN (file, 2);
14014 assemble_name (file, label);
14015 fputs (":\n", file);
14016 assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14018 else
14020 fputs ("\tb\t", file);
14021 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14022 if (NEED_PLT_RELOC)
14023 fputs ("(PLT)", file);
14024 fputc ('\n', file);
14029 arm_emit_vector_const (FILE *file, rtx x)
14031 int i;
14032 const char * pattern;
14034 if (GET_CODE (x) != CONST_VECTOR)
14035 abort ();
14037 switch (GET_MODE (x))
14039 case V2SImode: pattern = "%08x"; break;
14040 case V4HImode: pattern = "%04x"; break;
14041 case V8QImode: pattern = "%02x"; break;
14042 default: abort ();
14045 fprintf (file, "0x");
14046 for (i = CONST_VECTOR_NUNITS (x); i--;)
14048 rtx element;
14050 element = CONST_VECTOR_ELT (x, i);
14051 fprintf (file, pattern, INTVAL (element));
14054 return 1;
14057 const char *
14058 arm_output_load_gr (rtx *operands)
14060 rtx reg;
14061 rtx offset;
14062 rtx wcgr;
14063 rtx sum;
14065 if (GET_CODE (operands [1]) != MEM
14066 || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14067 || GET_CODE (reg = XEXP (sum, 0)) != REG
14068 || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14069 || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14070 return "wldrw%?\t%0, %1";
14072 /* Fix up an out-of-range load of a GR register. */
14073 output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14074 wcgr = operands[0];
14075 operands[0] = reg;
14076 output_asm_insn ("ldr%?\t%0, %1", operands);
14078 operands[0] = wcgr;
14079 operands[1] = reg;
14080 output_asm_insn ("tmcr%?\t%0, %1", operands);
14081 output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14083 return "";
14086 static rtx
14087 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14088 int incoming ATTRIBUTE_UNUSED)
14090 #if 0
14091 /* FIXME: The ARM backend has special code to handle structure
14092 returns, and will reserve its own hidden first argument. So
14093 if this macro is enabled a *second* hidden argument will be
14094 reserved, which will break binary compatibility with old
14095 toolchains and also thunk handling. One day this should be
14096 fixed. */
14097 return 0;
14098 #else
14099 /* Register in which address to store a structure value
14100 is passed to a function. */
14101 return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14102 #endif
14105 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14107 On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14108 named arg and all anonymous args onto the stack.
14109 XXX I know the prologue shouldn't be pushing registers, but it is faster
14110 that way. */
14112 static void
14113 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14114 enum machine_mode mode ATTRIBUTE_UNUSED,
14115 tree type ATTRIBUTE_UNUSED,
14116 int *pretend_size,
14117 int second_time ATTRIBUTE_UNUSED)
14119 cfun->machine->uses_anonymous_args = 1;
14120 if (cum->nregs < NUM_ARG_REGS)
14121 *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14124 /* Return nonzero if the CONSUMER instruction (a store) does not need
14125 PRODUCER's value to calculate the address. */
14128 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14130 rtx value = PATTERN (producer);
14131 rtx addr = PATTERN (consumer);
14133 if (GET_CODE (value) == COND_EXEC)
14134 value = COND_EXEC_CODE (value);
14135 if (GET_CODE (value) == PARALLEL)
14136 value = XVECEXP (value, 0, 0);
14137 value = XEXP (value, 0);
14138 if (GET_CODE (addr) == COND_EXEC)
14139 addr = COND_EXEC_CODE (addr);
14140 if (GET_CODE (addr) == PARALLEL)
14141 addr = XVECEXP (addr, 0, 0);
14142 addr = XEXP (addr, 0);
14144 return !reg_overlap_mentioned_p (value, addr);
14147 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14148 have an early register shift value or amount dependency on the
14149 result of PRODUCER. */
14152 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14154 rtx value = PATTERN (producer);
14155 rtx op = PATTERN (consumer);
14156 rtx early_op;
14158 if (GET_CODE (value) == COND_EXEC)
14159 value = COND_EXEC_CODE (value);
14160 if (GET_CODE (value) == PARALLEL)
14161 value = XVECEXP (value, 0, 0);
14162 value = XEXP (value, 0);
14163 if (GET_CODE (op) == COND_EXEC)
14164 op = COND_EXEC_CODE (op);
14165 if (GET_CODE (op) == PARALLEL)
14166 op = XVECEXP (op, 0, 0);
14167 op = XEXP (op, 1);
14169 early_op = XEXP (op, 0);
14170 /* This is either an actual independent shift, or a shift applied to
14171 the first operand of another operation. We want the whole shift
14172 operation. */
14173 if (GET_CODE (early_op) == REG)
14174 early_op = op;
14176 return !reg_overlap_mentioned_p (value, early_op);
14179 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14180 have an early register shift value dependency on the result of
14181 PRODUCER. */
14184 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14186 rtx value = PATTERN (producer);
14187 rtx op = PATTERN (consumer);
14188 rtx early_op;
14190 if (GET_CODE (value) == COND_EXEC)
14191 value = COND_EXEC_CODE (value);
14192 if (GET_CODE (value) == PARALLEL)
14193 value = XVECEXP (value, 0, 0);
14194 value = XEXP (value, 0);
14195 if (GET_CODE (op) == COND_EXEC)
14196 op = COND_EXEC_CODE (op);
14197 if (GET_CODE (op) == PARALLEL)
14198 op = XVECEXP (op, 0, 0);
14199 op = XEXP (op, 1);
14201 early_op = XEXP (op, 0);
14203 /* This is either an actual independent shift, or a shift applied to
14204 the first operand of another operation. We want the value being
14205 shifted, in either case. */
14206 if (GET_CODE (early_op) != REG)
14207 early_op = XEXP (early_op, 0);
14209 return !reg_overlap_mentioned_p (value, early_op);
14212 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14213 have an early register mult dependency on the result of
14214 PRODUCER. */
14217 arm_no_early_mul_dep (rtx producer, rtx consumer)
14219 rtx value = PATTERN (producer);
14220 rtx op = PATTERN (consumer);
14222 if (GET_CODE (value) == COND_EXEC)
14223 value = COND_EXEC_CODE (value);
14224 if (GET_CODE (value) == PARALLEL)
14225 value = XVECEXP (value, 0, 0);
14226 value = XEXP (value, 0);
14227 if (GET_CODE (op) == COND_EXEC)
14228 op = COND_EXEC_CODE (op);
14229 if (GET_CODE (op) == PARALLEL)
14230 op = XVECEXP (op, 0, 0);
14231 op = XEXP (op, 1);
14233 return (GET_CODE (op) == PLUS
14234 && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14238 /* We can't rely on the caller doing the proper promotion when
14239 using APCS or ATPCS. */
14241 static bool
14242 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14244 return !TARGET_AAPCS_BASED;
14248 /* AAPCS based ABIs use short enums by default. */
14250 static bool
14251 arm_default_short_enums (void)
14253 return TARGET_AAPCS_BASED;
14257 /* AAPCS requires that anonymous bitfields affect structure alignment. */
14259 static bool
14260 arm_align_anon_bitfield (void)
14262 return TARGET_AAPCS_BASED;
14266 /* The generic C++ ABI says 64-bit (long long). The EABI says 32-bit. */
14268 static tree
14269 arm_cxx_guard_type (void)
14271 return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14275 /* The EABI says test the least significan bit of a guard variable. */
14277 static bool
14278 arm_cxx_guard_mask_bit (void)
14280 return TARGET_AAPCS_BASED;
14284 /* The EABI specifies that all array cookies are 8 bytes long. */
14286 static tree
14287 arm_get_cookie_size (tree type)
14289 tree size;
14291 if (!TARGET_AAPCS_BASED)
14292 return default_cxx_get_cookie_size (type);
14294 size = build_int_cst (sizetype, 8);
14295 return size;
14299 /* The EABI says that array cookies should also contain the element size. */
14301 static bool
14302 arm_cookie_has_size (void)
14304 return TARGET_AAPCS_BASED;
14308 /* The EABI says constructors and destructors should return a pointer to
14309 the object constructed/destroyed. */
14311 static bool
14312 arm_cxx_cdtor_returns_this (void)
14314 return TARGET_AAPCS_BASED;
14317 /* The EABI says that an inline function may never be the key
14318 method. */
14320 static bool
14321 arm_cxx_key_method_may_be_inline (void)
14323 return !TARGET_AAPCS_BASED;
14326 static void
14327 arm_cxx_determine_class_data_visibility (tree decl)
14329 if (!TARGET_AAPCS_BASED)
14330 return;
14332 /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
14333 is exported. However, on systems without dynamic vague linkage,
14334 \S 3.2.5.6 says that COMDAT class data has hidden linkage. */
14335 if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
14336 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
14337 else
14338 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
14339 DECL_VISIBILITY_SPECIFIED (decl) = 1;
14342 static bool
14343 arm_cxx_class_data_always_comdat (void)
14345 /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
14346 vague linkage if the class has no key function. */
14347 return !TARGET_AAPCS_BASED;
14350 void
14351 arm_set_return_address (rtx source, rtx scratch)
14353 arm_stack_offsets *offsets;
14354 HOST_WIDE_INT delta;
14355 rtx addr;
14356 unsigned long saved_regs;
14358 saved_regs = arm_compute_save_reg_mask ();
14360 if ((saved_regs & (1 << LR_REGNUM)) == 0)
14361 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14362 else
14364 if (frame_pointer_needed)
14365 addr = plus_constant(hard_frame_pointer_rtx, -4);
14366 else
14368 /* LR will be the first saved register. */
14369 offsets = arm_get_frame_offsets ();
14370 delta = offsets->outgoing_args - (offsets->frame + 4);
14373 if (delta >= 4096)
14375 emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
14376 GEN_INT (delta & ~4095)));
14377 addr = scratch;
14378 delta &= 4095;
14380 else
14381 addr = stack_pointer_rtx;
14383 addr = plus_constant (addr, delta);
14385 emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14390 void
14391 thumb_set_return_address (rtx source, rtx scratch)
14393 arm_stack_offsets *offsets;
14394 HOST_WIDE_INT delta;
14395 int reg;
14396 rtx addr;
14397 unsigned long mask;
14399 emit_insn (gen_rtx_USE (VOIDmode, source));
14401 mask = thumb_compute_save_reg_mask ();
14402 if (mask & (1 << LR_REGNUM))
14404 offsets = arm_get_frame_offsets ();
14406 /* Find the saved regs. */
14407 if (frame_pointer_needed)
14409 delta = offsets->soft_frame - offsets->saved_args;
14410 reg = THUMB_HARD_FRAME_POINTER_REGNUM;
14412 else
14414 delta = offsets->outgoing_args - offsets->saved_args;
14415 reg = SP_REGNUM;
14417 /* Allow for the stack frame. */
14418 if (TARGET_BACKTRACE)
14419 delta -= 16;
14420 /* The link register is always the first saved register. */
14421 delta -= 4;
14423 /* Construct the address. */
14424 addr = gen_rtx_REG (SImode, reg);
14425 if ((reg != SP_REGNUM && delta >= 128)
14426 || delta >= 1024)
14428 emit_insn (gen_movsi (scratch, GEN_INT (delta)));
14429 emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
14430 addr = scratch;
14432 else
14433 addr = plus_constant (addr, delta);
14435 emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14437 else
14438 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14441 /* Implements target hook vector_mode_supported_p. */
14442 bool
14443 arm_vector_mode_supported_p (enum machine_mode mode)
14445 if ((mode == V2SImode)
14446 || (mode == V4HImode)
14447 || (mode == V8QImode))
14448 return true;
14450 return false;
14453 /* Implement TARGET_SHIFT_TRUNCATION_MASK. SImode shifts use normal
14454 ARM insns and therefore guarantee that the shift count is modulo 256.
14455 DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
14456 guarantee no particular behavior for out-of-range counts. */
14458 static unsigned HOST_WIDE_INT
14459 arm_shift_truncation_mask (enum machine_mode mode)
14461 return mode == SImode ? 255 : 0;
14465 /* Map internal gcc register numbers to DWARF2 register numbers. */
14467 unsigned int
14468 arm_dbx_register_number (unsigned int regno)
14470 if (regno < 16)
14471 return regno;
14473 /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
14474 compatibility. The EABI defines them as registers 96-103. */
14475 if (IS_FPA_REGNUM (regno))
14476 return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
14478 if (IS_VFP_REGNUM (regno))
14479 return 64 + regno - FIRST_VFP_REGNUM;
14481 if (IS_IWMMXT_GR_REGNUM (regno))
14482 return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
14484 if (IS_IWMMXT_REGNUM (regno))
14485 return 112 + regno - FIRST_IWMMXT_REGNUM;
14487 abort ();