* crtstuff.c: Handle targets that use .init_array.
[official-gcc.git] / gcc / config / arm / arm.c
blob6c0b896ec239526f384b7b7b73f4b60ef88ba666
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 #ifdef OBJECT_FORMAT_ELF
148 static void arm_elf_asm_constructor (rtx, int);
149 #endif
150 #ifndef ARM_PE
151 static void arm_encode_section_info (tree, rtx, int);
152 #endif
154 static void arm_file_end (void);
156 #ifdef AOF_ASSEMBLER
157 static void aof_globalize_label (FILE *, const char *);
158 static void aof_dump_imports (FILE *);
159 static void aof_dump_pic_table (FILE *);
160 static void aof_file_start (void);
161 static void aof_file_end (void);
162 #endif
163 static rtx arm_struct_value_rtx (tree, int);
164 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
165 tree, int *, int);
166 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
167 enum machine_mode, tree, bool);
168 static bool arm_promote_prototypes (tree);
169 static bool arm_default_short_enums (void);
170 static bool arm_align_anon_bitfield (void);
171 static bool arm_return_in_msb (tree);
172 static bool arm_must_pass_in_stack (enum machine_mode, tree);
174 static tree arm_cxx_guard_type (void);
175 static bool arm_cxx_guard_mask_bit (void);
176 static tree arm_get_cookie_size (tree);
177 static bool arm_cookie_has_size (void);
178 static bool arm_cxx_cdtor_returns_this (void);
179 static bool arm_cxx_key_method_may_be_inline (void);
180 static void arm_cxx_determine_class_data_visibility (tree);
181 static bool arm_cxx_class_data_always_comdat (void);
182 static bool arm_cxx_use_aeabi_atexit (void);
183 static void arm_init_libfuncs (void);
184 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
186 /* Initialize the GCC target structure. */
187 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
188 #undef TARGET_MERGE_DECL_ATTRIBUTES
189 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
190 #endif
192 #undef TARGET_ATTRIBUTE_TABLE
193 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
195 #undef TARGET_ASM_FILE_END
196 #define TARGET_ASM_FILE_END arm_file_end
198 #ifdef AOF_ASSEMBLER
199 #undef TARGET_ASM_BYTE_OP
200 #define TARGET_ASM_BYTE_OP "\tDCB\t"
201 #undef TARGET_ASM_ALIGNED_HI_OP
202 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
203 #undef TARGET_ASM_ALIGNED_SI_OP
204 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
205 #undef TARGET_ASM_GLOBALIZE_LABEL
206 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
207 #undef TARGET_ASM_FILE_START
208 #define TARGET_ASM_FILE_START aof_file_start
209 #undef TARGET_ASM_FILE_END
210 #define TARGET_ASM_FILE_END aof_file_end
211 #else
212 #undef TARGET_ASM_ALIGNED_SI_OP
213 #define TARGET_ASM_ALIGNED_SI_OP NULL
214 #undef TARGET_ASM_INTEGER
215 #define TARGET_ASM_INTEGER arm_assemble_integer
216 #endif
218 #undef TARGET_ASM_FUNCTION_PROLOGUE
219 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
221 #undef TARGET_ASM_FUNCTION_EPILOGUE
222 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
224 #undef TARGET_COMP_TYPE_ATTRIBUTES
225 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
227 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
228 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
230 #undef TARGET_SCHED_ADJUST_COST
231 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
233 #undef TARGET_ENCODE_SECTION_INFO
234 #ifdef ARM_PE
235 #define TARGET_ENCODE_SECTION_INFO arm_pe_encode_section_info
236 #else
237 #define TARGET_ENCODE_SECTION_INFO arm_encode_section_info
238 #endif
240 #undef TARGET_STRIP_NAME_ENCODING
241 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
243 #undef TARGET_ASM_INTERNAL_LABEL
244 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
246 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
247 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
249 #undef TARGET_ASM_OUTPUT_MI_THUNK
250 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
251 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
252 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
254 /* This will be overridden in arm_override_options. */
255 #undef TARGET_RTX_COSTS
256 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
257 #undef TARGET_ADDRESS_COST
258 #define TARGET_ADDRESS_COST arm_address_cost
260 #undef TARGET_SHIFT_TRUNCATION_MASK
261 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
262 #undef TARGET_VECTOR_MODE_SUPPORTED_P
263 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
265 #undef TARGET_MACHINE_DEPENDENT_REORG
266 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
268 #undef TARGET_INIT_BUILTINS
269 #define TARGET_INIT_BUILTINS arm_init_builtins
270 #undef TARGET_EXPAND_BUILTIN
271 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
273 #undef TARGET_INIT_LIBFUNCS
274 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
276 #undef TARGET_PROMOTE_FUNCTION_ARGS
277 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
278 #undef TARGET_PROMOTE_FUNCTION_RETURN
279 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
280 #undef TARGET_PROMOTE_PROTOTYPES
281 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
282 #undef TARGET_PASS_BY_REFERENCE
283 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
284 #undef TARGET_ARG_PARTIAL_BYTES
285 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
287 #undef TARGET_STRUCT_VALUE_RTX
288 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
290 #undef TARGET_SETUP_INCOMING_VARARGS
291 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
293 #undef TARGET_DEFAULT_SHORT_ENUMS
294 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
296 #undef TARGET_ALIGN_ANON_BITFIELD
297 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
299 #undef TARGET_CXX_GUARD_TYPE
300 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
302 #undef TARGET_CXX_GUARD_MASK_BIT
303 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
305 #undef TARGET_CXX_GET_COOKIE_SIZE
306 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
308 #undef TARGET_CXX_COOKIE_HAS_SIZE
309 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
311 #undef TARGET_CXX_CDTOR_RETURNS_THIS
312 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
314 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
315 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
317 #undef TARGET_CXX_USE_AEABI_ATEXIT
318 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
320 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
321 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
322 arm_cxx_determine_class_data_visibility
324 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
325 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
327 #undef TARGET_RETURN_IN_MSB
328 #define TARGET_RETURN_IN_MSB arm_return_in_msb
330 #undef TARGET_MUST_PASS_IN_STACK
331 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
333 struct gcc_target targetm = TARGET_INITIALIZER;
335 /* Obstack for minipool constant handling. */
336 static struct obstack minipool_obstack;
337 static char * minipool_startobj;
339 /* The maximum number of insns skipped which
340 will be conditionalised if possible. */
341 static int max_insns_skipped = 5;
343 extern FILE * asm_out_file;
345 /* True if we are currently building a constant table. */
346 int making_const_table;
348 /* Define the information needed to generate branch insns. This is
349 stored from the compare operation. */
350 rtx arm_compare_op0, arm_compare_op1;
352 /* The processor for which instructions should be scheduled. */
353 enum processor_type arm_tune = arm_none;
355 /* Which floating point model to use. */
356 enum arm_fp_model arm_fp_model;
358 /* Which floating point hardware is available. */
359 enum fputype arm_fpu_arch;
361 /* Which floating point hardware to schedule for. */
362 enum fputype arm_fpu_tune;
364 /* Whether to use floating point hardware. */
365 enum float_abi_type arm_float_abi;
367 /* Which ABI to use. */
368 enum arm_abi_type arm_abi;
370 /* Set by the -mfpu=... option. */
371 const char * target_fpu_name = NULL;
373 /* Set by the -mfpe=... option. */
374 const char * target_fpe_name = NULL;
376 /* Set by the -mfloat-abi=... option. */
377 const char * target_float_abi_name = NULL;
379 /* Set by the legacy -mhard-float and -msoft-float options. */
380 const char * target_float_switch = NULL;
382 /* Set by the -mabi=... option. */
383 const char * target_abi_name = NULL;
385 /* Used to parse -mstructure_size_boundary command line option. */
386 const char * structure_size_string = NULL;
387 int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
389 /* Used for Thumb call_via trampolines. */
390 rtx thumb_call_via_label[14];
391 static int thumb_call_reg_needed;
393 /* Bit values used to identify processor capabilities. */
394 #define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
395 #define FL_ARCH3M (1 << 1) /* Extended multiply */
396 #define FL_MODE26 (1 << 2) /* 26-bit mode support */
397 #define FL_MODE32 (1 << 3) /* 32-bit mode support */
398 #define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
399 #define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
400 #define FL_THUMB (1 << 6) /* Thumb aware */
401 #define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
402 #define FL_STRONG (1 << 8) /* StrongARM */
403 #define FL_ARCH5E (1 << 9) /* DSP extensions to v5 */
404 #define FL_XSCALE (1 << 10) /* XScale */
405 #define FL_CIRRUS (1 << 11) /* Cirrus/DSP. */
406 #define FL_ARCH6 (1 << 12) /* Architecture rel 6. Adds
407 media instructions. */
408 #define FL_VFPV2 (1 << 13) /* Vector Floating Point V2. */
409 #define FL_WBUF (1 << 14) /* Schedule for write buffer ops.
410 Note: ARM6 & 7 derivatives only. */
412 #define FL_IWMMXT (1 << 29) /* XScale v2 or "Intel Wireless MMX technology". */
414 #define FL_FOR_ARCH2 0
415 #define FL_FOR_ARCH3 FL_MODE32
416 #define FL_FOR_ARCH3M (FL_FOR_ARCH3 | FL_ARCH3M)
417 #define FL_FOR_ARCH4 (FL_FOR_ARCH3M | FL_ARCH4)
418 #define FL_FOR_ARCH4T (FL_FOR_ARCH4 | FL_THUMB)
419 #define FL_FOR_ARCH5 (FL_FOR_ARCH4 | FL_ARCH5)
420 #define FL_FOR_ARCH5T (FL_FOR_ARCH5 | FL_THUMB)
421 #define FL_FOR_ARCH5E (FL_FOR_ARCH5 | FL_ARCH5E)
422 #define FL_FOR_ARCH5TE (FL_FOR_ARCH5E | FL_THUMB)
423 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
424 #define FL_FOR_ARCH6 (FL_FOR_ARCH5TE | FL_ARCH6)
425 #define FL_FOR_ARCH6J FL_FOR_ARCH6
426 #define FL_FOR_ARCH6K FL_FOR_ARCH6
427 #define FL_FOR_ARCH6Z FL_FOR_ARCH6
428 #define FL_FOR_ARCH6ZK FL_FOR_ARCH6
430 /* The bits in this mask specify which
431 instructions we are allowed to generate. */
432 static unsigned long insn_flags = 0;
434 /* The bits in this mask specify which instruction scheduling options should
435 be used. */
436 static unsigned long tune_flags = 0;
438 /* The following are used in the arm.md file as equivalents to bits
439 in the above two flag variables. */
441 /* Nonzero if this chip supports the ARM Architecture 3M extensions. */
442 int arm_arch3m = 0;
444 /* Nonzero if this chip supports the ARM Architecture 4 extensions. */
445 int arm_arch4 = 0;
447 /* Nonzero if this chip supports the ARM Architecture 4t extensions. */
448 int arm_arch4t = 0;
450 /* Nonzero if this chip supports the ARM Architecture 5 extensions. */
451 int arm_arch5 = 0;
453 /* Nonzero if this chip supports the ARM Architecture 5E extensions. */
454 int arm_arch5e = 0;
456 /* Nonzero if this chip supports the ARM Architecture 6 extensions. */
457 int arm_arch6 = 0;
459 /* Nonzero if this chip can benefit from load scheduling. */
460 int arm_ld_sched = 0;
462 /* Nonzero if this chip is a StrongARM. */
463 int arm_tune_strongarm = 0;
465 /* Nonzero if this chip is a Cirrus variant. */
466 int arm_arch_cirrus = 0;
468 /* Nonzero if this chip supports Intel Wireless MMX technology. */
469 int arm_arch_iwmmxt = 0;
471 /* Nonzero if this chip is an XScale. */
472 int arm_arch_xscale = 0;
474 /* Nonzero if tuning for XScale */
475 int arm_tune_xscale = 0;
477 /* Nonzero if we want to tune for stores that access the write-buffer.
478 This typically means an ARM6 or ARM7 with MMU or MPU. */
479 int arm_tune_wbuf = 0;
481 /* Nonzero if generating Thumb instructions. */
482 int thumb_code = 0;
484 /* Nonzero if we should define __THUMB_INTERWORK__ in the
485 preprocessor.
486 XXX This is a bit of a hack, it's intended to help work around
487 problems in GLD which doesn't understand that armv5t code is
488 interworking clean. */
489 int arm_cpp_interwork = 0;
491 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
492 must report the mode of the memory reference from PRINT_OPERAND to
493 PRINT_OPERAND_ADDRESS. */
494 enum machine_mode output_memory_reference_mode;
496 /* The register number to be used for the PIC offset register. */
497 const char * arm_pic_register_string = NULL;
498 int arm_pic_register = INVALID_REGNUM;
500 /* Set to 1 when a return insn is output, this means that the epilogue
501 is not needed. */
502 int return_used_this_function;
504 /* Set to 1 after arm_reorg has started. Reset to start at the start of
505 the next function. */
506 static int after_arm_reorg = 0;
508 /* The maximum number of insns to be used when loading a constant. */
509 static int arm_constant_limit = 3;
511 /* For an explanation of these variables, see final_prescan_insn below. */
512 int arm_ccfsm_state;
513 enum arm_cond_code arm_current_cc;
514 rtx arm_target_insn;
515 int arm_target_label;
517 /* The condition codes of the ARM, and the inverse function. */
518 static const char * const arm_condition_codes[] =
520 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
521 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
524 #define streq(string1, string2) (strcmp (string1, string2) == 0)
526 /* Initialization code. */
528 struct processors
530 const char *const name;
531 enum processor_type core;
532 const char *arch;
533 const unsigned long flags;
534 bool (* rtx_costs) (rtx, int, int, int *);
537 /* Not all of these give usefully different compilation alternatives,
538 but there is no simple way of generalizing them. */
539 static const struct processors all_cores[] =
541 /* ARM Cores */
542 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
543 {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
544 #include "arm-cores.def"
545 #undef ARM_CORE
546 {NULL, arm_none, NULL, 0, NULL}
549 static const struct processors all_architectures[] =
551 /* ARM Architectures */
552 /* We don't specify rtx_costs here as it will be figured out
553 from the core. */
555 {"armv2", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
556 {"armv2a", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
557 {"armv3", arm6, "3", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
558 {"armv3m", arm7m, "3M", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
559 {"armv4", arm7tdmi, "4", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
560 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
561 implementations that support it, so we will leave it out for now. */
562 {"armv4t", arm7tdmi, "4T", FL_CO_PROC | FL_FOR_ARCH4T, NULL},
563 {"armv5", arm10tdmi, "5", FL_CO_PROC | FL_FOR_ARCH5, NULL},
564 {"armv5t", arm10tdmi, "5T", FL_CO_PROC | FL_FOR_ARCH5T, NULL},
565 {"armv5e", arm1026ejs, "5E", FL_CO_PROC | FL_FOR_ARCH5E, NULL},
566 {"armv5te", arm1026ejs, "5TE", FL_CO_PROC | FL_FOR_ARCH5TE, NULL},
567 {"armv6", arm1136js, "6", FL_CO_PROC | FL_FOR_ARCH6, NULL},
568 {"armv6j", arm1136js, "6J", FL_CO_PROC | FL_FOR_ARCH6J, NULL},
569 {"armv6k", mpcore, "6K", FL_CO_PROC | FL_FOR_ARCH6K, NULL},
570 {"armv6z", arm1176jzs, "6Z", FL_CO_PROC | FL_FOR_ARCH6Z, NULL},
571 {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC | FL_FOR_ARCH6ZK, NULL},
572 {"ep9312", ep9312, "4T", FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
573 {"iwmmxt", iwmmxt, "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
574 {NULL, arm_none, NULL, 0 , NULL}
577 /* This is a magic structure. The 'string' field is magically filled in
578 with a pointer to the value specified by the user on the command line
579 assuming that the user has specified such a value. */
581 struct arm_cpu_select arm_select[] =
583 /* string name processors */
584 { NULL, "-mcpu=", all_cores },
585 { NULL, "-march=", all_architectures },
586 { NULL, "-mtune=", all_cores }
590 /* The name of the proprocessor macro to define for this architecture. */
592 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
594 struct fpu_desc
596 const char * name;
597 enum fputype fpu;
601 /* Available values for for -mfpu=. */
603 static const struct fpu_desc all_fpus[] =
605 {"fpa", FPUTYPE_FPA},
606 {"fpe2", FPUTYPE_FPA_EMU2},
607 {"fpe3", FPUTYPE_FPA_EMU2},
608 {"maverick", FPUTYPE_MAVERICK},
609 {"vfp", FPUTYPE_VFP}
613 /* Floating point models used by the different hardware.
614 See fputype in arm.h. */
616 static const enum fputype fp_model_for_fpu[] =
618 /* No FP hardware. */
619 ARM_FP_MODEL_UNKNOWN, /* FPUTYPE_NONE */
620 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA */
621 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU2 */
622 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU3 */
623 ARM_FP_MODEL_MAVERICK, /* FPUTYPE_MAVERICK */
624 ARM_FP_MODEL_VFP /* FPUTYPE_VFP */
628 struct float_abi
630 const char * name;
631 enum float_abi_type abi_type;
635 /* Available values for -mfloat-abi=. */
637 static const struct float_abi all_float_abis[] =
639 {"soft", ARM_FLOAT_ABI_SOFT},
640 {"softfp", ARM_FLOAT_ABI_SOFTFP},
641 {"hard", ARM_FLOAT_ABI_HARD}
645 struct abi_name
647 const char *name;
648 enum arm_abi_type abi_type;
652 /* Available values for -mabi=. */
654 static const struct abi_name arm_all_abis[] =
656 {"apcs-gnu", ARM_ABI_APCS},
657 {"atpcs", ARM_ABI_ATPCS},
658 {"aapcs", ARM_ABI_AAPCS},
659 {"iwmmxt", ARM_ABI_IWMMXT}
662 /* Return the number of bits set in VALUE. */
663 static unsigned
664 bit_count (unsigned long value)
666 unsigned long count = 0;
668 while (value)
670 count++;
671 value &= value - 1; /* Clear the least-significant set bit. */
674 return count;
677 /* Set up library functions unique to ARM. */
679 static void
680 arm_init_libfuncs (void)
682 /* There are no special library functions unless we are using the
683 ARM BPABI. */
684 if (!TARGET_BPABI)
685 return;
687 /* The functions below are described in Section 4 of the "Run-Time
688 ABI for the ARM architecture", Version 1.0. */
690 /* Double-precision floating-point arithmetic. Table 2. */
691 set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
692 set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
693 set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
694 set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
695 set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
697 /* Double-precision comparisons. Table 3. */
698 set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
699 set_optab_libfunc (ne_optab, DFmode, NULL);
700 set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
701 set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
702 set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
703 set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
704 set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
706 /* Single-precision floating-point arithmetic. Table 4. */
707 set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
708 set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
709 set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
710 set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
711 set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
713 /* Single-precision comparisons. Table 5. */
714 set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
715 set_optab_libfunc (ne_optab, SFmode, NULL);
716 set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
717 set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
718 set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
719 set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
720 set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
722 /* Floating-point to integer conversions. Table 6. */
723 set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
724 set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
725 set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
726 set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
727 set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
728 set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
729 set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
730 set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
732 /* Conversions between floating types. Table 7. */
733 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
734 set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
736 /* Integer to floating-point conversions. Table 8. */
737 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
738 set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
739 set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
740 set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
741 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
742 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
743 set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
744 set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
746 /* Long long. Table 9. */
747 set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
748 set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
749 set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
750 set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
751 set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
752 set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
753 set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
754 set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
756 /* Integer (32/32->32) division. \S 4.3.1. */
757 set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
758 set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
760 /* The divmod functions are designed so that they can be used for
761 plain division, even though they return both the quotient and the
762 remainder. The quotient is returned in the usual location (i.e.,
763 r0 for SImode, {r0, r1} for DImode), just as would be expected
764 for an ordinary division routine. Because the AAPCS calling
765 conventions specify that all of { r0, r1, r2, r3 } are
766 callee-saved registers, there is no need to tell the compiler
767 explicitly that those registers are clobbered by these
768 routines. */
769 set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
770 set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
771 set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idivmod");
772 set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidivmod");
774 /* We don't have mod libcalls. Fortunately gcc knows how to use the
775 divmod libcalls instead. */
776 set_optab_libfunc (smod_optab, DImode, NULL);
777 set_optab_libfunc (umod_optab, DImode, NULL);
778 set_optab_libfunc (smod_optab, SImode, NULL);
779 set_optab_libfunc (umod_optab, SImode, NULL);
782 /* Fix up any incompatible options that the user has specified.
783 This has now turned into a maze. */
784 void
785 arm_override_options (void)
787 unsigned i;
789 /* Set up the flags based on the cpu/architecture selected by the user. */
790 for (i = ARRAY_SIZE (arm_select); i--;)
792 struct arm_cpu_select * ptr = arm_select + i;
794 if (ptr->string != NULL && ptr->string[0] != '\0')
796 const struct processors * sel;
798 for (sel = ptr->processors; sel->name != NULL; sel++)
799 if (streq (ptr->string, sel->name))
801 /* Set the architecture define. */
802 if (i != 2)
803 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
805 /* Determine the processor core for which we should
806 tune code-generation. */
807 if (/* -mcpu= is a sensible default. */
808 i == 0
809 /* If -march= is used, and -mcpu= has not been used,
810 assume that we should tune for a representative
811 CPU from that architecture. */
812 || i == 1
813 /* -mtune= overrides -mcpu= and -march=. */
814 || i == 2)
815 arm_tune = (enum processor_type) (sel - ptr->processors);
817 if (i != 2)
819 /* If we have been given an architecture and a processor
820 make sure that they are compatible. We only generate
821 a warning though, and we prefer the CPU over the
822 architecture. */
823 if (insn_flags != 0 && (insn_flags ^ sel->flags))
824 warning (0, "switch -mcpu=%s conflicts with -march= switch",
825 ptr->string);
827 insn_flags = sel->flags;
830 break;
833 if (sel->name == NULL)
834 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
838 /* If the user did not specify a processor, choose one for them. */
839 if (insn_flags == 0)
841 const struct processors * sel;
842 unsigned int sought;
843 enum processor_type cpu;
845 cpu = TARGET_CPU_DEFAULT;
846 if (cpu == arm_none)
848 #ifdef SUBTARGET_CPU_DEFAULT
849 /* Use the subtarget default CPU if none was specified by
850 configure. */
851 cpu = SUBTARGET_CPU_DEFAULT;
852 #endif
853 /* Default to ARM6. */
854 if (cpu == arm_none)
855 cpu = arm6;
857 sel = &all_cores[cpu];
859 insn_flags = sel->flags;
861 /* Now check to see if the user has specified some command line
862 switch that require certain abilities from the cpu. */
863 sought = 0;
865 if (TARGET_INTERWORK || TARGET_THUMB)
867 sought |= (FL_THUMB | FL_MODE32);
869 /* There are no ARM processors that support both APCS-26 and
870 interworking. Therefore we force FL_MODE26 to be removed
871 from insn_flags here (if it was set), so that the search
872 below will always be able to find a compatible processor. */
873 insn_flags &= ~FL_MODE26;
876 if (sought != 0 && ((sought & insn_flags) != sought))
878 /* Try to locate a CPU type that supports all of the abilities
879 of the default CPU, plus the extra abilities requested by
880 the user. */
881 for (sel = all_cores; sel->name != NULL; sel++)
882 if ((sel->flags & sought) == (sought | insn_flags))
883 break;
885 if (sel->name == NULL)
887 unsigned current_bit_count = 0;
888 const struct processors * best_fit = NULL;
890 /* Ideally we would like to issue an error message here
891 saying that it was not possible to find a CPU compatible
892 with the default CPU, but which also supports the command
893 line options specified by the programmer, and so they
894 ought to use the -mcpu=<name> command line option to
895 override the default CPU type.
897 If we cannot find a cpu that has both the
898 characteristics of the default cpu and the given
899 command line options we scan the array again looking
900 for a best match. */
901 for (sel = all_cores; sel->name != NULL; sel++)
902 if ((sel->flags & sought) == sought)
904 unsigned count;
906 count = bit_count (sel->flags & insn_flags);
908 if (count >= current_bit_count)
910 best_fit = sel;
911 current_bit_count = count;
915 gcc_assert (best_fit);
916 sel = best_fit;
919 insn_flags = sel->flags;
921 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
922 if (arm_tune == arm_none)
923 arm_tune = (enum processor_type) (sel - all_cores);
926 /* The processor for which we should tune should now have been
927 chosen. */
928 gcc_assert (arm_tune != arm_none);
930 tune_flags = all_cores[(int)arm_tune].flags;
931 if (optimize_size)
932 targetm.rtx_costs = arm_size_rtx_costs;
933 else
934 targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
936 /* Make sure that the processor choice does not conflict with any of the
937 other command line choices. */
938 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
940 warning (0, "target CPU does not support interworking" );
941 target_flags &= ~ARM_FLAG_INTERWORK;
944 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
946 warning (0, "target CPU does not support THUMB instructions");
947 target_flags &= ~ARM_FLAG_THUMB;
950 if (TARGET_APCS_FRAME && TARGET_THUMB)
952 /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
953 target_flags &= ~ARM_FLAG_APCS_FRAME;
956 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
957 from here where no function is being compiled currently. */
958 if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
959 && TARGET_ARM)
960 warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
962 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
963 warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
965 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
966 warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
968 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
970 warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
971 target_flags |= ARM_FLAG_APCS_FRAME;
974 if (TARGET_POKE_FUNCTION_NAME)
975 target_flags |= ARM_FLAG_APCS_FRAME;
977 if (TARGET_APCS_REENT && flag_pic)
978 error ("-fpic and -mapcs-reent are incompatible");
980 if (TARGET_APCS_REENT)
981 warning (0, "APCS reentrant code not supported. Ignored");
983 /* If this target is normally configured to use APCS frames, warn if they
984 are turned off and debugging is turned on. */
985 if (TARGET_ARM
986 && write_symbols != NO_DEBUG
987 && !TARGET_APCS_FRAME
988 && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
989 warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
991 /* If stack checking is disabled, we can use r10 as the PIC register,
992 which keeps r9 available. */
993 if (flag_pic)
994 arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
996 if (TARGET_APCS_FLOAT)
997 warning (0, "passing floating point arguments in fp regs not yet supported");
999 /* Initialize boolean versions of the flags, for use in the arm.md file. */
1000 arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1001 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1002 arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1003 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1004 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1005 arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1006 arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1007 arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1009 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1010 arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1011 thumb_code = (TARGET_ARM == 0);
1012 arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1013 arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1014 arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1016 /* V5 code we generate is completely interworking capable, so we turn off
1017 TARGET_INTERWORK here to avoid many tests later on. */
1019 /* XXX However, we must pass the right pre-processor defines to CPP
1020 or GLD can get confused. This is a hack. */
1021 if (TARGET_INTERWORK)
1022 arm_cpp_interwork = 1;
1024 if (arm_arch5)
1025 target_flags &= ~ARM_FLAG_INTERWORK;
1027 if (target_abi_name)
1029 for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1031 if (streq (arm_all_abis[i].name, target_abi_name))
1033 arm_abi = arm_all_abis[i].abi_type;
1034 break;
1037 if (i == ARRAY_SIZE (arm_all_abis))
1038 error ("invalid ABI option: -mabi=%s", target_abi_name);
1040 else
1041 arm_abi = ARM_DEFAULT_ABI;
1043 if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1044 error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1046 if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1047 error ("iwmmxt abi requires an iwmmxt capable cpu");
1049 arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1050 if (target_fpu_name == NULL && target_fpe_name != NULL)
1052 if (streq (target_fpe_name, "2"))
1053 target_fpu_name = "fpe2";
1054 else if (streq (target_fpe_name, "3"))
1055 target_fpu_name = "fpe3";
1056 else
1057 error ("invalid floating point emulation option: -mfpe=%s",
1058 target_fpe_name);
1060 if (target_fpu_name != NULL)
1062 /* The user specified a FPU. */
1063 for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1065 if (streq (all_fpus[i].name, target_fpu_name))
1067 arm_fpu_arch = all_fpus[i].fpu;
1068 arm_fpu_tune = arm_fpu_arch;
1069 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1070 break;
1073 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1074 error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1076 else
1078 #ifdef FPUTYPE_DEFAULT
1079 /* Use the default if it is specified for this platform. */
1080 arm_fpu_arch = FPUTYPE_DEFAULT;
1081 arm_fpu_tune = FPUTYPE_DEFAULT;
1082 #else
1083 /* Pick one based on CPU type. */
1084 /* ??? Some targets assume FPA is the default.
1085 if ((insn_flags & FL_VFP) != 0)
1086 arm_fpu_arch = FPUTYPE_VFP;
1087 else
1089 if (arm_arch_cirrus)
1090 arm_fpu_arch = FPUTYPE_MAVERICK;
1091 else
1092 arm_fpu_arch = FPUTYPE_FPA_EMU2;
1093 #endif
1094 if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1095 arm_fpu_tune = FPUTYPE_FPA;
1096 else
1097 arm_fpu_tune = arm_fpu_arch;
1098 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1099 gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1102 if (target_float_abi_name != NULL)
1104 /* The user specified a FP ABI. */
1105 for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1107 if (streq (all_float_abis[i].name, target_float_abi_name))
1109 arm_float_abi = all_float_abis[i].abi_type;
1110 break;
1113 if (i == ARRAY_SIZE (all_float_abis))
1114 error ("invalid floating point abi: -mfloat-abi=%s",
1115 target_float_abi_name);
1117 else if (target_float_switch)
1119 /* This is a bit of a hack to avoid needing target flags for these. */
1120 if (target_float_switch[0] == 'h')
1121 arm_float_abi = ARM_FLOAT_ABI_HARD;
1122 else
1123 arm_float_abi = ARM_FLOAT_ABI_SOFT;
1125 else
1126 arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1128 if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1129 sorry ("-mfloat-abi=hard and VFP");
1131 /* If soft-float is specified then don't use FPU. */
1132 if (TARGET_SOFT_FLOAT)
1133 arm_fpu_arch = FPUTYPE_NONE;
1135 /* For arm2/3 there is no need to do any scheduling if there is only
1136 a floating point emulator, or we are doing software floating-point. */
1137 if ((TARGET_SOFT_FLOAT
1138 || arm_fpu_tune == FPUTYPE_FPA_EMU2
1139 || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1140 && (tune_flags & FL_MODE32) == 0)
1141 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1143 /* Override the default structure alignment for AAPCS ABI. */
1144 if (arm_abi == ARM_ABI_AAPCS)
1145 arm_structure_size_boundary = 8;
1147 if (structure_size_string != NULL)
1149 int size = strtol (structure_size_string, NULL, 0);
1151 if (size == 8 || size == 32
1152 || (ARM_DOUBLEWORD_ALIGN && size == 64))
1153 arm_structure_size_boundary = size;
1154 else
1155 warning (0, "structure size boundary can only be set to %s",
1156 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1159 if (arm_pic_register_string != NULL)
1161 int pic_register = decode_reg_name (arm_pic_register_string);
1163 if (!flag_pic)
1164 warning (0, "-mpic-register= is useless without -fpic");
1166 /* Prevent the user from choosing an obviously stupid PIC register. */
1167 else if (pic_register < 0 || call_used_regs[pic_register]
1168 || pic_register == HARD_FRAME_POINTER_REGNUM
1169 || pic_register == STACK_POINTER_REGNUM
1170 || pic_register >= PC_REGNUM)
1171 error ("unable to use '%s' for PIC register", arm_pic_register_string);
1172 else
1173 arm_pic_register = pic_register;
1176 if (TARGET_THUMB && flag_schedule_insns)
1178 /* Don't warn since it's on by default in -O2. */
1179 flag_schedule_insns = 0;
1182 if (optimize_size)
1184 /* There's some dispute as to whether this should be 1 or 2. However,
1185 experiments seem to show that in pathological cases a setting of
1186 1 degrades less severely than a setting of 2. This could change if
1187 other parts of the compiler change their behavior. */
1188 arm_constant_limit = 1;
1190 /* If optimizing for size, bump the number of instructions that we
1191 are prepared to conditionally execute (even on a StrongARM). */
1192 max_insns_skipped = 6;
1194 else
1196 /* For processors with load scheduling, it never costs more than
1197 2 cycles to load a constant, and the load scheduler may well
1198 reduce that to 1. */
1199 if (arm_ld_sched)
1200 arm_constant_limit = 1;
1202 /* On XScale the longer latency of a load makes it more difficult
1203 to achieve a good schedule, so it's faster to synthesize
1204 constants that can be done in two insns. */
1205 if (arm_tune_xscale)
1206 arm_constant_limit = 2;
1208 /* StrongARM has early execution of branches, so a sequence
1209 that is worth skipping is shorter. */
1210 if (arm_tune_strongarm)
1211 max_insns_skipped = 3;
1214 /* Register global variables with the garbage collector. */
1215 arm_add_gc_roots ();
1218 static void
1219 arm_add_gc_roots (void)
1221 gcc_obstack_init(&minipool_obstack);
1222 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1225 /* A table of known ARM exception types.
1226 For use with the interrupt function attribute. */
1228 typedef struct
1230 const char *const arg;
1231 const unsigned long return_value;
1233 isr_attribute_arg;
1235 static const isr_attribute_arg isr_attribute_args [] =
1237 { "IRQ", ARM_FT_ISR },
1238 { "irq", ARM_FT_ISR },
1239 { "FIQ", ARM_FT_FIQ },
1240 { "fiq", ARM_FT_FIQ },
1241 { "ABORT", ARM_FT_ISR },
1242 { "abort", ARM_FT_ISR },
1243 { "ABORT", ARM_FT_ISR },
1244 { "abort", ARM_FT_ISR },
1245 { "UNDEF", ARM_FT_EXCEPTION },
1246 { "undef", ARM_FT_EXCEPTION },
1247 { "SWI", ARM_FT_EXCEPTION },
1248 { "swi", ARM_FT_EXCEPTION },
1249 { NULL, ARM_FT_NORMAL }
1252 /* Returns the (interrupt) function type of the current
1253 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
1255 static unsigned long
1256 arm_isr_value (tree argument)
1258 const isr_attribute_arg * ptr;
1259 const char * arg;
1261 /* No argument - default to IRQ. */
1262 if (argument == NULL_TREE)
1263 return ARM_FT_ISR;
1265 /* Get the value of the argument. */
1266 if (TREE_VALUE (argument) == NULL_TREE
1267 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1268 return ARM_FT_UNKNOWN;
1270 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1272 /* Check it against the list of known arguments. */
1273 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1274 if (streq (arg, ptr->arg))
1275 return ptr->return_value;
1277 /* An unrecognized interrupt type. */
1278 return ARM_FT_UNKNOWN;
1281 /* Computes the type of the current function. */
1283 static unsigned long
1284 arm_compute_func_type (void)
1286 unsigned long type = ARM_FT_UNKNOWN;
1287 tree a;
1288 tree attr;
1290 gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1292 /* Decide if the current function is volatile. Such functions
1293 never return, and many memory cycles can be saved by not storing
1294 register values that will never be needed again. This optimization
1295 was added to speed up context switching in a kernel application. */
1296 if (optimize > 0
1297 && TREE_NOTHROW (current_function_decl)
1298 && TREE_THIS_VOLATILE (current_function_decl))
1299 type |= ARM_FT_VOLATILE;
1301 if (cfun->static_chain_decl != NULL)
1302 type |= ARM_FT_NESTED;
1304 attr = DECL_ATTRIBUTES (current_function_decl);
1306 a = lookup_attribute ("naked", attr);
1307 if (a != NULL_TREE)
1308 type |= ARM_FT_NAKED;
1310 a = lookup_attribute ("isr", attr);
1311 if (a == NULL_TREE)
1312 a = lookup_attribute ("interrupt", attr);
1314 if (a == NULL_TREE)
1315 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1316 else
1317 type |= arm_isr_value (TREE_VALUE (a));
1319 return type;
1322 /* Returns the type of the current function. */
1324 unsigned long
1325 arm_current_func_type (void)
1327 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1328 cfun->machine->func_type = arm_compute_func_type ();
1330 return cfun->machine->func_type;
1333 /* Return 1 if it is possible to return using a single instruction.
1334 If SIBLING is non-null, this is a test for a return before a sibling
1335 call. SIBLING is the call insn, so we can examine its register usage. */
1338 use_return_insn (int iscond, rtx sibling)
1340 int regno;
1341 unsigned int func_type;
1342 unsigned long saved_int_regs;
1343 unsigned HOST_WIDE_INT stack_adjust;
1344 arm_stack_offsets *offsets;
1346 /* Never use a return instruction before reload has run. */
1347 if (!reload_completed)
1348 return 0;
1350 func_type = arm_current_func_type ();
1352 /* Naked functions and volatile functions need special
1353 consideration. */
1354 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1355 return 0;
1357 /* So do interrupt functions that use the frame pointer. */
1358 if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1359 return 0;
1361 offsets = arm_get_frame_offsets ();
1362 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1364 /* As do variadic functions. */
1365 if (current_function_pretend_args_size
1366 || cfun->machine->uses_anonymous_args
1367 /* Or if the function calls __builtin_eh_return () */
1368 || current_function_calls_eh_return
1369 /* Or if the function calls alloca */
1370 || current_function_calls_alloca
1371 /* Or if there is a stack adjustment. However, if the stack pointer
1372 is saved on the stack, we can use a pre-incrementing stack load. */
1373 || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1374 return 0;
1376 saved_int_regs = arm_compute_save_reg_mask ();
1378 /* Unfortunately, the insn
1380 ldmib sp, {..., sp, ...}
1382 triggers a bug on most SA-110 based devices, such that the stack
1383 pointer won't be correctly restored if the instruction takes a
1384 page fault. We work around this problem by popping r3 along with
1385 the other registers, since that is never slower than executing
1386 another instruction.
1388 We test for !arm_arch5 here, because code for any architecture
1389 less than this could potentially be run on one of the buggy
1390 chips. */
1391 if (stack_adjust == 4 && !arm_arch5)
1393 /* Validate that r3 is a call-clobbered register (always true in
1394 the default abi) ... */
1395 if (!call_used_regs[3])
1396 return 0;
1398 /* ... that it isn't being used for a return value (always true
1399 until we implement return-in-regs), or for a tail-call
1400 argument ... */
1401 if (sibling)
1403 gcc_assert (GET_CODE (sibling) == CALL_INSN);
1405 if (find_regno_fusage (sibling, USE, 3))
1406 return 0;
1409 /* ... and that there are no call-saved registers in r0-r2
1410 (always true in the default ABI). */
1411 if (saved_int_regs & 0x7)
1412 return 0;
1415 /* Can't be done if interworking with Thumb, and any registers have been
1416 stacked. */
1417 if (TARGET_INTERWORK && saved_int_regs != 0)
1418 return 0;
1420 /* On StrongARM, conditional returns are expensive if they aren't
1421 taken and multiple registers have been stacked. */
1422 if (iscond && arm_tune_strongarm)
1424 /* Conditional return when just the LR is stored is a simple
1425 conditional-load instruction, that's not expensive. */
1426 if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1427 return 0;
1429 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1430 return 0;
1433 /* If there are saved registers but the LR isn't saved, then we need
1434 two instructions for the return. */
1435 if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1436 return 0;
1438 /* Can't be done if any of the FPA regs are pushed,
1439 since this also requires an insn. */
1440 if (TARGET_HARD_FLOAT && TARGET_FPA)
1441 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1442 if (regs_ever_live[regno] && !call_used_regs[regno])
1443 return 0;
1445 /* Likewise VFP regs. */
1446 if (TARGET_HARD_FLOAT && TARGET_VFP)
1447 for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1448 if (regs_ever_live[regno] && !call_used_regs[regno])
1449 return 0;
1451 if (TARGET_REALLY_IWMMXT)
1452 for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1453 if (regs_ever_live[regno] && ! call_used_regs [regno])
1454 return 0;
1456 return 1;
1459 /* Return TRUE if int I is a valid immediate ARM constant. */
1462 const_ok_for_arm (HOST_WIDE_INT i)
1464 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1466 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1467 be all zero, or all one. */
1468 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1469 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1470 != ((~(unsigned HOST_WIDE_INT) 0)
1471 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1472 return FALSE;
1474 /* Fast return for 0 and powers of 2 */
1475 if ((i & (i - 1)) == 0)
1476 return TRUE;
1480 if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1481 return TRUE;
1482 mask =
1483 (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1484 >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1486 while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1488 return FALSE;
1491 /* Return true if I is a valid constant for the operation CODE. */
1492 static int
1493 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1495 if (const_ok_for_arm (i))
1496 return 1;
1498 switch (code)
1500 case PLUS:
1501 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1503 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
1504 case XOR:
1505 case IOR:
1506 return 0;
1508 case AND:
1509 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1511 default:
1512 gcc_unreachable ();
1516 /* Emit a sequence of insns to handle a large constant.
1517 CODE is the code of the operation required, it can be any of SET, PLUS,
1518 IOR, AND, XOR, MINUS;
1519 MODE is the mode in which the operation is being performed;
1520 VAL is the integer to operate on;
1521 SOURCE is the other operand (a register, or a null-pointer for SET);
1522 SUBTARGETS means it is safe to create scratch registers if that will
1523 either produce a simpler sequence, or we will want to cse the values.
1524 Return value is the number of insns emitted. */
1527 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1528 HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1530 rtx cond;
1532 if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1533 cond = COND_EXEC_TEST (PATTERN (insn));
1534 else
1535 cond = NULL_RTX;
1537 if (subtargets || code == SET
1538 || (GET_CODE (target) == REG && GET_CODE (source) == REG
1539 && REGNO (target) != REGNO (source)))
1541 /* After arm_reorg has been called, we can't fix up expensive
1542 constants by pushing them into memory so we must synthesize
1543 them in-line, regardless of the cost. This is only likely to
1544 be more costly on chips that have load delay slots and we are
1545 compiling without running the scheduler (so no splitting
1546 occurred before the final instruction emission).
1548 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1550 if (!after_arm_reorg
1551 && !cond
1552 && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1553 1, 0)
1554 > arm_constant_limit + (code != SET)))
1556 if (code == SET)
1558 /* Currently SET is the only monadic value for CODE, all
1559 the rest are diadic. */
1560 emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1561 return 1;
1563 else
1565 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1567 emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1568 /* For MINUS, the value is subtracted from, since we never
1569 have subtraction of a constant. */
1570 if (code == MINUS)
1571 emit_insn (gen_rtx_SET (VOIDmode, target,
1572 gen_rtx_MINUS (mode, temp, source)));
1573 else
1574 emit_insn (gen_rtx_SET (VOIDmode, target,
1575 gen_rtx_fmt_ee (code, mode, source, temp)));
1576 return 2;
1581 return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1585 static int
1586 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1588 HOST_WIDE_INT temp1;
1589 int num_insns = 0;
1592 int end;
1594 if (i <= 0)
1595 i += 32;
1596 if (remainder & (3 << (i - 2)))
1598 end = i - 8;
1599 if (end < 0)
1600 end += 32;
1601 temp1 = remainder & ((0x0ff << end)
1602 | ((i < end) ? (0xff >> (32 - end)) : 0));
1603 remainder &= ~temp1;
1604 num_insns++;
1605 i -= 6;
1607 i -= 2;
1608 } while (remainder);
1609 return num_insns;
1612 /* Emit an instruction with the indicated PATTERN. If COND is
1613 non-NULL, conditionalize the execution of the instruction on COND
1614 being true. */
1616 static void
1617 emit_constant_insn (rtx cond, rtx pattern)
1619 if (cond)
1620 pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1621 emit_insn (pattern);
1624 /* As above, but extra parameter GENERATE which, if clear, suppresses
1625 RTL generation. */
1627 static int
1628 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1629 HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1630 int generate)
1632 int can_invert = 0;
1633 int can_negate = 0;
1634 int can_negate_initial = 0;
1635 int can_shift = 0;
1636 int i;
1637 int num_bits_set = 0;
1638 int set_sign_bit_copies = 0;
1639 int clear_sign_bit_copies = 0;
1640 int clear_zero_bit_copies = 0;
1641 int set_zero_bit_copies = 0;
1642 int insns = 0;
1643 unsigned HOST_WIDE_INT temp1, temp2;
1644 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1646 /* Find out which operations are safe for a given CODE. Also do a quick
1647 check for degenerate cases; these can occur when DImode operations
1648 are split. */
1649 switch (code)
1651 case SET:
1652 can_invert = 1;
1653 can_shift = 1;
1654 can_negate = 1;
1655 break;
1657 case PLUS:
1658 can_negate = 1;
1659 can_negate_initial = 1;
1660 break;
1662 case IOR:
1663 if (remainder == 0xffffffff)
1665 if (generate)
1666 emit_constant_insn (cond,
1667 gen_rtx_SET (VOIDmode, target,
1668 GEN_INT (ARM_SIGN_EXTEND (val))));
1669 return 1;
1671 if (remainder == 0)
1673 if (reload_completed && rtx_equal_p (target, source))
1674 return 0;
1675 if (generate)
1676 emit_constant_insn (cond,
1677 gen_rtx_SET (VOIDmode, target, source));
1678 return 1;
1680 break;
1682 case AND:
1683 if (remainder == 0)
1685 if (generate)
1686 emit_constant_insn (cond,
1687 gen_rtx_SET (VOIDmode, target, const0_rtx));
1688 return 1;
1690 if (remainder == 0xffffffff)
1692 if (reload_completed && rtx_equal_p (target, source))
1693 return 0;
1694 if (generate)
1695 emit_constant_insn (cond,
1696 gen_rtx_SET (VOIDmode, target, source));
1697 return 1;
1699 can_invert = 1;
1700 break;
1702 case XOR:
1703 if (remainder == 0)
1705 if (reload_completed && rtx_equal_p (target, source))
1706 return 0;
1707 if (generate)
1708 emit_constant_insn (cond,
1709 gen_rtx_SET (VOIDmode, target, source));
1710 return 1;
1713 /* We don't know how to handle other cases yet. */
1714 gcc_assert (remainder == 0xffffffff);
1716 if (generate)
1717 emit_constant_insn (cond,
1718 gen_rtx_SET (VOIDmode, target,
1719 gen_rtx_NOT (mode, source)));
1720 return 1;
1722 case MINUS:
1723 /* We treat MINUS as (val - source), since (source - val) is always
1724 passed as (source + (-val)). */
1725 if (remainder == 0)
1727 if (generate)
1728 emit_constant_insn (cond,
1729 gen_rtx_SET (VOIDmode, target,
1730 gen_rtx_NEG (mode, source)));
1731 return 1;
1733 if (const_ok_for_arm (val))
1735 if (generate)
1736 emit_constant_insn (cond,
1737 gen_rtx_SET (VOIDmode, target,
1738 gen_rtx_MINUS (mode, GEN_INT (val),
1739 source)));
1740 return 1;
1742 can_negate = 1;
1744 break;
1746 default:
1747 gcc_unreachable ();
1750 /* If we can do it in one insn get out quickly. */
1751 if (const_ok_for_arm (val)
1752 || (can_negate_initial && const_ok_for_arm (-val))
1753 || (can_invert && const_ok_for_arm (~val)))
1755 if (generate)
1756 emit_constant_insn (cond,
1757 gen_rtx_SET (VOIDmode, target,
1758 (source
1759 ? gen_rtx_fmt_ee (code, mode, source,
1760 GEN_INT (val))
1761 : GEN_INT (val))));
1762 return 1;
1765 /* Calculate a few attributes that may be useful for specific
1766 optimizations. */
1767 for (i = 31; i >= 0; i--)
1769 if ((remainder & (1 << i)) == 0)
1770 clear_sign_bit_copies++;
1771 else
1772 break;
1775 for (i = 31; i >= 0; i--)
1777 if ((remainder & (1 << i)) != 0)
1778 set_sign_bit_copies++;
1779 else
1780 break;
1783 for (i = 0; i <= 31; i++)
1785 if ((remainder & (1 << i)) == 0)
1786 clear_zero_bit_copies++;
1787 else
1788 break;
1791 for (i = 0; i <= 31; i++)
1793 if ((remainder & (1 << i)) != 0)
1794 set_zero_bit_copies++;
1795 else
1796 break;
1799 switch (code)
1801 case SET:
1802 /* See if we can do this by sign_extending a constant that is known
1803 to be negative. This is a good, way of doing it, since the shift
1804 may well merge into a subsequent insn. */
1805 if (set_sign_bit_copies > 1)
1807 if (const_ok_for_arm
1808 (temp1 = ARM_SIGN_EXTEND (remainder
1809 << (set_sign_bit_copies - 1))))
1811 if (generate)
1813 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1814 emit_constant_insn (cond,
1815 gen_rtx_SET (VOIDmode, new_src,
1816 GEN_INT (temp1)));
1817 emit_constant_insn (cond,
1818 gen_ashrsi3 (target, new_src,
1819 GEN_INT (set_sign_bit_copies - 1)));
1821 return 2;
1823 /* For an inverted constant, we will need to set the low bits,
1824 these will be shifted out of harm's way. */
1825 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1826 if (const_ok_for_arm (~temp1))
1828 if (generate)
1830 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1831 emit_constant_insn (cond,
1832 gen_rtx_SET (VOIDmode, new_src,
1833 GEN_INT (temp1)));
1834 emit_constant_insn (cond,
1835 gen_ashrsi3 (target, new_src,
1836 GEN_INT (set_sign_bit_copies - 1)));
1838 return 2;
1842 /* See if we can generate this by setting the bottom (or the top)
1843 16 bits, and then shifting these into the other half of the
1844 word. We only look for the simplest cases, to do more would cost
1845 too much. Be careful, however, not to generate this when the
1846 alternative would take fewer insns. */
1847 if (val & 0xffff0000)
1849 temp1 = remainder & 0xffff0000;
1850 temp2 = remainder & 0x0000ffff;
1852 /* Overlaps outside this range are best done using other methods. */
1853 for (i = 9; i < 24; i++)
1855 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1856 && !const_ok_for_arm (temp2))
1858 rtx new_src = (subtargets
1859 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1860 : target);
1861 insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1862 source, subtargets, generate);
1863 source = new_src;
1864 if (generate)
1865 emit_constant_insn
1866 (cond,
1867 gen_rtx_SET
1868 (VOIDmode, target,
1869 gen_rtx_IOR (mode,
1870 gen_rtx_ASHIFT (mode, source,
1871 GEN_INT (i)),
1872 source)));
1873 return insns + 1;
1877 /* Don't duplicate cases already considered. */
1878 for (i = 17; i < 24; i++)
1880 if (((temp1 | (temp1 >> i)) == remainder)
1881 && !const_ok_for_arm (temp1))
1883 rtx new_src = (subtargets
1884 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1885 : target);
1886 insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1887 source, subtargets, generate);
1888 source = new_src;
1889 if (generate)
1890 emit_constant_insn
1891 (cond,
1892 gen_rtx_SET (VOIDmode, target,
1893 gen_rtx_IOR
1894 (mode,
1895 gen_rtx_LSHIFTRT (mode, source,
1896 GEN_INT (i)),
1897 source)));
1898 return insns + 1;
1902 break;
1904 case IOR:
1905 case XOR:
1906 /* If we have IOR or XOR, and the constant can be loaded in a
1907 single instruction, and we can find a temporary to put it in,
1908 then this can be done in two instructions instead of 3-4. */
1909 if (subtargets
1910 /* TARGET can't be NULL if SUBTARGETS is 0 */
1911 || (reload_completed && !reg_mentioned_p (target, source)))
1913 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1915 if (generate)
1917 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1919 emit_constant_insn (cond,
1920 gen_rtx_SET (VOIDmode, sub,
1921 GEN_INT (val)));
1922 emit_constant_insn (cond,
1923 gen_rtx_SET (VOIDmode, target,
1924 gen_rtx_fmt_ee (code, mode,
1925 source, sub)));
1927 return 2;
1931 if (code == XOR)
1932 break;
1934 if (set_sign_bit_copies > 8
1935 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1937 if (generate)
1939 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1940 rtx shift = GEN_INT (set_sign_bit_copies);
1942 emit_constant_insn
1943 (cond,
1944 gen_rtx_SET (VOIDmode, sub,
1945 gen_rtx_NOT (mode,
1946 gen_rtx_ASHIFT (mode,
1947 source,
1948 shift))));
1949 emit_constant_insn
1950 (cond,
1951 gen_rtx_SET (VOIDmode, target,
1952 gen_rtx_NOT (mode,
1953 gen_rtx_LSHIFTRT (mode, sub,
1954 shift))));
1956 return 2;
1959 if (set_zero_bit_copies > 8
1960 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1962 if (generate)
1964 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1965 rtx shift = GEN_INT (set_zero_bit_copies);
1967 emit_constant_insn
1968 (cond,
1969 gen_rtx_SET (VOIDmode, sub,
1970 gen_rtx_NOT (mode,
1971 gen_rtx_LSHIFTRT (mode,
1972 source,
1973 shift))));
1974 emit_constant_insn
1975 (cond,
1976 gen_rtx_SET (VOIDmode, target,
1977 gen_rtx_NOT (mode,
1978 gen_rtx_ASHIFT (mode, sub,
1979 shift))));
1981 return 2;
1984 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1986 if (generate)
1988 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1989 emit_constant_insn (cond,
1990 gen_rtx_SET (VOIDmode, sub,
1991 gen_rtx_NOT (mode, source)));
1992 source = sub;
1993 if (subtargets)
1994 sub = gen_reg_rtx (mode);
1995 emit_constant_insn (cond,
1996 gen_rtx_SET (VOIDmode, sub,
1997 gen_rtx_AND (mode, source,
1998 GEN_INT (temp1))));
1999 emit_constant_insn (cond,
2000 gen_rtx_SET (VOIDmode, target,
2001 gen_rtx_NOT (mode, sub)));
2003 return 3;
2005 break;
2007 case AND:
2008 /* See if two shifts will do 2 or more insn's worth of work. */
2009 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2011 HOST_WIDE_INT shift_mask = ((0xffffffff
2012 << (32 - clear_sign_bit_copies))
2013 & 0xffffffff);
2015 if ((remainder | shift_mask) != 0xffffffff)
2017 if (generate)
2019 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2020 insns = arm_gen_constant (AND, mode, cond,
2021 remainder | shift_mask,
2022 new_src, source, subtargets, 1);
2023 source = new_src;
2025 else
2027 rtx targ = subtargets ? NULL_RTX : target;
2028 insns = arm_gen_constant (AND, mode, cond,
2029 remainder | shift_mask,
2030 targ, source, subtargets, 0);
2034 if (generate)
2036 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2037 rtx shift = GEN_INT (clear_sign_bit_copies);
2039 emit_insn (gen_ashlsi3 (new_src, source, shift));
2040 emit_insn (gen_lshrsi3 (target, new_src, shift));
2043 return insns + 2;
2046 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2048 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2050 if ((remainder | shift_mask) != 0xffffffff)
2052 if (generate)
2054 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2056 insns = arm_gen_constant (AND, mode, cond,
2057 remainder | shift_mask,
2058 new_src, source, subtargets, 1);
2059 source = new_src;
2061 else
2063 rtx targ = subtargets ? NULL_RTX : target;
2065 insns = arm_gen_constant (AND, mode, cond,
2066 remainder | shift_mask,
2067 targ, source, subtargets, 0);
2071 if (generate)
2073 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2074 rtx shift = GEN_INT (clear_zero_bit_copies);
2076 emit_insn (gen_lshrsi3 (new_src, source, shift));
2077 emit_insn (gen_ashlsi3 (target, new_src, shift));
2080 return insns + 2;
2083 break;
2085 default:
2086 break;
2089 for (i = 0; i < 32; i++)
2090 if (remainder & (1 << i))
2091 num_bits_set++;
2093 if (code == AND || (can_invert && num_bits_set > 16))
2094 remainder = (~remainder) & 0xffffffff;
2095 else if (code == PLUS && num_bits_set > 16)
2096 remainder = (-remainder) & 0xffffffff;
2097 else
2099 can_invert = 0;
2100 can_negate = 0;
2103 /* Now try and find a way of doing the job in either two or three
2104 instructions.
2105 We start by looking for the largest block of zeros that are aligned on
2106 a 2-bit boundary, we then fill up the temps, wrapping around to the
2107 top of the word when we drop off the bottom.
2108 In the worst case this code should produce no more than four insns. */
2110 int best_start = 0;
2111 int best_consecutive_zeros = 0;
2113 for (i = 0; i < 32; i += 2)
2115 int consecutive_zeros = 0;
2117 if (!(remainder & (3 << i)))
2119 while ((i < 32) && !(remainder & (3 << i)))
2121 consecutive_zeros += 2;
2122 i += 2;
2124 if (consecutive_zeros > best_consecutive_zeros)
2126 best_consecutive_zeros = consecutive_zeros;
2127 best_start = i - consecutive_zeros;
2129 i -= 2;
2133 /* So long as it won't require any more insns to do so, it's
2134 desirable to emit a small constant (in bits 0...9) in the last
2135 insn. This way there is more chance that it can be combined with
2136 a later addressing insn to form a pre-indexed load or store
2137 operation. Consider:
2139 *((volatile int *)0xe0000100) = 1;
2140 *((volatile int *)0xe0000110) = 2;
2142 We want this to wind up as:
2144 mov rA, #0xe0000000
2145 mov rB, #1
2146 str rB, [rA, #0x100]
2147 mov rB, #2
2148 str rB, [rA, #0x110]
2150 rather than having to synthesize both large constants from scratch.
2152 Therefore, we calculate how many insns would be required to emit
2153 the constant starting from `best_start', and also starting from
2154 zero (i.e. with bit 31 first to be output). If `best_start' doesn't
2155 yield a shorter sequence, we may as well use zero. */
2156 if (best_start != 0
2157 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2158 && (count_insns_for_constant (remainder, 0) <=
2159 count_insns_for_constant (remainder, best_start)))
2160 best_start = 0;
2162 /* Now start emitting the insns. */
2163 i = best_start;
2166 int end;
2168 if (i <= 0)
2169 i += 32;
2170 if (remainder & (3 << (i - 2)))
2172 end = i - 8;
2173 if (end < 0)
2174 end += 32;
2175 temp1 = remainder & ((0x0ff << end)
2176 | ((i < end) ? (0xff >> (32 - end)) : 0));
2177 remainder &= ~temp1;
2179 if (generate)
2181 rtx new_src, temp1_rtx;
2183 if (code == SET || code == MINUS)
2185 new_src = (subtargets ? gen_reg_rtx (mode) : target);
2186 if (can_invert && code != MINUS)
2187 temp1 = ~temp1;
2189 else
2191 if (remainder && subtargets)
2192 new_src = gen_reg_rtx (mode);
2193 else
2194 new_src = target;
2195 if (can_invert)
2196 temp1 = ~temp1;
2197 else if (can_negate)
2198 temp1 = -temp1;
2201 temp1 = trunc_int_for_mode (temp1, mode);
2202 temp1_rtx = GEN_INT (temp1);
2204 if (code == SET)
2206 else if (code == MINUS)
2207 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2208 else
2209 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2211 emit_constant_insn (cond,
2212 gen_rtx_SET (VOIDmode, new_src,
2213 temp1_rtx));
2214 source = new_src;
2217 if (code == SET)
2219 can_invert = 0;
2220 code = PLUS;
2222 else if (code == MINUS)
2223 code = PLUS;
2225 insns++;
2226 i -= 6;
2228 i -= 2;
2230 while (remainder);
2233 return insns;
2236 /* Canonicalize a comparison so that we are more likely to recognize it.
2237 This can be done for a few constant compares, where we can make the
2238 immediate value easier to load. */
2240 enum rtx_code
2241 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2243 unsigned HOST_WIDE_INT i = INTVAL (*op1);
2245 switch (code)
2247 case EQ:
2248 case NE:
2249 return code;
2251 case GT:
2252 case LE:
2253 if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2254 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2256 *op1 = GEN_INT (i + 1);
2257 return code == GT ? GE : LT;
2259 break;
2261 case GE:
2262 case LT:
2263 if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2264 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2266 *op1 = GEN_INT (i - 1);
2267 return code == GE ? GT : LE;
2269 break;
2271 case GTU:
2272 case LEU:
2273 if (i != ~((unsigned HOST_WIDE_INT) 0)
2274 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2276 *op1 = GEN_INT (i + 1);
2277 return code == GTU ? GEU : LTU;
2279 break;
2281 case GEU:
2282 case LTU:
2283 if (i != 0
2284 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2286 *op1 = GEN_INT (i - 1);
2287 return code == GEU ? GTU : LEU;
2289 break;
2291 default:
2292 gcc_unreachable ();
2295 return code;
2299 /* Define how to find the value returned by a function. */
2302 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2304 enum machine_mode mode;
2305 int unsignedp ATTRIBUTE_UNUSED;
2306 rtx r ATTRIBUTE_UNUSED;
2308 mode = TYPE_MODE (type);
2309 /* Promote integer types. */
2310 if (INTEGRAL_TYPE_P (type))
2311 PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2313 /* Promotes small structs returned in a register to full-word size
2314 for big-endian AAPCS. */
2315 if (arm_return_in_msb (type))
2317 HOST_WIDE_INT size = int_size_in_bytes (type);
2318 if (size % UNITS_PER_WORD != 0)
2320 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2321 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2325 return LIBCALL_VALUE(mode);
2328 /* Determine the amount of memory needed to store the possible return
2329 registers of an untyped call. */
2331 arm_apply_result_size (void)
2333 int size = 16;
2335 if (TARGET_ARM)
2337 if (TARGET_HARD_FLOAT_ABI)
2339 if (TARGET_FPA)
2340 size += 12;
2341 if (TARGET_MAVERICK)
2342 size += 8;
2344 if (TARGET_IWMMXT_ABI)
2345 size += 8;
2348 return size;
2351 /* Decide whether a type should be returned in memory (true)
2352 or in a register (false). This is called by the macro
2353 RETURN_IN_MEMORY. */
2355 arm_return_in_memory (tree type)
2357 HOST_WIDE_INT size;
2359 if (!AGGREGATE_TYPE_P (type) &&
2360 !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2361 /* All simple types are returned in registers.
2362 For AAPCS, complex types are treated the same as aggregates. */
2363 return 0;
2365 size = int_size_in_bytes (type);
2367 if (arm_abi != ARM_ABI_APCS)
2369 /* ATPCS and later return aggregate types in memory only if they are
2370 larger than a word (or are variable size). */
2371 return (size < 0 || size > UNITS_PER_WORD);
2374 /* For the arm-wince targets we choose to be compatible with Microsoft's
2375 ARM and Thumb compilers, which always return aggregates in memory. */
2376 #ifndef ARM_WINCE
2377 /* All structures/unions bigger than one word are returned in memory.
2378 Also catch the case where int_size_in_bytes returns -1. In this case
2379 the aggregate is either huge or of variable size, and in either case
2380 we will want to return it via memory and not in a register. */
2381 if (size < 0 || size > UNITS_PER_WORD)
2382 return 1;
2384 if (TREE_CODE (type) == RECORD_TYPE)
2386 tree field;
2388 /* For a struct the APCS says that we only return in a register
2389 if the type is 'integer like' and every addressable element
2390 has an offset of zero. For practical purposes this means
2391 that the structure can have at most one non bit-field element
2392 and that this element must be the first one in the structure. */
2394 /* Find the first field, ignoring non FIELD_DECL things which will
2395 have been created by C++. */
2396 for (field = TYPE_FIELDS (type);
2397 field && TREE_CODE (field) != FIELD_DECL;
2398 field = TREE_CHAIN (field))
2399 continue;
2401 if (field == NULL)
2402 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
2404 /* Check that the first field is valid for returning in a register. */
2406 /* ... Floats are not allowed */
2407 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2408 return 1;
2410 /* ... Aggregates that are not themselves valid for returning in
2411 a register are not allowed. */
2412 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2413 return 1;
2415 /* Now check the remaining fields, if any. Only bitfields are allowed,
2416 since they are not addressable. */
2417 for (field = TREE_CHAIN (field);
2418 field;
2419 field = TREE_CHAIN (field))
2421 if (TREE_CODE (field) != FIELD_DECL)
2422 continue;
2424 if (!DECL_BIT_FIELD_TYPE (field))
2425 return 1;
2428 return 0;
2431 if (TREE_CODE (type) == UNION_TYPE)
2433 tree field;
2435 /* Unions can be returned in registers if every element is
2436 integral, or can be returned in an integer register. */
2437 for (field = TYPE_FIELDS (type);
2438 field;
2439 field = TREE_CHAIN (field))
2441 if (TREE_CODE (field) != FIELD_DECL)
2442 continue;
2444 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2445 return 1;
2447 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2448 return 1;
2451 return 0;
2453 #endif /* not ARM_WINCE */
2455 /* Return all other types in memory. */
2456 return 1;
2459 /* Indicate whether or not words of a double are in big-endian order. */
2462 arm_float_words_big_endian (void)
2464 if (TARGET_MAVERICK)
2465 return 0;
2467 /* For FPA, float words are always big-endian. For VFP, floats words
2468 follow the memory system mode. */
2470 if (TARGET_FPA)
2472 return 1;
2475 if (TARGET_VFP)
2476 return (TARGET_BIG_END ? 1 : 0);
2478 return 1;
2481 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2482 for a call to a function whose data type is FNTYPE.
2483 For a library call, FNTYPE is NULL. */
2484 void
2485 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2486 rtx libname ATTRIBUTE_UNUSED,
2487 tree fndecl ATTRIBUTE_UNUSED)
2489 /* On the ARM, the offset starts at 0. */
2490 pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2491 pcum->iwmmxt_nregs = 0;
2492 pcum->can_split = true;
2494 pcum->call_cookie = CALL_NORMAL;
2496 if (TARGET_LONG_CALLS)
2497 pcum->call_cookie = CALL_LONG;
2499 /* Check for long call/short call attributes. The attributes
2500 override any command line option. */
2501 if (fntype)
2503 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2504 pcum->call_cookie = CALL_SHORT;
2505 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2506 pcum->call_cookie = CALL_LONG;
2509 /* Varargs vectors are treated the same as long long.
2510 named_count avoids having to change the way arm handles 'named' */
2511 pcum->named_count = 0;
2512 pcum->nargs = 0;
2514 if (TARGET_REALLY_IWMMXT && fntype)
2516 tree fn_arg;
2518 for (fn_arg = TYPE_ARG_TYPES (fntype);
2519 fn_arg;
2520 fn_arg = TREE_CHAIN (fn_arg))
2521 pcum->named_count += 1;
2523 if (! pcum->named_count)
2524 pcum->named_count = INT_MAX;
2529 /* Return true if mode/type need doubleword alignment. */
2530 bool
2531 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2533 return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2534 || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2538 /* Determine where to put an argument to a function.
2539 Value is zero to push the argument on the stack,
2540 or a hard register in which to store the argument.
2542 MODE is the argument's machine mode.
2543 TYPE is the data type of the argument (as a tree).
2544 This is null for libcalls where that information may
2545 not be available.
2546 CUM is a variable of type CUMULATIVE_ARGS which gives info about
2547 the preceding args and about the function being called.
2548 NAMED is nonzero if this argument is a named parameter
2549 (otherwise it is an extra parameter matching an ellipsis). */
2552 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2553 tree type, int named)
2555 int nregs;
2557 /* Varargs vectors are treated the same as long long.
2558 named_count avoids having to change the way arm handles 'named' */
2559 if (TARGET_IWMMXT_ABI
2560 && arm_vector_mode_supported_p (mode)
2561 && pcum->named_count > pcum->nargs + 1)
2563 if (pcum->iwmmxt_nregs <= 9)
2564 return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2565 else
2567 pcum->can_split = false;
2568 return NULL_RTX;
2572 /* Put doubleword aligned quantities in even register pairs. */
2573 if (pcum->nregs & 1
2574 && ARM_DOUBLEWORD_ALIGN
2575 && arm_needs_doubleword_align (mode, type))
2576 pcum->nregs++;
2578 if (mode == VOIDmode)
2579 /* Compute operand 2 of the call insn. */
2580 return GEN_INT (pcum->call_cookie);
2582 /* Only allow splitting an arg between regs and memory if all preceding
2583 args were allocated to regs. For args passed by reference we only count
2584 the reference pointer. */
2585 if (pcum->can_split)
2586 nregs = 1;
2587 else
2588 nregs = ARM_NUM_REGS2 (mode, type);
2590 if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2591 return NULL_RTX;
2593 return gen_rtx_REG (mode, pcum->nregs);
2596 static int
2597 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2598 tree type, bool named ATTRIBUTE_UNUSED)
2600 int nregs = pcum->nregs;
2602 if (arm_vector_mode_supported_p (mode))
2603 return 0;
2605 if (NUM_ARG_REGS > nregs
2606 && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2607 && pcum->can_split)
2608 return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2610 return 0;
2613 /* Variable sized types are passed by reference. This is a GCC
2614 extension to the ARM ABI. */
2616 static bool
2617 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2618 enum machine_mode mode ATTRIBUTE_UNUSED,
2619 tree type, bool named ATTRIBUTE_UNUSED)
2621 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2624 /* Encode the current state of the #pragma [no_]long_calls. */
2625 typedef enum
2627 OFF, /* No #pramgma [no_]long_calls is in effect. */
2628 LONG, /* #pragma long_calls is in effect. */
2629 SHORT /* #pragma no_long_calls is in effect. */
2630 } arm_pragma_enum;
2632 static arm_pragma_enum arm_pragma_long_calls = OFF;
2634 void
2635 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2637 arm_pragma_long_calls = LONG;
2640 void
2641 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2643 arm_pragma_long_calls = SHORT;
2646 void
2647 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2649 arm_pragma_long_calls = OFF;
2652 /* Table of machine attributes. */
2653 const struct attribute_spec arm_attribute_table[] =
2655 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2656 /* Function calls made to this symbol must be done indirectly, because
2657 it may lie outside of the 26 bit addressing range of a normal function
2658 call. */
2659 { "long_call", 0, 0, false, true, true, NULL },
2660 /* Whereas these functions are always known to reside within the 26 bit
2661 addressing range. */
2662 { "short_call", 0, 0, false, true, true, NULL },
2663 /* Interrupt Service Routines have special prologue and epilogue requirements. */
2664 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
2665 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
2666 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2667 #ifdef ARM_PE
2668 /* ARM/PE has three new attributes:
2669 interfacearm - ?
2670 dllexport - for exporting a function/variable that will live in a dll
2671 dllimport - for importing a function/variable from a dll
2673 Microsoft allows multiple declspecs in one __declspec, separating
2674 them with spaces. We do NOT support this. Instead, use __declspec
2675 multiple times.
2677 { "dllimport", 0, 0, true, false, false, NULL },
2678 { "dllexport", 0, 0, true, false, false, NULL },
2679 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2680 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2681 { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
2682 { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
2683 { "notshared", 0, 0, false, true, false, arm_handle_notshared_attribute },
2684 #endif
2685 { NULL, 0, 0, false, false, false, NULL }
2688 /* Handle an attribute requiring a FUNCTION_DECL;
2689 arguments as in struct attribute_spec.handler. */
2690 static tree
2691 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2692 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2694 if (TREE_CODE (*node) != FUNCTION_DECL)
2696 warning (0, "%qs attribute only applies to functions",
2697 IDENTIFIER_POINTER (name));
2698 *no_add_attrs = true;
2701 return NULL_TREE;
2704 /* Handle an "interrupt" or "isr" attribute;
2705 arguments as in struct attribute_spec.handler. */
2706 static tree
2707 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2708 bool *no_add_attrs)
2710 if (DECL_P (*node))
2712 if (TREE_CODE (*node) != FUNCTION_DECL)
2714 warning (0, "%qs attribute only applies to functions",
2715 IDENTIFIER_POINTER (name));
2716 *no_add_attrs = true;
2718 /* FIXME: the argument if any is checked for type attributes;
2719 should it be checked for decl ones? */
2721 else
2723 if (TREE_CODE (*node) == FUNCTION_TYPE
2724 || TREE_CODE (*node) == METHOD_TYPE)
2726 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2728 warning (0, "%qs attribute ignored", IDENTIFIER_POINTER (name));
2729 *no_add_attrs = true;
2732 else if (TREE_CODE (*node) == POINTER_TYPE
2733 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2734 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2735 && arm_isr_value (args) != ARM_FT_UNKNOWN)
2737 *node = build_variant_type_copy (*node);
2738 TREE_TYPE (*node) = build_type_attribute_variant
2739 (TREE_TYPE (*node),
2740 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2741 *no_add_attrs = true;
2743 else
2745 /* Possibly pass this attribute on from the type to a decl. */
2746 if (flags & ((int) ATTR_FLAG_DECL_NEXT
2747 | (int) ATTR_FLAG_FUNCTION_NEXT
2748 | (int) ATTR_FLAG_ARRAY_NEXT))
2750 *no_add_attrs = true;
2751 return tree_cons (name, args, NULL_TREE);
2753 else
2755 warning (0, "%qs attribute ignored", IDENTIFIER_POINTER (name));
2760 return NULL_TREE;
2763 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2764 /* Handle the "notshared" attribute. This attribute is another way of
2765 requesting hidden visibility. ARM's compiler supports
2766 "__declspec(notshared)"; we support the same thing via an
2767 attribute. */
2769 static tree
2770 arm_handle_notshared_attribute (tree *node,
2771 tree name ATTRIBUTE_UNUSED,
2772 tree args ATTRIBUTE_UNUSED,
2773 int flags ATTRIBUTE_UNUSED,
2774 bool *no_add_attrs)
2776 tree decl = TYPE_NAME (*node);
2778 if (decl)
2780 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2781 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2782 *no_add_attrs = false;
2784 return NULL_TREE;
2786 #endif
2788 /* Return 0 if the attributes for two types are incompatible, 1 if they
2789 are compatible, and 2 if they are nearly compatible (which causes a
2790 warning to be generated). */
2791 static int
2792 arm_comp_type_attributes (tree type1, tree type2)
2794 int l1, l2, s1, s2;
2796 /* Check for mismatch of non-default calling convention. */
2797 if (TREE_CODE (type1) != FUNCTION_TYPE)
2798 return 1;
2800 /* Check for mismatched call attributes. */
2801 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2802 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2803 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2804 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2806 /* Only bother to check if an attribute is defined. */
2807 if (l1 | l2 | s1 | s2)
2809 /* If one type has an attribute, the other must have the same attribute. */
2810 if ((l1 != l2) || (s1 != s2))
2811 return 0;
2813 /* Disallow mixed attributes. */
2814 if ((l1 & s2) || (l2 & s1))
2815 return 0;
2818 /* Check for mismatched ISR attribute. */
2819 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2820 if (! l1)
2821 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2822 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2823 if (! l2)
2824 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2825 if (l1 != l2)
2826 return 0;
2828 return 1;
2831 /* Encode long_call or short_call attribute by prefixing
2832 symbol name in DECL with a special character FLAG. */
2833 void
2834 arm_encode_call_attribute (tree decl, int flag)
2836 const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2837 int len = strlen (str);
2838 char * newstr;
2840 /* Do not allow weak functions to be treated as short call. */
2841 if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2842 return;
2844 newstr = alloca (len + 2);
2845 newstr[0] = flag;
2846 strcpy (newstr + 1, str);
2848 newstr = (char *) ggc_alloc_string (newstr, len + 1);
2849 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2852 /* Assigns default attributes to newly defined type. This is used to
2853 set short_call/long_call attributes for function types of
2854 functions defined inside corresponding #pragma scopes. */
2855 static void
2856 arm_set_default_type_attributes (tree type)
2858 /* Add __attribute__ ((long_call)) to all functions, when
2859 inside #pragma long_calls or __attribute__ ((short_call)),
2860 when inside #pragma no_long_calls. */
2861 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2863 tree type_attr_list, attr_name;
2864 type_attr_list = TYPE_ATTRIBUTES (type);
2866 if (arm_pragma_long_calls == LONG)
2867 attr_name = get_identifier ("long_call");
2868 else if (arm_pragma_long_calls == SHORT)
2869 attr_name = get_identifier ("short_call");
2870 else
2871 return;
2873 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2874 TYPE_ATTRIBUTES (type) = type_attr_list;
2878 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2879 defined within the current compilation unit. If this cannot be
2880 determined, then 0 is returned. */
2881 static int
2882 current_file_function_operand (rtx sym_ref)
2884 /* This is a bit of a fib. A function will have a short call flag
2885 applied to its name if it has the short call attribute, or it has
2886 already been defined within the current compilation unit. */
2887 if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2888 return 1;
2890 /* The current function is always defined within the current compilation
2891 unit. If it s a weak definition however, then this may not be the real
2892 definition of the function, and so we have to say no. */
2893 if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2894 && !DECL_WEAK (current_function_decl))
2895 return 1;
2897 /* We cannot make the determination - default to returning 0. */
2898 return 0;
2901 /* Return nonzero if a 32 bit "long_call" should be generated for
2902 this call. We generate a long_call if the function:
2904 a. has an __attribute__((long call))
2905 or b. is within the scope of a #pragma long_calls
2906 or c. the -mlong-calls command line switch has been specified
2907 . and either:
2908 1. -ffunction-sections is in effect
2909 or 2. the current function has __attribute__ ((section))
2910 or 3. the target function has __attribute__ ((section))
2912 However we do not generate a long call if the function:
2914 d. has an __attribute__ ((short_call))
2915 or e. is inside the scope of a #pragma no_long_calls
2916 or f. is defined within the current compilation unit.
2918 This function will be called by C fragments contained in the machine
2919 description file. SYM_REF and CALL_COOKIE correspond to the matched
2920 rtl operands. CALL_SYMBOL is used to distinguish between
2921 two different callers of the function. It is set to 1 in the
2922 "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2923 and "call_value" patterns. This is because of the difference in the
2924 SYM_REFs passed by these patterns. */
2926 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2928 if (!call_symbol)
2930 if (GET_CODE (sym_ref) != MEM)
2931 return 0;
2933 sym_ref = XEXP (sym_ref, 0);
2936 if (GET_CODE (sym_ref) != SYMBOL_REF)
2937 return 0;
2939 if (call_cookie & CALL_SHORT)
2940 return 0;
2942 if (TARGET_LONG_CALLS)
2944 if (flag_function_sections
2945 || DECL_SECTION_NAME (current_function_decl))
2946 /* c.3 is handled by the definition of the
2947 ARM_DECLARE_FUNCTION_SIZE macro. */
2948 return 1;
2951 if (current_file_function_operand (sym_ref))
2952 return 0;
2954 return (call_cookie & CALL_LONG)
2955 || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2956 || TARGET_LONG_CALLS;
2959 /* Return nonzero if it is ok to make a tail-call to DECL. */
2960 static bool
2961 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
2963 int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2965 if (cfun->machine->sibcall_blocked)
2966 return false;
2968 /* Never tailcall something for which we have no decl, or if we
2969 are in Thumb mode. */
2970 if (decl == NULL || TARGET_THUMB)
2971 return false;
2973 /* Get the calling method. */
2974 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2975 call_type = CALL_SHORT;
2976 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2977 call_type = CALL_LONG;
2979 /* Cannot tail-call to long calls, since these are out of range of
2980 a branch instruction. However, if not compiling PIC, we know
2981 we can reach the symbol if it is in this compilation unit. */
2982 if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2983 return false;
2985 /* If we are interworking and the function is not declared static
2986 then we can't tail-call it unless we know that it exists in this
2987 compilation unit (since it might be a Thumb routine). */
2988 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2989 return false;
2991 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
2992 if (IS_INTERRUPT (arm_current_func_type ()))
2993 return false;
2995 /* Everything else is ok. */
2996 return true;
3000 /* Addressing mode support functions. */
3002 /* Return nonzero if X is a legitimate immediate operand when compiling
3003 for PIC. */
3005 legitimate_pic_operand_p (rtx x)
3007 if (CONSTANT_P (x)
3008 && flag_pic
3009 && (GET_CODE (x) == SYMBOL_REF
3010 || (GET_CODE (x) == CONST
3011 && GET_CODE (XEXP (x, 0)) == PLUS
3012 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
3013 return 0;
3015 return 1;
3019 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3021 if (GET_CODE (orig) == SYMBOL_REF
3022 || GET_CODE (orig) == LABEL_REF)
3024 #ifndef AOF_ASSEMBLER
3025 rtx pic_ref, address;
3026 #endif
3027 rtx insn;
3028 int subregs = 0;
3030 if (reg == 0)
3032 gcc_assert (!no_new_pseudos);
3033 reg = gen_reg_rtx (Pmode);
3035 subregs = 1;
3038 #ifdef AOF_ASSEMBLER
3039 /* The AOF assembler can generate relocations for these directly, and
3040 understands that the PIC register has to be added into the offset. */
3041 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3042 #else
3043 if (subregs)
3044 address = gen_reg_rtx (Pmode);
3045 else
3046 address = reg;
3048 if (TARGET_ARM)
3049 emit_insn (gen_pic_load_addr_arm (address, orig));
3050 else
3051 emit_insn (gen_pic_load_addr_thumb (address, orig));
3053 if ((GET_CODE (orig) == LABEL_REF
3054 || (GET_CODE (orig) == SYMBOL_REF &&
3055 SYMBOL_REF_LOCAL_P (orig)))
3056 && NEED_GOT_RELOC)
3057 pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
3058 else
3060 pic_ref = gen_const_mem (Pmode,
3061 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3062 address));
3065 insn = emit_move_insn (reg, pic_ref);
3066 #endif
3067 current_function_uses_pic_offset_table = 1;
3068 /* Put a REG_EQUAL note on this insn, so that it can be optimized
3069 by loop. */
3070 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3071 REG_NOTES (insn));
3072 return reg;
3074 else if (GET_CODE (orig) == CONST)
3076 rtx base, offset;
3078 if (GET_CODE (XEXP (orig, 0)) == PLUS
3079 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3080 return orig;
3082 if (reg == 0)
3084 gcc_assert (!no_new_pseudos);
3085 reg = gen_reg_rtx (Pmode);
3088 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3090 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3091 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3092 base == reg ? 0 : reg);
3094 if (GET_CODE (offset) == CONST_INT)
3096 /* The base register doesn't really matter, we only want to
3097 test the index for the appropriate mode. */
3098 if (!arm_legitimate_index_p (mode, offset, SET, 0))
3100 gcc_assert (!no_new_pseudos);
3101 offset = force_reg (Pmode, offset);
3104 if (GET_CODE (offset) == CONST_INT)
3105 return plus_constant (base, INTVAL (offset));
3108 if (GET_MODE_SIZE (mode) > 4
3109 && (GET_MODE_CLASS (mode) == MODE_INT
3110 || TARGET_SOFT_FLOAT))
3112 emit_insn (gen_addsi3 (reg, base, offset));
3113 return reg;
3116 return gen_rtx_PLUS (Pmode, base, offset);
3119 return orig;
3123 /* Find a spare low register to use during the prolog of a function. */
3125 static int
3126 thumb_find_work_register (unsigned long pushed_regs_mask)
3128 int reg;
3130 /* Check the argument registers first as these are call-used. The
3131 register allocation order means that sometimes r3 might be used
3132 but earlier argument registers might not, so check them all. */
3133 for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3134 if (!regs_ever_live[reg])
3135 return reg;
3137 /* Before going on to check the call-saved registers we can try a couple
3138 more ways of deducing that r3 is available. The first is when we are
3139 pushing anonymous arguments onto the stack and we have less than 4
3140 registers worth of fixed arguments(*). In this case r3 will be part of
3141 the variable argument list and so we can be sure that it will be
3142 pushed right at the start of the function. Hence it will be available
3143 for the rest of the prologue.
3144 (*): ie current_function_pretend_args_size is greater than 0. */
3145 if (cfun->machine->uses_anonymous_args
3146 && current_function_pretend_args_size > 0)
3147 return LAST_ARG_REGNUM;
3149 /* The other case is when we have fixed arguments but less than 4 registers
3150 worth. In this case r3 might be used in the body of the function, but
3151 it is not being used to convey an argument into the function. In theory
3152 we could just check current_function_args_size to see how many bytes are
3153 being passed in argument registers, but it seems that it is unreliable.
3154 Sometimes it will have the value 0 when in fact arguments are being
3155 passed. (See testcase execute/20021111-1.c for an example). So we also
3156 check the args_info.nregs field as well. The problem with this field is
3157 that it makes no allowances for arguments that are passed to the
3158 function but which are not used. Hence we could miss an opportunity
3159 when a function has an unused argument in r3. But it is better to be
3160 safe than to be sorry. */
3161 if (! cfun->machine->uses_anonymous_args
3162 && current_function_args_size >= 0
3163 && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3164 && cfun->args_info.nregs < 4)
3165 return LAST_ARG_REGNUM;
3167 /* Otherwise look for a call-saved register that is going to be pushed. */
3168 for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3169 if (pushed_regs_mask & (1 << reg))
3170 return reg;
3172 /* Something went wrong - thumb_compute_save_reg_mask()
3173 should have arranged for a suitable register to be pushed. */
3174 gcc_unreachable ();
3178 /* Generate code to load the PIC register. In thumb mode SCRATCH is a
3179 low register. */
3181 void
3182 arm_load_pic_register (unsigned int scratch)
3184 #ifndef AOF_ASSEMBLER
3185 rtx l1, pic_tmp, pic_tmp2, pic_rtx;
3186 rtx global_offset_table;
3188 if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3189 return;
3191 gcc_assert (flag_pic);
3193 l1 = gen_label_rtx ();
3195 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3196 /* On the ARM the PC register contains 'dot + 8' at the time of the
3197 addition, on the Thumb it is 'dot + 4'. */
3198 pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
3199 if (GOT_PCREL)
3200 pic_tmp2 = gen_rtx_CONST (VOIDmode,
3201 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3202 else
3203 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3205 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3207 if (TARGET_ARM)
3209 emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3210 emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
3212 else
3214 if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3216 /* We will have pushed the pic register, so should always be
3217 able to find a work register. */
3218 pic_tmp = gen_rtx_REG (SImode, scratch);
3219 emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3220 emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3222 else
3223 emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3224 emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
3227 /* Need to emit this whether or not we obey regdecls,
3228 since setjmp/longjmp can cause life info to screw up. */
3229 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3230 #endif /* AOF_ASSEMBLER */
3234 /* Return nonzero if X is valid as an ARM state addressing register. */
3235 static int
3236 arm_address_register_rtx_p (rtx x, int strict_p)
3238 int regno;
3240 if (GET_CODE (x) != REG)
3241 return 0;
3243 regno = REGNO (x);
3245 if (strict_p)
3246 return ARM_REGNO_OK_FOR_BASE_P (regno);
3248 return (regno <= LAST_ARM_REGNUM
3249 || regno >= FIRST_PSEUDO_REGISTER
3250 || regno == FRAME_POINTER_REGNUM
3251 || regno == ARG_POINTER_REGNUM);
3254 /* Return nonzero if X is a valid ARM state address operand. */
3256 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3257 int strict_p)
3259 bool use_ldrd;
3260 enum rtx_code code = GET_CODE (x);
3262 if (arm_address_register_rtx_p (x, strict_p))
3263 return 1;
3265 use_ldrd = (TARGET_LDRD
3266 && (mode == DImode
3267 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3269 if (code == POST_INC || code == PRE_DEC
3270 || ((code == PRE_INC || code == POST_DEC)
3271 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3272 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3274 else if ((code == POST_MODIFY || code == PRE_MODIFY)
3275 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3276 && GET_CODE (XEXP (x, 1)) == PLUS
3277 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3279 rtx addend = XEXP (XEXP (x, 1), 1);
3281 /* Don't allow ldrd post increment by register because it's hard
3282 to fixup invalid register choices. */
3283 if (use_ldrd
3284 && GET_CODE (x) == POST_MODIFY
3285 && GET_CODE (addend) == REG)
3286 return 0;
3288 return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3289 && arm_legitimate_index_p (mode, addend, outer, strict_p));
3292 /* After reload constants split into minipools will have addresses
3293 from a LABEL_REF. */
3294 else if (reload_completed
3295 && (code == LABEL_REF
3296 || (code == CONST
3297 && GET_CODE (XEXP (x, 0)) == PLUS
3298 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3299 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3300 return 1;
3302 else if (mode == TImode)
3303 return 0;
3305 else if (code == PLUS)
3307 rtx xop0 = XEXP (x, 0);
3308 rtx xop1 = XEXP (x, 1);
3310 return ((arm_address_register_rtx_p (xop0, strict_p)
3311 && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3312 || (arm_address_register_rtx_p (xop1, strict_p)
3313 && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3316 #if 0
3317 /* Reload currently can't handle MINUS, so disable this for now */
3318 else if (GET_CODE (x) == MINUS)
3320 rtx xop0 = XEXP (x, 0);
3321 rtx xop1 = XEXP (x, 1);
3323 return (arm_address_register_rtx_p (xop0, strict_p)
3324 && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3326 #endif
3328 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3329 && code == SYMBOL_REF
3330 && CONSTANT_POOL_ADDRESS_P (x)
3331 && ! (flag_pic
3332 && symbol_mentioned_p (get_pool_constant (x))))
3333 return 1;
3335 return 0;
3338 /* Return nonzero if INDEX is valid for an address index operand in
3339 ARM state. */
3340 static int
3341 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3342 int strict_p)
3344 HOST_WIDE_INT range;
3345 enum rtx_code code = GET_CODE (index);
3347 /* Standard coprocessor addressing modes. */
3348 if (TARGET_HARD_FLOAT
3349 && (TARGET_FPA || TARGET_MAVERICK)
3350 && (GET_MODE_CLASS (mode) == MODE_FLOAT
3351 || (TARGET_MAVERICK && mode == DImode)))
3352 return (code == CONST_INT && INTVAL (index) < 1024
3353 && INTVAL (index) > -1024
3354 && (INTVAL (index) & 3) == 0);
3356 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3357 return (code == CONST_INT
3358 && INTVAL (index) < 1024
3359 && INTVAL (index) > -1024
3360 && (INTVAL (index) & 3) == 0);
3362 if (arm_address_register_rtx_p (index, strict_p)
3363 && (GET_MODE_SIZE (mode) <= 4))
3364 return 1;
3366 if (mode == DImode || mode == DFmode)
3368 if (code == CONST_INT)
3370 HOST_WIDE_INT val = INTVAL (index);
3372 if (TARGET_LDRD)
3373 return val > -256 && val < 256;
3374 else
3375 return val > -4096 && val < 4092;
3378 return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3381 if (GET_MODE_SIZE (mode) <= 4
3382 && ! (arm_arch4
3383 && (mode == HImode
3384 || (mode == QImode && outer == SIGN_EXTEND))))
3386 if (code == MULT)
3388 rtx xiop0 = XEXP (index, 0);
3389 rtx xiop1 = XEXP (index, 1);
3391 return ((arm_address_register_rtx_p (xiop0, strict_p)
3392 && power_of_two_operand (xiop1, SImode))
3393 || (arm_address_register_rtx_p (xiop1, strict_p)
3394 && power_of_two_operand (xiop0, SImode)));
3396 else if (code == LSHIFTRT || code == ASHIFTRT
3397 || code == ASHIFT || code == ROTATERT)
3399 rtx op = XEXP (index, 1);
3401 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3402 && GET_CODE (op) == CONST_INT
3403 && INTVAL (op) > 0
3404 && INTVAL (op) <= 31);
3408 /* For ARM v4 we may be doing a sign-extend operation during the
3409 load. */
3410 if (arm_arch4)
3412 if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3413 range = 256;
3414 else
3415 range = 4096;
3417 else
3418 range = (mode == HImode) ? 4095 : 4096;
3420 return (code == CONST_INT
3421 && INTVAL (index) < range
3422 && INTVAL (index) > -range);
3425 /* Return nonzero if X is valid as a Thumb state base register. */
3426 static int
3427 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3429 int regno;
3431 if (GET_CODE (x) != REG)
3432 return 0;
3434 regno = REGNO (x);
3436 if (strict_p)
3437 return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3439 return (regno <= LAST_LO_REGNUM
3440 || regno > LAST_VIRTUAL_REGISTER
3441 || regno == FRAME_POINTER_REGNUM
3442 || (GET_MODE_SIZE (mode) >= 4
3443 && (regno == STACK_POINTER_REGNUM
3444 || regno >= FIRST_PSEUDO_REGISTER
3445 || x == hard_frame_pointer_rtx
3446 || x == arg_pointer_rtx)));
3449 /* Return nonzero if x is a legitimate index register. This is the case
3450 for any base register that can access a QImode object. */
3451 inline static int
3452 thumb_index_register_rtx_p (rtx x, int strict_p)
3454 return thumb_base_register_rtx_p (x, QImode, strict_p);
3457 /* Return nonzero if x is a legitimate Thumb-state address.
3459 The AP may be eliminated to either the SP or the FP, so we use the
3460 least common denominator, e.g. SImode, and offsets from 0 to 64.
3462 ??? Verify whether the above is the right approach.
3464 ??? Also, the FP may be eliminated to the SP, so perhaps that
3465 needs special handling also.
3467 ??? Look at how the mips16 port solves this problem. It probably uses
3468 better ways to solve some of these problems.
3470 Although it is not incorrect, we don't accept QImode and HImode
3471 addresses based on the frame pointer or arg pointer until the
3472 reload pass starts. This is so that eliminating such addresses
3473 into stack based ones won't produce impossible code. */
3475 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3477 /* ??? Not clear if this is right. Experiment. */
3478 if (GET_MODE_SIZE (mode) < 4
3479 && !(reload_in_progress || reload_completed)
3480 && (reg_mentioned_p (frame_pointer_rtx, x)
3481 || reg_mentioned_p (arg_pointer_rtx, x)
3482 || reg_mentioned_p (virtual_incoming_args_rtx, x)
3483 || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3484 || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3485 || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3486 return 0;
3488 /* Accept any base register. SP only in SImode or larger. */
3489 else if (thumb_base_register_rtx_p (x, mode, strict_p))
3490 return 1;
3492 /* This is PC relative data before arm_reorg runs. */
3493 else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3494 && GET_CODE (x) == SYMBOL_REF
3495 && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3496 return 1;
3498 /* This is PC relative data after arm_reorg runs. */
3499 else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3500 && (GET_CODE (x) == LABEL_REF
3501 || (GET_CODE (x) == CONST
3502 && GET_CODE (XEXP (x, 0)) == PLUS
3503 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3504 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3505 return 1;
3507 /* Post-inc indexing only supported for SImode and larger. */
3508 else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3509 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3510 return 1;
3512 else if (GET_CODE (x) == PLUS)
3514 /* REG+REG address can be any two index registers. */
3515 /* We disallow FRAME+REG addressing since we know that FRAME
3516 will be replaced with STACK, and SP relative addressing only
3517 permits SP+OFFSET. */
3518 if (GET_MODE_SIZE (mode) <= 4
3519 && XEXP (x, 0) != frame_pointer_rtx
3520 && XEXP (x, 1) != frame_pointer_rtx
3521 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3522 && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3523 return 1;
3525 /* REG+const has 5-7 bit offset for non-SP registers. */
3526 else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3527 || XEXP (x, 0) == arg_pointer_rtx)
3528 && GET_CODE (XEXP (x, 1)) == CONST_INT
3529 && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3530 return 1;
3532 /* REG+const has 10 bit offset for SP, but only SImode and
3533 larger is supported. */
3534 /* ??? Should probably check for DI/DFmode overflow here
3535 just like GO_IF_LEGITIMATE_OFFSET does. */
3536 else if (GET_CODE (XEXP (x, 0)) == REG
3537 && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3538 && GET_MODE_SIZE (mode) >= 4
3539 && GET_CODE (XEXP (x, 1)) == CONST_INT
3540 && INTVAL (XEXP (x, 1)) >= 0
3541 && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3542 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3543 return 1;
3545 else if (GET_CODE (XEXP (x, 0)) == REG
3546 && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3547 && GET_MODE_SIZE (mode) >= 4
3548 && GET_CODE (XEXP (x, 1)) == CONST_INT
3549 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3550 return 1;
3553 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3554 && GET_MODE_SIZE (mode) == 4
3555 && GET_CODE (x) == SYMBOL_REF
3556 && CONSTANT_POOL_ADDRESS_P (x)
3557 && !(flag_pic
3558 && symbol_mentioned_p (get_pool_constant (x))))
3559 return 1;
3561 return 0;
3564 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3565 instruction of mode MODE. */
3567 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3569 switch (GET_MODE_SIZE (mode))
3571 case 1:
3572 return val >= 0 && val < 32;
3574 case 2:
3575 return val >= 0 && val < 64 && (val & 1) == 0;
3577 default:
3578 return (val >= 0
3579 && (val + GET_MODE_SIZE (mode)) <= 128
3580 && (val & 3) == 0);
3584 /* Try machine-dependent ways of modifying an illegitimate address
3585 to be legitimate. If we find one, return the new, valid address. */
3587 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3589 if (GET_CODE (x) == PLUS)
3591 rtx xop0 = XEXP (x, 0);
3592 rtx xop1 = XEXP (x, 1);
3594 if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3595 xop0 = force_reg (SImode, xop0);
3597 if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3598 xop1 = force_reg (SImode, xop1);
3600 if (ARM_BASE_REGISTER_RTX_P (xop0)
3601 && GET_CODE (xop1) == CONST_INT)
3603 HOST_WIDE_INT n, low_n;
3604 rtx base_reg, val;
3605 n = INTVAL (xop1);
3607 /* VFP addressing modes actually allow greater offsets, but for
3608 now we just stick with the lowest common denominator. */
3609 if (mode == DImode
3610 || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3612 low_n = n & 0x0f;
3613 n &= ~0x0f;
3614 if (low_n > 4)
3616 n += 16;
3617 low_n -= 16;
3620 else
3622 low_n = ((mode) == TImode ? 0
3623 : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3624 n -= low_n;
3627 base_reg = gen_reg_rtx (SImode);
3628 val = force_operand (gen_rtx_PLUS (SImode, xop0,
3629 GEN_INT (n)), NULL_RTX);
3630 emit_move_insn (base_reg, val);
3631 x = (low_n == 0 ? base_reg
3632 : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3634 else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3635 x = gen_rtx_PLUS (SImode, xop0, xop1);
3638 /* XXX We don't allow MINUS any more -- see comment in
3639 arm_legitimate_address_p (). */
3640 else if (GET_CODE (x) == MINUS)
3642 rtx xop0 = XEXP (x, 0);
3643 rtx xop1 = XEXP (x, 1);
3645 if (CONSTANT_P (xop0))
3646 xop0 = force_reg (SImode, xop0);
3648 if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3649 xop1 = force_reg (SImode, xop1);
3651 if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3652 x = gen_rtx_MINUS (SImode, xop0, xop1);
3655 if (flag_pic)
3657 /* We need to find and carefully transform any SYMBOL and LABEL
3658 references; so go back to the original address expression. */
3659 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3661 if (new_x != orig_x)
3662 x = new_x;
3665 return x;
3669 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3670 to be legitimate. If we find one, return the new, valid address. */
3672 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3674 if (GET_CODE (x) == PLUS
3675 && GET_CODE (XEXP (x, 1)) == CONST_INT
3676 && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3677 || INTVAL (XEXP (x, 1)) < 0))
3679 rtx xop0 = XEXP (x, 0);
3680 rtx xop1 = XEXP (x, 1);
3681 HOST_WIDE_INT offset = INTVAL (xop1);
3683 /* Try and fold the offset into a biasing of the base register and
3684 then offsetting that. Don't do this when optimizing for space
3685 since it can cause too many CSEs. */
3686 if (optimize_size && offset >= 0
3687 && offset < 256 + 31 * GET_MODE_SIZE (mode))
3689 HOST_WIDE_INT delta;
3691 if (offset >= 256)
3692 delta = offset - (256 - GET_MODE_SIZE (mode));
3693 else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3694 delta = 31 * GET_MODE_SIZE (mode);
3695 else
3696 delta = offset & (~31 * GET_MODE_SIZE (mode));
3698 xop0 = force_operand (plus_constant (xop0, offset - delta),
3699 NULL_RTX);
3700 x = plus_constant (xop0, delta);
3702 else if (offset < 0 && offset > -256)
3703 /* Small negative offsets are best done with a subtract before the
3704 dereference, forcing these into a register normally takes two
3705 instructions. */
3706 x = force_operand (x, NULL_RTX);
3707 else
3709 /* For the remaining cases, force the constant into a register. */
3710 xop1 = force_reg (SImode, xop1);
3711 x = gen_rtx_PLUS (SImode, xop0, xop1);
3714 else if (GET_CODE (x) == PLUS
3715 && s_register_operand (XEXP (x, 1), SImode)
3716 && !s_register_operand (XEXP (x, 0), SImode))
3718 rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3720 x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3723 if (flag_pic)
3725 /* We need to find and carefully transform any SYMBOL and LABEL
3726 references; so go back to the original address expression. */
3727 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3729 if (new_x != orig_x)
3730 x = new_x;
3733 return x;
3738 #define REG_OR_SUBREG_REG(X) \
3739 (GET_CODE (X) == REG \
3740 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3742 #define REG_OR_SUBREG_RTX(X) \
3743 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3745 #ifndef COSTS_N_INSNS
3746 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3747 #endif
3748 static inline int
3749 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3751 enum machine_mode mode = GET_MODE (x);
3753 switch (code)
3755 case ASHIFT:
3756 case ASHIFTRT:
3757 case LSHIFTRT:
3758 case ROTATERT:
3759 case PLUS:
3760 case MINUS:
3761 case COMPARE:
3762 case NEG:
3763 case NOT:
3764 return COSTS_N_INSNS (1);
3766 case MULT:
3767 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3769 int cycles = 0;
3770 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3772 while (i)
3774 i >>= 2;
3775 cycles++;
3777 return COSTS_N_INSNS (2) + cycles;
3779 return COSTS_N_INSNS (1) + 16;
3781 case SET:
3782 return (COSTS_N_INSNS (1)
3783 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3784 + GET_CODE (SET_DEST (x)) == MEM));
3786 case CONST_INT:
3787 if (outer == SET)
3789 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3790 return 0;
3791 if (thumb_shiftable_const (INTVAL (x)))
3792 return COSTS_N_INSNS (2);
3793 return COSTS_N_INSNS (3);
3795 else if ((outer == PLUS || outer == COMPARE)
3796 && INTVAL (x) < 256 && INTVAL (x) > -256)
3797 return 0;
3798 else if (outer == AND
3799 && INTVAL (x) < 256 && INTVAL (x) >= -256)
3800 return COSTS_N_INSNS (1);
3801 else if (outer == ASHIFT || outer == ASHIFTRT
3802 || outer == LSHIFTRT)
3803 return 0;
3804 return COSTS_N_INSNS (2);
3806 case CONST:
3807 case CONST_DOUBLE:
3808 case LABEL_REF:
3809 case SYMBOL_REF:
3810 return COSTS_N_INSNS (3);
3812 case UDIV:
3813 case UMOD:
3814 case DIV:
3815 case MOD:
3816 return 100;
3818 case TRUNCATE:
3819 return 99;
3821 case AND:
3822 case XOR:
3823 case IOR:
3824 /* XXX guess. */
3825 return 8;
3827 case MEM:
3828 /* XXX another guess. */
3829 /* Memory costs quite a lot for the first word, but subsequent words
3830 load at the equivalent of a single insn each. */
3831 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3832 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3833 ? 4 : 0));
3835 case IF_THEN_ELSE:
3836 /* XXX a guess. */
3837 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3838 return 14;
3839 return 2;
3841 case ZERO_EXTEND:
3842 /* XXX still guessing. */
3843 switch (GET_MODE (XEXP (x, 0)))
3845 case QImode:
3846 return (1 + (mode == DImode ? 4 : 0)
3847 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3849 case HImode:
3850 return (4 + (mode == DImode ? 4 : 0)
3851 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3853 case SImode:
3854 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3856 default:
3857 return 99;
3860 default:
3861 return 99;
3866 /* Worker routine for arm_rtx_costs. */
3867 static inline int
3868 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3870 enum machine_mode mode = GET_MODE (x);
3871 enum rtx_code subcode;
3872 int extra_cost;
3874 switch (code)
3876 case MEM:
3877 /* Memory costs quite a lot for the first word, but subsequent words
3878 load at the equivalent of a single insn each. */
3879 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3880 + (GET_CODE (x) == SYMBOL_REF
3881 && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3883 case DIV:
3884 case MOD:
3885 case UDIV:
3886 case UMOD:
3887 return optimize_size ? COSTS_N_INSNS (2) : 100;
3889 case ROTATE:
3890 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3891 return 4;
3892 /* Fall through */
3893 case ROTATERT:
3894 if (mode != SImode)
3895 return 8;
3896 /* Fall through */
3897 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3898 if (mode == DImode)
3899 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3900 + ((GET_CODE (XEXP (x, 0)) == REG
3901 || (GET_CODE (XEXP (x, 0)) == SUBREG
3902 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3903 ? 0 : 8));
3904 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3905 || (GET_CODE (XEXP (x, 0)) == SUBREG
3906 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3907 ? 0 : 4)
3908 + ((GET_CODE (XEXP (x, 1)) == REG
3909 || (GET_CODE (XEXP (x, 1)) == SUBREG
3910 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3911 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3912 ? 0 : 4));
3914 case MINUS:
3915 if (mode == DImode)
3916 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3917 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3918 || (GET_CODE (XEXP (x, 0)) == CONST_INT
3919 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3920 ? 0 : 8));
3922 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3923 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3924 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3925 && arm_const_double_rtx (XEXP (x, 1))))
3926 ? 0 : 8)
3927 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3928 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3929 && arm_const_double_rtx (XEXP (x, 0))))
3930 ? 0 : 8));
3932 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3933 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3934 && REG_OR_SUBREG_REG (XEXP (x, 1))))
3935 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3936 || subcode == ASHIFTRT || subcode == LSHIFTRT
3937 || subcode == ROTATE || subcode == ROTATERT
3938 || (subcode == MULT
3939 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3940 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3941 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3942 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3943 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3944 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3945 && REG_OR_SUBREG_REG (XEXP (x, 0))))
3946 return 1;
3947 /* Fall through */
3949 case PLUS:
3950 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3951 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3952 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3953 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3954 && arm_const_double_rtx (XEXP (x, 1))))
3955 ? 0 : 8));
3957 /* Fall through */
3958 case AND: case XOR: case IOR:
3959 extra_cost = 0;
3961 /* Normally the frame registers will be spilt into reg+const during
3962 reload, so it is a bad idea to combine them with other instructions,
3963 since then they might not be moved outside of loops. As a compromise
3964 we allow integration with ops that have a constant as their second
3965 operand. */
3966 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3967 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3968 && GET_CODE (XEXP (x, 1)) != CONST_INT)
3969 || (REG_OR_SUBREG_REG (XEXP (x, 0))
3970 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3971 extra_cost = 4;
3973 if (mode == DImode)
3974 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3975 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3976 || (GET_CODE (XEXP (x, 1)) == CONST_INT
3977 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3978 ? 0 : 8));
3980 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3981 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3982 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3983 || (GET_CODE (XEXP (x, 1)) == CONST_INT
3984 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3985 ? 0 : 4));
3987 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3988 return (1 + extra_cost
3989 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3990 || subcode == LSHIFTRT || subcode == ASHIFTRT
3991 || subcode == ROTATE || subcode == ROTATERT
3992 || (subcode == MULT
3993 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3994 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3995 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3996 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3997 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3998 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3999 ? 0 : 4));
4001 return 8;
4003 case MULT:
4004 /* This should have been handled by the CPU specific routines. */
4005 gcc_unreachable ();
4007 case TRUNCATE:
4008 if (arm_arch3m && mode == SImode
4009 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4010 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4011 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
4012 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4013 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4014 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
4015 return 8;
4016 return 99;
4018 case NEG:
4019 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4020 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4021 /* Fall through */
4022 case NOT:
4023 if (mode == DImode)
4024 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4026 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4028 case IF_THEN_ELSE:
4029 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4030 return 14;
4031 return 2;
4033 case COMPARE:
4034 return 1;
4036 case ABS:
4037 return 4 + (mode == DImode ? 4 : 0);
4039 case SIGN_EXTEND:
4040 if (GET_MODE (XEXP (x, 0)) == QImode)
4041 return (4 + (mode == DImode ? 4 : 0)
4042 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4043 /* Fall through */
4044 case ZERO_EXTEND:
4045 switch (GET_MODE (XEXP (x, 0)))
4047 case QImode:
4048 return (1 + (mode == DImode ? 4 : 0)
4049 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4051 case HImode:
4052 return (4 + (mode == DImode ? 4 : 0)
4053 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4055 case SImode:
4056 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4058 case V8QImode:
4059 case V4HImode:
4060 case V2SImode:
4061 case V4QImode:
4062 case V2HImode:
4063 return 1;
4065 default:
4066 gcc_unreachable ();
4068 gcc_unreachable ();
4070 case CONST_INT:
4071 if (const_ok_for_arm (INTVAL (x)))
4072 return outer == SET ? 2 : -1;
4073 else if (outer == AND
4074 && const_ok_for_arm (~INTVAL (x)))
4075 return -1;
4076 else if ((outer == COMPARE
4077 || outer == PLUS || outer == MINUS)
4078 && const_ok_for_arm (-INTVAL (x)))
4079 return -1;
4080 else
4081 return 5;
4083 case CONST:
4084 case LABEL_REF:
4085 case SYMBOL_REF:
4086 return 6;
4088 case CONST_DOUBLE:
4089 if (arm_const_double_rtx (x))
4090 return outer == SET ? 2 : -1;
4091 else if ((outer == COMPARE || outer == PLUS)
4092 && neg_const_double_rtx_ok_for_fpa (x))
4093 return -1;
4094 return 7;
4096 default:
4097 return 99;
4101 /* RTX costs when optimizing for size. */
4102 static bool
4103 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4105 enum machine_mode mode = GET_MODE (x);
4107 if (TARGET_THUMB)
4109 /* XXX TBD. For now, use the standard costs. */
4110 *total = thumb_rtx_costs (x, code, outer_code);
4111 return true;
4114 switch (code)
4116 case MEM:
4117 /* A memory access costs 1 insn if the mode is small, or the address is
4118 a single register, otherwise it costs one insn per word. */
4119 if (REG_P (XEXP (x, 0)))
4120 *total = COSTS_N_INSNS (1);
4121 else
4122 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4123 return true;
4125 case DIV:
4126 case MOD:
4127 case UDIV:
4128 case UMOD:
4129 /* Needs a libcall, so it costs about this. */
4130 *total = COSTS_N_INSNS (2);
4131 return false;
4133 case ROTATE:
4134 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4136 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4137 return true;
4139 /* Fall through */
4140 case ROTATERT:
4141 case ASHIFT:
4142 case LSHIFTRT:
4143 case ASHIFTRT:
4144 if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4146 *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4147 return true;
4149 else if (mode == SImode)
4151 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4152 /* Slightly disparage register shifts, but not by much. */
4153 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4154 *total += 1 + rtx_cost (XEXP (x, 1), code);
4155 return true;
4158 /* Needs a libcall. */
4159 *total = COSTS_N_INSNS (2);
4160 return false;
4162 case MINUS:
4163 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4165 *total = COSTS_N_INSNS (1);
4166 return false;
4169 if (mode == SImode)
4171 enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4172 enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4174 if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4175 || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4176 || subcode1 == ROTATE || subcode1 == ROTATERT
4177 || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4178 || subcode1 == ASHIFTRT)
4180 /* It's just the cost of the two operands. */
4181 *total = 0;
4182 return false;
4185 *total = COSTS_N_INSNS (1);
4186 return false;
4189 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4190 return false;
4192 case PLUS:
4193 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4195 *total = COSTS_N_INSNS (1);
4196 return false;
4199 /* Fall through */
4200 case AND: case XOR: case IOR:
4201 if (mode == SImode)
4203 enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4205 if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4206 || subcode == LSHIFTRT || subcode == ASHIFTRT
4207 || (code == AND && subcode == NOT))
4209 /* It's just the cost of the two operands. */
4210 *total = 0;
4211 return false;
4215 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4216 return false;
4218 case MULT:
4219 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4220 return false;
4222 case NEG:
4223 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4224 *total = COSTS_N_INSNS (1);
4225 /* Fall through */
4226 case NOT:
4227 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4229 return false;
4231 case IF_THEN_ELSE:
4232 *total = 0;
4233 return false;
4235 case COMPARE:
4236 if (cc_register (XEXP (x, 0), VOIDmode))
4237 * total = 0;
4238 else
4239 *total = COSTS_N_INSNS (1);
4240 return false;
4242 case ABS:
4243 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4244 *total = COSTS_N_INSNS (1);
4245 else
4246 *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4247 return false;
4249 case SIGN_EXTEND:
4250 *total = 0;
4251 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4253 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4254 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4256 if (mode == DImode)
4257 *total += COSTS_N_INSNS (1);
4258 return false;
4260 case ZERO_EXTEND:
4261 *total = 0;
4262 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4264 switch (GET_MODE (XEXP (x, 0)))
4266 case QImode:
4267 *total += COSTS_N_INSNS (1);
4268 break;
4270 case HImode:
4271 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4273 case SImode:
4274 break;
4276 default:
4277 *total += COSTS_N_INSNS (2);
4281 if (mode == DImode)
4282 *total += COSTS_N_INSNS (1);
4284 return false;
4286 case CONST_INT:
4287 if (const_ok_for_arm (INTVAL (x)))
4288 *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4289 else if (const_ok_for_arm (~INTVAL (x)))
4290 *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4291 else if (const_ok_for_arm (-INTVAL (x)))
4293 if (outer_code == COMPARE || outer_code == PLUS
4294 || outer_code == MINUS)
4295 *total = 0;
4296 else
4297 *total = COSTS_N_INSNS (1);
4299 else
4300 *total = COSTS_N_INSNS (2);
4301 return true;
4303 case CONST:
4304 case LABEL_REF:
4305 case SYMBOL_REF:
4306 *total = COSTS_N_INSNS (2);
4307 return true;
4309 case CONST_DOUBLE:
4310 *total = COSTS_N_INSNS (4);
4311 return true;
4313 default:
4314 if (mode != VOIDmode)
4315 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4316 else
4317 *total = COSTS_N_INSNS (4); /* How knows? */
4318 return false;
4322 /* RTX costs for cores with a slow MUL implementation. */
4324 static bool
4325 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4327 enum machine_mode mode = GET_MODE (x);
4329 if (TARGET_THUMB)
4331 *total = thumb_rtx_costs (x, code, outer_code);
4332 return true;
4335 switch (code)
4337 case MULT:
4338 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4339 || mode == DImode)
4341 *total = 30;
4342 return true;
4345 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4347 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4348 & (unsigned HOST_WIDE_INT) 0xffffffff);
4349 int cost, const_ok = const_ok_for_arm (i);
4350 int j, booth_unit_size;
4352 /* Tune as appropriate. */
4353 cost = const_ok ? 4 : 8;
4354 booth_unit_size = 2;
4355 for (j = 0; i && j < 32; j += booth_unit_size)
4357 i >>= booth_unit_size;
4358 cost += 2;
4361 *total = cost;
4362 return true;
4365 *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4366 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4367 return true;
4369 default:
4370 *total = arm_rtx_costs_1 (x, code, outer_code);
4371 return true;
4376 /* RTX cost for cores with a fast multiply unit (M variants). */
4378 static bool
4379 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4381 enum machine_mode mode = GET_MODE (x);
4383 if (TARGET_THUMB)
4385 *total = thumb_rtx_costs (x, code, outer_code);
4386 return true;
4389 switch (code)
4391 case MULT:
4392 /* There is no point basing this on the tuning, since it is always the
4393 fast variant if it exists at all. */
4394 if (mode == DImode
4395 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4396 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4397 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4399 *total = 8;
4400 return true;
4404 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4405 || mode == DImode)
4407 *total = 30;
4408 return true;
4411 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4413 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4414 & (unsigned HOST_WIDE_INT) 0xffffffff);
4415 int cost, const_ok = const_ok_for_arm (i);
4416 int j, booth_unit_size;
4418 /* Tune as appropriate. */
4419 cost = const_ok ? 4 : 8;
4420 booth_unit_size = 8;
4421 for (j = 0; i && j < 32; j += booth_unit_size)
4423 i >>= booth_unit_size;
4424 cost += 2;
4427 *total = cost;
4428 return true;
4431 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4432 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4433 return true;
4435 default:
4436 *total = arm_rtx_costs_1 (x, code, outer_code);
4437 return true;
4442 /* RTX cost for XScale CPUs. */
4444 static bool
4445 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4447 enum machine_mode mode = GET_MODE (x);
4449 if (TARGET_THUMB)
4451 *total = thumb_rtx_costs (x, code, outer_code);
4452 return true;
4455 switch (code)
4457 case MULT:
4458 /* There is no point basing this on the tuning, since it is always the
4459 fast variant if it exists at all. */
4460 if (mode == DImode
4461 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4462 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4463 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4465 *total = 8;
4466 return true;
4470 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4471 || mode == DImode)
4473 *total = 30;
4474 return true;
4477 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4479 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4480 & (unsigned HOST_WIDE_INT) 0xffffffff);
4481 int cost, const_ok = const_ok_for_arm (i);
4482 unsigned HOST_WIDE_INT masked_const;
4484 /* The cost will be related to two insns.
4485 First a load of the constant (MOV or LDR), then a multiply. */
4486 cost = 2;
4487 if (! const_ok)
4488 cost += 1; /* LDR is probably more expensive because
4489 of longer result latency. */
4490 masked_const = i & 0xffff8000;
4491 if (masked_const != 0 && masked_const != 0xffff8000)
4493 masked_const = i & 0xf8000000;
4494 if (masked_const == 0 || masked_const == 0xf8000000)
4495 cost += 1;
4496 else
4497 cost += 2;
4499 *total = cost;
4500 return true;
4503 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4504 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4505 return true;
4507 case COMPARE:
4508 /* A COMPARE of a MULT is slow on XScale; the muls instruction
4509 will stall until the multiplication is complete. */
4510 if (GET_CODE (XEXP (x, 0)) == MULT)
4511 *total = 4 + rtx_cost (XEXP (x, 0), code);
4512 else
4513 *total = arm_rtx_costs_1 (x, code, outer_code);
4514 return true;
4516 default:
4517 *total = arm_rtx_costs_1 (x, code, outer_code);
4518 return true;
4523 /* RTX costs for 9e (and later) cores. */
4525 static bool
4526 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4528 enum machine_mode mode = GET_MODE (x);
4529 int nonreg_cost;
4530 int cost;
4532 if (TARGET_THUMB)
4534 switch (code)
4536 case MULT:
4537 *total = COSTS_N_INSNS (3);
4538 return true;
4540 default:
4541 *total = thumb_rtx_costs (x, code, outer_code);
4542 return true;
4546 switch (code)
4548 case MULT:
4549 /* There is no point basing this on the tuning, since it is always the
4550 fast variant if it exists at all. */
4551 if (mode == DImode
4552 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4553 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4554 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4556 *total = 3;
4557 return true;
4561 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4563 *total = 30;
4564 return true;
4566 if (mode == DImode)
4568 cost = 7;
4569 nonreg_cost = 8;
4571 else
4573 cost = 2;
4574 nonreg_cost = 4;
4578 *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4579 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4580 return true;
4582 default:
4583 *total = arm_rtx_costs_1 (x, code, outer_code);
4584 return true;
4587 /* All address computations that can be done are free, but rtx cost returns
4588 the same for practically all of them. So we weight the different types
4589 of address here in the order (most pref first):
4590 PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
4591 static inline int
4592 arm_arm_address_cost (rtx x)
4594 enum rtx_code c = GET_CODE (x);
4596 if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4597 return 0;
4598 if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4599 return 10;
4601 if (c == PLUS || c == MINUS)
4603 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4604 return 2;
4606 if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4607 return 3;
4609 return 4;
4612 return 6;
4615 static inline int
4616 arm_thumb_address_cost (rtx x)
4618 enum rtx_code c = GET_CODE (x);
4620 if (c == REG)
4621 return 1;
4622 if (c == PLUS
4623 && GET_CODE (XEXP (x, 0)) == REG
4624 && GET_CODE (XEXP (x, 1)) == CONST_INT)
4625 return 1;
4627 return 2;
4630 static int
4631 arm_address_cost (rtx x)
4633 return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4636 static int
4637 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4639 rtx i_pat, d_pat;
4641 /* Some true dependencies can have a higher cost depending
4642 on precisely how certain input operands are used. */
4643 if (arm_tune_xscale
4644 && REG_NOTE_KIND (link) == 0
4645 && recog_memoized (insn) >= 0
4646 && recog_memoized (dep) >= 0)
4648 int shift_opnum = get_attr_shift (insn);
4649 enum attr_type attr_type = get_attr_type (dep);
4651 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4652 operand for INSN. If we have a shifted input operand and the
4653 instruction we depend on is another ALU instruction, then we may
4654 have to account for an additional stall. */
4655 if (shift_opnum != 0
4656 && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4658 rtx shifted_operand;
4659 int opno;
4661 /* Get the shifted operand. */
4662 extract_insn (insn);
4663 shifted_operand = recog_data.operand[shift_opnum];
4665 /* Iterate over all the operands in DEP. If we write an operand
4666 that overlaps with SHIFTED_OPERAND, then we have increase the
4667 cost of this dependency. */
4668 extract_insn (dep);
4669 preprocess_constraints ();
4670 for (opno = 0; opno < recog_data.n_operands; opno++)
4672 /* We can ignore strict inputs. */
4673 if (recog_data.operand_type[opno] == OP_IN)
4674 continue;
4676 if (reg_overlap_mentioned_p (recog_data.operand[opno],
4677 shifted_operand))
4678 return 2;
4683 /* XXX This is not strictly true for the FPA. */
4684 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4685 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4686 return 0;
4688 /* Call insns don't incur a stall, even if they follow a load. */
4689 if (REG_NOTE_KIND (link) == 0
4690 && GET_CODE (insn) == CALL_INSN)
4691 return 1;
4693 if ((i_pat = single_set (insn)) != NULL
4694 && GET_CODE (SET_SRC (i_pat)) == MEM
4695 && (d_pat = single_set (dep)) != NULL
4696 && GET_CODE (SET_DEST (d_pat)) == MEM)
4698 rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4699 /* This is a load after a store, there is no conflict if the load reads
4700 from a cached area. Assume that loads from the stack, and from the
4701 constant pool are cached, and that others will miss. This is a
4702 hack. */
4704 if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4705 || reg_mentioned_p (stack_pointer_rtx, src_mem)
4706 || reg_mentioned_p (frame_pointer_rtx, src_mem)
4707 || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4708 return 1;
4711 return cost;
4714 static int fp_consts_inited = 0;
4716 /* Only zero is valid for VFP. Other values are also valid for FPA. */
4717 static const char * const strings_fp[8] =
4719 "0", "1", "2", "3",
4720 "4", "5", "0.5", "10"
4723 static REAL_VALUE_TYPE values_fp[8];
4725 static void
4726 init_fp_table (void)
4728 int i;
4729 REAL_VALUE_TYPE r;
4731 if (TARGET_VFP)
4732 fp_consts_inited = 1;
4733 else
4734 fp_consts_inited = 8;
4736 for (i = 0; i < fp_consts_inited; i++)
4738 r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4739 values_fp[i] = r;
4743 /* Return TRUE if rtx X is a valid immediate FP constant. */
4745 arm_const_double_rtx (rtx x)
4747 REAL_VALUE_TYPE r;
4748 int i;
4750 if (!fp_consts_inited)
4751 init_fp_table ();
4753 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4754 if (REAL_VALUE_MINUS_ZERO (r))
4755 return 0;
4757 for (i = 0; i < fp_consts_inited; i++)
4758 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4759 return 1;
4761 return 0;
4764 /* Return TRUE if rtx X is a valid immediate FPA constant. */
4766 neg_const_double_rtx_ok_for_fpa (rtx x)
4768 REAL_VALUE_TYPE r;
4769 int i;
4771 if (!fp_consts_inited)
4772 init_fp_table ();
4774 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4775 r = REAL_VALUE_NEGATE (r);
4776 if (REAL_VALUE_MINUS_ZERO (r))
4777 return 0;
4779 for (i = 0; i < 8; i++)
4780 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4781 return 1;
4783 return 0;
4786 /* Predicates for `match_operand' and `match_operator'. */
4788 /* Return nonzero if OP is a valid Cirrus memory address pattern. */
4790 cirrus_memory_offset (rtx op)
4792 /* Reject eliminable registers. */
4793 if (! (reload_in_progress || reload_completed)
4794 && ( reg_mentioned_p (frame_pointer_rtx, op)
4795 || reg_mentioned_p (arg_pointer_rtx, op)
4796 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4797 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4798 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4799 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4800 return 0;
4802 if (GET_CODE (op) == MEM)
4804 rtx ind;
4806 ind = XEXP (op, 0);
4808 /* Match: (mem (reg)). */
4809 if (GET_CODE (ind) == REG)
4810 return 1;
4812 /* Match:
4813 (mem (plus (reg)
4814 (const))). */
4815 if (GET_CODE (ind) == PLUS
4816 && GET_CODE (XEXP (ind, 0)) == REG
4817 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4818 && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4819 return 1;
4822 return 0;
4825 /* Return TRUE if OP is a valid VFP memory address pattern.
4826 WB if true if writeback address modes are allowed. */
4829 arm_coproc_mem_operand (rtx op, bool wb)
4831 rtx ind;
4833 /* Reject eliminable registers. */
4834 if (! (reload_in_progress || reload_completed)
4835 && ( reg_mentioned_p (frame_pointer_rtx, op)
4836 || reg_mentioned_p (arg_pointer_rtx, op)
4837 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4838 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4839 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4840 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4841 return FALSE;
4843 /* Constants are converted into offsets from labels. */
4844 if (GET_CODE (op) != MEM)
4845 return FALSE;
4847 ind = XEXP (op, 0);
4849 if (reload_completed
4850 && (GET_CODE (ind) == LABEL_REF
4851 || (GET_CODE (ind) == CONST
4852 && GET_CODE (XEXP (ind, 0)) == PLUS
4853 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4854 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4855 return TRUE;
4857 /* Match: (mem (reg)). */
4858 if (GET_CODE (ind) == REG)
4859 return arm_address_register_rtx_p (ind, 0);
4861 /* Autoincremment addressing modes. */
4862 if (wb
4863 && (GET_CODE (ind) == PRE_INC
4864 || GET_CODE (ind) == POST_INC
4865 || GET_CODE (ind) == PRE_DEC
4866 || GET_CODE (ind) == POST_DEC))
4867 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
4869 if (wb
4870 && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
4871 && arm_address_register_rtx_p (XEXP (ind, 0), 0)
4872 && GET_CODE (XEXP (ind, 1)) == PLUS
4873 && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
4874 ind = XEXP (ind, 1);
4876 /* Match:
4877 (plus (reg)
4878 (const)). */
4879 if (GET_CODE (ind) == PLUS
4880 && GET_CODE (XEXP (ind, 0)) == REG
4881 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4882 && GET_CODE (XEXP (ind, 1)) == CONST_INT
4883 && INTVAL (XEXP (ind, 1)) > -1024
4884 && INTVAL (XEXP (ind, 1)) < 1024
4885 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
4886 return TRUE;
4888 return FALSE;
4891 /* Return true if X is a register that will be eliminated later on. */
4893 arm_eliminable_register (rtx x)
4895 return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
4896 || REGNO (x) == ARG_POINTER_REGNUM
4897 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
4898 && REGNO (x) <= LAST_VIRTUAL_REGISTER));
4901 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4902 VFP registers. Otherwise return NO_REGS. */
4904 enum reg_class
4905 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4907 if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
4908 return NO_REGS;
4910 return GENERAL_REGS;
4913 /* Values which must be returned in the most-significant end of the return
4914 register. */
4916 static bool
4917 arm_return_in_msb (tree valtype)
4919 return (TARGET_AAPCS_BASED
4920 && BYTES_BIG_ENDIAN
4921 && (AGGREGATE_TYPE_P (valtype)
4922 || TREE_CODE (valtype) == COMPLEX_TYPE));
4925 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4926 Use by the Cirrus Maverick code which has to workaround
4927 a hardware bug triggered by such instructions. */
4928 static bool
4929 arm_memory_load_p (rtx insn)
4931 rtx body, lhs, rhs;;
4933 if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4934 return false;
4936 body = PATTERN (insn);
4938 if (GET_CODE (body) != SET)
4939 return false;
4941 lhs = XEXP (body, 0);
4942 rhs = XEXP (body, 1);
4944 lhs = REG_OR_SUBREG_RTX (lhs);
4946 /* If the destination is not a general purpose
4947 register we do not have to worry. */
4948 if (GET_CODE (lhs) != REG
4949 || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4950 return false;
4952 /* As well as loads from memory we also have to react
4953 to loads of invalid constants which will be turned
4954 into loads from the minipool. */
4955 return (GET_CODE (rhs) == MEM
4956 || GET_CODE (rhs) == SYMBOL_REF
4957 || note_invalid_constants (insn, -1, false));
4960 /* Return TRUE if INSN is a Cirrus instruction. */
4961 static bool
4962 arm_cirrus_insn_p (rtx insn)
4964 enum attr_cirrus attr;
4966 /* get_attr cannot accept USE or CLOBBER. */
4967 if (!insn
4968 || GET_CODE (insn) != INSN
4969 || GET_CODE (PATTERN (insn)) == USE
4970 || GET_CODE (PATTERN (insn)) == CLOBBER)
4971 return 0;
4973 attr = get_attr_cirrus (insn);
4975 return attr != CIRRUS_NOT;
4978 /* Cirrus reorg for invalid instruction combinations. */
4979 static void
4980 cirrus_reorg (rtx first)
4982 enum attr_cirrus attr;
4983 rtx body = PATTERN (first);
4984 rtx t;
4985 int nops;
4987 /* Any branch must be followed by 2 non Cirrus instructions. */
4988 if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4990 nops = 0;
4991 t = next_nonnote_insn (first);
4993 if (arm_cirrus_insn_p (t))
4994 ++ nops;
4996 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4997 ++ nops;
4999 while (nops --)
5000 emit_insn_after (gen_nop (), first);
5002 return;
5005 /* (float (blah)) is in parallel with a clobber. */
5006 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
5007 body = XVECEXP (body, 0, 0);
5009 if (GET_CODE (body) == SET)
5011 rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
5013 /* cfldrd, cfldr64, cfstrd, cfstr64 must
5014 be followed by a non Cirrus insn. */
5015 if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
5017 if (arm_cirrus_insn_p (next_nonnote_insn (first)))
5018 emit_insn_after (gen_nop (), first);
5020 return;
5022 else if (arm_memory_load_p (first))
5024 unsigned int arm_regno;
5026 /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
5027 ldr/cfmv64hr combination where the Rd field is the same
5028 in both instructions must be split with a non Cirrus
5029 insn. Example:
5031 ldr r0, blah
5033 cfmvsr mvf0, r0. */
5035 /* Get Arm register number for ldr insn. */
5036 if (GET_CODE (lhs) == REG)
5037 arm_regno = REGNO (lhs);
5038 else
5040 gcc_assert (GET_CODE (rhs) == REG);
5041 arm_regno = REGNO (rhs);
5044 /* Next insn. */
5045 first = next_nonnote_insn (first);
5047 if (! arm_cirrus_insn_p (first))
5048 return;
5050 body = PATTERN (first);
5052 /* (float (blah)) is in parallel with a clobber. */
5053 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5054 body = XVECEXP (body, 0, 0);
5056 if (GET_CODE (body) == FLOAT)
5057 body = XEXP (body, 0);
5059 if (get_attr_cirrus (first) == CIRRUS_MOVE
5060 && GET_CODE (XEXP (body, 1)) == REG
5061 && arm_regno == REGNO (XEXP (body, 1)))
5062 emit_insn_after (gen_nop (), first);
5064 return;
5068 /* get_attr cannot accept USE or CLOBBER. */
5069 if (!first
5070 || GET_CODE (first) != INSN
5071 || GET_CODE (PATTERN (first)) == USE
5072 || GET_CODE (PATTERN (first)) == CLOBBER)
5073 return;
5075 attr = get_attr_cirrus (first);
5077 /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5078 must be followed by a non-coprocessor instruction. */
5079 if (attr == CIRRUS_COMPARE)
5081 nops = 0;
5083 t = next_nonnote_insn (first);
5085 if (arm_cirrus_insn_p (t))
5086 ++ nops;
5088 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5089 ++ nops;
5091 while (nops --)
5092 emit_insn_after (gen_nop (), first);
5094 return;
5098 /* Return TRUE if X references a SYMBOL_REF. */
5100 symbol_mentioned_p (rtx x)
5102 const char * fmt;
5103 int i;
5105 if (GET_CODE (x) == SYMBOL_REF)
5106 return 1;
5108 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 (symbol_mentioned_p (XVECEXP (x, i, j)))
5118 return 1;
5120 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5121 return 1;
5124 return 0;
5127 /* Return TRUE if X references a LABEL_REF. */
5129 label_mentioned_p (rtx x)
5131 const char * fmt;
5132 int i;
5134 if (GET_CODE (x) == LABEL_REF)
5135 return 1;
5137 fmt = GET_RTX_FORMAT (GET_CODE (x));
5138 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5140 if (fmt[i] == 'E')
5142 int j;
5144 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5145 if (label_mentioned_p (XVECEXP (x, i, j)))
5146 return 1;
5148 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5149 return 1;
5152 return 0;
5155 enum rtx_code
5156 minmax_code (rtx x)
5158 enum rtx_code code = GET_CODE (x);
5160 switch (code)
5162 case SMAX:
5163 return GE;
5164 case SMIN:
5165 return LE;
5166 case UMIN:
5167 return LEU;
5168 case UMAX:
5169 return GEU;
5170 default:
5171 gcc_unreachable ();
5175 /* Return 1 if memory locations are adjacent. */
5177 adjacent_mem_locations (rtx a, rtx b)
5179 /* We don't guarantee to preserve the order of these memory refs. */
5180 if (volatile_refs_p (a) || volatile_refs_p (b))
5181 return 0;
5183 if ((GET_CODE (XEXP (a, 0)) == REG
5184 || (GET_CODE (XEXP (a, 0)) == PLUS
5185 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5186 && (GET_CODE (XEXP (b, 0)) == REG
5187 || (GET_CODE (XEXP (b, 0)) == PLUS
5188 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5190 HOST_WIDE_INT val0 = 0, val1 = 0;
5191 rtx reg0, reg1;
5192 int val_diff;
5194 if (GET_CODE (XEXP (a, 0)) == PLUS)
5196 reg0 = XEXP (XEXP (a, 0), 0);
5197 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5199 else
5200 reg0 = XEXP (a, 0);
5202 if (GET_CODE (XEXP (b, 0)) == PLUS)
5204 reg1 = XEXP (XEXP (b, 0), 0);
5205 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5207 else
5208 reg1 = XEXP (b, 0);
5210 /* Don't accept any offset that will require multiple
5211 instructions to handle, since this would cause the
5212 arith_adjacentmem pattern to output an overlong sequence. */
5213 if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5214 return 0;
5216 /* Don't allow an eliminable register: register elimination can make
5217 the offset too large. */
5218 if (arm_eliminable_register (reg0))
5219 return 0;
5221 val_diff = val1 - val0;
5223 if (arm_ld_sched)
5225 /* If the target has load delay slots, then there's no benefit
5226 to using an ldm instruction unless the offset is zero and
5227 we are optimizing for size. */
5228 return (optimize_size && (REGNO (reg0) == REGNO (reg1))
5229 && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
5230 && (val_diff == 4 || val_diff == -4));
5233 return ((REGNO (reg0) == REGNO (reg1))
5234 && (val_diff == 4 || val_diff == -4));
5237 return 0;
5241 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5242 HOST_WIDE_INT *load_offset)
5244 int unsorted_regs[4];
5245 HOST_WIDE_INT unsorted_offsets[4];
5246 int order[4];
5247 int base_reg = -1;
5248 int i;
5250 /* Can only handle 2, 3, or 4 insns at present,
5251 though could be easily extended if required. */
5252 gcc_assert (nops >= 2 && nops <= 4);
5254 /* Loop over the operands and check that the memory references are
5255 suitable (i.e. immediate offsets from the same base register). At
5256 the same time, extract the target register, and the memory
5257 offsets. */
5258 for (i = 0; i < nops; i++)
5260 rtx reg;
5261 rtx offset;
5263 /* Convert a subreg of a mem into the mem itself. */
5264 if (GET_CODE (operands[nops + i]) == SUBREG)
5265 operands[nops + i] = alter_subreg (operands + (nops + i));
5267 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
5269 /* Don't reorder volatile memory references; it doesn't seem worth
5270 looking for the case where the order is ok anyway. */
5271 if (MEM_VOLATILE_P (operands[nops + i]))
5272 return 0;
5274 offset = const0_rtx;
5276 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5277 || (GET_CODE (reg) == SUBREG
5278 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5279 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5280 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5281 == REG)
5282 || (GET_CODE (reg) == SUBREG
5283 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5284 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5285 == CONST_INT)))
5287 if (i == 0)
5289 base_reg = REGNO (reg);
5290 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5291 ? REGNO (operands[i])
5292 : REGNO (SUBREG_REG (operands[i])));
5293 order[0] = 0;
5295 else
5297 if (base_reg != (int) REGNO (reg))
5298 /* Not addressed from the same base register. */
5299 return 0;
5301 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5302 ? REGNO (operands[i])
5303 : REGNO (SUBREG_REG (operands[i])));
5304 if (unsorted_regs[i] < unsorted_regs[order[0]])
5305 order[0] = i;
5308 /* If it isn't an integer register, or if it overwrites the
5309 base register but isn't the last insn in the list, then
5310 we can't do this. */
5311 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5312 || (i != nops - 1 && unsorted_regs[i] == base_reg))
5313 return 0;
5315 unsorted_offsets[i] = INTVAL (offset);
5317 else
5318 /* Not a suitable memory address. */
5319 return 0;
5322 /* All the useful information has now been extracted from the
5323 operands into unsorted_regs and unsorted_offsets; additionally,
5324 order[0] has been set to the lowest numbered register in the
5325 list. Sort the registers into order, and check that the memory
5326 offsets are ascending and adjacent. */
5328 for (i = 1; i < nops; i++)
5330 int j;
5332 order[i] = order[i - 1];
5333 for (j = 0; j < nops; j++)
5334 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5335 && (order[i] == order[i - 1]
5336 || unsorted_regs[j] < unsorted_regs[order[i]]))
5337 order[i] = j;
5339 /* Have we found a suitable register? if not, one must be used more
5340 than once. */
5341 if (order[i] == order[i - 1])
5342 return 0;
5344 /* Is the memory address adjacent and ascending? */
5345 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5346 return 0;
5349 if (base)
5351 *base = base_reg;
5353 for (i = 0; i < nops; i++)
5354 regs[i] = unsorted_regs[order[i]];
5356 *load_offset = unsorted_offsets[order[0]];
5359 if (unsorted_offsets[order[0]] == 0)
5360 return 1; /* ldmia */
5362 if (unsorted_offsets[order[0]] == 4)
5363 return 2; /* ldmib */
5365 if (unsorted_offsets[order[nops - 1]] == 0)
5366 return 3; /* ldmda */
5368 if (unsorted_offsets[order[nops - 1]] == -4)
5369 return 4; /* ldmdb */
5371 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5372 if the offset isn't small enough. The reason 2 ldrs are faster
5373 is because these ARMs are able to do more than one cache access
5374 in a single cycle. The ARM9 and StrongARM have Harvard caches,
5375 whilst the ARM8 has a double bandwidth cache. This means that
5376 these cores can do both an instruction fetch and a data fetch in
5377 a single cycle, so the trick of calculating the address into a
5378 scratch register (one of the result regs) and then doing a load
5379 multiple actually becomes slower (and no smaller in code size).
5380 That is the transformation
5382 ldr rd1, [rbase + offset]
5383 ldr rd2, [rbase + offset + 4]
5387 add rd1, rbase, offset
5388 ldmia rd1, {rd1, rd2}
5390 produces worse code -- '3 cycles + any stalls on rd2' instead of
5391 '2 cycles + any stalls on rd2'. On ARMs with only one cache
5392 access per cycle, the first sequence could never complete in less
5393 than 6 cycles, whereas the ldm sequence would only take 5 and
5394 would make better use of sequential accesses if not hitting the
5395 cache.
5397 We cheat here and test 'arm_ld_sched' which we currently know to
5398 only be true for the ARM8, ARM9 and StrongARM. If this ever
5399 changes, then the test below needs to be reworked. */
5400 if (nops == 2 && arm_ld_sched)
5401 return 0;
5403 /* Can't do it without setting up the offset, only do this if it takes
5404 no more than one insn. */
5405 return (const_ok_for_arm (unsorted_offsets[order[0]])
5406 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5409 const char *
5410 emit_ldm_seq (rtx *operands, int nops)
5412 int regs[4];
5413 int base_reg;
5414 HOST_WIDE_INT offset;
5415 char buf[100];
5416 int i;
5418 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5420 case 1:
5421 strcpy (buf, "ldm%?ia\t");
5422 break;
5424 case 2:
5425 strcpy (buf, "ldm%?ib\t");
5426 break;
5428 case 3:
5429 strcpy (buf, "ldm%?da\t");
5430 break;
5432 case 4:
5433 strcpy (buf, "ldm%?db\t");
5434 break;
5436 case 5:
5437 if (offset >= 0)
5438 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5439 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5440 (long) offset);
5441 else
5442 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5443 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5444 (long) -offset);
5445 output_asm_insn (buf, operands);
5446 base_reg = regs[0];
5447 strcpy (buf, "ldm%?ia\t");
5448 break;
5450 default:
5451 gcc_unreachable ();
5454 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5455 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5457 for (i = 1; i < nops; i++)
5458 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5459 reg_names[regs[i]]);
5461 strcat (buf, "}\t%@ phole ldm");
5463 output_asm_insn (buf, operands);
5464 return "";
5468 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5469 HOST_WIDE_INT * load_offset)
5471 int unsorted_regs[4];
5472 HOST_WIDE_INT unsorted_offsets[4];
5473 int order[4];
5474 int base_reg = -1;
5475 int i;
5477 /* Can only handle 2, 3, or 4 insns at present, though could be easily
5478 extended if required. */
5479 gcc_assert (nops >= 2 && nops <= 4);
5481 /* Loop over the operands and check that the memory references are
5482 suitable (i.e. immediate offsets from the same base register). At
5483 the same time, extract the target register, and the memory
5484 offsets. */
5485 for (i = 0; i < nops; i++)
5487 rtx reg;
5488 rtx offset;
5490 /* Convert a subreg of a mem into the mem itself. */
5491 if (GET_CODE (operands[nops + i]) == SUBREG)
5492 operands[nops + i] = alter_subreg (operands + (nops + i));
5494 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
5496 /* Don't reorder volatile memory references; it doesn't seem worth
5497 looking for the case where the order is ok anyway. */
5498 if (MEM_VOLATILE_P (operands[nops + i]))
5499 return 0;
5501 offset = const0_rtx;
5503 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5504 || (GET_CODE (reg) == SUBREG
5505 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5506 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5507 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5508 == REG)
5509 || (GET_CODE (reg) == SUBREG
5510 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5511 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5512 == CONST_INT)))
5514 if (i == 0)
5516 base_reg = REGNO (reg);
5517 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5518 ? REGNO (operands[i])
5519 : REGNO (SUBREG_REG (operands[i])));
5520 order[0] = 0;
5522 else
5524 if (base_reg != (int) REGNO (reg))
5525 /* Not addressed from the same base register. */
5526 return 0;
5528 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5529 ? REGNO (operands[i])
5530 : REGNO (SUBREG_REG (operands[i])));
5531 if (unsorted_regs[i] < unsorted_regs[order[0]])
5532 order[0] = i;
5535 /* If it isn't an integer register, then we can't do this. */
5536 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5537 return 0;
5539 unsorted_offsets[i] = INTVAL (offset);
5541 else
5542 /* Not a suitable memory address. */
5543 return 0;
5546 /* All the useful information has now been extracted from the
5547 operands into unsorted_regs and unsorted_offsets; additionally,
5548 order[0] has been set to the lowest numbered register in the
5549 list. Sort the registers into order, and check that the memory
5550 offsets are ascending and adjacent. */
5552 for (i = 1; i < nops; i++)
5554 int j;
5556 order[i] = order[i - 1];
5557 for (j = 0; j < nops; j++)
5558 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5559 && (order[i] == order[i - 1]
5560 || unsorted_regs[j] < unsorted_regs[order[i]]))
5561 order[i] = j;
5563 /* Have we found a suitable register? if not, one must be used more
5564 than once. */
5565 if (order[i] == order[i - 1])
5566 return 0;
5568 /* Is the memory address adjacent and ascending? */
5569 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5570 return 0;
5573 if (base)
5575 *base = base_reg;
5577 for (i = 0; i < nops; i++)
5578 regs[i] = unsorted_regs[order[i]];
5580 *load_offset = unsorted_offsets[order[0]];
5583 if (unsorted_offsets[order[0]] == 0)
5584 return 1; /* stmia */
5586 if (unsorted_offsets[order[0]] == 4)
5587 return 2; /* stmib */
5589 if (unsorted_offsets[order[nops - 1]] == 0)
5590 return 3; /* stmda */
5592 if (unsorted_offsets[order[nops - 1]] == -4)
5593 return 4; /* stmdb */
5595 return 0;
5598 const char *
5599 emit_stm_seq (rtx *operands, int nops)
5601 int regs[4];
5602 int base_reg;
5603 HOST_WIDE_INT offset;
5604 char buf[100];
5605 int i;
5607 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5609 case 1:
5610 strcpy (buf, "stm%?ia\t");
5611 break;
5613 case 2:
5614 strcpy (buf, "stm%?ib\t");
5615 break;
5617 case 3:
5618 strcpy (buf, "stm%?da\t");
5619 break;
5621 case 4:
5622 strcpy (buf, "stm%?db\t");
5623 break;
5625 default:
5626 gcc_unreachable ();
5629 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5630 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5632 for (i = 1; i < nops; i++)
5633 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5634 reg_names[regs[i]]);
5636 strcat (buf, "}\t%@ phole stm");
5638 output_asm_insn (buf, operands);
5639 return "";
5643 /* Routines for use in generating RTL. */
5646 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5647 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5649 HOST_WIDE_INT offset = *offsetp;
5650 int i = 0, j;
5651 rtx result;
5652 int sign = up ? 1 : -1;
5653 rtx mem, addr;
5655 /* XScale has load-store double instructions, but they have stricter
5656 alignment requirements than load-store multiple, so we cannot
5657 use them.
5659 For XScale ldm requires 2 + NREGS cycles to complete and blocks
5660 the pipeline until completion.
5662 NREGS CYCLES
5668 An ldr instruction takes 1-3 cycles, but does not block the
5669 pipeline.
5671 NREGS CYCLES
5672 1 1-3
5673 2 2-6
5674 3 3-9
5675 4 4-12
5677 Best case ldr will always win. However, the more ldr instructions
5678 we issue, the less likely we are to be able to schedule them well.
5679 Using ldr instructions also increases code size.
5681 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5682 for counts of 3 or 4 regs. */
5683 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5685 rtx seq;
5687 start_sequence ();
5689 for (i = 0; i < count; i++)
5691 addr = plus_constant (from, i * 4 * sign);
5692 mem = adjust_automodify_address (basemem, SImode, addr, offset);
5693 emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5694 offset += 4 * sign;
5697 if (write_back)
5699 emit_move_insn (from, plus_constant (from, count * 4 * sign));
5700 *offsetp = offset;
5703 seq = get_insns ();
5704 end_sequence ();
5706 return seq;
5709 result = gen_rtx_PARALLEL (VOIDmode,
5710 rtvec_alloc (count + (write_back ? 1 : 0)));
5711 if (write_back)
5713 XVECEXP (result, 0, 0)
5714 = gen_rtx_SET (GET_MODE (from), from,
5715 plus_constant (from, count * 4 * sign));
5716 i = 1;
5717 count++;
5720 for (j = 0; i < count; i++, j++)
5722 addr = plus_constant (from, j * 4 * sign);
5723 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5724 XVECEXP (result, 0, i)
5725 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5726 offset += 4 * sign;
5729 if (write_back)
5730 *offsetp = offset;
5732 return result;
5736 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5737 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5739 HOST_WIDE_INT offset = *offsetp;
5740 int i = 0, j;
5741 rtx result;
5742 int sign = up ? 1 : -1;
5743 rtx mem, addr;
5745 /* See arm_gen_load_multiple for discussion of
5746 the pros/cons of ldm/stm usage for XScale. */
5747 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5749 rtx seq;
5751 start_sequence ();
5753 for (i = 0; i < count; i++)
5755 addr = plus_constant (to, i * 4 * sign);
5756 mem = adjust_automodify_address (basemem, SImode, addr, offset);
5757 emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5758 offset += 4 * sign;
5761 if (write_back)
5763 emit_move_insn (to, plus_constant (to, count * 4 * sign));
5764 *offsetp = offset;
5767 seq = get_insns ();
5768 end_sequence ();
5770 return seq;
5773 result = gen_rtx_PARALLEL (VOIDmode,
5774 rtvec_alloc (count + (write_back ? 1 : 0)));
5775 if (write_back)
5777 XVECEXP (result, 0, 0)
5778 = gen_rtx_SET (GET_MODE (to), to,
5779 plus_constant (to, count * 4 * sign));
5780 i = 1;
5781 count++;
5784 for (j = 0; i < count; i++, j++)
5786 addr = plus_constant (to, j * 4 * sign);
5787 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5788 XVECEXP (result, 0, i)
5789 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5790 offset += 4 * sign;
5793 if (write_back)
5794 *offsetp = offset;
5796 return result;
5800 arm_gen_movmemqi (rtx *operands)
5802 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5803 HOST_WIDE_INT srcoffset, dstoffset;
5804 int i;
5805 rtx src, dst, srcbase, dstbase;
5806 rtx part_bytes_reg = NULL;
5807 rtx mem;
5809 if (GET_CODE (operands[2]) != CONST_INT
5810 || GET_CODE (operands[3]) != CONST_INT
5811 || INTVAL (operands[2]) > 64
5812 || INTVAL (operands[3]) & 3)
5813 return 0;
5815 dstbase = operands[0];
5816 srcbase = operands[1];
5818 dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
5819 src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
5821 in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5822 out_words_to_go = INTVAL (operands[2]) / 4;
5823 last_bytes = INTVAL (operands[2]) & 3;
5824 dstoffset = srcoffset = 0;
5826 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5827 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5829 for (i = 0; in_words_to_go >= 2; i+=4)
5831 if (in_words_to_go > 4)
5832 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5833 srcbase, &srcoffset));
5834 else
5835 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
5836 FALSE, srcbase, &srcoffset));
5838 if (out_words_to_go)
5840 if (out_words_to_go > 4)
5841 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5842 dstbase, &dstoffset));
5843 else if (out_words_to_go != 1)
5844 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5845 dst, TRUE,
5846 (last_bytes == 0
5847 ? FALSE : TRUE),
5848 dstbase, &dstoffset));
5849 else
5851 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5852 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5853 if (last_bytes != 0)
5855 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5856 dstoffset += 4;
5861 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5862 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
5865 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
5866 if (out_words_to_go)
5868 rtx sreg;
5870 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5871 sreg = copy_to_reg (mem);
5873 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5874 emit_move_insn (mem, sreg);
5875 in_words_to_go--;
5877 gcc_assert (!in_words_to_go); /* Sanity check */
5880 if (in_words_to_go)
5882 gcc_assert (in_words_to_go > 0);
5884 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5885 part_bytes_reg = copy_to_mode_reg (SImode, mem);
5888 gcc_assert (!last_bytes || part_bytes_reg);
5890 if (BYTES_BIG_ENDIAN && last_bytes)
5892 rtx tmp = gen_reg_rtx (SImode);
5894 /* The bytes we want are in the top end of the word. */
5895 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
5896 GEN_INT (8 * (4 - last_bytes))));
5897 part_bytes_reg = tmp;
5899 while (last_bytes)
5901 mem = adjust_automodify_address (dstbase, QImode,
5902 plus_constant (dst, last_bytes - 1),
5903 dstoffset + last_bytes - 1);
5904 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5906 if (--last_bytes)
5908 tmp = gen_reg_rtx (SImode);
5909 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
5910 part_bytes_reg = tmp;
5915 else
5917 if (last_bytes > 1)
5919 mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
5920 emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
5921 last_bytes -= 2;
5922 if (last_bytes)
5924 rtx tmp = gen_reg_rtx (SImode);
5925 emit_insn (gen_addsi3 (dst, dst, const2_rtx));
5926 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
5927 part_bytes_reg = tmp;
5928 dstoffset += 2;
5932 if (last_bytes)
5934 mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
5935 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5939 return 1;
5942 /* Generate a memory reference for a half word, such that it will be loaded
5943 into the top 16 bits of the word. We can assume that the address is
5944 known to be alignable and of the form reg, or plus (reg, const). */
5947 arm_gen_rotated_half_load (rtx memref)
5949 HOST_WIDE_INT offset = 0;
5950 rtx base = XEXP (memref, 0);
5952 if (GET_CODE (base) == PLUS)
5954 offset = INTVAL (XEXP (base, 1));
5955 base = XEXP (base, 0);
5958 /* If we aren't allowed to generate unaligned addresses, then fail. */
5959 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0))
5960 return NULL;
5962 base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
5964 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
5965 return base;
5967 return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
5970 /* Select a dominance comparison mode if possible for a test of the general
5971 form (OP (COND_OR (X) (Y)) (const_int 0)). We support three forms.
5972 COND_OR == DOM_CC_X_AND_Y => (X && Y)
5973 COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
5974 COND_OR == DOM_CC_X_OR_Y => (X || Y)
5975 In all cases OP will be either EQ or NE, but we don't need to know which
5976 here. If we are unable to support a dominance comparison we return
5977 CC mode. This will then fail to match for the RTL expressions that
5978 generate this call. */
5979 enum machine_mode
5980 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
5982 enum rtx_code cond1, cond2;
5983 int swapped = 0;
5985 /* Currently we will probably get the wrong result if the individual
5986 comparisons are not simple. This also ensures that it is safe to
5987 reverse a comparison if necessary. */
5988 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
5989 != CCmode)
5990 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
5991 != CCmode))
5992 return CCmode;
5994 /* The if_then_else variant of this tests the second condition if the
5995 first passes, but is true if the first fails. Reverse the first
5996 condition to get a true "inclusive-or" expression. */
5997 if (cond_or == DOM_CC_NX_OR_Y)
5998 cond1 = reverse_condition (cond1);
6000 /* If the comparisons are not equal, and one doesn't dominate the other,
6001 then we can't do this. */
6002 if (cond1 != cond2
6003 && !comparison_dominates_p (cond1, cond2)
6004 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6005 return CCmode;
6007 if (swapped)
6009 enum rtx_code temp = cond1;
6010 cond1 = cond2;
6011 cond2 = temp;
6014 switch (cond1)
6016 case EQ:
6017 if (cond_or == DOM_CC_X_AND_Y)
6018 return CC_DEQmode;
6020 switch (cond2)
6022 case EQ: return CC_DEQmode;
6023 case LE: return CC_DLEmode;
6024 case LEU: return CC_DLEUmode;
6025 case GE: return CC_DGEmode;
6026 case GEU: return CC_DGEUmode;
6027 default: gcc_unreachable ();
6030 case LT:
6031 if (cond_or == DOM_CC_X_AND_Y)
6032 return CC_DLTmode;
6034 switch (cond2)
6036 case LT:
6037 return CC_DLTmode;
6038 case LE:
6039 return CC_DLEmode;
6040 case NE:
6041 return CC_DNEmode;
6042 default:
6043 gcc_unreachable ();
6046 case GT:
6047 if (cond_or == DOM_CC_X_AND_Y)
6048 return CC_DGTmode;
6050 switch (cond2)
6052 case GT:
6053 return CC_DGTmode;
6054 case GE:
6055 return CC_DGEmode;
6056 case NE:
6057 return CC_DNEmode;
6058 default:
6059 gcc_unreachable ();
6062 case LTU:
6063 if (cond_or == DOM_CC_X_AND_Y)
6064 return CC_DLTUmode;
6066 switch (cond2)
6068 case LTU:
6069 return CC_DLTUmode;
6070 case LEU:
6071 return CC_DLEUmode;
6072 case NE:
6073 return CC_DNEmode;
6074 default:
6075 gcc_unreachable ();
6078 case GTU:
6079 if (cond_or == DOM_CC_X_AND_Y)
6080 return CC_DGTUmode;
6082 switch (cond2)
6084 case GTU:
6085 return CC_DGTUmode;
6086 case GEU:
6087 return CC_DGEUmode;
6088 case NE:
6089 return CC_DNEmode;
6090 default:
6091 gcc_unreachable ();
6094 /* The remaining cases only occur when both comparisons are the
6095 same. */
6096 case NE:
6097 gcc_assert (cond1 == cond2);
6098 return CC_DNEmode;
6100 case LE:
6101 gcc_assert (cond1 == cond2);
6102 return CC_DLEmode;
6104 case GE:
6105 gcc_assert (cond1 == cond2);
6106 return CC_DGEmode;
6108 case LEU:
6109 gcc_assert (cond1 == cond2);
6110 return CC_DLEUmode;
6112 case GEU:
6113 gcc_assert (cond1 == cond2);
6114 return CC_DGEUmode;
6116 default:
6117 gcc_unreachable ();
6121 enum machine_mode
6122 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6124 /* All floating point compares return CCFP if it is an equality
6125 comparison, and CCFPE otherwise. */
6126 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6128 switch (op)
6130 case EQ:
6131 case NE:
6132 case UNORDERED:
6133 case ORDERED:
6134 case UNLT:
6135 case UNLE:
6136 case UNGT:
6137 case UNGE:
6138 case UNEQ:
6139 case LTGT:
6140 return CCFPmode;
6142 case LT:
6143 case LE:
6144 case GT:
6145 case GE:
6146 if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6147 return CCFPmode;
6148 return CCFPEmode;
6150 default:
6151 gcc_unreachable ();
6155 /* A compare with a shifted operand. Because of canonicalization, the
6156 comparison will have to be swapped when we emit the assembler. */
6157 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6158 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6159 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6160 || GET_CODE (x) == ROTATERT))
6161 return CC_SWPmode;
6163 /* This operation is performed swapped, but since we only rely on the Z
6164 flag we don't need an additional mode. */
6165 if (GET_MODE (y) == SImode && REG_P (y)
6166 && GET_CODE (x) == NEG
6167 && (op == EQ || op == NE))
6168 return CC_Zmode;
6170 /* This is a special case that is used by combine to allow a
6171 comparison of a shifted byte load to be split into a zero-extend
6172 followed by a comparison of the shifted integer (only valid for
6173 equalities and unsigned inequalities). */
6174 if (GET_MODE (x) == SImode
6175 && GET_CODE (x) == ASHIFT
6176 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6177 && GET_CODE (XEXP (x, 0)) == SUBREG
6178 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6179 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6180 && (op == EQ || op == NE
6181 || op == GEU || op == GTU || op == LTU || op == LEU)
6182 && GET_CODE (y) == CONST_INT)
6183 return CC_Zmode;
6185 /* A construct for a conditional compare, if the false arm contains
6186 0, then both conditions must be true, otherwise either condition
6187 must be true. Not all conditions are possible, so CCmode is
6188 returned if it can't be done. */
6189 if (GET_CODE (x) == IF_THEN_ELSE
6190 && (XEXP (x, 2) == const0_rtx
6191 || XEXP (x, 2) == const1_rtx)
6192 && COMPARISON_P (XEXP (x, 0))
6193 && COMPARISON_P (XEXP (x, 1)))
6194 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6195 INTVAL (XEXP (x, 2)));
6197 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
6198 if (GET_CODE (x) == AND
6199 && COMPARISON_P (XEXP (x, 0))
6200 && COMPARISON_P (XEXP (x, 1)))
6201 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6202 DOM_CC_X_AND_Y);
6204 if (GET_CODE (x) == IOR
6205 && COMPARISON_P (XEXP (x, 0))
6206 && COMPARISON_P (XEXP (x, 1)))
6207 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6208 DOM_CC_X_OR_Y);
6210 /* An operation (on Thumb) where we want to test for a single bit.
6211 This is done by shifting that bit up into the top bit of a
6212 scratch register; we can then branch on the sign bit. */
6213 if (TARGET_THUMB
6214 && GET_MODE (x) == SImode
6215 && (op == EQ || op == NE)
6216 && (GET_CODE (x) == ZERO_EXTRACT))
6217 return CC_Nmode;
6219 /* An operation that sets the condition codes as a side-effect, the
6220 V flag is not set correctly, so we can only use comparisons where
6221 this doesn't matter. (For LT and GE we can use "mi" and "pl"
6222 instead.) */
6223 if (GET_MODE (x) == SImode
6224 && y == const0_rtx
6225 && (op == EQ || op == NE || op == LT || op == GE)
6226 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6227 || GET_CODE (x) == AND || GET_CODE (x) == IOR
6228 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6229 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6230 || GET_CODE (x) == LSHIFTRT
6231 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6232 || GET_CODE (x) == ROTATERT
6233 || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6234 return CC_NOOVmode;
6236 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6237 return CC_Zmode;
6239 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6240 && GET_CODE (x) == PLUS
6241 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6242 return CC_Cmode;
6244 return CCmode;
6247 /* X and Y are two things to compare using CODE. Emit the compare insn and
6248 return the rtx for register 0 in the proper mode. FP means this is a
6249 floating point compare: I don't think that it is needed on the arm. */
6251 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6253 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6254 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6256 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6257 gen_rtx_COMPARE (mode, x, y)));
6259 return cc_reg;
6262 /* Generate a sequence of insns that will generate the correct return
6263 address mask depending on the physical architecture that the program
6264 is running on. */
6266 arm_gen_return_addr_mask (void)
6268 rtx reg = gen_reg_rtx (Pmode);
6270 emit_insn (gen_return_addr_mask (reg));
6271 return reg;
6274 void
6275 arm_reload_in_hi (rtx *operands)
6277 rtx ref = operands[1];
6278 rtx base, scratch;
6279 HOST_WIDE_INT offset = 0;
6281 if (GET_CODE (ref) == SUBREG)
6283 offset = SUBREG_BYTE (ref);
6284 ref = SUBREG_REG (ref);
6287 if (GET_CODE (ref) == REG)
6289 /* We have a pseudo which has been spilt onto the stack; there
6290 are two cases here: the first where there is a simple
6291 stack-slot replacement and a second where the stack-slot is
6292 out of range, or is used as a subreg. */
6293 if (reg_equiv_mem[REGNO (ref)])
6295 ref = reg_equiv_mem[REGNO (ref)];
6296 base = find_replacement (&XEXP (ref, 0));
6298 else
6299 /* The slot is out of range, or was dressed up in a SUBREG. */
6300 base = reg_equiv_address[REGNO (ref)];
6302 else
6303 base = find_replacement (&XEXP (ref, 0));
6305 /* Handle the case where the address is too complex to be offset by 1. */
6306 if (GET_CODE (base) == MINUS
6307 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6309 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6311 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6312 base = base_plus;
6314 else if (GET_CODE (base) == PLUS)
6316 /* The addend must be CONST_INT, or we would have dealt with it above. */
6317 HOST_WIDE_INT hi, lo;
6319 offset += INTVAL (XEXP (base, 1));
6320 base = XEXP (base, 0);
6322 /* Rework the address into a legal sequence of insns. */
6323 /* Valid range for lo is -4095 -> 4095 */
6324 lo = (offset >= 0
6325 ? (offset & 0xfff)
6326 : -((-offset) & 0xfff));
6328 /* Corner case, if lo is the max offset then we would be out of range
6329 once we have added the additional 1 below, so bump the msb into the
6330 pre-loading insn(s). */
6331 if (lo == 4095)
6332 lo &= 0x7ff;
6334 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6335 ^ (HOST_WIDE_INT) 0x80000000)
6336 - (HOST_WIDE_INT) 0x80000000);
6338 gcc_assert (hi + lo == offset);
6340 if (hi != 0)
6342 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6344 /* Get the base address; addsi3 knows how to handle constants
6345 that require more than one insn. */
6346 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6347 base = base_plus;
6348 offset = lo;
6352 /* Operands[2] may overlap operands[0] (though it won't overlap
6353 operands[1]), that's why we asked for a DImode reg -- so we can
6354 use the bit that does not overlap. */
6355 if (REGNO (operands[2]) == REGNO (operands[0]))
6356 scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6357 else
6358 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6360 emit_insn (gen_zero_extendqisi2 (scratch,
6361 gen_rtx_MEM (QImode,
6362 plus_constant (base,
6363 offset))));
6364 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6365 gen_rtx_MEM (QImode,
6366 plus_constant (base,
6367 offset + 1))));
6368 if (!BYTES_BIG_ENDIAN)
6369 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6370 gen_rtx_IOR (SImode,
6371 gen_rtx_ASHIFT
6372 (SImode,
6373 gen_rtx_SUBREG (SImode, operands[0], 0),
6374 GEN_INT (8)),
6375 scratch)));
6376 else
6377 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6378 gen_rtx_IOR (SImode,
6379 gen_rtx_ASHIFT (SImode, scratch,
6380 GEN_INT (8)),
6381 gen_rtx_SUBREG (SImode, operands[0],
6382 0))));
6385 /* Handle storing a half-word to memory during reload by synthesizing as two
6386 byte stores. Take care not to clobber the input values until after we
6387 have moved them somewhere safe. This code assumes that if the DImode
6388 scratch in operands[2] overlaps either the input value or output address
6389 in some way, then that value must die in this insn (we absolutely need
6390 two scratch registers for some corner cases). */
6391 void
6392 arm_reload_out_hi (rtx *operands)
6394 rtx ref = operands[0];
6395 rtx outval = operands[1];
6396 rtx base, scratch;
6397 HOST_WIDE_INT offset = 0;
6399 if (GET_CODE (ref) == SUBREG)
6401 offset = SUBREG_BYTE (ref);
6402 ref = SUBREG_REG (ref);
6405 if (GET_CODE (ref) == REG)
6407 /* We have a pseudo which has been spilt onto the stack; there
6408 are two cases here: the first where there is a simple
6409 stack-slot replacement and a second where the stack-slot is
6410 out of range, or is used as a subreg. */
6411 if (reg_equiv_mem[REGNO (ref)])
6413 ref = reg_equiv_mem[REGNO (ref)];
6414 base = find_replacement (&XEXP (ref, 0));
6416 else
6417 /* The slot is out of range, or was dressed up in a SUBREG. */
6418 base = reg_equiv_address[REGNO (ref)];
6420 else
6421 base = find_replacement (&XEXP (ref, 0));
6423 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6425 /* Handle the case where the address is too complex to be offset by 1. */
6426 if (GET_CODE (base) == MINUS
6427 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6429 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6431 /* Be careful not to destroy OUTVAL. */
6432 if (reg_overlap_mentioned_p (base_plus, outval))
6434 /* Updating base_plus might destroy outval, see if we can
6435 swap the scratch and base_plus. */
6436 if (!reg_overlap_mentioned_p (scratch, outval))
6438 rtx tmp = scratch;
6439 scratch = base_plus;
6440 base_plus = tmp;
6442 else
6444 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6446 /* Be conservative and copy OUTVAL into the scratch now,
6447 this should only be necessary if outval is a subreg
6448 of something larger than a word. */
6449 /* XXX Might this clobber base? I can't see how it can,
6450 since scratch is known to overlap with OUTVAL, and
6451 must be wider than a word. */
6452 emit_insn (gen_movhi (scratch_hi, outval));
6453 outval = scratch_hi;
6457 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6458 base = base_plus;
6460 else if (GET_CODE (base) == PLUS)
6462 /* The addend must be CONST_INT, or we would have dealt with it above. */
6463 HOST_WIDE_INT hi, lo;
6465 offset += INTVAL (XEXP (base, 1));
6466 base = XEXP (base, 0);
6468 /* Rework the address into a legal sequence of insns. */
6469 /* Valid range for lo is -4095 -> 4095 */
6470 lo = (offset >= 0
6471 ? (offset & 0xfff)
6472 : -((-offset) & 0xfff));
6474 /* Corner case, if lo is the max offset then we would be out of range
6475 once we have added the additional 1 below, so bump the msb into the
6476 pre-loading insn(s). */
6477 if (lo == 4095)
6478 lo &= 0x7ff;
6480 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6481 ^ (HOST_WIDE_INT) 0x80000000)
6482 - (HOST_WIDE_INT) 0x80000000);
6484 gcc_assert (hi + lo == offset);
6486 if (hi != 0)
6488 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6490 /* Be careful not to destroy OUTVAL. */
6491 if (reg_overlap_mentioned_p (base_plus, outval))
6493 /* Updating base_plus might destroy outval, see if we
6494 can swap the scratch and base_plus. */
6495 if (!reg_overlap_mentioned_p (scratch, outval))
6497 rtx tmp = scratch;
6498 scratch = base_plus;
6499 base_plus = tmp;
6501 else
6503 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6505 /* Be conservative and copy outval into scratch now,
6506 this should only be necessary if outval is a
6507 subreg of something larger than a word. */
6508 /* XXX Might this clobber base? I can't see how it
6509 can, since scratch is known to overlap with
6510 outval. */
6511 emit_insn (gen_movhi (scratch_hi, outval));
6512 outval = scratch_hi;
6516 /* Get the base address; addsi3 knows how to handle constants
6517 that require more than one insn. */
6518 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6519 base = base_plus;
6520 offset = lo;
6524 if (BYTES_BIG_ENDIAN)
6526 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6527 plus_constant (base, offset + 1)),
6528 gen_lowpart (QImode, outval)));
6529 emit_insn (gen_lshrsi3 (scratch,
6530 gen_rtx_SUBREG (SImode, outval, 0),
6531 GEN_INT (8)));
6532 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6533 gen_lowpart (QImode, scratch)));
6535 else
6537 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6538 gen_lowpart (QImode, outval)));
6539 emit_insn (gen_lshrsi3 (scratch,
6540 gen_rtx_SUBREG (SImode, outval, 0),
6541 GEN_INT (8)));
6542 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6543 plus_constant (base, offset + 1)),
6544 gen_lowpart (QImode, scratch)));
6548 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
6549 (padded to the size of a word) should be passed in a register. */
6551 static bool
6552 arm_must_pass_in_stack (enum machine_mode mode, tree type)
6554 if (TARGET_AAPCS_BASED)
6555 return must_pass_in_stack_var_size (mode, type);
6556 else
6557 return must_pass_in_stack_var_size_or_pad (mode, type);
6561 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
6562 Return true if an argument passed on the stack should be padded upwards,
6563 i.e. if the least-significant byte has useful data. */
6565 bool
6566 arm_pad_arg_upward (enum machine_mode mode, tree type)
6568 if (!TARGET_AAPCS_BASED)
6569 return DEFAULT_FUNCTION_ARG_PADDING(mode, type);
6571 if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
6572 return false;
6574 return true;
6578 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
6579 For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
6580 byte of the register has useful data, and return the opposite if the
6581 most significant byte does.
6582 For AAPCS, small aggregates and small complex types are always padded
6583 upwards. */
6585 bool
6586 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
6587 tree type, int first ATTRIBUTE_UNUSED)
6589 if (TARGET_AAPCS_BASED
6590 && BYTES_BIG_ENDIAN
6591 && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
6592 && int_size_in_bytes (type) <= 4)
6593 return true;
6595 /* Otherwise, use default padding. */
6596 return !BYTES_BIG_ENDIAN;
6601 /* Print a symbolic form of X to the debug file, F. */
6602 static void
6603 arm_print_value (FILE *f, rtx x)
6605 switch (GET_CODE (x))
6607 case CONST_INT:
6608 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6609 return;
6611 case CONST_DOUBLE:
6612 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6613 return;
6615 case CONST_VECTOR:
6617 int i;
6619 fprintf (f, "<");
6620 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6622 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6623 if (i < (CONST_VECTOR_NUNITS (x) - 1))
6624 fputc (',', f);
6626 fprintf (f, ">");
6628 return;
6630 case CONST_STRING:
6631 fprintf (f, "\"%s\"", XSTR (x, 0));
6632 return;
6634 case SYMBOL_REF:
6635 fprintf (f, "`%s'", XSTR (x, 0));
6636 return;
6638 case LABEL_REF:
6639 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6640 return;
6642 case CONST:
6643 arm_print_value (f, XEXP (x, 0));
6644 return;
6646 case PLUS:
6647 arm_print_value (f, XEXP (x, 0));
6648 fprintf (f, "+");
6649 arm_print_value (f, XEXP (x, 1));
6650 return;
6652 case PC:
6653 fprintf (f, "pc");
6654 return;
6656 default:
6657 fprintf (f, "????");
6658 return;
6662 /* Routines for manipulation of the constant pool. */
6664 /* Arm instructions cannot load a large constant directly into a
6665 register; they have to come from a pc relative load. The constant
6666 must therefore be placed in the addressable range of the pc
6667 relative load. Depending on the precise pc relative load
6668 instruction the range is somewhere between 256 bytes and 4k. This
6669 means that we often have to dump a constant inside a function, and
6670 generate code to branch around it.
6672 It is important to minimize this, since the branches will slow
6673 things down and make the code larger.
6675 Normally we can hide the table after an existing unconditional
6676 branch so that there is no interruption of the flow, but in the
6677 worst case the code looks like this:
6679 ldr rn, L1
6681 b L2
6682 align
6683 L1: .long value
6687 ldr rn, L3
6689 b L4
6690 align
6691 L3: .long value
6695 We fix this by performing a scan after scheduling, which notices
6696 which instructions need to have their operands fetched from the
6697 constant table and builds the table.
6699 The algorithm starts by building a table of all the constants that
6700 need fixing up and all the natural barriers in the function (places
6701 where a constant table can be dropped without breaking the flow).
6702 For each fixup we note how far the pc-relative replacement will be
6703 able to reach and the offset of the instruction into the function.
6705 Having built the table we then group the fixes together to form
6706 tables that are as large as possible (subject to addressing
6707 constraints) and emit each table of constants after the last
6708 barrier that is within range of all the instructions in the group.
6709 If a group does not contain a barrier, then we forcibly create one
6710 by inserting a jump instruction into the flow. Once the table has
6711 been inserted, the insns are then modified to reference the
6712 relevant entry in the pool.
6714 Possible enhancements to the algorithm (not implemented) are:
6716 1) For some processors and object formats, there may be benefit in
6717 aligning the pools to the start of cache lines; this alignment
6718 would need to be taken into account when calculating addressability
6719 of a pool. */
6721 /* These typedefs are located at the start of this file, so that
6722 they can be used in the prototypes there. This comment is to
6723 remind readers of that fact so that the following structures
6724 can be understood more easily.
6726 typedef struct minipool_node Mnode;
6727 typedef struct minipool_fixup Mfix; */
6729 struct minipool_node
6731 /* Doubly linked chain of entries. */
6732 Mnode * next;
6733 Mnode * prev;
6734 /* The maximum offset into the code that this entry can be placed. While
6735 pushing fixes for forward references, all entries are sorted in order
6736 of increasing max_address. */
6737 HOST_WIDE_INT max_address;
6738 /* Similarly for an entry inserted for a backwards ref. */
6739 HOST_WIDE_INT min_address;
6740 /* The number of fixes referencing this entry. This can become zero
6741 if we "unpush" an entry. In this case we ignore the entry when we
6742 come to emit the code. */
6743 int refcount;
6744 /* The offset from the start of the minipool. */
6745 HOST_WIDE_INT offset;
6746 /* The value in table. */
6747 rtx value;
6748 /* The mode of value. */
6749 enum machine_mode mode;
6750 /* The size of the value. With iWMMXt enabled
6751 sizes > 4 also imply an alignment of 8-bytes. */
6752 int fix_size;
6755 struct minipool_fixup
6757 Mfix * next;
6758 rtx insn;
6759 HOST_WIDE_INT address;
6760 rtx * loc;
6761 enum machine_mode mode;
6762 int fix_size;
6763 rtx value;
6764 Mnode * minipool;
6765 HOST_WIDE_INT forwards;
6766 HOST_WIDE_INT backwards;
6769 /* Fixes less than a word need padding out to a word boundary. */
6770 #define MINIPOOL_FIX_SIZE(mode) \
6771 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6773 static Mnode * minipool_vector_head;
6774 static Mnode * minipool_vector_tail;
6775 static rtx minipool_vector_label;
6777 /* The linked list of all minipool fixes required for this function. */
6778 Mfix * minipool_fix_head;
6779 Mfix * minipool_fix_tail;
6780 /* The fix entry for the current minipool, once it has been placed. */
6781 Mfix * minipool_barrier;
6783 /* Determines if INSN is the start of a jump table. Returns the end
6784 of the TABLE or NULL_RTX. */
6785 static rtx
6786 is_jump_table (rtx insn)
6788 rtx table;
6790 if (GET_CODE (insn) == JUMP_INSN
6791 && JUMP_LABEL (insn) != NULL
6792 && ((table = next_real_insn (JUMP_LABEL (insn)))
6793 == next_real_insn (insn))
6794 && table != NULL
6795 && GET_CODE (table) == JUMP_INSN
6796 && (GET_CODE (PATTERN (table)) == ADDR_VEC
6797 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6798 return table;
6800 return NULL_RTX;
6803 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6804 #define JUMP_TABLES_IN_TEXT_SECTION 0
6805 #endif
6807 static HOST_WIDE_INT
6808 get_jump_table_size (rtx insn)
6810 /* ADDR_VECs only take room if read-only data does into the text
6811 section. */
6812 if (JUMP_TABLES_IN_TEXT_SECTION
6813 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6814 || 1
6815 #endif
6818 rtx body = PATTERN (insn);
6819 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6821 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6824 return 0;
6827 /* Move a minipool fix MP from its current location to before MAX_MP.
6828 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6829 constraints may need updating. */
6830 static Mnode *
6831 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6832 HOST_WIDE_INT max_address)
6834 /* The code below assumes these are different. */
6835 gcc_assert (mp != max_mp);
6837 if (max_mp == NULL)
6839 if (max_address < mp->max_address)
6840 mp->max_address = max_address;
6842 else
6844 if (max_address > max_mp->max_address - mp->fix_size)
6845 mp->max_address = max_mp->max_address - mp->fix_size;
6846 else
6847 mp->max_address = max_address;
6849 /* Unlink MP from its current position. Since max_mp is non-null,
6850 mp->prev must be non-null. */
6851 mp->prev->next = mp->next;
6852 if (mp->next != NULL)
6853 mp->next->prev = mp->prev;
6854 else
6855 minipool_vector_tail = mp->prev;
6857 /* Re-insert it before MAX_MP. */
6858 mp->next = max_mp;
6859 mp->prev = max_mp->prev;
6860 max_mp->prev = mp;
6862 if (mp->prev != NULL)
6863 mp->prev->next = mp;
6864 else
6865 minipool_vector_head = mp;
6868 /* Save the new entry. */
6869 max_mp = mp;
6871 /* Scan over the preceding entries and adjust their addresses as
6872 required. */
6873 while (mp->prev != NULL
6874 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6876 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6877 mp = mp->prev;
6880 return max_mp;
6883 /* Add a constant to the minipool for a forward reference. Returns the
6884 node added or NULL if the constant will not fit in this pool. */
6885 static Mnode *
6886 add_minipool_forward_ref (Mfix *fix)
6888 /* If set, max_mp is the first pool_entry that has a lower
6889 constraint than the one we are trying to add. */
6890 Mnode * max_mp = NULL;
6891 HOST_WIDE_INT max_address = fix->address + fix->forwards;
6892 Mnode * mp;
6894 /* If this fix's address is greater than the address of the first
6895 entry, then we can't put the fix in this pool. We subtract the
6896 size of the current fix to ensure that if the table is fully
6897 packed we still have enough room to insert this value by suffling
6898 the other fixes forwards. */
6899 if (minipool_vector_head &&
6900 fix->address >= minipool_vector_head->max_address - fix->fix_size)
6901 return NULL;
6903 /* Scan the pool to see if a constant with the same value has
6904 already been added. While we are doing this, also note the
6905 location where we must insert the constant if it doesn't already
6906 exist. */
6907 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6909 if (GET_CODE (fix->value) == GET_CODE (mp->value)
6910 && fix->mode == mp->mode
6911 && (GET_CODE (fix->value) != CODE_LABEL
6912 || (CODE_LABEL_NUMBER (fix->value)
6913 == CODE_LABEL_NUMBER (mp->value)))
6914 && rtx_equal_p (fix->value, mp->value))
6916 /* More than one fix references this entry. */
6917 mp->refcount++;
6918 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6921 /* Note the insertion point if necessary. */
6922 if (max_mp == NULL
6923 && mp->max_address > max_address)
6924 max_mp = mp;
6926 /* If we are inserting an 8-bytes aligned quantity and
6927 we have not already found an insertion point, then
6928 make sure that all such 8-byte aligned quantities are
6929 placed at the start of the pool. */
6930 if (ARM_DOUBLEWORD_ALIGN
6931 && max_mp == NULL
6932 && fix->fix_size == 8
6933 && mp->fix_size != 8)
6935 max_mp = mp;
6936 max_address = mp->max_address;
6940 /* The value is not currently in the minipool, so we need to create
6941 a new entry for it. If MAX_MP is NULL, the entry will be put on
6942 the end of the list since the placement is less constrained than
6943 any existing entry. Otherwise, we insert the new fix before
6944 MAX_MP and, if necessary, adjust the constraints on the other
6945 entries. */
6946 mp = xmalloc (sizeof (* mp));
6947 mp->fix_size = fix->fix_size;
6948 mp->mode = fix->mode;
6949 mp->value = fix->value;
6950 mp->refcount = 1;
6951 /* Not yet required for a backwards ref. */
6952 mp->min_address = -65536;
6954 if (max_mp == NULL)
6956 mp->max_address = max_address;
6957 mp->next = NULL;
6958 mp->prev = minipool_vector_tail;
6960 if (mp->prev == NULL)
6962 minipool_vector_head = mp;
6963 minipool_vector_label = gen_label_rtx ();
6965 else
6966 mp->prev->next = mp;
6968 minipool_vector_tail = mp;
6970 else
6972 if (max_address > max_mp->max_address - mp->fix_size)
6973 mp->max_address = max_mp->max_address - mp->fix_size;
6974 else
6975 mp->max_address = max_address;
6977 mp->next = max_mp;
6978 mp->prev = max_mp->prev;
6979 max_mp->prev = mp;
6980 if (mp->prev != NULL)
6981 mp->prev->next = mp;
6982 else
6983 minipool_vector_head = mp;
6986 /* Save the new entry. */
6987 max_mp = mp;
6989 /* Scan over the preceding entries and adjust their addresses as
6990 required. */
6991 while (mp->prev != NULL
6992 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6994 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6995 mp = mp->prev;
6998 return max_mp;
7001 static Mnode *
7002 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7003 HOST_WIDE_INT min_address)
7005 HOST_WIDE_INT offset;
7007 /* The code below assumes these are different. */
7008 gcc_assert (mp != min_mp);
7010 if (min_mp == NULL)
7012 if (min_address > mp->min_address)
7013 mp->min_address = min_address;
7015 else
7017 /* We will adjust this below if it is too loose. */
7018 mp->min_address = min_address;
7020 /* Unlink MP from its current position. Since min_mp is non-null,
7021 mp->next must be non-null. */
7022 mp->next->prev = mp->prev;
7023 if (mp->prev != NULL)
7024 mp->prev->next = mp->next;
7025 else
7026 minipool_vector_head = mp->next;
7028 /* Reinsert it after MIN_MP. */
7029 mp->prev = min_mp;
7030 mp->next = min_mp->next;
7031 min_mp->next = mp;
7032 if (mp->next != NULL)
7033 mp->next->prev = mp;
7034 else
7035 minipool_vector_tail = mp;
7038 min_mp = mp;
7040 offset = 0;
7041 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7043 mp->offset = offset;
7044 if (mp->refcount > 0)
7045 offset += mp->fix_size;
7047 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7048 mp->next->min_address = mp->min_address + mp->fix_size;
7051 return min_mp;
7054 /* Add a constant to the minipool for a backward reference. Returns the
7055 node added or NULL if the constant will not fit in this pool.
7057 Note that the code for insertion for a backwards reference can be
7058 somewhat confusing because the calculated offsets for each fix do
7059 not take into account the size of the pool (which is still under
7060 construction. */
7061 static Mnode *
7062 add_minipool_backward_ref (Mfix *fix)
7064 /* If set, min_mp is the last pool_entry that has a lower constraint
7065 than the one we are trying to add. */
7066 Mnode *min_mp = NULL;
7067 /* This can be negative, since it is only a constraint. */
7068 HOST_WIDE_INT min_address = fix->address - fix->backwards;
7069 Mnode *mp;
7071 /* If we can't reach the current pool from this insn, or if we can't
7072 insert this entry at the end of the pool without pushing other
7073 fixes out of range, then we don't try. This ensures that we
7074 can't fail later on. */
7075 if (min_address >= minipool_barrier->address
7076 || (minipool_vector_tail->min_address + fix->fix_size
7077 >= minipool_barrier->address))
7078 return NULL;
7080 /* Scan the pool to see if a constant with the same value has
7081 already been added. While we are doing this, also note the
7082 location where we must insert the constant if it doesn't already
7083 exist. */
7084 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7086 if (GET_CODE (fix->value) == GET_CODE (mp->value)
7087 && fix->mode == mp->mode
7088 && (GET_CODE (fix->value) != CODE_LABEL
7089 || (CODE_LABEL_NUMBER (fix->value)
7090 == CODE_LABEL_NUMBER (mp->value)))
7091 && rtx_equal_p (fix->value, mp->value)
7092 /* Check that there is enough slack to move this entry to the
7093 end of the table (this is conservative). */
7094 && (mp->max_address
7095 > (minipool_barrier->address
7096 + minipool_vector_tail->offset
7097 + minipool_vector_tail->fix_size)))
7099 mp->refcount++;
7100 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7103 if (min_mp != NULL)
7104 mp->min_address += fix->fix_size;
7105 else
7107 /* Note the insertion point if necessary. */
7108 if (mp->min_address < min_address)
7110 /* For now, we do not allow the insertion of 8-byte alignment
7111 requiring nodes anywhere but at the start of the pool. */
7112 if (ARM_DOUBLEWORD_ALIGN
7113 && fix->fix_size == 8 && mp->fix_size != 8)
7114 return NULL;
7115 else
7116 min_mp = mp;
7118 else if (mp->max_address
7119 < minipool_barrier->address + mp->offset + fix->fix_size)
7121 /* Inserting before this entry would push the fix beyond
7122 its maximum address (which can happen if we have
7123 re-located a forwards fix); force the new fix to come
7124 after it. */
7125 min_mp = mp;
7126 min_address = mp->min_address + fix->fix_size;
7128 /* If we are inserting an 8-bytes aligned quantity and
7129 we have not already found an insertion point, then
7130 make sure that all such 8-byte aligned quantities are
7131 placed at the start of the pool. */
7132 else if (ARM_DOUBLEWORD_ALIGN
7133 && min_mp == NULL
7134 && fix->fix_size == 8
7135 && mp->fix_size < 8)
7137 min_mp = mp;
7138 min_address = mp->min_address + fix->fix_size;
7143 /* We need to create a new entry. */
7144 mp = xmalloc (sizeof (* mp));
7145 mp->fix_size = fix->fix_size;
7146 mp->mode = fix->mode;
7147 mp->value = fix->value;
7148 mp->refcount = 1;
7149 mp->max_address = minipool_barrier->address + 65536;
7151 mp->min_address = min_address;
7153 if (min_mp == NULL)
7155 mp->prev = NULL;
7156 mp->next = minipool_vector_head;
7158 if (mp->next == NULL)
7160 minipool_vector_tail = mp;
7161 minipool_vector_label = gen_label_rtx ();
7163 else
7164 mp->next->prev = mp;
7166 minipool_vector_head = mp;
7168 else
7170 mp->next = min_mp->next;
7171 mp->prev = min_mp;
7172 min_mp->next = mp;
7174 if (mp->next != NULL)
7175 mp->next->prev = mp;
7176 else
7177 minipool_vector_tail = mp;
7180 /* Save the new entry. */
7181 min_mp = mp;
7183 if (mp->prev)
7184 mp = mp->prev;
7185 else
7186 mp->offset = 0;
7188 /* Scan over the following entries and adjust their offsets. */
7189 while (mp->next != NULL)
7191 if (mp->next->min_address < mp->min_address + mp->fix_size)
7192 mp->next->min_address = mp->min_address + mp->fix_size;
7194 if (mp->refcount)
7195 mp->next->offset = mp->offset + mp->fix_size;
7196 else
7197 mp->next->offset = mp->offset;
7199 mp = mp->next;
7202 return min_mp;
7205 static void
7206 assign_minipool_offsets (Mfix *barrier)
7208 HOST_WIDE_INT offset = 0;
7209 Mnode *mp;
7211 minipool_barrier = barrier;
7213 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7215 mp->offset = offset;
7217 if (mp->refcount > 0)
7218 offset += mp->fix_size;
7222 /* Output the literal table */
7223 static void
7224 dump_minipool (rtx scan)
7226 Mnode * mp;
7227 Mnode * nmp;
7228 int align64 = 0;
7230 if (ARM_DOUBLEWORD_ALIGN)
7231 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7232 if (mp->refcount > 0 && mp->fix_size == 8)
7234 align64 = 1;
7235 break;
7238 if (dump_file)
7239 fprintf (dump_file,
7240 ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7241 INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7243 scan = emit_label_after (gen_label_rtx (), scan);
7244 scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7245 scan = emit_label_after (minipool_vector_label, scan);
7247 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7249 if (mp->refcount > 0)
7251 if (dump_file)
7253 fprintf (dump_file,
7254 ";; Offset %u, min %ld, max %ld ",
7255 (unsigned) mp->offset, (unsigned long) mp->min_address,
7256 (unsigned long) mp->max_address);
7257 arm_print_value (dump_file, mp->value);
7258 fputc ('\n', dump_file);
7261 switch (mp->fix_size)
7263 #ifdef HAVE_consttable_1
7264 case 1:
7265 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7266 break;
7268 #endif
7269 #ifdef HAVE_consttable_2
7270 case 2:
7271 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7272 break;
7274 #endif
7275 #ifdef HAVE_consttable_4
7276 case 4:
7277 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7278 break;
7280 #endif
7281 #ifdef HAVE_consttable_8
7282 case 8:
7283 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7284 break;
7286 #endif
7287 default:
7288 gcc_unreachable ();
7292 nmp = mp->next;
7293 free (mp);
7296 minipool_vector_head = minipool_vector_tail = NULL;
7297 scan = emit_insn_after (gen_consttable_end (), scan);
7298 scan = emit_barrier_after (scan);
7301 /* Return the cost of forcibly inserting a barrier after INSN. */
7302 static int
7303 arm_barrier_cost (rtx insn)
7305 /* Basing the location of the pool on the loop depth is preferable,
7306 but at the moment, the basic block information seems to be
7307 corrupt by this stage of the compilation. */
7308 int base_cost = 50;
7309 rtx next = next_nonnote_insn (insn);
7311 if (next != NULL && GET_CODE (next) == CODE_LABEL)
7312 base_cost -= 20;
7314 switch (GET_CODE (insn))
7316 case CODE_LABEL:
7317 /* It will always be better to place the table before the label, rather
7318 than after it. */
7319 return 50;
7321 case INSN:
7322 case CALL_INSN:
7323 return base_cost;
7325 case JUMP_INSN:
7326 return base_cost - 10;
7328 default:
7329 return base_cost + 10;
7333 /* Find the best place in the insn stream in the range
7334 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7335 Create the barrier by inserting a jump and add a new fix entry for
7336 it. */
7337 static Mfix *
7338 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7340 HOST_WIDE_INT count = 0;
7341 rtx barrier;
7342 rtx from = fix->insn;
7343 rtx selected = from;
7344 int selected_cost;
7345 HOST_WIDE_INT selected_address;
7346 Mfix * new_fix;
7347 HOST_WIDE_INT max_count = max_address - fix->address;
7348 rtx label = gen_label_rtx ();
7350 selected_cost = arm_barrier_cost (from);
7351 selected_address = fix->address;
7353 while (from && count < max_count)
7355 rtx tmp;
7356 int new_cost;
7358 /* This code shouldn't have been called if there was a natural barrier
7359 within range. */
7360 gcc_assert (GET_CODE (from) != BARRIER);
7362 /* Count the length of this insn. */
7363 count += get_attr_length (from);
7365 /* If there is a jump table, add its length. */
7366 tmp = is_jump_table (from);
7367 if (tmp != NULL)
7369 count += get_jump_table_size (tmp);
7371 /* Jump tables aren't in a basic block, so base the cost on
7372 the dispatch insn. If we select this location, we will
7373 still put the pool after the table. */
7374 new_cost = arm_barrier_cost (from);
7376 if (count < max_count && new_cost <= selected_cost)
7378 selected = tmp;
7379 selected_cost = new_cost;
7380 selected_address = fix->address + count;
7383 /* Continue after the dispatch table. */
7384 from = NEXT_INSN (tmp);
7385 continue;
7388 new_cost = arm_barrier_cost (from);
7390 if (count < max_count && new_cost <= selected_cost)
7392 selected = from;
7393 selected_cost = new_cost;
7394 selected_address = fix->address + count;
7397 from = NEXT_INSN (from);
7400 /* Create a new JUMP_INSN that branches around a barrier. */
7401 from = emit_jump_insn_after (gen_jump (label), selected);
7402 JUMP_LABEL (from) = label;
7403 barrier = emit_barrier_after (from);
7404 emit_label_after (label, barrier);
7406 /* Create a minipool barrier entry for the new barrier. */
7407 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7408 new_fix->insn = barrier;
7409 new_fix->address = selected_address;
7410 new_fix->next = fix->next;
7411 fix->next = new_fix;
7413 return new_fix;
7416 /* Record that there is a natural barrier in the insn stream at
7417 ADDRESS. */
7418 static void
7419 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7421 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7423 fix->insn = insn;
7424 fix->address = address;
7426 fix->next = NULL;
7427 if (minipool_fix_head != NULL)
7428 minipool_fix_tail->next = fix;
7429 else
7430 minipool_fix_head = fix;
7432 minipool_fix_tail = fix;
7435 /* Record INSN, which will need fixing up to load a value from the
7436 minipool. ADDRESS is the offset of the insn since the start of the
7437 function; LOC is a pointer to the part of the insn which requires
7438 fixing; VALUE is the constant that must be loaded, which is of type
7439 MODE. */
7440 static void
7441 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7442 enum machine_mode mode, rtx value)
7444 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7446 #ifdef AOF_ASSEMBLER
7447 /* PIC symbol references need to be converted into offsets into the
7448 based area. */
7449 /* XXX This shouldn't be done here. */
7450 if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7451 value = aof_pic_entry (value);
7452 #endif /* AOF_ASSEMBLER */
7454 fix->insn = insn;
7455 fix->address = address;
7456 fix->loc = loc;
7457 fix->mode = mode;
7458 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7459 fix->value = value;
7460 fix->forwards = get_attr_pool_range (insn);
7461 fix->backwards = get_attr_neg_pool_range (insn);
7462 fix->minipool = NULL;
7464 /* If an insn doesn't have a range defined for it, then it isn't
7465 expecting to be reworked by this code. Better to stop now than
7466 to generate duff assembly code. */
7467 gcc_assert (fix->forwards || fix->backwards);
7469 /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7470 So there might be an empty word before the start of the pool.
7471 Hence we reduce the forward range by 4 to allow for this
7472 possibility. */
7473 if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7474 fix->forwards -= 4;
7476 if (dump_file)
7478 fprintf (dump_file,
7479 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7480 GET_MODE_NAME (mode),
7481 INSN_UID (insn), (unsigned long) address,
7482 -1 * (long)fix->backwards, (long)fix->forwards);
7483 arm_print_value (dump_file, fix->value);
7484 fprintf (dump_file, "\n");
7487 /* Add it to the chain of fixes. */
7488 fix->next = NULL;
7490 if (minipool_fix_head != NULL)
7491 minipool_fix_tail->next = fix;
7492 else
7493 minipool_fix_head = fix;
7495 minipool_fix_tail = fix;
7498 /* Return the cost of synthesizing a 64-bit constant VAL inline.
7499 Returns the number of insns needed, or 99 if we don't know how to
7500 do it. */
7502 arm_const_double_inline_cost (rtx val)
7504 rtx lowpart, highpart;
7505 enum machine_mode mode;
7507 mode = GET_MODE (val);
7509 if (mode == VOIDmode)
7510 mode = DImode;
7512 gcc_assert (GET_MODE_SIZE (mode) == 8);
7514 lowpart = gen_lowpart (SImode, val);
7515 highpart = gen_highpart_mode (SImode, mode, val);
7517 gcc_assert (GET_CODE (lowpart) == CONST_INT);
7518 gcc_assert (GET_CODE (highpart) == CONST_INT);
7520 return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
7521 NULL_RTX, NULL_RTX, 0, 0)
7522 + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
7523 NULL_RTX, NULL_RTX, 0, 0));
7526 /* Return true if it is worthwhile to split a 64-bit constant into two
7527 32-bit operations. This is the case if optimizing for size, or
7528 if we have load delay slots, or if one 32-bit part can be done with
7529 a single data operation. */
7530 bool
7531 arm_const_double_by_parts (rtx val)
7533 enum machine_mode mode = GET_MODE (val);
7534 rtx part;
7536 if (optimize_size || arm_ld_sched)
7537 return true;
7539 if (mode == VOIDmode)
7540 mode = DImode;
7542 part = gen_highpart_mode (SImode, mode, val);
7544 gcc_assert (GET_CODE (part) == CONST_INT);
7546 if (const_ok_for_arm (INTVAL (part))
7547 || const_ok_for_arm (~INTVAL (part)))
7548 return true;
7550 part = gen_lowpart (SImode, val);
7552 gcc_assert (GET_CODE (part) == CONST_INT);
7554 if (const_ok_for_arm (INTVAL (part))
7555 || const_ok_for_arm (~INTVAL (part)))
7556 return true;
7558 return false;
7561 /* Scan INSN and note any of its operands that need fixing.
7562 If DO_PUSHES is false we do not actually push any of the fixups
7563 needed. The function returns TRUE if any fixups were needed/pushed.
7564 This is used by arm_memory_load_p() which needs to know about loads
7565 of constants that will be converted into minipool loads. */
7566 static bool
7567 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7569 bool result = false;
7570 int opno;
7572 extract_insn (insn);
7574 if (!constrain_operands (1))
7575 fatal_insn_not_found (insn);
7577 if (recog_data.n_alternatives == 0)
7578 return false;
7580 /* Fill in recog_op_alt with information about the constraints of
7581 this insn. */
7582 preprocess_constraints ();
7584 for (opno = 0; opno < recog_data.n_operands; opno++)
7586 /* Things we need to fix can only occur in inputs. */
7587 if (recog_data.operand_type[opno] != OP_IN)
7588 continue;
7590 /* If this alternative is a memory reference, then any mention
7591 of constants in this alternative is really to fool reload
7592 into allowing us to accept one there. We need to fix them up
7593 now so that we output the right code. */
7594 if (recog_op_alt[opno][which_alternative].memory_ok)
7596 rtx op = recog_data.operand[opno];
7598 if (CONSTANT_P (op))
7600 if (do_pushes)
7601 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7602 recog_data.operand_mode[opno], op);
7603 result = true;
7605 else if (GET_CODE (op) == MEM
7606 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7607 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7609 if (do_pushes)
7611 rtx cop = avoid_constant_pool_reference (op);
7613 /* Casting the address of something to a mode narrower
7614 than a word can cause avoid_constant_pool_reference()
7615 to return the pool reference itself. That's no good to
7616 us here. Lets just hope that we can use the
7617 constant pool value directly. */
7618 if (op == cop)
7619 cop = get_pool_constant (XEXP (op, 0));
7621 push_minipool_fix (insn, address,
7622 recog_data.operand_loc[opno],
7623 recog_data.operand_mode[opno], cop);
7626 result = true;
7631 return result;
7634 /* Gcc puts the pool in the wrong place for ARM, since we can only
7635 load addresses a limited distance around the pc. We do some
7636 special munging to move the constant pool values to the correct
7637 point in the code. */
7638 static void
7639 arm_reorg (void)
7641 rtx insn;
7642 HOST_WIDE_INT address = 0;
7643 Mfix * fix;
7645 minipool_fix_head = minipool_fix_tail = NULL;
7647 /* The first insn must always be a note, or the code below won't
7648 scan it properly. */
7649 insn = get_insns ();
7650 gcc_assert (GET_CODE (insn) == NOTE);
7652 /* Scan all the insns and record the operands that will need fixing. */
7653 for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7655 if (TARGET_CIRRUS_FIX_INVALID_INSNS
7656 && (arm_cirrus_insn_p (insn)
7657 || GET_CODE (insn) == JUMP_INSN
7658 || arm_memory_load_p (insn)))
7659 cirrus_reorg (insn);
7661 if (GET_CODE (insn) == BARRIER)
7662 push_minipool_barrier (insn, address);
7663 else if (INSN_P (insn))
7665 rtx table;
7667 note_invalid_constants (insn, address, true);
7668 address += get_attr_length (insn);
7670 /* If the insn is a vector jump, add the size of the table
7671 and skip the table. */
7672 if ((table = is_jump_table (insn)) != NULL)
7674 address += get_jump_table_size (table);
7675 insn = table;
7680 fix = minipool_fix_head;
7682 /* Now scan the fixups and perform the required changes. */
7683 while (fix)
7685 Mfix * ftmp;
7686 Mfix * fdel;
7687 Mfix * last_added_fix;
7688 Mfix * last_barrier = NULL;
7689 Mfix * this_fix;
7691 /* Skip any further barriers before the next fix. */
7692 while (fix && GET_CODE (fix->insn) == BARRIER)
7693 fix = fix->next;
7695 /* No more fixes. */
7696 if (fix == NULL)
7697 break;
7699 last_added_fix = NULL;
7701 for (ftmp = fix; ftmp; ftmp = ftmp->next)
7703 if (GET_CODE (ftmp->insn) == BARRIER)
7705 if (ftmp->address >= minipool_vector_head->max_address)
7706 break;
7708 last_barrier = ftmp;
7710 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7711 break;
7713 last_added_fix = ftmp; /* Keep track of the last fix added. */
7716 /* If we found a barrier, drop back to that; any fixes that we
7717 could have reached but come after the barrier will now go in
7718 the next mini-pool. */
7719 if (last_barrier != NULL)
7721 /* Reduce the refcount for those fixes that won't go into this
7722 pool after all. */
7723 for (fdel = last_barrier->next;
7724 fdel && fdel != ftmp;
7725 fdel = fdel->next)
7727 fdel->minipool->refcount--;
7728 fdel->minipool = NULL;
7731 ftmp = last_barrier;
7733 else
7735 /* ftmp is first fix that we can't fit into this pool and
7736 there no natural barriers that we could use. Insert a
7737 new barrier in the code somewhere between the previous
7738 fix and this one, and arrange to jump around it. */
7739 HOST_WIDE_INT max_address;
7741 /* The last item on the list of fixes must be a barrier, so
7742 we can never run off the end of the list of fixes without
7743 last_barrier being set. */
7744 gcc_assert (ftmp);
7746 max_address = minipool_vector_head->max_address;
7747 /* Check that there isn't another fix that is in range that
7748 we couldn't fit into this pool because the pool was
7749 already too large: we need to put the pool before such an
7750 instruction. */
7751 if (ftmp->address < max_address)
7752 max_address = ftmp->address;
7754 last_barrier = create_fix_barrier (last_added_fix, max_address);
7757 assign_minipool_offsets (last_barrier);
7759 while (ftmp)
7761 if (GET_CODE (ftmp->insn) != BARRIER
7762 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7763 == NULL))
7764 break;
7766 ftmp = ftmp->next;
7769 /* Scan over the fixes we have identified for this pool, fixing them
7770 up and adding the constants to the pool itself. */
7771 for (this_fix = fix; this_fix && ftmp != this_fix;
7772 this_fix = this_fix->next)
7773 if (GET_CODE (this_fix->insn) != BARRIER)
7775 rtx addr
7776 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
7777 minipool_vector_label),
7778 this_fix->minipool->offset);
7779 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7782 dump_minipool (last_barrier->insn);
7783 fix = ftmp;
7786 /* From now on we must synthesize any constants that we can't handle
7787 directly. This can happen if the RTL gets split during final
7788 instruction generation. */
7789 after_arm_reorg = 1;
7791 /* Free the minipool memory. */
7792 obstack_free (&minipool_obstack, minipool_startobj);
7795 /* Routines to output assembly language. */
7797 /* If the rtx is the correct value then return the string of the number.
7798 In this way we can ensure that valid double constants are generated even
7799 when cross compiling. */
7800 const char *
7801 fp_immediate_constant (rtx x)
7803 REAL_VALUE_TYPE r;
7804 int i;
7806 if (!fp_consts_inited)
7807 init_fp_table ();
7809 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7810 for (i = 0; i < 8; i++)
7811 if (REAL_VALUES_EQUAL (r, values_fp[i]))
7812 return strings_fp[i];
7814 gcc_unreachable ();
7817 /* As for fp_immediate_constant, but value is passed directly, not in rtx. */
7818 static const char *
7819 fp_const_from_val (REAL_VALUE_TYPE *r)
7821 int i;
7823 if (!fp_consts_inited)
7824 init_fp_table ();
7826 for (i = 0; i < 8; i++)
7827 if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7828 return strings_fp[i];
7830 gcc_unreachable ();
7833 /* Output the operands of a LDM/STM instruction to STREAM.
7834 MASK is the ARM register set mask of which only bits 0-15 are important.
7835 REG is the base register, either the frame pointer or the stack pointer,
7836 INSTR is the possibly suffixed load or store instruction. */
7838 static void
7839 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
7840 unsigned long mask)
7842 unsigned i;
7843 bool not_first = FALSE;
7845 fputc ('\t', stream);
7846 asm_fprintf (stream, instr, reg);
7847 fputs (", {", stream);
7849 for (i = 0; i <= LAST_ARM_REGNUM; i++)
7850 if (mask & (1 << i))
7852 if (not_first)
7853 fprintf (stream, ", ");
7855 asm_fprintf (stream, "%r", i);
7856 not_first = TRUE;
7859 fprintf (stream, "}\n");
7863 /* Output a FLDMX instruction to STREAM.
7864 BASE if the register containing the address.
7865 REG and COUNT specify the register range.
7866 Extra registers may be added to avoid hardware bugs. */
7868 static void
7869 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7871 int i;
7873 /* Workaround ARM10 VFPr1 bug. */
7874 if (count == 2 && !arm_arch6)
7876 if (reg == 15)
7877 reg--;
7878 count++;
7881 fputc ('\t', stream);
7882 asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7884 for (i = reg; i < reg + count; i++)
7886 if (i > reg)
7887 fputs (", ", stream);
7888 asm_fprintf (stream, "d%d", i);
7890 fputs ("}\n", stream);
7895 /* Output the assembly for a store multiple. */
7897 const char *
7898 vfp_output_fstmx (rtx * operands)
7900 char pattern[100];
7901 int p;
7902 int base;
7903 int i;
7905 strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7906 p = strlen (pattern);
7908 gcc_assert (GET_CODE (operands[1]) == REG);
7910 base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7911 for (i = 1; i < XVECLEN (operands[2], 0); i++)
7913 p += sprintf (&pattern[p], ", d%d", base + i);
7915 strcpy (&pattern[p], "}");
7917 output_asm_insn (pattern, operands);
7918 return "";
7922 /* Emit RTL to save block of VFP register pairs to the stack. Returns the
7923 number of bytes pushed. */
7925 static int
7926 vfp_emit_fstmx (int base_reg, int count)
7928 rtx par;
7929 rtx dwarf;
7930 rtx tmp, reg;
7931 int i;
7933 /* Workaround ARM10 VFPr1 bug. Data corruption can occur when exactly two
7934 register pairs are stored by a store multiple insn. We avoid this
7935 by pushing an extra pair. */
7936 if (count == 2 && !arm_arch6)
7938 if (base_reg == LAST_VFP_REGNUM - 3)
7939 base_reg -= 2;
7940 count++;
7943 /* ??? The frame layout is implementation defined. We describe
7944 standard format 1 (equivalent to a FSTMD insn and unused pad word).
7945 We really need some way of representing the whole block so that the
7946 unwinder can figure it out at runtime. */
7947 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7948 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
7950 reg = gen_rtx_REG (DFmode, base_reg);
7951 base_reg += 2;
7953 XVECEXP (par, 0, 0)
7954 = gen_rtx_SET (VOIDmode,
7955 gen_rtx_MEM (BLKmode,
7956 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7957 gen_rtx_UNSPEC (BLKmode,
7958 gen_rtvec (1, reg),
7959 UNSPEC_PUSH_MULT));
7961 tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7962 gen_rtx_PLUS (SImode, stack_pointer_rtx,
7963 GEN_INT (-(count * 8 + 4))));
7964 RTX_FRAME_RELATED_P (tmp) = 1;
7965 XVECEXP (dwarf, 0, 0) = tmp;
7967 tmp = gen_rtx_SET (VOIDmode,
7968 gen_rtx_MEM (DFmode, stack_pointer_rtx),
7969 reg);
7970 RTX_FRAME_RELATED_P (tmp) = 1;
7971 XVECEXP (dwarf, 0, 1) = tmp;
7973 for (i = 1; i < count; i++)
7975 reg = gen_rtx_REG (DFmode, base_reg);
7976 base_reg += 2;
7977 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
7979 tmp = gen_rtx_SET (VOIDmode,
7980 gen_rtx_MEM (DFmode,
7981 gen_rtx_PLUS (SImode,
7982 stack_pointer_rtx,
7983 GEN_INT (i * 8))),
7984 reg);
7985 RTX_FRAME_RELATED_P (tmp) = 1;
7986 XVECEXP (dwarf, 0, i + 1) = tmp;
7989 par = emit_insn (par);
7990 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7991 REG_NOTES (par));
7992 RTX_FRAME_RELATED_P (par) = 1;
7994 return count * 8 + 4;
7998 /* Output a 'call' insn. */
7999 const char *
8000 output_call (rtx *operands)
8002 gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly. */
8004 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
8005 if (REGNO (operands[0]) == LR_REGNUM)
8007 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8008 output_asm_insn ("mov%?\t%0, %|lr", operands);
8011 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8013 if (TARGET_INTERWORK || arm_arch4t)
8014 output_asm_insn ("bx%?\t%0", operands);
8015 else
8016 output_asm_insn ("mov%?\t%|pc, %0", operands);
8018 return "";
8021 /* Output a 'call' insn that is a reference in memory. */
8022 const char *
8023 output_call_mem (rtx *operands)
8025 if (TARGET_INTERWORK && !arm_arch5)
8027 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8028 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8029 output_asm_insn ("bx%?\t%|ip", operands);
8031 else if (regno_use_in (LR_REGNUM, operands[0]))
8033 /* LR is used in the memory address. We load the address in the
8034 first instruction. It's safe to use IP as the target of the
8035 load since the call will kill it anyway. */
8036 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8037 if (arm_arch5)
8038 output_asm_insn ("blx%?\t%|ip", operands);
8039 else
8041 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8042 if (arm_arch4t)
8043 output_asm_insn ("bx%?\t%|ip", operands);
8044 else
8045 output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8048 else
8050 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8051 output_asm_insn ("ldr%?\t%|pc, %0", operands);
8054 return "";
8058 /* Output a move from arm registers to an fpa registers.
8059 OPERANDS[0] is an fpa register.
8060 OPERANDS[1] is the first registers of an arm register pair. */
8061 const char *
8062 output_mov_long_double_fpa_from_arm (rtx *operands)
8064 int arm_reg0 = REGNO (operands[1]);
8065 rtx ops[3];
8067 gcc_assert (arm_reg0 != IP_REGNUM);
8069 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8070 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8071 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8073 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8074 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8076 return "";
8079 /* Output a move from an fpa register to arm registers.
8080 OPERANDS[0] is the first registers of an arm register pair.
8081 OPERANDS[1] is an fpa register. */
8082 const char *
8083 output_mov_long_double_arm_from_fpa (rtx *operands)
8085 int arm_reg0 = REGNO (operands[0]);
8086 rtx ops[3];
8088 gcc_assert (arm_reg0 != IP_REGNUM);
8090 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8091 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8092 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8094 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8095 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8096 return "";
8099 /* Output a move from arm registers to arm registers of a long double
8100 OPERANDS[0] is the destination.
8101 OPERANDS[1] is the source. */
8102 const char *
8103 output_mov_long_double_arm_from_arm (rtx *operands)
8105 /* We have to be careful here because the two might overlap. */
8106 int dest_start = REGNO (operands[0]);
8107 int src_start = REGNO (operands[1]);
8108 rtx ops[2];
8109 int i;
8111 if (dest_start < src_start)
8113 for (i = 0; i < 3; i++)
8115 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8116 ops[1] = gen_rtx_REG (SImode, src_start + i);
8117 output_asm_insn ("mov%?\t%0, %1", ops);
8120 else
8122 for (i = 2; i >= 0; i--)
8124 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8125 ops[1] = gen_rtx_REG (SImode, src_start + i);
8126 output_asm_insn ("mov%?\t%0, %1", ops);
8130 return "";
8134 /* Output a move from arm registers to an fpa registers.
8135 OPERANDS[0] is an fpa register.
8136 OPERANDS[1] is the first registers of an arm register pair. */
8137 const char *
8138 output_mov_double_fpa_from_arm (rtx *operands)
8140 int arm_reg0 = REGNO (operands[1]);
8141 rtx ops[2];
8143 gcc_assert (arm_reg0 != IP_REGNUM);
8145 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8146 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8147 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8148 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8149 return "";
8152 /* Output a move from an fpa register to arm registers.
8153 OPERANDS[0] is the first registers of an arm register pair.
8154 OPERANDS[1] is an fpa register. */
8155 const char *
8156 output_mov_double_arm_from_fpa (rtx *operands)
8158 int arm_reg0 = REGNO (operands[0]);
8159 rtx ops[2];
8161 gcc_assert (arm_reg0 != IP_REGNUM);
8163 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8164 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8165 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8166 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8167 return "";
8170 /* Output a move between double words.
8171 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8172 or MEM<-REG and all MEMs must be offsettable addresses. */
8173 const char *
8174 output_move_double (rtx *operands)
8176 enum rtx_code code0 = GET_CODE (operands[0]);
8177 enum rtx_code code1 = GET_CODE (operands[1]);
8178 rtx otherops[3];
8180 if (code0 == REG)
8182 int reg0 = REGNO (operands[0]);
8184 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8186 gcc_assert (code1 == MEM); /* Constraints should ensure this. */
8188 switch (GET_CODE (XEXP (operands[1], 0)))
8190 case REG:
8191 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8192 break;
8194 case PRE_INC:
8195 gcc_assert (TARGET_LDRD);
8196 output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8197 break;
8199 case PRE_DEC:
8200 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8201 break;
8203 case POST_INC:
8204 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8205 break;
8207 case POST_DEC:
8208 gcc_assert (TARGET_LDRD);
8209 output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8210 break;
8212 case PRE_MODIFY:
8213 case POST_MODIFY:
8214 otherops[0] = operands[0];
8215 otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8216 otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8218 if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8220 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8222 /* Registers overlap so split out the increment. */
8223 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8224 output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8226 else
8227 output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8229 else
8231 /* We only allow constant increments, so this is safe. */
8232 output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8234 break;
8236 case LABEL_REF:
8237 case CONST:
8238 output_asm_insn ("adr%?\t%0, %1", operands);
8239 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8240 break;
8242 default:
8243 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8244 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8246 otherops[0] = operands[0];
8247 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8248 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8250 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8252 if (GET_CODE (otherops[2]) == CONST_INT)
8254 switch ((int) INTVAL (otherops[2]))
8256 case -8:
8257 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8258 return "";
8259 case -4:
8260 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8261 return "";
8262 case 4:
8263 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8264 return "";
8267 if (TARGET_LDRD
8268 && (GET_CODE (otherops[2]) == REG
8269 || (GET_CODE (otherops[2]) == CONST_INT
8270 && INTVAL (otherops[2]) > -256
8271 && INTVAL (otherops[2]) < 256)))
8273 if (reg_overlap_mentioned_p (otherops[0],
8274 otherops[2]))
8276 /* Swap base and index registers over to
8277 avoid a conflict. */
8278 otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8279 otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8282 /* If both registers conflict, it will usually
8283 have been fixed by a splitter. */
8284 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8286 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8287 output_asm_insn ("ldr%?d\t%0, [%1]",
8288 otherops);
8290 else
8291 output_asm_insn ("ldr%?d\t%0, [%1, %2]", otherops);
8292 return "";
8295 if (GET_CODE (otherops[2]) == CONST_INT)
8297 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8298 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8299 else
8300 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8302 else
8303 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8305 else
8306 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8308 return "ldm%?ia\t%0, %M0";
8310 else
8312 otherops[1] = adjust_address (operands[1], SImode, 4);
8313 /* Take care of overlapping base/data reg. */
8314 if (reg_mentioned_p (operands[0], operands[1]))
8316 output_asm_insn ("ldr%?\t%0, %1", otherops);
8317 output_asm_insn ("ldr%?\t%0, %1", operands);
8319 else
8321 output_asm_insn ("ldr%?\t%0, %1", operands);
8322 output_asm_insn ("ldr%?\t%0, %1", otherops);
8327 else
8329 /* Constraints should ensure this. */
8330 gcc_assert (code0 == MEM && code1 == REG);
8331 gcc_assert (REGNO (operands[1]) != IP_REGNUM);
8333 switch (GET_CODE (XEXP (operands[0], 0)))
8335 case REG:
8336 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8337 break;
8339 case PRE_INC:
8340 gcc_assert (TARGET_LDRD);
8341 output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8342 break;
8344 case PRE_DEC:
8345 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8346 break;
8348 case POST_INC:
8349 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8350 break;
8352 case POST_DEC:
8353 gcc_assert (TARGET_LDRD);
8354 output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8355 break;
8357 case PRE_MODIFY:
8358 case POST_MODIFY:
8359 otherops[0] = operands[1];
8360 otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8361 otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8363 if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8364 output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8365 else
8366 output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8367 break;
8369 case PLUS:
8370 otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8371 if (GET_CODE (otherops[2]) == CONST_INT)
8373 switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8375 case -8:
8376 output_asm_insn ("stm%?db\t%m0, %M1", operands);
8377 return "";
8379 case -4:
8380 output_asm_insn ("stm%?da\t%m0, %M1", operands);
8381 return "";
8383 case 4:
8384 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8385 return "";
8388 if (TARGET_LDRD
8389 && (GET_CODE (otherops[2]) == REG
8390 || (GET_CODE (otherops[2]) == CONST_INT
8391 && INTVAL (otherops[2]) > -256
8392 && INTVAL (otherops[2]) < 256)))
8394 otherops[0] = operands[1];
8395 otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8396 output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8397 return "";
8399 /* Fall through */
8401 default:
8402 otherops[0] = adjust_address (operands[0], SImode, 4);
8403 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8404 output_asm_insn ("str%?\t%1, %0", operands);
8405 output_asm_insn ("str%?\t%1, %0", otherops);
8409 return "";
8412 /* Output an ADD r, s, #n where n may be too big for one instruction.
8413 If adding zero to one register, output nothing. */
8414 const char *
8415 output_add_immediate (rtx *operands)
8417 HOST_WIDE_INT n = INTVAL (operands[2]);
8419 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8421 if (n < 0)
8422 output_multi_immediate (operands,
8423 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8424 -n);
8425 else
8426 output_multi_immediate (operands,
8427 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8431 return "";
8434 /* Output a multiple immediate operation.
8435 OPERANDS is the vector of operands referred to in the output patterns.
8436 INSTR1 is the output pattern to use for the first constant.
8437 INSTR2 is the output pattern to use for subsequent constants.
8438 IMMED_OP is the index of the constant slot in OPERANDS.
8439 N is the constant value. */
8440 static const char *
8441 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8442 int immed_op, HOST_WIDE_INT n)
8444 #if HOST_BITS_PER_WIDE_INT > 32
8445 n &= 0xffffffff;
8446 #endif
8448 if (n == 0)
8450 /* Quick and easy output. */
8451 operands[immed_op] = const0_rtx;
8452 output_asm_insn (instr1, operands);
8454 else
8456 int i;
8457 const char * instr = instr1;
8459 /* Note that n is never zero here (which would give no output). */
8460 for (i = 0; i < 32; i += 2)
8462 if (n & (3 << i))
8464 operands[immed_op] = GEN_INT (n & (255 << i));
8465 output_asm_insn (instr, operands);
8466 instr = instr2;
8467 i += 6;
8472 return "";
8475 /* Return the appropriate ARM instruction for the operation code.
8476 The returned result should not be overwritten. OP is the rtx of the
8477 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8478 was shifted. */
8479 const char *
8480 arithmetic_instr (rtx op, int shift_first_arg)
8482 switch (GET_CODE (op))
8484 case PLUS:
8485 return "add";
8487 case MINUS:
8488 return shift_first_arg ? "rsb" : "sub";
8490 case IOR:
8491 return "orr";
8493 case XOR:
8494 return "eor";
8496 case AND:
8497 return "and";
8499 default:
8500 gcc_unreachable ();
8504 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8505 for the operation code. The returned result should not be overwritten.
8506 OP is the rtx code of the shift.
8507 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8508 shift. */
8509 static const char *
8510 shift_op (rtx op, HOST_WIDE_INT *amountp)
8512 const char * mnem;
8513 enum rtx_code code = GET_CODE (op);
8515 switch (GET_CODE (XEXP (op, 1)))
8517 case REG:
8518 case SUBREG:
8519 *amountp = -1;
8520 break;
8522 case CONST_INT:
8523 *amountp = INTVAL (XEXP (op, 1));
8524 break;
8526 default:
8527 gcc_unreachable ();
8530 switch (code)
8532 case ASHIFT:
8533 mnem = "asl";
8534 break;
8536 case ASHIFTRT:
8537 mnem = "asr";
8538 break;
8540 case LSHIFTRT:
8541 mnem = "lsr";
8542 break;
8544 case ROTATE:
8545 gcc_assert (*amountp != -1);
8546 *amountp = 32 - *amountp;
8548 /* Fall through. */
8550 case ROTATERT:
8551 mnem = "ror";
8552 break;
8554 case MULT:
8555 /* We never have to worry about the amount being other than a
8556 power of 2, since this case can never be reloaded from a reg. */
8557 gcc_assert (*amountp != -1);
8558 *amountp = int_log2 (*amountp);
8559 return "asl";
8561 default:
8562 gcc_unreachable ();
8565 if (*amountp != -1)
8567 /* This is not 100% correct, but follows from the desire to merge
8568 multiplication by a power of 2 with the recognizer for a
8569 shift. >=32 is not a valid shift for "asl", so we must try and
8570 output a shift that produces the correct arithmetical result.
8571 Using lsr #32 is identical except for the fact that the carry bit
8572 is not set correctly if we set the flags; but we never use the
8573 carry bit from such an operation, so we can ignore that. */
8574 if (code == ROTATERT)
8575 /* Rotate is just modulo 32. */
8576 *amountp &= 31;
8577 else if (*amountp != (*amountp & 31))
8579 if (code == ASHIFT)
8580 mnem = "lsr";
8581 *amountp = 32;
8584 /* Shifts of 0 are no-ops. */
8585 if (*amountp == 0)
8586 return NULL;
8589 return mnem;
8592 /* Obtain the shift from the POWER of two. */
8594 static HOST_WIDE_INT
8595 int_log2 (HOST_WIDE_INT power)
8597 HOST_WIDE_INT shift = 0;
8599 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8601 gcc_assert (shift <= 31);
8602 shift++;
8605 return shift;
8608 /* Output a .ascii pseudo-op, keeping track of lengths. This is
8609 because /bin/as is horribly restrictive. The judgement about
8610 whether or not each character is 'printable' (and can be output as
8611 is) or not (and must be printed with an octal escape) must be made
8612 with reference to the *host* character set -- the situation is
8613 similar to that discussed in the comments above pp_c_char in
8614 c-pretty-print.c. */
8616 #define MAX_ASCII_LEN 51
8618 void
8619 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8621 int i;
8622 int len_so_far = 0;
8624 fputs ("\t.ascii\t\"", stream);
8626 for (i = 0; i < len; i++)
8628 int c = p[i];
8630 if (len_so_far >= MAX_ASCII_LEN)
8632 fputs ("\"\n\t.ascii\t\"", stream);
8633 len_so_far = 0;
8636 if (ISPRINT (c))
8638 if (c == '\\' || c == '\"')
8640 putc ('\\', stream);
8641 len_so_far++;
8643 putc (c, stream);
8644 len_so_far++;
8646 else
8648 fprintf (stream, "\\%03o", c);
8649 len_so_far += 4;
8653 fputs ("\"\n", stream);
8656 /* Compute the register save mask for registers 0 through 12
8657 inclusive. This code is used by arm_compute_save_reg_mask. */
8659 static unsigned long
8660 arm_compute_save_reg0_reg12_mask (void)
8662 unsigned long func_type = arm_current_func_type ();
8663 unsigned long save_reg_mask = 0;
8664 unsigned int reg;
8666 if (IS_INTERRUPT (func_type))
8668 unsigned int max_reg;
8669 /* Interrupt functions must not corrupt any registers,
8670 even call clobbered ones. If this is a leaf function
8671 we can just examine the registers used by the RTL, but
8672 otherwise we have to assume that whatever function is
8673 called might clobber anything, and so we have to save
8674 all the call-clobbered registers as well. */
8675 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8676 /* FIQ handlers have registers r8 - r12 banked, so
8677 we only need to check r0 - r7, Normal ISRs only
8678 bank r14 and r15, so we must check up to r12.
8679 r13 is the stack pointer which is always preserved,
8680 so we do not need to consider it here. */
8681 max_reg = 7;
8682 else
8683 max_reg = 12;
8685 for (reg = 0; reg <= max_reg; reg++)
8686 if (regs_ever_live[reg]
8687 || (! current_function_is_leaf && call_used_regs [reg]))
8688 save_reg_mask |= (1 << reg);
8690 /* Also save the pic base register if necessary. */
8691 if (flag_pic
8692 && !TARGET_SINGLE_PIC_BASE
8693 && current_function_uses_pic_offset_table)
8694 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8696 else
8698 /* In the normal case we only need to save those registers
8699 which are call saved and which are used by this function. */
8700 for (reg = 0; reg <= 10; reg++)
8701 if (regs_ever_live[reg] && ! call_used_regs [reg])
8702 save_reg_mask |= (1 << reg);
8704 /* Handle the frame pointer as a special case. */
8705 if (! TARGET_APCS_FRAME
8706 && ! frame_pointer_needed
8707 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8708 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8709 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8711 /* If we aren't loading the PIC register,
8712 don't stack it even though it may be live. */
8713 if (flag_pic
8714 && !TARGET_SINGLE_PIC_BASE
8715 && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
8716 || current_function_uses_pic_offset_table))
8717 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8720 /* Save registers so the exception handler can modify them. */
8721 if (current_function_calls_eh_return)
8723 unsigned int i;
8725 for (i = 0; ; i++)
8727 reg = EH_RETURN_DATA_REGNO (i);
8728 if (reg == INVALID_REGNUM)
8729 break;
8730 save_reg_mask |= 1 << reg;
8734 return save_reg_mask;
8737 /* Compute a bit mask of which registers need to be
8738 saved on the stack for the current function. */
8740 static unsigned long
8741 arm_compute_save_reg_mask (void)
8743 unsigned int save_reg_mask = 0;
8744 unsigned long func_type = arm_current_func_type ();
8746 if (IS_NAKED (func_type))
8747 /* This should never really happen. */
8748 return 0;
8750 /* If we are creating a stack frame, then we must save the frame pointer,
8751 IP (which will hold the old stack pointer), LR and the PC. */
8752 if (frame_pointer_needed)
8753 save_reg_mask |=
8754 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8755 | (1 << IP_REGNUM)
8756 | (1 << LR_REGNUM)
8757 | (1 << PC_REGNUM);
8759 /* Volatile functions do not return, so there
8760 is no need to save any other registers. */
8761 if (IS_VOLATILE (func_type))
8762 return save_reg_mask;
8764 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8766 /* Decide if we need to save the link register.
8767 Interrupt routines have their own banked link register,
8768 so they never need to save it.
8769 Otherwise if we do not use the link register we do not need to save
8770 it. If we are pushing other registers onto the stack however, we
8771 can save an instruction in the epilogue by pushing the link register
8772 now and then popping it back into the PC. This incurs extra memory
8773 accesses though, so we only do it when optimizing for size, and only
8774 if we know that we will not need a fancy return sequence. */
8775 if (regs_ever_live [LR_REGNUM]
8776 || (save_reg_mask
8777 && optimize_size
8778 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
8779 && !current_function_calls_eh_return))
8780 save_reg_mask |= 1 << LR_REGNUM;
8782 if (cfun->machine->lr_save_eliminated)
8783 save_reg_mask &= ~ (1 << LR_REGNUM);
8785 if (TARGET_REALLY_IWMMXT
8786 && ((bit_count (save_reg_mask)
8787 + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8789 unsigned int reg;
8791 /* The total number of registers that are going to be pushed
8792 onto the stack is odd. We need to ensure that the stack
8793 is 64-bit aligned before we start to save iWMMXt registers,
8794 and also before we start to create locals. (A local variable
8795 might be a double or long long which we will load/store using
8796 an iWMMXt instruction). Therefore we need to push another
8797 ARM register, so that the stack will be 64-bit aligned. We
8798 try to avoid using the arg registers (r0 -r3) as they might be
8799 used to pass values in a tail call. */
8800 for (reg = 4; reg <= 12; reg++)
8801 if ((save_reg_mask & (1 << reg)) == 0)
8802 break;
8804 if (reg <= 12)
8805 save_reg_mask |= (1 << reg);
8806 else
8808 cfun->machine->sibcall_blocked = 1;
8809 save_reg_mask |= (1 << 3);
8813 return save_reg_mask;
8817 /* Compute a bit mask of which registers need to be
8818 saved on the stack for the current function. */
8819 static unsigned long
8820 thumb_compute_save_reg_mask (void)
8822 unsigned long mask;
8823 unsigned reg;
8825 mask = 0;
8826 for (reg = 0; reg < 12; reg ++)
8827 if (regs_ever_live[reg] && !call_used_regs[reg])
8828 mask |= 1 << reg;
8830 if (flag_pic && !TARGET_SINGLE_PIC_BASE)
8831 mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
8833 if (TARGET_SINGLE_PIC_BASE)
8834 mask &= ~(1 << arm_pic_register);
8836 /* See if we might need r11 for calls to _interwork_r11_call_via_rN(). */
8837 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
8838 mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
8840 /* LR will also be pushed if any lo regs are pushed. */
8841 if (mask & 0xff || thumb_force_lr_save ())
8842 mask |= (1 << LR_REGNUM);
8844 /* Make sure we have a low work register if we need one.
8845 We will need one if we are going to push a high register,
8846 but we are not currently intending to push a low register. */
8847 if ((mask & 0xff) == 0
8848 && ((mask & 0x0f00) || TARGET_BACKTRACE))
8850 /* Use thumb_find_work_register to choose which register
8851 we will use. If the register is live then we will
8852 have to push it. Use LAST_LO_REGNUM as our fallback
8853 choice for the register to select. */
8854 reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
8856 if (! call_used_regs[reg])
8857 mask |= 1 << reg;
8860 return mask;
8864 /* Return the number of bytes required to save VFP registers. */
8865 static int
8866 arm_get_vfp_saved_size (void)
8868 unsigned int regno;
8869 int count;
8870 int saved;
8872 saved = 0;
8873 /* Space for saved VFP registers. */
8874 if (TARGET_HARD_FLOAT && TARGET_VFP)
8876 count = 0;
8877 for (regno = FIRST_VFP_REGNUM;
8878 regno < LAST_VFP_REGNUM;
8879 regno += 2)
8881 if ((!regs_ever_live[regno] || call_used_regs[regno])
8882 && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8884 if (count > 0)
8886 /* Workaround ARM10 VFPr1 bug. */
8887 if (count == 2 && !arm_arch6)
8888 count++;
8889 saved += count * 8 + 4;
8891 count = 0;
8893 else
8894 count++;
8896 if (count > 0)
8898 if (count == 2 && !arm_arch6)
8899 count++;
8900 saved += count * 8 + 4;
8903 return saved;
8907 /* Generate a function exit sequence. If REALLY_RETURN is false, then do
8908 everything bar the final return instruction. */
8909 const char *
8910 output_return_instruction (rtx operand, int really_return, int reverse)
8912 char conditional[10];
8913 char instr[100];
8914 unsigned reg;
8915 unsigned long live_regs_mask;
8916 unsigned long func_type;
8917 arm_stack_offsets *offsets;
8919 func_type = arm_current_func_type ();
8921 if (IS_NAKED (func_type))
8922 return "";
8924 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
8926 /* If this function was declared non-returning, and we have
8927 found a tail call, then we have to trust that the called
8928 function won't return. */
8929 if (really_return)
8931 rtx ops[2];
8933 /* Otherwise, trap an attempted return by aborting. */
8934 ops[0] = operand;
8935 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
8936 : "abort");
8937 assemble_external_libcall (ops[1]);
8938 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
8941 return "";
8944 gcc_assert (!current_function_calls_alloca || really_return);
8946 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
8948 return_used_this_function = 1;
8950 live_regs_mask = arm_compute_save_reg_mask ();
8952 if (live_regs_mask)
8954 const char * return_reg;
8956 /* If we do not have any special requirements for function exit
8957 (e.g. interworking, or ISR) then we can load the return address
8958 directly into the PC. Otherwise we must load it into LR. */
8959 if (really_return
8960 && ! TARGET_INTERWORK)
8961 return_reg = reg_names[PC_REGNUM];
8962 else
8963 return_reg = reg_names[LR_REGNUM];
8965 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
8967 /* There are three possible reasons for the IP register
8968 being saved. 1) a stack frame was created, in which case
8969 IP contains the old stack pointer, or 2) an ISR routine
8970 corrupted it, or 3) it was saved to align the stack on
8971 iWMMXt. In case 1, restore IP into SP, otherwise just
8972 restore IP. */
8973 if (frame_pointer_needed)
8975 live_regs_mask &= ~ (1 << IP_REGNUM);
8976 live_regs_mask |= (1 << SP_REGNUM);
8978 else
8979 gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
8982 /* On some ARM architectures it is faster to use LDR rather than
8983 LDM to load a single register. On other architectures, the
8984 cost is the same. In 26 bit mode, or for exception handlers,
8985 we have to use LDM to load the PC so that the CPSR is also
8986 restored. */
8987 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
8988 if (live_regs_mask == (1U << reg))
8989 break;
8991 if (reg <= LAST_ARM_REGNUM
8992 && (reg != LR_REGNUM
8993 || ! really_return
8994 || ! IS_INTERRUPT (func_type)))
8996 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
8997 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
8999 else
9001 char *p;
9002 int first = 1;
9004 /* Generate the load multiple instruction to restore the
9005 registers. Note we can get here, even if
9006 frame_pointer_needed is true, but only if sp already
9007 points to the base of the saved core registers. */
9008 if (live_regs_mask & (1 << SP_REGNUM))
9010 unsigned HOST_WIDE_INT stack_adjust;
9012 offsets = arm_get_frame_offsets ();
9013 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9014 gcc_assert (stack_adjust == 0 || stack_adjust == 4);
9016 if (stack_adjust && arm_arch5)
9017 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9018 else
9020 /* If we can't use ldmib (SA110 bug),
9021 then try to pop r3 instead. */
9022 if (stack_adjust)
9023 live_regs_mask |= 1 << 3;
9024 sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9027 else
9028 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9030 p = instr + strlen (instr);
9032 for (reg = 0; reg <= SP_REGNUM; reg++)
9033 if (live_regs_mask & (1 << reg))
9035 int l = strlen (reg_names[reg]);
9037 if (first)
9038 first = 0;
9039 else
9041 memcpy (p, ", ", 2);
9042 p += 2;
9045 memcpy (p, "%|", 2);
9046 memcpy (p + 2, reg_names[reg], l);
9047 p += l + 2;
9050 if (live_regs_mask & (1 << LR_REGNUM))
9052 sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9053 /* If returning from an interrupt, restore the CPSR. */
9054 if (IS_INTERRUPT (func_type))
9055 strcat (p, "^");
9057 else
9058 strcpy (p, "}");
9061 output_asm_insn (instr, & operand);
9063 /* See if we need to generate an extra instruction to
9064 perform the actual function return. */
9065 if (really_return
9066 && func_type != ARM_FT_INTERWORKED
9067 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9069 /* The return has already been handled
9070 by loading the LR into the PC. */
9071 really_return = 0;
9075 if (really_return)
9077 switch ((int) ARM_FUNC_TYPE (func_type))
9079 case ARM_FT_ISR:
9080 case ARM_FT_FIQ:
9081 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9082 break;
9084 case ARM_FT_INTERWORKED:
9085 sprintf (instr, "bx%s\t%%|lr", conditional);
9086 break;
9088 case ARM_FT_EXCEPTION:
9089 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9090 break;
9092 default:
9093 /* Use bx if it's available. */
9094 if (arm_arch5 || arm_arch4t)
9095 sprintf (instr, "bx%s\t%%|lr", conditional);
9096 else
9097 sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9098 break;
9101 output_asm_insn (instr, & operand);
9104 return "";
9107 /* Write the function name into the code section, directly preceding
9108 the function prologue.
9110 Code will be output similar to this:
9112 .ascii "arm_poke_function_name", 0
9113 .align
9115 .word 0xff000000 + (t1 - t0)
9116 arm_poke_function_name
9117 mov ip, sp
9118 stmfd sp!, {fp, ip, lr, pc}
9119 sub fp, ip, #4
9121 When performing a stack backtrace, code can inspect the value
9122 of 'pc' stored at 'fp' + 0. If the trace function then looks
9123 at location pc - 12 and the top 8 bits are set, then we know
9124 that there is a function name embedded immediately preceding this
9125 location and has length ((pc[-3]) & 0xff000000).
9127 We assume that pc is declared as a pointer to an unsigned long.
9129 It is of no benefit to output the function name if we are assembling
9130 a leaf function. These function types will not contain a stack
9131 backtrace structure, therefore it is not possible to determine the
9132 function name. */
9133 void
9134 arm_poke_function_name (FILE *stream, const char *name)
9136 unsigned long alignlength;
9137 unsigned long length;
9138 rtx x;
9140 length = strlen (name) + 1;
9141 alignlength = ROUND_UP_WORD (length);
9143 ASM_OUTPUT_ASCII (stream, name, length);
9144 ASM_OUTPUT_ALIGN (stream, 2);
9145 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9146 assemble_aligned_integer (UNITS_PER_WORD, x);
9149 /* Place some comments into the assembler stream
9150 describing the current function. */
9151 static void
9152 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9154 unsigned long func_type;
9156 if (!TARGET_ARM)
9158 thumb_output_function_prologue (f, frame_size);
9159 return;
9162 /* Sanity check. */
9163 gcc_assert (!arm_ccfsm_state && !arm_target_insn);
9165 func_type = arm_current_func_type ();
9167 switch ((int) ARM_FUNC_TYPE (func_type))
9169 default:
9170 case ARM_FT_NORMAL:
9171 break;
9172 case ARM_FT_INTERWORKED:
9173 asm_fprintf (f, "\t%@ Function supports interworking.\n");
9174 break;
9175 case ARM_FT_ISR:
9176 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9177 break;
9178 case ARM_FT_FIQ:
9179 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9180 break;
9181 case ARM_FT_EXCEPTION:
9182 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9183 break;
9186 if (IS_NAKED (func_type))
9187 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9189 if (IS_VOLATILE (func_type))
9190 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9192 if (IS_NESTED (func_type))
9193 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9195 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9196 current_function_args_size,
9197 current_function_pretend_args_size, frame_size);
9199 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9200 frame_pointer_needed,
9201 cfun->machine->uses_anonymous_args);
9203 if (cfun->machine->lr_save_eliminated)
9204 asm_fprintf (f, "\t%@ link register save eliminated.\n");
9206 if (current_function_calls_eh_return)
9207 asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9209 #ifdef AOF_ASSEMBLER
9210 if (flag_pic)
9211 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9212 #endif
9214 return_used_this_function = 0;
9217 const char *
9218 arm_output_epilogue (rtx sibling)
9220 int reg;
9221 unsigned long saved_regs_mask;
9222 unsigned long func_type;
9223 /* Floats_offset is the offset from the "virtual" frame. In an APCS
9224 frame that is $fp + 4 for a non-variadic function. */
9225 int floats_offset = 0;
9226 rtx operands[3];
9227 FILE * f = asm_out_file;
9228 unsigned int lrm_count = 0;
9229 int really_return = (sibling == NULL);
9230 int start_reg;
9231 arm_stack_offsets *offsets;
9233 /* If we have already generated the return instruction
9234 then it is futile to generate anything else. */
9235 if (use_return_insn (FALSE, sibling) && return_used_this_function)
9236 return "";
9238 func_type = arm_current_func_type ();
9240 if (IS_NAKED (func_type))
9241 /* Naked functions don't have epilogues. */
9242 return "";
9244 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9246 rtx op;
9248 /* A volatile function should never return. Call abort. */
9249 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9250 assemble_external_libcall (op);
9251 output_asm_insn ("bl\t%a0", &op);
9253 return "";
9256 /* If we are throwing an exception, then we really must be doing a
9257 return, so we can't tail-call. */
9258 gcc_assert (!current_function_calls_eh_return || really_return);
9260 offsets = arm_get_frame_offsets ();
9261 saved_regs_mask = arm_compute_save_reg_mask ();
9263 if (TARGET_IWMMXT)
9264 lrm_count = bit_count (saved_regs_mask);
9266 floats_offset = offsets->saved_args;
9267 /* Compute how far away the floats will be. */
9268 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9269 if (saved_regs_mask & (1 << reg))
9270 floats_offset += 4;
9272 if (frame_pointer_needed)
9274 /* This variable is for the Virtual Frame Pointer, not VFP regs. */
9275 int vfp_offset = offsets->frame;
9277 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9279 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9280 if (regs_ever_live[reg] && !call_used_regs[reg])
9282 floats_offset += 12;
9283 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9284 reg, FP_REGNUM, floats_offset - vfp_offset);
9287 else
9289 start_reg = LAST_FPA_REGNUM;
9291 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9293 if (regs_ever_live[reg] && !call_used_regs[reg])
9295 floats_offset += 12;
9297 /* We can't unstack more than four registers at once. */
9298 if (start_reg - reg == 3)
9300 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9301 reg, FP_REGNUM, floats_offset - vfp_offset);
9302 start_reg = reg - 1;
9305 else
9307 if (reg != start_reg)
9308 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9309 reg + 1, start_reg - reg,
9310 FP_REGNUM, floats_offset - vfp_offset);
9311 start_reg = reg - 1;
9315 /* Just in case the last register checked also needs unstacking. */
9316 if (reg != start_reg)
9317 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9318 reg + 1, start_reg - reg,
9319 FP_REGNUM, floats_offset - vfp_offset);
9322 if (TARGET_HARD_FLOAT && TARGET_VFP)
9324 int saved_size;
9326 /* The fldmx insn does not have base+offset addressing modes,
9327 so we use IP to hold the address. */
9328 saved_size = arm_get_vfp_saved_size ();
9330 if (saved_size > 0)
9332 floats_offset += saved_size;
9333 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9334 FP_REGNUM, floats_offset - vfp_offset);
9336 start_reg = FIRST_VFP_REGNUM;
9337 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9339 if ((!regs_ever_live[reg] || call_used_regs[reg])
9340 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9342 if (start_reg != reg)
9343 arm_output_fldmx (f, IP_REGNUM,
9344 (start_reg - FIRST_VFP_REGNUM) / 2,
9345 (reg - start_reg) / 2);
9346 start_reg = reg + 2;
9349 if (start_reg != reg)
9350 arm_output_fldmx (f, IP_REGNUM,
9351 (start_reg - FIRST_VFP_REGNUM) / 2,
9352 (reg - start_reg) / 2);
9355 if (TARGET_IWMMXT)
9357 /* The frame pointer is guaranteed to be non-double-word aligned.
9358 This is because it is set to (old_stack_pointer - 4) and the
9359 old_stack_pointer was double word aligned. Thus the offset to
9360 the iWMMXt registers to be loaded must also be non-double-word
9361 sized, so that the resultant address *is* double-word aligned.
9362 We can ignore floats_offset since that was already included in
9363 the live_regs_mask. */
9364 lrm_count += (lrm_count % 2 ? 2 : 1);
9366 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9367 if (regs_ever_live[reg] && !call_used_regs[reg])
9369 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9370 reg, FP_REGNUM, lrm_count * 4);
9371 lrm_count += 2;
9375 /* saved_regs_mask should contain the IP, which at the time of stack
9376 frame generation actually contains the old stack pointer. So a
9377 quick way to unwind the stack is just pop the IP register directly
9378 into the stack pointer. */
9379 gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
9380 saved_regs_mask &= ~ (1 << IP_REGNUM);
9381 saved_regs_mask |= (1 << SP_REGNUM);
9383 /* There are two registers left in saved_regs_mask - LR and PC. We
9384 only need to restore the LR register (the return address), but to
9385 save time we can load it directly into the PC, unless we need a
9386 special function exit sequence, or we are not really returning. */
9387 if (really_return
9388 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9389 && !current_function_calls_eh_return)
9390 /* Delete the LR from the register mask, so that the LR on
9391 the stack is loaded into the PC in the register mask. */
9392 saved_regs_mask &= ~ (1 << LR_REGNUM);
9393 else
9394 saved_regs_mask &= ~ (1 << PC_REGNUM);
9396 /* We must use SP as the base register, because SP is one of the
9397 registers being restored. If an interrupt or page fault
9398 happens in the ldm instruction, the SP might or might not
9399 have been restored. That would be bad, as then SP will no
9400 longer indicate the safe area of stack, and we can get stack
9401 corruption. Using SP as the base register means that it will
9402 be reset correctly to the original value, should an interrupt
9403 occur. If the stack pointer already points at the right
9404 place, then omit the subtraction. */
9405 if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9406 || current_function_calls_alloca)
9407 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9408 4 * bit_count (saved_regs_mask));
9409 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9411 if (IS_INTERRUPT (func_type))
9412 /* Interrupt handlers will have pushed the
9413 IP onto the stack, so restore it now. */
9414 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9416 else
9418 /* Restore stack pointer if necessary. */
9419 if (offsets->outgoing_args != offsets->saved_regs)
9421 operands[0] = operands[1] = stack_pointer_rtx;
9422 operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9423 output_add_immediate (operands);
9426 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9428 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9429 if (regs_ever_live[reg] && !call_used_regs[reg])
9430 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9431 reg, SP_REGNUM);
9433 else
9435 start_reg = FIRST_FPA_REGNUM;
9437 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9439 if (regs_ever_live[reg] && !call_used_regs[reg])
9441 if (reg - start_reg == 3)
9443 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9444 start_reg, SP_REGNUM);
9445 start_reg = reg + 1;
9448 else
9450 if (reg != start_reg)
9451 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9452 start_reg, reg - start_reg,
9453 SP_REGNUM);
9455 start_reg = reg + 1;
9459 /* Just in case the last register checked also needs unstacking. */
9460 if (reg != start_reg)
9461 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9462 start_reg, reg - start_reg, SP_REGNUM);
9465 if (TARGET_HARD_FLOAT && TARGET_VFP)
9467 start_reg = FIRST_VFP_REGNUM;
9468 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9470 if ((!regs_ever_live[reg] || call_used_regs[reg])
9471 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9473 if (start_reg != reg)
9474 arm_output_fldmx (f, SP_REGNUM,
9475 (start_reg - FIRST_VFP_REGNUM) / 2,
9476 (reg - start_reg) / 2);
9477 start_reg = reg + 2;
9480 if (start_reg != reg)
9481 arm_output_fldmx (f, SP_REGNUM,
9482 (start_reg - FIRST_VFP_REGNUM) / 2,
9483 (reg - start_reg) / 2);
9485 if (TARGET_IWMMXT)
9486 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9487 if (regs_ever_live[reg] && !call_used_regs[reg])
9488 asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9490 /* If we can, restore the LR into the PC. */
9491 if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9492 && really_return
9493 && current_function_pretend_args_size == 0
9494 && saved_regs_mask & (1 << LR_REGNUM)
9495 && !current_function_calls_eh_return)
9497 saved_regs_mask &= ~ (1 << LR_REGNUM);
9498 saved_regs_mask |= (1 << PC_REGNUM);
9501 /* Load the registers off the stack. If we only have one register
9502 to load use the LDR instruction - it is faster. */
9503 if (saved_regs_mask == (1 << LR_REGNUM))
9505 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9507 else if (saved_regs_mask)
9509 if (saved_regs_mask & (1 << SP_REGNUM))
9510 /* Note - write back to the stack register is not enabled
9511 (i.e. "ldmfd sp!..."). We know that the stack pointer is
9512 in the list of registers and if we add writeback the
9513 instruction becomes UNPREDICTABLE. */
9514 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9515 else
9516 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9519 if (current_function_pretend_args_size)
9521 /* Unwind the pre-pushed regs. */
9522 operands[0] = operands[1] = stack_pointer_rtx;
9523 operands[2] = GEN_INT (current_function_pretend_args_size);
9524 output_add_immediate (operands);
9528 /* We may have already restored PC directly from the stack. */
9529 if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
9530 return "";
9532 /* Stack adjustment for exception handler. */
9533 if (current_function_calls_eh_return)
9534 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
9535 ARM_EH_STACKADJ_REGNUM);
9537 /* Generate the return instruction. */
9538 switch ((int) ARM_FUNC_TYPE (func_type))
9540 case ARM_FT_ISR:
9541 case ARM_FT_FIQ:
9542 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9543 break;
9545 case ARM_FT_EXCEPTION:
9546 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9547 break;
9549 case ARM_FT_INTERWORKED:
9550 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9551 break;
9553 default:
9554 if (arm_arch5 || arm_arch4t)
9555 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9556 else
9557 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9558 break;
9561 return "";
9564 static void
9565 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9566 HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9568 arm_stack_offsets *offsets;
9570 if (TARGET_THUMB)
9572 int regno;
9574 /* Emit any call-via-reg trampolines that are needed for v4t support
9575 of call_reg and call_value_reg type insns. */
9576 for (regno = 0; regno < LR_REGNUM; regno++)
9578 rtx label = cfun->machine->call_via[regno];
9580 if (label != NULL)
9582 function_section (current_function_decl);
9583 targetm.asm_out.internal_label (asm_out_file, "L",
9584 CODE_LABEL_NUMBER (label));
9585 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
9589 /* ??? Probably not safe to set this here, since it assumes that a
9590 function will be emitted as assembly immediately after we generate
9591 RTL for it. This does not happen for inline functions. */
9592 return_used_this_function = 0;
9594 else
9596 /* We need to take into account any stack-frame rounding. */
9597 offsets = arm_get_frame_offsets ();
9599 gcc_assert (!use_return_insn (FALSE, NULL)
9600 || !return_used_this_function
9601 || offsets->saved_regs == offsets->outgoing_args
9602 || frame_pointer_needed);
9604 /* Reset the ARM-specific per-function variables. */
9605 after_arm_reorg = 0;
9609 /* Generate and emit an insn that we will recognize as a push_multi.
9610 Unfortunately, since this insn does not reflect very well the actual
9611 semantics of the operation, we need to annotate the insn for the benefit
9612 of DWARF2 frame unwind information. */
9613 static rtx
9614 emit_multi_reg_push (unsigned long mask)
9616 int num_regs = 0;
9617 int num_dwarf_regs;
9618 int i, j;
9619 rtx par;
9620 rtx dwarf;
9621 int dwarf_par_index;
9622 rtx tmp, reg;
9624 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9625 if (mask & (1 << i))
9626 num_regs++;
9628 gcc_assert (num_regs && num_regs <= 16);
9630 /* We don't record the PC in the dwarf frame information. */
9631 num_dwarf_regs = num_regs;
9632 if (mask & (1 << PC_REGNUM))
9633 num_dwarf_regs--;
9635 /* For the body of the insn we are going to generate an UNSPEC in
9636 parallel with several USEs. This allows the insn to be recognized
9637 by the push_multi pattern in the arm.md file. The insn looks
9638 something like this:
9640 (parallel [
9641 (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9642 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9643 (use (reg:SI 11 fp))
9644 (use (reg:SI 12 ip))
9645 (use (reg:SI 14 lr))
9646 (use (reg:SI 15 pc))
9649 For the frame note however, we try to be more explicit and actually
9650 show each register being stored into the stack frame, plus a (single)
9651 decrement of the stack pointer. We do it this way in order to be
9652 friendly to the stack unwinding code, which only wants to see a single
9653 stack decrement per instruction. The RTL we generate for the note looks
9654 something like this:
9656 (sequence [
9657 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9658 (set (mem:SI (reg:SI sp)) (reg:SI r4))
9659 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9660 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9661 (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9664 This sequence is used both by the code to support stack unwinding for
9665 exceptions handlers and the code to generate dwarf2 frame debugging. */
9667 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9668 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9669 dwarf_par_index = 1;
9671 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9673 if (mask & (1 << i))
9675 reg = gen_rtx_REG (SImode, i);
9677 XVECEXP (par, 0, 0)
9678 = gen_rtx_SET (VOIDmode,
9679 gen_rtx_MEM (BLKmode,
9680 gen_rtx_PRE_DEC (BLKmode,
9681 stack_pointer_rtx)),
9682 gen_rtx_UNSPEC (BLKmode,
9683 gen_rtvec (1, reg),
9684 UNSPEC_PUSH_MULT));
9686 if (i != PC_REGNUM)
9688 tmp = gen_rtx_SET (VOIDmode,
9689 gen_rtx_MEM (SImode, stack_pointer_rtx),
9690 reg);
9691 RTX_FRAME_RELATED_P (tmp) = 1;
9692 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9693 dwarf_par_index++;
9696 break;
9700 for (j = 1, i++; j < num_regs; i++)
9702 if (mask & (1 << i))
9704 reg = gen_rtx_REG (SImode, i);
9706 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9708 if (i != PC_REGNUM)
9710 tmp = gen_rtx_SET (VOIDmode,
9711 gen_rtx_MEM (SImode,
9712 plus_constant (stack_pointer_rtx,
9713 4 * j)),
9714 reg);
9715 RTX_FRAME_RELATED_P (tmp) = 1;
9716 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9719 j++;
9723 par = emit_insn (par);
9725 tmp = gen_rtx_SET (SImode,
9726 stack_pointer_rtx,
9727 gen_rtx_PLUS (SImode,
9728 stack_pointer_rtx,
9729 GEN_INT (-4 * num_regs)));
9730 RTX_FRAME_RELATED_P (tmp) = 1;
9731 XVECEXP (dwarf, 0, 0) = tmp;
9733 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9734 REG_NOTES (par));
9735 return par;
9738 static rtx
9739 emit_sfm (int base_reg, int count)
9741 rtx par;
9742 rtx dwarf;
9743 rtx tmp, reg;
9744 int i;
9746 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9747 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9749 reg = gen_rtx_REG (XFmode, base_reg++);
9751 XVECEXP (par, 0, 0)
9752 = gen_rtx_SET (VOIDmode,
9753 gen_rtx_MEM (BLKmode,
9754 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9755 gen_rtx_UNSPEC (BLKmode,
9756 gen_rtvec (1, reg),
9757 UNSPEC_PUSH_MULT));
9758 tmp = gen_rtx_SET (VOIDmode,
9759 gen_rtx_MEM (XFmode, stack_pointer_rtx), reg);
9760 RTX_FRAME_RELATED_P (tmp) = 1;
9761 XVECEXP (dwarf, 0, 1) = tmp;
9763 for (i = 1; i < count; i++)
9765 reg = gen_rtx_REG (XFmode, base_reg++);
9766 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9768 tmp = gen_rtx_SET (VOIDmode,
9769 gen_rtx_MEM (XFmode,
9770 plus_constant (stack_pointer_rtx,
9771 i * 12)),
9772 reg);
9773 RTX_FRAME_RELATED_P (tmp) = 1;
9774 XVECEXP (dwarf, 0, i + 1) = tmp;
9777 tmp = gen_rtx_SET (VOIDmode,
9778 stack_pointer_rtx,
9779 gen_rtx_PLUS (SImode,
9780 stack_pointer_rtx,
9781 GEN_INT (-12 * count)));
9782 RTX_FRAME_RELATED_P (tmp) = 1;
9783 XVECEXP (dwarf, 0, 0) = tmp;
9785 par = emit_insn (par);
9786 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9787 REG_NOTES (par));
9788 return par;
9792 /* Return true if the current function needs to save/restore LR. */
9794 static bool
9795 thumb_force_lr_save (void)
9797 return !cfun->machine->lr_save_eliminated
9798 && (!leaf_function_p ()
9799 || thumb_far_jump_used_p ()
9800 || regs_ever_live [LR_REGNUM]);
9804 /* Compute the distance from register FROM to register TO.
9805 These can be the arg pointer (26), the soft frame pointer (25),
9806 the stack pointer (13) or the hard frame pointer (11).
9807 In thumb mode r7 is used as the soft frame pointer, if needed.
9808 Typical stack layout looks like this:
9810 old stack pointer -> | |
9811 ----
9812 | | \
9813 | | saved arguments for
9814 | | vararg functions
9815 | | /
9817 hard FP & arg pointer -> | | \
9818 | | stack
9819 | | frame
9820 | | /
9822 | | \
9823 | | call saved
9824 | | registers
9825 soft frame pointer -> | | /
9827 | | \
9828 | | local
9829 | | variables
9830 | | /
9832 | | \
9833 | | outgoing
9834 | | arguments
9835 current stack pointer -> | | /
9838 For a given function some or all of these stack components
9839 may not be needed, giving rise to the possibility of
9840 eliminating some of the registers.
9842 The values returned by this function must reflect the behavior
9843 of arm_expand_prologue() and arm_compute_save_reg_mask().
9845 The sign of the number returned reflects the direction of stack
9846 growth, so the values are positive for all eliminations except
9847 from the soft frame pointer to the hard frame pointer.
9849 SFP may point just inside the local variables block to ensure correct
9850 alignment. */
9853 /* Calculate stack offsets. These are used to calculate register elimination
9854 offsets and in prologue/epilogue code. */
9856 static arm_stack_offsets *
9857 arm_get_frame_offsets (void)
9859 struct arm_stack_offsets *offsets;
9860 unsigned long func_type;
9861 int leaf;
9862 int saved;
9863 HOST_WIDE_INT frame_size;
9865 offsets = &cfun->machine->stack_offsets;
9867 /* We need to know if we are a leaf function. Unfortunately, it
9868 is possible to be called after start_sequence has been called,
9869 which causes get_insns to return the insns for the sequence,
9870 not the function, which will cause leaf_function_p to return
9871 the incorrect result.
9873 to know about leaf functions once reload has completed, and the
9874 frame size cannot be changed after that time, so we can safely
9875 use the cached value. */
9877 if (reload_completed)
9878 return offsets;
9880 /* Initially this is the size of the local variables. It will translated
9881 into an offset once we have determined the size of preceding data. */
9882 frame_size = ROUND_UP_WORD (get_frame_size ());
9884 leaf = leaf_function_p ();
9886 /* Space for variadic functions. */
9887 offsets->saved_args = current_function_pretend_args_size;
9889 offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
9891 if (TARGET_ARM)
9893 unsigned int regno;
9895 saved = bit_count (arm_compute_save_reg_mask ()) * 4;
9897 /* We know that SP will be doubleword aligned on entry, and we must
9898 preserve that condition at any subroutine call. We also require the
9899 soft frame pointer to be doubleword aligned. */
9901 if (TARGET_REALLY_IWMMXT)
9903 /* Check for the call-saved iWMMXt registers. */
9904 for (regno = FIRST_IWMMXT_REGNUM;
9905 regno <= LAST_IWMMXT_REGNUM;
9906 regno++)
9907 if (regs_ever_live [regno] && ! call_used_regs [regno])
9908 saved += 8;
9911 func_type = arm_current_func_type ();
9912 if (! IS_VOLATILE (func_type))
9914 /* Space for saved FPA registers. */
9915 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
9916 if (regs_ever_live[regno] && ! call_used_regs[regno])
9917 saved += 12;
9919 /* Space for saved VFP registers. */
9920 if (TARGET_HARD_FLOAT && TARGET_VFP)
9921 saved += arm_get_vfp_saved_size ();
9924 else /* TARGET_THUMB */
9926 saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
9927 if (TARGET_BACKTRACE)
9928 saved += 16;
9931 /* Saved registers include the stack frame. */
9932 offsets->saved_regs = offsets->saved_args + saved;
9933 offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
9934 /* A leaf function does not need any stack alignment if it has nothing
9935 on the stack. */
9936 if (leaf && frame_size == 0)
9938 offsets->outgoing_args = offsets->soft_frame;
9939 return offsets;
9942 /* Ensure SFP has the correct alignment. */
9943 if (ARM_DOUBLEWORD_ALIGN
9944 && (offsets->soft_frame & 7))
9945 offsets->soft_frame += 4;
9947 offsets->outgoing_args = offsets->soft_frame + frame_size
9948 + current_function_outgoing_args_size;
9950 if (ARM_DOUBLEWORD_ALIGN)
9952 /* Ensure SP remains doubleword aligned. */
9953 if (offsets->outgoing_args & 7)
9954 offsets->outgoing_args += 4;
9955 gcc_assert (!(offsets->outgoing_args & 7));
9958 return offsets;
9962 /* Calculate the relative offsets for the different stack pointers. Positive
9963 offsets are in the direction of stack growth. */
9965 HOST_WIDE_INT
9966 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
9968 arm_stack_offsets *offsets;
9970 offsets = arm_get_frame_offsets ();
9972 /* OK, now we have enough information to compute the distances.
9973 There must be an entry in these switch tables for each pair
9974 of registers in ELIMINABLE_REGS, even if some of the entries
9975 seem to be redundant or useless. */
9976 switch (from)
9978 case ARG_POINTER_REGNUM:
9979 switch (to)
9981 case THUMB_HARD_FRAME_POINTER_REGNUM:
9982 return 0;
9984 case FRAME_POINTER_REGNUM:
9985 /* This is the reverse of the soft frame pointer
9986 to hard frame pointer elimination below. */
9987 return offsets->soft_frame - offsets->saved_args;
9989 case ARM_HARD_FRAME_POINTER_REGNUM:
9990 /* If there is no stack frame then the hard
9991 frame pointer and the arg pointer coincide. */
9992 if (offsets->frame == offsets->saved_regs)
9993 return 0;
9994 /* FIXME: Not sure about this. Maybe we should always return 0 ? */
9995 return (frame_pointer_needed
9996 && cfun->static_chain_decl != NULL
9997 && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
9999 case STACK_POINTER_REGNUM:
10000 /* If nothing has been pushed on the stack at all
10001 then this will return -4. This *is* correct! */
10002 return offsets->outgoing_args - (offsets->saved_args + 4);
10004 default:
10005 gcc_unreachable ();
10007 gcc_unreachable ();
10009 case FRAME_POINTER_REGNUM:
10010 switch (to)
10012 case THUMB_HARD_FRAME_POINTER_REGNUM:
10013 return 0;
10015 case ARM_HARD_FRAME_POINTER_REGNUM:
10016 /* The hard frame pointer points to the top entry in the
10017 stack frame. The soft frame pointer to the bottom entry
10018 in the stack frame. If there is no stack frame at all,
10019 then they are identical. */
10021 return offsets->frame - offsets->soft_frame;
10023 case STACK_POINTER_REGNUM:
10024 return offsets->outgoing_args - offsets->soft_frame;
10026 default:
10027 gcc_unreachable ();
10029 gcc_unreachable ();
10031 default:
10032 /* You cannot eliminate from the stack pointer.
10033 In theory you could eliminate from the hard frame
10034 pointer to the stack pointer, but this will never
10035 happen, since if a stack frame is not needed the
10036 hard frame pointer will never be used. */
10037 gcc_unreachable ();
10042 /* Generate the prologue instructions for entry into an ARM function. */
10043 void
10044 arm_expand_prologue (void)
10046 int reg;
10047 rtx amount;
10048 rtx insn;
10049 rtx ip_rtx;
10050 unsigned long live_regs_mask;
10051 unsigned long func_type;
10052 int fp_offset = 0;
10053 int saved_pretend_args = 0;
10054 int saved_regs = 0;
10055 unsigned HOST_WIDE_INT args_to_push;
10056 arm_stack_offsets *offsets;
10058 func_type = arm_current_func_type ();
10060 /* Naked functions don't have prologues. */
10061 if (IS_NAKED (func_type))
10062 return;
10064 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
10065 args_to_push = current_function_pretend_args_size;
10067 /* Compute which register we will have to save onto the stack. */
10068 live_regs_mask = arm_compute_save_reg_mask ();
10070 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10072 if (frame_pointer_needed)
10074 if (IS_INTERRUPT (func_type))
10076 /* Interrupt functions must not corrupt any registers.
10077 Creating a frame pointer however, corrupts the IP
10078 register, so we must push it first. */
10079 insn = emit_multi_reg_push (1 << IP_REGNUM);
10081 /* Do not set RTX_FRAME_RELATED_P on this insn.
10082 The dwarf stack unwinding code only wants to see one
10083 stack decrement per function, and this is not it. If
10084 this instruction is labeled as being part of the frame
10085 creation sequence then dwarf2out_frame_debug_expr will
10086 die when it encounters the assignment of IP to FP
10087 later on, since the use of SP here establishes SP as
10088 the CFA register and not IP.
10090 Anyway this instruction is not really part of the stack
10091 frame creation although it is part of the prologue. */
10093 else if (IS_NESTED (func_type))
10095 /* The Static chain register is the same as the IP register
10096 used as a scratch register during stack frame creation.
10097 To get around this need to find somewhere to store IP
10098 whilst the frame is being created. We try the following
10099 places in order:
10101 1. The last argument register.
10102 2. A slot on the stack above the frame. (This only
10103 works if the function is not a varargs function).
10104 3. Register r3, after pushing the argument registers
10105 onto the stack.
10107 Note - we only need to tell the dwarf2 backend about the SP
10108 adjustment in the second variant; the static chain register
10109 doesn't need to be unwound, as it doesn't contain a value
10110 inherited from the caller. */
10112 if (regs_ever_live[3] == 0)
10114 insn = gen_rtx_REG (SImode, 3);
10115 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10116 insn = emit_insn (insn);
10118 else if (args_to_push == 0)
10120 rtx dwarf;
10121 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10122 insn = gen_rtx_MEM (SImode, insn);
10123 insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10124 insn = emit_insn (insn);
10126 fp_offset = 4;
10128 /* Just tell the dwarf backend that we adjusted SP. */
10129 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10130 gen_rtx_PLUS (SImode, stack_pointer_rtx,
10131 GEN_INT (-fp_offset)));
10132 RTX_FRAME_RELATED_P (insn) = 1;
10133 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10134 dwarf, REG_NOTES (insn));
10136 else
10138 /* Store the args on the stack. */
10139 if (cfun->machine->uses_anonymous_args)
10140 insn = emit_multi_reg_push
10141 ((0xf0 >> (args_to_push / 4)) & 0xf);
10142 else
10143 insn = emit_insn
10144 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10145 GEN_INT (- args_to_push)));
10147 RTX_FRAME_RELATED_P (insn) = 1;
10149 saved_pretend_args = 1;
10150 fp_offset = args_to_push;
10151 args_to_push = 0;
10153 /* Now reuse r3 to preserve IP. */
10154 insn = gen_rtx_REG (SImode, 3);
10155 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10156 (void) emit_insn (insn);
10160 if (fp_offset)
10162 insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10163 insn = gen_rtx_SET (SImode, ip_rtx, insn);
10165 else
10166 insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10168 insn = emit_insn (insn);
10169 RTX_FRAME_RELATED_P (insn) = 1;
10172 if (args_to_push)
10174 /* Push the argument registers, or reserve space for them. */
10175 if (cfun->machine->uses_anonymous_args)
10176 insn = emit_multi_reg_push
10177 ((0xf0 >> (args_to_push / 4)) & 0xf);
10178 else
10179 insn = emit_insn
10180 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10181 GEN_INT (- args_to_push)));
10182 RTX_FRAME_RELATED_P (insn) = 1;
10185 /* If this is an interrupt service routine, and the link register
10186 is going to be pushed, and we are not creating a stack frame,
10187 (which would involve an extra push of IP and a pop in the epilogue)
10188 subtracting four from LR now will mean that the function return
10189 can be done with a single instruction. */
10190 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10191 && (live_regs_mask & (1 << LR_REGNUM)) != 0
10192 && ! frame_pointer_needed)
10193 emit_insn (gen_rtx_SET (SImode,
10194 gen_rtx_REG (SImode, LR_REGNUM),
10195 gen_rtx_PLUS (SImode,
10196 gen_rtx_REG (SImode, LR_REGNUM),
10197 GEN_INT (-4))));
10199 if (live_regs_mask)
10201 insn = emit_multi_reg_push (live_regs_mask);
10202 saved_regs += bit_count (live_regs_mask) * 4;
10203 RTX_FRAME_RELATED_P (insn) = 1;
10206 if (TARGET_IWMMXT)
10207 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10208 if (regs_ever_live[reg] && ! call_used_regs [reg])
10210 insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10211 insn = gen_rtx_MEM (V2SImode, insn);
10212 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10213 gen_rtx_REG (V2SImode, reg)));
10214 RTX_FRAME_RELATED_P (insn) = 1;
10215 saved_regs += 8;
10218 if (! IS_VOLATILE (func_type))
10220 int start_reg;
10222 /* Save any floating point call-saved registers used by this
10223 function. */
10224 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10226 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10227 if (regs_ever_live[reg] && !call_used_regs[reg])
10229 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10230 insn = gen_rtx_MEM (XFmode, insn);
10231 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10232 gen_rtx_REG (XFmode, reg)));
10233 RTX_FRAME_RELATED_P (insn) = 1;
10234 saved_regs += 12;
10237 else
10239 start_reg = LAST_FPA_REGNUM;
10241 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10243 if (regs_ever_live[reg] && !call_used_regs[reg])
10245 if (start_reg - reg == 3)
10247 insn = emit_sfm (reg, 4);
10248 RTX_FRAME_RELATED_P (insn) = 1;
10249 saved_regs += 48;
10250 start_reg = reg - 1;
10253 else
10255 if (start_reg != reg)
10257 insn = emit_sfm (reg + 1, start_reg - reg);
10258 RTX_FRAME_RELATED_P (insn) = 1;
10259 saved_regs += (start_reg - reg) * 12;
10261 start_reg = reg - 1;
10265 if (start_reg != reg)
10267 insn = emit_sfm (reg + 1, start_reg - reg);
10268 saved_regs += (start_reg - reg) * 12;
10269 RTX_FRAME_RELATED_P (insn) = 1;
10272 if (TARGET_HARD_FLOAT && TARGET_VFP)
10274 start_reg = FIRST_VFP_REGNUM;
10276 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10278 if ((!regs_ever_live[reg] || call_used_regs[reg])
10279 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10281 if (start_reg != reg)
10282 saved_regs += vfp_emit_fstmx (start_reg,
10283 (reg - start_reg) / 2);
10284 start_reg = reg + 2;
10287 if (start_reg != reg)
10288 saved_regs += vfp_emit_fstmx (start_reg,
10289 (reg - start_reg) / 2);
10293 if (frame_pointer_needed)
10295 /* Create the new frame pointer. */
10296 insn = GEN_INT (-(4 + args_to_push + fp_offset));
10297 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10298 RTX_FRAME_RELATED_P (insn) = 1;
10300 if (IS_NESTED (func_type))
10302 /* Recover the static chain register. */
10303 if (regs_ever_live [3] == 0
10304 || saved_pretend_args)
10305 insn = gen_rtx_REG (SImode, 3);
10306 else /* if (current_function_pretend_args_size == 0) */
10308 insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10309 GEN_INT (4));
10310 insn = gen_rtx_MEM (SImode, insn);
10313 emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10314 /* Add a USE to stop propagate_one_insn() from barfing. */
10315 emit_insn (gen_prologue_use (ip_rtx));
10319 offsets = arm_get_frame_offsets ();
10320 if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10322 /* This add can produce multiple insns for a large constant, so we
10323 need to get tricky. */
10324 rtx last = get_last_insn ();
10326 amount = GEN_INT (offsets->saved_args + saved_regs
10327 - offsets->outgoing_args);
10329 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10330 amount));
10333 last = last ? NEXT_INSN (last) : get_insns ();
10334 RTX_FRAME_RELATED_P (last) = 1;
10336 while (last != insn);
10338 /* If the frame pointer is needed, emit a special barrier that
10339 will prevent the scheduler from moving stores to the frame
10340 before the stack adjustment. */
10341 if (frame_pointer_needed)
10342 insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10343 hard_frame_pointer_rtx));
10347 if (flag_pic)
10348 arm_load_pic_register (INVALID_REGNUM);
10350 /* If we are profiling, make sure no instructions are scheduled before
10351 the call to mcount. Similarly if the user has requested no
10352 scheduling in the prolog. */
10353 if (current_function_profile || TARGET_NO_SCHED_PRO)
10354 emit_insn (gen_blockage ());
10356 /* If the link register is being kept alive, with the return address in it,
10357 then make sure that it does not get reused by the ce2 pass. */
10358 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10360 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10361 cfun->machine->lr_save_eliminated = 1;
10365 /* If CODE is 'd', then the X is a condition operand and the instruction
10366 should only be executed if the condition is true.
10367 if CODE is 'D', then the X is a condition operand and the instruction
10368 should only be executed if the condition is false: however, if the mode
10369 of the comparison is CCFPEmode, then always execute the instruction -- we
10370 do this because in these circumstances !GE does not necessarily imply LT;
10371 in these cases the instruction pattern will take care to make sure that
10372 an instruction containing %d will follow, thereby undoing the effects of
10373 doing this instruction unconditionally.
10374 If CODE is 'N' then X is a floating point operand that must be negated
10375 before output.
10376 If CODE is 'B' then output a bitwise inverted value of X (a const int).
10377 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
10378 void
10379 arm_print_operand (FILE *stream, rtx x, int code)
10381 switch (code)
10383 case '@':
10384 fputs (ASM_COMMENT_START, stream);
10385 return;
10387 case '_':
10388 fputs (user_label_prefix, stream);
10389 return;
10391 case '|':
10392 fputs (REGISTER_PREFIX, stream);
10393 return;
10395 case '?':
10396 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10398 if (TARGET_THUMB)
10400 output_operand_lossage ("predicated Thumb instruction");
10401 break;
10403 if (current_insn_predicate != NULL)
10405 output_operand_lossage
10406 ("predicated instruction in conditional sequence");
10407 break;
10410 fputs (arm_condition_codes[arm_current_cc], stream);
10412 else if (current_insn_predicate)
10414 enum arm_cond_code code;
10416 if (TARGET_THUMB)
10418 output_operand_lossage ("predicated Thumb instruction");
10419 break;
10422 code = get_arm_condition_code (current_insn_predicate);
10423 fputs (arm_condition_codes[code], stream);
10425 return;
10427 case 'N':
10429 REAL_VALUE_TYPE r;
10430 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10431 r = REAL_VALUE_NEGATE (r);
10432 fprintf (stream, "%s", fp_const_from_val (&r));
10434 return;
10436 case 'B':
10437 if (GET_CODE (x) == CONST_INT)
10439 HOST_WIDE_INT val;
10440 val = ARM_SIGN_EXTEND (~INTVAL (x));
10441 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10443 else
10445 putc ('~', stream);
10446 output_addr_const (stream, x);
10448 return;
10450 case 'i':
10451 fprintf (stream, "%s", arithmetic_instr (x, 1));
10452 return;
10454 /* Truncate Cirrus shift counts. */
10455 case 's':
10456 if (GET_CODE (x) == CONST_INT)
10458 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10459 return;
10461 arm_print_operand (stream, x, 0);
10462 return;
10464 case 'I':
10465 fprintf (stream, "%s", arithmetic_instr (x, 0));
10466 return;
10468 case 'S':
10470 HOST_WIDE_INT val;
10471 const char * shift = shift_op (x, &val);
10473 if (shift)
10475 fprintf (stream, ", %s ", shift_op (x, &val));
10476 if (val == -1)
10477 arm_print_operand (stream, XEXP (x, 1), 0);
10478 else
10479 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10482 return;
10484 /* An explanation of the 'Q', 'R' and 'H' register operands:
10486 In a pair of registers containing a DI or DF value the 'Q'
10487 operand returns the register number of the register containing
10488 the least significant part of the value. The 'R' operand returns
10489 the register number of the register containing the most
10490 significant part of the value.
10492 The 'H' operand returns the higher of the two register numbers.
10493 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10494 same as the 'Q' operand, since the most significant part of the
10495 value is held in the lower number register. The reverse is true
10496 on systems where WORDS_BIG_ENDIAN is false.
10498 The purpose of these operands is to distinguish between cases
10499 where the endian-ness of the values is important (for example
10500 when they are added together), and cases where the endian-ness
10501 is irrelevant, but the order of register operations is important.
10502 For example when loading a value from memory into a register
10503 pair, the endian-ness does not matter. Provided that the value
10504 from the lower memory address is put into the lower numbered
10505 register, and the value from the higher address is put into the
10506 higher numbered register, the load will work regardless of whether
10507 the value being loaded is big-wordian or little-wordian. The
10508 order of the two register loads can matter however, if the address
10509 of the memory location is actually held in one of the registers
10510 being overwritten by the load. */
10511 case 'Q':
10512 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10514 output_operand_lossage ("invalid operand for code '%c'", code);
10515 return;
10518 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10519 return;
10521 case 'R':
10522 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10524 output_operand_lossage ("invalid operand for code '%c'", code);
10525 return;
10528 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10529 return;
10531 case 'H':
10532 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10534 output_operand_lossage ("invalid operand for code '%c'", code);
10535 return;
10538 asm_fprintf (stream, "%r", REGNO (x) + 1);
10539 return;
10541 case 'm':
10542 asm_fprintf (stream, "%r",
10543 GET_CODE (XEXP (x, 0)) == REG
10544 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10545 return;
10547 case 'M':
10548 asm_fprintf (stream, "{%r-%r}",
10549 REGNO (x),
10550 REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10551 return;
10553 case 'd':
10554 /* CONST_TRUE_RTX means always -- that's the default. */
10555 if (x == const_true_rtx)
10556 return;
10558 if (!COMPARISON_P (x))
10560 output_operand_lossage ("invalid operand for code '%c'", code);
10561 return;
10564 fputs (arm_condition_codes[get_arm_condition_code (x)],
10565 stream);
10566 return;
10568 case 'D':
10569 /* CONST_TRUE_RTX means not always -- i.e. never. We shouldn't ever
10570 want to do that. */
10571 if (x == const_true_rtx)
10573 output_operand_lossage ("instruction never exectued");
10574 return;
10576 if (!COMPARISON_P (x))
10578 output_operand_lossage ("invalid operand for code '%c'", code);
10579 return;
10582 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10583 (get_arm_condition_code (x))],
10584 stream);
10585 return;
10587 /* Cirrus registers can be accessed in a variety of ways:
10588 single floating point (f)
10589 double floating point (d)
10590 32bit integer (fx)
10591 64bit integer (dx). */
10592 case 'W': /* Cirrus register in F mode. */
10593 case 'X': /* Cirrus register in D mode. */
10594 case 'Y': /* Cirrus register in FX mode. */
10595 case 'Z': /* Cirrus register in DX mode. */
10596 gcc_assert (GET_CODE (x) == REG
10597 && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
10599 fprintf (stream, "mv%s%s",
10600 code == 'W' ? "f"
10601 : code == 'X' ? "d"
10602 : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10604 return;
10606 /* Print cirrus register in the mode specified by the register's mode. */
10607 case 'V':
10609 int mode = GET_MODE (x);
10611 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10613 output_operand_lossage ("invalid operand for code '%c'", code);
10614 return;
10617 fprintf (stream, "mv%s%s",
10618 mode == DFmode ? "d"
10619 : mode == SImode ? "fx"
10620 : mode == DImode ? "dx"
10621 : "f", reg_names[REGNO (x)] + 2);
10623 return;
10626 case 'U':
10627 if (GET_CODE (x) != REG
10628 || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10629 || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10630 /* Bad value for wCG register number. */
10632 output_operand_lossage ("invalid operand for code '%c'", code);
10633 return;
10636 else
10637 fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10638 return;
10640 /* Print an iWMMXt control register name. */
10641 case 'w':
10642 if (GET_CODE (x) != CONST_INT
10643 || INTVAL (x) < 0
10644 || INTVAL (x) >= 16)
10645 /* Bad value for wC register number. */
10647 output_operand_lossage ("invalid operand for code '%c'", code);
10648 return;
10651 else
10653 static const char * wc_reg_names [16] =
10655 "wCID", "wCon", "wCSSF", "wCASF",
10656 "wC4", "wC5", "wC6", "wC7",
10657 "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10658 "wC12", "wC13", "wC14", "wC15"
10661 fprintf (stream, wc_reg_names [INTVAL (x)]);
10663 return;
10665 /* Print a VFP double precision register name. */
10666 case 'P':
10668 int mode = GET_MODE (x);
10669 int num;
10671 if (mode != DImode && mode != DFmode)
10673 output_operand_lossage ("invalid operand for code '%c'", code);
10674 return;
10677 if (GET_CODE (x) != REG
10678 || !IS_VFP_REGNUM (REGNO (x)))
10680 output_operand_lossage ("invalid operand for code '%c'", code);
10681 return;
10684 num = REGNO(x) - FIRST_VFP_REGNUM;
10685 if (num & 1)
10687 output_operand_lossage ("invalid operand for code '%c'", code);
10688 return;
10691 fprintf (stream, "d%d", num >> 1);
10693 return;
10695 default:
10696 if (x == 0)
10698 output_operand_lossage ("missing operand");
10699 return;
10702 switch (GET_CODE (x))
10704 case REG:
10705 asm_fprintf (stream, "%r", REGNO (x));
10706 break;
10708 case MEM:
10709 output_memory_reference_mode = GET_MODE (x);
10710 output_address (XEXP (x, 0));
10711 break;
10713 case CONST_DOUBLE:
10714 fprintf (stream, "#%s", fp_immediate_constant (x));
10715 break;
10717 default:
10718 gcc_assert (GET_CODE (x) != NEG);
10719 fputc ('#', stream);
10720 output_addr_const (stream, x);
10721 break;
10726 #ifndef AOF_ASSEMBLER
10727 /* Target hook for assembling integer objects. The ARM version needs to
10728 handle word-sized values specially. */
10729 static bool
10730 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10732 if (size == UNITS_PER_WORD && aligned_p)
10734 fputs ("\t.word\t", asm_out_file);
10735 output_addr_const (asm_out_file, x);
10737 /* Mark symbols as position independent. We only do this in the
10738 .text segment, not in the .data segment. */
10739 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10740 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10742 if (GET_CODE (x) == SYMBOL_REF
10743 && (CONSTANT_POOL_ADDRESS_P (x)
10744 || SYMBOL_REF_LOCAL_P (x)))
10745 fputs ("(GOTOFF)", asm_out_file);
10746 else if (GET_CODE (x) == LABEL_REF)
10747 fputs ("(GOTOFF)", asm_out_file);
10748 else
10749 fputs ("(GOT)", asm_out_file);
10751 fputc ('\n', asm_out_file);
10752 return true;
10755 if (arm_vector_mode_supported_p (GET_MODE (x)))
10757 int i, units;
10759 gcc_assert (GET_CODE (x) == CONST_VECTOR);
10761 units = CONST_VECTOR_NUNITS (x);
10763 switch (GET_MODE (x))
10765 case V2SImode: size = 4; break;
10766 case V4HImode: size = 2; break;
10767 case V8QImode: size = 1; break;
10768 default:
10769 gcc_unreachable ();
10772 for (i = 0; i < units; i++)
10774 rtx elt;
10776 elt = CONST_VECTOR_ELT (x, i);
10777 assemble_integer
10778 (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10781 return true;
10784 return default_assemble_integer (x, size, aligned_p);
10788 /* Add a function to the list of static constructors. */
10790 static void
10791 arm_elf_asm_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
10793 if (!TARGET_AAPCS_BASED)
10795 default_named_section_asm_out_constructor (symbol, priority);
10796 return;
10799 /* Put these in the .init_array section, using a special relocation. */
10800 ctors_section ();
10801 assemble_align (POINTER_SIZE);
10802 fputs ("\t.word\t", asm_out_file);
10803 output_addr_const (asm_out_file, symbol);
10804 fputs ("(target1)\n", asm_out_file);
10806 #endif
10808 /* A finite state machine takes care of noticing whether or not instructions
10809 can be conditionally executed, and thus decrease execution time and code
10810 size by deleting branch instructions. The fsm is controlled by
10811 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
10813 /* The state of the fsm controlling condition codes are:
10814 0: normal, do nothing special
10815 1: make ASM_OUTPUT_OPCODE not output this instruction
10816 2: make ASM_OUTPUT_OPCODE not output this instruction
10817 3: make instructions conditional
10818 4: make instructions conditional
10820 State transitions (state->state by whom under condition):
10821 0 -> 1 final_prescan_insn if the `target' is a label
10822 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10823 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10824 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10825 3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10826 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10827 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10828 (the target insn is arm_target_insn).
10830 If the jump clobbers the conditions then we use states 2 and 4.
10832 A similar thing can be done with conditional return insns.
10834 XXX In case the `target' is an unconditional branch, this conditionalising
10835 of the instructions always reduces code size, but not always execution
10836 time. But then, I want to reduce the code size to somewhere near what
10837 /bin/cc produces. */
10839 /* Returns the index of the ARM condition code string in
10840 `arm_condition_codes'. COMPARISON should be an rtx like
10841 `(eq (...) (...))'. */
10842 static enum arm_cond_code
10843 get_arm_condition_code (rtx comparison)
10845 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10846 int code;
10847 enum rtx_code comp_code = GET_CODE (comparison);
10849 if (GET_MODE_CLASS (mode) != MODE_CC)
10850 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10851 XEXP (comparison, 1));
10853 switch (mode)
10855 case CC_DNEmode: code = ARM_NE; goto dominance;
10856 case CC_DEQmode: code = ARM_EQ; goto dominance;
10857 case CC_DGEmode: code = ARM_GE; goto dominance;
10858 case CC_DGTmode: code = ARM_GT; goto dominance;
10859 case CC_DLEmode: code = ARM_LE; goto dominance;
10860 case CC_DLTmode: code = ARM_LT; goto dominance;
10861 case CC_DGEUmode: code = ARM_CS; goto dominance;
10862 case CC_DGTUmode: code = ARM_HI; goto dominance;
10863 case CC_DLEUmode: code = ARM_LS; goto dominance;
10864 case CC_DLTUmode: code = ARM_CC;
10866 dominance:
10867 gcc_assert (comp_code == EQ || comp_code == NE);
10869 if (comp_code == EQ)
10870 return ARM_INVERSE_CONDITION_CODE (code);
10871 return code;
10873 case CC_NOOVmode:
10874 switch (comp_code)
10876 case NE: return ARM_NE;
10877 case EQ: return ARM_EQ;
10878 case GE: return ARM_PL;
10879 case LT: return ARM_MI;
10880 default: gcc_unreachable ();
10883 case CC_Zmode:
10884 switch (comp_code)
10886 case NE: return ARM_NE;
10887 case EQ: return ARM_EQ;
10888 default: gcc_unreachable ();
10891 case CC_Nmode:
10892 switch (comp_code)
10894 case NE: return ARM_MI;
10895 case EQ: return ARM_PL;
10896 default: gcc_unreachable ();
10899 case CCFPEmode:
10900 case CCFPmode:
10901 /* These encodings assume that AC=1 in the FPA system control
10902 byte. This allows us to handle all cases except UNEQ and
10903 LTGT. */
10904 switch (comp_code)
10906 case GE: return ARM_GE;
10907 case GT: return ARM_GT;
10908 case LE: return ARM_LS;
10909 case LT: return ARM_MI;
10910 case NE: return ARM_NE;
10911 case EQ: return ARM_EQ;
10912 case ORDERED: return ARM_VC;
10913 case UNORDERED: return ARM_VS;
10914 case UNLT: return ARM_LT;
10915 case UNLE: return ARM_LE;
10916 case UNGT: return ARM_HI;
10917 case UNGE: return ARM_PL;
10918 /* UNEQ and LTGT do not have a representation. */
10919 case UNEQ: /* Fall through. */
10920 case LTGT: /* Fall through. */
10921 default: gcc_unreachable ();
10924 case CC_SWPmode:
10925 switch (comp_code)
10927 case NE: return ARM_NE;
10928 case EQ: return ARM_EQ;
10929 case GE: return ARM_LE;
10930 case GT: return ARM_LT;
10931 case LE: return ARM_GE;
10932 case LT: return ARM_GT;
10933 case GEU: return ARM_LS;
10934 case GTU: return ARM_CC;
10935 case LEU: return ARM_CS;
10936 case LTU: return ARM_HI;
10937 default: gcc_unreachable ();
10940 case CC_Cmode:
10941 switch (comp_code)
10943 case LTU: return ARM_CS;
10944 case GEU: return ARM_CC;
10945 default: gcc_unreachable ();
10948 case CCmode:
10949 switch (comp_code)
10951 case NE: return ARM_NE;
10952 case EQ: return ARM_EQ;
10953 case GE: return ARM_GE;
10954 case GT: return ARM_GT;
10955 case LE: return ARM_LE;
10956 case LT: return ARM_LT;
10957 case GEU: return ARM_CS;
10958 case GTU: return ARM_HI;
10959 case LEU: return ARM_LS;
10960 case LTU: return ARM_CC;
10961 default: gcc_unreachable ();
10964 default: gcc_unreachable ();
10968 void
10969 arm_final_prescan_insn (rtx insn)
10971 /* BODY will hold the body of INSN. */
10972 rtx body = PATTERN (insn);
10974 /* This will be 1 if trying to repeat the trick, and things need to be
10975 reversed if it appears to fail. */
10976 int reverse = 0;
10978 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
10979 taken are clobbered, even if the rtl suggests otherwise. It also
10980 means that we have to grub around within the jump expression to find
10981 out what the conditions are when the jump isn't taken. */
10982 int jump_clobbers = 0;
10984 /* If we start with a return insn, we only succeed if we find another one. */
10985 int seeking_return = 0;
10987 /* START_INSN will hold the insn from where we start looking. This is the
10988 first insn after the following code_label if REVERSE is true. */
10989 rtx start_insn = insn;
10991 /* If in state 4, check if the target branch is reached, in order to
10992 change back to state 0. */
10993 if (arm_ccfsm_state == 4)
10995 if (insn == arm_target_insn)
10997 arm_target_insn = NULL;
10998 arm_ccfsm_state = 0;
11000 return;
11003 /* If in state 3, it is possible to repeat the trick, if this insn is an
11004 unconditional branch to a label, and immediately following this branch
11005 is the previous target label which is only used once, and the label this
11006 branch jumps to is not too far off. */
11007 if (arm_ccfsm_state == 3)
11009 if (simplejump_p (insn))
11011 start_insn = next_nonnote_insn (start_insn);
11012 if (GET_CODE (start_insn) == BARRIER)
11014 /* XXX Isn't this always a barrier? */
11015 start_insn = next_nonnote_insn (start_insn);
11017 if (GET_CODE (start_insn) == CODE_LABEL
11018 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11019 && LABEL_NUSES (start_insn) == 1)
11020 reverse = TRUE;
11021 else
11022 return;
11024 else if (GET_CODE (body) == RETURN)
11026 start_insn = next_nonnote_insn (start_insn);
11027 if (GET_CODE (start_insn) == BARRIER)
11028 start_insn = next_nonnote_insn (start_insn);
11029 if (GET_CODE (start_insn) == CODE_LABEL
11030 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11031 && LABEL_NUSES (start_insn) == 1)
11033 reverse = TRUE;
11034 seeking_return = 1;
11036 else
11037 return;
11039 else
11040 return;
11043 gcc_assert (!arm_ccfsm_state || reverse);
11044 if (GET_CODE (insn) != JUMP_INSN)
11045 return;
11047 /* This jump might be paralleled with a clobber of the condition codes
11048 the jump should always come first */
11049 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11050 body = XVECEXP (body, 0, 0);
11052 if (reverse
11053 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11054 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11056 int insns_skipped;
11057 int fail = FALSE, succeed = FALSE;
11058 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
11059 int then_not_else = TRUE;
11060 rtx this_insn = start_insn, label = 0;
11062 /* If the jump cannot be done with one instruction, we cannot
11063 conditionally execute the instruction in the inverse case. */
11064 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11066 jump_clobbers = 1;
11067 return;
11070 /* Register the insn jumped to. */
11071 if (reverse)
11073 if (!seeking_return)
11074 label = XEXP (SET_SRC (body), 0);
11076 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11077 label = XEXP (XEXP (SET_SRC (body), 1), 0);
11078 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11080 label = XEXP (XEXP (SET_SRC (body), 2), 0);
11081 then_not_else = FALSE;
11083 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11084 seeking_return = 1;
11085 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11087 seeking_return = 1;
11088 then_not_else = FALSE;
11090 else
11091 gcc_unreachable ();
11093 /* See how many insns this branch skips, and what kind of insns. If all
11094 insns are okay, and the label or unconditional branch to the same
11095 label is not too far away, succeed. */
11096 for (insns_skipped = 0;
11097 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11099 rtx scanbody;
11101 this_insn = next_nonnote_insn (this_insn);
11102 if (!this_insn)
11103 break;
11105 switch (GET_CODE (this_insn))
11107 case CODE_LABEL:
11108 /* Succeed if it is the target label, otherwise fail since
11109 control falls in from somewhere else. */
11110 if (this_insn == label)
11112 if (jump_clobbers)
11114 arm_ccfsm_state = 2;
11115 this_insn = next_nonnote_insn (this_insn);
11117 else
11118 arm_ccfsm_state = 1;
11119 succeed = TRUE;
11121 else
11122 fail = TRUE;
11123 break;
11125 case BARRIER:
11126 /* Succeed if the following insn is the target label.
11127 Otherwise fail.
11128 If return insns are used then the last insn in a function
11129 will be a barrier. */
11130 this_insn = next_nonnote_insn (this_insn);
11131 if (this_insn && this_insn == label)
11133 if (jump_clobbers)
11135 arm_ccfsm_state = 2;
11136 this_insn = next_nonnote_insn (this_insn);
11138 else
11139 arm_ccfsm_state = 1;
11140 succeed = TRUE;
11142 else
11143 fail = TRUE;
11144 break;
11146 case CALL_INSN:
11147 /* The AAPCS says that conditional calls should not be
11148 used since they make interworking inefficient (the
11149 linker can't transform BL<cond> into BLX). That's
11150 only a problem if the machine has BLX. */
11151 if (arm_arch5)
11153 fail = TRUE;
11154 break;
11157 /* Succeed if the following insn is the target label, or
11158 if the following two insns are a barrier and the
11159 target label. */
11160 this_insn = next_nonnote_insn (this_insn);
11161 if (this_insn && GET_CODE (this_insn) == BARRIER)
11162 this_insn = next_nonnote_insn (this_insn);
11164 if (this_insn && this_insn == label
11165 && insns_skipped < max_insns_skipped)
11167 if (jump_clobbers)
11169 arm_ccfsm_state = 2;
11170 this_insn = next_nonnote_insn (this_insn);
11172 else
11173 arm_ccfsm_state = 1;
11174 succeed = TRUE;
11176 else
11177 fail = TRUE;
11178 break;
11180 case JUMP_INSN:
11181 /* If this is an unconditional branch to the same label, succeed.
11182 If it is to another label, do nothing. If it is conditional,
11183 fail. */
11184 /* XXX Probably, the tests for SET and the PC are
11185 unnecessary. */
11187 scanbody = PATTERN (this_insn);
11188 if (GET_CODE (scanbody) == SET
11189 && GET_CODE (SET_DEST (scanbody)) == PC)
11191 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11192 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11194 arm_ccfsm_state = 2;
11195 succeed = TRUE;
11197 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11198 fail = TRUE;
11200 /* Fail if a conditional return is undesirable (e.g. on a
11201 StrongARM), but still allow this if optimizing for size. */
11202 else if (GET_CODE (scanbody) == RETURN
11203 && !use_return_insn (TRUE, NULL)
11204 && !optimize_size)
11205 fail = TRUE;
11206 else if (GET_CODE (scanbody) == RETURN
11207 && seeking_return)
11209 arm_ccfsm_state = 2;
11210 succeed = TRUE;
11212 else if (GET_CODE (scanbody) == PARALLEL)
11214 switch (get_attr_conds (this_insn))
11216 case CONDS_NOCOND:
11217 break;
11218 default:
11219 fail = TRUE;
11220 break;
11223 else
11224 fail = TRUE; /* Unrecognized jump (e.g. epilogue). */
11226 break;
11228 case INSN:
11229 /* Instructions using or affecting the condition codes make it
11230 fail. */
11231 scanbody = PATTERN (this_insn);
11232 if (!(GET_CODE (scanbody) == SET
11233 || GET_CODE (scanbody) == PARALLEL)
11234 || get_attr_conds (this_insn) != CONDS_NOCOND)
11235 fail = TRUE;
11237 /* A conditional cirrus instruction must be followed by
11238 a non Cirrus instruction. However, since we
11239 conditionalize instructions in this function and by
11240 the time we get here we can't add instructions
11241 (nops), because shorten_branches() has already been
11242 called, we will disable conditionalizing Cirrus
11243 instructions to be safe. */
11244 if (GET_CODE (scanbody) != USE
11245 && GET_CODE (scanbody) != CLOBBER
11246 && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11247 fail = TRUE;
11248 break;
11250 default:
11251 break;
11254 if (succeed)
11256 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11257 arm_target_label = CODE_LABEL_NUMBER (label);
11258 else
11260 gcc_assert (seeking_return || arm_ccfsm_state == 2);
11262 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11264 this_insn = next_nonnote_insn (this_insn);
11265 gcc_assert (!this_insn
11266 || (GET_CODE (this_insn) != BARRIER
11267 && GET_CODE (this_insn) != CODE_LABEL));
11269 if (!this_insn)
11271 /* Oh, dear! we ran off the end.. give up. */
11272 recog (PATTERN (insn), insn, NULL);
11273 arm_ccfsm_state = 0;
11274 arm_target_insn = NULL;
11275 return;
11277 arm_target_insn = this_insn;
11279 if (jump_clobbers)
11281 gcc_assert (!reverse);
11282 arm_current_cc =
11283 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11284 0), 0), 1));
11285 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11286 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11287 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11288 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11290 else
11292 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11293 what it was. */
11294 if (!reverse)
11295 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11296 0));
11299 if (reverse || then_not_else)
11300 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11303 /* Restore recog_data (getting the attributes of other insns can
11304 destroy this array, but final.c assumes that it remains intact
11305 across this call; since the insn has been recognized already we
11306 call recog direct). */
11307 recog (PATTERN (insn), insn, NULL);
11311 /* Returns true if REGNO is a valid register
11312 for holding a quantity of type MODE. */
11314 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11316 if (GET_MODE_CLASS (mode) == MODE_CC)
11317 return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11319 if (TARGET_THUMB)
11320 /* For the Thumb we only allow values bigger than SImode in
11321 registers 0 - 6, so that there is always a second low
11322 register available to hold the upper part of the value.
11323 We probably we ought to ensure that the register is the
11324 start of an even numbered register pair. */
11325 return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11327 if (IS_CIRRUS_REGNUM (regno))
11328 /* We have outlawed SI values in Cirrus registers because they
11329 reside in the lower 32 bits, but SF values reside in the
11330 upper 32 bits. This causes gcc all sorts of grief. We can't
11331 even split the registers into pairs because Cirrus SI values
11332 get sign extended to 64bits-- aldyh. */
11333 return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11335 if (IS_VFP_REGNUM (regno))
11337 if (mode == SFmode || mode == SImode)
11338 return TRUE;
11340 /* DFmode values are only valid in even register pairs. */
11341 if (mode == DFmode)
11342 return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11343 return FALSE;
11346 if (IS_IWMMXT_GR_REGNUM (regno))
11347 return mode == SImode;
11349 if (IS_IWMMXT_REGNUM (regno))
11350 return VALID_IWMMXT_REG_MODE (mode);
11352 /* We allow any value to be stored in the general registers.
11353 Restrict doubleword quantities to even register pairs so that we can
11354 use ldrd. */
11355 if (regno <= LAST_ARM_REGNUM)
11356 return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11358 if ( regno == FRAME_POINTER_REGNUM
11359 || regno == ARG_POINTER_REGNUM)
11360 /* We only allow integers in the fake hard registers. */
11361 return GET_MODE_CLASS (mode) == MODE_INT;
11363 /* The only registers left are the FPA registers
11364 which we only allow to hold FP values. */
11365 return GET_MODE_CLASS (mode) == MODE_FLOAT
11366 && regno >= FIRST_FPA_REGNUM
11367 && regno <= LAST_FPA_REGNUM;
11371 arm_regno_class (int regno)
11373 if (TARGET_THUMB)
11375 if (regno == STACK_POINTER_REGNUM)
11376 return STACK_REG;
11377 if (regno == CC_REGNUM)
11378 return CC_REG;
11379 if (regno < 8)
11380 return LO_REGS;
11381 return HI_REGS;
11384 if ( regno <= LAST_ARM_REGNUM
11385 || regno == FRAME_POINTER_REGNUM
11386 || regno == ARG_POINTER_REGNUM)
11387 return GENERAL_REGS;
11389 if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11390 return NO_REGS;
11392 if (IS_CIRRUS_REGNUM (regno))
11393 return CIRRUS_REGS;
11395 if (IS_VFP_REGNUM (regno))
11396 return VFP_REGS;
11398 if (IS_IWMMXT_REGNUM (regno))
11399 return IWMMXT_REGS;
11401 if (IS_IWMMXT_GR_REGNUM (regno))
11402 return IWMMXT_GR_REGS;
11404 return FPA_REGS;
11407 /* Handle a special case when computing the offset
11408 of an argument from the frame pointer. */
11410 arm_debugger_arg_offset (int value, rtx addr)
11412 rtx insn;
11414 /* We are only interested if dbxout_parms() failed to compute the offset. */
11415 if (value != 0)
11416 return 0;
11418 /* We can only cope with the case where the address is held in a register. */
11419 if (GET_CODE (addr) != REG)
11420 return 0;
11422 /* If we are using the frame pointer to point at the argument, then
11423 an offset of 0 is correct. */
11424 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11425 return 0;
11427 /* If we are using the stack pointer to point at the
11428 argument, then an offset of 0 is correct. */
11429 if ((TARGET_THUMB || !frame_pointer_needed)
11430 && REGNO (addr) == SP_REGNUM)
11431 return 0;
11433 /* Oh dear. The argument is pointed to by a register rather
11434 than being held in a register, or being stored at a known
11435 offset from the frame pointer. Since GDB only understands
11436 those two kinds of argument we must translate the address
11437 held in the register into an offset from the frame pointer.
11438 We do this by searching through the insns for the function
11439 looking to see where this register gets its value. If the
11440 register is initialized from the frame pointer plus an offset
11441 then we are in luck and we can continue, otherwise we give up.
11443 This code is exercised by producing debugging information
11444 for a function with arguments like this:
11446 double func (double a, double b, int c, double d) {return d;}
11448 Without this code the stab for parameter 'd' will be set to
11449 an offset of 0 from the frame pointer, rather than 8. */
11451 /* The if() statement says:
11453 If the insn is a normal instruction
11454 and if the insn is setting the value in a register
11455 and if the register being set is the register holding the address of the argument
11456 and if the address is computing by an addition
11457 that involves adding to a register
11458 which is the frame pointer
11459 a constant integer
11461 then... */
11463 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11465 if ( GET_CODE (insn) == INSN
11466 && GET_CODE (PATTERN (insn)) == SET
11467 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11468 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11469 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11470 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11471 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11474 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11476 break;
11480 if (value == 0)
11482 debug_rtx (addr);
11483 warning (0, "unable to compute real location of stacked parameter");
11484 value = 8; /* XXX magic hack */
11487 return value;
11490 #define def_mbuiltin(MASK, NAME, TYPE, CODE) \
11491 do \
11493 if ((MASK) & insn_flags) \
11494 lang_hooks.builtin_function ((NAME), (TYPE), (CODE), \
11495 BUILT_IN_MD, NULL, NULL_TREE); \
11497 while (0)
11499 struct builtin_description
11501 const unsigned int mask;
11502 const enum insn_code icode;
11503 const char * const name;
11504 const enum arm_builtins code;
11505 const enum rtx_code comparison;
11506 const unsigned int flag;
11509 static const struct builtin_description bdesc_2arg[] =
11511 #define IWMMXT_BUILTIN(code, string, builtin) \
11512 { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11513 ARM_BUILTIN_##builtin, 0, 0 },
11515 IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11516 IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11517 IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11518 IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11519 IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11520 IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11521 IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11522 IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11523 IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11524 IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11525 IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11526 IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11527 IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11528 IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11529 IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11530 IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11531 IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11532 IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11533 IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11534 IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11535 IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11536 IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11537 IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11538 IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11539 IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11540 IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11541 IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11542 IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11543 IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11544 IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11545 IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11546 IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11547 IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11548 IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11549 IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11550 IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11551 IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11552 IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11553 IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11554 IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11555 IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11556 IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11557 IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11558 IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11559 IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11560 IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11561 IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11562 IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11563 IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11564 IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11565 IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11566 IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11567 IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11568 IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11569 IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11570 IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11571 IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11572 IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11574 #define IWMMXT_BUILTIN2(code, builtin) \
11575 { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11577 IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11578 IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11579 IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11580 IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11581 IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11582 IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11583 IWMMXT_BUILTIN2 (ashlv4hi3_di, WSLLH)
11584 IWMMXT_BUILTIN2 (ashlv4hi3, WSLLHI)
11585 IWMMXT_BUILTIN2 (ashlv2si3_di, WSLLW)
11586 IWMMXT_BUILTIN2 (ashlv2si3, WSLLWI)
11587 IWMMXT_BUILTIN2 (ashldi3_di, WSLLD)
11588 IWMMXT_BUILTIN2 (ashldi3_iwmmxt, WSLLDI)
11589 IWMMXT_BUILTIN2 (lshrv4hi3_di, WSRLH)
11590 IWMMXT_BUILTIN2 (lshrv4hi3, WSRLHI)
11591 IWMMXT_BUILTIN2 (lshrv2si3_di, WSRLW)
11592 IWMMXT_BUILTIN2 (lshrv2si3, WSRLWI)
11593 IWMMXT_BUILTIN2 (lshrdi3_di, WSRLD)
11594 IWMMXT_BUILTIN2 (lshrdi3_iwmmxt, WSRLDI)
11595 IWMMXT_BUILTIN2 (ashrv4hi3_di, WSRAH)
11596 IWMMXT_BUILTIN2 (ashrv4hi3, WSRAHI)
11597 IWMMXT_BUILTIN2 (ashrv2si3_di, WSRAW)
11598 IWMMXT_BUILTIN2 (ashrv2si3, WSRAWI)
11599 IWMMXT_BUILTIN2 (ashrdi3_di, WSRAD)
11600 IWMMXT_BUILTIN2 (ashrdi3_iwmmxt, WSRADI)
11601 IWMMXT_BUILTIN2 (rorv4hi3_di, WRORH)
11602 IWMMXT_BUILTIN2 (rorv4hi3, WRORHI)
11603 IWMMXT_BUILTIN2 (rorv2si3_di, WRORW)
11604 IWMMXT_BUILTIN2 (rorv2si3, WRORWI)
11605 IWMMXT_BUILTIN2 (rordi3_di, WRORD)
11606 IWMMXT_BUILTIN2 (rordi3, WRORDI)
11607 IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
11608 IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
11611 static const struct builtin_description bdesc_1arg[] =
11613 IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11614 IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11615 IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11616 IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11617 IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11618 IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11619 IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11620 IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11621 IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11622 IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11623 IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11624 IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11625 IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11626 IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11627 IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11628 IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11629 IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11630 IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11633 /* Set up all the iWMMXt builtins. This is
11634 not called if TARGET_IWMMXT is zero. */
11636 static void
11637 arm_init_iwmmxt_builtins (void)
11639 const struct builtin_description * d;
11640 size_t i;
11641 tree endlink = void_list_node;
11643 tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11644 tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11645 tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11647 tree int_ftype_int
11648 = build_function_type (integer_type_node,
11649 tree_cons (NULL_TREE, integer_type_node, endlink));
11650 tree v8qi_ftype_v8qi_v8qi_int
11651 = build_function_type (V8QI_type_node,
11652 tree_cons (NULL_TREE, V8QI_type_node,
11653 tree_cons (NULL_TREE, V8QI_type_node,
11654 tree_cons (NULL_TREE,
11655 integer_type_node,
11656 endlink))));
11657 tree v4hi_ftype_v4hi_int
11658 = build_function_type (V4HI_type_node,
11659 tree_cons (NULL_TREE, V4HI_type_node,
11660 tree_cons (NULL_TREE, integer_type_node,
11661 endlink)));
11662 tree v2si_ftype_v2si_int
11663 = build_function_type (V2SI_type_node,
11664 tree_cons (NULL_TREE, V2SI_type_node,
11665 tree_cons (NULL_TREE, integer_type_node,
11666 endlink)));
11667 tree v2si_ftype_di_di
11668 = build_function_type (V2SI_type_node,
11669 tree_cons (NULL_TREE, long_long_integer_type_node,
11670 tree_cons (NULL_TREE, long_long_integer_type_node,
11671 endlink)));
11672 tree di_ftype_di_int
11673 = build_function_type (long_long_integer_type_node,
11674 tree_cons (NULL_TREE, long_long_integer_type_node,
11675 tree_cons (NULL_TREE, integer_type_node,
11676 endlink)));
11677 tree di_ftype_di_int_int
11678 = build_function_type (long_long_integer_type_node,
11679 tree_cons (NULL_TREE, long_long_integer_type_node,
11680 tree_cons (NULL_TREE, integer_type_node,
11681 tree_cons (NULL_TREE,
11682 integer_type_node,
11683 endlink))));
11684 tree int_ftype_v8qi
11685 = build_function_type (integer_type_node,
11686 tree_cons (NULL_TREE, V8QI_type_node,
11687 endlink));
11688 tree int_ftype_v4hi
11689 = build_function_type (integer_type_node,
11690 tree_cons (NULL_TREE, V4HI_type_node,
11691 endlink));
11692 tree int_ftype_v2si
11693 = build_function_type (integer_type_node,
11694 tree_cons (NULL_TREE, V2SI_type_node,
11695 endlink));
11696 tree int_ftype_v8qi_int
11697 = build_function_type (integer_type_node,
11698 tree_cons (NULL_TREE, V8QI_type_node,
11699 tree_cons (NULL_TREE, integer_type_node,
11700 endlink)));
11701 tree int_ftype_v4hi_int
11702 = build_function_type (integer_type_node,
11703 tree_cons (NULL_TREE, V4HI_type_node,
11704 tree_cons (NULL_TREE, integer_type_node,
11705 endlink)));
11706 tree int_ftype_v2si_int
11707 = build_function_type (integer_type_node,
11708 tree_cons (NULL_TREE, V2SI_type_node,
11709 tree_cons (NULL_TREE, integer_type_node,
11710 endlink)));
11711 tree v8qi_ftype_v8qi_int_int
11712 = build_function_type (V8QI_type_node,
11713 tree_cons (NULL_TREE, V8QI_type_node,
11714 tree_cons (NULL_TREE, integer_type_node,
11715 tree_cons (NULL_TREE,
11716 integer_type_node,
11717 endlink))));
11718 tree v4hi_ftype_v4hi_int_int
11719 = build_function_type (V4HI_type_node,
11720 tree_cons (NULL_TREE, V4HI_type_node,
11721 tree_cons (NULL_TREE, integer_type_node,
11722 tree_cons (NULL_TREE,
11723 integer_type_node,
11724 endlink))));
11725 tree v2si_ftype_v2si_int_int
11726 = build_function_type (V2SI_type_node,
11727 tree_cons (NULL_TREE, V2SI_type_node,
11728 tree_cons (NULL_TREE, integer_type_node,
11729 tree_cons (NULL_TREE,
11730 integer_type_node,
11731 endlink))));
11732 /* Miscellaneous. */
11733 tree v8qi_ftype_v4hi_v4hi
11734 = build_function_type (V8QI_type_node,
11735 tree_cons (NULL_TREE, V4HI_type_node,
11736 tree_cons (NULL_TREE, V4HI_type_node,
11737 endlink)));
11738 tree v4hi_ftype_v2si_v2si
11739 = build_function_type (V4HI_type_node,
11740 tree_cons (NULL_TREE, V2SI_type_node,
11741 tree_cons (NULL_TREE, V2SI_type_node,
11742 endlink)));
11743 tree v2si_ftype_v4hi_v4hi
11744 = build_function_type (V2SI_type_node,
11745 tree_cons (NULL_TREE, V4HI_type_node,
11746 tree_cons (NULL_TREE, V4HI_type_node,
11747 endlink)));
11748 tree v2si_ftype_v8qi_v8qi
11749 = build_function_type (V2SI_type_node,
11750 tree_cons (NULL_TREE, V8QI_type_node,
11751 tree_cons (NULL_TREE, V8QI_type_node,
11752 endlink)));
11753 tree v4hi_ftype_v4hi_di
11754 = build_function_type (V4HI_type_node,
11755 tree_cons (NULL_TREE, V4HI_type_node,
11756 tree_cons (NULL_TREE,
11757 long_long_integer_type_node,
11758 endlink)));
11759 tree v2si_ftype_v2si_di
11760 = build_function_type (V2SI_type_node,
11761 tree_cons (NULL_TREE, V2SI_type_node,
11762 tree_cons (NULL_TREE,
11763 long_long_integer_type_node,
11764 endlink)));
11765 tree void_ftype_int_int
11766 = build_function_type (void_type_node,
11767 tree_cons (NULL_TREE, integer_type_node,
11768 tree_cons (NULL_TREE, integer_type_node,
11769 endlink)));
11770 tree di_ftype_void
11771 = build_function_type (long_long_unsigned_type_node, endlink);
11772 tree di_ftype_v8qi
11773 = build_function_type (long_long_integer_type_node,
11774 tree_cons (NULL_TREE, V8QI_type_node,
11775 endlink));
11776 tree di_ftype_v4hi
11777 = build_function_type (long_long_integer_type_node,
11778 tree_cons (NULL_TREE, V4HI_type_node,
11779 endlink));
11780 tree di_ftype_v2si
11781 = build_function_type (long_long_integer_type_node,
11782 tree_cons (NULL_TREE, V2SI_type_node,
11783 endlink));
11784 tree v2si_ftype_v4hi
11785 = build_function_type (V2SI_type_node,
11786 tree_cons (NULL_TREE, V4HI_type_node,
11787 endlink));
11788 tree v4hi_ftype_v8qi
11789 = build_function_type (V4HI_type_node,
11790 tree_cons (NULL_TREE, V8QI_type_node,
11791 endlink));
11793 tree di_ftype_di_v4hi_v4hi
11794 = build_function_type (long_long_unsigned_type_node,
11795 tree_cons (NULL_TREE,
11796 long_long_unsigned_type_node,
11797 tree_cons (NULL_TREE, V4HI_type_node,
11798 tree_cons (NULL_TREE,
11799 V4HI_type_node,
11800 endlink))));
11802 tree di_ftype_v4hi_v4hi
11803 = build_function_type (long_long_unsigned_type_node,
11804 tree_cons (NULL_TREE, V4HI_type_node,
11805 tree_cons (NULL_TREE, V4HI_type_node,
11806 endlink)));
11808 /* Normal vector binops. */
11809 tree v8qi_ftype_v8qi_v8qi
11810 = build_function_type (V8QI_type_node,
11811 tree_cons (NULL_TREE, V8QI_type_node,
11812 tree_cons (NULL_TREE, V8QI_type_node,
11813 endlink)));
11814 tree v4hi_ftype_v4hi_v4hi
11815 = build_function_type (V4HI_type_node,
11816 tree_cons (NULL_TREE, V4HI_type_node,
11817 tree_cons (NULL_TREE, V4HI_type_node,
11818 endlink)));
11819 tree v2si_ftype_v2si_v2si
11820 = build_function_type (V2SI_type_node,
11821 tree_cons (NULL_TREE, V2SI_type_node,
11822 tree_cons (NULL_TREE, V2SI_type_node,
11823 endlink)));
11824 tree di_ftype_di_di
11825 = build_function_type (long_long_unsigned_type_node,
11826 tree_cons (NULL_TREE, long_long_unsigned_type_node,
11827 tree_cons (NULL_TREE,
11828 long_long_unsigned_type_node,
11829 endlink)));
11831 /* Add all builtins that are more or less simple operations on two
11832 operands. */
11833 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11835 /* Use one of the operands; the target can have a different mode for
11836 mask-generating compares. */
11837 enum machine_mode mode;
11838 tree type;
11840 if (d->name == 0)
11841 continue;
11843 mode = insn_data[d->icode].operand[1].mode;
11845 switch (mode)
11847 case V8QImode:
11848 type = v8qi_ftype_v8qi_v8qi;
11849 break;
11850 case V4HImode:
11851 type = v4hi_ftype_v4hi_v4hi;
11852 break;
11853 case V2SImode:
11854 type = v2si_ftype_v2si_v2si;
11855 break;
11856 case DImode:
11857 type = di_ftype_di_di;
11858 break;
11860 default:
11861 gcc_unreachable ();
11864 def_mbuiltin (d->mask, d->name, type, d->code);
11867 /* Add the remaining MMX insns with somewhat more complicated types. */
11868 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11869 def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11870 def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11872 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11873 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11874 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11875 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11876 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11877 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11879 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11880 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11881 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11882 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11883 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11884 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11886 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11887 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11888 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11889 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11890 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11891 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11893 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11894 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11895 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11896 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11897 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11898 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11900 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11902 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11903 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11904 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11905 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11907 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11908 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11909 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11910 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11911 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11912 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11913 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11914 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11915 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11917 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11918 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11919 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11921 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11922 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11923 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
11925 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
11926 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
11927 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
11928 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
11929 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
11930 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
11932 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
11933 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
11934 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
11935 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
11936 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
11937 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
11938 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
11939 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
11940 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
11941 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
11942 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
11943 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
11945 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
11946 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
11947 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
11948 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
11950 def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
11951 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
11952 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
11953 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
11954 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
11955 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
11956 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
11959 static void
11960 arm_init_builtins (void)
11962 if (TARGET_REALLY_IWMMXT)
11963 arm_init_iwmmxt_builtins ();
11966 /* Errors in the source file can cause expand_expr to return const0_rtx
11967 where we expect a vector. To avoid crashing, use one of the vector
11968 clear instructions. */
11970 static rtx
11971 safe_vector_operand (rtx x, enum machine_mode mode)
11973 if (x != const0_rtx)
11974 return x;
11975 x = gen_reg_rtx (mode);
11977 emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
11978 : gen_rtx_SUBREG (DImode, x, 0)));
11979 return x;
11982 /* Subroutine of arm_expand_builtin to take care of binop insns. */
11984 static rtx
11985 arm_expand_binop_builtin (enum insn_code icode,
11986 tree arglist, rtx target)
11988 rtx pat;
11989 tree arg0 = TREE_VALUE (arglist);
11990 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11991 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11992 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11993 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11994 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11995 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11997 if (VECTOR_MODE_P (mode0))
11998 op0 = safe_vector_operand (op0, mode0);
11999 if (VECTOR_MODE_P (mode1))
12000 op1 = safe_vector_operand (op1, mode1);
12002 if (! target
12003 || GET_MODE (target) != tmode
12004 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12005 target = gen_reg_rtx (tmode);
12007 gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
12009 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12010 op0 = copy_to_mode_reg (mode0, op0);
12011 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12012 op1 = copy_to_mode_reg (mode1, op1);
12014 pat = GEN_FCN (icode) (target, op0, op1);
12015 if (! pat)
12016 return 0;
12017 emit_insn (pat);
12018 return target;
12021 /* Subroutine of arm_expand_builtin to take care of unop insns. */
12023 static rtx
12024 arm_expand_unop_builtin (enum insn_code icode,
12025 tree arglist, rtx target, int do_load)
12027 rtx pat;
12028 tree arg0 = TREE_VALUE (arglist);
12029 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12030 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12031 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12033 if (! target
12034 || GET_MODE (target) != tmode
12035 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12036 target = gen_reg_rtx (tmode);
12037 if (do_load)
12038 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12039 else
12041 if (VECTOR_MODE_P (mode0))
12042 op0 = safe_vector_operand (op0, mode0);
12044 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12045 op0 = copy_to_mode_reg (mode0, op0);
12048 pat = GEN_FCN (icode) (target, op0);
12049 if (! pat)
12050 return 0;
12051 emit_insn (pat);
12052 return target;
12055 /* Expand an expression EXP that calls a built-in function,
12056 with result going to TARGET if that's convenient
12057 (and in mode MODE if that's convenient).
12058 SUBTARGET may be used as the target for computing one of EXP's operands.
12059 IGNORE is nonzero if the value is to be ignored. */
12061 static rtx
12062 arm_expand_builtin (tree exp,
12063 rtx target,
12064 rtx subtarget ATTRIBUTE_UNUSED,
12065 enum machine_mode mode ATTRIBUTE_UNUSED,
12066 int ignore ATTRIBUTE_UNUSED)
12068 const struct builtin_description * d;
12069 enum insn_code icode;
12070 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12071 tree arglist = TREE_OPERAND (exp, 1);
12072 tree arg0;
12073 tree arg1;
12074 tree arg2;
12075 rtx op0;
12076 rtx op1;
12077 rtx op2;
12078 rtx pat;
12079 int fcode = DECL_FUNCTION_CODE (fndecl);
12080 size_t i;
12081 enum machine_mode tmode;
12082 enum machine_mode mode0;
12083 enum machine_mode mode1;
12084 enum machine_mode mode2;
12086 switch (fcode)
12088 case ARM_BUILTIN_TEXTRMSB:
12089 case ARM_BUILTIN_TEXTRMUB:
12090 case ARM_BUILTIN_TEXTRMSH:
12091 case ARM_BUILTIN_TEXTRMUH:
12092 case ARM_BUILTIN_TEXTRMSW:
12093 case ARM_BUILTIN_TEXTRMUW:
12094 icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12095 : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12096 : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12097 : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12098 : CODE_FOR_iwmmxt_textrmw);
12100 arg0 = TREE_VALUE (arglist);
12101 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12102 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12103 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12104 tmode = insn_data[icode].operand[0].mode;
12105 mode0 = insn_data[icode].operand[1].mode;
12106 mode1 = insn_data[icode].operand[2].mode;
12108 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12109 op0 = copy_to_mode_reg (mode0, op0);
12110 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12112 /* @@@ better error message */
12113 error ("selector must be an immediate");
12114 return gen_reg_rtx (tmode);
12116 if (target == 0
12117 || GET_MODE (target) != tmode
12118 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12119 target = gen_reg_rtx (tmode);
12120 pat = GEN_FCN (icode) (target, op0, op1);
12121 if (! pat)
12122 return 0;
12123 emit_insn (pat);
12124 return target;
12126 case ARM_BUILTIN_TINSRB:
12127 case ARM_BUILTIN_TINSRH:
12128 case ARM_BUILTIN_TINSRW:
12129 icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12130 : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12131 : CODE_FOR_iwmmxt_tinsrw);
12132 arg0 = TREE_VALUE (arglist);
12133 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12134 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12135 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12136 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12137 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12138 tmode = insn_data[icode].operand[0].mode;
12139 mode0 = insn_data[icode].operand[1].mode;
12140 mode1 = insn_data[icode].operand[2].mode;
12141 mode2 = insn_data[icode].operand[3].mode;
12143 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12144 op0 = copy_to_mode_reg (mode0, op0);
12145 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12146 op1 = copy_to_mode_reg (mode1, op1);
12147 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12149 /* @@@ better error message */
12150 error ("selector must be an immediate");
12151 return const0_rtx;
12153 if (target == 0
12154 || GET_MODE (target) != tmode
12155 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12156 target = gen_reg_rtx (tmode);
12157 pat = GEN_FCN (icode) (target, op0, op1, op2);
12158 if (! pat)
12159 return 0;
12160 emit_insn (pat);
12161 return target;
12163 case ARM_BUILTIN_SETWCX:
12164 arg0 = TREE_VALUE (arglist);
12165 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12166 op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12167 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12168 emit_insn (gen_iwmmxt_tmcr (op1, op0));
12169 return 0;
12171 case ARM_BUILTIN_GETWCX:
12172 arg0 = TREE_VALUE (arglist);
12173 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12174 target = gen_reg_rtx (SImode);
12175 emit_insn (gen_iwmmxt_tmrc (target, op0));
12176 return target;
12178 case ARM_BUILTIN_WSHUFH:
12179 icode = CODE_FOR_iwmmxt_wshufh;
12180 arg0 = TREE_VALUE (arglist);
12181 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12182 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12183 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12184 tmode = insn_data[icode].operand[0].mode;
12185 mode1 = insn_data[icode].operand[1].mode;
12186 mode2 = insn_data[icode].operand[2].mode;
12188 if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12189 op0 = copy_to_mode_reg (mode1, op0);
12190 if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12192 /* @@@ better error message */
12193 error ("mask must be an immediate");
12194 return const0_rtx;
12196 if (target == 0
12197 || GET_MODE (target) != tmode
12198 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12199 target = gen_reg_rtx (tmode);
12200 pat = GEN_FCN (icode) (target, op0, op1);
12201 if (! pat)
12202 return 0;
12203 emit_insn (pat);
12204 return target;
12206 case ARM_BUILTIN_WSADB:
12207 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12208 case ARM_BUILTIN_WSADH:
12209 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12210 case ARM_BUILTIN_WSADBZ:
12211 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12212 case ARM_BUILTIN_WSADHZ:
12213 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12215 /* Several three-argument builtins. */
12216 case ARM_BUILTIN_WMACS:
12217 case ARM_BUILTIN_WMACU:
12218 case ARM_BUILTIN_WALIGN:
12219 case ARM_BUILTIN_TMIA:
12220 case ARM_BUILTIN_TMIAPH:
12221 case ARM_BUILTIN_TMIATT:
12222 case ARM_BUILTIN_TMIATB:
12223 case ARM_BUILTIN_TMIABT:
12224 case ARM_BUILTIN_TMIABB:
12225 icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12226 : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12227 : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12228 : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12229 : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12230 : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12231 : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12232 : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12233 : CODE_FOR_iwmmxt_walign);
12234 arg0 = TREE_VALUE (arglist);
12235 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12236 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12237 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12238 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12239 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12240 tmode = insn_data[icode].operand[0].mode;
12241 mode0 = insn_data[icode].operand[1].mode;
12242 mode1 = insn_data[icode].operand[2].mode;
12243 mode2 = insn_data[icode].operand[3].mode;
12245 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12246 op0 = copy_to_mode_reg (mode0, op0);
12247 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12248 op1 = copy_to_mode_reg (mode1, op1);
12249 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12250 op2 = copy_to_mode_reg (mode2, op2);
12251 if (target == 0
12252 || GET_MODE (target) != tmode
12253 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12254 target = gen_reg_rtx (tmode);
12255 pat = GEN_FCN (icode) (target, op0, op1, op2);
12256 if (! pat)
12257 return 0;
12258 emit_insn (pat);
12259 return target;
12261 case ARM_BUILTIN_WZERO:
12262 target = gen_reg_rtx (DImode);
12263 emit_insn (gen_iwmmxt_clrdi (target));
12264 return target;
12266 default:
12267 break;
12270 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12271 if (d->code == (const enum arm_builtins) fcode)
12272 return arm_expand_binop_builtin (d->icode, arglist, target);
12274 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12275 if (d->code == (const enum arm_builtins) fcode)
12276 return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12278 /* @@@ Should really do something sensible here. */
12279 return NULL_RTX;
12282 /* Return the number (counting from 0) of
12283 the least significant set bit in MASK. */
12285 inline static int
12286 number_of_first_bit_set (unsigned mask)
12288 int bit;
12290 for (bit = 0;
12291 (mask & (1 << bit)) == 0;
12292 ++bit)
12293 continue;
12295 return bit;
12298 /* Emit code to push or pop registers to or from the stack. F is the
12299 assembly file. MASK is the registers to push or pop. PUSH is
12300 nonzero if we should push, and zero if we should pop. For debugging
12301 output, if pushing, adjust CFA_OFFSET by the amount of space added
12302 to the stack. REAL_REGS should have the same number of bits set as
12303 MASK, and will be used instead (in the same order) to describe which
12304 registers were saved - this is used to mark the save slots when we
12305 push high registers after moving them to low registers. */
12306 static void
12307 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
12308 unsigned long real_regs)
12310 int regno;
12311 int lo_mask = mask & 0xFF;
12312 int pushed_words = 0;
12314 gcc_assert (mask);
12316 if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12318 /* Special case. Do not generate a POP PC statement here, do it in
12319 thumb_exit() */
12320 thumb_exit (f, -1);
12321 return;
12324 fprintf (f, "\t%s\t{", push ? "push" : "pop");
12326 /* Look at the low registers first. */
12327 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12329 if (lo_mask & 1)
12331 asm_fprintf (f, "%r", regno);
12333 if ((lo_mask & ~1) != 0)
12334 fprintf (f, ", ");
12336 pushed_words++;
12340 if (push && (mask & (1 << LR_REGNUM)))
12342 /* Catch pushing the LR. */
12343 if (mask & 0xFF)
12344 fprintf (f, ", ");
12346 asm_fprintf (f, "%r", LR_REGNUM);
12348 pushed_words++;
12350 else if (!push && (mask & (1 << PC_REGNUM)))
12352 /* Catch popping the PC. */
12353 if (TARGET_INTERWORK || TARGET_BACKTRACE
12354 || current_function_calls_eh_return)
12356 /* The PC is never poped directly, instead
12357 it is popped into r3 and then BX is used. */
12358 fprintf (f, "}\n");
12360 thumb_exit (f, -1);
12362 return;
12364 else
12366 if (mask & 0xFF)
12367 fprintf (f, ", ");
12369 asm_fprintf (f, "%r", PC_REGNUM);
12373 fprintf (f, "}\n");
12375 if (push && pushed_words && dwarf2out_do_frame ())
12377 char *l = dwarf2out_cfi_label ();
12378 int pushed_mask = real_regs;
12380 *cfa_offset += pushed_words * 4;
12381 dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12383 pushed_words = 0;
12384 pushed_mask = real_regs;
12385 for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12387 if (pushed_mask & 1)
12388 dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12393 /* Generate code to return from a thumb function.
12394 If 'reg_containing_return_addr' is -1, then the return address is
12395 actually on the stack, at the stack pointer. */
12396 static void
12397 thumb_exit (FILE *f, int reg_containing_return_addr)
12399 unsigned regs_available_for_popping;
12400 unsigned regs_to_pop;
12401 int pops_needed;
12402 unsigned available;
12403 unsigned required;
12404 int mode;
12405 int size;
12406 int restore_a4 = FALSE;
12408 /* Compute the registers we need to pop. */
12409 regs_to_pop = 0;
12410 pops_needed = 0;
12412 if (reg_containing_return_addr == -1)
12414 regs_to_pop |= 1 << LR_REGNUM;
12415 ++pops_needed;
12418 if (TARGET_BACKTRACE)
12420 /* Restore the (ARM) frame pointer and stack pointer. */
12421 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12422 pops_needed += 2;
12425 /* If there is nothing to pop then just emit the BX instruction and
12426 return. */
12427 if (pops_needed == 0)
12429 if (current_function_calls_eh_return)
12430 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12432 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12433 return;
12435 /* Otherwise if we are not supporting interworking and we have not created
12436 a backtrace structure and the function was not entered in ARM mode then
12437 just pop the return address straight into the PC. */
12438 else if (!TARGET_INTERWORK
12439 && !TARGET_BACKTRACE
12440 && !is_called_in_ARM_mode (current_function_decl)
12441 && !current_function_calls_eh_return)
12443 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12444 return;
12447 /* Find out how many of the (return) argument registers we can corrupt. */
12448 regs_available_for_popping = 0;
12450 /* If returning via __builtin_eh_return, the bottom three registers
12451 all contain information needed for the return. */
12452 if (current_function_calls_eh_return)
12453 size = 12;
12454 else
12456 /* If we can deduce the registers used from the function's
12457 return value. This is more reliable that examining
12458 regs_ever_live[] because that will be set if the register is
12459 ever used in the function, not just if the register is used
12460 to hold a return value. */
12462 if (current_function_return_rtx != 0)
12463 mode = GET_MODE (current_function_return_rtx);
12464 else
12465 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12467 size = GET_MODE_SIZE (mode);
12469 if (size == 0)
12471 /* In a void function we can use any argument register.
12472 In a function that returns a structure on the stack
12473 we can use the second and third argument registers. */
12474 if (mode == VOIDmode)
12475 regs_available_for_popping =
12476 (1 << ARG_REGISTER (1))
12477 | (1 << ARG_REGISTER (2))
12478 | (1 << ARG_REGISTER (3));
12479 else
12480 regs_available_for_popping =
12481 (1 << ARG_REGISTER (2))
12482 | (1 << ARG_REGISTER (3));
12484 else if (size <= 4)
12485 regs_available_for_popping =
12486 (1 << ARG_REGISTER (2))
12487 | (1 << ARG_REGISTER (3));
12488 else if (size <= 8)
12489 regs_available_for_popping =
12490 (1 << ARG_REGISTER (3));
12493 /* Match registers to be popped with registers into which we pop them. */
12494 for (available = regs_available_for_popping,
12495 required = regs_to_pop;
12496 required != 0 && available != 0;
12497 available &= ~(available & - available),
12498 required &= ~(required & - required))
12499 -- pops_needed;
12501 /* If we have any popping registers left over, remove them. */
12502 if (available > 0)
12503 regs_available_for_popping &= ~available;
12505 /* Otherwise if we need another popping register we can use
12506 the fourth argument register. */
12507 else if (pops_needed)
12509 /* If we have not found any free argument registers and
12510 reg a4 contains the return address, we must move it. */
12511 if (regs_available_for_popping == 0
12512 && reg_containing_return_addr == LAST_ARG_REGNUM)
12514 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12515 reg_containing_return_addr = LR_REGNUM;
12517 else if (size > 12)
12519 /* Register a4 is being used to hold part of the return value,
12520 but we have dire need of a free, low register. */
12521 restore_a4 = TRUE;
12523 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12526 if (reg_containing_return_addr != LAST_ARG_REGNUM)
12528 /* The fourth argument register is available. */
12529 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12531 --pops_needed;
12535 /* Pop as many registers as we can. */
12536 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12537 regs_available_for_popping);
12539 /* Process the registers we popped. */
12540 if (reg_containing_return_addr == -1)
12542 /* The return address was popped into the lowest numbered register. */
12543 regs_to_pop &= ~(1 << LR_REGNUM);
12545 reg_containing_return_addr =
12546 number_of_first_bit_set (regs_available_for_popping);
12548 /* Remove this register for the mask of available registers, so that
12549 the return address will not be corrupted by further pops. */
12550 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12553 /* If we popped other registers then handle them here. */
12554 if (regs_available_for_popping)
12556 int frame_pointer;
12558 /* Work out which register currently contains the frame pointer. */
12559 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12561 /* Move it into the correct place. */
12562 asm_fprintf (f, "\tmov\t%r, %r\n",
12563 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12565 /* (Temporarily) remove it from the mask of popped registers. */
12566 regs_available_for_popping &= ~(1 << frame_pointer);
12567 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12569 if (regs_available_for_popping)
12571 int stack_pointer;
12573 /* We popped the stack pointer as well,
12574 find the register that contains it. */
12575 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12577 /* Move it into the stack register. */
12578 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12580 /* At this point we have popped all necessary registers, so
12581 do not worry about restoring regs_available_for_popping
12582 to its correct value:
12584 assert (pops_needed == 0)
12585 assert (regs_available_for_popping == (1 << frame_pointer))
12586 assert (regs_to_pop == (1 << STACK_POINTER)) */
12588 else
12590 /* Since we have just move the popped value into the frame
12591 pointer, the popping register is available for reuse, and
12592 we know that we still have the stack pointer left to pop. */
12593 regs_available_for_popping |= (1 << frame_pointer);
12597 /* If we still have registers left on the stack, but we no longer have
12598 any registers into which we can pop them, then we must move the return
12599 address into the link register and make available the register that
12600 contained it. */
12601 if (regs_available_for_popping == 0 && pops_needed > 0)
12603 regs_available_for_popping |= 1 << reg_containing_return_addr;
12605 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12606 reg_containing_return_addr);
12608 reg_containing_return_addr = LR_REGNUM;
12611 /* If we have registers left on the stack then pop some more.
12612 We know that at most we will want to pop FP and SP. */
12613 if (pops_needed > 0)
12615 int popped_into;
12616 int move_to;
12618 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12619 regs_available_for_popping);
12621 /* We have popped either FP or SP.
12622 Move whichever one it is into the correct register. */
12623 popped_into = number_of_first_bit_set (regs_available_for_popping);
12624 move_to = number_of_first_bit_set (regs_to_pop);
12626 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12628 regs_to_pop &= ~(1 << move_to);
12630 --pops_needed;
12633 /* If we still have not popped everything then we must have only
12634 had one register available to us and we are now popping the SP. */
12635 if (pops_needed > 0)
12637 int popped_into;
12639 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12640 regs_available_for_popping);
12642 popped_into = number_of_first_bit_set (regs_available_for_popping);
12644 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12646 assert (regs_to_pop == (1 << STACK_POINTER))
12647 assert (pops_needed == 1)
12651 /* If necessary restore the a4 register. */
12652 if (restore_a4)
12654 if (reg_containing_return_addr != LR_REGNUM)
12656 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12657 reg_containing_return_addr = LR_REGNUM;
12660 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12663 if (current_function_calls_eh_return)
12664 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12666 /* Return to caller. */
12667 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12671 void
12672 thumb_final_prescan_insn (rtx insn)
12674 if (flag_print_asm_name)
12675 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12676 INSN_ADDRESSES (INSN_UID (insn)));
12680 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12682 unsigned HOST_WIDE_INT mask = 0xff;
12683 int i;
12685 if (val == 0) /* XXX */
12686 return 0;
12688 for (i = 0; i < 25; i++)
12689 if ((val & (mask << i)) == val)
12690 return 1;
12692 return 0;
12695 /* Returns nonzero if the current function contains,
12696 or might contain a far jump. */
12697 static int
12698 thumb_far_jump_used_p (void)
12700 rtx insn;
12702 /* This test is only important for leaf functions. */
12703 /* assert (!leaf_function_p ()); */
12705 /* If we have already decided that far jumps may be used,
12706 do not bother checking again, and always return true even if
12707 it turns out that they are not being used. Once we have made
12708 the decision that far jumps are present (and that hence the link
12709 register will be pushed onto the stack) we cannot go back on it. */
12710 if (cfun->machine->far_jump_used)
12711 return 1;
12713 /* If this function is not being called from the prologue/epilogue
12714 generation code then it must be being called from the
12715 INITIAL_ELIMINATION_OFFSET macro. */
12716 if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12718 /* In this case we know that we are being asked about the elimination
12719 of the arg pointer register. If that register is not being used,
12720 then there are no arguments on the stack, and we do not have to
12721 worry that a far jump might force the prologue to push the link
12722 register, changing the stack offsets. In this case we can just
12723 return false, since the presence of far jumps in the function will
12724 not affect stack offsets.
12726 If the arg pointer is live (or if it was live, but has now been
12727 eliminated and so set to dead) then we do have to test to see if
12728 the function might contain a far jump. This test can lead to some
12729 false negatives, since before reload is completed, then length of
12730 branch instructions is not known, so gcc defaults to returning their
12731 longest length, which in turn sets the far jump attribute to true.
12733 A false negative will not result in bad code being generated, but it
12734 will result in a needless push and pop of the link register. We
12735 hope that this does not occur too often.
12737 If we need doubleword stack alignment this could affect the other
12738 elimination offsets so we can't risk getting it wrong. */
12739 if (regs_ever_live [ARG_POINTER_REGNUM])
12740 cfun->machine->arg_pointer_live = 1;
12741 else if (!cfun->machine->arg_pointer_live)
12742 return 0;
12745 /* Check to see if the function contains a branch
12746 insn with the far jump attribute set. */
12747 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12749 if (GET_CODE (insn) == JUMP_INSN
12750 /* Ignore tablejump patterns. */
12751 && GET_CODE (PATTERN (insn)) != ADDR_VEC
12752 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12753 && get_attr_far_jump (insn) == FAR_JUMP_YES
12756 /* Record the fact that we have decided that
12757 the function does use far jumps. */
12758 cfun->machine->far_jump_used = 1;
12759 return 1;
12763 return 0;
12766 /* Return nonzero if FUNC must be entered in ARM mode. */
12768 is_called_in_ARM_mode (tree func)
12770 gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
12772 /* Ignore the problem about functions whose address is taken. */
12773 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12774 return TRUE;
12776 #ifdef ARM_PE
12777 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12778 #else
12779 return FALSE;
12780 #endif
12783 /* The bits which aren't usefully expanded as rtl. */
12784 const char *
12785 thumb_unexpanded_epilogue (void)
12787 int regno;
12788 unsigned long live_regs_mask = 0;
12789 int high_regs_pushed = 0;
12790 int had_to_push_lr;
12791 int size;
12792 int mode;
12794 if (return_used_this_function)
12795 return "";
12797 if (IS_NAKED (arm_current_func_type ()))
12798 return "";
12800 live_regs_mask = thumb_compute_save_reg_mask ();
12801 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
12803 /* If we can deduce the registers used from the function's return value.
12804 This is more reliable that examining regs_ever_live[] because that
12805 will be set if the register is ever used in the function, not just if
12806 the register is used to hold a return value. */
12808 if (current_function_return_rtx != 0)
12809 mode = GET_MODE (current_function_return_rtx);
12810 else
12811 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12813 size = GET_MODE_SIZE (mode);
12815 /* The prolog may have pushed some high registers to use as
12816 work registers. e.g. the testsuite file:
12817 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12818 compiles to produce:
12819 push {r4, r5, r6, r7, lr}
12820 mov r7, r9
12821 mov r6, r8
12822 push {r6, r7}
12823 as part of the prolog. We have to undo that pushing here. */
12825 if (high_regs_pushed)
12827 unsigned long mask = live_regs_mask & 0xff;
12828 int next_hi_reg;
12830 /* The available low registers depend on the size of the value we are
12831 returning. */
12832 if (size <= 12)
12833 mask |= 1 << 3;
12834 if (size <= 8)
12835 mask |= 1 << 2;
12837 if (mask == 0)
12838 /* Oh dear! We have no low registers into which we can pop
12839 high registers! */
12840 internal_error
12841 ("no low registers available for popping high registers");
12843 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12844 if (live_regs_mask & (1 << next_hi_reg))
12845 break;
12847 while (high_regs_pushed)
12849 /* Find lo register(s) into which the high register(s) can
12850 be popped. */
12851 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12853 if (mask & (1 << regno))
12854 high_regs_pushed--;
12855 if (high_regs_pushed == 0)
12856 break;
12859 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
12861 /* Pop the values into the low register(s). */
12862 thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
12864 /* Move the value(s) into the high registers. */
12865 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12867 if (mask & (1 << regno))
12869 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12870 regno);
12872 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
12873 if (live_regs_mask & (1 << next_hi_reg))
12874 break;
12878 live_regs_mask &= ~0x0f00;
12881 had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
12882 live_regs_mask &= 0xff;
12884 if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12886 /* Pop the return address into the PC. */
12887 if (had_to_push_lr)
12888 live_regs_mask |= 1 << PC_REGNUM;
12890 /* Either no argument registers were pushed or a backtrace
12891 structure was created which includes an adjusted stack
12892 pointer, so just pop everything. */
12893 if (live_regs_mask)
12894 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12895 live_regs_mask);
12897 /* We have either just popped the return address into the
12898 PC or it is was kept in LR for the entire function. */
12899 if (!had_to_push_lr)
12900 thumb_exit (asm_out_file, LR_REGNUM);
12902 else
12904 /* Pop everything but the return address. */
12905 if (live_regs_mask)
12906 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12907 live_regs_mask);
12909 if (had_to_push_lr)
12911 if (size > 12)
12913 /* We have no free low regs, so save one. */
12914 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
12915 LAST_ARG_REGNUM);
12918 /* Get the return address into a temporary register. */
12919 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
12920 1 << LAST_ARG_REGNUM);
12922 if (size > 12)
12924 /* Move the return address to lr. */
12925 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
12926 LAST_ARG_REGNUM);
12927 /* Restore the low register. */
12928 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
12929 IP_REGNUM);
12930 regno = LR_REGNUM;
12932 else
12933 regno = LAST_ARG_REGNUM;
12935 else
12936 regno = LR_REGNUM;
12938 /* Remove the argument registers that were pushed onto the stack. */
12939 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
12940 SP_REGNUM, SP_REGNUM,
12941 current_function_pretend_args_size);
12943 thumb_exit (asm_out_file, regno);
12946 return "";
12949 /* Functions to save and restore machine-specific function data. */
12950 static struct machine_function *
12951 arm_init_machine_status (void)
12953 struct machine_function *machine;
12954 machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
12956 #if ARM_FT_UNKNOWN != 0
12957 machine->func_type = ARM_FT_UNKNOWN;
12958 #endif
12959 return machine;
12962 /* Return an RTX indicating where the return address to the
12963 calling function can be found. */
12965 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
12967 if (count != 0)
12968 return NULL_RTX;
12970 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
12973 /* Do anything needed before RTL is emitted for each function. */
12974 void
12975 arm_init_expanders (void)
12977 /* Arrange to initialize and mark the machine per-function status. */
12978 init_machine_status = arm_init_machine_status;
12980 /* This is to stop the combine pass optimizing away the alignment
12981 adjustment of va_arg. */
12982 /* ??? It is claimed that this should not be necessary. */
12983 if (cfun)
12984 mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
12988 /* Like arm_compute_initial_elimination offset. Simpler because
12989 THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer. */
12991 HOST_WIDE_INT
12992 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
12994 arm_stack_offsets *offsets;
12996 offsets = arm_get_frame_offsets ();
12998 switch (from)
13000 case ARG_POINTER_REGNUM:
13001 switch (to)
13003 case STACK_POINTER_REGNUM:
13004 return offsets->outgoing_args - offsets->saved_args;
13006 case FRAME_POINTER_REGNUM:
13007 return offsets->soft_frame - offsets->saved_args;
13009 case THUMB_HARD_FRAME_POINTER_REGNUM:
13010 case ARM_HARD_FRAME_POINTER_REGNUM:
13011 return offsets->saved_regs - offsets->saved_args;
13013 default:
13014 gcc_unreachable ();
13016 break;
13018 case FRAME_POINTER_REGNUM:
13019 switch (to)
13021 case STACK_POINTER_REGNUM:
13022 return offsets->outgoing_args - offsets->soft_frame;
13024 case THUMB_HARD_FRAME_POINTER_REGNUM:
13025 case ARM_HARD_FRAME_POINTER_REGNUM:
13026 return offsets->saved_regs - offsets->soft_frame;
13028 default:
13029 gcc_unreachable ();
13031 break;
13033 default:
13034 gcc_unreachable ();
13039 /* Generate the rest of a function's prologue. */
13040 void
13041 thumb_expand_prologue (void)
13043 rtx insn, dwarf;
13045 HOST_WIDE_INT amount;
13046 arm_stack_offsets *offsets;
13047 unsigned long func_type;
13048 int regno;
13049 unsigned long live_regs_mask;
13051 func_type = arm_current_func_type ();
13053 /* Naked functions don't have prologues. */
13054 if (IS_NAKED (func_type))
13055 return;
13057 if (IS_INTERRUPT (func_type))
13059 error ("interrupt Service Routines cannot be coded in Thumb mode");
13060 return;
13063 live_regs_mask = thumb_compute_save_reg_mask ();
13064 /* Load the pic register before setting the frame pointer,
13065 so we can use r7 as a temporary work register. */
13066 if (flag_pic)
13067 arm_load_pic_register (thumb_find_work_register (live_regs_mask));
13069 offsets = arm_get_frame_offsets ();
13071 if (frame_pointer_needed)
13073 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
13074 stack_pointer_rtx));
13075 RTX_FRAME_RELATED_P (insn) = 1;
13077 else if (CALLER_INTERWORKING_SLOT_SIZE > 0)
13078 emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
13079 stack_pointer_rtx);
13081 amount = offsets->outgoing_args - offsets->saved_regs;
13082 if (amount)
13084 if (amount < 512)
13086 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13087 GEN_INT (- amount)));
13088 RTX_FRAME_RELATED_P (insn) = 1;
13090 else
13092 rtx reg;
13094 /* The stack decrement is too big for an immediate value in a single
13095 insn. In theory we could issue multiple subtracts, but after
13096 three of them it becomes more space efficient to place the full
13097 value in the constant pool and load into a register. (Also the
13098 ARM debugger really likes to see only one stack decrement per
13099 function). So instead we look for a scratch register into which
13100 we can load the decrement, and then we subtract this from the
13101 stack pointer. Unfortunately on the thumb the only available
13102 scratch registers are the argument registers, and we cannot use
13103 these as they may hold arguments to the function. Instead we
13104 attempt to locate a call preserved register which is used by this
13105 function. If we can find one, then we know that it will have
13106 been pushed at the start of the prologue and so we can corrupt
13107 it now. */
13108 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13109 if (live_regs_mask & (1 << regno)
13110 && !(frame_pointer_needed
13111 && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13112 break;
13114 if (regno > LAST_LO_REGNUM) /* Very unlikely. */
13116 rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13118 /* Choose an arbitrary, non-argument low register. */
13119 reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13121 /* Save it by copying it into a high, scratch register. */
13122 emit_insn (gen_movsi (spare, reg));
13123 /* Add a USE to stop propagate_one_insn() from barfing. */
13124 emit_insn (gen_prologue_use (spare));
13126 /* Decrement the stack. */
13127 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13128 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13129 stack_pointer_rtx, reg));
13130 RTX_FRAME_RELATED_P (insn) = 1;
13131 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13132 plus_constant (stack_pointer_rtx,
13133 -amount));
13134 RTX_FRAME_RELATED_P (dwarf) = 1;
13135 REG_NOTES (insn)
13136 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13137 REG_NOTES (insn));
13139 /* Restore the low register's original value. */
13140 emit_insn (gen_movsi (reg, spare));
13142 /* Emit a USE of the restored scratch register, so that flow
13143 analysis will not consider the restore redundant. The
13144 register won't be used again in this function and isn't
13145 restored by the epilogue. */
13146 emit_insn (gen_prologue_use (reg));
13148 else
13150 reg = gen_rtx_REG (SImode, regno);
13152 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13154 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13155 stack_pointer_rtx, reg));
13156 RTX_FRAME_RELATED_P (insn) = 1;
13157 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13158 plus_constant (stack_pointer_rtx,
13159 -amount));
13160 RTX_FRAME_RELATED_P (dwarf) = 1;
13161 REG_NOTES (insn)
13162 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13163 REG_NOTES (insn));
13166 /* If the frame pointer is needed, emit a special barrier that
13167 will prevent the scheduler from moving stores to the frame
13168 before the stack adjustment. */
13169 if (frame_pointer_needed)
13170 emit_insn (gen_stack_tie (stack_pointer_rtx,
13171 hard_frame_pointer_rtx));
13174 if (current_function_profile || TARGET_NO_SCHED_PRO)
13175 emit_insn (gen_blockage ());
13177 cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13178 if (live_regs_mask & 0xff)
13179 cfun->machine->lr_save_eliminated = 0;
13181 /* If the link register is being kept alive, with the return address in it,
13182 then make sure that it does not get reused by the ce2 pass. */
13183 if (cfun->machine->lr_save_eliminated)
13184 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13188 void
13189 thumb_expand_epilogue (void)
13191 HOST_WIDE_INT amount;
13192 arm_stack_offsets *offsets;
13193 int regno;
13195 /* Naked functions don't have prologues. */
13196 if (IS_NAKED (arm_current_func_type ()))
13197 return;
13199 offsets = arm_get_frame_offsets ();
13200 amount = offsets->outgoing_args - offsets->saved_regs;
13202 if (frame_pointer_needed)
13203 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13204 else if (amount)
13206 if (amount < 512)
13207 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13208 GEN_INT (amount)));
13209 else
13211 /* r3 is always free in the epilogue. */
13212 rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13214 emit_insn (gen_movsi (reg, GEN_INT (amount)));
13215 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13219 /* Emit a USE (stack_pointer_rtx), so that
13220 the stack adjustment will not be deleted. */
13221 emit_insn (gen_prologue_use (stack_pointer_rtx));
13223 if (current_function_profile || TARGET_NO_SCHED_PRO)
13224 emit_insn (gen_blockage ());
13226 /* Emit a clobber for each insn that will be restored in the epilogue,
13227 so that flow2 will get register lifetimes correct. */
13228 for (regno = 0; regno < 13; regno++)
13229 if (regs_ever_live[regno] && !call_used_regs[regno])
13230 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13232 if (! regs_ever_live[LR_REGNUM])
13233 emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13236 static void
13237 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13239 unsigned long live_regs_mask = 0;
13240 unsigned long l_mask;
13241 unsigned high_regs_pushed = 0;
13242 int cfa_offset = 0;
13243 int regno;
13245 if (IS_NAKED (arm_current_func_type ()))
13246 return;
13248 if (is_called_in_ARM_mode (current_function_decl))
13250 const char * name;
13252 gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
13253 gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
13254 == SYMBOL_REF);
13255 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
13257 /* Generate code sequence to switch us into Thumb mode. */
13258 /* The .code 32 directive has already been emitted by
13259 ASM_DECLARE_FUNCTION_NAME. */
13260 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13261 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13263 /* Generate a label, so that the debugger will notice the
13264 change in instruction sets. This label is also used by
13265 the assembler to bypass the ARM code when this function
13266 is called from a Thumb encoded function elsewhere in the
13267 same file. Hence the definition of STUB_NAME here must
13268 agree with the definition in gas/config/tc-arm.c. */
13270 #define STUB_NAME ".real_start_of"
13272 fprintf (f, "\t.code\t16\n");
13273 #ifdef ARM_PE
13274 if (arm_dllexport_name_p (name))
13275 name = arm_strip_name_encoding (name);
13276 #endif
13277 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13278 fprintf (f, "\t.thumb_func\n");
13279 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13282 if (current_function_pretend_args_size)
13284 if (cfun->machine->uses_anonymous_args)
13286 int num_pushes;
13288 fprintf (f, "\tpush\t{");
13290 num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13292 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13293 regno <= LAST_ARG_REGNUM;
13294 regno++)
13295 asm_fprintf (f, "%r%s", regno,
13296 regno == LAST_ARG_REGNUM ? "" : ", ");
13298 fprintf (f, "}\n");
13300 else
13301 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13302 SP_REGNUM, SP_REGNUM,
13303 current_function_pretend_args_size);
13305 /* We don't need to record the stores for unwinding (would it
13306 help the debugger any if we did?), but record the change in
13307 the stack pointer. */
13308 if (dwarf2out_do_frame ())
13310 char *l = dwarf2out_cfi_label ();
13312 cfa_offset = cfa_offset + current_function_pretend_args_size;
13313 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13317 /* Get the registers we are going to push. */
13318 live_regs_mask = thumb_compute_save_reg_mask ();
13319 /* Extract a mask of the ones we can give to the Thumb's push instruction. */
13320 l_mask = live_regs_mask & 0x40ff;
13321 /* Then count how many other high registers will need to be pushed. */
13322 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13324 if (TARGET_BACKTRACE)
13326 unsigned offset;
13327 unsigned work_register;
13329 /* We have been asked to create a stack backtrace structure.
13330 The code looks like this:
13332 0 .align 2
13333 0 func:
13334 0 sub SP, #16 Reserve space for 4 registers.
13335 2 push {R7} Push low registers.
13336 4 add R7, SP, #20 Get the stack pointer before the push.
13337 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
13338 8 mov R7, PC Get hold of the start of this code plus 12.
13339 10 str R7, [SP, #16] Store it.
13340 12 mov R7, FP Get hold of the current frame pointer.
13341 14 str R7, [SP, #4] Store it.
13342 16 mov R7, LR Get hold of the current return address.
13343 18 str R7, [SP, #12] Store it.
13344 20 add R7, SP, #16 Point at the start of the backtrace structure.
13345 22 mov FP, R7 Put this value into the frame pointer. */
13347 work_register = thumb_find_work_register (live_regs_mask);
13349 asm_fprintf
13350 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13351 SP_REGNUM, SP_REGNUM);
13353 if (dwarf2out_do_frame ())
13355 char *l = dwarf2out_cfi_label ();
13357 cfa_offset = cfa_offset + 16;
13358 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13361 if (l_mask)
13363 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13364 offset = bit_count (l_mask);
13366 else
13367 offset = 0;
13369 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13370 offset + 16 + current_function_pretend_args_size);
13372 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13373 offset + 4);
13375 /* Make sure that the instruction fetching the PC is in the right place
13376 to calculate "start of backtrace creation code + 12". */
13377 if (l_mask)
13379 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13380 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13381 offset + 12);
13382 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13383 ARM_HARD_FRAME_POINTER_REGNUM);
13384 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13385 offset);
13387 else
13389 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13390 ARM_HARD_FRAME_POINTER_REGNUM);
13391 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13392 offset);
13393 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13394 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13395 offset + 12);
13398 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13399 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13400 offset + 8);
13401 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13402 offset + 12);
13403 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13404 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13406 /* Optimisation: If we are not pushing any low registers but we are going
13407 to push some high registers then delay our first push. This will just
13408 be a push of LR and we can combine it with the push of the first high
13409 register. */
13410 else if ((l_mask & 0xff) != 0
13411 || (high_regs_pushed == 0 && l_mask))
13412 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13414 if (high_regs_pushed)
13416 unsigned pushable_regs;
13417 unsigned next_hi_reg;
13419 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13420 if (live_regs_mask & (1 << next_hi_reg))
13421 break;
13423 pushable_regs = l_mask & 0xff;
13425 if (pushable_regs == 0)
13426 pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
13428 while (high_regs_pushed > 0)
13430 unsigned long real_regs_mask = 0;
13432 for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
13434 if (pushable_regs & (1 << regno))
13436 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13438 high_regs_pushed --;
13439 real_regs_mask |= (1 << next_hi_reg);
13441 if (high_regs_pushed)
13443 for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
13444 next_hi_reg --)
13445 if (live_regs_mask & (1 << next_hi_reg))
13446 break;
13448 else
13450 pushable_regs &= ~((1 << regno) - 1);
13451 break;
13456 /* If we had to find a work register and we have not yet
13457 saved the LR then add it to the list of regs to push. */
13458 if (l_mask == (1 << LR_REGNUM))
13460 thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
13461 1, &cfa_offset,
13462 real_regs_mask | (1 << LR_REGNUM));
13463 l_mask = 0;
13465 else
13466 thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
13471 /* Handle the case of a double word load into a low register from
13472 a computed memory address. The computed address may involve a
13473 register which is overwritten by the load. */
13474 const char *
13475 thumb_load_double_from_address (rtx *operands)
13477 rtx addr;
13478 rtx base;
13479 rtx offset;
13480 rtx arg1;
13481 rtx arg2;
13483 gcc_assert (GET_CODE (operands[0]) == REG);
13484 gcc_assert (GET_CODE (operands[1]) == MEM);
13486 /* Get the memory address. */
13487 addr = XEXP (operands[1], 0);
13489 /* Work out how the memory address is computed. */
13490 switch (GET_CODE (addr))
13492 case REG:
13493 operands[2] = gen_rtx_MEM (SImode,
13494 plus_constant (XEXP (operands[1], 0), 4));
13496 if (REGNO (operands[0]) == REGNO (addr))
13498 output_asm_insn ("ldr\t%H0, %2", operands);
13499 output_asm_insn ("ldr\t%0, %1", operands);
13501 else
13503 output_asm_insn ("ldr\t%0, %1", operands);
13504 output_asm_insn ("ldr\t%H0, %2", operands);
13506 break;
13508 case CONST:
13509 /* Compute <address> + 4 for the high order load. */
13510 operands[2] = gen_rtx_MEM (SImode,
13511 plus_constant (XEXP (operands[1], 0), 4));
13513 output_asm_insn ("ldr\t%0, %1", operands);
13514 output_asm_insn ("ldr\t%H0, %2", operands);
13515 break;
13517 case PLUS:
13518 arg1 = XEXP (addr, 0);
13519 arg2 = XEXP (addr, 1);
13521 if (CONSTANT_P (arg1))
13522 base = arg2, offset = arg1;
13523 else
13524 base = arg1, offset = arg2;
13526 gcc_assert (GET_CODE (base) == REG);
13528 /* Catch the case of <address> = <reg> + <reg> */
13529 if (GET_CODE (offset) == REG)
13531 int reg_offset = REGNO (offset);
13532 int reg_base = REGNO (base);
13533 int reg_dest = REGNO (operands[0]);
13535 /* Add the base and offset registers together into the
13536 higher destination register. */
13537 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13538 reg_dest + 1, reg_base, reg_offset);
13540 /* Load the lower destination register from the address in
13541 the higher destination register. */
13542 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13543 reg_dest, reg_dest + 1);
13545 /* Load the higher destination register from its own address
13546 plus 4. */
13547 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13548 reg_dest + 1, reg_dest + 1);
13550 else
13552 /* Compute <address> + 4 for the high order load. */
13553 operands[2] = gen_rtx_MEM (SImode,
13554 plus_constant (XEXP (operands[1], 0), 4));
13556 /* If the computed address is held in the low order register
13557 then load the high order register first, otherwise always
13558 load the low order register first. */
13559 if (REGNO (operands[0]) == REGNO (base))
13561 output_asm_insn ("ldr\t%H0, %2", operands);
13562 output_asm_insn ("ldr\t%0, %1", operands);
13564 else
13566 output_asm_insn ("ldr\t%0, %1", operands);
13567 output_asm_insn ("ldr\t%H0, %2", operands);
13570 break;
13572 case LABEL_REF:
13573 /* With no registers to worry about we can just load the value
13574 directly. */
13575 operands[2] = gen_rtx_MEM (SImode,
13576 plus_constant (XEXP (operands[1], 0), 4));
13578 output_asm_insn ("ldr\t%H0, %2", operands);
13579 output_asm_insn ("ldr\t%0, %1", operands);
13580 break;
13582 default:
13583 gcc_unreachable ();
13586 return "";
13589 const char *
13590 thumb_output_move_mem_multiple (int n, rtx *operands)
13592 rtx tmp;
13594 switch (n)
13596 case 2:
13597 if (REGNO (operands[4]) > REGNO (operands[5]))
13599 tmp = operands[4];
13600 operands[4] = operands[5];
13601 operands[5] = tmp;
13603 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13604 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13605 break;
13607 case 3:
13608 if (REGNO (operands[4]) > REGNO (operands[5]))
13610 tmp = operands[4];
13611 operands[4] = operands[5];
13612 operands[5] = tmp;
13614 if (REGNO (operands[5]) > REGNO (operands[6]))
13616 tmp = operands[5];
13617 operands[5] = operands[6];
13618 operands[6] = tmp;
13620 if (REGNO (operands[4]) > REGNO (operands[5]))
13622 tmp = operands[4];
13623 operands[4] = operands[5];
13624 operands[5] = tmp;
13627 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13628 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13629 break;
13631 default:
13632 gcc_unreachable ();
13635 return "";
13638 /* Output a call-via instruction for thumb state. */
13639 const char *
13640 thumb_call_via_reg (rtx reg)
13642 int regno = REGNO (reg);
13643 rtx *labelp;
13645 gcc_assert (regno < LR_REGNUM);
13647 /* If we are in the normal text section we can use a single instance
13648 per compilation unit. If we are doing function sections, then we need
13649 an entry per section, since we can't rely on reachability. */
13650 if (in_text_section ())
13652 thumb_call_reg_needed = 1;
13654 if (thumb_call_via_label[regno] == NULL)
13655 thumb_call_via_label[regno] = gen_label_rtx ();
13656 labelp = thumb_call_via_label + regno;
13658 else
13660 if (cfun->machine->call_via[regno] == NULL)
13661 cfun->machine->call_via[regno] = gen_label_rtx ();
13662 labelp = cfun->machine->call_via + regno;
13665 output_asm_insn ("bl\t%a0", labelp);
13666 return "";
13669 /* Routines for generating rtl. */
13670 void
13671 thumb_expand_movmemqi (rtx *operands)
13673 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13674 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13675 HOST_WIDE_INT len = INTVAL (operands[2]);
13676 HOST_WIDE_INT offset = 0;
13678 while (len >= 12)
13680 emit_insn (gen_movmem12b (out, in, out, in));
13681 len -= 12;
13684 if (len >= 8)
13686 emit_insn (gen_movmem8b (out, in, out, in));
13687 len -= 8;
13690 if (len >= 4)
13692 rtx reg = gen_reg_rtx (SImode);
13693 emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13694 emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13695 len -= 4;
13696 offset += 4;
13699 if (len >= 2)
13701 rtx reg = gen_reg_rtx (HImode);
13702 emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13703 plus_constant (in, offset))));
13704 emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13705 reg));
13706 len -= 2;
13707 offset += 2;
13710 if (len)
13712 rtx reg = gen_reg_rtx (QImode);
13713 emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13714 plus_constant (in, offset))));
13715 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13716 reg));
13720 void
13721 thumb_reload_out_hi (rtx *operands)
13723 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13726 /* Handle reading a half-word from memory during reload. */
13727 void
13728 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13730 gcc_unreachable ();
13733 /* Return the length of a function name prefix
13734 that starts with the character 'c'. */
13735 static int
13736 arm_get_strip_length (int c)
13738 switch (c)
13740 ARM_NAME_ENCODING_LENGTHS
13741 default: return 0;
13745 /* Return a pointer to a function's name with any
13746 and all prefix encodings stripped from it. */
13747 const char *
13748 arm_strip_name_encoding (const char *name)
13750 int skip;
13752 while ((skip = arm_get_strip_length (* name)))
13753 name += skip;
13755 return name;
13758 /* If there is a '*' anywhere in the name's prefix, then
13759 emit the stripped name verbatim, otherwise prepend an
13760 underscore if leading underscores are being used. */
13761 void
13762 arm_asm_output_labelref (FILE *stream, const char *name)
13764 int skip;
13765 int verbatim = 0;
13767 while ((skip = arm_get_strip_length (* name)))
13769 verbatim |= (*name == '*');
13770 name += skip;
13773 if (verbatim)
13774 fputs (name, stream);
13775 else
13776 asm_fprintf (stream, "%U%s", name);
13779 static void
13780 arm_file_end (void)
13782 int regno;
13784 if (! thumb_call_reg_needed)
13785 return;
13787 text_section ();
13788 asm_fprintf (asm_out_file, "\t.code 16\n");
13789 ASM_OUTPUT_ALIGN (asm_out_file, 1);
13791 for (regno = 0; regno < LR_REGNUM; regno++)
13793 rtx label = thumb_call_via_label[regno];
13795 if (label != 0)
13797 targetm.asm_out.internal_label (asm_out_file, "L",
13798 CODE_LABEL_NUMBER (label));
13799 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
13804 rtx aof_pic_label;
13806 #ifdef AOF_ASSEMBLER
13807 /* Special functions only needed when producing AOF syntax assembler. */
13809 struct pic_chain
13811 struct pic_chain * next;
13812 const char * symname;
13815 static struct pic_chain * aof_pic_chain = NULL;
13818 aof_pic_entry (rtx x)
13820 struct pic_chain ** chainp;
13821 int offset;
13823 if (aof_pic_label == NULL_RTX)
13825 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13828 for (offset = 0, chainp = &aof_pic_chain; *chainp;
13829 offset += 4, chainp = &(*chainp)->next)
13830 if ((*chainp)->symname == XSTR (x, 0))
13831 return plus_constant (aof_pic_label, offset);
13833 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13834 (*chainp)->next = NULL;
13835 (*chainp)->symname = XSTR (x, 0);
13836 return plus_constant (aof_pic_label, offset);
13839 void
13840 aof_dump_pic_table (FILE *f)
13842 struct pic_chain * chain;
13844 if (aof_pic_chain == NULL)
13845 return;
13847 asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13848 PIC_OFFSET_TABLE_REGNUM,
13849 PIC_OFFSET_TABLE_REGNUM);
13850 fputs ("|x$adcons|\n", f);
13852 for (chain = aof_pic_chain; chain; chain = chain->next)
13854 fputs ("\tDCD\t", f);
13855 assemble_name (f, chain->symname);
13856 fputs ("\n", f);
13860 int arm_text_section_count = 1;
13862 char *
13863 aof_text_section (void )
13865 static char buf[100];
13866 sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13867 arm_text_section_count++);
13868 if (flag_pic)
13869 strcat (buf, ", PIC, REENTRANT");
13870 return buf;
13873 static int arm_data_section_count = 1;
13875 char *
13876 aof_data_section (void)
13878 static char buf[100];
13879 sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13880 return buf;
13883 /* The AOF assembler is religiously strict about declarations of
13884 imported and exported symbols, so that it is impossible to declare
13885 a function as imported near the beginning of the file, and then to
13886 export it later on. It is, however, possible to delay the decision
13887 until all the functions in the file have been compiled. To get
13888 around this, we maintain a list of the imports and exports, and
13889 delete from it any that are subsequently defined. At the end of
13890 compilation we spit the remainder of the list out before the END
13891 directive. */
13893 struct import
13895 struct import * next;
13896 const char * name;
13899 static struct import * imports_list = NULL;
13901 void
13902 aof_add_import (const char *name)
13904 struct import * new;
13906 for (new = imports_list; new; new = new->next)
13907 if (new->name == name)
13908 return;
13910 new = (struct import *) xmalloc (sizeof (struct import));
13911 new->next = imports_list;
13912 imports_list = new;
13913 new->name = name;
13916 void
13917 aof_delete_import (const char *name)
13919 struct import ** old;
13921 for (old = &imports_list; *old; old = & (*old)->next)
13923 if ((*old)->name == name)
13925 *old = (*old)->next;
13926 return;
13931 int arm_main_function = 0;
13933 static void
13934 aof_dump_imports (FILE *f)
13936 /* The AOF assembler needs this to cause the startup code to be extracted
13937 from the library. Brining in __main causes the whole thing to work
13938 automagically. */
13939 if (arm_main_function)
13941 text_section ();
13942 fputs ("\tIMPORT __main\n", f);
13943 fputs ("\tDCD __main\n", f);
13946 /* Now dump the remaining imports. */
13947 while (imports_list)
13949 fprintf (f, "\tIMPORT\t");
13950 assemble_name (f, imports_list->name);
13951 fputc ('\n', f);
13952 imports_list = imports_list->next;
13956 static void
13957 aof_globalize_label (FILE *stream, const char *name)
13959 default_globalize_label (stream, name);
13960 if (! strcmp (name, "main"))
13961 arm_main_function = 1;
13964 static void
13965 aof_file_start (void)
13967 fputs ("__r0\tRN\t0\n", asm_out_file);
13968 fputs ("__a1\tRN\t0\n", asm_out_file);
13969 fputs ("__a2\tRN\t1\n", asm_out_file);
13970 fputs ("__a3\tRN\t2\n", asm_out_file);
13971 fputs ("__a4\tRN\t3\n", asm_out_file);
13972 fputs ("__v1\tRN\t4\n", asm_out_file);
13973 fputs ("__v2\tRN\t5\n", asm_out_file);
13974 fputs ("__v3\tRN\t6\n", asm_out_file);
13975 fputs ("__v4\tRN\t7\n", asm_out_file);
13976 fputs ("__v5\tRN\t8\n", asm_out_file);
13977 fputs ("__v6\tRN\t9\n", asm_out_file);
13978 fputs ("__sl\tRN\t10\n", asm_out_file);
13979 fputs ("__fp\tRN\t11\n", asm_out_file);
13980 fputs ("__ip\tRN\t12\n", asm_out_file);
13981 fputs ("__sp\tRN\t13\n", asm_out_file);
13982 fputs ("__lr\tRN\t14\n", asm_out_file);
13983 fputs ("__pc\tRN\t15\n", asm_out_file);
13984 fputs ("__f0\tFN\t0\n", asm_out_file);
13985 fputs ("__f1\tFN\t1\n", asm_out_file);
13986 fputs ("__f2\tFN\t2\n", asm_out_file);
13987 fputs ("__f3\tFN\t3\n", asm_out_file);
13988 fputs ("__f4\tFN\t4\n", asm_out_file);
13989 fputs ("__f5\tFN\t5\n", asm_out_file);
13990 fputs ("__f6\tFN\t6\n", asm_out_file);
13991 fputs ("__f7\tFN\t7\n", asm_out_file);
13992 text_section ();
13995 static void
13996 aof_file_end (void)
13998 if (flag_pic)
13999 aof_dump_pic_table (asm_out_file);
14000 arm_file_end ();
14001 aof_dump_imports (asm_out_file);
14002 fputs ("\tEND\n", asm_out_file);
14004 #endif /* AOF_ASSEMBLER */
14006 #ifndef ARM_PE
14007 /* Symbols in the text segment can be accessed without indirecting via the
14008 constant pool; it may take an extra binary operation, but this is still
14009 faster than indirecting via memory. Don't do this when not optimizing,
14010 since we won't be calculating al of the offsets necessary to do this
14011 simplification. */
14013 static void
14014 arm_encode_section_info (tree decl, rtx rtl, int first)
14016 /* This doesn't work with AOF syntax, since the string table may be in
14017 a different AREA. */
14018 #ifndef AOF_ASSEMBLER
14019 if (optimize > 0 && TREE_CONSTANT (decl))
14020 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14021 #endif
14023 /* If we are referencing a function that is weak then encode a long call
14024 flag in the function name, otherwise if the function is static or
14025 or known to be defined in this file then encode a short call flag. */
14026 if (first && DECL_P (decl))
14028 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14029 arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14030 else if (! TREE_PUBLIC (decl))
14031 arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14034 #endif /* !ARM_PE */
14036 static void
14037 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14039 if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14040 && !strcmp (prefix, "L"))
14042 arm_ccfsm_state = 0;
14043 arm_target_insn = NULL;
14045 default_internal_label (stream, prefix, labelno);
14048 /* Output code to add DELTA to the first argument, and then jump
14049 to FUNCTION. Used for C++ multiple inheritance. */
14050 static void
14051 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14052 HOST_WIDE_INT delta,
14053 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14054 tree function)
14056 static int thunk_label = 0;
14057 char label[256];
14058 int mi_delta = delta;
14059 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14060 int shift = 0;
14061 int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14062 ? 1 : 0);
14063 if (mi_delta < 0)
14064 mi_delta = - mi_delta;
14065 if (TARGET_THUMB)
14067 int labelno = thunk_label++;
14068 ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14069 fputs ("\tldr\tr12, ", file);
14070 assemble_name (file, label);
14071 fputc ('\n', file);
14073 while (mi_delta != 0)
14075 if ((mi_delta & (3 << shift)) == 0)
14076 shift += 2;
14077 else
14079 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14080 mi_op, this_regno, this_regno,
14081 mi_delta & (0xff << shift));
14082 mi_delta &= ~(0xff << shift);
14083 shift += 8;
14086 if (TARGET_THUMB)
14088 fprintf (file, "\tbx\tr12\n");
14089 ASM_OUTPUT_ALIGN (file, 2);
14090 assemble_name (file, label);
14091 fputs (":\n", file);
14092 assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14094 else
14096 fputs ("\tb\t", file);
14097 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14098 if (NEED_PLT_RELOC)
14099 fputs ("(PLT)", file);
14100 fputc ('\n', file);
14105 arm_emit_vector_const (FILE *file, rtx x)
14107 int i;
14108 const char * pattern;
14110 gcc_assert (GET_CODE (x) == CONST_VECTOR);
14112 switch (GET_MODE (x))
14114 case V2SImode: pattern = "%08x"; break;
14115 case V4HImode: pattern = "%04x"; break;
14116 case V8QImode: pattern = "%02x"; break;
14117 default: gcc_unreachable ();
14120 fprintf (file, "0x");
14121 for (i = CONST_VECTOR_NUNITS (x); i--;)
14123 rtx element;
14125 element = CONST_VECTOR_ELT (x, i);
14126 fprintf (file, pattern, INTVAL (element));
14129 return 1;
14132 const char *
14133 arm_output_load_gr (rtx *operands)
14135 rtx reg;
14136 rtx offset;
14137 rtx wcgr;
14138 rtx sum;
14140 if (GET_CODE (operands [1]) != MEM
14141 || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14142 || GET_CODE (reg = XEXP (sum, 0)) != REG
14143 || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14144 || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14145 return "wldrw%?\t%0, %1";
14147 /* Fix up an out-of-range load of a GR register. */
14148 output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14149 wcgr = operands[0];
14150 operands[0] = reg;
14151 output_asm_insn ("ldr%?\t%0, %1", operands);
14153 operands[0] = wcgr;
14154 operands[1] = reg;
14155 output_asm_insn ("tmcr%?\t%0, %1", operands);
14156 output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14158 return "";
14161 static rtx
14162 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14163 int incoming ATTRIBUTE_UNUSED)
14165 #if 0
14166 /* FIXME: The ARM backend has special code to handle structure
14167 returns, and will reserve its own hidden first argument. So
14168 if this macro is enabled a *second* hidden argument will be
14169 reserved, which will break binary compatibility with old
14170 toolchains and also thunk handling. One day this should be
14171 fixed. */
14172 return 0;
14173 #else
14174 /* Register in which address to store a structure value
14175 is passed to a function. */
14176 return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14177 #endif
14180 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14182 On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14183 named arg and all anonymous args onto the stack.
14184 XXX I know the prologue shouldn't be pushing registers, but it is faster
14185 that way. */
14187 static void
14188 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14189 enum machine_mode mode ATTRIBUTE_UNUSED,
14190 tree type ATTRIBUTE_UNUSED,
14191 int *pretend_size,
14192 int second_time ATTRIBUTE_UNUSED)
14194 cfun->machine->uses_anonymous_args = 1;
14195 if (cum->nregs < NUM_ARG_REGS)
14196 *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14199 /* Return nonzero if the CONSUMER instruction (a store) does not need
14200 PRODUCER's value to calculate the address. */
14203 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14205 rtx value = PATTERN (producer);
14206 rtx addr = PATTERN (consumer);
14208 if (GET_CODE (value) == COND_EXEC)
14209 value = COND_EXEC_CODE (value);
14210 if (GET_CODE (value) == PARALLEL)
14211 value = XVECEXP (value, 0, 0);
14212 value = XEXP (value, 0);
14213 if (GET_CODE (addr) == COND_EXEC)
14214 addr = COND_EXEC_CODE (addr);
14215 if (GET_CODE (addr) == PARALLEL)
14216 addr = XVECEXP (addr, 0, 0);
14217 addr = XEXP (addr, 0);
14219 return !reg_overlap_mentioned_p (value, addr);
14222 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14223 have an early register shift value or amount dependency on the
14224 result of PRODUCER. */
14227 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14229 rtx value = PATTERN (producer);
14230 rtx op = PATTERN (consumer);
14231 rtx early_op;
14233 if (GET_CODE (value) == COND_EXEC)
14234 value = COND_EXEC_CODE (value);
14235 if (GET_CODE (value) == PARALLEL)
14236 value = XVECEXP (value, 0, 0);
14237 value = XEXP (value, 0);
14238 if (GET_CODE (op) == COND_EXEC)
14239 op = COND_EXEC_CODE (op);
14240 if (GET_CODE (op) == PARALLEL)
14241 op = XVECEXP (op, 0, 0);
14242 op = XEXP (op, 1);
14244 early_op = XEXP (op, 0);
14245 /* This is either an actual independent shift, or a shift applied to
14246 the first operand of another operation. We want the whole shift
14247 operation. */
14248 if (GET_CODE (early_op) == REG)
14249 early_op = op;
14251 return !reg_overlap_mentioned_p (value, early_op);
14254 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14255 have an early register shift value dependency on the result of
14256 PRODUCER. */
14259 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14261 rtx value = PATTERN (producer);
14262 rtx op = PATTERN (consumer);
14263 rtx early_op;
14265 if (GET_CODE (value) == COND_EXEC)
14266 value = COND_EXEC_CODE (value);
14267 if (GET_CODE (value) == PARALLEL)
14268 value = XVECEXP (value, 0, 0);
14269 value = XEXP (value, 0);
14270 if (GET_CODE (op) == COND_EXEC)
14271 op = COND_EXEC_CODE (op);
14272 if (GET_CODE (op) == PARALLEL)
14273 op = XVECEXP (op, 0, 0);
14274 op = XEXP (op, 1);
14276 early_op = XEXP (op, 0);
14278 /* This is either an actual independent shift, or a shift applied to
14279 the first operand of another operation. We want the value being
14280 shifted, in either case. */
14281 if (GET_CODE (early_op) != REG)
14282 early_op = XEXP (early_op, 0);
14284 return !reg_overlap_mentioned_p (value, early_op);
14287 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14288 have an early register mult dependency on the result of
14289 PRODUCER. */
14292 arm_no_early_mul_dep (rtx producer, rtx consumer)
14294 rtx value = PATTERN (producer);
14295 rtx op = PATTERN (consumer);
14297 if (GET_CODE (value) == COND_EXEC)
14298 value = COND_EXEC_CODE (value);
14299 if (GET_CODE (value) == PARALLEL)
14300 value = XVECEXP (value, 0, 0);
14301 value = XEXP (value, 0);
14302 if (GET_CODE (op) == COND_EXEC)
14303 op = COND_EXEC_CODE (op);
14304 if (GET_CODE (op) == PARALLEL)
14305 op = XVECEXP (op, 0, 0);
14306 op = XEXP (op, 1);
14308 return (GET_CODE (op) == PLUS
14309 && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14313 /* We can't rely on the caller doing the proper promotion when
14314 using APCS or ATPCS. */
14316 static bool
14317 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14319 return !TARGET_AAPCS_BASED;
14323 /* AAPCS based ABIs use short enums by default. */
14325 static bool
14326 arm_default_short_enums (void)
14328 return TARGET_AAPCS_BASED;
14332 /* AAPCS requires that anonymous bitfields affect structure alignment. */
14334 static bool
14335 arm_align_anon_bitfield (void)
14337 return TARGET_AAPCS_BASED;
14341 /* The generic C++ ABI says 64-bit (long long). The EABI says 32-bit. */
14343 static tree
14344 arm_cxx_guard_type (void)
14346 return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14350 /* The EABI says test the least significan bit of a guard variable. */
14352 static bool
14353 arm_cxx_guard_mask_bit (void)
14355 return TARGET_AAPCS_BASED;
14359 /* The EABI specifies that all array cookies are 8 bytes long. */
14361 static tree
14362 arm_get_cookie_size (tree type)
14364 tree size;
14366 if (!TARGET_AAPCS_BASED)
14367 return default_cxx_get_cookie_size (type);
14369 size = build_int_cst (sizetype, 8);
14370 return size;
14374 /* The EABI says that array cookies should also contain the element size. */
14376 static bool
14377 arm_cookie_has_size (void)
14379 return TARGET_AAPCS_BASED;
14383 /* The EABI says constructors and destructors should return a pointer to
14384 the object constructed/destroyed. */
14386 static bool
14387 arm_cxx_cdtor_returns_this (void)
14389 return TARGET_AAPCS_BASED;
14392 /* The EABI says that an inline function may never be the key
14393 method. */
14395 static bool
14396 arm_cxx_key_method_may_be_inline (void)
14398 return !TARGET_AAPCS_BASED;
14401 static void
14402 arm_cxx_determine_class_data_visibility (tree decl)
14404 if (!TARGET_AAPCS_BASED)
14405 return;
14407 /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
14408 is exported. However, on systems without dynamic vague linkage,
14409 \S 3.2.5.6 says that COMDAT class data has hidden linkage. */
14410 if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
14411 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
14412 else
14413 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
14414 DECL_VISIBILITY_SPECIFIED (decl) = 1;
14417 static bool
14418 arm_cxx_class_data_always_comdat (void)
14420 /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
14421 vague linkage if the class has no key function. */
14422 return !TARGET_AAPCS_BASED;
14426 /* The EABI says __aeabi_atexit should be used to register static
14427 destructors. */
14429 static bool
14430 arm_cxx_use_aeabi_atexit (void)
14432 return TARGET_AAPCS_BASED;
14436 void
14437 arm_set_return_address (rtx source, rtx scratch)
14439 arm_stack_offsets *offsets;
14440 HOST_WIDE_INT delta;
14441 rtx addr;
14442 unsigned long saved_regs;
14444 saved_regs = arm_compute_save_reg_mask ();
14446 if ((saved_regs & (1 << LR_REGNUM)) == 0)
14447 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14448 else
14450 if (frame_pointer_needed)
14451 addr = plus_constant(hard_frame_pointer_rtx, -4);
14452 else
14454 /* LR will be the first saved register. */
14455 offsets = arm_get_frame_offsets ();
14456 delta = offsets->outgoing_args - (offsets->frame + 4);
14459 if (delta >= 4096)
14461 emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
14462 GEN_INT (delta & ~4095)));
14463 addr = scratch;
14464 delta &= 4095;
14466 else
14467 addr = stack_pointer_rtx;
14469 addr = plus_constant (addr, delta);
14471 emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14476 void
14477 thumb_set_return_address (rtx source, rtx scratch)
14479 arm_stack_offsets *offsets;
14480 HOST_WIDE_INT delta;
14481 int reg;
14482 rtx addr;
14483 unsigned long mask;
14485 emit_insn (gen_rtx_USE (VOIDmode, source));
14487 mask = thumb_compute_save_reg_mask ();
14488 if (mask & (1 << LR_REGNUM))
14490 offsets = arm_get_frame_offsets ();
14492 /* Find the saved regs. */
14493 if (frame_pointer_needed)
14495 delta = offsets->soft_frame - offsets->saved_args;
14496 reg = THUMB_HARD_FRAME_POINTER_REGNUM;
14498 else
14500 delta = offsets->outgoing_args - offsets->saved_args;
14501 reg = SP_REGNUM;
14503 /* Allow for the stack frame. */
14504 if (TARGET_BACKTRACE)
14505 delta -= 16;
14506 /* The link register is always the first saved register. */
14507 delta -= 4;
14509 /* Construct the address. */
14510 addr = gen_rtx_REG (SImode, reg);
14511 if ((reg != SP_REGNUM && delta >= 128)
14512 || delta >= 1024)
14514 emit_insn (gen_movsi (scratch, GEN_INT (delta)));
14515 emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
14516 addr = scratch;
14518 else
14519 addr = plus_constant (addr, delta);
14521 emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14523 else
14524 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14527 /* Implements target hook vector_mode_supported_p. */
14528 bool
14529 arm_vector_mode_supported_p (enum machine_mode mode)
14531 if ((mode == V2SImode)
14532 || (mode == V4HImode)
14533 || (mode == V8QImode))
14534 return true;
14536 return false;
14539 /* Implement TARGET_SHIFT_TRUNCATION_MASK. SImode shifts use normal
14540 ARM insns and therefore guarantee that the shift count is modulo 256.
14541 DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
14542 guarantee no particular behavior for out-of-range counts. */
14544 static unsigned HOST_WIDE_INT
14545 arm_shift_truncation_mask (enum machine_mode mode)
14547 return mode == SImode ? 255 : 0;
14551 /* Map internal gcc register numbers to DWARF2 register numbers. */
14553 unsigned int
14554 arm_dbx_register_number (unsigned int regno)
14556 if (regno < 16)
14557 return regno;
14559 /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
14560 compatibility. The EABI defines them as registers 96-103. */
14561 if (IS_FPA_REGNUM (regno))
14562 return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
14564 if (IS_VFP_REGNUM (regno))
14565 return 64 + regno - FIRST_VFP_REGNUM;
14567 if (IS_IWMMXT_GR_REGNUM (regno))
14568 return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
14570 if (IS_IWMMXT_REGNUM (regno))
14571 return 112 + regno - FIRST_IWMMXT_REGNUM;
14573 gcc_unreachable ();