* config.gcc (arm*-wince-pe*, arm-*-pe*, strongarm-*-pe): Add
[official-gcc.git] / gcc / config / arm / arm.c
blob4a71420144af04b60e48cc98edbc6e60bb9b0cc8
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 bool arm_handle_option (size_t, const char *, int);
185 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
187 /* Initialize the GCC target structure. */
188 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
189 #undef TARGET_MERGE_DECL_ATTRIBUTES
190 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
191 #endif
193 #undef TARGET_ATTRIBUTE_TABLE
194 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
196 #undef TARGET_ASM_FILE_END
197 #define TARGET_ASM_FILE_END arm_file_end
199 #ifdef AOF_ASSEMBLER
200 #undef TARGET_ASM_BYTE_OP
201 #define TARGET_ASM_BYTE_OP "\tDCB\t"
202 #undef TARGET_ASM_ALIGNED_HI_OP
203 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
204 #undef TARGET_ASM_ALIGNED_SI_OP
205 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
206 #undef TARGET_ASM_GLOBALIZE_LABEL
207 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
208 #undef TARGET_ASM_FILE_START
209 #define TARGET_ASM_FILE_START aof_file_start
210 #undef TARGET_ASM_FILE_END
211 #define TARGET_ASM_FILE_END aof_file_end
212 #else
213 #undef TARGET_ASM_ALIGNED_SI_OP
214 #define TARGET_ASM_ALIGNED_SI_OP NULL
215 #undef TARGET_ASM_INTEGER
216 #define TARGET_ASM_INTEGER arm_assemble_integer
217 #endif
219 #undef TARGET_ASM_FUNCTION_PROLOGUE
220 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
222 #undef TARGET_ASM_FUNCTION_EPILOGUE
223 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
225 #undef TARGET_DEFAULT_TARGET_FLAGS
226 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
227 #undef TARGET_HANDLE_OPTION
228 #define TARGET_HANDLE_OPTION arm_handle_option
230 #undef TARGET_COMP_TYPE_ATTRIBUTES
231 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
233 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
234 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
236 #undef TARGET_SCHED_ADJUST_COST
237 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
239 #undef TARGET_ENCODE_SECTION_INFO
240 #ifdef ARM_PE
241 #define TARGET_ENCODE_SECTION_INFO arm_pe_encode_section_info
242 #else
243 #define TARGET_ENCODE_SECTION_INFO arm_encode_section_info
244 #endif
246 #undef TARGET_STRIP_NAME_ENCODING
247 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
249 #undef TARGET_ASM_INTERNAL_LABEL
250 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
252 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
253 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
255 #undef TARGET_ASM_OUTPUT_MI_THUNK
256 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
257 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
258 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
260 /* This will be overridden in arm_override_options. */
261 #undef TARGET_RTX_COSTS
262 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
263 #undef TARGET_ADDRESS_COST
264 #define TARGET_ADDRESS_COST arm_address_cost
266 #undef TARGET_SHIFT_TRUNCATION_MASK
267 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
268 #undef TARGET_VECTOR_MODE_SUPPORTED_P
269 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
271 #undef TARGET_MACHINE_DEPENDENT_REORG
272 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
274 #undef TARGET_INIT_BUILTINS
275 #define TARGET_INIT_BUILTINS arm_init_builtins
276 #undef TARGET_EXPAND_BUILTIN
277 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
279 #undef TARGET_INIT_LIBFUNCS
280 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
282 #undef TARGET_PROMOTE_FUNCTION_ARGS
283 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
284 #undef TARGET_PROMOTE_FUNCTION_RETURN
285 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
286 #undef TARGET_PROMOTE_PROTOTYPES
287 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
288 #undef TARGET_PASS_BY_REFERENCE
289 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
290 #undef TARGET_ARG_PARTIAL_BYTES
291 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
293 #undef TARGET_STRUCT_VALUE_RTX
294 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
296 #undef TARGET_SETUP_INCOMING_VARARGS
297 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
299 #undef TARGET_DEFAULT_SHORT_ENUMS
300 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
302 #undef TARGET_ALIGN_ANON_BITFIELD
303 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
305 #undef TARGET_CXX_GUARD_TYPE
306 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
308 #undef TARGET_CXX_GUARD_MASK_BIT
309 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
311 #undef TARGET_CXX_GET_COOKIE_SIZE
312 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
314 #undef TARGET_CXX_COOKIE_HAS_SIZE
315 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
317 #undef TARGET_CXX_CDTOR_RETURNS_THIS
318 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
320 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
321 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
323 #undef TARGET_CXX_USE_AEABI_ATEXIT
324 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
326 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
327 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
328 arm_cxx_determine_class_data_visibility
330 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
331 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
333 #undef TARGET_RETURN_IN_MSB
334 #define TARGET_RETURN_IN_MSB arm_return_in_msb
336 #undef TARGET_MUST_PASS_IN_STACK
337 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
339 struct gcc_target targetm = TARGET_INITIALIZER;
341 /* Obstack for minipool constant handling. */
342 static struct obstack minipool_obstack;
343 static char * minipool_startobj;
345 /* The maximum number of insns skipped which
346 will be conditionalised if possible. */
347 static int max_insns_skipped = 5;
349 extern FILE * asm_out_file;
351 /* True if we are currently building a constant table. */
352 int making_const_table;
354 /* Define the information needed to generate branch insns. This is
355 stored from the compare operation. */
356 rtx arm_compare_op0, arm_compare_op1;
358 /* The processor for which instructions should be scheduled. */
359 enum processor_type arm_tune = arm_none;
361 /* Which floating point model to use. */
362 enum arm_fp_model arm_fp_model;
364 /* Which floating point hardware is available. */
365 enum fputype arm_fpu_arch;
367 /* Which floating point hardware to schedule for. */
368 enum fputype arm_fpu_tune;
370 /* Whether to use floating point hardware. */
371 enum float_abi_type arm_float_abi;
373 /* Which ABI to use. */
374 enum arm_abi_type arm_abi;
376 /* Set by the -mfpu=... option. */
377 static const char * target_fpu_name = NULL;
379 /* Set by the -mfpe=... option. */
380 static const char * target_fpe_name = NULL;
382 /* Set by the -mfloat-abi=... option. */
383 static const char * target_float_abi_name = NULL;
385 /* Set by the -mabi=... option. */
386 static const char * target_abi_name = NULL;
388 /* Used to parse -mstructure_size_boundary command line option. */
389 static const char * structure_size_string = NULL;
390 int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
392 /* Used for Thumb call_via trampolines. */
393 rtx thumb_call_via_label[14];
394 static int thumb_call_reg_needed;
396 /* Bit values used to identify processor capabilities. */
397 #define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
398 #define FL_ARCH3M (1 << 1) /* Extended multiply */
399 #define FL_MODE26 (1 << 2) /* 26-bit mode support */
400 #define FL_MODE32 (1 << 3) /* 32-bit mode support */
401 #define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
402 #define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
403 #define FL_THUMB (1 << 6) /* Thumb aware */
404 #define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
405 #define FL_STRONG (1 << 8) /* StrongARM */
406 #define FL_ARCH5E (1 << 9) /* DSP extensions to v5 */
407 #define FL_XSCALE (1 << 10) /* XScale */
408 #define FL_CIRRUS (1 << 11) /* Cirrus/DSP. */
409 #define FL_ARCH6 (1 << 12) /* Architecture rel 6. Adds
410 media instructions. */
411 #define FL_VFPV2 (1 << 13) /* Vector Floating Point V2. */
412 #define FL_WBUF (1 << 14) /* Schedule for write buffer ops.
413 Note: ARM6 & 7 derivatives only. */
415 #define FL_IWMMXT (1 << 29) /* XScale v2 or "Intel Wireless MMX technology". */
417 #define FL_FOR_ARCH2 0
418 #define FL_FOR_ARCH3 FL_MODE32
419 #define FL_FOR_ARCH3M (FL_FOR_ARCH3 | FL_ARCH3M)
420 #define FL_FOR_ARCH4 (FL_FOR_ARCH3M | FL_ARCH4)
421 #define FL_FOR_ARCH4T (FL_FOR_ARCH4 | FL_THUMB)
422 #define FL_FOR_ARCH5 (FL_FOR_ARCH4 | FL_ARCH5)
423 #define FL_FOR_ARCH5T (FL_FOR_ARCH5 | FL_THUMB)
424 #define FL_FOR_ARCH5E (FL_FOR_ARCH5 | FL_ARCH5E)
425 #define FL_FOR_ARCH5TE (FL_FOR_ARCH5E | FL_THUMB)
426 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
427 #define FL_FOR_ARCH6 (FL_FOR_ARCH5TE | FL_ARCH6)
428 #define FL_FOR_ARCH6J FL_FOR_ARCH6
429 #define FL_FOR_ARCH6K FL_FOR_ARCH6
430 #define FL_FOR_ARCH6Z FL_FOR_ARCH6
431 #define FL_FOR_ARCH6ZK FL_FOR_ARCH6
433 /* The bits in this mask specify which
434 instructions we are allowed to generate. */
435 static unsigned long insn_flags = 0;
437 /* The bits in this mask specify which instruction scheduling options should
438 be used. */
439 static unsigned long tune_flags = 0;
441 /* The following are used in the arm.md file as equivalents to bits
442 in the above two flag variables. */
444 /* Nonzero if this chip supports the ARM Architecture 3M extensions. */
445 int arm_arch3m = 0;
447 /* Nonzero if this chip supports the ARM Architecture 4 extensions. */
448 int arm_arch4 = 0;
450 /* Nonzero if this chip supports the ARM Architecture 4t extensions. */
451 int arm_arch4t = 0;
453 /* Nonzero if this chip supports the ARM Architecture 5 extensions. */
454 int arm_arch5 = 0;
456 /* Nonzero if this chip supports the ARM Architecture 5E extensions. */
457 int arm_arch5e = 0;
459 /* Nonzero if this chip supports the ARM Architecture 6 extensions. */
460 int arm_arch6 = 0;
462 /* Nonzero if this chip can benefit from load scheduling. */
463 int arm_ld_sched = 0;
465 /* Nonzero if this chip is a StrongARM. */
466 int arm_tune_strongarm = 0;
468 /* Nonzero if this chip is a Cirrus variant. */
469 int arm_arch_cirrus = 0;
471 /* Nonzero if this chip supports Intel Wireless MMX technology. */
472 int arm_arch_iwmmxt = 0;
474 /* Nonzero if this chip is an XScale. */
475 int arm_arch_xscale = 0;
477 /* Nonzero if tuning for XScale */
478 int arm_tune_xscale = 0;
480 /* Nonzero if we want to tune for stores that access the write-buffer.
481 This typically means an ARM6 or ARM7 with MMU or MPU. */
482 int arm_tune_wbuf = 0;
484 /* Nonzero if generating Thumb instructions. */
485 int thumb_code = 0;
487 /* Nonzero if we should define __THUMB_INTERWORK__ in the
488 preprocessor.
489 XXX This is a bit of a hack, it's intended to help work around
490 problems in GLD which doesn't understand that armv5t code is
491 interworking clean. */
492 int arm_cpp_interwork = 0;
494 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
495 must report the mode of the memory reference from PRINT_OPERAND to
496 PRINT_OPERAND_ADDRESS. */
497 enum machine_mode output_memory_reference_mode;
499 /* The register number to be used for the PIC offset register. */
500 static const char * arm_pic_register_string = NULL;
501 int arm_pic_register = INVALID_REGNUM;
503 /* Set to 1 when a return insn is output, this means that the epilogue
504 is not needed. */
505 int return_used_this_function;
507 /* Set to 1 after arm_reorg has started. Reset to start at the start of
508 the next function. */
509 static int after_arm_reorg = 0;
511 /* The maximum number of insns to be used when loading a constant. */
512 static int arm_constant_limit = 3;
514 /* For an explanation of these variables, see final_prescan_insn below. */
515 int arm_ccfsm_state;
516 enum arm_cond_code arm_current_cc;
517 rtx arm_target_insn;
518 int arm_target_label;
520 /* The condition codes of the ARM, and the inverse function. */
521 static const char * const arm_condition_codes[] =
523 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
524 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
527 #define streq(string1, string2) (strcmp (string1, string2) == 0)
529 /* Initialization code. */
531 struct processors
533 const char *const name;
534 enum processor_type core;
535 const char *arch;
536 const unsigned long flags;
537 bool (* rtx_costs) (rtx, int, int, int *);
540 /* Not all of these give usefully different compilation alternatives,
541 but there is no simple way of generalizing them. */
542 static const struct processors all_cores[] =
544 /* ARM Cores */
545 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
546 {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
547 #include "arm-cores.def"
548 #undef ARM_CORE
549 {NULL, arm_none, NULL, 0, NULL}
552 static const struct processors all_architectures[] =
554 /* ARM Architectures */
555 /* We don't specify rtx_costs here as it will be figured out
556 from the core. */
558 {"armv2", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
559 {"armv2a", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
560 {"armv3", arm6, "3", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
561 {"armv3m", arm7m, "3M", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
562 {"armv4", arm7tdmi, "4", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
563 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
564 implementations that support it, so we will leave it out for now. */
565 {"armv4t", arm7tdmi, "4T", FL_CO_PROC | FL_FOR_ARCH4T, NULL},
566 {"armv5", arm10tdmi, "5", FL_CO_PROC | FL_FOR_ARCH5, NULL},
567 {"armv5t", arm10tdmi, "5T", FL_CO_PROC | FL_FOR_ARCH5T, NULL},
568 {"armv5e", arm1026ejs, "5E", FL_CO_PROC | FL_FOR_ARCH5E, NULL},
569 {"armv5te", arm1026ejs, "5TE", FL_CO_PROC | FL_FOR_ARCH5TE, NULL},
570 {"armv6", arm1136js, "6", FL_CO_PROC | FL_FOR_ARCH6, NULL},
571 {"armv6j", arm1136js, "6J", FL_CO_PROC | FL_FOR_ARCH6J, NULL},
572 {"armv6k", mpcore, "6K", FL_CO_PROC | FL_FOR_ARCH6K, NULL},
573 {"armv6z", arm1176jzs, "6Z", FL_CO_PROC | FL_FOR_ARCH6Z, NULL},
574 {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC | FL_FOR_ARCH6ZK, NULL},
575 {"ep9312", ep9312, "4T", FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
576 {"iwmmxt", iwmmxt, "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
577 {NULL, arm_none, NULL, 0 , NULL}
580 struct arm_cpu_select
582 const char * string;
583 const char * name;
584 const struct processors * processors;
587 /* This is a magic structure. The 'string' field is magically filled in
588 with a pointer to the value specified by the user on the command line
589 assuming that the user has specified such a value. */
591 static struct arm_cpu_select arm_select[] =
593 /* string name processors */
594 { NULL, "-mcpu=", all_cores },
595 { NULL, "-march=", all_architectures },
596 { NULL, "-mtune=", all_cores }
600 /* The name of the proprocessor macro to define for this architecture. */
602 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
604 struct fpu_desc
606 const char * name;
607 enum fputype fpu;
611 /* Available values for for -mfpu=. */
613 static const struct fpu_desc all_fpus[] =
615 {"fpa", FPUTYPE_FPA},
616 {"fpe2", FPUTYPE_FPA_EMU2},
617 {"fpe3", FPUTYPE_FPA_EMU2},
618 {"maverick", FPUTYPE_MAVERICK},
619 {"vfp", FPUTYPE_VFP}
623 /* Floating point models used by the different hardware.
624 See fputype in arm.h. */
626 static const enum fputype fp_model_for_fpu[] =
628 /* No FP hardware. */
629 ARM_FP_MODEL_UNKNOWN, /* FPUTYPE_NONE */
630 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA */
631 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU2 */
632 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU3 */
633 ARM_FP_MODEL_MAVERICK, /* FPUTYPE_MAVERICK */
634 ARM_FP_MODEL_VFP /* FPUTYPE_VFP */
638 struct float_abi
640 const char * name;
641 enum float_abi_type abi_type;
645 /* Available values for -mfloat-abi=. */
647 static const struct float_abi all_float_abis[] =
649 {"soft", ARM_FLOAT_ABI_SOFT},
650 {"softfp", ARM_FLOAT_ABI_SOFTFP},
651 {"hard", ARM_FLOAT_ABI_HARD}
655 struct abi_name
657 const char *name;
658 enum arm_abi_type abi_type;
662 /* Available values for -mabi=. */
664 static const struct abi_name arm_all_abis[] =
666 {"apcs-gnu", ARM_ABI_APCS},
667 {"atpcs", ARM_ABI_ATPCS},
668 {"aapcs", ARM_ABI_AAPCS},
669 {"iwmmxt", ARM_ABI_IWMMXT}
672 /* Return the number of bits set in VALUE. */
673 static unsigned
674 bit_count (unsigned long value)
676 unsigned long count = 0;
678 while (value)
680 count++;
681 value &= value - 1; /* Clear the least-significant set bit. */
684 return count;
687 /* Set up library functions unique to ARM. */
689 static void
690 arm_init_libfuncs (void)
692 /* There are no special library functions unless we are using the
693 ARM BPABI. */
694 if (!TARGET_BPABI)
695 return;
697 /* The functions below are described in Section 4 of the "Run-Time
698 ABI for the ARM architecture", Version 1.0. */
700 /* Double-precision floating-point arithmetic. Table 2. */
701 set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
702 set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
703 set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
704 set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
705 set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
707 /* Double-precision comparisons. Table 3. */
708 set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
709 set_optab_libfunc (ne_optab, DFmode, NULL);
710 set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
711 set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
712 set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
713 set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
714 set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
716 /* Single-precision floating-point arithmetic. Table 4. */
717 set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
718 set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
719 set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
720 set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
721 set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
723 /* Single-precision comparisons. Table 5. */
724 set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
725 set_optab_libfunc (ne_optab, SFmode, NULL);
726 set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
727 set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
728 set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
729 set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
730 set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
732 /* Floating-point to integer conversions. Table 6. */
733 set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
734 set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
735 set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
736 set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
737 set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
738 set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
739 set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
740 set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
742 /* Conversions between floating types. Table 7. */
743 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
744 set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
746 /* Integer to floating-point conversions. Table 8. */
747 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
748 set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
749 set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
750 set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
751 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
752 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
753 set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
754 set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
756 /* Long long. Table 9. */
757 set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
758 set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
759 set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
760 set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
761 set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
762 set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
763 set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
764 set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
766 /* Integer (32/32->32) division. \S 4.3.1. */
767 set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
768 set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
770 /* The divmod functions are designed so that they can be used for
771 plain division, even though they return both the quotient and the
772 remainder. The quotient is returned in the usual location (i.e.,
773 r0 for SImode, {r0, r1} for DImode), just as would be expected
774 for an ordinary division routine. Because the AAPCS calling
775 conventions specify that all of { r0, r1, r2, r3 } are
776 callee-saved registers, there is no need to tell the compiler
777 explicitly that those registers are clobbered by these
778 routines. */
779 set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
780 set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
781 set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idivmod");
782 set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidivmod");
784 /* We don't have mod libcalls. Fortunately gcc knows how to use the
785 divmod libcalls instead. */
786 set_optab_libfunc (smod_optab, DImode, NULL);
787 set_optab_libfunc (umod_optab, DImode, NULL);
788 set_optab_libfunc (smod_optab, SImode, NULL);
789 set_optab_libfunc (umod_optab, SImode, NULL);
792 /* Implement TARGET_HANDLE_OPTION. */
794 static bool
795 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
797 switch (code)
799 case OPT_mabi_:
800 target_abi_name = arg;
801 return true;
803 case OPT_march_:
804 arm_select[1].string = arg;
805 return true;
807 case OPT_mcpu_:
808 arm_select[0].string = arg;
809 return true;
811 case OPT_mfloat_abi_:
812 target_float_abi_name = arg;
813 return true;
815 case OPT_mfp_:
816 case OPT_mfpe_:
817 target_fpe_name = arg;
818 return true;
820 case OPT_mfpu_:
821 target_fpu_name = arg;
822 return true;
824 case OPT_mhard_float:
825 target_float_abi_name = "hard";
826 return true;
828 case OPT_mpic_register_:
829 arm_pic_register_string = arg;
830 return true;
832 case OPT_msoft_float:
833 target_float_abi_name = "soft";
834 return true;
836 case OPT_mstructure_size_boundary_:
837 structure_size_string = arg;
838 return true;
840 case OPT_mtune_:
841 arm_select[2].string = arg;
842 return true;
844 default:
845 return true;
849 /* Fix up any incompatible options that the user has specified.
850 This has now turned into a maze. */
851 void
852 arm_override_options (void)
854 unsigned i;
856 /* Set up the flags based on the cpu/architecture selected by the user. */
857 for (i = ARRAY_SIZE (arm_select); i--;)
859 struct arm_cpu_select * ptr = arm_select + i;
861 if (ptr->string != NULL && ptr->string[0] != '\0')
863 const struct processors * sel;
865 for (sel = ptr->processors; sel->name != NULL; sel++)
866 if (streq (ptr->string, sel->name))
868 /* Set the architecture define. */
869 if (i != 2)
870 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
872 /* Determine the processor core for which we should
873 tune code-generation. */
874 if (/* -mcpu= is a sensible default. */
875 i == 0
876 /* If -march= is used, and -mcpu= has not been used,
877 assume that we should tune for a representative
878 CPU from that architecture. */
879 || i == 1
880 /* -mtune= overrides -mcpu= and -march=. */
881 || i == 2)
882 arm_tune = (enum processor_type) (sel - ptr->processors);
884 if (i != 2)
886 /* If we have been given an architecture and a processor
887 make sure that they are compatible. We only generate
888 a warning though, and we prefer the CPU over the
889 architecture. */
890 if (insn_flags != 0 && (insn_flags ^ sel->flags))
891 warning (0, "switch -mcpu=%s conflicts with -march= switch",
892 ptr->string);
894 insn_flags = sel->flags;
897 break;
900 if (sel->name == NULL)
901 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
905 /* If the user did not specify a processor, choose one for them. */
906 if (insn_flags == 0)
908 const struct processors * sel;
909 unsigned int sought;
910 enum processor_type cpu;
912 cpu = TARGET_CPU_DEFAULT;
913 if (cpu == arm_none)
915 #ifdef SUBTARGET_CPU_DEFAULT
916 /* Use the subtarget default CPU if none was specified by
917 configure. */
918 cpu = SUBTARGET_CPU_DEFAULT;
919 #endif
920 /* Default to ARM6. */
921 if (cpu == arm_none)
922 cpu = arm6;
924 sel = &all_cores[cpu];
926 insn_flags = sel->flags;
928 /* Now check to see if the user has specified some command line
929 switch that require certain abilities from the cpu. */
930 sought = 0;
932 if (TARGET_INTERWORK || TARGET_THUMB)
934 sought |= (FL_THUMB | FL_MODE32);
936 /* There are no ARM processors that support both APCS-26 and
937 interworking. Therefore we force FL_MODE26 to be removed
938 from insn_flags here (if it was set), so that the search
939 below will always be able to find a compatible processor. */
940 insn_flags &= ~FL_MODE26;
943 if (sought != 0 && ((sought & insn_flags) != sought))
945 /* Try to locate a CPU type that supports all of the abilities
946 of the default CPU, plus the extra abilities requested by
947 the user. */
948 for (sel = all_cores; sel->name != NULL; sel++)
949 if ((sel->flags & sought) == (sought | insn_flags))
950 break;
952 if (sel->name == NULL)
954 unsigned current_bit_count = 0;
955 const struct processors * best_fit = NULL;
957 /* Ideally we would like to issue an error message here
958 saying that it was not possible to find a CPU compatible
959 with the default CPU, but which also supports the command
960 line options specified by the programmer, and so they
961 ought to use the -mcpu=<name> command line option to
962 override the default CPU type.
964 If we cannot find a cpu that has both the
965 characteristics of the default cpu and the given
966 command line options we scan the array again looking
967 for a best match. */
968 for (sel = all_cores; sel->name != NULL; sel++)
969 if ((sel->flags & sought) == sought)
971 unsigned count;
973 count = bit_count (sel->flags & insn_flags);
975 if (count >= current_bit_count)
977 best_fit = sel;
978 current_bit_count = count;
982 gcc_assert (best_fit);
983 sel = best_fit;
986 insn_flags = sel->flags;
988 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
989 if (arm_tune == arm_none)
990 arm_tune = (enum processor_type) (sel - all_cores);
993 /* The processor for which we should tune should now have been
994 chosen. */
995 gcc_assert (arm_tune != arm_none);
997 tune_flags = all_cores[(int)arm_tune].flags;
998 if (optimize_size)
999 targetm.rtx_costs = arm_size_rtx_costs;
1000 else
1001 targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
1003 /* Make sure that the processor choice does not conflict with any of the
1004 other command line choices. */
1005 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
1007 warning (0, "target CPU does not support interworking" );
1008 target_flags &= ~MASK_INTERWORK;
1011 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1013 warning (0, "target CPU does not support THUMB instructions");
1014 target_flags &= ~MASK_THUMB;
1017 if (TARGET_APCS_FRAME && TARGET_THUMB)
1019 /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1020 target_flags &= ~MASK_APCS_FRAME;
1023 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1024 from here where no function is being compiled currently. */
1025 if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1026 warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1028 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1029 warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1031 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1032 warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1034 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1036 warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1037 target_flags |= MASK_APCS_FRAME;
1040 if (TARGET_POKE_FUNCTION_NAME)
1041 target_flags |= MASK_APCS_FRAME;
1043 if (TARGET_APCS_REENT && flag_pic)
1044 error ("-fpic and -mapcs-reent are incompatible");
1046 if (TARGET_APCS_REENT)
1047 warning (0, "APCS reentrant code not supported. Ignored");
1049 /* If this target is normally configured to use APCS frames, warn if they
1050 are turned off and debugging is turned on. */
1051 if (TARGET_ARM
1052 && write_symbols != NO_DEBUG
1053 && !TARGET_APCS_FRAME
1054 && (TARGET_DEFAULT & MASK_APCS_FRAME))
1055 warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1057 /* If stack checking is disabled, we can use r10 as the PIC register,
1058 which keeps r9 available. */
1059 if (flag_pic)
1060 arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
1062 if (TARGET_APCS_FLOAT)
1063 warning (0, "passing floating point arguments in fp regs not yet supported");
1065 /* Initialize boolean versions of the flags, for use in the arm.md file. */
1066 arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1067 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1068 arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1069 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1070 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1071 arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1072 arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1073 arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1075 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1076 arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1077 thumb_code = (TARGET_ARM == 0);
1078 arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1079 arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1080 arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1082 /* V5 code we generate is completely interworking capable, so we turn off
1083 TARGET_INTERWORK here to avoid many tests later on. */
1085 /* XXX However, we must pass the right pre-processor defines to CPP
1086 or GLD can get confused. This is a hack. */
1087 if (TARGET_INTERWORK)
1088 arm_cpp_interwork = 1;
1090 if (arm_arch5)
1091 target_flags &= ~MASK_INTERWORK;
1093 if (target_abi_name)
1095 for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1097 if (streq (arm_all_abis[i].name, target_abi_name))
1099 arm_abi = arm_all_abis[i].abi_type;
1100 break;
1103 if (i == ARRAY_SIZE (arm_all_abis))
1104 error ("invalid ABI option: -mabi=%s", target_abi_name);
1106 else
1107 arm_abi = ARM_DEFAULT_ABI;
1109 if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1110 error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1112 if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1113 error ("iwmmxt abi requires an iwmmxt capable cpu");
1115 arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1116 if (target_fpu_name == NULL && target_fpe_name != NULL)
1118 if (streq (target_fpe_name, "2"))
1119 target_fpu_name = "fpe2";
1120 else if (streq (target_fpe_name, "3"))
1121 target_fpu_name = "fpe3";
1122 else
1123 error ("invalid floating point emulation option: -mfpe=%s",
1124 target_fpe_name);
1126 if (target_fpu_name != NULL)
1128 /* The user specified a FPU. */
1129 for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1131 if (streq (all_fpus[i].name, target_fpu_name))
1133 arm_fpu_arch = all_fpus[i].fpu;
1134 arm_fpu_tune = arm_fpu_arch;
1135 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1136 break;
1139 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1140 error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1142 else
1144 #ifdef FPUTYPE_DEFAULT
1145 /* Use the default if it is specified for this platform. */
1146 arm_fpu_arch = FPUTYPE_DEFAULT;
1147 arm_fpu_tune = FPUTYPE_DEFAULT;
1148 #else
1149 /* Pick one based on CPU type. */
1150 /* ??? Some targets assume FPA is the default.
1151 if ((insn_flags & FL_VFP) != 0)
1152 arm_fpu_arch = FPUTYPE_VFP;
1153 else
1155 if (arm_arch_cirrus)
1156 arm_fpu_arch = FPUTYPE_MAVERICK;
1157 else
1158 arm_fpu_arch = FPUTYPE_FPA_EMU2;
1159 #endif
1160 if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1161 arm_fpu_tune = FPUTYPE_FPA;
1162 else
1163 arm_fpu_tune = arm_fpu_arch;
1164 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1165 gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1168 if (target_float_abi_name != NULL)
1170 /* The user specified a FP ABI. */
1171 for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1173 if (streq (all_float_abis[i].name, target_float_abi_name))
1175 arm_float_abi = all_float_abis[i].abi_type;
1176 break;
1179 if (i == ARRAY_SIZE (all_float_abis))
1180 error ("invalid floating point abi: -mfloat-abi=%s",
1181 target_float_abi_name);
1183 else
1184 arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1186 if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1187 sorry ("-mfloat-abi=hard and VFP");
1189 /* If soft-float is specified then don't use FPU. */
1190 if (TARGET_SOFT_FLOAT)
1191 arm_fpu_arch = FPUTYPE_NONE;
1193 /* For arm2/3 there is no need to do any scheduling if there is only
1194 a floating point emulator, or we are doing software floating-point. */
1195 if ((TARGET_SOFT_FLOAT
1196 || arm_fpu_tune == FPUTYPE_FPA_EMU2
1197 || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1198 && (tune_flags & FL_MODE32) == 0)
1199 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1201 /* Override the default structure alignment for AAPCS ABI. */
1202 if (arm_abi == ARM_ABI_AAPCS)
1203 arm_structure_size_boundary = 8;
1205 if (structure_size_string != NULL)
1207 int size = strtol (structure_size_string, NULL, 0);
1209 if (size == 8 || size == 32
1210 || (ARM_DOUBLEWORD_ALIGN && size == 64))
1211 arm_structure_size_boundary = size;
1212 else
1213 warning (0, "structure size boundary can only be set to %s",
1214 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1217 if (arm_pic_register_string != NULL)
1219 int pic_register = decode_reg_name (arm_pic_register_string);
1221 if (!flag_pic)
1222 warning (0, "-mpic-register= is useless without -fpic");
1224 /* Prevent the user from choosing an obviously stupid PIC register. */
1225 else if (pic_register < 0 || call_used_regs[pic_register]
1226 || pic_register == HARD_FRAME_POINTER_REGNUM
1227 || pic_register == STACK_POINTER_REGNUM
1228 || pic_register >= PC_REGNUM)
1229 error ("unable to use '%s' for PIC register", arm_pic_register_string);
1230 else
1231 arm_pic_register = pic_register;
1234 if (TARGET_THUMB && flag_schedule_insns)
1236 /* Don't warn since it's on by default in -O2. */
1237 flag_schedule_insns = 0;
1240 if (optimize_size)
1242 /* There's some dispute as to whether this should be 1 or 2. However,
1243 experiments seem to show that in pathological cases a setting of
1244 1 degrades less severely than a setting of 2. This could change if
1245 other parts of the compiler change their behavior. */
1246 arm_constant_limit = 1;
1248 /* If optimizing for size, bump the number of instructions that we
1249 are prepared to conditionally execute (even on a StrongARM). */
1250 max_insns_skipped = 6;
1252 else
1254 /* For processors with load scheduling, it never costs more than
1255 2 cycles to load a constant, and the load scheduler may well
1256 reduce that to 1. */
1257 if (arm_ld_sched)
1258 arm_constant_limit = 1;
1260 /* On XScale the longer latency of a load makes it more difficult
1261 to achieve a good schedule, so it's faster to synthesize
1262 constants that can be done in two insns. */
1263 if (arm_tune_xscale)
1264 arm_constant_limit = 2;
1266 /* StrongARM has early execution of branches, so a sequence
1267 that is worth skipping is shorter. */
1268 if (arm_tune_strongarm)
1269 max_insns_skipped = 3;
1272 /* Register global variables with the garbage collector. */
1273 arm_add_gc_roots ();
1276 static void
1277 arm_add_gc_roots (void)
1279 gcc_obstack_init(&minipool_obstack);
1280 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1283 /* A table of known ARM exception types.
1284 For use with the interrupt function attribute. */
1286 typedef struct
1288 const char *const arg;
1289 const unsigned long return_value;
1291 isr_attribute_arg;
1293 static const isr_attribute_arg isr_attribute_args [] =
1295 { "IRQ", ARM_FT_ISR },
1296 { "irq", ARM_FT_ISR },
1297 { "FIQ", ARM_FT_FIQ },
1298 { "fiq", ARM_FT_FIQ },
1299 { "ABORT", ARM_FT_ISR },
1300 { "abort", ARM_FT_ISR },
1301 { "ABORT", ARM_FT_ISR },
1302 { "abort", ARM_FT_ISR },
1303 { "UNDEF", ARM_FT_EXCEPTION },
1304 { "undef", ARM_FT_EXCEPTION },
1305 { "SWI", ARM_FT_EXCEPTION },
1306 { "swi", ARM_FT_EXCEPTION },
1307 { NULL, ARM_FT_NORMAL }
1310 /* Returns the (interrupt) function type of the current
1311 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
1313 static unsigned long
1314 arm_isr_value (tree argument)
1316 const isr_attribute_arg * ptr;
1317 const char * arg;
1319 /* No argument - default to IRQ. */
1320 if (argument == NULL_TREE)
1321 return ARM_FT_ISR;
1323 /* Get the value of the argument. */
1324 if (TREE_VALUE (argument) == NULL_TREE
1325 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1326 return ARM_FT_UNKNOWN;
1328 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1330 /* Check it against the list of known arguments. */
1331 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1332 if (streq (arg, ptr->arg))
1333 return ptr->return_value;
1335 /* An unrecognized interrupt type. */
1336 return ARM_FT_UNKNOWN;
1339 /* Computes the type of the current function. */
1341 static unsigned long
1342 arm_compute_func_type (void)
1344 unsigned long type = ARM_FT_UNKNOWN;
1345 tree a;
1346 tree attr;
1348 gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1350 /* Decide if the current function is volatile. Such functions
1351 never return, and many memory cycles can be saved by not storing
1352 register values that will never be needed again. This optimization
1353 was added to speed up context switching in a kernel application. */
1354 if (optimize > 0
1355 && TREE_NOTHROW (current_function_decl)
1356 && TREE_THIS_VOLATILE (current_function_decl))
1357 type |= ARM_FT_VOLATILE;
1359 if (cfun->static_chain_decl != NULL)
1360 type |= ARM_FT_NESTED;
1362 attr = DECL_ATTRIBUTES (current_function_decl);
1364 a = lookup_attribute ("naked", attr);
1365 if (a != NULL_TREE)
1366 type |= ARM_FT_NAKED;
1368 a = lookup_attribute ("isr", attr);
1369 if (a == NULL_TREE)
1370 a = lookup_attribute ("interrupt", attr);
1372 if (a == NULL_TREE)
1373 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1374 else
1375 type |= arm_isr_value (TREE_VALUE (a));
1377 return type;
1380 /* Returns the type of the current function. */
1382 unsigned long
1383 arm_current_func_type (void)
1385 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1386 cfun->machine->func_type = arm_compute_func_type ();
1388 return cfun->machine->func_type;
1391 /* Return 1 if it is possible to return using a single instruction.
1392 If SIBLING is non-null, this is a test for a return before a sibling
1393 call. SIBLING is the call insn, so we can examine its register usage. */
1396 use_return_insn (int iscond, rtx sibling)
1398 int regno;
1399 unsigned int func_type;
1400 unsigned long saved_int_regs;
1401 unsigned HOST_WIDE_INT stack_adjust;
1402 arm_stack_offsets *offsets;
1404 /* Never use a return instruction before reload has run. */
1405 if (!reload_completed)
1406 return 0;
1408 func_type = arm_current_func_type ();
1410 /* Naked functions and volatile functions need special
1411 consideration. */
1412 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1413 return 0;
1415 /* So do interrupt functions that use the frame pointer. */
1416 if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1417 return 0;
1419 offsets = arm_get_frame_offsets ();
1420 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1422 /* As do variadic functions. */
1423 if (current_function_pretend_args_size
1424 || cfun->machine->uses_anonymous_args
1425 /* Or if the function calls __builtin_eh_return () */
1426 || current_function_calls_eh_return
1427 /* Or if the function calls alloca */
1428 || current_function_calls_alloca
1429 /* Or if there is a stack adjustment. However, if the stack pointer
1430 is saved on the stack, we can use a pre-incrementing stack load. */
1431 || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1432 return 0;
1434 saved_int_regs = arm_compute_save_reg_mask ();
1436 /* Unfortunately, the insn
1438 ldmib sp, {..., sp, ...}
1440 triggers a bug on most SA-110 based devices, such that the stack
1441 pointer won't be correctly restored if the instruction takes a
1442 page fault. We work around this problem by popping r3 along with
1443 the other registers, since that is never slower than executing
1444 another instruction.
1446 We test for !arm_arch5 here, because code for any architecture
1447 less than this could potentially be run on one of the buggy
1448 chips. */
1449 if (stack_adjust == 4 && !arm_arch5)
1451 /* Validate that r3 is a call-clobbered register (always true in
1452 the default abi) ... */
1453 if (!call_used_regs[3])
1454 return 0;
1456 /* ... that it isn't being used for a return value (always true
1457 until we implement return-in-regs), or for a tail-call
1458 argument ... */
1459 if (sibling)
1461 gcc_assert (GET_CODE (sibling) == CALL_INSN);
1463 if (find_regno_fusage (sibling, USE, 3))
1464 return 0;
1467 /* ... and that there are no call-saved registers in r0-r2
1468 (always true in the default ABI). */
1469 if (saved_int_regs & 0x7)
1470 return 0;
1473 /* Can't be done if interworking with Thumb, and any registers have been
1474 stacked. */
1475 if (TARGET_INTERWORK && saved_int_regs != 0)
1476 return 0;
1478 /* On StrongARM, conditional returns are expensive if they aren't
1479 taken and multiple registers have been stacked. */
1480 if (iscond && arm_tune_strongarm)
1482 /* Conditional return when just the LR is stored is a simple
1483 conditional-load instruction, that's not expensive. */
1484 if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1485 return 0;
1487 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1488 return 0;
1491 /* If there are saved registers but the LR isn't saved, then we need
1492 two instructions for the return. */
1493 if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1494 return 0;
1496 /* Can't be done if any of the FPA regs are pushed,
1497 since this also requires an insn. */
1498 if (TARGET_HARD_FLOAT && TARGET_FPA)
1499 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1500 if (regs_ever_live[regno] && !call_used_regs[regno])
1501 return 0;
1503 /* Likewise VFP regs. */
1504 if (TARGET_HARD_FLOAT && TARGET_VFP)
1505 for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1506 if (regs_ever_live[regno] && !call_used_regs[regno])
1507 return 0;
1509 if (TARGET_REALLY_IWMMXT)
1510 for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1511 if (regs_ever_live[regno] && ! call_used_regs [regno])
1512 return 0;
1514 return 1;
1517 /* Return TRUE if int I is a valid immediate ARM constant. */
1520 const_ok_for_arm (HOST_WIDE_INT i)
1522 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1524 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1525 be all zero, or all one. */
1526 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1527 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1528 != ((~(unsigned HOST_WIDE_INT) 0)
1529 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1530 return FALSE;
1532 /* Fast return for 0 and powers of 2 */
1533 if ((i & (i - 1)) == 0)
1534 return TRUE;
1538 if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1539 return TRUE;
1540 mask =
1541 (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1542 >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1544 while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1546 return FALSE;
1549 /* Return true if I is a valid constant for the operation CODE. */
1550 static int
1551 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1553 if (const_ok_for_arm (i))
1554 return 1;
1556 switch (code)
1558 case PLUS:
1559 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1561 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
1562 case XOR:
1563 case IOR:
1564 return 0;
1566 case AND:
1567 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1569 default:
1570 gcc_unreachable ();
1574 /* Emit a sequence of insns to handle a large constant.
1575 CODE is the code of the operation required, it can be any of SET, PLUS,
1576 IOR, AND, XOR, MINUS;
1577 MODE is the mode in which the operation is being performed;
1578 VAL is the integer to operate on;
1579 SOURCE is the other operand (a register, or a null-pointer for SET);
1580 SUBTARGETS means it is safe to create scratch registers if that will
1581 either produce a simpler sequence, or we will want to cse the values.
1582 Return value is the number of insns emitted. */
1585 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1586 HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1588 rtx cond;
1590 if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1591 cond = COND_EXEC_TEST (PATTERN (insn));
1592 else
1593 cond = NULL_RTX;
1595 if (subtargets || code == SET
1596 || (GET_CODE (target) == REG && GET_CODE (source) == REG
1597 && REGNO (target) != REGNO (source)))
1599 /* After arm_reorg has been called, we can't fix up expensive
1600 constants by pushing them into memory so we must synthesize
1601 them in-line, regardless of the cost. This is only likely to
1602 be more costly on chips that have load delay slots and we are
1603 compiling without running the scheduler (so no splitting
1604 occurred before the final instruction emission).
1606 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1608 if (!after_arm_reorg
1609 && !cond
1610 && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1611 1, 0)
1612 > arm_constant_limit + (code != SET)))
1614 if (code == SET)
1616 /* Currently SET is the only monadic value for CODE, all
1617 the rest are diadic. */
1618 emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1619 return 1;
1621 else
1623 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1625 emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1626 /* For MINUS, the value is subtracted from, since we never
1627 have subtraction of a constant. */
1628 if (code == MINUS)
1629 emit_insn (gen_rtx_SET (VOIDmode, target,
1630 gen_rtx_MINUS (mode, temp, source)));
1631 else
1632 emit_insn (gen_rtx_SET (VOIDmode, target,
1633 gen_rtx_fmt_ee (code, mode, source, temp)));
1634 return 2;
1639 return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1643 static int
1644 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1646 HOST_WIDE_INT temp1;
1647 int num_insns = 0;
1650 int end;
1652 if (i <= 0)
1653 i += 32;
1654 if (remainder & (3 << (i - 2)))
1656 end = i - 8;
1657 if (end < 0)
1658 end += 32;
1659 temp1 = remainder & ((0x0ff << end)
1660 | ((i < end) ? (0xff >> (32 - end)) : 0));
1661 remainder &= ~temp1;
1662 num_insns++;
1663 i -= 6;
1665 i -= 2;
1666 } while (remainder);
1667 return num_insns;
1670 /* Emit an instruction with the indicated PATTERN. If COND is
1671 non-NULL, conditionalize the execution of the instruction on COND
1672 being true. */
1674 static void
1675 emit_constant_insn (rtx cond, rtx pattern)
1677 if (cond)
1678 pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1679 emit_insn (pattern);
1682 /* As above, but extra parameter GENERATE which, if clear, suppresses
1683 RTL generation. */
1685 static int
1686 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1687 HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1688 int generate)
1690 int can_invert = 0;
1691 int can_negate = 0;
1692 int can_negate_initial = 0;
1693 int can_shift = 0;
1694 int i;
1695 int num_bits_set = 0;
1696 int set_sign_bit_copies = 0;
1697 int clear_sign_bit_copies = 0;
1698 int clear_zero_bit_copies = 0;
1699 int set_zero_bit_copies = 0;
1700 int insns = 0;
1701 unsigned HOST_WIDE_INT temp1, temp2;
1702 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1704 /* Find out which operations are safe for a given CODE. Also do a quick
1705 check for degenerate cases; these can occur when DImode operations
1706 are split. */
1707 switch (code)
1709 case SET:
1710 can_invert = 1;
1711 can_shift = 1;
1712 can_negate = 1;
1713 break;
1715 case PLUS:
1716 can_negate = 1;
1717 can_negate_initial = 1;
1718 break;
1720 case IOR:
1721 if (remainder == 0xffffffff)
1723 if (generate)
1724 emit_constant_insn (cond,
1725 gen_rtx_SET (VOIDmode, target,
1726 GEN_INT (ARM_SIGN_EXTEND (val))));
1727 return 1;
1729 if (remainder == 0)
1731 if (reload_completed && rtx_equal_p (target, source))
1732 return 0;
1733 if (generate)
1734 emit_constant_insn (cond,
1735 gen_rtx_SET (VOIDmode, target, source));
1736 return 1;
1738 break;
1740 case AND:
1741 if (remainder == 0)
1743 if (generate)
1744 emit_constant_insn (cond,
1745 gen_rtx_SET (VOIDmode, target, const0_rtx));
1746 return 1;
1748 if (remainder == 0xffffffff)
1750 if (reload_completed && rtx_equal_p (target, source))
1751 return 0;
1752 if (generate)
1753 emit_constant_insn (cond,
1754 gen_rtx_SET (VOIDmode, target, source));
1755 return 1;
1757 can_invert = 1;
1758 break;
1760 case XOR:
1761 if (remainder == 0)
1763 if (reload_completed && rtx_equal_p (target, source))
1764 return 0;
1765 if (generate)
1766 emit_constant_insn (cond,
1767 gen_rtx_SET (VOIDmode, target, source));
1768 return 1;
1771 /* We don't know how to handle other cases yet. */
1772 gcc_assert (remainder == 0xffffffff);
1774 if (generate)
1775 emit_constant_insn (cond,
1776 gen_rtx_SET (VOIDmode, target,
1777 gen_rtx_NOT (mode, source)));
1778 return 1;
1780 case MINUS:
1781 /* We treat MINUS as (val - source), since (source - val) is always
1782 passed as (source + (-val)). */
1783 if (remainder == 0)
1785 if (generate)
1786 emit_constant_insn (cond,
1787 gen_rtx_SET (VOIDmode, target,
1788 gen_rtx_NEG (mode, source)));
1789 return 1;
1791 if (const_ok_for_arm (val))
1793 if (generate)
1794 emit_constant_insn (cond,
1795 gen_rtx_SET (VOIDmode, target,
1796 gen_rtx_MINUS (mode, GEN_INT (val),
1797 source)));
1798 return 1;
1800 can_negate = 1;
1802 break;
1804 default:
1805 gcc_unreachable ();
1808 /* If we can do it in one insn get out quickly. */
1809 if (const_ok_for_arm (val)
1810 || (can_negate_initial && const_ok_for_arm (-val))
1811 || (can_invert && const_ok_for_arm (~val)))
1813 if (generate)
1814 emit_constant_insn (cond,
1815 gen_rtx_SET (VOIDmode, target,
1816 (source
1817 ? gen_rtx_fmt_ee (code, mode, source,
1818 GEN_INT (val))
1819 : GEN_INT (val))));
1820 return 1;
1823 /* Calculate a few attributes that may be useful for specific
1824 optimizations. */
1825 for (i = 31; i >= 0; i--)
1827 if ((remainder & (1 << i)) == 0)
1828 clear_sign_bit_copies++;
1829 else
1830 break;
1833 for (i = 31; i >= 0; i--)
1835 if ((remainder & (1 << i)) != 0)
1836 set_sign_bit_copies++;
1837 else
1838 break;
1841 for (i = 0; i <= 31; i++)
1843 if ((remainder & (1 << i)) == 0)
1844 clear_zero_bit_copies++;
1845 else
1846 break;
1849 for (i = 0; i <= 31; i++)
1851 if ((remainder & (1 << i)) != 0)
1852 set_zero_bit_copies++;
1853 else
1854 break;
1857 switch (code)
1859 case SET:
1860 /* See if we can do this by sign_extending a constant that is known
1861 to be negative. This is a good, way of doing it, since the shift
1862 may well merge into a subsequent insn. */
1863 if (set_sign_bit_copies > 1)
1865 if (const_ok_for_arm
1866 (temp1 = ARM_SIGN_EXTEND (remainder
1867 << (set_sign_bit_copies - 1))))
1869 if (generate)
1871 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1872 emit_constant_insn (cond,
1873 gen_rtx_SET (VOIDmode, new_src,
1874 GEN_INT (temp1)));
1875 emit_constant_insn (cond,
1876 gen_ashrsi3 (target, new_src,
1877 GEN_INT (set_sign_bit_copies - 1)));
1879 return 2;
1881 /* For an inverted constant, we will need to set the low bits,
1882 these will be shifted out of harm's way. */
1883 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1884 if (const_ok_for_arm (~temp1))
1886 if (generate)
1888 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1889 emit_constant_insn (cond,
1890 gen_rtx_SET (VOIDmode, new_src,
1891 GEN_INT (temp1)));
1892 emit_constant_insn (cond,
1893 gen_ashrsi3 (target, new_src,
1894 GEN_INT (set_sign_bit_copies - 1)));
1896 return 2;
1900 /* See if we can generate this by setting the bottom (or the top)
1901 16 bits, and then shifting these into the other half of the
1902 word. We only look for the simplest cases, to do more would cost
1903 too much. Be careful, however, not to generate this when the
1904 alternative would take fewer insns. */
1905 if (val & 0xffff0000)
1907 temp1 = remainder & 0xffff0000;
1908 temp2 = remainder & 0x0000ffff;
1910 /* Overlaps outside this range are best done using other methods. */
1911 for (i = 9; i < 24; i++)
1913 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1914 && !const_ok_for_arm (temp2))
1916 rtx new_src = (subtargets
1917 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1918 : target);
1919 insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1920 source, subtargets, generate);
1921 source = new_src;
1922 if (generate)
1923 emit_constant_insn
1924 (cond,
1925 gen_rtx_SET
1926 (VOIDmode, target,
1927 gen_rtx_IOR (mode,
1928 gen_rtx_ASHIFT (mode, source,
1929 GEN_INT (i)),
1930 source)));
1931 return insns + 1;
1935 /* Don't duplicate cases already considered. */
1936 for (i = 17; i < 24; i++)
1938 if (((temp1 | (temp1 >> i)) == remainder)
1939 && !const_ok_for_arm (temp1))
1941 rtx new_src = (subtargets
1942 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1943 : target);
1944 insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1945 source, subtargets, generate);
1946 source = new_src;
1947 if (generate)
1948 emit_constant_insn
1949 (cond,
1950 gen_rtx_SET (VOIDmode, target,
1951 gen_rtx_IOR
1952 (mode,
1953 gen_rtx_LSHIFTRT (mode, source,
1954 GEN_INT (i)),
1955 source)));
1956 return insns + 1;
1960 break;
1962 case IOR:
1963 case XOR:
1964 /* If we have IOR or XOR, and the constant can be loaded in a
1965 single instruction, and we can find a temporary to put it in,
1966 then this can be done in two instructions instead of 3-4. */
1967 if (subtargets
1968 /* TARGET can't be NULL if SUBTARGETS is 0 */
1969 || (reload_completed && !reg_mentioned_p (target, source)))
1971 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1973 if (generate)
1975 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1977 emit_constant_insn (cond,
1978 gen_rtx_SET (VOIDmode, sub,
1979 GEN_INT (val)));
1980 emit_constant_insn (cond,
1981 gen_rtx_SET (VOIDmode, target,
1982 gen_rtx_fmt_ee (code, mode,
1983 source, sub)));
1985 return 2;
1989 if (code == XOR)
1990 break;
1992 if (set_sign_bit_copies > 8
1993 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1995 if (generate)
1997 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1998 rtx shift = GEN_INT (set_sign_bit_copies);
2000 emit_constant_insn
2001 (cond,
2002 gen_rtx_SET (VOIDmode, sub,
2003 gen_rtx_NOT (mode,
2004 gen_rtx_ASHIFT (mode,
2005 source,
2006 shift))));
2007 emit_constant_insn
2008 (cond,
2009 gen_rtx_SET (VOIDmode, target,
2010 gen_rtx_NOT (mode,
2011 gen_rtx_LSHIFTRT (mode, sub,
2012 shift))));
2014 return 2;
2017 if (set_zero_bit_copies > 8
2018 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2020 if (generate)
2022 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2023 rtx shift = GEN_INT (set_zero_bit_copies);
2025 emit_constant_insn
2026 (cond,
2027 gen_rtx_SET (VOIDmode, sub,
2028 gen_rtx_NOT (mode,
2029 gen_rtx_LSHIFTRT (mode,
2030 source,
2031 shift))));
2032 emit_constant_insn
2033 (cond,
2034 gen_rtx_SET (VOIDmode, target,
2035 gen_rtx_NOT (mode,
2036 gen_rtx_ASHIFT (mode, sub,
2037 shift))));
2039 return 2;
2042 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2044 if (generate)
2046 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2047 emit_constant_insn (cond,
2048 gen_rtx_SET (VOIDmode, sub,
2049 gen_rtx_NOT (mode, source)));
2050 source = sub;
2051 if (subtargets)
2052 sub = gen_reg_rtx (mode);
2053 emit_constant_insn (cond,
2054 gen_rtx_SET (VOIDmode, sub,
2055 gen_rtx_AND (mode, source,
2056 GEN_INT (temp1))));
2057 emit_constant_insn (cond,
2058 gen_rtx_SET (VOIDmode, target,
2059 gen_rtx_NOT (mode, sub)));
2061 return 3;
2063 break;
2065 case AND:
2066 /* See if two shifts will do 2 or more insn's worth of work. */
2067 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2069 HOST_WIDE_INT shift_mask = ((0xffffffff
2070 << (32 - clear_sign_bit_copies))
2071 & 0xffffffff);
2073 if ((remainder | shift_mask) != 0xffffffff)
2075 if (generate)
2077 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2078 insns = arm_gen_constant (AND, mode, cond,
2079 remainder | shift_mask,
2080 new_src, source, subtargets, 1);
2081 source = new_src;
2083 else
2085 rtx targ = subtargets ? NULL_RTX : target;
2086 insns = arm_gen_constant (AND, mode, cond,
2087 remainder | shift_mask,
2088 targ, source, subtargets, 0);
2092 if (generate)
2094 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2095 rtx shift = GEN_INT (clear_sign_bit_copies);
2097 emit_insn (gen_ashlsi3 (new_src, source, shift));
2098 emit_insn (gen_lshrsi3 (target, new_src, shift));
2101 return insns + 2;
2104 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2106 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2108 if ((remainder | shift_mask) != 0xffffffff)
2110 if (generate)
2112 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2114 insns = arm_gen_constant (AND, mode, cond,
2115 remainder | shift_mask,
2116 new_src, source, subtargets, 1);
2117 source = new_src;
2119 else
2121 rtx targ = subtargets ? NULL_RTX : target;
2123 insns = arm_gen_constant (AND, mode, cond,
2124 remainder | shift_mask,
2125 targ, source, subtargets, 0);
2129 if (generate)
2131 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2132 rtx shift = GEN_INT (clear_zero_bit_copies);
2134 emit_insn (gen_lshrsi3 (new_src, source, shift));
2135 emit_insn (gen_ashlsi3 (target, new_src, shift));
2138 return insns + 2;
2141 break;
2143 default:
2144 break;
2147 for (i = 0; i < 32; i++)
2148 if (remainder & (1 << i))
2149 num_bits_set++;
2151 if (code == AND || (can_invert && num_bits_set > 16))
2152 remainder = (~remainder) & 0xffffffff;
2153 else if (code == PLUS && num_bits_set > 16)
2154 remainder = (-remainder) & 0xffffffff;
2155 else
2157 can_invert = 0;
2158 can_negate = 0;
2161 /* Now try and find a way of doing the job in either two or three
2162 instructions.
2163 We start by looking for the largest block of zeros that are aligned on
2164 a 2-bit boundary, we then fill up the temps, wrapping around to the
2165 top of the word when we drop off the bottom.
2166 In the worst case this code should produce no more than four insns. */
2168 int best_start = 0;
2169 int best_consecutive_zeros = 0;
2171 for (i = 0; i < 32; i += 2)
2173 int consecutive_zeros = 0;
2175 if (!(remainder & (3 << i)))
2177 while ((i < 32) && !(remainder & (3 << i)))
2179 consecutive_zeros += 2;
2180 i += 2;
2182 if (consecutive_zeros > best_consecutive_zeros)
2184 best_consecutive_zeros = consecutive_zeros;
2185 best_start = i - consecutive_zeros;
2187 i -= 2;
2191 /* So long as it won't require any more insns to do so, it's
2192 desirable to emit a small constant (in bits 0...9) in the last
2193 insn. This way there is more chance that it can be combined with
2194 a later addressing insn to form a pre-indexed load or store
2195 operation. Consider:
2197 *((volatile int *)0xe0000100) = 1;
2198 *((volatile int *)0xe0000110) = 2;
2200 We want this to wind up as:
2202 mov rA, #0xe0000000
2203 mov rB, #1
2204 str rB, [rA, #0x100]
2205 mov rB, #2
2206 str rB, [rA, #0x110]
2208 rather than having to synthesize both large constants from scratch.
2210 Therefore, we calculate how many insns would be required to emit
2211 the constant starting from `best_start', and also starting from
2212 zero (i.e. with bit 31 first to be output). If `best_start' doesn't
2213 yield a shorter sequence, we may as well use zero. */
2214 if (best_start != 0
2215 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2216 && (count_insns_for_constant (remainder, 0) <=
2217 count_insns_for_constant (remainder, best_start)))
2218 best_start = 0;
2220 /* Now start emitting the insns. */
2221 i = best_start;
2224 int end;
2226 if (i <= 0)
2227 i += 32;
2228 if (remainder & (3 << (i - 2)))
2230 end = i - 8;
2231 if (end < 0)
2232 end += 32;
2233 temp1 = remainder & ((0x0ff << end)
2234 | ((i < end) ? (0xff >> (32 - end)) : 0));
2235 remainder &= ~temp1;
2237 if (generate)
2239 rtx new_src, temp1_rtx;
2241 if (code == SET || code == MINUS)
2243 new_src = (subtargets ? gen_reg_rtx (mode) : target);
2244 if (can_invert && code != MINUS)
2245 temp1 = ~temp1;
2247 else
2249 if (remainder && subtargets)
2250 new_src = gen_reg_rtx (mode);
2251 else
2252 new_src = target;
2253 if (can_invert)
2254 temp1 = ~temp1;
2255 else if (can_negate)
2256 temp1 = -temp1;
2259 temp1 = trunc_int_for_mode (temp1, mode);
2260 temp1_rtx = GEN_INT (temp1);
2262 if (code == SET)
2264 else if (code == MINUS)
2265 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2266 else
2267 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2269 emit_constant_insn (cond,
2270 gen_rtx_SET (VOIDmode, new_src,
2271 temp1_rtx));
2272 source = new_src;
2275 if (code == SET)
2277 can_invert = 0;
2278 code = PLUS;
2280 else if (code == MINUS)
2281 code = PLUS;
2283 insns++;
2284 i -= 6;
2286 i -= 2;
2288 while (remainder);
2291 return insns;
2294 /* Canonicalize a comparison so that we are more likely to recognize it.
2295 This can be done for a few constant compares, where we can make the
2296 immediate value easier to load. */
2298 enum rtx_code
2299 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2301 unsigned HOST_WIDE_INT i = INTVAL (*op1);
2303 switch (code)
2305 case EQ:
2306 case NE:
2307 return code;
2309 case GT:
2310 case LE:
2311 if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2312 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2314 *op1 = GEN_INT (i + 1);
2315 return code == GT ? GE : LT;
2317 break;
2319 case GE:
2320 case LT:
2321 if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2322 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2324 *op1 = GEN_INT (i - 1);
2325 return code == GE ? GT : LE;
2327 break;
2329 case GTU:
2330 case LEU:
2331 if (i != ~((unsigned HOST_WIDE_INT) 0)
2332 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2334 *op1 = GEN_INT (i + 1);
2335 return code == GTU ? GEU : LTU;
2337 break;
2339 case GEU:
2340 case LTU:
2341 if (i != 0
2342 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2344 *op1 = GEN_INT (i - 1);
2345 return code == GEU ? GTU : LEU;
2347 break;
2349 default:
2350 gcc_unreachable ();
2353 return code;
2357 /* Define how to find the value returned by a function. */
2360 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2362 enum machine_mode mode;
2363 int unsignedp ATTRIBUTE_UNUSED;
2364 rtx r ATTRIBUTE_UNUSED;
2366 mode = TYPE_MODE (type);
2367 /* Promote integer types. */
2368 if (INTEGRAL_TYPE_P (type))
2369 PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2371 /* Promotes small structs returned in a register to full-word size
2372 for big-endian AAPCS. */
2373 if (arm_return_in_msb (type))
2375 HOST_WIDE_INT size = int_size_in_bytes (type);
2376 if (size % UNITS_PER_WORD != 0)
2378 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2379 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2383 return LIBCALL_VALUE(mode);
2386 /* Determine the amount of memory needed to store the possible return
2387 registers of an untyped call. */
2389 arm_apply_result_size (void)
2391 int size = 16;
2393 if (TARGET_ARM)
2395 if (TARGET_HARD_FLOAT_ABI)
2397 if (TARGET_FPA)
2398 size += 12;
2399 if (TARGET_MAVERICK)
2400 size += 8;
2402 if (TARGET_IWMMXT_ABI)
2403 size += 8;
2406 return size;
2409 /* Decide whether a type should be returned in memory (true)
2410 or in a register (false). This is called by the macro
2411 RETURN_IN_MEMORY. */
2413 arm_return_in_memory (tree type)
2415 HOST_WIDE_INT size;
2417 if (!AGGREGATE_TYPE_P (type) &&
2418 !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2419 /* All simple types are returned in registers.
2420 For AAPCS, complex types are treated the same as aggregates. */
2421 return 0;
2423 size = int_size_in_bytes (type);
2425 if (arm_abi != ARM_ABI_APCS)
2427 /* ATPCS and later return aggregate types in memory only if they are
2428 larger than a word (or are variable size). */
2429 return (size < 0 || size > UNITS_PER_WORD);
2432 /* For the arm-wince targets we choose to be compatible with Microsoft's
2433 ARM and Thumb compilers, which always return aggregates in memory. */
2434 #ifndef ARM_WINCE
2435 /* All structures/unions bigger than one word are returned in memory.
2436 Also catch the case where int_size_in_bytes returns -1. In this case
2437 the aggregate is either huge or of variable size, and in either case
2438 we will want to return it via memory and not in a register. */
2439 if (size < 0 || size > UNITS_PER_WORD)
2440 return 1;
2442 if (TREE_CODE (type) == RECORD_TYPE)
2444 tree field;
2446 /* For a struct the APCS says that we only return in a register
2447 if the type is 'integer like' and every addressable element
2448 has an offset of zero. For practical purposes this means
2449 that the structure can have at most one non bit-field element
2450 and that this element must be the first one in the structure. */
2452 /* Find the first field, ignoring non FIELD_DECL things which will
2453 have been created by C++. */
2454 for (field = TYPE_FIELDS (type);
2455 field && TREE_CODE (field) != FIELD_DECL;
2456 field = TREE_CHAIN (field))
2457 continue;
2459 if (field == NULL)
2460 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
2462 /* Check that the first field is valid for returning in a register. */
2464 /* ... Floats are not allowed */
2465 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2466 return 1;
2468 /* ... Aggregates that are not themselves valid for returning in
2469 a register are not allowed. */
2470 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2471 return 1;
2473 /* Now check the remaining fields, if any. Only bitfields are allowed,
2474 since they are not addressable. */
2475 for (field = TREE_CHAIN (field);
2476 field;
2477 field = TREE_CHAIN (field))
2479 if (TREE_CODE (field) != FIELD_DECL)
2480 continue;
2482 if (!DECL_BIT_FIELD_TYPE (field))
2483 return 1;
2486 return 0;
2489 if (TREE_CODE (type) == UNION_TYPE)
2491 tree field;
2493 /* Unions can be returned in registers if every element is
2494 integral, or can be returned in an integer register. */
2495 for (field = TYPE_FIELDS (type);
2496 field;
2497 field = TREE_CHAIN (field))
2499 if (TREE_CODE (field) != FIELD_DECL)
2500 continue;
2502 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2503 return 1;
2505 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2506 return 1;
2509 return 0;
2511 #endif /* not ARM_WINCE */
2513 /* Return all other types in memory. */
2514 return 1;
2517 /* Indicate whether or not words of a double are in big-endian order. */
2520 arm_float_words_big_endian (void)
2522 if (TARGET_MAVERICK)
2523 return 0;
2525 /* For FPA, float words are always big-endian. For VFP, floats words
2526 follow the memory system mode. */
2528 if (TARGET_FPA)
2530 return 1;
2533 if (TARGET_VFP)
2534 return (TARGET_BIG_END ? 1 : 0);
2536 return 1;
2539 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2540 for a call to a function whose data type is FNTYPE.
2541 For a library call, FNTYPE is NULL. */
2542 void
2543 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2544 rtx libname ATTRIBUTE_UNUSED,
2545 tree fndecl ATTRIBUTE_UNUSED)
2547 /* On the ARM, the offset starts at 0. */
2548 pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2549 pcum->iwmmxt_nregs = 0;
2550 pcum->can_split = true;
2552 pcum->call_cookie = CALL_NORMAL;
2554 if (TARGET_LONG_CALLS)
2555 pcum->call_cookie = CALL_LONG;
2557 /* Check for long call/short call attributes. The attributes
2558 override any command line option. */
2559 if (fntype)
2561 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2562 pcum->call_cookie = CALL_SHORT;
2563 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2564 pcum->call_cookie = CALL_LONG;
2567 /* Varargs vectors are treated the same as long long.
2568 named_count avoids having to change the way arm handles 'named' */
2569 pcum->named_count = 0;
2570 pcum->nargs = 0;
2572 if (TARGET_REALLY_IWMMXT && fntype)
2574 tree fn_arg;
2576 for (fn_arg = TYPE_ARG_TYPES (fntype);
2577 fn_arg;
2578 fn_arg = TREE_CHAIN (fn_arg))
2579 pcum->named_count += 1;
2581 if (! pcum->named_count)
2582 pcum->named_count = INT_MAX;
2587 /* Return true if mode/type need doubleword alignment. */
2588 bool
2589 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2591 return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2592 || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2596 /* Determine where to put an argument to a function.
2597 Value is zero to push the argument on the stack,
2598 or a hard register in which to store the argument.
2600 MODE is the argument's machine mode.
2601 TYPE is the data type of the argument (as a tree).
2602 This is null for libcalls where that information may
2603 not be available.
2604 CUM is a variable of type CUMULATIVE_ARGS which gives info about
2605 the preceding args and about the function being called.
2606 NAMED is nonzero if this argument is a named parameter
2607 (otherwise it is an extra parameter matching an ellipsis). */
2610 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2611 tree type, int named)
2613 int nregs;
2615 /* Varargs vectors are treated the same as long long.
2616 named_count avoids having to change the way arm handles 'named' */
2617 if (TARGET_IWMMXT_ABI
2618 && arm_vector_mode_supported_p (mode)
2619 && pcum->named_count > pcum->nargs + 1)
2621 if (pcum->iwmmxt_nregs <= 9)
2622 return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2623 else
2625 pcum->can_split = false;
2626 return NULL_RTX;
2630 /* Put doubleword aligned quantities in even register pairs. */
2631 if (pcum->nregs & 1
2632 && ARM_DOUBLEWORD_ALIGN
2633 && arm_needs_doubleword_align (mode, type))
2634 pcum->nregs++;
2636 if (mode == VOIDmode)
2637 /* Compute operand 2 of the call insn. */
2638 return GEN_INT (pcum->call_cookie);
2640 /* Only allow splitting an arg between regs and memory if all preceding
2641 args were allocated to regs. For args passed by reference we only count
2642 the reference pointer. */
2643 if (pcum->can_split)
2644 nregs = 1;
2645 else
2646 nregs = ARM_NUM_REGS2 (mode, type);
2648 if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2649 return NULL_RTX;
2651 return gen_rtx_REG (mode, pcum->nregs);
2654 static int
2655 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2656 tree type, bool named ATTRIBUTE_UNUSED)
2658 int nregs = pcum->nregs;
2660 if (arm_vector_mode_supported_p (mode))
2661 return 0;
2663 if (NUM_ARG_REGS > nregs
2664 && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2665 && pcum->can_split)
2666 return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2668 return 0;
2671 /* Variable sized types are passed by reference. This is a GCC
2672 extension to the ARM ABI. */
2674 static bool
2675 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2676 enum machine_mode mode ATTRIBUTE_UNUSED,
2677 tree type, bool named ATTRIBUTE_UNUSED)
2679 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2682 /* Encode the current state of the #pragma [no_]long_calls. */
2683 typedef enum
2685 OFF, /* No #pramgma [no_]long_calls is in effect. */
2686 LONG, /* #pragma long_calls is in effect. */
2687 SHORT /* #pragma no_long_calls is in effect. */
2688 } arm_pragma_enum;
2690 static arm_pragma_enum arm_pragma_long_calls = OFF;
2692 void
2693 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2695 arm_pragma_long_calls = LONG;
2698 void
2699 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2701 arm_pragma_long_calls = SHORT;
2704 void
2705 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2707 arm_pragma_long_calls = OFF;
2710 /* Table of machine attributes. */
2711 const struct attribute_spec arm_attribute_table[] =
2713 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2714 /* Function calls made to this symbol must be done indirectly, because
2715 it may lie outside of the 26 bit addressing range of a normal function
2716 call. */
2717 { "long_call", 0, 0, false, true, true, NULL },
2718 /* Whereas these functions are always known to reside within the 26 bit
2719 addressing range. */
2720 { "short_call", 0, 0, false, true, true, NULL },
2721 /* Interrupt Service Routines have special prologue and epilogue requirements. */
2722 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
2723 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
2724 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2725 #ifdef ARM_PE
2726 /* ARM/PE has three new attributes:
2727 interfacearm - ?
2728 dllexport - for exporting a function/variable that will live in a dll
2729 dllimport - for importing a function/variable from a dll
2731 Microsoft allows multiple declspecs in one __declspec, separating
2732 them with spaces. We do NOT support this. Instead, use __declspec
2733 multiple times.
2735 { "dllimport", 0, 0, true, false, false, NULL },
2736 { "dllexport", 0, 0, true, false, false, NULL },
2737 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2738 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2739 { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
2740 { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
2741 { "notshared", 0, 0, false, true, false, arm_handle_notshared_attribute },
2742 #endif
2743 { NULL, 0, 0, false, false, false, NULL }
2746 /* Handle an attribute requiring a FUNCTION_DECL;
2747 arguments as in struct attribute_spec.handler. */
2748 static tree
2749 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2750 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2752 if (TREE_CODE (*node) != FUNCTION_DECL)
2754 warning (0, "%qs attribute only applies to functions",
2755 IDENTIFIER_POINTER (name));
2756 *no_add_attrs = true;
2759 return NULL_TREE;
2762 /* Handle an "interrupt" or "isr" attribute;
2763 arguments as in struct attribute_spec.handler. */
2764 static tree
2765 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2766 bool *no_add_attrs)
2768 if (DECL_P (*node))
2770 if (TREE_CODE (*node) != FUNCTION_DECL)
2772 warning (0, "%qs attribute only applies to functions",
2773 IDENTIFIER_POINTER (name));
2774 *no_add_attrs = true;
2776 /* FIXME: the argument if any is checked for type attributes;
2777 should it be checked for decl ones? */
2779 else
2781 if (TREE_CODE (*node) == FUNCTION_TYPE
2782 || TREE_CODE (*node) == METHOD_TYPE)
2784 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2786 warning (0, "%qs attribute ignored", IDENTIFIER_POINTER (name));
2787 *no_add_attrs = true;
2790 else if (TREE_CODE (*node) == POINTER_TYPE
2791 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2792 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2793 && arm_isr_value (args) != ARM_FT_UNKNOWN)
2795 *node = build_variant_type_copy (*node);
2796 TREE_TYPE (*node) = build_type_attribute_variant
2797 (TREE_TYPE (*node),
2798 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2799 *no_add_attrs = true;
2801 else
2803 /* Possibly pass this attribute on from the type to a decl. */
2804 if (flags & ((int) ATTR_FLAG_DECL_NEXT
2805 | (int) ATTR_FLAG_FUNCTION_NEXT
2806 | (int) ATTR_FLAG_ARRAY_NEXT))
2808 *no_add_attrs = true;
2809 return tree_cons (name, args, NULL_TREE);
2811 else
2813 warning (0, "%qs attribute ignored", IDENTIFIER_POINTER (name));
2818 return NULL_TREE;
2821 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2822 /* Handle the "notshared" attribute. This attribute is another way of
2823 requesting hidden visibility. ARM's compiler supports
2824 "__declspec(notshared)"; we support the same thing via an
2825 attribute. */
2827 static tree
2828 arm_handle_notshared_attribute (tree *node,
2829 tree name ATTRIBUTE_UNUSED,
2830 tree args ATTRIBUTE_UNUSED,
2831 int flags ATTRIBUTE_UNUSED,
2832 bool *no_add_attrs)
2834 tree decl = TYPE_NAME (*node);
2836 if (decl)
2838 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2839 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2840 *no_add_attrs = false;
2842 return NULL_TREE;
2844 #endif
2846 /* Return 0 if the attributes for two types are incompatible, 1 if they
2847 are compatible, and 2 if they are nearly compatible (which causes a
2848 warning to be generated). */
2849 static int
2850 arm_comp_type_attributes (tree type1, tree type2)
2852 int l1, l2, s1, s2;
2854 /* Check for mismatch of non-default calling convention. */
2855 if (TREE_CODE (type1) != FUNCTION_TYPE)
2856 return 1;
2858 /* Check for mismatched call attributes. */
2859 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2860 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2861 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2862 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2864 /* Only bother to check if an attribute is defined. */
2865 if (l1 | l2 | s1 | s2)
2867 /* If one type has an attribute, the other must have the same attribute. */
2868 if ((l1 != l2) || (s1 != s2))
2869 return 0;
2871 /* Disallow mixed attributes. */
2872 if ((l1 & s2) || (l2 & s1))
2873 return 0;
2876 /* Check for mismatched ISR attribute. */
2877 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2878 if (! l1)
2879 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2880 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2881 if (! l2)
2882 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2883 if (l1 != l2)
2884 return 0;
2886 return 1;
2889 /* Encode long_call or short_call attribute by prefixing
2890 symbol name in DECL with a special character FLAG. */
2891 void
2892 arm_encode_call_attribute (tree decl, int flag)
2894 const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2895 int len = strlen (str);
2896 char * newstr;
2898 /* Do not allow weak functions to be treated as short call. */
2899 if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2900 return;
2902 newstr = alloca (len + 2);
2903 newstr[0] = flag;
2904 strcpy (newstr + 1, str);
2906 newstr = (char *) ggc_alloc_string (newstr, len + 1);
2907 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2910 /* Assigns default attributes to newly defined type. This is used to
2911 set short_call/long_call attributes for function types of
2912 functions defined inside corresponding #pragma scopes. */
2913 static void
2914 arm_set_default_type_attributes (tree type)
2916 /* Add __attribute__ ((long_call)) to all functions, when
2917 inside #pragma long_calls or __attribute__ ((short_call)),
2918 when inside #pragma no_long_calls. */
2919 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2921 tree type_attr_list, attr_name;
2922 type_attr_list = TYPE_ATTRIBUTES (type);
2924 if (arm_pragma_long_calls == LONG)
2925 attr_name = get_identifier ("long_call");
2926 else if (arm_pragma_long_calls == SHORT)
2927 attr_name = get_identifier ("short_call");
2928 else
2929 return;
2931 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2932 TYPE_ATTRIBUTES (type) = type_attr_list;
2936 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2937 defined within the current compilation unit. If this cannot be
2938 determined, then 0 is returned. */
2939 static int
2940 current_file_function_operand (rtx sym_ref)
2942 /* This is a bit of a fib. A function will have a short call flag
2943 applied to its name if it has the short call attribute, or it has
2944 already been defined within the current compilation unit. */
2945 if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2946 return 1;
2948 /* The current function is always defined within the current compilation
2949 unit. If it s a weak definition however, then this may not be the real
2950 definition of the function, and so we have to say no. */
2951 if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2952 && !DECL_WEAK (current_function_decl))
2953 return 1;
2955 /* We cannot make the determination - default to returning 0. */
2956 return 0;
2959 /* Return nonzero if a 32 bit "long_call" should be generated for
2960 this call. We generate a long_call if the function:
2962 a. has an __attribute__((long call))
2963 or b. is within the scope of a #pragma long_calls
2964 or c. the -mlong-calls command line switch has been specified
2965 . and either:
2966 1. -ffunction-sections is in effect
2967 or 2. the current function has __attribute__ ((section))
2968 or 3. the target function has __attribute__ ((section))
2970 However we do not generate a long call if the function:
2972 d. has an __attribute__ ((short_call))
2973 or e. is inside the scope of a #pragma no_long_calls
2974 or f. is defined within the current compilation unit.
2976 This function will be called by C fragments contained in the machine
2977 description file. SYM_REF and CALL_COOKIE correspond to the matched
2978 rtl operands. CALL_SYMBOL is used to distinguish between
2979 two different callers of the function. It is set to 1 in the
2980 "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2981 and "call_value" patterns. This is because of the difference in the
2982 SYM_REFs passed by these patterns. */
2984 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2986 if (!call_symbol)
2988 if (GET_CODE (sym_ref) != MEM)
2989 return 0;
2991 sym_ref = XEXP (sym_ref, 0);
2994 if (GET_CODE (sym_ref) != SYMBOL_REF)
2995 return 0;
2997 if (call_cookie & CALL_SHORT)
2998 return 0;
3000 if (TARGET_LONG_CALLS)
3002 if (flag_function_sections
3003 || DECL_SECTION_NAME (current_function_decl))
3004 /* c.3 is handled by the definition of the
3005 ARM_DECLARE_FUNCTION_SIZE macro. */
3006 return 1;
3009 if (current_file_function_operand (sym_ref))
3010 return 0;
3012 return (call_cookie & CALL_LONG)
3013 || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
3014 || TARGET_LONG_CALLS;
3017 /* Return nonzero if it is ok to make a tail-call to DECL. */
3018 static bool
3019 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3021 int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
3023 if (cfun->machine->sibcall_blocked)
3024 return false;
3026 /* Never tailcall something for which we have no decl, or if we
3027 are in Thumb mode. */
3028 if (decl == NULL || TARGET_THUMB)
3029 return false;
3031 /* Get the calling method. */
3032 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3033 call_type = CALL_SHORT;
3034 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3035 call_type = CALL_LONG;
3037 /* Cannot tail-call to long calls, since these are out of range of
3038 a branch instruction. However, if not compiling PIC, we know
3039 we can reach the symbol if it is in this compilation unit. */
3040 if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
3041 return false;
3043 /* If we are interworking and the function is not declared static
3044 then we can't tail-call it unless we know that it exists in this
3045 compilation unit (since it might be a Thumb routine). */
3046 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3047 return false;
3049 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
3050 if (IS_INTERRUPT (arm_current_func_type ()))
3051 return false;
3053 /* Everything else is ok. */
3054 return true;
3058 /* Addressing mode support functions. */
3060 /* Return nonzero if X is a legitimate immediate operand when compiling
3061 for PIC. */
3063 legitimate_pic_operand_p (rtx x)
3065 if (CONSTANT_P (x)
3066 && flag_pic
3067 && (GET_CODE (x) == SYMBOL_REF
3068 || (GET_CODE (x) == CONST
3069 && GET_CODE (XEXP (x, 0)) == PLUS
3070 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
3071 return 0;
3073 return 1;
3077 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3079 if (GET_CODE (orig) == SYMBOL_REF
3080 || GET_CODE (orig) == LABEL_REF)
3082 #ifndef AOF_ASSEMBLER
3083 rtx pic_ref, address;
3084 #endif
3085 rtx insn;
3086 int subregs = 0;
3088 if (reg == 0)
3090 gcc_assert (!no_new_pseudos);
3091 reg = gen_reg_rtx (Pmode);
3093 subregs = 1;
3096 #ifdef AOF_ASSEMBLER
3097 /* The AOF assembler can generate relocations for these directly, and
3098 understands that the PIC register has to be added into the offset. */
3099 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3100 #else
3101 if (subregs)
3102 address = gen_reg_rtx (Pmode);
3103 else
3104 address = reg;
3106 if (TARGET_ARM)
3107 emit_insn (gen_pic_load_addr_arm (address, orig));
3108 else
3109 emit_insn (gen_pic_load_addr_thumb (address, orig));
3111 if ((GET_CODE (orig) == LABEL_REF
3112 || (GET_CODE (orig) == SYMBOL_REF &&
3113 SYMBOL_REF_LOCAL_P (orig)))
3114 && NEED_GOT_RELOC)
3115 pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
3116 else
3118 pic_ref = gen_const_mem (Pmode,
3119 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3120 address));
3123 insn = emit_move_insn (reg, pic_ref);
3124 #endif
3125 current_function_uses_pic_offset_table = 1;
3126 /* Put a REG_EQUAL note on this insn, so that it can be optimized
3127 by loop. */
3128 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3129 REG_NOTES (insn));
3130 return reg;
3132 else if (GET_CODE (orig) == CONST)
3134 rtx base, offset;
3136 if (GET_CODE (XEXP (orig, 0)) == PLUS
3137 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3138 return orig;
3140 if (reg == 0)
3142 gcc_assert (!no_new_pseudos);
3143 reg = gen_reg_rtx (Pmode);
3146 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3148 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3149 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3150 base == reg ? 0 : reg);
3152 if (GET_CODE (offset) == CONST_INT)
3154 /* The base register doesn't really matter, we only want to
3155 test the index for the appropriate mode. */
3156 if (!arm_legitimate_index_p (mode, offset, SET, 0))
3158 gcc_assert (!no_new_pseudos);
3159 offset = force_reg (Pmode, offset);
3162 if (GET_CODE (offset) == CONST_INT)
3163 return plus_constant (base, INTVAL (offset));
3166 if (GET_MODE_SIZE (mode) > 4
3167 && (GET_MODE_CLASS (mode) == MODE_INT
3168 || TARGET_SOFT_FLOAT))
3170 emit_insn (gen_addsi3 (reg, base, offset));
3171 return reg;
3174 return gen_rtx_PLUS (Pmode, base, offset);
3177 return orig;
3181 /* Find a spare low register to use during the prolog of a function. */
3183 static int
3184 thumb_find_work_register (unsigned long pushed_regs_mask)
3186 int reg;
3188 /* Check the argument registers first as these are call-used. The
3189 register allocation order means that sometimes r3 might be used
3190 but earlier argument registers might not, so check them all. */
3191 for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3192 if (!regs_ever_live[reg])
3193 return reg;
3195 /* Before going on to check the call-saved registers we can try a couple
3196 more ways of deducing that r3 is available. The first is when we are
3197 pushing anonymous arguments onto the stack and we have less than 4
3198 registers worth of fixed arguments(*). In this case r3 will be part of
3199 the variable argument list and so we can be sure that it will be
3200 pushed right at the start of the function. Hence it will be available
3201 for the rest of the prologue.
3202 (*): ie current_function_pretend_args_size is greater than 0. */
3203 if (cfun->machine->uses_anonymous_args
3204 && current_function_pretend_args_size > 0)
3205 return LAST_ARG_REGNUM;
3207 /* The other case is when we have fixed arguments but less than 4 registers
3208 worth. In this case r3 might be used in the body of the function, but
3209 it is not being used to convey an argument into the function. In theory
3210 we could just check current_function_args_size to see how many bytes are
3211 being passed in argument registers, but it seems that it is unreliable.
3212 Sometimes it will have the value 0 when in fact arguments are being
3213 passed. (See testcase execute/20021111-1.c for an example). So we also
3214 check the args_info.nregs field as well. The problem with this field is
3215 that it makes no allowances for arguments that are passed to the
3216 function but which are not used. Hence we could miss an opportunity
3217 when a function has an unused argument in r3. But it is better to be
3218 safe than to be sorry. */
3219 if (! cfun->machine->uses_anonymous_args
3220 && current_function_args_size >= 0
3221 && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3222 && cfun->args_info.nregs < 4)
3223 return LAST_ARG_REGNUM;
3225 /* Otherwise look for a call-saved register that is going to be pushed. */
3226 for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3227 if (pushed_regs_mask & (1 << reg))
3228 return reg;
3230 /* Something went wrong - thumb_compute_save_reg_mask()
3231 should have arranged for a suitable register to be pushed. */
3232 gcc_unreachable ();
3236 /* Generate code to load the PIC register. In thumb mode SCRATCH is a
3237 low register. */
3239 void
3240 arm_load_pic_register (unsigned int scratch)
3242 #ifndef AOF_ASSEMBLER
3243 rtx l1, pic_tmp, pic_tmp2, pic_rtx;
3244 rtx global_offset_table;
3246 if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3247 return;
3249 gcc_assert (flag_pic);
3251 l1 = gen_label_rtx ();
3253 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3254 /* On the ARM the PC register contains 'dot + 8' at the time of the
3255 addition, on the Thumb it is 'dot + 4'. */
3256 pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
3257 if (GOT_PCREL)
3258 pic_tmp2 = gen_rtx_CONST (VOIDmode,
3259 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3260 else
3261 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3263 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3265 if (TARGET_ARM)
3267 emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3268 emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
3270 else
3272 if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3274 /* We will have pushed the pic register, so should always be
3275 able to find a work register. */
3276 pic_tmp = gen_rtx_REG (SImode, scratch);
3277 emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3278 emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3280 else
3281 emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3282 emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
3285 /* Need to emit this whether or not we obey regdecls,
3286 since setjmp/longjmp can cause life info to screw up. */
3287 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3288 #endif /* AOF_ASSEMBLER */
3292 /* Return nonzero if X is valid as an ARM state addressing register. */
3293 static int
3294 arm_address_register_rtx_p (rtx x, int strict_p)
3296 int regno;
3298 if (GET_CODE (x) != REG)
3299 return 0;
3301 regno = REGNO (x);
3303 if (strict_p)
3304 return ARM_REGNO_OK_FOR_BASE_P (regno);
3306 return (regno <= LAST_ARM_REGNUM
3307 || regno >= FIRST_PSEUDO_REGISTER
3308 || regno == FRAME_POINTER_REGNUM
3309 || regno == ARG_POINTER_REGNUM);
3312 /* Return nonzero if X is a valid ARM state address operand. */
3314 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3315 int strict_p)
3317 bool use_ldrd;
3318 enum rtx_code code = GET_CODE (x);
3320 if (arm_address_register_rtx_p (x, strict_p))
3321 return 1;
3323 use_ldrd = (TARGET_LDRD
3324 && (mode == DImode
3325 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3327 if (code == POST_INC || code == PRE_DEC
3328 || ((code == PRE_INC || code == POST_DEC)
3329 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3330 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3332 else if ((code == POST_MODIFY || code == PRE_MODIFY)
3333 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3334 && GET_CODE (XEXP (x, 1)) == PLUS
3335 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3337 rtx addend = XEXP (XEXP (x, 1), 1);
3339 /* Don't allow ldrd post increment by register because it's hard
3340 to fixup invalid register choices. */
3341 if (use_ldrd
3342 && GET_CODE (x) == POST_MODIFY
3343 && GET_CODE (addend) == REG)
3344 return 0;
3346 return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3347 && arm_legitimate_index_p (mode, addend, outer, strict_p));
3350 /* After reload constants split into minipools will have addresses
3351 from a LABEL_REF. */
3352 else if (reload_completed
3353 && (code == LABEL_REF
3354 || (code == CONST
3355 && GET_CODE (XEXP (x, 0)) == PLUS
3356 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3357 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3358 return 1;
3360 else if (mode == TImode)
3361 return 0;
3363 else if (code == PLUS)
3365 rtx xop0 = XEXP (x, 0);
3366 rtx xop1 = XEXP (x, 1);
3368 return ((arm_address_register_rtx_p (xop0, strict_p)
3369 && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3370 || (arm_address_register_rtx_p (xop1, strict_p)
3371 && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3374 #if 0
3375 /* Reload currently can't handle MINUS, so disable this for now */
3376 else if (GET_CODE (x) == MINUS)
3378 rtx xop0 = XEXP (x, 0);
3379 rtx xop1 = XEXP (x, 1);
3381 return (arm_address_register_rtx_p (xop0, strict_p)
3382 && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3384 #endif
3386 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3387 && code == SYMBOL_REF
3388 && CONSTANT_POOL_ADDRESS_P (x)
3389 && ! (flag_pic
3390 && symbol_mentioned_p (get_pool_constant (x))))
3391 return 1;
3393 return 0;
3396 /* Return nonzero if INDEX is valid for an address index operand in
3397 ARM state. */
3398 static int
3399 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3400 int strict_p)
3402 HOST_WIDE_INT range;
3403 enum rtx_code code = GET_CODE (index);
3405 /* Standard coprocessor addressing modes. */
3406 if (TARGET_HARD_FLOAT
3407 && (TARGET_FPA || TARGET_MAVERICK)
3408 && (GET_MODE_CLASS (mode) == MODE_FLOAT
3409 || (TARGET_MAVERICK && mode == DImode)))
3410 return (code == CONST_INT && INTVAL (index) < 1024
3411 && INTVAL (index) > -1024
3412 && (INTVAL (index) & 3) == 0);
3414 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3415 return (code == CONST_INT
3416 && INTVAL (index) < 1024
3417 && INTVAL (index) > -1024
3418 && (INTVAL (index) & 3) == 0);
3420 if (arm_address_register_rtx_p (index, strict_p)
3421 && (GET_MODE_SIZE (mode) <= 4))
3422 return 1;
3424 if (mode == DImode || mode == DFmode)
3426 if (code == CONST_INT)
3428 HOST_WIDE_INT val = INTVAL (index);
3430 if (TARGET_LDRD)
3431 return val > -256 && val < 256;
3432 else
3433 return val > -4096 && val < 4092;
3436 return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3439 if (GET_MODE_SIZE (mode) <= 4
3440 && ! (arm_arch4
3441 && (mode == HImode
3442 || (mode == QImode && outer == SIGN_EXTEND))))
3444 if (code == MULT)
3446 rtx xiop0 = XEXP (index, 0);
3447 rtx xiop1 = XEXP (index, 1);
3449 return ((arm_address_register_rtx_p (xiop0, strict_p)
3450 && power_of_two_operand (xiop1, SImode))
3451 || (arm_address_register_rtx_p (xiop1, strict_p)
3452 && power_of_two_operand (xiop0, SImode)));
3454 else if (code == LSHIFTRT || code == ASHIFTRT
3455 || code == ASHIFT || code == ROTATERT)
3457 rtx op = XEXP (index, 1);
3459 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3460 && GET_CODE (op) == CONST_INT
3461 && INTVAL (op) > 0
3462 && INTVAL (op) <= 31);
3466 /* For ARM v4 we may be doing a sign-extend operation during the
3467 load. */
3468 if (arm_arch4)
3470 if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3471 range = 256;
3472 else
3473 range = 4096;
3475 else
3476 range = (mode == HImode) ? 4095 : 4096;
3478 return (code == CONST_INT
3479 && INTVAL (index) < range
3480 && INTVAL (index) > -range);
3483 /* Return nonzero if X is valid as a Thumb state base register. */
3484 static int
3485 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3487 int regno;
3489 if (GET_CODE (x) != REG)
3490 return 0;
3492 regno = REGNO (x);
3494 if (strict_p)
3495 return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3497 return (regno <= LAST_LO_REGNUM
3498 || regno > LAST_VIRTUAL_REGISTER
3499 || regno == FRAME_POINTER_REGNUM
3500 || (GET_MODE_SIZE (mode) >= 4
3501 && (regno == STACK_POINTER_REGNUM
3502 || regno >= FIRST_PSEUDO_REGISTER
3503 || x == hard_frame_pointer_rtx
3504 || x == arg_pointer_rtx)));
3507 /* Return nonzero if x is a legitimate index register. This is the case
3508 for any base register that can access a QImode object. */
3509 inline static int
3510 thumb_index_register_rtx_p (rtx x, int strict_p)
3512 return thumb_base_register_rtx_p (x, QImode, strict_p);
3515 /* Return nonzero if x is a legitimate Thumb-state address.
3517 The AP may be eliminated to either the SP or the FP, so we use the
3518 least common denominator, e.g. SImode, and offsets from 0 to 64.
3520 ??? Verify whether the above is the right approach.
3522 ??? Also, the FP may be eliminated to the SP, so perhaps that
3523 needs special handling also.
3525 ??? Look at how the mips16 port solves this problem. It probably uses
3526 better ways to solve some of these problems.
3528 Although it is not incorrect, we don't accept QImode and HImode
3529 addresses based on the frame pointer or arg pointer until the
3530 reload pass starts. This is so that eliminating such addresses
3531 into stack based ones won't produce impossible code. */
3533 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3535 /* ??? Not clear if this is right. Experiment. */
3536 if (GET_MODE_SIZE (mode) < 4
3537 && !(reload_in_progress || reload_completed)
3538 && (reg_mentioned_p (frame_pointer_rtx, x)
3539 || reg_mentioned_p (arg_pointer_rtx, x)
3540 || reg_mentioned_p (virtual_incoming_args_rtx, x)
3541 || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3542 || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3543 || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3544 return 0;
3546 /* Accept any base register. SP only in SImode or larger. */
3547 else if (thumb_base_register_rtx_p (x, mode, strict_p))
3548 return 1;
3550 /* This is PC relative data before arm_reorg runs. */
3551 else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3552 && GET_CODE (x) == SYMBOL_REF
3553 && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3554 return 1;
3556 /* This is PC relative data after arm_reorg runs. */
3557 else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3558 && (GET_CODE (x) == LABEL_REF
3559 || (GET_CODE (x) == CONST
3560 && GET_CODE (XEXP (x, 0)) == PLUS
3561 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3562 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3563 return 1;
3565 /* Post-inc indexing only supported for SImode and larger. */
3566 else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3567 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3568 return 1;
3570 else if (GET_CODE (x) == PLUS)
3572 /* REG+REG address can be any two index registers. */
3573 /* We disallow FRAME+REG addressing since we know that FRAME
3574 will be replaced with STACK, and SP relative addressing only
3575 permits SP+OFFSET. */
3576 if (GET_MODE_SIZE (mode) <= 4
3577 && XEXP (x, 0) != frame_pointer_rtx
3578 && XEXP (x, 1) != frame_pointer_rtx
3579 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3580 && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3581 return 1;
3583 /* REG+const has 5-7 bit offset for non-SP registers. */
3584 else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3585 || XEXP (x, 0) == arg_pointer_rtx)
3586 && GET_CODE (XEXP (x, 1)) == CONST_INT
3587 && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3588 return 1;
3590 /* REG+const has 10 bit offset for SP, but only SImode and
3591 larger is supported. */
3592 /* ??? Should probably check for DI/DFmode overflow here
3593 just like GO_IF_LEGITIMATE_OFFSET does. */
3594 else if (GET_CODE (XEXP (x, 0)) == REG
3595 && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3596 && GET_MODE_SIZE (mode) >= 4
3597 && GET_CODE (XEXP (x, 1)) == CONST_INT
3598 && INTVAL (XEXP (x, 1)) >= 0
3599 && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3600 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3601 return 1;
3603 else if (GET_CODE (XEXP (x, 0)) == REG
3604 && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3605 && GET_MODE_SIZE (mode) >= 4
3606 && GET_CODE (XEXP (x, 1)) == CONST_INT
3607 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3608 return 1;
3611 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3612 && GET_MODE_SIZE (mode) == 4
3613 && GET_CODE (x) == SYMBOL_REF
3614 && CONSTANT_POOL_ADDRESS_P (x)
3615 && !(flag_pic
3616 && symbol_mentioned_p (get_pool_constant (x))))
3617 return 1;
3619 return 0;
3622 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3623 instruction of mode MODE. */
3625 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3627 switch (GET_MODE_SIZE (mode))
3629 case 1:
3630 return val >= 0 && val < 32;
3632 case 2:
3633 return val >= 0 && val < 64 && (val & 1) == 0;
3635 default:
3636 return (val >= 0
3637 && (val + GET_MODE_SIZE (mode)) <= 128
3638 && (val & 3) == 0);
3642 /* Try machine-dependent ways of modifying an illegitimate address
3643 to be legitimate. If we find one, return the new, valid address. */
3645 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3647 if (GET_CODE (x) == PLUS)
3649 rtx xop0 = XEXP (x, 0);
3650 rtx xop1 = XEXP (x, 1);
3652 if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3653 xop0 = force_reg (SImode, xop0);
3655 if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3656 xop1 = force_reg (SImode, xop1);
3658 if (ARM_BASE_REGISTER_RTX_P (xop0)
3659 && GET_CODE (xop1) == CONST_INT)
3661 HOST_WIDE_INT n, low_n;
3662 rtx base_reg, val;
3663 n = INTVAL (xop1);
3665 /* VFP addressing modes actually allow greater offsets, but for
3666 now we just stick with the lowest common denominator. */
3667 if (mode == DImode
3668 || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3670 low_n = n & 0x0f;
3671 n &= ~0x0f;
3672 if (low_n > 4)
3674 n += 16;
3675 low_n -= 16;
3678 else
3680 low_n = ((mode) == TImode ? 0
3681 : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3682 n -= low_n;
3685 base_reg = gen_reg_rtx (SImode);
3686 val = force_operand (gen_rtx_PLUS (SImode, xop0,
3687 GEN_INT (n)), NULL_RTX);
3688 emit_move_insn (base_reg, val);
3689 x = (low_n == 0 ? base_reg
3690 : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3692 else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3693 x = gen_rtx_PLUS (SImode, xop0, xop1);
3696 /* XXX We don't allow MINUS any more -- see comment in
3697 arm_legitimate_address_p (). */
3698 else if (GET_CODE (x) == MINUS)
3700 rtx xop0 = XEXP (x, 0);
3701 rtx xop1 = XEXP (x, 1);
3703 if (CONSTANT_P (xop0))
3704 xop0 = force_reg (SImode, xop0);
3706 if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3707 xop1 = force_reg (SImode, xop1);
3709 if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3710 x = gen_rtx_MINUS (SImode, xop0, xop1);
3713 if (flag_pic)
3715 /* We need to find and carefully transform any SYMBOL and LABEL
3716 references; so go back to the original address expression. */
3717 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3719 if (new_x != orig_x)
3720 x = new_x;
3723 return x;
3727 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3728 to be legitimate. If we find one, return the new, valid address. */
3730 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3732 if (GET_CODE (x) == PLUS
3733 && GET_CODE (XEXP (x, 1)) == CONST_INT
3734 && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3735 || INTVAL (XEXP (x, 1)) < 0))
3737 rtx xop0 = XEXP (x, 0);
3738 rtx xop1 = XEXP (x, 1);
3739 HOST_WIDE_INT offset = INTVAL (xop1);
3741 /* Try and fold the offset into a biasing of the base register and
3742 then offsetting that. Don't do this when optimizing for space
3743 since it can cause too many CSEs. */
3744 if (optimize_size && offset >= 0
3745 && offset < 256 + 31 * GET_MODE_SIZE (mode))
3747 HOST_WIDE_INT delta;
3749 if (offset >= 256)
3750 delta = offset - (256 - GET_MODE_SIZE (mode));
3751 else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3752 delta = 31 * GET_MODE_SIZE (mode);
3753 else
3754 delta = offset & (~31 * GET_MODE_SIZE (mode));
3756 xop0 = force_operand (plus_constant (xop0, offset - delta),
3757 NULL_RTX);
3758 x = plus_constant (xop0, delta);
3760 else if (offset < 0 && offset > -256)
3761 /* Small negative offsets are best done with a subtract before the
3762 dereference, forcing these into a register normally takes two
3763 instructions. */
3764 x = force_operand (x, NULL_RTX);
3765 else
3767 /* For the remaining cases, force the constant into a register. */
3768 xop1 = force_reg (SImode, xop1);
3769 x = gen_rtx_PLUS (SImode, xop0, xop1);
3772 else if (GET_CODE (x) == PLUS
3773 && s_register_operand (XEXP (x, 1), SImode)
3774 && !s_register_operand (XEXP (x, 0), SImode))
3776 rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3778 x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3781 if (flag_pic)
3783 /* We need to find and carefully transform any SYMBOL and LABEL
3784 references; so go back to the original address expression. */
3785 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3787 if (new_x != orig_x)
3788 x = new_x;
3791 return x;
3796 #define REG_OR_SUBREG_REG(X) \
3797 (GET_CODE (X) == REG \
3798 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3800 #define REG_OR_SUBREG_RTX(X) \
3801 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3803 #ifndef COSTS_N_INSNS
3804 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3805 #endif
3806 static inline int
3807 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3809 enum machine_mode mode = GET_MODE (x);
3811 switch (code)
3813 case ASHIFT:
3814 case ASHIFTRT:
3815 case LSHIFTRT:
3816 case ROTATERT:
3817 case PLUS:
3818 case MINUS:
3819 case COMPARE:
3820 case NEG:
3821 case NOT:
3822 return COSTS_N_INSNS (1);
3824 case MULT:
3825 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3827 int cycles = 0;
3828 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3830 while (i)
3832 i >>= 2;
3833 cycles++;
3835 return COSTS_N_INSNS (2) + cycles;
3837 return COSTS_N_INSNS (1) + 16;
3839 case SET:
3840 return (COSTS_N_INSNS (1)
3841 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3842 + GET_CODE (SET_DEST (x)) == MEM));
3844 case CONST_INT:
3845 if (outer == SET)
3847 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3848 return 0;
3849 if (thumb_shiftable_const (INTVAL (x)))
3850 return COSTS_N_INSNS (2);
3851 return COSTS_N_INSNS (3);
3853 else if ((outer == PLUS || outer == COMPARE)
3854 && INTVAL (x) < 256 && INTVAL (x) > -256)
3855 return 0;
3856 else if (outer == AND
3857 && INTVAL (x) < 256 && INTVAL (x) >= -256)
3858 return COSTS_N_INSNS (1);
3859 else if (outer == ASHIFT || outer == ASHIFTRT
3860 || outer == LSHIFTRT)
3861 return 0;
3862 return COSTS_N_INSNS (2);
3864 case CONST:
3865 case CONST_DOUBLE:
3866 case LABEL_REF:
3867 case SYMBOL_REF:
3868 return COSTS_N_INSNS (3);
3870 case UDIV:
3871 case UMOD:
3872 case DIV:
3873 case MOD:
3874 return 100;
3876 case TRUNCATE:
3877 return 99;
3879 case AND:
3880 case XOR:
3881 case IOR:
3882 /* XXX guess. */
3883 return 8;
3885 case MEM:
3886 /* XXX another guess. */
3887 /* Memory costs quite a lot for the first word, but subsequent words
3888 load at the equivalent of a single insn each. */
3889 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3890 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3891 ? 4 : 0));
3893 case IF_THEN_ELSE:
3894 /* XXX a guess. */
3895 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3896 return 14;
3897 return 2;
3899 case ZERO_EXTEND:
3900 /* XXX still guessing. */
3901 switch (GET_MODE (XEXP (x, 0)))
3903 case QImode:
3904 return (1 + (mode == DImode ? 4 : 0)
3905 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3907 case HImode:
3908 return (4 + (mode == DImode ? 4 : 0)
3909 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3911 case SImode:
3912 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3914 default:
3915 return 99;
3918 default:
3919 return 99;
3924 /* Worker routine for arm_rtx_costs. */
3925 static inline int
3926 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3928 enum machine_mode mode = GET_MODE (x);
3929 enum rtx_code subcode;
3930 int extra_cost;
3932 switch (code)
3934 case MEM:
3935 /* Memory costs quite a lot for the first word, but subsequent words
3936 load at the equivalent of a single insn each. */
3937 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3938 + (GET_CODE (x) == SYMBOL_REF
3939 && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3941 case DIV:
3942 case MOD:
3943 case UDIV:
3944 case UMOD:
3945 return optimize_size ? COSTS_N_INSNS (2) : 100;
3947 case ROTATE:
3948 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3949 return 4;
3950 /* Fall through */
3951 case ROTATERT:
3952 if (mode != SImode)
3953 return 8;
3954 /* Fall through */
3955 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3956 if (mode == DImode)
3957 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3958 + ((GET_CODE (XEXP (x, 0)) == REG
3959 || (GET_CODE (XEXP (x, 0)) == SUBREG
3960 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3961 ? 0 : 8));
3962 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3963 || (GET_CODE (XEXP (x, 0)) == SUBREG
3964 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3965 ? 0 : 4)
3966 + ((GET_CODE (XEXP (x, 1)) == REG
3967 || (GET_CODE (XEXP (x, 1)) == SUBREG
3968 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3969 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3970 ? 0 : 4));
3972 case MINUS:
3973 if (mode == DImode)
3974 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3975 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3976 || (GET_CODE (XEXP (x, 0)) == CONST_INT
3977 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3978 ? 0 : 8));
3980 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3981 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3982 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3983 && arm_const_double_rtx (XEXP (x, 1))))
3984 ? 0 : 8)
3985 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3986 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3987 && arm_const_double_rtx (XEXP (x, 0))))
3988 ? 0 : 8));
3990 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3991 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3992 && REG_OR_SUBREG_REG (XEXP (x, 1))))
3993 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3994 || subcode == ASHIFTRT || subcode == LSHIFTRT
3995 || subcode == ROTATE || subcode == ROTATERT
3996 || (subcode == MULT
3997 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3998 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3999 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4000 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4001 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4002 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4003 && REG_OR_SUBREG_REG (XEXP (x, 0))))
4004 return 1;
4005 /* Fall through */
4007 case PLUS:
4008 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4009 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4010 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4011 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4012 && arm_const_double_rtx (XEXP (x, 1))))
4013 ? 0 : 8));
4015 /* Fall through */
4016 case AND: case XOR: case IOR:
4017 extra_cost = 0;
4019 /* Normally the frame registers will be spilt into reg+const during
4020 reload, so it is a bad idea to combine them with other instructions,
4021 since then they might not be moved outside of loops. As a compromise
4022 we allow integration with ops that have a constant as their second
4023 operand. */
4024 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
4025 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
4026 && GET_CODE (XEXP (x, 1)) != CONST_INT)
4027 || (REG_OR_SUBREG_REG (XEXP (x, 0))
4028 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
4029 extra_cost = 4;
4031 if (mode == DImode)
4032 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4033 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4034 || (GET_CODE (XEXP (x, 1)) == CONST_INT
4035 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4036 ? 0 : 8));
4038 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
4039 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
4040 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4041 || (GET_CODE (XEXP (x, 1)) == CONST_INT
4042 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4043 ? 0 : 4));
4045 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
4046 return (1 + extra_cost
4047 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
4048 || subcode == LSHIFTRT || subcode == ASHIFTRT
4049 || subcode == ROTATE || subcode == ROTATERT
4050 || (subcode == MULT
4051 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4052 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
4053 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
4054 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
4055 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
4056 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4057 ? 0 : 4));
4059 return 8;
4061 case MULT:
4062 /* This should have been handled by the CPU specific routines. */
4063 gcc_unreachable ();
4065 case TRUNCATE:
4066 if (arm_arch3m && mode == SImode
4067 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4068 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4069 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
4070 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4071 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4072 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
4073 return 8;
4074 return 99;
4076 case NEG:
4077 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4078 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4079 /* Fall through */
4080 case NOT:
4081 if (mode == DImode)
4082 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4084 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4086 case IF_THEN_ELSE:
4087 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4088 return 14;
4089 return 2;
4091 case COMPARE:
4092 return 1;
4094 case ABS:
4095 return 4 + (mode == DImode ? 4 : 0);
4097 case SIGN_EXTEND:
4098 if (GET_MODE (XEXP (x, 0)) == QImode)
4099 return (4 + (mode == DImode ? 4 : 0)
4100 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4101 /* Fall through */
4102 case ZERO_EXTEND:
4103 switch (GET_MODE (XEXP (x, 0)))
4105 case QImode:
4106 return (1 + (mode == DImode ? 4 : 0)
4107 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4109 case HImode:
4110 return (4 + (mode == DImode ? 4 : 0)
4111 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4113 case SImode:
4114 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4116 case V8QImode:
4117 case V4HImode:
4118 case V2SImode:
4119 case V4QImode:
4120 case V2HImode:
4121 return 1;
4123 default:
4124 gcc_unreachable ();
4126 gcc_unreachable ();
4128 case CONST_INT:
4129 if (const_ok_for_arm (INTVAL (x)))
4130 return outer == SET ? 2 : -1;
4131 else if (outer == AND
4132 && const_ok_for_arm (~INTVAL (x)))
4133 return -1;
4134 else if ((outer == COMPARE
4135 || outer == PLUS || outer == MINUS)
4136 && const_ok_for_arm (-INTVAL (x)))
4137 return -1;
4138 else
4139 return 5;
4141 case CONST:
4142 case LABEL_REF:
4143 case SYMBOL_REF:
4144 return 6;
4146 case CONST_DOUBLE:
4147 if (arm_const_double_rtx (x))
4148 return outer == SET ? 2 : -1;
4149 else if ((outer == COMPARE || outer == PLUS)
4150 && neg_const_double_rtx_ok_for_fpa (x))
4151 return -1;
4152 return 7;
4154 default:
4155 return 99;
4159 /* RTX costs when optimizing for size. */
4160 static bool
4161 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4163 enum machine_mode mode = GET_MODE (x);
4165 if (TARGET_THUMB)
4167 /* XXX TBD. For now, use the standard costs. */
4168 *total = thumb_rtx_costs (x, code, outer_code);
4169 return true;
4172 switch (code)
4174 case MEM:
4175 /* A memory access costs 1 insn if the mode is small, or the address is
4176 a single register, otherwise it costs one insn per word. */
4177 if (REG_P (XEXP (x, 0)))
4178 *total = COSTS_N_INSNS (1);
4179 else
4180 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4181 return true;
4183 case DIV:
4184 case MOD:
4185 case UDIV:
4186 case UMOD:
4187 /* Needs a libcall, so it costs about this. */
4188 *total = COSTS_N_INSNS (2);
4189 return false;
4191 case ROTATE:
4192 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4194 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4195 return true;
4197 /* Fall through */
4198 case ROTATERT:
4199 case ASHIFT:
4200 case LSHIFTRT:
4201 case ASHIFTRT:
4202 if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4204 *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4205 return true;
4207 else if (mode == SImode)
4209 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4210 /* Slightly disparage register shifts, but not by much. */
4211 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4212 *total += 1 + rtx_cost (XEXP (x, 1), code);
4213 return true;
4216 /* Needs a libcall. */
4217 *total = COSTS_N_INSNS (2);
4218 return false;
4220 case MINUS:
4221 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4223 *total = COSTS_N_INSNS (1);
4224 return false;
4227 if (mode == SImode)
4229 enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4230 enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4232 if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4233 || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4234 || subcode1 == ROTATE || subcode1 == ROTATERT
4235 || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4236 || subcode1 == ASHIFTRT)
4238 /* It's just the cost of the two operands. */
4239 *total = 0;
4240 return false;
4243 *total = COSTS_N_INSNS (1);
4244 return false;
4247 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4248 return false;
4250 case PLUS:
4251 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4253 *total = COSTS_N_INSNS (1);
4254 return false;
4257 /* Fall through */
4258 case AND: case XOR: case IOR:
4259 if (mode == SImode)
4261 enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4263 if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4264 || subcode == LSHIFTRT || subcode == ASHIFTRT
4265 || (code == AND && subcode == NOT))
4267 /* It's just the cost of the two operands. */
4268 *total = 0;
4269 return false;
4273 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4274 return false;
4276 case MULT:
4277 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4278 return false;
4280 case NEG:
4281 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4282 *total = COSTS_N_INSNS (1);
4283 /* Fall through */
4284 case NOT:
4285 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4287 return false;
4289 case IF_THEN_ELSE:
4290 *total = 0;
4291 return false;
4293 case COMPARE:
4294 if (cc_register (XEXP (x, 0), VOIDmode))
4295 * total = 0;
4296 else
4297 *total = COSTS_N_INSNS (1);
4298 return false;
4300 case ABS:
4301 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4302 *total = COSTS_N_INSNS (1);
4303 else
4304 *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4305 return false;
4307 case SIGN_EXTEND:
4308 *total = 0;
4309 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4311 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4312 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4314 if (mode == DImode)
4315 *total += COSTS_N_INSNS (1);
4316 return false;
4318 case ZERO_EXTEND:
4319 *total = 0;
4320 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4322 switch (GET_MODE (XEXP (x, 0)))
4324 case QImode:
4325 *total += COSTS_N_INSNS (1);
4326 break;
4328 case HImode:
4329 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4331 case SImode:
4332 break;
4334 default:
4335 *total += COSTS_N_INSNS (2);
4339 if (mode == DImode)
4340 *total += COSTS_N_INSNS (1);
4342 return false;
4344 case CONST_INT:
4345 if (const_ok_for_arm (INTVAL (x)))
4346 *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4347 else if (const_ok_for_arm (~INTVAL (x)))
4348 *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4349 else if (const_ok_for_arm (-INTVAL (x)))
4351 if (outer_code == COMPARE || outer_code == PLUS
4352 || outer_code == MINUS)
4353 *total = 0;
4354 else
4355 *total = COSTS_N_INSNS (1);
4357 else
4358 *total = COSTS_N_INSNS (2);
4359 return true;
4361 case CONST:
4362 case LABEL_REF:
4363 case SYMBOL_REF:
4364 *total = COSTS_N_INSNS (2);
4365 return true;
4367 case CONST_DOUBLE:
4368 *total = COSTS_N_INSNS (4);
4369 return true;
4371 default:
4372 if (mode != VOIDmode)
4373 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4374 else
4375 *total = COSTS_N_INSNS (4); /* How knows? */
4376 return false;
4380 /* RTX costs for cores with a slow MUL implementation. */
4382 static bool
4383 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4385 enum machine_mode mode = GET_MODE (x);
4387 if (TARGET_THUMB)
4389 *total = thumb_rtx_costs (x, code, outer_code);
4390 return true;
4393 switch (code)
4395 case MULT:
4396 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4397 || mode == DImode)
4399 *total = 30;
4400 return true;
4403 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4405 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4406 & (unsigned HOST_WIDE_INT) 0xffffffff);
4407 int cost, const_ok = const_ok_for_arm (i);
4408 int j, booth_unit_size;
4410 /* Tune as appropriate. */
4411 cost = const_ok ? 4 : 8;
4412 booth_unit_size = 2;
4413 for (j = 0; i && j < 32; j += booth_unit_size)
4415 i >>= booth_unit_size;
4416 cost += 2;
4419 *total = cost;
4420 return true;
4423 *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4424 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4425 return true;
4427 default:
4428 *total = arm_rtx_costs_1 (x, code, outer_code);
4429 return true;
4434 /* RTX cost for cores with a fast multiply unit (M variants). */
4436 static bool
4437 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4439 enum machine_mode mode = GET_MODE (x);
4441 if (TARGET_THUMB)
4443 *total = thumb_rtx_costs (x, code, outer_code);
4444 return true;
4447 switch (code)
4449 case MULT:
4450 /* There is no point basing this on the tuning, since it is always the
4451 fast variant if it exists at all. */
4452 if (mode == DImode
4453 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4454 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4455 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4457 *total = 8;
4458 return true;
4462 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4463 || mode == DImode)
4465 *total = 30;
4466 return true;
4469 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4471 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4472 & (unsigned HOST_WIDE_INT) 0xffffffff);
4473 int cost, const_ok = const_ok_for_arm (i);
4474 int j, booth_unit_size;
4476 /* Tune as appropriate. */
4477 cost = const_ok ? 4 : 8;
4478 booth_unit_size = 8;
4479 for (j = 0; i && j < 32; j += booth_unit_size)
4481 i >>= booth_unit_size;
4482 cost += 2;
4485 *total = cost;
4486 return true;
4489 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4490 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4491 return true;
4493 default:
4494 *total = arm_rtx_costs_1 (x, code, outer_code);
4495 return true;
4500 /* RTX cost for XScale CPUs. */
4502 static bool
4503 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4505 enum machine_mode mode = GET_MODE (x);
4507 if (TARGET_THUMB)
4509 *total = thumb_rtx_costs (x, code, outer_code);
4510 return true;
4513 switch (code)
4515 case MULT:
4516 /* There is no point basing this on the tuning, since it is always the
4517 fast variant if it exists at all. */
4518 if (mode == DImode
4519 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4520 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4521 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4523 *total = 8;
4524 return true;
4528 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4529 || mode == DImode)
4531 *total = 30;
4532 return true;
4535 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4537 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4538 & (unsigned HOST_WIDE_INT) 0xffffffff);
4539 int cost, const_ok = const_ok_for_arm (i);
4540 unsigned HOST_WIDE_INT masked_const;
4542 /* The cost will be related to two insns.
4543 First a load of the constant (MOV or LDR), then a multiply. */
4544 cost = 2;
4545 if (! const_ok)
4546 cost += 1; /* LDR is probably more expensive because
4547 of longer result latency. */
4548 masked_const = i & 0xffff8000;
4549 if (masked_const != 0 && masked_const != 0xffff8000)
4551 masked_const = i & 0xf8000000;
4552 if (masked_const == 0 || masked_const == 0xf8000000)
4553 cost += 1;
4554 else
4555 cost += 2;
4557 *total = cost;
4558 return true;
4561 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4562 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4563 return true;
4565 case COMPARE:
4566 /* A COMPARE of a MULT is slow on XScale; the muls instruction
4567 will stall until the multiplication is complete. */
4568 if (GET_CODE (XEXP (x, 0)) == MULT)
4569 *total = 4 + rtx_cost (XEXP (x, 0), code);
4570 else
4571 *total = arm_rtx_costs_1 (x, code, outer_code);
4572 return true;
4574 default:
4575 *total = arm_rtx_costs_1 (x, code, outer_code);
4576 return true;
4581 /* RTX costs for 9e (and later) cores. */
4583 static bool
4584 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4586 enum machine_mode mode = GET_MODE (x);
4587 int nonreg_cost;
4588 int cost;
4590 if (TARGET_THUMB)
4592 switch (code)
4594 case MULT:
4595 *total = COSTS_N_INSNS (3);
4596 return true;
4598 default:
4599 *total = thumb_rtx_costs (x, code, outer_code);
4600 return true;
4604 switch (code)
4606 case MULT:
4607 /* There is no point basing this on the tuning, since it is always the
4608 fast variant if it exists at all. */
4609 if (mode == DImode
4610 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4611 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4612 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4614 *total = 3;
4615 return true;
4619 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4621 *total = 30;
4622 return true;
4624 if (mode == DImode)
4626 cost = 7;
4627 nonreg_cost = 8;
4629 else
4631 cost = 2;
4632 nonreg_cost = 4;
4636 *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4637 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4638 return true;
4640 default:
4641 *total = arm_rtx_costs_1 (x, code, outer_code);
4642 return true;
4645 /* All address computations that can be done are free, but rtx cost returns
4646 the same for practically all of them. So we weight the different types
4647 of address here in the order (most pref first):
4648 PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
4649 static inline int
4650 arm_arm_address_cost (rtx x)
4652 enum rtx_code c = GET_CODE (x);
4654 if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4655 return 0;
4656 if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4657 return 10;
4659 if (c == PLUS || c == MINUS)
4661 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4662 return 2;
4664 if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4665 return 3;
4667 return 4;
4670 return 6;
4673 static inline int
4674 arm_thumb_address_cost (rtx x)
4676 enum rtx_code c = GET_CODE (x);
4678 if (c == REG)
4679 return 1;
4680 if (c == PLUS
4681 && GET_CODE (XEXP (x, 0)) == REG
4682 && GET_CODE (XEXP (x, 1)) == CONST_INT)
4683 return 1;
4685 return 2;
4688 static int
4689 arm_address_cost (rtx x)
4691 return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4694 static int
4695 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4697 rtx i_pat, d_pat;
4699 /* Some true dependencies can have a higher cost depending
4700 on precisely how certain input operands are used. */
4701 if (arm_tune_xscale
4702 && REG_NOTE_KIND (link) == 0
4703 && recog_memoized (insn) >= 0
4704 && recog_memoized (dep) >= 0)
4706 int shift_opnum = get_attr_shift (insn);
4707 enum attr_type attr_type = get_attr_type (dep);
4709 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4710 operand for INSN. If we have a shifted input operand and the
4711 instruction we depend on is another ALU instruction, then we may
4712 have to account for an additional stall. */
4713 if (shift_opnum != 0
4714 && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4716 rtx shifted_operand;
4717 int opno;
4719 /* Get the shifted operand. */
4720 extract_insn (insn);
4721 shifted_operand = recog_data.operand[shift_opnum];
4723 /* Iterate over all the operands in DEP. If we write an operand
4724 that overlaps with SHIFTED_OPERAND, then we have increase the
4725 cost of this dependency. */
4726 extract_insn (dep);
4727 preprocess_constraints ();
4728 for (opno = 0; opno < recog_data.n_operands; opno++)
4730 /* We can ignore strict inputs. */
4731 if (recog_data.operand_type[opno] == OP_IN)
4732 continue;
4734 if (reg_overlap_mentioned_p (recog_data.operand[opno],
4735 shifted_operand))
4736 return 2;
4741 /* XXX This is not strictly true for the FPA. */
4742 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4743 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4744 return 0;
4746 /* Call insns don't incur a stall, even if they follow a load. */
4747 if (REG_NOTE_KIND (link) == 0
4748 && GET_CODE (insn) == CALL_INSN)
4749 return 1;
4751 if ((i_pat = single_set (insn)) != NULL
4752 && GET_CODE (SET_SRC (i_pat)) == MEM
4753 && (d_pat = single_set (dep)) != NULL
4754 && GET_CODE (SET_DEST (d_pat)) == MEM)
4756 rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4757 /* This is a load after a store, there is no conflict if the load reads
4758 from a cached area. Assume that loads from the stack, and from the
4759 constant pool are cached, and that others will miss. This is a
4760 hack. */
4762 if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4763 || reg_mentioned_p (stack_pointer_rtx, src_mem)
4764 || reg_mentioned_p (frame_pointer_rtx, src_mem)
4765 || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4766 return 1;
4769 return cost;
4772 static int fp_consts_inited = 0;
4774 /* Only zero is valid for VFP. Other values are also valid for FPA. */
4775 static const char * const strings_fp[8] =
4777 "0", "1", "2", "3",
4778 "4", "5", "0.5", "10"
4781 static REAL_VALUE_TYPE values_fp[8];
4783 static void
4784 init_fp_table (void)
4786 int i;
4787 REAL_VALUE_TYPE r;
4789 if (TARGET_VFP)
4790 fp_consts_inited = 1;
4791 else
4792 fp_consts_inited = 8;
4794 for (i = 0; i < fp_consts_inited; i++)
4796 r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4797 values_fp[i] = r;
4801 /* Return TRUE if rtx X is a valid immediate FP constant. */
4803 arm_const_double_rtx (rtx x)
4805 REAL_VALUE_TYPE r;
4806 int i;
4808 if (!fp_consts_inited)
4809 init_fp_table ();
4811 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4812 if (REAL_VALUE_MINUS_ZERO (r))
4813 return 0;
4815 for (i = 0; i < fp_consts_inited; i++)
4816 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4817 return 1;
4819 return 0;
4822 /* Return TRUE if rtx X is a valid immediate FPA constant. */
4824 neg_const_double_rtx_ok_for_fpa (rtx x)
4826 REAL_VALUE_TYPE r;
4827 int i;
4829 if (!fp_consts_inited)
4830 init_fp_table ();
4832 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4833 r = REAL_VALUE_NEGATE (r);
4834 if (REAL_VALUE_MINUS_ZERO (r))
4835 return 0;
4837 for (i = 0; i < 8; i++)
4838 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4839 return 1;
4841 return 0;
4844 /* Predicates for `match_operand' and `match_operator'. */
4846 /* Return nonzero if OP is a valid Cirrus memory address pattern. */
4848 cirrus_memory_offset (rtx op)
4850 /* Reject eliminable registers. */
4851 if (! (reload_in_progress || reload_completed)
4852 && ( reg_mentioned_p (frame_pointer_rtx, op)
4853 || reg_mentioned_p (arg_pointer_rtx, op)
4854 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4855 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4856 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4857 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4858 return 0;
4860 if (GET_CODE (op) == MEM)
4862 rtx ind;
4864 ind = XEXP (op, 0);
4866 /* Match: (mem (reg)). */
4867 if (GET_CODE (ind) == REG)
4868 return 1;
4870 /* Match:
4871 (mem (plus (reg)
4872 (const))). */
4873 if (GET_CODE (ind) == PLUS
4874 && GET_CODE (XEXP (ind, 0)) == REG
4875 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4876 && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4877 return 1;
4880 return 0;
4883 /* Return TRUE if OP is a valid VFP memory address pattern.
4884 WB if true if writeback address modes are allowed. */
4887 arm_coproc_mem_operand (rtx op, bool wb)
4889 rtx ind;
4891 /* Reject eliminable registers. */
4892 if (! (reload_in_progress || reload_completed)
4893 && ( reg_mentioned_p (frame_pointer_rtx, op)
4894 || reg_mentioned_p (arg_pointer_rtx, op)
4895 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4896 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4897 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4898 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4899 return FALSE;
4901 /* Constants are converted into offsets from labels. */
4902 if (GET_CODE (op) != MEM)
4903 return FALSE;
4905 ind = XEXP (op, 0);
4907 if (reload_completed
4908 && (GET_CODE (ind) == LABEL_REF
4909 || (GET_CODE (ind) == CONST
4910 && GET_CODE (XEXP (ind, 0)) == PLUS
4911 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4912 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4913 return TRUE;
4915 /* Match: (mem (reg)). */
4916 if (GET_CODE (ind) == REG)
4917 return arm_address_register_rtx_p (ind, 0);
4919 /* Autoincremment addressing modes. */
4920 if (wb
4921 && (GET_CODE (ind) == PRE_INC
4922 || GET_CODE (ind) == POST_INC
4923 || GET_CODE (ind) == PRE_DEC
4924 || GET_CODE (ind) == POST_DEC))
4925 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
4927 if (wb
4928 && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
4929 && arm_address_register_rtx_p (XEXP (ind, 0), 0)
4930 && GET_CODE (XEXP (ind, 1)) == PLUS
4931 && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
4932 ind = XEXP (ind, 1);
4934 /* Match:
4935 (plus (reg)
4936 (const)). */
4937 if (GET_CODE (ind) == PLUS
4938 && GET_CODE (XEXP (ind, 0)) == REG
4939 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4940 && GET_CODE (XEXP (ind, 1)) == CONST_INT
4941 && INTVAL (XEXP (ind, 1)) > -1024
4942 && INTVAL (XEXP (ind, 1)) < 1024
4943 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
4944 return TRUE;
4946 return FALSE;
4949 /* Return true if X is a register that will be eliminated later on. */
4951 arm_eliminable_register (rtx x)
4953 return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
4954 || REGNO (x) == ARG_POINTER_REGNUM
4955 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
4956 && REGNO (x) <= LAST_VIRTUAL_REGISTER));
4959 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4960 VFP registers. Otherwise return NO_REGS. */
4962 enum reg_class
4963 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4965 if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
4966 return NO_REGS;
4968 return GENERAL_REGS;
4971 /* Values which must be returned in the most-significant end of the return
4972 register. */
4974 static bool
4975 arm_return_in_msb (tree valtype)
4977 return (TARGET_AAPCS_BASED
4978 && BYTES_BIG_ENDIAN
4979 && (AGGREGATE_TYPE_P (valtype)
4980 || TREE_CODE (valtype) == COMPLEX_TYPE));
4983 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4984 Use by the Cirrus Maverick code which has to workaround
4985 a hardware bug triggered by such instructions. */
4986 static bool
4987 arm_memory_load_p (rtx insn)
4989 rtx body, lhs, rhs;;
4991 if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4992 return false;
4994 body = PATTERN (insn);
4996 if (GET_CODE (body) != SET)
4997 return false;
4999 lhs = XEXP (body, 0);
5000 rhs = XEXP (body, 1);
5002 lhs = REG_OR_SUBREG_RTX (lhs);
5004 /* If the destination is not a general purpose
5005 register we do not have to worry. */
5006 if (GET_CODE (lhs) != REG
5007 || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
5008 return false;
5010 /* As well as loads from memory we also have to react
5011 to loads of invalid constants which will be turned
5012 into loads from the minipool. */
5013 return (GET_CODE (rhs) == MEM
5014 || GET_CODE (rhs) == SYMBOL_REF
5015 || note_invalid_constants (insn, -1, false));
5018 /* Return TRUE if INSN is a Cirrus instruction. */
5019 static bool
5020 arm_cirrus_insn_p (rtx insn)
5022 enum attr_cirrus attr;
5024 /* get_attr cannot accept USE or CLOBBER. */
5025 if (!insn
5026 || GET_CODE (insn) != INSN
5027 || GET_CODE (PATTERN (insn)) == USE
5028 || GET_CODE (PATTERN (insn)) == CLOBBER)
5029 return 0;
5031 attr = get_attr_cirrus (insn);
5033 return attr != CIRRUS_NOT;
5036 /* Cirrus reorg for invalid instruction combinations. */
5037 static void
5038 cirrus_reorg (rtx first)
5040 enum attr_cirrus attr;
5041 rtx body = PATTERN (first);
5042 rtx t;
5043 int nops;
5045 /* Any branch must be followed by 2 non Cirrus instructions. */
5046 if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
5048 nops = 0;
5049 t = next_nonnote_insn (first);
5051 if (arm_cirrus_insn_p (t))
5052 ++ nops;
5054 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5055 ++ nops;
5057 while (nops --)
5058 emit_insn_after (gen_nop (), first);
5060 return;
5063 /* (float (blah)) is in parallel with a clobber. */
5064 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
5065 body = XVECEXP (body, 0, 0);
5067 if (GET_CODE (body) == SET)
5069 rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
5071 /* cfldrd, cfldr64, cfstrd, cfstr64 must
5072 be followed by a non Cirrus insn. */
5073 if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
5075 if (arm_cirrus_insn_p (next_nonnote_insn (first)))
5076 emit_insn_after (gen_nop (), first);
5078 return;
5080 else if (arm_memory_load_p (first))
5082 unsigned int arm_regno;
5084 /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
5085 ldr/cfmv64hr combination where the Rd field is the same
5086 in both instructions must be split with a non Cirrus
5087 insn. Example:
5089 ldr r0, blah
5091 cfmvsr mvf0, r0. */
5093 /* Get Arm register number for ldr insn. */
5094 if (GET_CODE (lhs) == REG)
5095 arm_regno = REGNO (lhs);
5096 else
5098 gcc_assert (GET_CODE (rhs) == REG);
5099 arm_regno = REGNO (rhs);
5102 /* Next insn. */
5103 first = next_nonnote_insn (first);
5105 if (! arm_cirrus_insn_p (first))
5106 return;
5108 body = PATTERN (first);
5110 /* (float (blah)) is in parallel with a clobber. */
5111 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5112 body = XVECEXP (body, 0, 0);
5114 if (GET_CODE (body) == FLOAT)
5115 body = XEXP (body, 0);
5117 if (get_attr_cirrus (first) == CIRRUS_MOVE
5118 && GET_CODE (XEXP (body, 1)) == REG
5119 && arm_regno == REGNO (XEXP (body, 1)))
5120 emit_insn_after (gen_nop (), first);
5122 return;
5126 /* get_attr cannot accept USE or CLOBBER. */
5127 if (!first
5128 || GET_CODE (first) != INSN
5129 || GET_CODE (PATTERN (first)) == USE
5130 || GET_CODE (PATTERN (first)) == CLOBBER)
5131 return;
5133 attr = get_attr_cirrus (first);
5135 /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5136 must be followed by a non-coprocessor instruction. */
5137 if (attr == CIRRUS_COMPARE)
5139 nops = 0;
5141 t = next_nonnote_insn (first);
5143 if (arm_cirrus_insn_p (t))
5144 ++ nops;
5146 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5147 ++ nops;
5149 while (nops --)
5150 emit_insn_after (gen_nop (), first);
5152 return;
5156 /* Return TRUE if X references a SYMBOL_REF. */
5158 symbol_mentioned_p (rtx x)
5160 const char * fmt;
5161 int i;
5163 if (GET_CODE (x) == SYMBOL_REF)
5164 return 1;
5166 fmt = GET_RTX_FORMAT (GET_CODE (x));
5168 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5170 if (fmt[i] == 'E')
5172 int j;
5174 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5175 if (symbol_mentioned_p (XVECEXP (x, i, j)))
5176 return 1;
5178 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5179 return 1;
5182 return 0;
5185 /* Return TRUE if X references a LABEL_REF. */
5187 label_mentioned_p (rtx x)
5189 const char * fmt;
5190 int i;
5192 if (GET_CODE (x) == LABEL_REF)
5193 return 1;
5195 fmt = GET_RTX_FORMAT (GET_CODE (x));
5196 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5198 if (fmt[i] == 'E')
5200 int j;
5202 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5203 if (label_mentioned_p (XVECEXP (x, i, j)))
5204 return 1;
5206 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5207 return 1;
5210 return 0;
5213 enum rtx_code
5214 minmax_code (rtx x)
5216 enum rtx_code code = GET_CODE (x);
5218 switch (code)
5220 case SMAX:
5221 return GE;
5222 case SMIN:
5223 return LE;
5224 case UMIN:
5225 return LEU;
5226 case UMAX:
5227 return GEU;
5228 default:
5229 gcc_unreachable ();
5233 /* Return 1 if memory locations are adjacent. */
5235 adjacent_mem_locations (rtx a, rtx b)
5237 /* We don't guarantee to preserve the order of these memory refs. */
5238 if (volatile_refs_p (a) || volatile_refs_p (b))
5239 return 0;
5241 if ((GET_CODE (XEXP (a, 0)) == REG
5242 || (GET_CODE (XEXP (a, 0)) == PLUS
5243 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5244 && (GET_CODE (XEXP (b, 0)) == REG
5245 || (GET_CODE (XEXP (b, 0)) == PLUS
5246 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5248 HOST_WIDE_INT val0 = 0, val1 = 0;
5249 rtx reg0, reg1;
5250 int val_diff;
5252 if (GET_CODE (XEXP (a, 0)) == PLUS)
5254 reg0 = XEXP (XEXP (a, 0), 0);
5255 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5257 else
5258 reg0 = XEXP (a, 0);
5260 if (GET_CODE (XEXP (b, 0)) == PLUS)
5262 reg1 = XEXP (XEXP (b, 0), 0);
5263 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5265 else
5266 reg1 = XEXP (b, 0);
5268 /* Don't accept any offset that will require multiple
5269 instructions to handle, since this would cause the
5270 arith_adjacentmem pattern to output an overlong sequence. */
5271 if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5272 return 0;
5274 /* Don't allow an eliminable register: register elimination can make
5275 the offset too large. */
5276 if (arm_eliminable_register (reg0))
5277 return 0;
5279 val_diff = val1 - val0;
5281 if (arm_ld_sched)
5283 /* If the target has load delay slots, then there's no benefit
5284 to using an ldm instruction unless the offset is zero and
5285 we are optimizing for size. */
5286 return (optimize_size && (REGNO (reg0) == REGNO (reg1))
5287 && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
5288 && (val_diff == 4 || val_diff == -4));
5291 return ((REGNO (reg0) == REGNO (reg1))
5292 && (val_diff == 4 || val_diff == -4));
5295 return 0;
5299 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5300 HOST_WIDE_INT *load_offset)
5302 int unsorted_regs[4];
5303 HOST_WIDE_INT unsorted_offsets[4];
5304 int order[4];
5305 int base_reg = -1;
5306 int i;
5308 /* Can only handle 2, 3, or 4 insns at present,
5309 though could be easily extended if required. */
5310 gcc_assert (nops >= 2 && nops <= 4);
5312 /* Loop over the operands and check that the memory references are
5313 suitable (i.e. immediate offsets from the same base register). At
5314 the same time, extract the target register, and the memory
5315 offsets. */
5316 for (i = 0; i < nops; i++)
5318 rtx reg;
5319 rtx offset;
5321 /* Convert a subreg of a mem into the mem itself. */
5322 if (GET_CODE (operands[nops + i]) == SUBREG)
5323 operands[nops + i] = alter_subreg (operands + (nops + i));
5325 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
5327 /* Don't reorder volatile memory references; it doesn't seem worth
5328 looking for the case where the order is ok anyway. */
5329 if (MEM_VOLATILE_P (operands[nops + i]))
5330 return 0;
5332 offset = const0_rtx;
5334 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5335 || (GET_CODE (reg) == SUBREG
5336 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5337 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5338 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5339 == REG)
5340 || (GET_CODE (reg) == SUBREG
5341 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5342 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5343 == CONST_INT)))
5345 if (i == 0)
5347 base_reg = REGNO (reg);
5348 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5349 ? REGNO (operands[i])
5350 : REGNO (SUBREG_REG (operands[i])));
5351 order[0] = 0;
5353 else
5355 if (base_reg != (int) REGNO (reg))
5356 /* Not addressed from the same base register. */
5357 return 0;
5359 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5360 ? REGNO (operands[i])
5361 : REGNO (SUBREG_REG (operands[i])));
5362 if (unsorted_regs[i] < unsorted_regs[order[0]])
5363 order[0] = i;
5366 /* If it isn't an integer register, or if it overwrites the
5367 base register but isn't the last insn in the list, then
5368 we can't do this. */
5369 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5370 || (i != nops - 1 && unsorted_regs[i] == base_reg))
5371 return 0;
5373 unsorted_offsets[i] = INTVAL (offset);
5375 else
5376 /* Not a suitable memory address. */
5377 return 0;
5380 /* All the useful information has now been extracted from the
5381 operands into unsorted_regs and unsorted_offsets; additionally,
5382 order[0] has been set to the lowest numbered register in the
5383 list. Sort the registers into order, and check that the memory
5384 offsets are ascending and adjacent. */
5386 for (i = 1; i < nops; i++)
5388 int j;
5390 order[i] = order[i - 1];
5391 for (j = 0; j < nops; j++)
5392 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5393 && (order[i] == order[i - 1]
5394 || unsorted_regs[j] < unsorted_regs[order[i]]))
5395 order[i] = j;
5397 /* Have we found a suitable register? if not, one must be used more
5398 than once. */
5399 if (order[i] == order[i - 1])
5400 return 0;
5402 /* Is the memory address adjacent and ascending? */
5403 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5404 return 0;
5407 if (base)
5409 *base = base_reg;
5411 for (i = 0; i < nops; i++)
5412 regs[i] = unsorted_regs[order[i]];
5414 *load_offset = unsorted_offsets[order[0]];
5417 if (unsorted_offsets[order[0]] == 0)
5418 return 1; /* ldmia */
5420 if (unsorted_offsets[order[0]] == 4)
5421 return 2; /* ldmib */
5423 if (unsorted_offsets[order[nops - 1]] == 0)
5424 return 3; /* ldmda */
5426 if (unsorted_offsets[order[nops - 1]] == -4)
5427 return 4; /* ldmdb */
5429 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5430 if the offset isn't small enough. The reason 2 ldrs are faster
5431 is because these ARMs are able to do more than one cache access
5432 in a single cycle. The ARM9 and StrongARM have Harvard caches,
5433 whilst the ARM8 has a double bandwidth cache. This means that
5434 these cores can do both an instruction fetch and a data fetch in
5435 a single cycle, so the trick of calculating the address into a
5436 scratch register (one of the result regs) and then doing a load
5437 multiple actually becomes slower (and no smaller in code size).
5438 That is the transformation
5440 ldr rd1, [rbase + offset]
5441 ldr rd2, [rbase + offset + 4]
5445 add rd1, rbase, offset
5446 ldmia rd1, {rd1, rd2}
5448 produces worse code -- '3 cycles + any stalls on rd2' instead of
5449 '2 cycles + any stalls on rd2'. On ARMs with only one cache
5450 access per cycle, the first sequence could never complete in less
5451 than 6 cycles, whereas the ldm sequence would only take 5 and
5452 would make better use of sequential accesses if not hitting the
5453 cache.
5455 We cheat here and test 'arm_ld_sched' which we currently know to
5456 only be true for the ARM8, ARM9 and StrongARM. If this ever
5457 changes, then the test below needs to be reworked. */
5458 if (nops == 2 && arm_ld_sched)
5459 return 0;
5461 /* Can't do it without setting up the offset, only do this if it takes
5462 no more than one insn. */
5463 return (const_ok_for_arm (unsorted_offsets[order[0]])
5464 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5467 const char *
5468 emit_ldm_seq (rtx *operands, int nops)
5470 int regs[4];
5471 int base_reg;
5472 HOST_WIDE_INT offset;
5473 char buf[100];
5474 int i;
5476 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5478 case 1:
5479 strcpy (buf, "ldm%?ia\t");
5480 break;
5482 case 2:
5483 strcpy (buf, "ldm%?ib\t");
5484 break;
5486 case 3:
5487 strcpy (buf, "ldm%?da\t");
5488 break;
5490 case 4:
5491 strcpy (buf, "ldm%?db\t");
5492 break;
5494 case 5:
5495 if (offset >= 0)
5496 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5497 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5498 (long) offset);
5499 else
5500 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5501 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5502 (long) -offset);
5503 output_asm_insn (buf, operands);
5504 base_reg = regs[0];
5505 strcpy (buf, "ldm%?ia\t");
5506 break;
5508 default:
5509 gcc_unreachable ();
5512 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5513 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5515 for (i = 1; i < nops; i++)
5516 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5517 reg_names[regs[i]]);
5519 strcat (buf, "}\t%@ phole ldm");
5521 output_asm_insn (buf, operands);
5522 return "";
5526 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5527 HOST_WIDE_INT * load_offset)
5529 int unsorted_regs[4];
5530 HOST_WIDE_INT unsorted_offsets[4];
5531 int order[4];
5532 int base_reg = -1;
5533 int i;
5535 /* Can only handle 2, 3, or 4 insns at present, though could be easily
5536 extended if required. */
5537 gcc_assert (nops >= 2 && nops <= 4);
5539 /* Loop over the operands and check that the memory references are
5540 suitable (i.e. immediate offsets from the same base register). At
5541 the same time, extract the target register, and the memory
5542 offsets. */
5543 for (i = 0; i < nops; i++)
5545 rtx reg;
5546 rtx offset;
5548 /* Convert a subreg of a mem into the mem itself. */
5549 if (GET_CODE (operands[nops + i]) == SUBREG)
5550 operands[nops + i] = alter_subreg (operands + (nops + i));
5552 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
5554 /* Don't reorder volatile memory references; it doesn't seem worth
5555 looking for the case where the order is ok anyway. */
5556 if (MEM_VOLATILE_P (operands[nops + i]))
5557 return 0;
5559 offset = const0_rtx;
5561 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5562 || (GET_CODE (reg) == SUBREG
5563 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5564 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5565 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5566 == REG)
5567 || (GET_CODE (reg) == SUBREG
5568 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5569 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5570 == CONST_INT)))
5572 if (i == 0)
5574 base_reg = REGNO (reg);
5575 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5576 ? REGNO (operands[i])
5577 : REGNO (SUBREG_REG (operands[i])));
5578 order[0] = 0;
5580 else
5582 if (base_reg != (int) REGNO (reg))
5583 /* Not addressed from the same base register. */
5584 return 0;
5586 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5587 ? REGNO (operands[i])
5588 : REGNO (SUBREG_REG (operands[i])));
5589 if (unsorted_regs[i] < unsorted_regs[order[0]])
5590 order[0] = i;
5593 /* If it isn't an integer register, then we can't do this. */
5594 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5595 return 0;
5597 unsorted_offsets[i] = INTVAL (offset);
5599 else
5600 /* Not a suitable memory address. */
5601 return 0;
5604 /* All the useful information has now been extracted from the
5605 operands into unsorted_regs and unsorted_offsets; additionally,
5606 order[0] has been set to the lowest numbered register in the
5607 list. Sort the registers into order, and check that the memory
5608 offsets are ascending and adjacent. */
5610 for (i = 1; i < nops; i++)
5612 int j;
5614 order[i] = order[i - 1];
5615 for (j = 0; j < nops; j++)
5616 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5617 && (order[i] == order[i - 1]
5618 || unsorted_regs[j] < unsorted_regs[order[i]]))
5619 order[i] = j;
5621 /* Have we found a suitable register? if not, one must be used more
5622 than once. */
5623 if (order[i] == order[i - 1])
5624 return 0;
5626 /* Is the memory address adjacent and ascending? */
5627 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5628 return 0;
5631 if (base)
5633 *base = base_reg;
5635 for (i = 0; i < nops; i++)
5636 regs[i] = unsorted_regs[order[i]];
5638 *load_offset = unsorted_offsets[order[0]];
5641 if (unsorted_offsets[order[0]] == 0)
5642 return 1; /* stmia */
5644 if (unsorted_offsets[order[0]] == 4)
5645 return 2; /* stmib */
5647 if (unsorted_offsets[order[nops - 1]] == 0)
5648 return 3; /* stmda */
5650 if (unsorted_offsets[order[nops - 1]] == -4)
5651 return 4; /* stmdb */
5653 return 0;
5656 const char *
5657 emit_stm_seq (rtx *operands, int nops)
5659 int regs[4];
5660 int base_reg;
5661 HOST_WIDE_INT offset;
5662 char buf[100];
5663 int i;
5665 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5667 case 1:
5668 strcpy (buf, "stm%?ia\t");
5669 break;
5671 case 2:
5672 strcpy (buf, "stm%?ib\t");
5673 break;
5675 case 3:
5676 strcpy (buf, "stm%?da\t");
5677 break;
5679 case 4:
5680 strcpy (buf, "stm%?db\t");
5681 break;
5683 default:
5684 gcc_unreachable ();
5687 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5688 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5690 for (i = 1; i < nops; i++)
5691 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5692 reg_names[regs[i]]);
5694 strcat (buf, "}\t%@ phole stm");
5696 output_asm_insn (buf, operands);
5697 return "";
5701 /* Routines for use in generating RTL. */
5704 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5705 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5707 HOST_WIDE_INT offset = *offsetp;
5708 int i = 0, j;
5709 rtx result;
5710 int sign = up ? 1 : -1;
5711 rtx mem, addr;
5713 /* XScale has load-store double instructions, but they have stricter
5714 alignment requirements than load-store multiple, so we cannot
5715 use them.
5717 For XScale ldm requires 2 + NREGS cycles to complete and blocks
5718 the pipeline until completion.
5720 NREGS CYCLES
5726 An ldr instruction takes 1-3 cycles, but does not block the
5727 pipeline.
5729 NREGS CYCLES
5730 1 1-3
5731 2 2-6
5732 3 3-9
5733 4 4-12
5735 Best case ldr will always win. However, the more ldr instructions
5736 we issue, the less likely we are to be able to schedule them well.
5737 Using ldr instructions also increases code size.
5739 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5740 for counts of 3 or 4 regs. */
5741 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5743 rtx seq;
5745 start_sequence ();
5747 for (i = 0; i < count; i++)
5749 addr = plus_constant (from, i * 4 * sign);
5750 mem = adjust_automodify_address (basemem, SImode, addr, offset);
5751 emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5752 offset += 4 * sign;
5755 if (write_back)
5757 emit_move_insn (from, plus_constant (from, count * 4 * sign));
5758 *offsetp = offset;
5761 seq = get_insns ();
5762 end_sequence ();
5764 return seq;
5767 result = gen_rtx_PARALLEL (VOIDmode,
5768 rtvec_alloc (count + (write_back ? 1 : 0)));
5769 if (write_back)
5771 XVECEXP (result, 0, 0)
5772 = gen_rtx_SET (GET_MODE (from), from,
5773 plus_constant (from, count * 4 * sign));
5774 i = 1;
5775 count++;
5778 for (j = 0; i < count; i++, j++)
5780 addr = plus_constant (from, j * 4 * sign);
5781 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5782 XVECEXP (result, 0, i)
5783 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5784 offset += 4 * sign;
5787 if (write_back)
5788 *offsetp = offset;
5790 return result;
5794 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5795 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5797 HOST_WIDE_INT offset = *offsetp;
5798 int i = 0, j;
5799 rtx result;
5800 int sign = up ? 1 : -1;
5801 rtx mem, addr;
5803 /* See arm_gen_load_multiple for discussion of
5804 the pros/cons of ldm/stm usage for XScale. */
5805 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5807 rtx seq;
5809 start_sequence ();
5811 for (i = 0; i < count; i++)
5813 addr = plus_constant (to, i * 4 * sign);
5814 mem = adjust_automodify_address (basemem, SImode, addr, offset);
5815 emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5816 offset += 4 * sign;
5819 if (write_back)
5821 emit_move_insn (to, plus_constant (to, count * 4 * sign));
5822 *offsetp = offset;
5825 seq = get_insns ();
5826 end_sequence ();
5828 return seq;
5831 result = gen_rtx_PARALLEL (VOIDmode,
5832 rtvec_alloc (count + (write_back ? 1 : 0)));
5833 if (write_back)
5835 XVECEXP (result, 0, 0)
5836 = gen_rtx_SET (GET_MODE (to), to,
5837 plus_constant (to, count * 4 * sign));
5838 i = 1;
5839 count++;
5842 for (j = 0; i < count; i++, j++)
5844 addr = plus_constant (to, j * 4 * sign);
5845 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5846 XVECEXP (result, 0, i)
5847 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5848 offset += 4 * sign;
5851 if (write_back)
5852 *offsetp = offset;
5854 return result;
5858 arm_gen_movmemqi (rtx *operands)
5860 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5861 HOST_WIDE_INT srcoffset, dstoffset;
5862 int i;
5863 rtx src, dst, srcbase, dstbase;
5864 rtx part_bytes_reg = NULL;
5865 rtx mem;
5867 if (GET_CODE (operands[2]) != CONST_INT
5868 || GET_CODE (operands[3]) != CONST_INT
5869 || INTVAL (operands[2]) > 64
5870 || INTVAL (operands[3]) & 3)
5871 return 0;
5873 dstbase = operands[0];
5874 srcbase = operands[1];
5876 dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
5877 src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
5879 in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5880 out_words_to_go = INTVAL (operands[2]) / 4;
5881 last_bytes = INTVAL (operands[2]) & 3;
5882 dstoffset = srcoffset = 0;
5884 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5885 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5887 for (i = 0; in_words_to_go >= 2; i+=4)
5889 if (in_words_to_go > 4)
5890 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5891 srcbase, &srcoffset));
5892 else
5893 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
5894 FALSE, srcbase, &srcoffset));
5896 if (out_words_to_go)
5898 if (out_words_to_go > 4)
5899 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5900 dstbase, &dstoffset));
5901 else if (out_words_to_go != 1)
5902 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5903 dst, TRUE,
5904 (last_bytes == 0
5905 ? FALSE : TRUE),
5906 dstbase, &dstoffset));
5907 else
5909 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5910 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5911 if (last_bytes != 0)
5913 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5914 dstoffset += 4;
5919 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5920 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
5923 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
5924 if (out_words_to_go)
5926 rtx sreg;
5928 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5929 sreg = copy_to_reg (mem);
5931 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5932 emit_move_insn (mem, sreg);
5933 in_words_to_go--;
5935 gcc_assert (!in_words_to_go); /* Sanity check */
5938 if (in_words_to_go)
5940 gcc_assert (in_words_to_go > 0);
5942 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5943 part_bytes_reg = copy_to_mode_reg (SImode, mem);
5946 gcc_assert (!last_bytes || part_bytes_reg);
5948 if (BYTES_BIG_ENDIAN && last_bytes)
5950 rtx tmp = gen_reg_rtx (SImode);
5952 /* The bytes we want are in the top end of the word. */
5953 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
5954 GEN_INT (8 * (4 - last_bytes))));
5955 part_bytes_reg = tmp;
5957 while (last_bytes)
5959 mem = adjust_automodify_address (dstbase, QImode,
5960 plus_constant (dst, last_bytes - 1),
5961 dstoffset + last_bytes - 1);
5962 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5964 if (--last_bytes)
5966 tmp = gen_reg_rtx (SImode);
5967 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
5968 part_bytes_reg = tmp;
5973 else
5975 if (last_bytes > 1)
5977 mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
5978 emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
5979 last_bytes -= 2;
5980 if (last_bytes)
5982 rtx tmp = gen_reg_rtx (SImode);
5983 emit_insn (gen_addsi3 (dst, dst, const2_rtx));
5984 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
5985 part_bytes_reg = tmp;
5986 dstoffset += 2;
5990 if (last_bytes)
5992 mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
5993 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5997 return 1;
6000 /* Generate a memory reference for a half word, such that it will be loaded
6001 into the top 16 bits of the word. We can assume that the address is
6002 known to be alignable and of the form reg, or plus (reg, const). */
6005 arm_gen_rotated_half_load (rtx memref)
6007 HOST_WIDE_INT offset = 0;
6008 rtx base = XEXP (memref, 0);
6010 if (GET_CODE (base) == PLUS)
6012 offset = INTVAL (XEXP (base, 1));
6013 base = XEXP (base, 0);
6016 /* If we aren't allowed to generate unaligned addresses, then fail. */
6017 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0))
6018 return NULL;
6020 base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
6022 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
6023 return base;
6025 return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
6028 /* Select a dominance comparison mode if possible for a test of the general
6029 form (OP (COND_OR (X) (Y)) (const_int 0)). We support three forms.
6030 COND_OR == DOM_CC_X_AND_Y => (X && Y)
6031 COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6032 COND_OR == DOM_CC_X_OR_Y => (X || Y)
6033 In all cases OP will be either EQ or NE, but we don't need to know which
6034 here. If we are unable to support a dominance comparison we return
6035 CC mode. This will then fail to match for the RTL expressions that
6036 generate this call. */
6037 enum machine_mode
6038 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6040 enum rtx_code cond1, cond2;
6041 int swapped = 0;
6043 /* Currently we will probably get the wrong result if the individual
6044 comparisons are not simple. This also ensures that it is safe to
6045 reverse a comparison if necessary. */
6046 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6047 != CCmode)
6048 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6049 != CCmode))
6050 return CCmode;
6052 /* The if_then_else variant of this tests the second condition if the
6053 first passes, but is true if the first fails. Reverse the first
6054 condition to get a true "inclusive-or" expression. */
6055 if (cond_or == DOM_CC_NX_OR_Y)
6056 cond1 = reverse_condition (cond1);
6058 /* If the comparisons are not equal, and one doesn't dominate the other,
6059 then we can't do this. */
6060 if (cond1 != cond2
6061 && !comparison_dominates_p (cond1, cond2)
6062 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6063 return CCmode;
6065 if (swapped)
6067 enum rtx_code temp = cond1;
6068 cond1 = cond2;
6069 cond2 = temp;
6072 switch (cond1)
6074 case EQ:
6075 if (cond_or == DOM_CC_X_AND_Y)
6076 return CC_DEQmode;
6078 switch (cond2)
6080 case EQ: return CC_DEQmode;
6081 case LE: return CC_DLEmode;
6082 case LEU: return CC_DLEUmode;
6083 case GE: return CC_DGEmode;
6084 case GEU: return CC_DGEUmode;
6085 default: gcc_unreachable ();
6088 case LT:
6089 if (cond_or == DOM_CC_X_AND_Y)
6090 return CC_DLTmode;
6092 switch (cond2)
6094 case LT:
6095 return CC_DLTmode;
6096 case LE:
6097 return CC_DLEmode;
6098 case NE:
6099 return CC_DNEmode;
6100 default:
6101 gcc_unreachable ();
6104 case GT:
6105 if (cond_or == DOM_CC_X_AND_Y)
6106 return CC_DGTmode;
6108 switch (cond2)
6110 case GT:
6111 return CC_DGTmode;
6112 case GE:
6113 return CC_DGEmode;
6114 case NE:
6115 return CC_DNEmode;
6116 default:
6117 gcc_unreachable ();
6120 case LTU:
6121 if (cond_or == DOM_CC_X_AND_Y)
6122 return CC_DLTUmode;
6124 switch (cond2)
6126 case LTU:
6127 return CC_DLTUmode;
6128 case LEU:
6129 return CC_DLEUmode;
6130 case NE:
6131 return CC_DNEmode;
6132 default:
6133 gcc_unreachable ();
6136 case GTU:
6137 if (cond_or == DOM_CC_X_AND_Y)
6138 return CC_DGTUmode;
6140 switch (cond2)
6142 case GTU:
6143 return CC_DGTUmode;
6144 case GEU:
6145 return CC_DGEUmode;
6146 case NE:
6147 return CC_DNEmode;
6148 default:
6149 gcc_unreachable ();
6152 /* The remaining cases only occur when both comparisons are the
6153 same. */
6154 case NE:
6155 gcc_assert (cond1 == cond2);
6156 return CC_DNEmode;
6158 case LE:
6159 gcc_assert (cond1 == cond2);
6160 return CC_DLEmode;
6162 case GE:
6163 gcc_assert (cond1 == cond2);
6164 return CC_DGEmode;
6166 case LEU:
6167 gcc_assert (cond1 == cond2);
6168 return CC_DLEUmode;
6170 case GEU:
6171 gcc_assert (cond1 == cond2);
6172 return CC_DGEUmode;
6174 default:
6175 gcc_unreachable ();
6179 enum machine_mode
6180 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6182 /* All floating point compares return CCFP if it is an equality
6183 comparison, and CCFPE otherwise. */
6184 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6186 switch (op)
6188 case EQ:
6189 case NE:
6190 case UNORDERED:
6191 case ORDERED:
6192 case UNLT:
6193 case UNLE:
6194 case UNGT:
6195 case UNGE:
6196 case UNEQ:
6197 case LTGT:
6198 return CCFPmode;
6200 case LT:
6201 case LE:
6202 case GT:
6203 case GE:
6204 if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6205 return CCFPmode;
6206 return CCFPEmode;
6208 default:
6209 gcc_unreachable ();
6213 /* A compare with a shifted operand. Because of canonicalization, the
6214 comparison will have to be swapped when we emit the assembler. */
6215 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6216 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6217 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6218 || GET_CODE (x) == ROTATERT))
6219 return CC_SWPmode;
6221 /* This operation is performed swapped, but since we only rely on the Z
6222 flag we don't need an additional mode. */
6223 if (GET_MODE (y) == SImode && REG_P (y)
6224 && GET_CODE (x) == NEG
6225 && (op == EQ || op == NE))
6226 return CC_Zmode;
6228 /* This is a special case that is used by combine to allow a
6229 comparison of a shifted byte load to be split into a zero-extend
6230 followed by a comparison of the shifted integer (only valid for
6231 equalities and unsigned inequalities). */
6232 if (GET_MODE (x) == SImode
6233 && GET_CODE (x) == ASHIFT
6234 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6235 && GET_CODE (XEXP (x, 0)) == SUBREG
6236 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6237 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6238 && (op == EQ || op == NE
6239 || op == GEU || op == GTU || op == LTU || op == LEU)
6240 && GET_CODE (y) == CONST_INT)
6241 return CC_Zmode;
6243 /* A construct for a conditional compare, if the false arm contains
6244 0, then both conditions must be true, otherwise either condition
6245 must be true. Not all conditions are possible, so CCmode is
6246 returned if it can't be done. */
6247 if (GET_CODE (x) == IF_THEN_ELSE
6248 && (XEXP (x, 2) == const0_rtx
6249 || XEXP (x, 2) == const1_rtx)
6250 && COMPARISON_P (XEXP (x, 0))
6251 && COMPARISON_P (XEXP (x, 1)))
6252 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6253 INTVAL (XEXP (x, 2)));
6255 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
6256 if (GET_CODE (x) == AND
6257 && COMPARISON_P (XEXP (x, 0))
6258 && COMPARISON_P (XEXP (x, 1)))
6259 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6260 DOM_CC_X_AND_Y);
6262 if (GET_CODE (x) == IOR
6263 && COMPARISON_P (XEXP (x, 0))
6264 && COMPARISON_P (XEXP (x, 1)))
6265 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6266 DOM_CC_X_OR_Y);
6268 /* An operation (on Thumb) where we want to test for a single bit.
6269 This is done by shifting that bit up into the top bit of a
6270 scratch register; we can then branch on the sign bit. */
6271 if (TARGET_THUMB
6272 && GET_MODE (x) == SImode
6273 && (op == EQ || op == NE)
6274 && (GET_CODE (x) == ZERO_EXTRACT))
6275 return CC_Nmode;
6277 /* An operation that sets the condition codes as a side-effect, the
6278 V flag is not set correctly, so we can only use comparisons where
6279 this doesn't matter. (For LT and GE we can use "mi" and "pl"
6280 instead.) */
6281 if (GET_MODE (x) == SImode
6282 && y == const0_rtx
6283 && (op == EQ || op == NE || op == LT || op == GE)
6284 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6285 || GET_CODE (x) == AND || GET_CODE (x) == IOR
6286 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6287 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6288 || GET_CODE (x) == LSHIFTRT
6289 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6290 || GET_CODE (x) == ROTATERT
6291 || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6292 return CC_NOOVmode;
6294 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6295 return CC_Zmode;
6297 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6298 && GET_CODE (x) == PLUS
6299 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6300 return CC_Cmode;
6302 return CCmode;
6305 /* X and Y are two things to compare using CODE. Emit the compare insn and
6306 return the rtx for register 0 in the proper mode. FP means this is a
6307 floating point compare: I don't think that it is needed on the arm. */
6309 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6311 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6312 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6314 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6315 gen_rtx_COMPARE (mode, x, y)));
6317 return cc_reg;
6320 /* Generate a sequence of insns that will generate the correct return
6321 address mask depending on the physical architecture that the program
6322 is running on. */
6324 arm_gen_return_addr_mask (void)
6326 rtx reg = gen_reg_rtx (Pmode);
6328 emit_insn (gen_return_addr_mask (reg));
6329 return reg;
6332 void
6333 arm_reload_in_hi (rtx *operands)
6335 rtx ref = operands[1];
6336 rtx base, scratch;
6337 HOST_WIDE_INT offset = 0;
6339 if (GET_CODE (ref) == SUBREG)
6341 offset = SUBREG_BYTE (ref);
6342 ref = SUBREG_REG (ref);
6345 if (GET_CODE (ref) == REG)
6347 /* We have a pseudo which has been spilt onto the stack; there
6348 are two cases here: the first where there is a simple
6349 stack-slot replacement and a second where the stack-slot is
6350 out of range, or is used as a subreg. */
6351 if (reg_equiv_mem[REGNO (ref)])
6353 ref = reg_equiv_mem[REGNO (ref)];
6354 base = find_replacement (&XEXP (ref, 0));
6356 else
6357 /* The slot is out of range, or was dressed up in a SUBREG. */
6358 base = reg_equiv_address[REGNO (ref)];
6360 else
6361 base = find_replacement (&XEXP (ref, 0));
6363 /* Handle the case where the address is too complex to be offset by 1. */
6364 if (GET_CODE (base) == MINUS
6365 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6367 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6369 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6370 base = base_plus;
6372 else if (GET_CODE (base) == PLUS)
6374 /* The addend must be CONST_INT, or we would have dealt with it above. */
6375 HOST_WIDE_INT hi, lo;
6377 offset += INTVAL (XEXP (base, 1));
6378 base = XEXP (base, 0);
6380 /* Rework the address into a legal sequence of insns. */
6381 /* Valid range for lo is -4095 -> 4095 */
6382 lo = (offset >= 0
6383 ? (offset & 0xfff)
6384 : -((-offset) & 0xfff));
6386 /* Corner case, if lo is the max offset then we would be out of range
6387 once we have added the additional 1 below, so bump the msb into the
6388 pre-loading insn(s). */
6389 if (lo == 4095)
6390 lo &= 0x7ff;
6392 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6393 ^ (HOST_WIDE_INT) 0x80000000)
6394 - (HOST_WIDE_INT) 0x80000000);
6396 gcc_assert (hi + lo == offset);
6398 if (hi != 0)
6400 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6402 /* Get the base address; addsi3 knows how to handle constants
6403 that require more than one insn. */
6404 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6405 base = base_plus;
6406 offset = lo;
6410 /* Operands[2] may overlap operands[0] (though it won't overlap
6411 operands[1]), that's why we asked for a DImode reg -- so we can
6412 use the bit that does not overlap. */
6413 if (REGNO (operands[2]) == REGNO (operands[0]))
6414 scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6415 else
6416 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6418 emit_insn (gen_zero_extendqisi2 (scratch,
6419 gen_rtx_MEM (QImode,
6420 plus_constant (base,
6421 offset))));
6422 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6423 gen_rtx_MEM (QImode,
6424 plus_constant (base,
6425 offset + 1))));
6426 if (!BYTES_BIG_ENDIAN)
6427 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6428 gen_rtx_IOR (SImode,
6429 gen_rtx_ASHIFT
6430 (SImode,
6431 gen_rtx_SUBREG (SImode, operands[0], 0),
6432 GEN_INT (8)),
6433 scratch)));
6434 else
6435 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6436 gen_rtx_IOR (SImode,
6437 gen_rtx_ASHIFT (SImode, scratch,
6438 GEN_INT (8)),
6439 gen_rtx_SUBREG (SImode, operands[0],
6440 0))));
6443 /* Handle storing a half-word to memory during reload by synthesizing as two
6444 byte stores. Take care not to clobber the input values until after we
6445 have moved them somewhere safe. This code assumes that if the DImode
6446 scratch in operands[2] overlaps either the input value or output address
6447 in some way, then that value must die in this insn (we absolutely need
6448 two scratch registers for some corner cases). */
6449 void
6450 arm_reload_out_hi (rtx *operands)
6452 rtx ref = operands[0];
6453 rtx outval = operands[1];
6454 rtx base, scratch;
6455 HOST_WIDE_INT offset = 0;
6457 if (GET_CODE (ref) == SUBREG)
6459 offset = SUBREG_BYTE (ref);
6460 ref = SUBREG_REG (ref);
6463 if (GET_CODE (ref) == REG)
6465 /* We have a pseudo which has been spilt onto the stack; there
6466 are two cases here: the first where there is a simple
6467 stack-slot replacement and a second where the stack-slot is
6468 out of range, or is used as a subreg. */
6469 if (reg_equiv_mem[REGNO (ref)])
6471 ref = reg_equiv_mem[REGNO (ref)];
6472 base = find_replacement (&XEXP (ref, 0));
6474 else
6475 /* The slot is out of range, or was dressed up in a SUBREG. */
6476 base = reg_equiv_address[REGNO (ref)];
6478 else
6479 base = find_replacement (&XEXP (ref, 0));
6481 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6483 /* Handle the case where the address is too complex to be offset by 1. */
6484 if (GET_CODE (base) == MINUS
6485 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6487 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6489 /* Be careful not to destroy OUTVAL. */
6490 if (reg_overlap_mentioned_p (base_plus, outval))
6492 /* Updating base_plus might destroy outval, see if we can
6493 swap the scratch and base_plus. */
6494 if (!reg_overlap_mentioned_p (scratch, outval))
6496 rtx tmp = scratch;
6497 scratch = base_plus;
6498 base_plus = tmp;
6500 else
6502 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6504 /* Be conservative and copy OUTVAL into the scratch now,
6505 this should only be necessary if outval is a subreg
6506 of something larger than a word. */
6507 /* XXX Might this clobber base? I can't see how it can,
6508 since scratch is known to overlap with OUTVAL, and
6509 must be wider than a word. */
6510 emit_insn (gen_movhi (scratch_hi, outval));
6511 outval = scratch_hi;
6515 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6516 base = base_plus;
6518 else if (GET_CODE (base) == PLUS)
6520 /* The addend must be CONST_INT, or we would have dealt with it above. */
6521 HOST_WIDE_INT hi, lo;
6523 offset += INTVAL (XEXP (base, 1));
6524 base = XEXP (base, 0);
6526 /* Rework the address into a legal sequence of insns. */
6527 /* Valid range for lo is -4095 -> 4095 */
6528 lo = (offset >= 0
6529 ? (offset & 0xfff)
6530 : -((-offset) & 0xfff));
6532 /* Corner case, if lo is the max offset then we would be out of range
6533 once we have added the additional 1 below, so bump the msb into the
6534 pre-loading insn(s). */
6535 if (lo == 4095)
6536 lo &= 0x7ff;
6538 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6539 ^ (HOST_WIDE_INT) 0x80000000)
6540 - (HOST_WIDE_INT) 0x80000000);
6542 gcc_assert (hi + lo == offset);
6544 if (hi != 0)
6546 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6548 /* Be careful not to destroy OUTVAL. */
6549 if (reg_overlap_mentioned_p (base_plus, outval))
6551 /* Updating base_plus might destroy outval, see if we
6552 can swap the scratch and base_plus. */
6553 if (!reg_overlap_mentioned_p (scratch, outval))
6555 rtx tmp = scratch;
6556 scratch = base_plus;
6557 base_plus = tmp;
6559 else
6561 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6563 /* Be conservative and copy outval into scratch now,
6564 this should only be necessary if outval is a
6565 subreg of something larger than a word. */
6566 /* XXX Might this clobber base? I can't see how it
6567 can, since scratch is known to overlap with
6568 outval. */
6569 emit_insn (gen_movhi (scratch_hi, outval));
6570 outval = scratch_hi;
6574 /* Get the base address; addsi3 knows how to handle constants
6575 that require more than one insn. */
6576 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6577 base = base_plus;
6578 offset = lo;
6582 if (BYTES_BIG_ENDIAN)
6584 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6585 plus_constant (base, offset + 1)),
6586 gen_lowpart (QImode, outval)));
6587 emit_insn (gen_lshrsi3 (scratch,
6588 gen_rtx_SUBREG (SImode, outval, 0),
6589 GEN_INT (8)));
6590 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6591 gen_lowpart (QImode, scratch)));
6593 else
6595 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6596 gen_lowpart (QImode, outval)));
6597 emit_insn (gen_lshrsi3 (scratch,
6598 gen_rtx_SUBREG (SImode, outval, 0),
6599 GEN_INT (8)));
6600 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6601 plus_constant (base, offset + 1)),
6602 gen_lowpart (QImode, scratch)));
6606 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
6607 (padded to the size of a word) should be passed in a register. */
6609 static bool
6610 arm_must_pass_in_stack (enum machine_mode mode, tree type)
6612 if (TARGET_AAPCS_BASED)
6613 return must_pass_in_stack_var_size (mode, type);
6614 else
6615 return must_pass_in_stack_var_size_or_pad (mode, type);
6619 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
6620 Return true if an argument passed on the stack should be padded upwards,
6621 i.e. if the least-significant byte has useful data. */
6623 bool
6624 arm_pad_arg_upward (enum machine_mode mode, tree type)
6626 if (!TARGET_AAPCS_BASED)
6627 return DEFAULT_FUNCTION_ARG_PADDING(mode, type);
6629 if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
6630 return false;
6632 return true;
6636 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
6637 For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
6638 byte of the register has useful data, and return the opposite if the
6639 most significant byte does.
6640 For AAPCS, small aggregates and small complex types are always padded
6641 upwards. */
6643 bool
6644 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
6645 tree type, int first ATTRIBUTE_UNUSED)
6647 if (TARGET_AAPCS_BASED
6648 && BYTES_BIG_ENDIAN
6649 && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
6650 && int_size_in_bytes (type) <= 4)
6651 return true;
6653 /* Otherwise, use default padding. */
6654 return !BYTES_BIG_ENDIAN;
6659 /* Print a symbolic form of X to the debug file, F. */
6660 static void
6661 arm_print_value (FILE *f, rtx x)
6663 switch (GET_CODE (x))
6665 case CONST_INT:
6666 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6667 return;
6669 case CONST_DOUBLE:
6670 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6671 return;
6673 case CONST_VECTOR:
6675 int i;
6677 fprintf (f, "<");
6678 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6680 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6681 if (i < (CONST_VECTOR_NUNITS (x) - 1))
6682 fputc (',', f);
6684 fprintf (f, ">");
6686 return;
6688 case CONST_STRING:
6689 fprintf (f, "\"%s\"", XSTR (x, 0));
6690 return;
6692 case SYMBOL_REF:
6693 fprintf (f, "`%s'", XSTR (x, 0));
6694 return;
6696 case LABEL_REF:
6697 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6698 return;
6700 case CONST:
6701 arm_print_value (f, XEXP (x, 0));
6702 return;
6704 case PLUS:
6705 arm_print_value (f, XEXP (x, 0));
6706 fprintf (f, "+");
6707 arm_print_value (f, XEXP (x, 1));
6708 return;
6710 case PC:
6711 fprintf (f, "pc");
6712 return;
6714 default:
6715 fprintf (f, "????");
6716 return;
6720 /* Routines for manipulation of the constant pool. */
6722 /* Arm instructions cannot load a large constant directly into a
6723 register; they have to come from a pc relative load. The constant
6724 must therefore be placed in the addressable range of the pc
6725 relative load. Depending on the precise pc relative load
6726 instruction the range is somewhere between 256 bytes and 4k. This
6727 means that we often have to dump a constant inside a function, and
6728 generate code to branch around it.
6730 It is important to minimize this, since the branches will slow
6731 things down and make the code larger.
6733 Normally we can hide the table after an existing unconditional
6734 branch so that there is no interruption of the flow, but in the
6735 worst case the code looks like this:
6737 ldr rn, L1
6739 b L2
6740 align
6741 L1: .long value
6745 ldr rn, L3
6747 b L4
6748 align
6749 L3: .long value
6753 We fix this by performing a scan after scheduling, which notices
6754 which instructions need to have their operands fetched from the
6755 constant table and builds the table.
6757 The algorithm starts by building a table of all the constants that
6758 need fixing up and all the natural barriers in the function (places
6759 where a constant table can be dropped without breaking the flow).
6760 For each fixup we note how far the pc-relative replacement will be
6761 able to reach and the offset of the instruction into the function.
6763 Having built the table we then group the fixes together to form
6764 tables that are as large as possible (subject to addressing
6765 constraints) and emit each table of constants after the last
6766 barrier that is within range of all the instructions in the group.
6767 If a group does not contain a barrier, then we forcibly create one
6768 by inserting a jump instruction into the flow. Once the table has
6769 been inserted, the insns are then modified to reference the
6770 relevant entry in the pool.
6772 Possible enhancements to the algorithm (not implemented) are:
6774 1) For some processors and object formats, there may be benefit in
6775 aligning the pools to the start of cache lines; this alignment
6776 would need to be taken into account when calculating addressability
6777 of a pool. */
6779 /* These typedefs are located at the start of this file, so that
6780 they can be used in the prototypes there. This comment is to
6781 remind readers of that fact so that the following structures
6782 can be understood more easily.
6784 typedef struct minipool_node Mnode;
6785 typedef struct minipool_fixup Mfix; */
6787 struct minipool_node
6789 /* Doubly linked chain of entries. */
6790 Mnode * next;
6791 Mnode * prev;
6792 /* The maximum offset into the code that this entry can be placed. While
6793 pushing fixes for forward references, all entries are sorted in order
6794 of increasing max_address. */
6795 HOST_WIDE_INT max_address;
6796 /* Similarly for an entry inserted for a backwards ref. */
6797 HOST_WIDE_INT min_address;
6798 /* The number of fixes referencing this entry. This can become zero
6799 if we "unpush" an entry. In this case we ignore the entry when we
6800 come to emit the code. */
6801 int refcount;
6802 /* The offset from the start of the minipool. */
6803 HOST_WIDE_INT offset;
6804 /* The value in table. */
6805 rtx value;
6806 /* The mode of value. */
6807 enum machine_mode mode;
6808 /* The size of the value. With iWMMXt enabled
6809 sizes > 4 also imply an alignment of 8-bytes. */
6810 int fix_size;
6813 struct minipool_fixup
6815 Mfix * next;
6816 rtx insn;
6817 HOST_WIDE_INT address;
6818 rtx * loc;
6819 enum machine_mode mode;
6820 int fix_size;
6821 rtx value;
6822 Mnode * minipool;
6823 HOST_WIDE_INT forwards;
6824 HOST_WIDE_INT backwards;
6827 /* Fixes less than a word need padding out to a word boundary. */
6828 #define MINIPOOL_FIX_SIZE(mode) \
6829 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6831 static Mnode * minipool_vector_head;
6832 static Mnode * minipool_vector_tail;
6833 static rtx minipool_vector_label;
6835 /* The linked list of all minipool fixes required for this function. */
6836 Mfix * minipool_fix_head;
6837 Mfix * minipool_fix_tail;
6838 /* The fix entry for the current minipool, once it has been placed. */
6839 Mfix * minipool_barrier;
6841 /* Determines if INSN is the start of a jump table. Returns the end
6842 of the TABLE or NULL_RTX. */
6843 static rtx
6844 is_jump_table (rtx insn)
6846 rtx table;
6848 if (GET_CODE (insn) == JUMP_INSN
6849 && JUMP_LABEL (insn) != NULL
6850 && ((table = next_real_insn (JUMP_LABEL (insn)))
6851 == next_real_insn (insn))
6852 && table != NULL
6853 && GET_CODE (table) == JUMP_INSN
6854 && (GET_CODE (PATTERN (table)) == ADDR_VEC
6855 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6856 return table;
6858 return NULL_RTX;
6861 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6862 #define JUMP_TABLES_IN_TEXT_SECTION 0
6863 #endif
6865 static HOST_WIDE_INT
6866 get_jump_table_size (rtx insn)
6868 /* ADDR_VECs only take room if read-only data does into the text
6869 section. */
6870 if (JUMP_TABLES_IN_TEXT_SECTION
6871 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6872 || 1
6873 #endif
6876 rtx body = PATTERN (insn);
6877 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6879 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6882 return 0;
6885 /* Move a minipool fix MP from its current location to before MAX_MP.
6886 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6887 constraints may need updating. */
6888 static Mnode *
6889 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6890 HOST_WIDE_INT max_address)
6892 /* The code below assumes these are different. */
6893 gcc_assert (mp != max_mp);
6895 if (max_mp == NULL)
6897 if (max_address < mp->max_address)
6898 mp->max_address = max_address;
6900 else
6902 if (max_address > max_mp->max_address - mp->fix_size)
6903 mp->max_address = max_mp->max_address - mp->fix_size;
6904 else
6905 mp->max_address = max_address;
6907 /* Unlink MP from its current position. Since max_mp is non-null,
6908 mp->prev must be non-null. */
6909 mp->prev->next = mp->next;
6910 if (mp->next != NULL)
6911 mp->next->prev = mp->prev;
6912 else
6913 minipool_vector_tail = mp->prev;
6915 /* Re-insert it before MAX_MP. */
6916 mp->next = max_mp;
6917 mp->prev = max_mp->prev;
6918 max_mp->prev = mp;
6920 if (mp->prev != NULL)
6921 mp->prev->next = mp;
6922 else
6923 minipool_vector_head = mp;
6926 /* Save the new entry. */
6927 max_mp = mp;
6929 /* Scan over the preceding entries and adjust their addresses as
6930 required. */
6931 while (mp->prev != NULL
6932 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6934 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6935 mp = mp->prev;
6938 return max_mp;
6941 /* Add a constant to the minipool for a forward reference. Returns the
6942 node added or NULL if the constant will not fit in this pool. */
6943 static Mnode *
6944 add_minipool_forward_ref (Mfix *fix)
6946 /* If set, max_mp is the first pool_entry that has a lower
6947 constraint than the one we are trying to add. */
6948 Mnode * max_mp = NULL;
6949 HOST_WIDE_INT max_address = fix->address + fix->forwards;
6950 Mnode * mp;
6952 /* If this fix's address is greater than the address of the first
6953 entry, then we can't put the fix in this pool. We subtract the
6954 size of the current fix to ensure that if the table is fully
6955 packed we still have enough room to insert this value by suffling
6956 the other fixes forwards. */
6957 if (minipool_vector_head &&
6958 fix->address >= minipool_vector_head->max_address - fix->fix_size)
6959 return NULL;
6961 /* Scan the pool to see if a constant with the same value has
6962 already been added. While we are doing this, also note the
6963 location where we must insert the constant if it doesn't already
6964 exist. */
6965 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6967 if (GET_CODE (fix->value) == GET_CODE (mp->value)
6968 && fix->mode == mp->mode
6969 && (GET_CODE (fix->value) != CODE_LABEL
6970 || (CODE_LABEL_NUMBER (fix->value)
6971 == CODE_LABEL_NUMBER (mp->value)))
6972 && rtx_equal_p (fix->value, mp->value))
6974 /* More than one fix references this entry. */
6975 mp->refcount++;
6976 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6979 /* Note the insertion point if necessary. */
6980 if (max_mp == NULL
6981 && mp->max_address > max_address)
6982 max_mp = mp;
6984 /* If we are inserting an 8-bytes aligned quantity and
6985 we have not already found an insertion point, then
6986 make sure that all such 8-byte aligned quantities are
6987 placed at the start of the pool. */
6988 if (ARM_DOUBLEWORD_ALIGN
6989 && max_mp == NULL
6990 && fix->fix_size == 8
6991 && mp->fix_size != 8)
6993 max_mp = mp;
6994 max_address = mp->max_address;
6998 /* The value is not currently in the minipool, so we need to create
6999 a new entry for it. If MAX_MP is NULL, the entry will be put on
7000 the end of the list since the placement is less constrained than
7001 any existing entry. Otherwise, we insert the new fix before
7002 MAX_MP and, if necessary, adjust the constraints on the other
7003 entries. */
7004 mp = xmalloc (sizeof (* mp));
7005 mp->fix_size = fix->fix_size;
7006 mp->mode = fix->mode;
7007 mp->value = fix->value;
7008 mp->refcount = 1;
7009 /* Not yet required for a backwards ref. */
7010 mp->min_address = -65536;
7012 if (max_mp == NULL)
7014 mp->max_address = max_address;
7015 mp->next = NULL;
7016 mp->prev = minipool_vector_tail;
7018 if (mp->prev == NULL)
7020 minipool_vector_head = mp;
7021 minipool_vector_label = gen_label_rtx ();
7023 else
7024 mp->prev->next = mp;
7026 minipool_vector_tail = mp;
7028 else
7030 if (max_address > max_mp->max_address - mp->fix_size)
7031 mp->max_address = max_mp->max_address - mp->fix_size;
7032 else
7033 mp->max_address = max_address;
7035 mp->next = max_mp;
7036 mp->prev = max_mp->prev;
7037 max_mp->prev = mp;
7038 if (mp->prev != NULL)
7039 mp->prev->next = mp;
7040 else
7041 minipool_vector_head = mp;
7044 /* Save the new entry. */
7045 max_mp = mp;
7047 /* Scan over the preceding entries and adjust their addresses as
7048 required. */
7049 while (mp->prev != NULL
7050 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7052 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7053 mp = mp->prev;
7056 return max_mp;
7059 static Mnode *
7060 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7061 HOST_WIDE_INT min_address)
7063 HOST_WIDE_INT offset;
7065 /* The code below assumes these are different. */
7066 gcc_assert (mp != min_mp);
7068 if (min_mp == NULL)
7070 if (min_address > mp->min_address)
7071 mp->min_address = min_address;
7073 else
7075 /* We will adjust this below if it is too loose. */
7076 mp->min_address = min_address;
7078 /* Unlink MP from its current position. Since min_mp is non-null,
7079 mp->next must be non-null. */
7080 mp->next->prev = mp->prev;
7081 if (mp->prev != NULL)
7082 mp->prev->next = mp->next;
7083 else
7084 minipool_vector_head = mp->next;
7086 /* Reinsert it after MIN_MP. */
7087 mp->prev = min_mp;
7088 mp->next = min_mp->next;
7089 min_mp->next = mp;
7090 if (mp->next != NULL)
7091 mp->next->prev = mp;
7092 else
7093 minipool_vector_tail = mp;
7096 min_mp = mp;
7098 offset = 0;
7099 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7101 mp->offset = offset;
7102 if (mp->refcount > 0)
7103 offset += mp->fix_size;
7105 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7106 mp->next->min_address = mp->min_address + mp->fix_size;
7109 return min_mp;
7112 /* Add a constant to the minipool for a backward reference. Returns the
7113 node added or NULL if the constant will not fit in this pool.
7115 Note that the code for insertion for a backwards reference can be
7116 somewhat confusing because the calculated offsets for each fix do
7117 not take into account the size of the pool (which is still under
7118 construction. */
7119 static Mnode *
7120 add_minipool_backward_ref (Mfix *fix)
7122 /* If set, min_mp is the last pool_entry that has a lower constraint
7123 than the one we are trying to add. */
7124 Mnode *min_mp = NULL;
7125 /* This can be negative, since it is only a constraint. */
7126 HOST_WIDE_INT min_address = fix->address - fix->backwards;
7127 Mnode *mp;
7129 /* If we can't reach the current pool from this insn, or if we can't
7130 insert this entry at the end of the pool without pushing other
7131 fixes out of range, then we don't try. This ensures that we
7132 can't fail later on. */
7133 if (min_address >= minipool_barrier->address
7134 || (minipool_vector_tail->min_address + fix->fix_size
7135 >= minipool_barrier->address))
7136 return NULL;
7138 /* Scan the pool to see if a constant with the same value has
7139 already been added. While we are doing this, also note the
7140 location where we must insert the constant if it doesn't already
7141 exist. */
7142 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7144 if (GET_CODE (fix->value) == GET_CODE (mp->value)
7145 && fix->mode == mp->mode
7146 && (GET_CODE (fix->value) != CODE_LABEL
7147 || (CODE_LABEL_NUMBER (fix->value)
7148 == CODE_LABEL_NUMBER (mp->value)))
7149 && rtx_equal_p (fix->value, mp->value)
7150 /* Check that there is enough slack to move this entry to the
7151 end of the table (this is conservative). */
7152 && (mp->max_address
7153 > (minipool_barrier->address
7154 + minipool_vector_tail->offset
7155 + minipool_vector_tail->fix_size)))
7157 mp->refcount++;
7158 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7161 if (min_mp != NULL)
7162 mp->min_address += fix->fix_size;
7163 else
7165 /* Note the insertion point if necessary. */
7166 if (mp->min_address < min_address)
7168 /* For now, we do not allow the insertion of 8-byte alignment
7169 requiring nodes anywhere but at the start of the pool. */
7170 if (ARM_DOUBLEWORD_ALIGN
7171 && fix->fix_size == 8 && mp->fix_size != 8)
7172 return NULL;
7173 else
7174 min_mp = mp;
7176 else if (mp->max_address
7177 < minipool_barrier->address + mp->offset + fix->fix_size)
7179 /* Inserting before this entry would push the fix beyond
7180 its maximum address (which can happen if we have
7181 re-located a forwards fix); force the new fix to come
7182 after it. */
7183 min_mp = mp;
7184 min_address = mp->min_address + fix->fix_size;
7186 /* If we are inserting an 8-bytes aligned quantity and
7187 we have not already found an insertion point, then
7188 make sure that all such 8-byte aligned quantities are
7189 placed at the start of the pool. */
7190 else if (ARM_DOUBLEWORD_ALIGN
7191 && min_mp == NULL
7192 && fix->fix_size == 8
7193 && mp->fix_size < 8)
7195 min_mp = mp;
7196 min_address = mp->min_address + fix->fix_size;
7201 /* We need to create a new entry. */
7202 mp = xmalloc (sizeof (* mp));
7203 mp->fix_size = fix->fix_size;
7204 mp->mode = fix->mode;
7205 mp->value = fix->value;
7206 mp->refcount = 1;
7207 mp->max_address = minipool_barrier->address + 65536;
7209 mp->min_address = min_address;
7211 if (min_mp == NULL)
7213 mp->prev = NULL;
7214 mp->next = minipool_vector_head;
7216 if (mp->next == NULL)
7218 minipool_vector_tail = mp;
7219 minipool_vector_label = gen_label_rtx ();
7221 else
7222 mp->next->prev = mp;
7224 minipool_vector_head = mp;
7226 else
7228 mp->next = min_mp->next;
7229 mp->prev = min_mp;
7230 min_mp->next = mp;
7232 if (mp->next != NULL)
7233 mp->next->prev = mp;
7234 else
7235 minipool_vector_tail = mp;
7238 /* Save the new entry. */
7239 min_mp = mp;
7241 if (mp->prev)
7242 mp = mp->prev;
7243 else
7244 mp->offset = 0;
7246 /* Scan over the following entries and adjust their offsets. */
7247 while (mp->next != NULL)
7249 if (mp->next->min_address < mp->min_address + mp->fix_size)
7250 mp->next->min_address = mp->min_address + mp->fix_size;
7252 if (mp->refcount)
7253 mp->next->offset = mp->offset + mp->fix_size;
7254 else
7255 mp->next->offset = mp->offset;
7257 mp = mp->next;
7260 return min_mp;
7263 static void
7264 assign_minipool_offsets (Mfix *barrier)
7266 HOST_WIDE_INT offset = 0;
7267 Mnode *mp;
7269 minipool_barrier = barrier;
7271 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7273 mp->offset = offset;
7275 if (mp->refcount > 0)
7276 offset += mp->fix_size;
7280 /* Output the literal table */
7281 static void
7282 dump_minipool (rtx scan)
7284 Mnode * mp;
7285 Mnode * nmp;
7286 int align64 = 0;
7288 if (ARM_DOUBLEWORD_ALIGN)
7289 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7290 if (mp->refcount > 0 && mp->fix_size == 8)
7292 align64 = 1;
7293 break;
7296 if (dump_file)
7297 fprintf (dump_file,
7298 ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7299 INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7301 scan = emit_label_after (gen_label_rtx (), scan);
7302 scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7303 scan = emit_label_after (minipool_vector_label, scan);
7305 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7307 if (mp->refcount > 0)
7309 if (dump_file)
7311 fprintf (dump_file,
7312 ";; Offset %u, min %ld, max %ld ",
7313 (unsigned) mp->offset, (unsigned long) mp->min_address,
7314 (unsigned long) mp->max_address);
7315 arm_print_value (dump_file, mp->value);
7316 fputc ('\n', dump_file);
7319 switch (mp->fix_size)
7321 #ifdef HAVE_consttable_1
7322 case 1:
7323 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7324 break;
7326 #endif
7327 #ifdef HAVE_consttable_2
7328 case 2:
7329 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7330 break;
7332 #endif
7333 #ifdef HAVE_consttable_4
7334 case 4:
7335 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7336 break;
7338 #endif
7339 #ifdef HAVE_consttable_8
7340 case 8:
7341 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7342 break;
7344 #endif
7345 default:
7346 gcc_unreachable ();
7350 nmp = mp->next;
7351 free (mp);
7354 minipool_vector_head = minipool_vector_tail = NULL;
7355 scan = emit_insn_after (gen_consttable_end (), scan);
7356 scan = emit_barrier_after (scan);
7359 /* Return the cost of forcibly inserting a barrier after INSN. */
7360 static int
7361 arm_barrier_cost (rtx insn)
7363 /* Basing the location of the pool on the loop depth is preferable,
7364 but at the moment, the basic block information seems to be
7365 corrupt by this stage of the compilation. */
7366 int base_cost = 50;
7367 rtx next = next_nonnote_insn (insn);
7369 if (next != NULL && GET_CODE (next) == CODE_LABEL)
7370 base_cost -= 20;
7372 switch (GET_CODE (insn))
7374 case CODE_LABEL:
7375 /* It will always be better to place the table before the label, rather
7376 than after it. */
7377 return 50;
7379 case INSN:
7380 case CALL_INSN:
7381 return base_cost;
7383 case JUMP_INSN:
7384 return base_cost - 10;
7386 default:
7387 return base_cost + 10;
7391 /* Find the best place in the insn stream in the range
7392 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7393 Create the barrier by inserting a jump and add a new fix entry for
7394 it. */
7395 static Mfix *
7396 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7398 HOST_WIDE_INT count = 0;
7399 rtx barrier;
7400 rtx from = fix->insn;
7401 rtx selected = from;
7402 int selected_cost;
7403 HOST_WIDE_INT selected_address;
7404 Mfix * new_fix;
7405 HOST_WIDE_INT max_count = max_address - fix->address;
7406 rtx label = gen_label_rtx ();
7408 selected_cost = arm_barrier_cost (from);
7409 selected_address = fix->address;
7411 while (from && count < max_count)
7413 rtx tmp;
7414 int new_cost;
7416 /* This code shouldn't have been called if there was a natural barrier
7417 within range. */
7418 gcc_assert (GET_CODE (from) != BARRIER);
7420 /* Count the length of this insn. */
7421 count += get_attr_length (from);
7423 /* If there is a jump table, add its length. */
7424 tmp = is_jump_table (from);
7425 if (tmp != NULL)
7427 count += get_jump_table_size (tmp);
7429 /* Jump tables aren't in a basic block, so base the cost on
7430 the dispatch insn. If we select this location, we will
7431 still put the pool after the table. */
7432 new_cost = arm_barrier_cost (from);
7434 if (count < max_count && new_cost <= selected_cost)
7436 selected = tmp;
7437 selected_cost = new_cost;
7438 selected_address = fix->address + count;
7441 /* Continue after the dispatch table. */
7442 from = NEXT_INSN (tmp);
7443 continue;
7446 new_cost = arm_barrier_cost (from);
7448 if (count < max_count && new_cost <= selected_cost)
7450 selected = from;
7451 selected_cost = new_cost;
7452 selected_address = fix->address + count;
7455 from = NEXT_INSN (from);
7458 /* Create a new JUMP_INSN that branches around a barrier. */
7459 from = emit_jump_insn_after (gen_jump (label), selected);
7460 JUMP_LABEL (from) = label;
7461 barrier = emit_barrier_after (from);
7462 emit_label_after (label, barrier);
7464 /* Create a minipool barrier entry for the new barrier. */
7465 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7466 new_fix->insn = barrier;
7467 new_fix->address = selected_address;
7468 new_fix->next = fix->next;
7469 fix->next = new_fix;
7471 return new_fix;
7474 /* Record that there is a natural barrier in the insn stream at
7475 ADDRESS. */
7476 static void
7477 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7479 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7481 fix->insn = insn;
7482 fix->address = address;
7484 fix->next = NULL;
7485 if (minipool_fix_head != NULL)
7486 minipool_fix_tail->next = fix;
7487 else
7488 minipool_fix_head = fix;
7490 minipool_fix_tail = fix;
7493 /* Record INSN, which will need fixing up to load a value from the
7494 minipool. ADDRESS is the offset of the insn since the start of the
7495 function; LOC is a pointer to the part of the insn which requires
7496 fixing; VALUE is the constant that must be loaded, which is of type
7497 MODE. */
7498 static void
7499 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7500 enum machine_mode mode, rtx value)
7502 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7504 #ifdef AOF_ASSEMBLER
7505 /* PIC symbol references need to be converted into offsets into the
7506 based area. */
7507 /* XXX This shouldn't be done here. */
7508 if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7509 value = aof_pic_entry (value);
7510 #endif /* AOF_ASSEMBLER */
7512 fix->insn = insn;
7513 fix->address = address;
7514 fix->loc = loc;
7515 fix->mode = mode;
7516 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7517 fix->value = value;
7518 fix->forwards = get_attr_pool_range (insn);
7519 fix->backwards = get_attr_neg_pool_range (insn);
7520 fix->minipool = NULL;
7522 /* If an insn doesn't have a range defined for it, then it isn't
7523 expecting to be reworked by this code. Better to stop now than
7524 to generate duff assembly code. */
7525 gcc_assert (fix->forwards || fix->backwards);
7527 /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7528 So there might be an empty word before the start of the pool.
7529 Hence we reduce the forward range by 4 to allow for this
7530 possibility. */
7531 if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7532 fix->forwards -= 4;
7534 if (dump_file)
7536 fprintf (dump_file,
7537 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7538 GET_MODE_NAME (mode),
7539 INSN_UID (insn), (unsigned long) address,
7540 -1 * (long)fix->backwards, (long)fix->forwards);
7541 arm_print_value (dump_file, fix->value);
7542 fprintf (dump_file, "\n");
7545 /* Add it to the chain of fixes. */
7546 fix->next = NULL;
7548 if (minipool_fix_head != NULL)
7549 minipool_fix_tail->next = fix;
7550 else
7551 minipool_fix_head = fix;
7553 minipool_fix_tail = fix;
7556 /* Return the cost of synthesizing a 64-bit constant VAL inline.
7557 Returns the number of insns needed, or 99 if we don't know how to
7558 do it. */
7560 arm_const_double_inline_cost (rtx val)
7562 rtx lowpart, highpart;
7563 enum machine_mode mode;
7565 mode = GET_MODE (val);
7567 if (mode == VOIDmode)
7568 mode = DImode;
7570 gcc_assert (GET_MODE_SIZE (mode) == 8);
7572 lowpart = gen_lowpart (SImode, val);
7573 highpart = gen_highpart_mode (SImode, mode, val);
7575 gcc_assert (GET_CODE (lowpart) == CONST_INT);
7576 gcc_assert (GET_CODE (highpart) == CONST_INT);
7578 return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
7579 NULL_RTX, NULL_RTX, 0, 0)
7580 + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
7581 NULL_RTX, NULL_RTX, 0, 0));
7584 /* Return true if it is worthwhile to split a 64-bit constant into two
7585 32-bit operations. This is the case if optimizing for size, or
7586 if we have load delay slots, or if one 32-bit part can be done with
7587 a single data operation. */
7588 bool
7589 arm_const_double_by_parts (rtx val)
7591 enum machine_mode mode = GET_MODE (val);
7592 rtx part;
7594 if (optimize_size || arm_ld_sched)
7595 return true;
7597 if (mode == VOIDmode)
7598 mode = DImode;
7600 part = gen_highpart_mode (SImode, mode, val);
7602 gcc_assert (GET_CODE (part) == CONST_INT);
7604 if (const_ok_for_arm (INTVAL (part))
7605 || const_ok_for_arm (~INTVAL (part)))
7606 return true;
7608 part = gen_lowpart (SImode, val);
7610 gcc_assert (GET_CODE (part) == CONST_INT);
7612 if (const_ok_for_arm (INTVAL (part))
7613 || const_ok_for_arm (~INTVAL (part)))
7614 return true;
7616 return false;
7619 /* Scan INSN and note any of its operands that need fixing.
7620 If DO_PUSHES is false we do not actually push any of the fixups
7621 needed. The function returns TRUE if any fixups were needed/pushed.
7622 This is used by arm_memory_load_p() which needs to know about loads
7623 of constants that will be converted into minipool loads. */
7624 static bool
7625 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7627 bool result = false;
7628 int opno;
7630 extract_insn (insn);
7632 if (!constrain_operands (1))
7633 fatal_insn_not_found (insn);
7635 if (recog_data.n_alternatives == 0)
7636 return false;
7638 /* Fill in recog_op_alt with information about the constraints of
7639 this insn. */
7640 preprocess_constraints ();
7642 for (opno = 0; opno < recog_data.n_operands; opno++)
7644 /* Things we need to fix can only occur in inputs. */
7645 if (recog_data.operand_type[opno] != OP_IN)
7646 continue;
7648 /* If this alternative is a memory reference, then any mention
7649 of constants in this alternative is really to fool reload
7650 into allowing us to accept one there. We need to fix them up
7651 now so that we output the right code. */
7652 if (recog_op_alt[opno][which_alternative].memory_ok)
7654 rtx op = recog_data.operand[opno];
7656 if (CONSTANT_P (op))
7658 if (do_pushes)
7659 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7660 recog_data.operand_mode[opno], op);
7661 result = true;
7663 else if (GET_CODE (op) == MEM
7664 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7665 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7667 if (do_pushes)
7669 rtx cop = avoid_constant_pool_reference (op);
7671 /* Casting the address of something to a mode narrower
7672 than a word can cause avoid_constant_pool_reference()
7673 to return the pool reference itself. That's no good to
7674 us here. Lets just hope that we can use the
7675 constant pool value directly. */
7676 if (op == cop)
7677 cop = get_pool_constant (XEXP (op, 0));
7679 push_minipool_fix (insn, address,
7680 recog_data.operand_loc[opno],
7681 recog_data.operand_mode[opno], cop);
7684 result = true;
7689 return result;
7692 /* Gcc puts the pool in the wrong place for ARM, since we can only
7693 load addresses a limited distance around the pc. We do some
7694 special munging to move the constant pool values to the correct
7695 point in the code. */
7696 static void
7697 arm_reorg (void)
7699 rtx insn;
7700 HOST_WIDE_INT address = 0;
7701 Mfix * fix;
7703 minipool_fix_head = minipool_fix_tail = NULL;
7705 /* The first insn must always be a note, or the code below won't
7706 scan it properly. */
7707 insn = get_insns ();
7708 gcc_assert (GET_CODE (insn) == NOTE);
7710 /* Scan all the insns and record the operands that will need fixing. */
7711 for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7713 if (TARGET_CIRRUS_FIX_INVALID_INSNS
7714 && (arm_cirrus_insn_p (insn)
7715 || GET_CODE (insn) == JUMP_INSN
7716 || arm_memory_load_p (insn)))
7717 cirrus_reorg (insn);
7719 if (GET_CODE (insn) == BARRIER)
7720 push_minipool_barrier (insn, address);
7721 else if (INSN_P (insn))
7723 rtx table;
7725 note_invalid_constants (insn, address, true);
7726 address += get_attr_length (insn);
7728 /* If the insn is a vector jump, add the size of the table
7729 and skip the table. */
7730 if ((table = is_jump_table (insn)) != NULL)
7732 address += get_jump_table_size (table);
7733 insn = table;
7738 fix = minipool_fix_head;
7740 /* Now scan the fixups and perform the required changes. */
7741 while (fix)
7743 Mfix * ftmp;
7744 Mfix * fdel;
7745 Mfix * last_added_fix;
7746 Mfix * last_barrier = NULL;
7747 Mfix * this_fix;
7749 /* Skip any further barriers before the next fix. */
7750 while (fix && GET_CODE (fix->insn) == BARRIER)
7751 fix = fix->next;
7753 /* No more fixes. */
7754 if (fix == NULL)
7755 break;
7757 last_added_fix = NULL;
7759 for (ftmp = fix; ftmp; ftmp = ftmp->next)
7761 if (GET_CODE (ftmp->insn) == BARRIER)
7763 if (ftmp->address >= minipool_vector_head->max_address)
7764 break;
7766 last_barrier = ftmp;
7768 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7769 break;
7771 last_added_fix = ftmp; /* Keep track of the last fix added. */
7774 /* If we found a barrier, drop back to that; any fixes that we
7775 could have reached but come after the barrier will now go in
7776 the next mini-pool. */
7777 if (last_barrier != NULL)
7779 /* Reduce the refcount for those fixes that won't go into this
7780 pool after all. */
7781 for (fdel = last_barrier->next;
7782 fdel && fdel != ftmp;
7783 fdel = fdel->next)
7785 fdel->minipool->refcount--;
7786 fdel->minipool = NULL;
7789 ftmp = last_barrier;
7791 else
7793 /* ftmp is first fix that we can't fit into this pool and
7794 there no natural barriers that we could use. Insert a
7795 new barrier in the code somewhere between the previous
7796 fix and this one, and arrange to jump around it. */
7797 HOST_WIDE_INT max_address;
7799 /* The last item on the list of fixes must be a barrier, so
7800 we can never run off the end of the list of fixes without
7801 last_barrier being set. */
7802 gcc_assert (ftmp);
7804 max_address = minipool_vector_head->max_address;
7805 /* Check that there isn't another fix that is in range that
7806 we couldn't fit into this pool because the pool was
7807 already too large: we need to put the pool before such an
7808 instruction. */
7809 if (ftmp->address < max_address)
7810 max_address = ftmp->address;
7812 last_barrier = create_fix_barrier (last_added_fix, max_address);
7815 assign_minipool_offsets (last_barrier);
7817 while (ftmp)
7819 if (GET_CODE (ftmp->insn) != BARRIER
7820 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7821 == NULL))
7822 break;
7824 ftmp = ftmp->next;
7827 /* Scan over the fixes we have identified for this pool, fixing them
7828 up and adding the constants to the pool itself. */
7829 for (this_fix = fix; this_fix && ftmp != this_fix;
7830 this_fix = this_fix->next)
7831 if (GET_CODE (this_fix->insn) != BARRIER)
7833 rtx addr
7834 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
7835 minipool_vector_label),
7836 this_fix->minipool->offset);
7837 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7840 dump_minipool (last_barrier->insn);
7841 fix = ftmp;
7844 /* From now on we must synthesize any constants that we can't handle
7845 directly. This can happen if the RTL gets split during final
7846 instruction generation. */
7847 after_arm_reorg = 1;
7849 /* Free the minipool memory. */
7850 obstack_free (&minipool_obstack, minipool_startobj);
7853 /* Routines to output assembly language. */
7855 /* If the rtx is the correct value then return the string of the number.
7856 In this way we can ensure that valid double constants are generated even
7857 when cross compiling. */
7858 const char *
7859 fp_immediate_constant (rtx x)
7861 REAL_VALUE_TYPE r;
7862 int i;
7864 if (!fp_consts_inited)
7865 init_fp_table ();
7867 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7868 for (i = 0; i < 8; i++)
7869 if (REAL_VALUES_EQUAL (r, values_fp[i]))
7870 return strings_fp[i];
7872 gcc_unreachable ();
7875 /* As for fp_immediate_constant, but value is passed directly, not in rtx. */
7876 static const char *
7877 fp_const_from_val (REAL_VALUE_TYPE *r)
7879 int i;
7881 if (!fp_consts_inited)
7882 init_fp_table ();
7884 for (i = 0; i < 8; i++)
7885 if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7886 return strings_fp[i];
7888 gcc_unreachable ();
7891 /* Output the operands of a LDM/STM instruction to STREAM.
7892 MASK is the ARM register set mask of which only bits 0-15 are important.
7893 REG is the base register, either the frame pointer or the stack pointer,
7894 INSTR is the possibly suffixed load or store instruction. */
7896 static void
7897 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
7898 unsigned long mask)
7900 unsigned i;
7901 bool not_first = FALSE;
7903 fputc ('\t', stream);
7904 asm_fprintf (stream, instr, reg);
7905 fputs (", {", stream);
7907 for (i = 0; i <= LAST_ARM_REGNUM; i++)
7908 if (mask & (1 << i))
7910 if (not_first)
7911 fprintf (stream, ", ");
7913 asm_fprintf (stream, "%r", i);
7914 not_first = TRUE;
7917 fprintf (stream, "}\n");
7921 /* Output a FLDMX instruction to STREAM.
7922 BASE if the register containing the address.
7923 REG and COUNT specify the register range.
7924 Extra registers may be added to avoid hardware bugs. */
7926 static void
7927 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7929 int i;
7931 /* Workaround ARM10 VFPr1 bug. */
7932 if (count == 2 && !arm_arch6)
7934 if (reg == 15)
7935 reg--;
7936 count++;
7939 fputc ('\t', stream);
7940 asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7942 for (i = reg; i < reg + count; i++)
7944 if (i > reg)
7945 fputs (", ", stream);
7946 asm_fprintf (stream, "d%d", i);
7948 fputs ("}\n", stream);
7953 /* Output the assembly for a store multiple. */
7955 const char *
7956 vfp_output_fstmx (rtx * operands)
7958 char pattern[100];
7959 int p;
7960 int base;
7961 int i;
7963 strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7964 p = strlen (pattern);
7966 gcc_assert (GET_CODE (operands[1]) == REG);
7968 base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7969 for (i = 1; i < XVECLEN (operands[2], 0); i++)
7971 p += sprintf (&pattern[p], ", d%d", base + i);
7973 strcpy (&pattern[p], "}");
7975 output_asm_insn (pattern, operands);
7976 return "";
7980 /* Emit RTL to save block of VFP register pairs to the stack. Returns the
7981 number of bytes pushed. */
7983 static int
7984 vfp_emit_fstmx (int base_reg, int count)
7986 rtx par;
7987 rtx dwarf;
7988 rtx tmp, reg;
7989 int i;
7991 /* Workaround ARM10 VFPr1 bug. Data corruption can occur when exactly two
7992 register pairs are stored by a store multiple insn. We avoid this
7993 by pushing an extra pair. */
7994 if (count == 2 && !arm_arch6)
7996 if (base_reg == LAST_VFP_REGNUM - 3)
7997 base_reg -= 2;
7998 count++;
8001 /* ??? The frame layout is implementation defined. We describe
8002 standard format 1 (equivalent to a FSTMD insn and unused pad word).
8003 We really need some way of representing the whole block so that the
8004 unwinder can figure it out at runtime. */
8005 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8006 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
8008 reg = gen_rtx_REG (DFmode, base_reg);
8009 base_reg += 2;
8011 XVECEXP (par, 0, 0)
8012 = gen_rtx_SET (VOIDmode,
8013 gen_rtx_MEM (BLKmode,
8014 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
8015 gen_rtx_UNSPEC (BLKmode,
8016 gen_rtvec (1, reg),
8017 UNSPEC_PUSH_MULT));
8019 tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8020 gen_rtx_PLUS (SImode, stack_pointer_rtx,
8021 GEN_INT (-(count * 8 + 4))));
8022 RTX_FRAME_RELATED_P (tmp) = 1;
8023 XVECEXP (dwarf, 0, 0) = tmp;
8025 tmp = gen_rtx_SET (VOIDmode,
8026 gen_rtx_MEM (DFmode, stack_pointer_rtx),
8027 reg);
8028 RTX_FRAME_RELATED_P (tmp) = 1;
8029 XVECEXP (dwarf, 0, 1) = tmp;
8031 for (i = 1; i < count; i++)
8033 reg = gen_rtx_REG (DFmode, base_reg);
8034 base_reg += 2;
8035 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8037 tmp = gen_rtx_SET (VOIDmode,
8038 gen_rtx_MEM (DFmode,
8039 gen_rtx_PLUS (SImode,
8040 stack_pointer_rtx,
8041 GEN_INT (i * 8))),
8042 reg);
8043 RTX_FRAME_RELATED_P (tmp) = 1;
8044 XVECEXP (dwarf, 0, i + 1) = tmp;
8047 par = emit_insn (par);
8048 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8049 REG_NOTES (par));
8050 RTX_FRAME_RELATED_P (par) = 1;
8052 return count * 8 + 4;
8056 /* Output a 'call' insn. */
8057 const char *
8058 output_call (rtx *operands)
8060 gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly. */
8062 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
8063 if (REGNO (operands[0]) == LR_REGNUM)
8065 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8066 output_asm_insn ("mov%?\t%0, %|lr", operands);
8069 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8071 if (TARGET_INTERWORK || arm_arch4t)
8072 output_asm_insn ("bx%?\t%0", operands);
8073 else
8074 output_asm_insn ("mov%?\t%|pc, %0", operands);
8076 return "";
8079 /* Output a 'call' insn that is a reference in memory. */
8080 const char *
8081 output_call_mem (rtx *operands)
8083 if (TARGET_INTERWORK && !arm_arch5)
8085 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8086 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8087 output_asm_insn ("bx%?\t%|ip", operands);
8089 else if (regno_use_in (LR_REGNUM, operands[0]))
8091 /* LR is used in the memory address. We load the address in the
8092 first instruction. It's safe to use IP as the target of the
8093 load since the call will kill it anyway. */
8094 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8095 if (arm_arch5)
8096 output_asm_insn ("blx%?\t%|ip", operands);
8097 else
8099 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8100 if (arm_arch4t)
8101 output_asm_insn ("bx%?\t%|ip", operands);
8102 else
8103 output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8106 else
8108 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8109 output_asm_insn ("ldr%?\t%|pc, %0", operands);
8112 return "";
8116 /* Output a move from arm registers to an fpa registers.
8117 OPERANDS[0] is an fpa register.
8118 OPERANDS[1] is the first registers of an arm register pair. */
8119 const char *
8120 output_mov_long_double_fpa_from_arm (rtx *operands)
8122 int arm_reg0 = REGNO (operands[1]);
8123 rtx ops[3];
8125 gcc_assert (arm_reg0 != IP_REGNUM);
8127 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8128 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8129 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8131 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8132 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8134 return "";
8137 /* Output a move from an fpa register to arm registers.
8138 OPERANDS[0] is the first registers of an arm register pair.
8139 OPERANDS[1] is an fpa register. */
8140 const char *
8141 output_mov_long_double_arm_from_fpa (rtx *operands)
8143 int arm_reg0 = REGNO (operands[0]);
8144 rtx ops[3];
8146 gcc_assert (arm_reg0 != IP_REGNUM);
8148 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8149 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8150 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8152 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8153 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8154 return "";
8157 /* Output a move from arm registers to arm registers of a long double
8158 OPERANDS[0] is the destination.
8159 OPERANDS[1] is the source. */
8160 const char *
8161 output_mov_long_double_arm_from_arm (rtx *operands)
8163 /* We have to be careful here because the two might overlap. */
8164 int dest_start = REGNO (operands[0]);
8165 int src_start = REGNO (operands[1]);
8166 rtx ops[2];
8167 int i;
8169 if (dest_start < src_start)
8171 for (i = 0; i < 3; i++)
8173 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8174 ops[1] = gen_rtx_REG (SImode, src_start + i);
8175 output_asm_insn ("mov%?\t%0, %1", ops);
8178 else
8180 for (i = 2; i >= 0; i--)
8182 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8183 ops[1] = gen_rtx_REG (SImode, src_start + i);
8184 output_asm_insn ("mov%?\t%0, %1", ops);
8188 return "";
8192 /* Output a move from arm registers to an fpa registers.
8193 OPERANDS[0] is an fpa register.
8194 OPERANDS[1] is the first registers of an arm register pair. */
8195 const char *
8196 output_mov_double_fpa_from_arm (rtx *operands)
8198 int arm_reg0 = REGNO (operands[1]);
8199 rtx ops[2];
8201 gcc_assert (arm_reg0 != IP_REGNUM);
8203 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8204 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8205 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8206 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8207 return "";
8210 /* Output a move from an fpa register to arm registers.
8211 OPERANDS[0] is the first registers of an arm register pair.
8212 OPERANDS[1] is an fpa register. */
8213 const char *
8214 output_mov_double_arm_from_fpa (rtx *operands)
8216 int arm_reg0 = REGNO (operands[0]);
8217 rtx ops[2];
8219 gcc_assert (arm_reg0 != IP_REGNUM);
8221 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8222 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8223 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8224 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8225 return "";
8228 /* Output a move between double words.
8229 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8230 or MEM<-REG and all MEMs must be offsettable addresses. */
8231 const char *
8232 output_move_double (rtx *operands)
8234 enum rtx_code code0 = GET_CODE (operands[0]);
8235 enum rtx_code code1 = GET_CODE (operands[1]);
8236 rtx otherops[3];
8238 if (code0 == REG)
8240 int reg0 = REGNO (operands[0]);
8242 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8244 gcc_assert (code1 == MEM); /* Constraints should ensure this. */
8246 switch (GET_CODE (XEXP (operands[1], 0)))
8248 case REG:
8249 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8250 break;
8252 case PRE_INC:
8253 gcc_assert (TARGET_LDRD);
8254 output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8255 break;
8257 case PRE_DEC:
8258 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8259 break;
8261 case POST_INC:
8262 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8263 break;
8265 case POST_DEC:
8266 gcc_assert (TARGET_LDRD);
8267 output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8268 break;
8270 case PRE_MODIFY:
8271 case POST_MODIFY:
8272 otherops[0] = operands[0];
8273 otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8274 otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8276 if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8278 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8280 /* Registers overlap so split out the increment. */
8281 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8282 output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8284 else
8285 output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8287 else
8289 /* We only allow constant increments, so this is safe. */
8290 output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8292 break;
8294 case LABEL_REF:
8295 case CONST:
8296 output_asm_insn ("adr%?\t%0, %1", operands);
8297 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8298 break;
8300 default:
8301 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8302 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8304 otherops[0] = operands[0];
8305 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8306 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8308 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8310 if (GET_CODE (otherops[2]) == CONST_INT)
8312 switch ((int) INTVAL (otherops[2]))
8314 case -8:
8315 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8316 return "";
8317 case -4:
8318 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8319 return "";
8320 case 4:
8321 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8322 return "";
8325 if (TARGET_LDRD
8326 && (GET_CODE (otherops[2]) == REG
8327 || (GET_CODE (otherops[2]) == CONST_INT
8328 && INTVAL (otherops[2]) > -256
8329 && INTVAL (otherops[2]) < 256)))
8331 if (reg_overlap_mentioned_p (otherops[0],
8332 otherops[2]))
8334 /* Swap base and index registers over to
8335 avoid a conflict. */
8336 otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8337 otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8340 /* If both registers conflict, it will usually
8341 have been fixed by a splitter. */
8342 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8344 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8345 output_asm_insn ("ldr%?d\t%0, [%1]",
8346 otherops);
8348 else
8349 output_asm_insn ("ldr%?d\t%0, [%1, %2]", otherops);
8350 return "";
8353 if (GET_CODE (otherops[2]) == CONST_INT)
8355 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8356 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8357 else
8358 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8360 else
8361 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8363 else
8364 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8366 return "ldm%?ia\t%0, %M0";
8368 else
8370 otherops[1] = adjust_address (operands[1], SImode, 4);
8371 /* Take care of overlapping base/data reg. */
8372 if (reg_mentioned_p (operands[0], operands[1]))
8374 output_asm_insn ("ldr%?\t%0, %1", otherops);
8375 output_asm_insn ("ldr%?\t%0, %1", operands);
8377 else
8379 output_asm_insn ("ldr%?\t%0, %1", operands);
8380 output_asm_insn ("ldr%?\t%0, %1", otherops);
8385 else
8387 /* Constraints should ensure this. */
8388 gcc_assert (code0 == MEM && code1 == REG);
8389 gcc_assert (REGNO (operands[1]) != IP_REGNUM);
8391 switch (GET_CODE (XEXP (operands[0], 0)))
8393 case REG:
8394 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8395 break;
8397 case PRE_INC:
8398 gcc_assert (TARGET_LDRD);
8399 output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8400 break;
8402 case PRE_DEC:
8403 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8404 break;
8406 case POST_INC:
8407 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8408 break;
8410 case POST_DEC:
8411 gcc_assert (TARGET_LDRD);
8412 output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8413 break;
8415 case PRE_MODIFY:
8416 case POST_MODIFY:
8417 otherops[0] = operands[1];
8418 otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8419 otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8421 if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8422 output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8423 else
8424 output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8425 break;
8427 case PLUS:
8428 otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8429 if (GET_CODE (otherops[2]) == CONST_INT)
8431 switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8433 case -8:
8434 output_asm_insn ("stm%?db\t%m0, %M1", operands);
8435 return "";
8437 case -4:
8438 output_asm_insn ("stm%?da\t%m0, %M1", operands);
8439 return "";
8441 case 4:
8442 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8443 return "";
8446 if (TARGET_LDRD
8447 && (GET_CODE (otherops[2]) == REG
8448 || (GET_CODE (otherops[2]) == CONST_INT
8449 && INTVAL (otherops[2]) > -256
8450 && INTVAL (otherops[2]) < 256)))
8452 otherops[0] = operands[1];
8453 otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8454 output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8455 return "";
8457 /* Fall through */
8459 default:
8460 otherops[0] = adjust_address (operands[0], SImode, 4);
8461 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8462 output_asm_insn ("str%?\t%1, %0", operands);
8463 output_asm_insn ("str%?\t%1, %0", otherops);
8467 return "";
8470 /* Output an ADD r, s, #n where n may be too big for one instruction.
8471 If adding zero to one register, output nothing. */
8472 const char *
8473 output_add_immediate (rtx *operands)
8475 HOST_WIDE_INT n = INTVAL (operands[2]);
8477 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8479 if (n < 0)
8480 output_multi_immediate (operands,
8481 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8482 -n);
8483 else
8484 output_multi_immediate (operands,
8485 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8489 return "";
8492 /* Output a multiple immediate operation.
8493 OPERANDS is the vector of operands referred to in the output patterns.
8494 INSTR1 is the output pattern to use for the first constant.
8495 INSTR2 is the output pattern to use for subsequent constants.
8496 IMMED_OP is the index of the constant slot in OPERANDS.
8497 N is the constant value. */
8498 static const char *
8499 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8500 int immed_op, HOST_WIDE_INT n)
8502 #if HOST_BITS_PER_WIDE_INT > 32
8503 n &= 0xffffffff;
8504 #endif
8506 if (n == 0)
8508 /* Quick and easy output. */
8509 operands[immed_op] = const0_rtx;
8510 output_asm_insn (instr1, operands);
8512 else
8514 int i;
8515 const char * instr = instr1;
8517 /* Note that n is never zero here (which would give no output). */
8518 for (i = 0; i < 32; i += 2)
8520 if (n & (3 << i))
8522 operands[immed_op] = GEN_INT (n & (255 << i));
8523 output_asm_insn (instr, operands);
8524 instr = instr2;
8525 i += 6;
8530 return "";
8533 /* Return the appropriate ARM instruction for the operation code.
8534 The returned result should not be overwritten. OP is the rtx of the
8535 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8536 was shifted. */
8537 const char *
8538 arithmetic_instr (rtx op, int shift_first_arg)
8540 switch (GET_CODE (op))
8542 case PLUS:
8543 return "add";
8545 case MINUS:
8546 return shift_first_arg ? "rsb" : "sub";
8548 case IOR:
8549 return "orr";
8551 case XOR:
8552 return "eor";
8554 case AND:
8555 return "and";
8557 default:
8558 gcc_unreachable ();
8562 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8563 for the operation code. The returned result should not be overwritten.
8564 OP is the rtx code of the shift.
8565 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8566 shift. */
8567 static const char *
8568 shift_op (rtx op, HOST_WIDE_INT *amountp)
8570 const char * mnem;
8571 enum rtx_code code = GET_CODE (op);
8573 switch (GET_CODE (XEXP (op, 1)))
8575 case REG:
8576 case SUBREG:
8577 *amountp = -1;
8578 break;
8580 case CONST_INT:
8581 *amountp = INTVAL (XEXP (op, 1));
8582 break;
8584 default:
8585 gcc_unreachable ();
8588 switch (code)
8590 case ASHIFT:
8591 mnem = "asl";
8592 break;
8594 case ASHIFTRT:
8595 mnem = "asr";
8596 break;
8598 case LSHIFTRT:
8599 mnem = "lsr";
8600 break;
8602 case ROTATE:
8603 gcc_assert (*amountp != -1);
8604 *amountp = 32 - *amountp;
8606 /* Fall through. */
8608 case ROTATERT:
8609 mnem = "ror";
8610 break;
8612 case MULT:
8613 /* We never have to worry about the amount being other than a
8614 power of 2, since this case can never be reloaded from a reg. */
8615 gcc_assert (*amountp != -1);
8616 *amountp = int_log2 (*amountp);
8617 return "asl";
8619 default:
8620 gcc_unreachable ();
8623 if (*amountp != -1)
8625 /* This is not 100% correct, but follows from the desire to merge
8626 multiplication by a power of 2 with the recognizer for a
8627 shift. >=32 is not a valid shift for "asl", so we must try and
8628 output a shift that produces the correct arithmetical result.
8629 Using lsr #32 is identical except for the fact that the carry bit
8630 is not set correctly if we set the flags; but we never use the
8631 carry bit from such an operation, so we can ignore that. */
8632 if (code == ROTATERT)
8633 /* Rotate is just modulo 32. */
8634 *amountp &= 31;
8635 else if (*amountp != (*amountp & 31))
8637 if (code == ASHIFT)
8638 mnem = "lsr";
8639 *amountp = 32;
8642 /* Shifts of 0 are no-ops. */
8643 if (*amountp == 0)
8644 return NULL;
8647 return mnem;
8650 /* Obtain the shift from the POWER of two. */
8652 static HOST_WIDE_INT
8653 int_log2 (HOST_WIDE_INT power)
8655 HOST_WIDE_INT shift = 0;
8657 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8659 gcc_assert (shift <= 31);
8660 shift++;
8663 return shift;
8666 /* Output a .ascii pseudo-op, keeping track of lengths. This is
8667 because /bin/as is horribly restrictive. The judgement about
8668 whether or not each character is 'printable' (and can be output as
8669 is) or not (and must be printed with an octal escape) must be made
8670 with reference to the *host* character set -- the situation is
8671 similar to that discussed in the comments above pp_c_char in
8672 c-pretty-print.c. */
8674 #define MAX_ASCII_LEN 51
8676 void
8677 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8679 int i;
8680 int len_so_far = 0;
8682 fputs ("\t.ascii\t\"", stream);
8684 for (i = 0; i < len; i++)
8686 int c = p[i];
8688 if (len_so_far >= MAX_ASCII_LEN)
8690 fputs ("\"\n\t.ascii\t\"", stream);
8691 len_so_far = 0;
8694 if (ISPRINT (c))
8696 if (c == '\\' || c == '\"')
8698 putc ('\\', stream);
8699 len_so_far++;
8701 putc (c, stream);
8702 len_so_far++;
8704 else
8706 fprintf (stream, "\\%03o", c);
8707 len_so_far += 4;
8711 fputs ("\"\n", stream);
8714 /* Compute the register save mask for registers 0 through 12
8715 inclusive. This code is used by arm_compute_save_reg_mask. */
8717 static unsigned long
8718 arm_compute_save_reg0_reg12_mask (void)
8720 unsigned long func_type = arm_current_func_type ();
8721 unsigned long save_reg_mask = 0;
8722 unsigned int reg;
8724 if (IS_INTERRUPT (func_type))
8726 unsigned int max_reg;
8727 /* Interrupt functions must not corrupt any registers,
8728 even call clobbered ones. If this is a leaf function
8729 we can just examine the registers used by the RTL, but
8730 otherwise we have to assume that whatever function is
8731 called might clobber anything, and so we have to save
8732 all the call-clobbered registers as well. */
8733 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8734 /* FIQ handlers have registers r8 - r12 banked, so
8735 we only need to check r0 - r7, Normal ISRs only
8736 bank r14 and r15, so we must check up to r12.
8737 r13 is the stack pointer which is always preserved,
8738 so we do not need to consider it here. */
8739 max_reg = 7;
8740 else
8741 max_reg = 12;
8743 for (reg = 0; reg <= max_reg; reg++)
8744 if (regs_ever_live[reg]
8745 || (! current_function_is_leaf && call_used_regs [reg]))
8746 save_reg_mask |= (1 << reg);
8748 /* Also save the pic base register if necessary. */
8749 if (flag_pic
8750 && !TARGET_SINGLE_PIC_BASE
8751 && current_function_uses_pic_offset_table)
8752 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8754 else
8756 /* In the normal case we only need to save those registers
8757 which are call saved and which are used by this function. */
8758 for (reg = 0; reg <= 10; reg++)
8759 if (regs_ever_live[reg] && ! call_used_regs [reg])
8760 save_reg_mask |= (1 << reg);
8762 /* Handle the frame pointer as a special case. */
8763 if (! TARGET_APCS_FRAME
8764 && ! frame_pointer_needed
8765 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8766 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8767 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8769 /* If we aren't loading the PIC register,
8770 don't stack it even though it may be live. */
8771 if (flag_pic
8772 && !TARGET_SINGLE_PIC_BASE
8773 && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
8774 || current_function_uses_pic_offset_table))
8775 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8778 /* Save registers so the exception handler can modify them. */
8779 if (current_function_calls_eh_return)
8781 unsigned int i;
8783 for (i = 0; ; i++)
8785 reg = EH_RETURN_DATA_REGNO (i);
8786 if (reg == INVALID_REGNUM)
8787 break;
8788 save_reg_mask |= 1 << reg;
8792 return save_reg_mask;
8795 /* Compute a bit mask of which registers need to be
8796 saved on the stack for the current function. */
8798 static unsigned long
8799 arm_compute_save_reg_mask (void)
8801 unsigned int save_reg_mask = 0;
8802 unsigned long func_type = arm_current_func_type ();
8804 if (IS_NAKED (func_type))
8805 /* This should never really happen. */
8806 return 0;
8808 /* If we are creating a stack frame, then we must save the frame pointer,
8809 IP (which will hold the old stack pointer), LR and the PC. */
8810 if (frame_pointer_needed)
8811 save_reg_mask |=
8812 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8813 | (1 << IP_REGNUM)
8814 | (1 << LR_REGNUM)
8815 | (1 << PC_REGNUM);
8817 /* Volatile functions do not return, so there
8818 is no need to save any other registers. */
8819 if (IS_VOLATILE (func_type))
8820 return save_reg_mask;
8822 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8824 /* Decide if we need to save the link register.
8825 Interrupt routines have their own banked link register,
8826 so they never need to save it.
8827 Otherwise if we do not use the link register we do not need to save
8828 it. If we are pushing other registers onto the stack however, we
8829 can save an instruction in the epilogue by pushing the link register
8830 now and then popping it back into the PC. This incurs extra memory
8831 accesses though, so we only do it when optimizing for size, and only
8832 if we know that we will not need a fancy return sequence. */
8833 if (regs_ever_live [LR_REGNUM]
8834 || (save_reg_mask
8835 && optimize_size
8836 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
8837 && !current_function_calls_eh_return))
8838 save_reg_mask |= 1 << LR_REGNUM;
8840 if (cfun->machine->lr_save_eliminated)
8841 save_reg_mask &= ~ (1 << LR_REGNUM);
8843 if (TARGET_REALLY_IWMMXT
8844 && ((bit_count (save_reg_mask)
8845 + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8847 unsigned int reg;
8849 /* The total number of registers that are going to be pushed
8850 onto the stack is odd. We need to ensure that the stack
8851 is 64-bit aligned before we start to save iWMMXt registers,
8852 and also before we start to create locals. (A local variable
8853 might be a double or long long which we will load/store using
8854 an iWMMXt instruction). Therefore we need to push another
8855 ARM register, so that the stack will be 64-bit aligned. We
8856 try to avoid using the arg registers (r0 -r3) as they might be
8857 used to pass values in a tail call. */
8858 for (reg = 4; reg <= 12; reg++)
8859 if ((save_reg_mask & (1 << reg)) == 0)
8860 break;
8862 if (reg <= 12)
8863 save_reg_mask |= (1 << reg);
8864 else
8866 cfun->machine->sibcall_blocked = 1;
8867 save_reg_mask |= (1 << 3);
8871 return save_reg_mask;
8875 /* Compute a bit mask of which registers need to be
8876 saved on the stack for the current function. */
8877 static unsigned long
8878 thumb_compute_save_reg_mask (void)
8880 unsigned long mask;
8881 unsigned reg;
8883 mask = 0;
8884 for (reg = 0; reg < 12; reg ++)
8885 if (regs_ever_live[reg] && !call_used_regs[reg])
8886 mask |= 1 << reg;
8888 if (flag_pic && !TARGET_SINGLE_PIC_BASE)
8889 mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
8891 if (TARGET_SINGLE_PIC_BASE)
8892 mask &= ~(1 << arm_pic_register);
8894 /* See if we might need r11 for calls to _interwork_r11_call_via_rN(). */
8895 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
8896 mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
8898 /* LR will also be pushed if any lo regs are pushed. */
8899 if (mask & 0xff || thumb_force_lr_save ())
8900 mask |= (1 << LR_REGNUM);
8902 /* Make sure we have a low work register if we need one.
8903 We will need one if we are going to push a high register,
8904 but we are not currently intending to push a low register. */
8905 if ((mask & 0xff) == 0
8906 && ((mask & 0x0f00) || TARGET_BACKTRACE))
8908 /* Use thumb_find_work_register to choose which register
8909 we will use. If the register is live then we will
8910 have to push it. Use LAST_LO_REGNUM as our fallback
8911 choice for the register to select. */
8912 reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
8914 if (! call_used_regs[reg])
8915 mask |= 1 << reg;
8918 return mask;
8922 /* Return the number of bytes required to save VFP registers. */
8923 static int
8924 arm_get_vfp_saved_size (void)
8926 unsigned int regno;
8927 int count;
8928 int saved;
8930 saved = 0;
8931 /* Space for saved VFP registers. */
8932 if (TARGET_HARD_FLOAT && TARGET_VFP)
8934 count = 0;
8935 for (regno = FIRST_VFP_REGNUM;
8936 regno < LAST_VFP_REGNUM;
8937 regno += 2)
8939 if ((!regs_ever_live[regno] || call_used_regs[regno])
8940 && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8942 if (count > 0)
8944 /* Workaround ARM10 VFPr1 bug. */
8945 if (count == 2 && !arm_arch6)
8946 count++;
8947 saved += count * 8 + 4;
8949 count = 0;
8951 else
8952 count++;
8954 if (count > 0)
8956 if (count == 2 && !arm_arch6)
8957 count++;
8958 saved += count * 8 + 4;
8961 return saved;
8965 /* Generate a function exit sequence. If REALLY_RETURN is false, then do
8966 everything bar the final return instruction. */
8967 const char *
8968 output_return_instruction (rtx operand, int really_return, int reverse)
8970 char conditional[10];
8971 char instr[100];
8972 unsigned reg;
8973 unsigned long live_regs_mask;
8974 unsigned long func_type;
8975 arm_stack_offsets *offsets;
8977 func_type = arm_current_func_type ();
8979 if (IS_NAKED (func_type))
8980 return "";
8982 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
8984 /* If this function was declared non-returning, and we have
8985 found a tail call, then we have to trust that the called
8986 function won't return. */
8987 if (really_return)
8989 rtx ops[2];
8991 /* Otherwise, trap an attempted return by aborting. */
8992 ops[0] = operand;
8993 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
8994 : "abort");
8995 assemble_external_libcall (ops[1]);
8996 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
8999 return "";
9002 gcc_assert (!current_function_calls_alloca || really_return);
9004 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9006 return_used_this_function = 1;
9008 live_regs_mask = arm_compute_save_reg_mask ();
9010 if (live_regs_mask)
9012 const char * return_reg;
9014 /* If we do not have any special requirements for function exit
9015 (e.g. interworking, or ISR) then we can load the return address
9016 directly into the PC. Otherwise we must load it into LR. */
9017 if (really_return
9018 && ! TARGET_INTERWORK)
9019 return_reg = reg_names[PC_REGNUM];
9020 else
9021 return_reg = reg_names[LR_REGNUM];
9023 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9025 /* There are three possible reasons for the IP register
9026 being saved. 1) a stack frame was created, in which case
9027 IP contains the old stack pointer, or 2) an ISR routine
9028 corrupted it, or 3) it was saved to align the stack on
9029 iWMMXt. In case 1, restore IP into SP, otherwise just
9030 restore IP. */
9031 if (frame_pointer_needed)
9033 live_regs_mask &= ~ (1 << IP_REGNUM);
9034 live_regs_mask |= (1 << SP_REGNUM);
9036 else
9037 gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
9040 /* On some ARM architectures it is faster to use LDR rather than
9041 LDM to load a single register. On other architectures, the
9042 cost is the same. In 26 bit mode, or for exception handlers,
9043 we have to use LDM to load the PC so that the CPSR is also
9044 restored. */
9045 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9046 if (live_regs_mask == (1U << reg))
9047 break;
9049 if (reg <= LAST_ARM_REGNUM
9050 && (reg != LR_REGNUM
9051 || ! really_return
9052 || ! IS_INTERRUPT (func_type)))
9054 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
9055 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9057 else
9059 char *p;
9060 int first = 1;
9062 /* Generate the load multiple instruction to restore the
9063 registers. Note we can get here, even if
9064 frame_pointer_needed is true, but only if sp already
9065 points to the base of the saved core registers. */
9066 if (live_regs_mask & (1 << SP_REGNUM))
9068 unsigned HOST_WIDE_INT stack_adjust;
9070 offsets = arm_get_frame_offsets ();
9071 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9072 gcc_assert (stack_adjust == 0 || stack_adjust == 4);
9074 if (stack_adjust && arm_arch5)
9075 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9076 else
9078 /* If we can't use ldmib (SA110 bug),
9079 then try to pop r3 instead. */
9080 if (stack_adjust)
9081 live_regs_mask |= 1 << 3;
9082 sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9085 else
9086 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9088 p = instr + strlen (instr);
9090 for (reg = 0; reg <= SP_REGNUM; reg++)
9091 if (live_regs_mask & (1 << reg))
9093 int l = strlen (reg_names[reg]);
9095 if (first)
9096 first = 0;
9097 else
9099 memcpy (p, ", ", 2);
9100 p += 2;
9103 memcpy (p, "%|", 2);
9104 memcpy (p + 2, reg_names[reg], l);
9105 p += l + 2;
9108 if (live_regs_mask & (1 << LR_REGNUM))
9110 sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9111 /* If returning from an interrupt, restore the CPSR. */
9112 if (IS_INTERRUPT (func_type))
9113 strcat (p, "^");
9115 else
9116 strcpy (p, "}");
9119 output_asm_insn (instr, & operand);
9121 /* See if we need to generate an extra instruction to
9122 perform the actual function return. */
9123 if (really_return
9124 && func_type != ARM_FT_INTERWORKED
9125 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9127 /* The return has already been handled
9128 by loading the LR into the PC. */
9129 really_return = 0;
9133 if (really_return)
9135 switch ((int) ARM_FUNC_TYPE (func_type))
9137 case ARM_FT_ISR:
9138 case ARM_FT_FIQ:
9139 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9140 break;
9142 case ARM_FT_INTERWORKED:
9143 sprintf (instr, "bx%s\t%%|lr", conditional);
9144 break;
9146 case ARM_FT_EXCEPTION:
9147 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9148 break;
9150 default:
9151 /* Use bx if it's available. */
9152 if (arm_arch5 || arm_arch4t)
9153 sprintf (instr, "bx%s\t%%|lr", conditional);
9154 else
9155 sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9156 break;
9159 output_asm_insn (instr, & operand);
9162 return "";
9165 /* Write the function name into the code section, directly preceding
9166 the function prologue.
9168 Code will be output similar to this:
9170 .ascii "arm_poke_function_name", 0
9171 .align
9173 .word 0xff000000 + (t1 - t0)
9174 arm_poke_function_name
9175 mov ip, sp
9176 stmfd sp!, {fp, ip, lr, pc}
9177 sub fp, ip, #4
9179 When performing a stack backtrace, code can inspect the value
9180 of 'pc' stored at 'fp' + 0. If the trace function then looks
9181 at location pc - 12 and the top 8 bits are set, then we know
9182 that there is a function name embedded immediately preceding this
9183 location and has length ((pc[-3]) & 0xff000000).
9185 We assume that pc is declared as a pointer to an unsigned long.
9187 It is of no benefit to output the function name if we are assembling
9188 a leaf function. These function types will not contain a stack
9189 backtrace structure, therefore it is not possible to determine the
9190 function name. */
9191 void
9192 arm_poke_function_name (FILE *stream, const char *name)
9194 unsigned long alignlength;
9195 unsigned long length;
9196 rtx x;
9198 length = strlen (name) + 1;
9199 alignlength = ROUND_UP_WORD (length);
9201 ASM_OUTPUT_ASCII (stream, name, length);
9202 ASM_OUTPUT_ALIGN (stream, 2);
9203 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9204 assemble_aligned_integer (UNITS_PER_WORD, x);
9207 /* Place some comments into the assembler stream
9208 describing the current function. */
9209 static void
9210 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9212 unsigned long func_type;
9214 if (!TARGET_ARM)
9216 thumb_output_function_prologue (f, frame_size);
9217 return;
9220 /* Sanity check. */
9221 gcc_assert (!arm_ccfsm_state && !arm_target_insn);
9223 func_type = arm_current_func_type ();
9225 switch ((int) ARM_FUNC_TYPE (func_type))
9227 default:
9228 case ARM_FT_NORMAL:
9229 break;
9230 case ARM_FT_INTERWORKED:
9231 asm_fprintf (f, "\t%@ Function supports interworking.\n");
9232 break;
9233 case ARM_FT_ISR:
9234 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9235 break;
9236 case ARM_FT_FIQ:
9237 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9238 break;
9239 case ARM_FT_EXCEPTION:
9240 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9241 break;
9244 if (IS_NAKED (func_type))
9245 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9247 if (IS_VOLATILE (func_type))
9248 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9250 if (IS_NESTED (func_type))
9251 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9253 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9254 current_function_args_size,
9255 current_function_pretend_args_size, frame_size);
9257 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9258 frame_pointer_needed,
9259 cfun->machine->uses_anonymous_args);
9261 if (cfun->machine->lr_save_eliminated)
9262 asm_fprintf (f, "\t%@ link register save eliminated.\n");
9264 if (current_function_calls_eh_return)
9265 asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9267 #ifdef AOF_ASSEMBLER
9268 if (flag_pic)
9269 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9270 #endif
9272 return_used_this_function = 0;
9275 const char *
9276 arm_output_epilogue (rtx sibling)
9278 int reg;
9279 unsigned long saved_regs_mask;
9280 unsigned long func_type;
9281 /* Floats_offset is the offset from the "virtual" frame. In an APCS
9282 frame that is $fp + 4 for a non-variadic function. */
9283 int floats_offset = 0;
9284 rtx operands[3];
9285 FILE * f = asm_out_file;
9286 unsigned int lrm_count = 0;
9287 int really_return = (sibling == NULL);
9288 int start_reg;
9289 arm_stack_offsets *offsets;
9291 /* If we have already generated the return instruction
9292 then it is futile to generate anything else. */
9293 if (use_return_insn (FALSE, sibling) && return_used_this_function)
9294 return "";
9296 func_type = arm_current_func_type ();
9298 if (IS_NAKED (func_type))
9299 /* Naked functions don't have epilogues. */
9300 return "";
9302 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9304 rtx op;
9306 /* A volatile function should never return. Call abort. */
9307 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9308 assemble_external_libcall (op);
9309 output_asm_insn ("bl\t%a0", &op);
9311 return "";
9314 /* If we are throwing an exception, then we really must be doing a
9315 return, so we can't tail-call. */
9316 gcc_assert (!current_function_calls_eh_return || really_return);
9318 offsets = arm_get_frame_offsets ();
9319 saved_regs_mask = arm_compute_save_reg_mask ();
9321 if (TARGET_IWMMXT)
9322 lrm_count = bit_count (saved_regs_mask);
9324 floats_offset = offsets->saved_args;
9325 /* Compute how far away the floats will be. */
9326 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9327 if (saved_regs_mask & (1 << reg))
9328 floats_offset += 4;
9330 if (frame_pointer_needed)
9332 /* This variable is for the Virtual Frame Pointer, not VFP regs. */
9333 int vfp_offset = offsets->frame;
9335 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9337 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9338 if (regs_ever_live[reg] && !call_used_regs[reg])
9340 floats_offset += 12;
9341 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9342 reg, FP_REGNUM, floats_offset - vfp_offset);
9345 else
9347 start_reg = LAST_FPA_REGNUM;
9349 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9351 if (regs_ever_live[reg] && !call_used_regs[reg])
9353 floats_offset += 12;
9355 /* We can't unstack more than four registers at once. */
9356 if (start_reg - reg == 3)
9358 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9359 reg, FP_REGNUM, floats_offset - vfp_offset);
9360 start_reg = reg - 1;
9363 else
9365 if (reg != start_reg)
9366 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9367 reg + 1, start_reg - reg,
9368 FP_REGNUM, floats_offset - vfp_offset);
9369 start_reg = reg - 1;
9373 /* Just in case the last register checked also needs unstacking. */
9374 if (reg != start_reg)
9375 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9376 reg + 1, start_reg - reg,
9377 FP_REGNUM, floats_offset - vfp_offset);
9380 if (TARGET_HARD_FLOAT && TARGET_VFP)
9382 int saved_size;
9384 /* The fldmx insn does not have base+offset addressing modes,
9385 so we use IP to hold the address. */
9386 saved_size = arm_get_vfp_saved_size ();
9388 if (saved_size > 0)
9390 floats_offset += saved_size;
9391 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9392 FP_REGNUM, floats_offset - vfp_offset);
9394 start_reg = FIRST_VFP_REGNUM;
9395 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9397 if ((!regs_ever_live[reg] || call_used_regs[reg])
9398 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9400 if (start_reg != reg)
9401 arm_output_fldmx (f, IP_REGNUM,
9402 (start_reg - FIRST_VFP_REGNUM) / 2,
9403 (reg - start_reg) / 2);
9404 start_reg = reg + 2;
9407 if (start_reg != reg)
9408 arm_output_fldmx (f, IP_REGNUM,
9409 (start_reg - FIRST_VFP_REGNUM) / 2,
9410 (reg - start_reg) / 2);
9413 if (TARGET_IWMMXT)
9415 /* The frame pointer is guaranteed to be non-double-word aligned.
9416 This is because it is set to (old_stack_pointer - 4) and the
9417 old_stack_pointer was double word aligned. Thus the offset to
9418 the iWMMXt registers to be loaded must also be non-double-word
9419 sized, so that the resultant address *is* double-word aligned.
9420 We can ignore floats_offset since that was already included in
9421 the live_regs_mask. */
9422 lrm_count += (lrm_count % 2 ? 2 : 1);
9424 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9425 if (regs_ever_live[reg] && !call_used_regs[reg])
9427 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9428 reg, FP_REGNUM, lrm_count * 4);
9429 lrm_count += 2;
9433 /* saved_regs_mask should contain the IP, which at the time of stack
9434 frame generation actually contains the old stack pointer. So a
9435 quick way to unwind the stack is just pop the IP register directly
9436 into the stack pointer. */
9437 gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
9438 saved_regs_mask &= ~ (1 << IP_REGNUM);
9439 saved_regs_mask |= (1 << SP_REGNUM);
9441 /* There are two registers left in saved_regs_mask - LR and PC. We
9442 only need to restore the LR register (the return address), but to
9443 save time we can load it directly into the PC, unless we need a
9444 special function exit sequence, or we are not really returning. */
9445 if (really_return
9446 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9447 && !current_function_calls_eh_return)
9448 /* Delete the LR from the register mask, so that the LR on
9449 the stack is loaded into the PC in the register mask. */
9450 saved_regs_mask &= ~ (1 << LR_REGNUM);
9451 else
9452 saved_regs_mask &= ~ (1 << PC_REGNUM);
9454 /* We must use SP as the base register, because SP is one of the
9455 registers being restored. If an interrupt or page fault
9456 happens in the ldm instruction, the SP might or might not
9457 have been restored. That would be bad, as then SP will no
9458 longer indicate the safe area of stack, and we can get stack
9459 corruption. Using SP as the base register means that it will
9460 be reset correctly to the original value, should an interrupt
9461 occur. If the stack pointer already points at the right
9462 place, then omit the subtraction. */
9463 if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9464 || current_function_calls_alloca)
9465 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9466 4 * bit_count (saved_regs_mask));
9467 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9469 if (IS_INTERRUPT (func_type))
9470 /* Interrupt handlers will have pushed the
9471 IP onto the stack, so restore it now. */
9472 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9474 else
9476 /* Restore stack pointer if necessary. */
9477 if (offsets->outgoing_args != offsets->saved_regs)
9479 operands[0] = operands[1] = stack_pointer_rtx;
9480 operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9481 output_add_immediate (operands);
9484 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9486 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9487 if (regs_ever_live[reg] && !call_used_regs[reg])
9488 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9489 reg, SP_REGNUM);
9491 else
9493 start_reg = FIRST_FPA_REGNUM;
9495 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9497 if (regs_ever_live[reg] && !call_used_regs[reg])
9499 if (reg - start_reg == 3)
9501 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9502 start_reg, SP_REGNUM);
9503 start_reg = reg + 1;
9506 else
9508 if (reg != start_reg)
9509 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9510 start_reg, reg - start_reg,
9511 SP_REGNUM);
9513 start_reg = reg + 1;
9517 /* Just in case the last register checked also needs unstacking. */
9518 if (reg != start_reg)
9519 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9520 start_reg, reg - start_reg, SP_REGNUM);
9523 if (TARGET_HARD_FLOAT && TARGET_VFP)
9525 start_reg = FIRST_VFP_REGNUM;
9526 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9528 if ((!regs_ever_live[reg] || call_used_regs[reg])
9529 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9531 if (start_reg != reg)
9532 arm_output_fldmx (f, SP_REGNUM,
9533 (start_reg - FIRST_VFP_REGNUM) / 2,
9534 (reg - start_reg) / 2);
9535 start_reg = reg + 2;
9538 if (start_reg != reg)
9539 arm_output_fldmx (f, SP_REGNUM,
9540 (start_reg - FIRST_VFP_REGNUM) / 2,
9541 (reg - start_reg) / 2);
9543 if (TARGET_IWMMXT)
9544 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9545 if (regs_ever_live[reg] && !call_used_regs[reg])
9546 asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9548 /* If we can, restore the LR into the PC. */
9549 if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9550 && really_return
9551 && current_function_pretend_args_size == 0
9552 && saved_regs_mask & (1 << LR_REGNUM)
9553 && !current_function_calls_eh_return)
9555 saved_regs_mask &= ~ (1 << LR_REGNUM);
9556 saved_regs_mask |= (1 << PC_REGNUM);
9559 /* Load the registers off the stack. If we only have one register
9560 to load use the LDR instruction - it is faster. */
9561 if (saved_regs_mask == (1 << LR_REGNUM))
9563 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9565 else if (saved_regs_mask)
9567 if (saved_regs_mask & (1 << SP_REGNUM))
9568 /* Note - write back to the stack register is not enabled
9569 (i.e. "ldmfd sp!..."). We know that the stack pointer is
9570 in the list of registers and if we add writeback the
9571 instruction becomes UNPREDICTABLE. */
9572 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9573 else
9574 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9577 if (current_function_pretend_args_size)
9579 /* Unwind the pre-pushed regs. */
9580 operands[0] = operands[1] = stack_pointer_rtx;
9581 operands[2] = GEN_INT (current_function_pretend_args_size);
9582 output_add_immediate (operands);
9586 /* We may have already restored PC directly from the stack. */
9587 if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
9588 return "";
9590 /* Stack adjustment for exception handler. */
9591 if (current_function_calls_eh_return)
9592 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
9593 ARM_EH_STACKADJ_REGNUM);
9595 /* Generate the return instruction. */
9596 switch ((int) ARM_FUNC_TYPE (func_type))
9598 case ARM_FT_ISR:
9599 case ARM_FT_FIQ:
9600 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9601 break;
9603 case ARM_FT_EXCEPTION:
9604 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9605 break;
9607 case ARM_FT_INTERWORKED:
9608 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9609 break;
9611 default:
9612 if (arm_arch5 || arm_arch4t)
9613 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9614 else
9615 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9616 break;
9619 return "";
9622 static void
9623 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9624 HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9626 arm_stack_offsets *offsets;
9628 if (TARGET_THUMB)
9630 int regno;
9632 /* Emit any call-via-reg trampolines that are needed for v4t support
9633 of call_reg and call_value_reg type insns. */
9634 for (regno = 0; regno < LR_REGNUM; regno++)
9636 rtx label = cfun->machine->call_via[regno];
9638 if (label != NULL)
9640 function_section (current_function_decl);
9641 targetm.asm_out.internal_label (asm_out_file, "L",
9642 CODE_LABEL_NUMBER (label));
9643 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
9647 /* ??? Probably not safe to set this here, since it assumes that a
9648 function will be emitted as assembly immediately after we generate
9649 RTL for it. This does not happen for inline functions. */
9650 return_used_this_function = 0;
9652 else
9654 /* We need to take into account any stack-frame rounding. */
9655 offsets = arm_get_frame_offsets ();
9657 gcc_assert (!use_return_insn (FALSE, NULL)
9658 || !return_used_this_function
9659 || offsets->saved_regs == offsets->outgoing_args
9660 || frame_pointer_needed);
9662 /* Reset the ARM-specific per-function variables. */
9663 after_arm_reorg = 0;
9667 /* Generate and emit an insn that we will recognize as a push_multi.
9668 Unfortunately, since this insn does not reflect very well the actual
9669 semantics of the operation, we need to annotate the insn for the benefit
9670 of DWARF2 frame unwind information. */
9671 static rtx
9672 emit_multi_reg_push (unsigned long mask)
9674 int num_regs = 0;
9675 int num_dwarf_regs;
9676 int i, j;
9677 rtx par;
9678 rtx dwarf;
9679 int dwarf_par_index;
9680 rtx tmp, reg;
9682 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9683 if (mask & (1 << i))
9684 num_regs++;
9686 gcc_assert (num_regs && num_regs <= 16);
9688 /* We don't record the PC in the dwarf frame information. */
9689 num_dwarf_regs = num_regs;
9690 if (mask & (1 << PC_REGNUM))
9691 num_dwarf_regs--;
9693 /* For the body of the insn we are going to generate an UNSPEC in
9694 parallel with several USEs. This allows the insn to be recognized
9695 by the push_multi pattern in the arm.md file. The insn looks
9696 something like this:
9698 (parallel [
9699 (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9700 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9701 (use (reg:SI 11 fp))
9702 (use (reg:SI 12 ip))
9703 (use (reg:SI 14 lr))
9704 (use (reg:SI 15 pc))
9707 For the frame note however, we try to be more explicit and actually
9708 show each register being stored into the stack frame, plus a (single)
9709 decrement of the stack pointer. We do it this way in order to be
9710 friendly to the stack unwinding code, which only wants to see a single
9711 stack decrement per instruction. The RTL we generate for the note looks
9712 something like this:
9714 (sequence [
9715 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9716 (set (mem:SI (reg:SI sp)) (reg:SI r4))
9717 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9718 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9719 (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9722 This sequence is used both by the code to support stack unwinding for
9723 exceptions handlers and the code to generate dwarf2 frame debugging. */
9725 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9726 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9727 dwarf_par_index = 1;
9729 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9731 if (mask & (1 << i))
9733 reg = gen_rtx_REG (SImode, i);
9735 XVECEXP (par, 0, 0)
9736 = gen_rtx_SET (VOIDmode,
9737 gen_rtx_MEM (BLKmode,
9738 gen_rtx_PRE_DEC (BLKmode,
9739 stack_pointer_rtx)),
9740 gen_rtx_UNSPEC (BLKmode,
9741 gen_rtvec (1, reg),
9742 UNSPEC_PUSH_MULT));
9744 if (i != PC_REGNUM)
9746 tmp = gen_rtx_SET (VOIDmode,
9747 gen_rtx_MEM (SImode, stack_pointer_rtx),
9748 reg);
9749 RTX_FRAME_RELATED_P (tmp) = 1;
9750 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9751 dwarf_par_index++;
9754 break;
9758 for (j = 1, i++; j < num_regs; i++)
9760 if (mask & (1 << i))
9762 reg = gen_rtx_REG (SImode, i);
9764 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9766 if (i != PC_REGNUM)
9768 tmp = gen_rtx_SET (VOIDmode,
9769 gen_rtx_MEM (SImode,
9770 plus_constant (stack_pointer_rtx,
9771 4 * j)),
9772 reg);
9773 RTX_FRAME_RELATED_P (tmp) = 1;
9774 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9777 j++;
9781 par = emit_insn (par);
9783 tmp = gen_rtx_SET (SImode,
9784 stack_pointer_rtx,
9785 gen_rtx_PLUS (SImode,
9786 stack_pointer_rtx,
9787 GEN_INT (-4 * num_regs)));
9788 RTX_FRAME_RELATED_P (tmp) = 1;
9789 XVECEXP (dwarf, 0, 0) = tmp;
9791 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9792 REG_NOTES (par));
9793 return par;
9796 static rtx
9797 emit_sfm (int base_reg, int count)
9799 rtx par;
9800 rtx dwarf;
9801 rtx tmp, reg;
9802 int i;
9804 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9805 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9807 reg = gen_rtx_REG (XFmode, base_reg++);
9809 XVECEXP (par, 0, 0)
9810 = gen_rtx_SET (VOIDmode,
9811 gen_rtx_MEM (BLKmode,
9812 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9813 gen_rtx_UNSPEC (BLKmode,
9814 gen_rtvec (1, reg),
9815 UNSPEC_PUSH_MULT));
9816 tmp = gen_rtx_SET (VOIDmode,
9817 gen_rtx_MEM (XFmode, stack_pointer_rtx), reg);
9818 RTX_FRAME_RELATED_P (tmp) = 1;
9819 XVECEXP (dwarf, 0, 1) = tmp;
9821 for (i = 1; i < count; i++)
9823 reg = gen_rtx_REG (XFmode, base_reg++);
9824 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9826 tmp = gen_rtx_SET (VOIDmode,
9827 gen_rtx_MEM (XFmode,
9828 plus_constant (stack_pointer_rtx,
9829 i * 12)),
9830 reg);
9831 RTX_FRAME_RELATED_P (tmp) = 1;
9832 XVECEXP (dwarf, 0, i + 1) = tmp;
9835 tmp = gen_rtx_SET (VOIDmode,
9836 stack_pointer_rtx,
9837 gen_rtx_PLUS (SImode,
9838 stack_pointer_rtx,
9839 GEN_INT (-12 * count)));
9840 RTX_FRAME_RELATED_P (tmp) = 1;
9841 XVECEXP (dwarf, 0, 0) = tmp;
9843 par = emit_insn (par);
9844 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9845 REG_NOTES (par));
9846 return par;
9850 /* Return true if the current function needs to save/restore LR. */
9852 static bool
9853 thumb_force_lr_save (void)
9855 return !cfun->machine->lr_save_eliminated
9856 && (!leaf_function_p ()
9857 || thumb_far_jump_used_p ()
9858 || regs_ever_live [LR_REGNUM]);
9862 /* Compute the distance from register FROM to register TO.
9863 These can be the arg pointer (26), the soft frame pointer (25),
9864 the stack pointer (13) or the hard frame pointer (11).
9865 In thumb mode r7 is used as the soft frame pointer, if needed.
9866 Typical stack layout looks like this:
9868 old stack pointer -> | |
9869 ----
9870 | | \
9871 | | saved arguments for
9872 | | vararg functions
9873 | | /
9875 hard FP & arg pointer -> | | \
9876 | | stack
9877 | | frame
9878 | | /
9880 | | \
9881 | | call saved
9882 | | registers
9883 soft frame pointer -> | | /
9885 | | \
9886 | | local
9887 | | variables
9888 | | /
9890 | | \
9891 | | outgoing
9892 | | arguments
9893 current stack pointer -> | | /
9896 For a given function some or all of these stack components
9897 may not be needed, giving rise to the possibility of
9898 eliminating some of the registers.
9900 The values returned by this function must reflect the behavior
9901 of arm_expand_prologue() and arm_compute_save_reg_mask().
9903 The sign of the number returned reflects the direction of stack
9904 growth, so the values are positive for all eliminations except
9905 from the soft frame pointer to the hard frame pointer.
9907 SFP may point just inside the local variables block to ensure correct
9908 alignment. */
9911 /* Calculate stack offsets. These are used to calculate register elimination
9912 offsets and in prologue/epilogue code. */
9914 static arm_stack_offsets *
9915 arm_get_frame_offsets (void)
9917 struct arm_stack_offsets *offsets;
9918 unsigned long func_type;
9919 int leaf;
9920 int saved;
9921 HOST_WIDE_INT frame_size;
9923 offsets = &cfun->machine->stack_offsets;
9925 /* We need to know if we are a leaf function. Unfortunately, it
9926 is possible to be called after start_sequence has been called,
9927 which causes get_insns to return the insns for the sequence,
9928 not the function, which will cause leaf_function_p to return
9929 the incorrect result.
9931 to know about leaf functions once reload has completed, and the
9932 frame size cannot be changed after that time, so we can safely
9933 use the cached value. */
9935 if (reload_completed)
9936 return offsets;
9938 /* Initially this is the size of the local variables. It will translated
9939 into an offset once we have determined the size of preceding data. */
9940 frame_size = ROUND_UP_WORD (get_frame_size ());
9942 leaf = leaf_function_p ();
9944 /* Space for variadic functions. */
9945 offsets->saved_args = current_function_pretend_args_size;
9947 offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
9949 if (TARGET_ARM)
9951 unsigned int regno;
9953 saved = bit_count (arm_compute_save_reg_mask ()) * 4;
9955 /* We know that SP will be doubleword aligned on entry, and we must
9956 preserve that condition at any subroutine call. We also require the
9957 soft frame pointer to be doubleword aligned. */
9959 if (TARGET_REALLY_IWMMXT)
9961 /* Check for the call-saved iWMMXt registers. */
9962 for (regno = FIRST_IWMMXT_REGNUM;
9963 regno <= LAST_IWMMXT_REGNUM;
9964 regno++)
9965 if (regs_ever_live [regno] && ! call_used_regs [regno])
9966 saved += 8;
9969 func_type = arm_current_func_type ();
9970 if (! IS_VOLATILE (func_type))
9972 /* Space for saved FPA registers. */
9973 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
9974 if (regs_ever_live[regno] && ! call_used_regs[regno])
9975 saved += 12;
9977 /* Space for saved VFP registers. */
9978 if (TARGET_HARD_FLOAT && TARGET_VFP)
9979 saved += arm_get_vfp_saved_size ();
9982 else /* TARGET_THUMB */
9984 saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
9985 if (TARGET_BACKTRACE)
9986 saved += 16;
9989 /* Saved registers include the stack frame. */
9990 offsets->saved_regs = offsets->saved_args + saved;
9991 offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
9992 /* A leaf function does not need any stack alignment if it has nothing
9993 on the stack. */
9994 if (leaf && frame_size == 0)
9996 offsets->outgoing_args = offsets->soft_frame;
9997 return offsets;
10000 /* Ensure SFP has the correct alignment. */
10001 if (ARM_DOUBLEWORD_ALIGN
10002 && (offsets->soft_frame & 7))
10003 offsets->soft_frame += 4;
10005 offsets->outgoing_args = offsets->soft_frame + frame_size
10006 + current_function_outgoing_args_size;
10008 if (ARM_DOUBLEWORD_ALIGN)
10010 /* Ensure SP remains doubleword aligned. */
10011 if (offsets->outgoing_args & 7)
10012 offsets->outgoing_args += 4;
10013 gcc_assert (!(offsets->outgoing_args & 7));
10016 return offsets;
10020 /* Calculate the relative offsets for the different stack pointers. Positive
10021 offsets are in the direction of stack growth. */
10023 HOST_WIDE_INT
10024 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10026 arm_stack_offsets *offsets;
10028 offsets = arm_get_frame_offsets ();
10030 /* OK, now we have enough information to compute the distances.
10031 There must be an entry in these switch tables for each pair
10032 of registers in ELIMINABLE_REGS, even if some of the entries
10033 seem to be redundant or useless. */
10034 switch (from)
10036 case ARG_POINTER_REGNUM:
10037 switch (to)
10039 case THUMB_HARD_FRAME_POINTER_REGNUM:
10040 return 0;
10042 case FRAME_POINTER_REGNUM:
10043 /* This is the reverse of the soft frame pointer
10044 to hard frame pointer elimination below. */
10045 return offsets->soft_frame - offsets->saved_args;
10047 case ARM_HARD_FRAME_POINTER_REGNUM:
10048 /* If there is no stack frame then the hard
10049 frame pointer and the arg pointer coincide. */
10050 if (offsets->frame == offsets->saved_regs)
10051 return 0;
10052 /* FIXME: Not sure about this. Maybe we should always return 0 ? */
10053 return (frame_pointer_needed
10054 && cfun->static_chain_decl != NULL
10055 && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10057 case STACK_POINTER_REGNUM:
10058 /* If nothing has been pushed on the stack at all
10059 then this will return -4. This *is* correct! */
10060 return offsets->outgoing_args - (offsets->saved_args + 4);
10062 default:
10063 gcc_unreachable ();
10065 gcc_unreachable ();
10067 case FRAME_POINTER_REGNUM:
10068 switch (to)
10070 case THUMB_HARD_FRAME_POINTER_REGNUM:
10071 return 0;
10073 case ARM_HARD_FRAME_POINTER_REGNUM:
10074 /* The hard frame pointer points to the top entry in the
10075 stack frame. The soft frame pointer to the bottom entry
10076 in the stack frame. If there is no stack frame at all,
10077 then they are identical. */
10079 return offsets->frame - offsets->soft_frame;
10081 case STACK_POINTER_REGNUM:
10082 return offsets->outgoing_args - offsets->soft_frame;
10084 default:
10085 gcc_unreachable ();
10087 gcc_unreachable ();
10089 default:
10090 /* You cannot eliminate from the stack pointer.
10091 In theory you could eliminate from the hard frame
10092 pointer to the stack pointer, but this will never
10093 happen, since if a stack frame is not needed the
10094 hard frame pointer will never be used. */
10095 gcc_unreachable ();
10100 /* Generate the prologue instructions for entry into an ARM function. */
10101 void
10102 arm_expand_prologue (void)
10104 int reg;
10105 rtx amount;
10106 rtx insn;
10107 rtx ip_rtx;
10108 unsigned long live_regs_mask;
10109 unsigned long func_type;
10110 int fp_offset = 0;
10111 int saved_pretend_args = 0;
10112 int saved_regs = 0;
10113 unsigned HOST_WIDE_INT args_to_push;
10114 arm_stack_offsets *offsets;
10116 func_type = arm_current_func_type ();
10118 /* Naked functions don't have prologues. */
10119 if (IS_NAKED (func_type))
10120 return;
10122 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
10123 args_to_push = current_function_pretend_args_size;
10125 /* Compute which register we will have to save onto the stack. */
10126 live_regs_mask = arm_compute_save_reg_mask ();
10128 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10130 if (frame_pointer_needed)
10132 if (IS_INTERRUPT (func_type))
10134 /* Interrupt functions must not corrupt any registers.
10135 Creating a frame pointer however, corrupts the IP
10136 register, so we must push it first. */
10137 insn = emit_multi_reg_push (1 << IP_REGNUM);
10139 /* Do not set RTX_FRAME_RELATED_P on this insn.
10140 The dwarf stack unwinding code only wants to see one
10141 stack decrement per function, and this is not it. If
10142 this instruction is labeled as being part of the frame
10143 creation sequence then dwarf2out_frame_debug_expr will
10144 die when it encounters the assignment of IP to FP
10145 later on, since the use of SP here establishes SP as
10146 the CFA register and not IP.
10148 Anyway this instruction is not really part of the stack
10149 frame creation although it is part of the prologue. */
10151 else if (IS_NESTED (func_type))
10153 /* The Static chain register is the same as the IP register
10154 used as a scratch register during stack frame creation.
10155 To get around this need to find somewhere to store IP
10156 whilst the frame is being created. We try the following
10157 places in order:
10159 1. The last argument register.
10160 2. A slot on the stack above the frame. (This only
10161 works if the function is not a varargs function).
10162 3. Register r3, after pushing the argument registers
10163 onto the stack.
10165 Note - we only need to tell the dwarf2 backend about the SP
10166 adjustment in the second variant; the static chain register
10167 doesn't need to be unwound, as it doesn't contain a value
10168 inherited from the caller. */
10170 if (regs_ever_live[3] == 0)
10172 insn = gen_rtx_REG (SImode, 3);
10173 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10174 insn = emit_insn (insn);
10176 else if (args_to_push == 0)
10178 rtx dwarf;
10179 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10180 insn = gen_rtx_MEM (SImode, insn);
10181 insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10182 insn = emit_insn (insn);
10184 fp_offset = 4;
10186 /* Just tell the dwarf backend that we adjusted SP. */
10187 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10188 gen_rtx_PLUS (SImode, stack_pointer_rtx,
10189 GEN_INT (-fp_offset)));
10190 RTX_FRAME_RELATED_P (insn) = 1;
10191 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10192 dwarf, REG_NOTES (insn));
10194 else
10196 /* Store the args on the stack. */
10197 if (cfun->machine->uses_anonymous_args)
10198 insn = emit_multi_reg_push
10199 ((0xf0 >> (args_to_push / 4)) & 0xf);
10200 else
10201 insn = emit_insn
10202 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10203 GEN_INT (- args_to_push)));
10205 RTX_FRAME_RELATED_P (insn) = 1;
10207 saved_pretend_args = 1;
10208 fp_offset = args_to_push;
10209 args_to_push = 0;
10211 /* Now reuse r3 to preserve IP. */
10212 insn = gen_rtx_REG (SImode, 3);
10213 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10214 (void) emit_insn (insn);
10218 if (fp_offset)
10220 insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10221 insn = gen_rtx_SET (SImode, ip_rtx, insn);
10223 else
10224 insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10226 insn = emit_insn (insn);
10227 RTX_FRAME_RELATED_P (insn) = 1;
10230 if (args_to_push)
10232 /* Push the argument registers, or reserve space for them. */
10233 if (cfun->machine->uses_anonymous_args)
10234 insn = emit_multi_reg_push
10235 ((0xf0 >> (args_to_push / 4)) & 0xf);
10236 else
10237 insn = emit_insn
10238 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10239 GEN_INT (- args_to_push)));
10240 RTX_FRAME_RELATED_P (insn) = 1;
10243 /* If this is an interrupt service routine, and the link register
10244 is going to be pushed, and we are not creating a stack frame,
10245 (which would involve an extra push of IP and a pop in the epilogue)
10246 subtracting four from LR now will mean that the function return
10247 can be done with a single instruction. */
10248 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10249 && (live_regs_mask & (1 << LR_REGNUM)) != 0
10250 && ! frame_pointer_needed)
10251 emit_insn (gen_rtx_SET (SImode,
10252 gen_rtx_REG (SImode, LR_REGNUM),
10253 gen_rtx_PLUS (SImode,
10254 gen_rtx_REG (SImode, LR_REGNUM),
10255 GEN_INT (-4))));
10257 if (live_regs_mask)
10259 insn = emit_multi_reg_push (live_regs_mask);
10260 saved_regs += bit_count (live_regs_mask) * 4;
10261 RTX_FRAME_RELATED_P (insn) = 1;
10264 if (TARGET_IWMMXT)
10265 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10266 if (regs_ever_live[reg] && ! call_used_regs [reg])
10268 insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10269 insn = gen_rtx_MEM (V2SImode, insn);
10270 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10271 gen_rtx_REG (V2SImode, reg)));
10272 RTX_FRAME_RELATED_P (insn) = 1;
10273 saved_regs += 8;
10276 if (! IS_VOLATILE (func_type))
10278 int start_reg;
10280 /* Save any floating point call-saved registers used by this
10281 function. */
10282 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10284 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10285 if (regs_ever_live[reg] && !call_used_regs[reg])
10287 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10288 insn = gen_rtx_MEM (XFmode, insn);
10289 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10290 gen_rtx_REG (XFmode, reg)));
10291 RTX_FRAME_RELATED_P (insn) = 1;
10292 saved_regs += 12;
10295 else
10297 start_reg = LAST_FPA_REGNUM;
10299 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10301 if (regs_ever_live[reg] && !call_used_regs[reg])
10303 if (start_reg - reg == 3)
10305 insn = emit_sfm (reg, 4);
10306 RTX_FRAME_RELATED_P (insn) = 1;
10307 saved_regs += 48;
10308 start_reg = reg - 1;
10311 else
10313 if (start_reg != reg)
10315 insn = emit_sfm (reg + 1, start_reg - reg);
10316 RTX_FRAME_RELATED_P (insn) = 1;
10317 saved_regs += (start_reg - reg) * 12;
10319 start_reg = reg - 1;
10323 if (start_reg != reg)
10325 insn = emit_sfm (reg + 1, start_reg - reg);
10326 saved_regs += (start_reg - reg) * 12;
10327 RTX_FRAME_RELATED_P (insn) = 1;
10330 if (TARGET_HARD_FLOAT && TARGET_VFP)
10332 start_reg = FIRST_VFP_REGNUM;
10334 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10336 if ((!regs_ever_live[reg] || call_used_regs[reg])
10337 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10339 if (start_reg != reg)
10340 saved_regs += vfp_emit_fstmx (start_reg,
10341 (reg - start_reg) / 2);
10342 start_reg = reg + 2;
10345 if (start_reg != reg)
10346 saved_regs += vfp_emit_fstmx (start_reg,
10347 (reg - start_reg) / 2);
10351 if (frame_pointer_needed)
10353 /* Create the new frame pointer. */
10354 insn = GEN_INT (-(4 + args_to_push + fp_offset));
10355 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10356 RTX_FRAME_RELATED_P (insn) = 1;
10358 if (IS_NESTED (func_type))
10360 /* Recover the static chain register. */
10361 if (regs_ever_live [3] == 0
10362 || saved_pretend_args)
10363 insn = gen_rtx_REG (SImode, 3);
10364 else /* if (current_function_pretend_args_size == 0) */
10366 insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10367 GEN_INT (4));
10368 insn = gen_rtx_MEM (SImode, insn);
10371 emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10372 /* Add a USE to stop propagate_one_insn() from barfing. */
10373 emit_insn (gen_prologue_use (ip_rtx));
10377 offsets = arm_get_frame_offsets ();
10378 if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10380 /* This add can produce multiple insns for a large constant, so we
10381 need to get tricky. */
10382 rtx last = get_last_insn ();
10384 amount = GEN_INT (offsets->saved_args + saved_regs
10385 - offsets->outgoing_args);
10387 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10388 amount));
10391 last = last ? NEXT_INSN (last) : get_insns ();
10392 RTX_FRAME_RELATED_P (last) = 1;
10394 while (last != insn);
10396 /* If the frame pointer is needed, emit a special barrier that
10397 will prevent the scheduler from moving stores to the frame
10398 before the stack adjustment. */
10399 if (frame_pointer_needed)
10400 insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10401 hard_frame_pointer_rtx));
10405 if (flag_pic)
10406 arm_load_pic_register (INVALID_REGNUM);
10408 /* If we are profiling, make sure no instructions are scheduled before
10409 the call to mcount. Similarly if the user has requested no
10410 scheduling in the prolog. */
10411 if (current_function_profile || !TARGET_SCHED_PROLOG)
10412 emit_insn (gen_blockage ());
10414 /* If the link register is being kept alive, with the return address in it,
10415 then make sure that it does not get reused by the ce2 pass. */
10416 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10418 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10419 cfun->machine->lr_save_eliminated = 1;
10423 /* If CODE is 'd', then the X is a condition operand and the instruction
10424 should only be executed if the condition is true.
10425 if CODE is 'D', then the X is a condition operand and the instruction
10426 should only be executed if the condition is false: however, if the mode
10427 of the comparison is CCFPEmode, then always execute the instruction -- we
10428 do this because in these circumstances !GE does not necessarily imply LT;
10429 in these cases the instruction pattern will take care to make sure that
10430 an instruction containing %d will follow, thereby undoing the effects of
10431 doing this instruction unconditionally.
10432 If CODE is 'N' then X is a floating point operand that must be negated
10433 before output.
10434 If CODE is 'B' then output a bitwise inverted value of X (a const int).
10435 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
10436 void
10437 arm_print_operand (FILE *stream, rtx x, int code)
10439 switch (code)
10441 case '@':
10442 fputs (ASM_COMMENT_START, stream);
10443 return;
10445 case '_':
10446 fputs (user_label_prefix, stream);
10447 return;
10449 case '|':
10450 fputs (REGISTER_PREFIX, stream);
10451 return;
10453 case '?':
10454 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10456 if (TARGET_THUMB)
10458 output_operand_lossage ("predicated Thumb instruction");
10459 break;
10461 if (current_insn_predicate != NULL)
10463 output_operand_lossage
10464 ("predicated instruction in conditional sequence");
10465 break;
10468 fputs (arm_condition_codes[arm_current_cc], stream);
10470 else if (current_insn_predicate)
10472 enum arm_cond_code code;
10474 if (TARGET_THUMB)
10476 output_operand_lossage ("predicated Thumb instruction");
10477 break;
10480 code = get_arm_condition_code (current_insn_predicate);
10481 fputs (arm_condition_codes[code], stream);
10483 return;
10485 case 'N':
10487 REAL_VALUE_TYPE r;
10488 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10489 r = REAL_VALUE_NEGATE (r);
10490 fprintf (stream, "%s", fp_const_from_val (&r));
10492 return;
10494 case 'B':
10495 if (GET_CODE (x) == CONST_INT)
10497 HOST_WIDE_INT val;
10498 val = ARM_SIGN_EXTEND (~INTVAL (x));
10499 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10501 else
10503 putc ('~', stream);
10504 output_addr_const (stream, x);
10506 return;
10508 case 'i':
10509 fprintf (stream, "%s", arithmetic_instr (x, 1));
10510 return;
10512 /* Truncate Cirrus shift counts. */
10513 case 's':
10514 if (GET_CODE (x) == CONST_INT)
10516 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10517 return;
10519 arm_print_operand (stream, x, 0);
10520 return;
10522 case 'I':
10523 fprintf (stream, "%s", arithmetic_instr (x, 0));
10524 return;
10526 case 'S':
10528 HOST_WIDE_INT val;
10529 const char * shift = shift_op (x, &val);
10531 if (shift)
10533 fprintf (stream, ", %s ", shift_op (x, &val));
10534 if (val == -1)
10535 arm_print_operand (stream, XEXP (x, 1), 0);
10536 else
10537 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10540 return;
10542 /* An explanation of the 'Q', 'R' and 'H' register operands:
10544 In a pair of registers containing a DI or DF value the 'Q'
10545 operand returns the register number of the register containing
10546 the least significant part of the value. The 'R' operand returns
10547 the register number of the register containing the most
10548 significant part of the value.
10550 The 'H' operand returns the higher of the two register numbers.
10551 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10552 same as the 'Q' operand, since the most significant part of the
10553 value is held in the lower number register. The reverse is true
10554 on systems where WORDS_BIG_ENDIAN is false.
10556 The purpose of these operands is to distinguish between cases
10557 where the endian-ness of the values is important (for example
10558 when they are added together), and cases where the endian-ness
10559 is irrelevant, but the order of register operations is important.
10560 For example when loading a value from memory into a register
10561 pair, the endian-ness does not matter. Provided that the value
10562 from the lower memory address is put into the lower numbered
10563 register, and the value from the higher address is put into the
10564 higher numbered register, the load will work regardless of whether
10565 the value being loaded is big-wordian or little-wordian. The
10566 order of the two register loads can matter however, if the address
10567 of the memory location is actually held in one of the registers
10568 being overwritten by the load. */
10569 case 'Q':
10570 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10572 output_operand_lossage ("invalid operand for code '%c'", code);
10573 return;
10576 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10577 return;
10579 case 'R':
10580 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10582 output_operand_lossage ("invalid operand for code '%c'", code);
10583 return;
10586 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10587 return;
10589 case 'H':
10590 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10592 output_operand_lossage ("invalid operand for code '%c'", code);
10593 return;
10596 asm_fprintf (stream, "%r", REGNO (x) + 1);
10597 return;
10599 case 'm':
10600 asm_fprintf (stream, "%r",
10601 GET_CODE (XEXP (x, 0)) == REG
10602 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10603 return;
10605 case 'M':
10606 asm_fprintf (stream, "{%r-%r}",
10607 REGNO (x),
10608 REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10609 return;
10611 case 'd':
10612 /* CONST_TRUE_RTX means always -- that's the default. */
10613 if (x == const_true_rtx)
10614 return;
10616 if (!COMPARISON_P (x))
10618 output_operand_lossage ("invalid operand for code '%c'", code);
10619 return;
10622 fputs (arm_condition_codes[get_arm_condition_code (x)],
10623 stream);
10624 return;
10626 case 'D':
10627 /* CONST_TRUE_RTX means not always -- i.e. never. We shouldn't ever
10628 want to do that. */
10629 if (x == const_true_rtx)
10631 output_operand_lossage ("instruction never exectued");
10632 return;
10634 if (!COMPARISON_P (x))
10636 output_operand_lossage ("invalid operand for code '%c'", code);
10637 return;
10640 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10641 (get_arm_condition_code (x))],
10642 stream);
10643 return;
10645 /* Cirrus registers can be accessed in a variety of ways:
10646 single floating point (f)
10647 double floating point (d)
10648 32bit integer (fx)
10649 64bit integer (dx). */
10650 case 'W': /* Cirrus register in F mode. */
10651 case 'X': /* Cirrus register in D mode. */
10652 case 'Y': /* Cirrus register in FX mode. */
10653 case 'Z': /* Cirrus register in DX mode. */
10654 gcc_assert (GET_CODE (x) == REG
10655 && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
10657 fprintf (stream, "mv%s%s",
10658 code == 'W' ? "f"
10659 : code == 'X' ? "d"
10660 : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10662 return;
10664 /* Print cirrus register in the mode specified by the register's mode. */
10665 case 'V':
10667 int mode = GET_MODE (x);
10669 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10671 output_operand_lossage ("invalid operand for code '%c'", code);
10672 return;
10675 fprintf (stream, "mv%s%s",
10676 mode == DFmode ? "d"
10677 : mode == SImode ? "fx"
10678 : mode == DImode ? "dx"
10679 : "f", reg_names[REGNO (x)] + 2);
10681 return;
10684 case 'U':
10685 if (GET_CODE (x) != REG
10686 || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10687 || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10688 /* Bad value for wCG register number. */
10690 output_operand_lossage ("invalid operand for code '%c'", code);
10691 return;
10694 else
10695 fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10696 return;
10698 /* Print an iWMMXt control register name. */
10699 case 'w':
10700 if (GET_CODE (x) != CONST_INT
10701 || INTVAL (x) < 0
10702 || INTVAL (x) >= 16)
10703 /* Bad value for wC register number. */
10705 output_operand_lossage ("invalid operand for code '%c'", code);
10706 return;
10709 else
10711 static const char * wc_reg_names [16] =
10713 "wCID", "wCon", "wCSSF", "wCASF",
10714 "wC4", "wC5", "wC6", "wC7",
10715 "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10716 "wC12", "wC13", "wC14", "wC15"
10719 fprintf (stream, wc_reg_names [INTVAL (x)]);
10721 return;
10723 /* Print a VFP double precision register name. */
10724 case 'P':
10726 int mode = GET_MODE (x);
10727 int num;
10729 if (mode != DImode && mode != DFmode)
10731 output_operand_lossage ("invalid operand for code '%c'", code);
10732 return;
10735 if (GET_CODE (x) != REG
10736 || !IS_VFP_REGNUM (REGNO (x)))
10738 output_operand_lossage ("invalid operand for code '%c'", code);
10739 return;
10742 num = REGNO(x) - FIRST_VFP_REGNUM;
10743 if (num & 1)
10745 output_operand_lossage ("invalid operand for code '%c'", code);
10746 return;
10749 fprintf (stream, "d%d", num >> 1);
10751 return;
10753 default:
10754 if (x == 0)
10756 output_operand_lossage ("missing operand");
10757 return;
10760 switch (GET_CODE (x))
10762 case REG:
10763 asm_fprintf (stream, "%r", REGNO (x));
10764 break;
10766 case MEM:
10767 output_memory_reference_mode = GET_MODE (x);
10768 output_address (XEXP (x, 0));
10769 break;
10771 case CONST_DOUBLE:
10772 fprintf (stream, "#%s", fp_immediate_constant (x));
10773 break;
10775 default:
10776 gcc_assert (GET_CODE (x) != NEG);
10777 fputc ('#', stream);
10778 output_addr_const (stream, x);
10779 break;
10784 #ifndef AOF_ASSEMBLER
10785 /* Target hook for assembling integer objects. The ARM version needs to
10786 handle word-sized values specially. */
10787 static bool
10788 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10790 if (size == UNITS_PER_WORD && aligned_p)
10792 fputs ("\t.word\t", asm_out_file);
10793 output_addr_const (asm_out_file, x);
10795 /* Mark symbols as position independent. We only do this in the
10796 .text segment, not in the .data segment. */
10797 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10798 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10800 if (GET_CODE (x) == SYMBOL_REF
10801 && (CONSTANT_POOL_ADDRESS_P (x)
10802 || SYMBOL_REF_LOCAL_P (x)))
10803 fputs ("(GOTOFF)", asm_out_file);
10804 else if (GET_CODE (x) == LABEL_REF)
10805 fputs ("(GOTOFF)", asm_out_file);
10806 else
10807 fputs ("(GOT)", asm_out_file);
10809 fputc ('\n', asm_out_file);
10810 return true;
10813 if (arm_vector_mode_supported_p (GET_MODE (x)))
10815 int i, units;
10817 gcc_assert (GET_CODE (x) == CONST_VECTOR);
10819 units = CONST_VECTOR_NUNITS (x);
10821 switch (GET_MODE (x))
10823 case V2SImode: size = 4; break;
10824 case V4HImode: size = 2; break;
10825 case V8QImode: size = 1; break;
10826 default:
10827 gcc_unreachable ();
10830 for (i = 0; i < units; i++)
10832 rtx elt;
10834 elt = CONST_VECTOR_ELT (x, i);
10835 assemble_integer
10836 (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10839 return true;
10842 return default_assemble_integer (x, size, aligned_p);
10846 /* Add a function to the list of static constructors. */
10848 static void
10849 arm_elf_asm_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
10851 if (!TARGET_AAPCS_BASED)
10853 default_named_section_asm_out_constructor (symbol, priority);
10854 return;
10857 /* Put these in the .init_array section, using a special relocation. */
10858 ctors_section ();
10859 assemble_align (POINTER_SIZE);
10860 fputs ("\t.word\t", asm_out_file);
10861 output_addr_const (asm_out_file, symbol);
10862 fputs ("(target1)\n", asm_out_file);
10864 #endif
10866 /* A finite state machine takes care of noticing whether or not instructions
10867 can be conditionally executed, and thus decrease execution time and code
10868 size by deleting branch instructions. The fsm is controlled by
10869 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
10871 /* The state of the fsm controlling condition codes are:
10872 0: normal, do nothing special
10873 1: make ASM_OUTPUT_OPCODE not output this instruction
10874 2: make ASM_OUTPUT_OPCODE not output this instruction
10875 3: make instructions conditional
10876 4: make instructions conditional
10878 State transitions (state->state by whom under condition):
10879 0 -> 1 final_prescan_insn if the `target' is a label
10880 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10881 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10882 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10883 3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10884 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10885 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10886 (the target insn is arm_target_insn).
10888 If the jump clobbers the conditions then we use states 2 and 4.
10890 A similar thing can be done with conditional return insns.
10892 XXX In case the `target' is an unconditional branch, this conditionalising
10893 of the instructions always reduces code size, but not always execution
10894 time. But then, I want to reduce the code size to somewhere near what
10895 /bin/cc produces. */
10897 /* Returns the index of the ARM condition code string in
10898 `arm_condition_codes'. COMPARISON should be an rtx like
10899 `(eq (...) (...))'. */
10900 static enum arm_cond_code
10901 get_arm_condition_code (rtx comparison)
10903 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10904 int code;
10905 enum rtx_code comp_code = GET_CODE (comparison);
10907 if (GET_MODE_CLASS (mode) != MODE_CC)
10908 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10909 XEXP (comparison, 1));
10911 switch (mode)
10913 case CC_DNEmode: code = ARM_NE; goto dominance;
10914 case CC_DEQmode: code = ARM_EQ; goto dominance;
10915 case CC_DGEmode: code = ARM_GE; goto dominance;
10916 case CC_DGTmode: code = ARM_GT; goto dominance;
10917 case CC_DLEmode: code = ARM_LE; goto dominance;
10918 case CC_DLTmode: code = ARM_LT; goto dominance;
10919 case CC_DGEUmode: code = ARM_CS; goto dominance;
10920 case CC_DGTUmode: code = ARM_HI; goto dominance;
10921 case CC_DLEUmode: code = ARM_LS; goto dominance;
10922 case CC_DLTUmode: code = ARM_CC;
10924 dominance:
10925 gcc_assert (comp_code == EQ || comp_code == NE);
10927 if (comp_code == EQ)
10928 return ARM_INVERSE_CONDITION_CODE (code);
10929 return code;
10931 case CC_NOOVmode:
10932 switch (comp_code)
10934 case NE: return ARM_NE;
10935 case EQ: return ARM_EQ;
10936 case GE: return ARM_PL;
10937 case LT: return ARM_MI;
10938 default: gcc_unreachable ();
10941 case CC_Zmode:
10942 switch (comp_code)
10944 case NE: return ARM_NE;
10945 case EQ: return ARM_EQ;
10946 default: gcc_unreachable ();
10949 case CC_Nmode:
10950 switch (comp_code)
10952 case NE: return ARM_MI;
10953 case EQ: return ARM_PL;
10954 default: gcc_unreachable ();
10957 case CCFPEmode:
10958 case CCFPmode:
10959 /* These encodings assume that AC=1 in the FPA system control
10960 byte. This allows us to handle all cases except UNEQ and
10961 LTGT. */
10962 switch (comp_code)
10964 case GE: return ARM_GE;
10965 case GT: return ARM_GT;
10966 case LE: return ARM_LS;
10967 case LT: return ARM_MI;
10968 case NE: return ARM_NE;
10969 case EQ: return ARM_EQ;
10970 case ORDERED: return ARM_VC;
10971 case UNORDERED: return ARM_VS;
10972 case UNLT: return ARM_LT;
10973 case UNLE: return ARM_LE;
10974 case UNGT: return ARM_HI;
10975 case UNGE: return ARM_PL;
10976 /* UNEQ and LTGT do not have a representation. */
10977 case UNEQ: /* Fall through. */
10978 case LTGT: /* Fall through. */
10979 default: gcc_unreachable ();
10982 case CC_SWPmode:
10983 switch (comp_code)
10985 case NE: return ARM_NE;
10986 case EQ: return ARM_EQ;
10987 case GE: return ARM_LE;
10988 case GT: return ARM_LT;
10989 case LE: return ARM_GE;
10990 case LT: return ARM_GT;
10991 case GEU: return ARM_LS;
10992 case GTU: return ARM_CC;
10993 case LEU: return ARM_CS;
10994 case LTU: return ARM_HI;
10995 default: gcc_unreachable ();
10998 case CC_Cmode:
10999 switch (comp_code)
11001 case LTU: return ARM_CS;
11002 case GEU: return ARM_CC;
11003 default: gcc_unreachable ();
11006 case CCmode:
11007 switch (comp_code)
11009 case NE: return ARM_NE;
11010 case EQ: return ARM_EQ;
11011 case GE: return ARM_GE;
11012 case GT: return ARM_GT;
11013 case LE: return ARM_LE;
11014 case LT: return ARM_LT;
11015 case GEU: return ARM_CS;
11016 case GTU: return ARM_HI;
11017 case LEU: return ARM_LS;
11018 case LTU: return ARM_CC;
11019 default: gcc_unreachable ();
11022 default: gcc_unreachable ();
11026 void
11027 arm_final_prescan_insn (rtx insn)
11029 /* BODY will hold the body of INSN. */
11030 rtx body = PATTERN (insn);
11032 /* This will be 1 if trying to repeat the trick, and things need to be
11033 reversed if it appears to fail. */
11034 int reverse = 0;
11036 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11037 taken are clobbered, even if the rtl suggests otherwise. It also
11038 means that we have to grub around within the jump expression to find
11039 out what the conditions are when the jump isn't taken. */
11040 int jump_clobbers = 0;
11042 /* If we start with a return insn, we only succeed if we find another one. */
11043 int seeking_return = 0;
11045 /* START_INSN will hold the insn from where we start looking. This is the
11046 first insn after the following code_label if REVERSE is true. */
11047 rtx start_insn = insn;
11049 /* If in state 4, check if the target branch is reached, in order to
11050 change back to state 0. */
11051 if (arm_ccfsm_state == 4)
11053 if (insn == arm_target_insn)
11055 arm_target_insn = NULL;
11056 arm_ccfsm_state = 0;
11058 return;
11061 /* If in state 3, it is possible to repeat the trick, if this insn is an
11062 unconditional branch to a label, and immediately following this branch
11063 is the previous target label which is only used once, and the label this
11064 branch jumps to is not too far off. */
11065 if (arm_ccfsm_state == 3)
11067 if (simplejump_p (insn))
11069 start_insn = next_nonnote_insn (start_insn);
11070 if (GET_CODE (start_insn) == BARRIER)
11072 /* XXX Isn't this always a barrier? */
11073 start_insn = next_nonnote_insn (start_insn);
11075 if (GET_CODE (start_insn) == CODE_LABEL
11076 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11077 && LABEL_NUSES (start_insn) == 1)
11078 reverse = TRUE;
11079 else
11080 return;
11082 else if (GET_CODE (body) == RETURN)
11084 start_insn = next_nonnote_insn (start_insn);
11085 if (GET_CODE (start_insn) == BARRIER)
11086 start_insn = next_nonnote_insn (start_insn);
11087 if (GET_CODE (start_insn) == CODE_LABEL
11088 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11089 && LABEL_NUSES (start_insn) == 1)
11091 reverse = TRUE;
11092 seeking_return = 1;
11094 else
11095 return;
11097 else
11098 return;
11101 gcc_assert (!arm_ccfsm_state || reverse);
11102 if (GET_CODE (insn) != JUMP_INSN)
11103 return;
11105 /* This jump might be paralleled with a clobber of the condition codes
11106 the jump should always come first */
11107 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11108 body = XVECEXP (body, 0, 0);
11110 if (reverse
11111 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11112 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11114 int insns_skipped;
11115 int fail = FALSE, succeed = FALSE;
11116 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
11117 int then_not_else = TRUE;
11118 rtx this_insn = start_insn, label = 0;
11120 /* If the jump cannot be done with one instruction, we cannot
11121 conditionally execute the instruction in the inverse case. */
11122 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11124 jump_clobbers = 1;
11125 return;
11128 /* Register the insn jumped to. */
11129 if (reverse)
11131 if (!seeking_return)
11132 label = XEXP (SET_SRC (body), 0);
11134 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11135 label = XEXP (XEXP (SET_SRC (body), 1), 0);
11136 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11138 label = XEXP (XEXP (SET_SRC (body), 2), 0);
11139 then_not_else = FALSE;
11141 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11142 seeking_return = 1;
11143 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11145 seeking_return = 1;
11146 then_not_else = FALSE;
11148 else
11149 gcc_unreachable ();
11151 /* See how many insns this branch skips, and what kind of insns. If all
11152 insns are okay, and the label or unconditional branch to the same
11153 label is not too far away, succeed. */
11154 for (insns_skipped = 0;
11155 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11157 rtx scanbody;
11159 this_insn = next_nonnote_insn (this_insn);
11160 if (!this_insn)
11161 break;
11163 switch (GET_CODE (this_insn))
11165 case CODE_LABEL:
11166 /* Succeed if it is the target label, otherwise fail since
11167 control falls in from somewhere else. */
11168 if (this_insn == label)
11170 if (jump_clobbers)
11172 arm_ccfsm_state = 2;
11173 this_insn = next_nonnote_insn (this_insn);
11175 else
11176 arm_ccfsm_state = 1;
11177 succeed = TRUE;
11179 else
11180 fail = TRUE;
11181 break;
11183 case BARRIER:
11184 /* Succeed if the following insn is the target label.
11185 Otherwise fail.
11186 If return insns are used then the last insn in a function
11187 will be a barrier. */
11188 this_insn = next_nonnote_insn (this_insn);
11189 if (this_insn && this_insn == label)
11191 if (jump_clobbers)
11193 arm_ccfsm_state = 2;
11194 this_insn = next_nonnote_insn (this_insn);
11196 else
11197 arm_ccfsm_state = 1;
11198 succeed = TRUE;
11200 else
11201 fail = TRUE;
11202 break;
11204 case CALL_INSN:
11205 /* The AAPCS says that conditional calls should not be
11206 used since they make interworking inefficient (the
11207 linker can't transform BL<cond> into BLX). That's
11208 only a problem if the machine has BLX. */
11209 if (arm_arch5)
11211 fail = TRUE;
11212 break;
11215 /* Succeed if the following insn is the target label, or
11216 if the following two insns are a barrier and the
11217 target label. */
11218 this_insn = next_nonnote_insn (this_insn);
11219 if (this_insn && GET_CODE (this_insn) == BARRIER)
11220 this_insn = next_nonnote_insn (this_insn);
11222 if (this_insn && this_insn == label
11223 && insns_skipped < max_insns_skipped)
11225 if (jump_clobbers)
11227 arm_ccfsm_state = 2;
11228 this_insn = next_nonnote_insn (this_insn);
11230 else
11231 arm_ccfsm_state = 1;
11232 succeed = TRUE;
11234 else
11235 fail = TRUE;
11236 break;
11238 case JUMP_INSN:
11239 /* If this is an unconditional branch to the same label, succeed.
11240 If it is to another label, do nothing. If it is conditional,
11241 fail. */
11242 /* XXX Probably, the tests for SET and the PC are
11243 unnecessary. */
11245 scanbody = PATTERN (this_insn);
11246 if (GET_CODE (scanbody) == SET
11247 && GET_CODE (SET_DEST (scanbody)) == PC)
11249 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11250 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11252 arm_ccfsm_state = 2;
11253 succeed = TRUE;
11255 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11256 fail = TRUE;
11258 /* Fail if a conditional return is undesirable (e.g. on a
11259 StrongARM), but still allow this if optimizing for size. */
11260 else if (GET_CODE (scanbody) == RETURN
11261 && !use_return_insn (TRUE, NULL)
11262 && !optimize_size)
11263 fail = TRUE;
11264 else if (GET_CODE (scanbody) == RETURN
11265 && seeking_return)
11267 arm_ccfsm_state = 2;
11268 succeed = TRUE;
11270 else if (GET_CODE (scanbody) == PARALLEL)
11272 switch (get_attr_conds (this_insn))
11274 case CONDS_NOCOND:
11275 break;
11276 default:
11277 fail = TRUE;
11278 break;
11281 else
11282 fail = TRUE; /* Unrecognized jump (e.g. epilogue). */
11284 break;
11286 case INSN:
11287 /* Instructions using or affecting the condition codes make it
11288 fail. */
11289 scanbody = PATTERN (this_insn);
11290 if (!(GET_CODE (scanbody) == SET
11291 || GET_CODE (scanbody) == PARALLEL)
11292 || get_attr_conds (this_insn) != CONDS_NOCOND)
11293 fail = TRUE;
11295 /* A conditional cirrus instruction must be followed by
11296 a non Cirrus instruction. However, since we
11297 conditionalize instructions in this function and by
11298 the time we get here we can't add instructions
11299 (nops), because shorten_branches() has already been
11300 called, we will disable conditionalizing Cirrus
11301 instructions to be safe. */
11302 if (GET_CODE (scanbody) != USE
11303 && GET_CODE (scanbody) != CLOBBER
11304 && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11305 fail = TRUE;
11306 break;
11308 default:
11309 break;
11312 if (succeed)
11314 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11315 arm_target_label = CODE_LABEL_NUMBER (label);
11316 else
11318 gcc_assert (seeking_return || arm_ccfsm_state == 2);
11320 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11322 this_insn = next_nonnote_insn (this_insn);
11323 gcc_assert (!this_insn
11324 || (GET_CODE (this_insn) != BARRIER
11325 && GET_CODE (this_insn) != CODE_LABEL));
11327 if (!this_insn)
11329 /* Oh, dear! we ran off the end.. give up. */
11330 recog (PATTERN (insn), insn, NULL);
11331 arm_ccfsm_state = 0;
11332 arm_target_insn = NULL;
11333 return;
11335 arm_target_insn = this_insn;
11337 if (jump_clobbers)
11339 gcc_assert (!reverse);
11340 arm_current_cc =
11341 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11342 0), 0), 1));
11343 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11344 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11345 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11346 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11348 else
11350 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11351 what it was. */
11352 if (!reverse)
11353 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11354 0));
11357 if (reverse || then_not_else)
11358 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11361 /* Restore recog_data (getting the attributes of other insns can
11362 destroy this array, but final.c assumes that it remains intact
11363 across this call; since the insn has been recognized already we
11364 call recog direct). */
11365 recog (PATTERN (insn), insn, NULL);
11369 /* Returns true if REGNO is a valid register
11370 for holding a quantity of type MODE. */
11372 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11374 if (GET_MODE_CLASS (mode) == MODE_CC)
11375 return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11377 if (TARGET_THUMB)
11378 /* For the Thumb we only allow values bigger than SImode in
11379 registers 0 - 6, so that there is always a second low
11380 register available to hold the upper part of the value.
11381 We probably we ought to ensure that the register is the
11382 start of an even numbered register pair. */
11383 return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11385 if (IS_CIRRUS_REGNUM (regno))
11386 /* We have outlawed SI values in Cirrus registers because they
11387 reside in the lower 32 bits, but SF values reside in the
11388 upper 32 bits. This causes gcc all sorts of grief. We can't
11389 even split the registers into pairs because Cirrus SI values
11390 get sign extended to 64bits-- aldyh. */
11391 return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11393 if (IS_VFP_REGNUM (regno))
11395 if (mode == SFmode || mode == SImode)
11396 return TRUE;
11398 /* DFmode values are only valid in even register pairs. */
11399 if (mode == DFmode)
11400 return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11401 return FALSE;
11404 if (IS_IWMMXT_GR_REGNUM (regno))
11405 return mode == SImode;
11407 if (IS_IWMMXT_REGNUM (regno))
11408 return VALID_IWMMXT_REG_MODE (mode);
11410 /* We allow any value to be stored in the general registers.
11411 Restrict doubleword quantities to even register pairs so that we can
11412 use ldrd. */
11413 if (regno <= LAST_ARM_REGNUM)
11414 return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11416 if ( regno == FRAME_POINTER_REGNUM
11417 || regno == ARG_POINTER_REGNUM)
11418 /* We only allow integers in the fake hard registers. */
11419 return GET_MODE_CLASS (mode) == MODE_INT;
11421 /* The only registers left are the FPA registers
11422 which we only allow to hold FP values. */
11423 return GET_MODE_CLASS (mode) == MODE_FLOAT
11424 && regno >= FIRST_FPA_REGNUM
11425 && regno <= LAST_FPA_REGNUM;
11429 arm_regno_class (int regno)
11431 if (TARGET_THUMB)
11433 if (regno == STACK_POINTER_REGNUM)
11434 return STACK_REG;
11435 if (regno == CC_REGNUM)
11436 return CC_REG;
11437 if (regno < 8)
11438 return LO_REGS;
11439 return HI_REGS;
11442 if ( regno <= LAST_ARM_REGNUM
11443 || regno == FRAME_POINTER_REGNUM
11444 || regno == ARG_POINTER_REGNUM)
11445 return GENERAL_REGS;
11447 if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11448 return NO_REGS;
11450 if (IS_CIRRUS_REGNUM (regno))
11451 return CIRRUS_REGS;
11453 if (IS_VFP_REGNUM (regno))
11454 return VFP_REGS;
11456 if (IS_IWMMXT_REGNUM (regno))
11457 return IWMMXT_REGS;
11459 if (IS_IWMMXT_GR_REGNUM (regno))
11460 return IWMMXT_GR_REGS;
11462 return FPA_REGS;
11465 /* Handle a special case when computing the offset
11466 of an argument from the frame pointer. */
11468 arm_debugger_arg_offset (int value, rtx addr)
11470 rtx insn;
11472 /* We are only interested if dbxout_parms() failed to compute the offset. */
11473 if (value != 0)
11474 return 0;
11476 /* We can only cope with the case where the address is held in a register. */
11477 if (GET_CODE (addr) != REG)
11478 return 0;
11480 /* If we are using the frame pointer to point at the argument, then
11481 an offset of 0 is correct. */
11482 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11483 return 0;
11485 /* If we are using the stack pointer to point at the
11486 argument, then an offset of 0 is correct. */
11487 if ((TARGET_THUMB || !frame_pointer_needed)
11488 && REGNO (addr) == SP_REGNUM)
11489 return 0;
11491 /* Oh dear. The argument is pointed to by a register rather
11492 than being held in a register, or being stored at a known
11493 offset from the frame pointer. Since GDB only understands
11494 those two kinds of argument we must translate the address
11495 held in the register into an offset from the frame pointer.
11496 We do this by searching through the insns for the function
11497 looking to see where this register gets its value. If the
11498 register is initialized from the frame pointer plus an offset
11499 then we are in luck and we can continue, otherwise we give up.
11501 This code is exercised by producing debugging information
11502 for a function with arguments like this:
11504 double func (double a, double b, int c, double d) {return d;}
11506 Without this code the stab for parameter 'd' will be set to
11507 an offset of 0 from the frame pointer, rather than 8. */
11509 /* The if() statement says:
11511 If the insn is a normal instruction
11512 and if the insn is setting the value in a register
11513 and if the register being set is the register holding the address of the argument
11514 and if the address is computing by an addition
11515 that involves adding to a register
11516 which is the frame pointer
11517 a constant integer
11519 then... */
11521 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11523 if ( GET_CODE (insn) == INSN
11524 && GET_CODE (PATTERN (insn)) == SET
11525 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11526 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11527 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11528 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11529 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11532 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11534 break;
11538 if (value == 0)
11540 debug_rtx (addr);
11541 warning (0, "unable to compute real location of stacked parameter");
11542 value = 8; /* XXX magic hack */
11545 return value;
11548 #define def_mbuiltin(MASK, NAME, TYPE, CODE) \
11549 do \
11551 if ((MASK) & insn_flags) \
11552 lang_hooks.builtin_function ((NAME), (TYPE), (CODE), \
11553 BUILT_IN_MD, NULL, NULL_TREE); \
11555 while (0)
11557 struct builtin_description
11559 const unsigned int mask;
11560 const enum insn_code icode;
11561 const char * const name;
11562 const enum arm_builtins code;
11563 const enum rtx_code comparison;
11564 const unsigned int flag;
11567 static const struct builtin_description bdesc_2arg[] =
11569 #define IWMMXT_BUILTIN(code, string, builtin) \
11570 { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11571 ARM_BUILTIN_##builtin, 0, 0 },
11573 IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11574 IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11575 IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11576 IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11577 IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11578 IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11579 IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11580 IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11581 IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11582 IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11583 IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11584 IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11585 IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11586 IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11587 IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11588 IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11589 IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11590 IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11591 IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11592 IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11593 IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11594 IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11595 IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11596 IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11597 IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11598 IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11599 IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11600 IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11601 IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11602 IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11603 IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11604 IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11605 IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11606 IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11607 IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11608 IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11609 IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11610 IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11611 IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11612 IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11613 IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11614 IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11615 IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11616 IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11617 IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11618 IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11619 IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11620 IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11621 IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11622 IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11623 IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11624 IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11625 IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11626 IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11627 IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11628 IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11629 IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11630 IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11632 #define IWMMXT_BUILTIN2(code, builtin) \
11633 { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11635 IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11636 IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11637 IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11638 IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11639 IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11640 IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11641 IWMMXT_BUILTIN2 (ashlv4hi3_di, WSLLH)
11642 IWMMXT_BUILTIN2 (ashlv4hi3, WSLLHI)
11643 IWMMXT_BUILTIN2 (ashlv2si3_di, WSLLW)
11644 IWMMXT_BUILTIN2 (ashlv2si3, WSLLWI)
11645 IWMMXT_BUILTIN2 (ashldi3_di, WSLLD)
11646 IWMMXT_BUILTIN2 (ashldi3_iwmmxt, WSLLDI)
11647 IWMMXT_BUILTIN2 (lshrv4hi3_di, WSRLH)
11648 IWMMXT_BUILTIN2 (lshrv4hi3, WSRLHI)
11649 IWMMXT_BUILTIN2 (lshrv2si3_di, WSRLW)
11650 IWMMXT_BUILTIN2 (lshrv2si3, WSRLWI)
11651 IWMMXT_BUILTIN2 (lshrdi3_di, WSRLD)
11652 IWMMXT_BUILTIN2 (lshrdi3_iwmmxt, WSRLDI)
11653 IWMMXT_BUILTIN2 (ashrv4hi3_di, WSRAH)
11654 IWMMXT_BUILTIN2 (ashrv4hi3, WSRAHI)
11655 IWMMXT_BUILTIN2 (ashrv2si3_di, WSRAW)
11656 IWMMXT_BUILTIN2 (ashrv2si3, WSRAWI)
11657 IWMMXT_BUILTIN2 (ashrdi3_di, WSRAD)
11658 IWMMXT_BUILTIN2 (ashrdi3_iwmmxt, WSRADI)
11659 IWMMXT_BUILTIN2 (rorv4hi3_di, WRORH)
11660 IWMMXT_BUILTIN2 (rorv4hi3, WRORHI)
11661 IWMMXT_BUILTIN2 (rorv2si3_di, WRORW)
11662 IWMMXT_BUILTIN2 (rorv2si3, WRORWI)
11663 IWMMXT_BUILTIN2 (rordi3_di, WRORD)
11664 IWMMXT_BUILTIN2 (rordi3, WRORDI)
11665 IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
11666 IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
11669 static const struct builtin_description bdesc_1arg[] =
11671 IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11672 IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11673 IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11674 IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11675 IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11676 IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11677 IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11678 IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11679 IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11680 IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11681 IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11682 IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11683 IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11684 IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11685 IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11686 IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11687 IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11688 IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11691 /* Set up all the iWMMXt builtins. This is
11692 not called if TARGET_IWMMXT is zero. */
11694 static void
11695 arm_init_iwmmxt_builtins (void)
11697 const struct builtin_description * d;
11698 size_t i;
11699 tree endlink = void_list_node;
11701 tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11702 tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11703 tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11705 tree int_ftype_int
11706 = build_function_type (integer_type_node,
11707 tree_cons (NULL_TREE, integer_type_node, endlink));
11708 tree v8qi_ftype_v8qi_v8qi_int
11709 = build_function_type (V8QI_type_node,
11710 tree_cons (NULL_TREE, V8QI_type_node,
11711 tree_cons (NULL_TREE, V8QI_type_node,
11712 tree_cons (NULL_TREE,
11713 integer_type_node,
11714 endlink))));
11715 tree v4hi_ftype_v4hi_int
11716 = build_function_type (V4HI_type_node,
11717 tree_cons (NULL_TREE, V4HI_type_node,
11718 tree_cons (NULL_TREE, integer_type_node,
11719 endlink)));
11720 tree v2si_ftype_v2si_int
11721 = build_function_type (V2SI_type_node,
11722 tree_cons (NULL_TREE, V2SI_type_node,
11723 tree_cons (NULL_TREE, integer_type_node,
11724 endlink)));
11725 tree v2si_ftype_di_di
11726 = build_function_type (V2SI_type_node,
11727 tree_cons (NULL_TREE, long_long_integer_type_node,
11728 tree_cons (NULL_TREE, long_long_integer_type_node,
11729 endlink)));
11730 tree di_ftype_di_int
11731 = build_function_type (long_long_integer_type_node,
11732 tree_cons (NULL_TREE, long_long_integer_type_node,
11733 tree_cons (NULL_TREE, integer_type_node,
11734 endlink)));
11735 tree di_ftype_di_int_int
11736 = build_function_type (long_long_integer_type_node,
11737 tree_cons (NULL_TREE, long_long_integer_type_node,
11738 tree_cons (NULL_TREE, integer_type_node,
11739 tree_cons (NULL_TREE,
11740 integer_type_node,
11741 endlink))));
11742 tree int_ftype_v8qi
11743 = build_function_type (integer_type_node,
11744 tree_cons (NULL_TREE, V8QI_type_node,
11745 endlink));
11746 tree int_ftype_v4hi
11747 = build_function_type (integer_type_node,
11748 tree_cons (NULL_TREE, V4HI_type_node,
11749 endlink));
11750 tree int_ftype_v2si
11751 = build_function_type (integer_type_node,
11752 tree_cons (NULL_TREE, V2SI_type_node,
11753 endlink));
11754 tree int_ftype_v8qi_int
11755 = build_function_type (integer_type_node,
11756 tree_cons (NULL_TREE, V8QI_type_node,
11757 tree_cons (NULL_TREE, integer_type_node,
11758 endlink)));
11759 tree int_ftype_v4hi_int
11760 = build_function_type (integer_type_node,
11761 tree_cons (NULL_TREE, V4HI_type_node,
11762 tree_cons (NULL_TREE, integer_type_node,
11763 endlink)));
11764 tree int_ftype_v2si_int
11765 = build_function_type (integer_type_node,
11766 tree_cons (NULL_TREE, V2SI_type_node,
11767 tree_cons (NULL_TREE, integer_type_node,
11768 endlink)));
11769 tree v8qi_ftype_v8qi_int_int
11770 = build_function_type (V8QI_type_node,
11771 tree_cons (NULL_TREE, V8QI_type_node,
11772 tree_cons (NULL_TREE, integer_type_node,
11773 tree_cons (NULL_TREE,
11774 integer_type_node,
11775 endlink))));
11776 tree v4hi_ftype_v4hi_int_int
11777 = build_function_type (V4HI_type_node,
11778 tree_cons (NULL_TREE, V4HI_type_node,
11779 tree_cons (NULL_TREE, integer_type_node,
11780 tree_cons (NULL_TREE,
11781 integer_type_node,
11782 endlink))));
11783 tree v2si_ftype_v2si_int_int
11784 = build_function_type (V2SI_type_node,
11785 tree_cons (NULL_TREE, V2SI_type_node,
11786 tree_cons (NULL_TREE, integer_type_node,
11787 tree_cons (NULL_TREE,
11788 integer_type_node,
11789 endlink))));
11790 /* Miscellaneous. */
11791 tree v8qi_ftype_v4hi_v4hi
11792 = build_function_type (V8QI_type_node,
11793 tree_cons (NULL_TREE, V4HI_type_node,
11794 tree_cons (NULL_TREE, V4HI_type_node,
11795 endlink)));
11796 tree v4hi_ftype_v2si_v2si
11797 = build_function_type (V4HI_type_node,
11798 tree_cons (NULL_TREE, V2SI_type_node,
11799 tree_cons (NULL_TREE, V2SI_type_node,
11800 endlink)));
11801 tree v2si_ftype_v4hi_v4hi
11802 = build_function_type (V2SI_type_node,
11803 tree_cons (NULL_TREE, V4HI_type_node,
11804 tree_cons (NULL_TREE, V4HI_type_node,
11805 endlink)));
11806 tree v2si_ftype_v8qi_v8qi
11807 = build_function_type (V2SI_type_node,
11808 tree_cons (NULL_TREE, V8QI_type_node,
11809 tree_cons (NULL_TREE, V8QI_type_node,
11810 endlink)));
11811 tree v4hi_ftype_v4hi_di
11812 = build_function_type (V4HI_type_node,
11813 tree_cons (NULL_TREE, V4HI_type_node,
11814 tree_cons (NULL_TREE,
11815 long_long_integer_type_node,
11816 endlink)));
11817 tree v2si_ftype_v2si_di
11818 = build_function_type (V2SI_type_node,
11819 tree_cons (NULL_TREE, V2SI_type_node,
11820 tree_cons (NULL_TREE,
11821 long_long_integer_type_node,
11822 endlink)));
11823 tree void_ftype_int_int
11824 = build_function_type (void_type_node,
11825 tree_cons (NULL_TREE, integer_type_node,
11826 tree_cons (NULL_TREE, integer_type_node,
11827 endlink)));
11828 tree di_ftype_void
11829 = build_function_type (long_long_unsigned_type_node, endlink);
11830 tree di_ftype_v8qi
11831 = build_function_type (long_long_integer_type_node,
11832 tree_cons (NULL_TREE, V8QI_type_node,
11833 endlink));
11834 tree di_ftype_v4hi
11835 = build_function_type (long_long_integer_type_node,
11836 tree_cons (NULL_TREE, V4HI_type_node,
11837 endlink));
11838 tree di_ftype_v2si
11839 = build_function_type (long_long_integer_type_node,
11840 tree_cons (NULL_TREE, V2SI_type_node,
11841 endlink));
11842 tree v2si_ftype_v4hi
11843 = build_function_type (V2SI_type_node,
11844 tree_cons (NULL_TREE, V4HI_type_node,
11845 endlink));
11846 tree v4hi_ftype_v8qi
11847 = build_function_type (V4HI_type_node,
11848 tree_cons (NULL_TREE, V8QI_type_node,
11849 endlink));
11851 tree di_ftype_di_v4hi_v4hi
11852 = build_function_type (long_long_unsigned_type_node,
11853 tree_cons (NULL_TREE,
11854 long_long_unsigned_type_node,
11855 tree_cons (NULL_TREE, V4HI_type_node,
11856 tree_cons (NULL_TREE,
11857 V4HI_type_node,
11858 endlink))));
11860 tree di_ftype_v4hi_v4hi
11861 = build_function_type (long_long_unsigned_type_node,
11862 tree_cons (NULL_TREE, V4HI_type_node,
11863 tree_cons (NULL_TREE, V4HI_type_node,
11864 endlink)));
11866 /* Normal vector binops. */
11867 tree v8qi_ftype_v8qi_v8qi
11868 = build_function_type (V8QI_type_node,
11869 tree_cons (NULL_TREE, V8QI_type_node,
11870 tree_cons (NULL_TREE, V8QI_type_node,
11871 endlink)));
11872 tree v4hi_ftype_v4hi_v4hi
11873 = build_function_type (V4HI_type_node,
11874 tree_cons (NULL_TREE, V4HI_type_node,
11875 tree_cons (NULL_TREE, V4HI_type_node,
11876 endlink)));
11877 tree v2si_ftype_v2si_v2si
11878 = build_function_type (V2SI_type_node,
11879 tree_cons (NULL_TREE, V2SI_type_node,
11880 tree_cons (NULL_TREE, V2SI_type_node,
11881 endlink)));
11882 tree di_ftype_di_di
11883 = build_function_type (long_long_unsigned_type_node,
11884 tree_cons (NULL_TREE, long_long_unsigned_type_node,
11885 tree_cons (NULL_TREE,
11886 long_long_unsigned_type_node,
11887 endlink)));
11889 /* Add all builtins that are more or less simple operations on two
11890 operands. */
11891 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11893 /* Use one of the operands; the target can have a different mode for
11894 mask-generating compares. */
11895 enum machine_mode mode;
11896 tree type;
11898 if (d->name == 0)
11899 continue;
11901 mode = insn_data[d->icode].operand[1].mode;
11903 switch (mode)
11905 case V8QImode:
11906 type = v8qi_ftype_v8qi_v8qi;
11907 break;
11908 case V4HImode:
11909 type = v4hi_ftype_v4hi_v4hi;
11910 break;
11911 case V2SImode:
11912 type = v2si_ftype_v2si_v2si;
11913 break;
11914 case DImode:
11915 type = di_ftype_di_di;
11916 break;
11918 default:
11919 gcc_unreachable ();
11922 def_mbuiltin (d->mask, d->name, type, d->code);
11925 /* Add the remaining MMX insns with somewhat more complicated types. */
11926 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11927 def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11928 def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11930 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11931 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11932 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11933 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11934 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11935 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11937 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11938 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11939 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11940 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11941 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11942 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11944 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11945 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11946 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11947 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11948 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11949 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11951 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11952 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11953 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11954 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11955 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11956 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11958 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11960 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11961 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11962 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11963 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11965 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11966 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11967 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11968 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11969 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11970 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11971 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11972 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11973 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11975 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11976 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11977 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11979 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11980 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11981 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
11983 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
11984 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
11985 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
11986 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
11987 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
11988 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
11990 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
11991 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
11992 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
11993 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
11994 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
11995 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
11996 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
11997 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
11998 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
11999 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
12000 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
12001 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
12003 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
12004 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
12005 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
12006 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
12008 def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
12009 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
12010 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
12011 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
12012 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
12013 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
12014 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
12017 static void
12018 arm_init_builtins (void)
12020 if (TARGET_REALLY_IWMMXT)
12021 arm_init_iwmmxt_builtins ();
12024 /* Errors in the source file can cause expand_expr to return const0_rtx
12025 where we expect a vector. To avoid crashing, use one of the vector
12026 clear instructions. */
12028 static rtx
12029 safe_vector_operand (rtx x, enum machine_mode mode)
12031 if (x != const0_rtx)
12032 return x;
12033 x = gen_reg_rtx (mode);
12035 emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12036 : gen_rtx_SUBREG (DImode, x, 0)));
12037 return x;
12040 /* Subroutine of arm_expand_builtin to take care of binop insns. */
12042 static rtx
12043 arm_expand_binop_builtin (enum insn_code icode,
12044 tree arglist, rtx target)
12046 rtx pat;
12047 tree arg0 = TREE_VALUE (arglist);
12048 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12049 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12050 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12051 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12052 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12053 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12055 if (VECTOR_MODE_P (mode0))
12056 op0 = safe_vector_operand (op0, mode0);
12057 if (VECTOR_MODE_P (mode1))
12058 op1 = safe_vector_operand (op1, mode1);
12060 if (! target
12061 || GET_MODE (target) != tmode
12062 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12063 target = gen_reg_rtx (tmode);
12065 gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
12067 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12068 op0 = copy_to_mode_reg (mode0, op0);
12069 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12070 op1 = copy_to_mode_reg (mode1, op1);
12072 pat = GEN_FCN (icode) (target, op0, op1);
12073 if (! pat)
12074 return 0;
12075 emit_insn (pat);
12076 return target;
12079 /* Subroutine of arm_expand_builtin to take care of unop insns. */
12081 static rtx
12082 arm_expand_unop_builtin (enum insn_code icode,
12083 tree arglist, rtx target, int do_load)
12085 rtx pat;
12086 tree arg0 = TREE_VALUE (arglist);
12087 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12088 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12089 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12091 if (! target
12092 || GET_MODE (target) != tmode
12093 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12094 target = gen_reg_rtx (tmode);
12095 if (do_load)
12096 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12097 else
12099 if (VECTOR_MODE_P (mode0))
12100 op0 = safe_vector_operand (op0, mode0);
12102 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12103 op0 = copy_to_mode_reg (mode0, op0);
12106 pat = GEN_FCN (icode) (target, op0);
12107 if (! pat)
12108 return 0;
12109 emit_insn (pat);
12110 return target;
12113 /* Expand an expression EXP that calls a built-in function,
12114 with result going to TARGET if that's convenient
12115 (and in mode MODE if that's convenient).
12116 SUBTARGET may be used as the target for computing one of EXP's operands.
12117 IGNORE is nonzero if the value is to be ignored. */
12119 static rtx
12120 arm_expand_builtin (tree exp,
12121 rtx target,
12122 rtx subtarget ATTRIBUTE_UNUSED,
12123 enum machine_mode mode ATTRIBUTE_UNUSED,
12124 int ignore ATTRIBUTE_UNUSED)
12126 const struct builtin_description * d;
12127 enum insn_code icode;
12128 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12129 tree arglist = TREE_OPERAND (exp, 1);
12130 tree arg0;
12131 tree arg1;
12132 tree arg2;
12133 rtx op0;
12134 rtx op1;
12135 rtx op2;
12136 rtx pat;
12137 int fcode = DECL_FUNCTION_CODE (fndecl);
12138 size_t i;
12139 enum machine_mode tmode;
12140 enum machine_mode mode0;
12141 enum machine_mode mode1;
12142 enum machine_mode mode2;
12144 switch (fcode)
12146 case ARM_BUILTIN_TEXTRMSB:
12147 case ARM_BUILTIN_TEXTRMUB:
12148 case ARM_BUILTIN_TEXTRMSH:
12149 case ARM_BUILTIN_TEXTRMUH:
12150 case ARM_BUILTIN_TEXTRMSW:
12151 case ARM_BUILTIN_TEXTRMUW:
12152 icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12153 : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12154 : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12155 : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12156 : CODE_FOR_iwmmxt_textrmw);
12158 arg0 = TREE_VALUE (arglist);
12159 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12160 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12161 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12162 tmode = insn_data[icode].operand[0].mode;
12163 mode0 = insn_data[icode].operand[1].mode;
12164 mode1 = insn_data[icode].operand[2].mode;
12166 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12167 op0 = copy_to_mode_reg (mode0, op0);
12168 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12170 /* @@@ better error message */
12171 error ("selector must be an immediate");
12172 return gen_reg_rtx (tmode);
12174 if (target == 0
12175 || GET_MODE (target) != tmode
12176 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12177 target = gen_reg_rtx (tmode);
12178 pat = GEN_FCN (icode) (target, op0, op1);
12179 if (! pat)
12180 return 0;
12181 emit_insn (pat);
12182 return target;
12184 case ARM_BUILTIN_TINSRB:
12185 case ARM_BUILTIN_TINSRH:
12186 case ARM_BUILTIN_TINSRW:
12187 icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12188 : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12189 : CODE_FOR_iwmmxt_tinsrw);
12190 arg0 = TREE_VALUE (arglist);
12191 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12192 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12193 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12194 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12195 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12196 tmode = insn_data[icode].operand[0].mode;
12197 mode0 = insn_data[icode].operand[1].mode;
12198 mode1 = insn_data[icode].operand[2].mode;
12199 mode2 = insn_data[icode].operand[3].mode;
12201 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12202 op0 = copy_to_mode_reg (mode0, op0);
12203 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12204 op1 = copy_to_mode_reg (mode1, op1);
12205 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12207 /* @@@ better error message */
12208 error ("selector must be an immediate");
12209 return const0_rtx;
12211 if (target == 0
12212 || GET_MODE (target) != tmode
12213 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12214 target = gen_reg_rtx (tmode);
12215 pat = GEN_FCN (icode) (target, op0, op1, op2);
12216 if (! pat)
12217 return 0;
12218 emit_insn (pat);
12219 return target;
12221 case ARM_BUILTIN_SETWCX:
12222 arg0 = TREE_VALUE (arglist);
12223 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12224 op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12225 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12226 emit_insn (gen_iwmmxt_tmcr (op1, op0));
12227 return 0;
12229 case ARM_BUILTIN_GETWCX:
12230 arg0 = TREE_VALUE (arglist);
12231 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12232 target = gen_reg_rtx (SImode);
12233 emit_insn (gen_iwmmxt_tmrc (target, op0));
12234 return target;
12236 case ARM_BUILTIN_WSHUFH:
12237 icode = CODE_FOR_iwmmxt_wshufh;
12238 arg0 = TREE_VALUE (arglist);
12239 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12240 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12241 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12242 tmode = insn_data[icode].operand[0].mode;
12243 mode1 = insn_data[icode].operand[1].mode;
12244 mode2 = insn_data[icode].operand[2].mode;
12246 if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12247 op0 = copy_to_mode_reg (mode1, op0);
12248 if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12250 /* @@@ better error message */
12251 error ("mask must be an immediate");
12252 return const0_rtx;
12254 if (target == 0
12255 || GET_MODE (target) != tmode
12256 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12257 target = gen_reg_rtx (tmode);
12258 pat = GEN_FCN (icode) (target, op0, op1);
12259 if (! pat)
12260 return 0;
12261 emit_insn (pat);
12262 return target;
12264 case ARM_BUILTIN_WSADB:
12265 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12266 case ARM_BUILTIN_WSADH:
12267 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12268 case ARM_BUILTIN_WSADBZ:
12269 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12270 case ARM_BUILTIN_WSADHZ:
12271 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12273 /* Several three-argument builtins. */
12274 case ARM_BUILTIN_WMACS:
12275 case ARM_BUILTIN_WMACU:
12276 case ARM_BUILTIN_WALIGN:
12277 case ARM_BUILTIN_TMIA:
12278 case ARM_BUILTIN_TMIAPH:
12279 case ARM_BUILTIN_TMIATT:
12280 case ARM_BUILTIN_TMIATB:
12281 case ARM_BUILTIN_TMIABT:
12282 case ARM_BUILTIN_TMIABB:
12283 icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12284 : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12285 : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12286 : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12287 : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12288 : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12289 : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12290 : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12291 : CODE_FOR_iwmmxt_walign);
12292 arg0 = TREE_VALUE (arglist);
12293 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12294 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12295 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12296 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12297 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12298 tmode = insn_data[icode].operand[0].mode;
12299 mode0 = insn_data[icode].operand[1].mode;
12300 mode1 = insn_data[icode].operand[2].mode;
12301 mode2 = insn_data[icode].operand[3].mode;
12303 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12304 op0 = copy_to_mode_reg (mode0, op0);
12305 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12306 op1 = copy_to_mode_reg (mode1, op1);
12307 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12308 op2 = copy_to_mode_reg (mode2, op2);
12309 if (target == 0
12310 || GET_MODE (target) != tmode
12311 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12312 target = gen_reg_rtx (tmode);
12313 pat = GEN_FCN (icode) (target, op0, op1, op2);
12314 if (! pat)
12315 return 0;
12316 emit_insn (pat);
12317 return target;
12319 case ARM_BUILTIN_WZERO:
12320 target = gen_reg_rtx (DImode);
12321 emit_insn (gen_iwmmxt_clrdi (target));
12322 return target;
12324 default:
12325 break;
12328 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12329 if (d->code == (const enum arm_builtins) fcode)
12330 return arm_expand_binop_builtin (d->icode, arglist, target);
12332 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12333 if (d->code == (const enum arm_builtins) fcode)
12334 return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12336 /* @@@ Should really do something sensible here. */
12337 return NULL_RTX;
12340 /* Return the number (counting from 0) of
12341 the least significant set bit in MASK. */
12343 inline static int
12344 number_of_first_bit_set (unsigned mask)
12346 int bit;
12348 for (bit = 0;
12349 (mask & (1 << bit)) == 0;
12350 ++bit)
12351 continue;
12353 return bit;
12356 /* Emit code to push or pop registers to or from the stack. F is the
12357 assembly file. MASK is the registers to push or pop. PUSH is
12358 nonzero if we should push, and zero if we should pop. For debugging
12359 output, if pushing, adjust CFA_OFFSET by the amount of space added
12360 to the stack. REAL_REGS should have the same number of bits set as
12361 MASK, and will be used instead (in the same order) to describe which
12362 registers were saved - this is used to mark the save slots when we
12363 push high registers after moving them to low registers. */
12364 static void
12365 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
12366 unsigned long real_regs)
12368 int regno;
12369 int lo_mask = mask & 0xFF;
12370 int pushed_words = 0;
12372 gcc_assert (mask);
12374 if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12376 /* Special case. Do not generate a POP PC statement here, do it in
12377 thumb_exit() */
12378 thumb_exit (f, -1);
12379 return;
12382 fprintf (f, "\t%s\t{", push ? "push" : "pop");
12384 /* Look at the low registers first. */
12385 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12387 if (lo_mask & 1)
12389 asm_fprintf (f, "%r", regno);
12391 if ((lo_mask & ~1) != 0)
12392 fprintf (f, ", ");
12394 pushed_words++;
12398 if (push && (mask & (1 << LR_REGNUM)))
12400 /* Catch pushing the LR. */
12401 if (mask & 0xFF)
12402 fprintf (f, ", ");
12404 asm_fprintf (f, "%r", LR_REGNUM);
12406 pushed_words++;
12408 else if (!push && (mask & (1 << PC_REGNUM)))
12410 /* Catch popping the PC. */
12411 if (TARGET_INTERWORK || TARGET_BACKTRACE
12412 || current_function_calls_eh_return)
12414 /* The PC is never poped directly, instead
12415 it is popped into r3 and then BX is used. */
12416 fprintf (f, "}\n");
12418 thumb_exit (f, -1);
12420 return;
12422 else
12424 if (mask & 0xFF)
12425 fprintf (f, ", ");
12427 asm_fprintf (f, "%r", PC_REGNUM);
12431 fprintf (f, "}\n");
12433 if (push && pushed_words && dwarf2out_do_frame ())
12435 char *l = dwarf2out_cfi_label ();
12436 int pushed_mask = real_regs;
12438 *cfa_offset += pushed_words * 4;
12439 dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12441 pushed_words = 0;
12442 pushed_mask = real_regs;
12443 for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12445 if (pushed_mask & 1)
12446 dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12451 /* Generate code to return from a thumb function.
12452 If 'reg_containing_return_addr' is -1, then the return address is
12453 actually on the stack, at the stack pointer. */
12454 static void
12455 thumb_exit (FILE *f, int reg_containing_return_addr)
12457 unsigned regs_available_for_popping;
12458 unsigned regs_to_pop;
12459 int pops_needed;
12460 unsigned available;
12461 unsigned required;
12462 int mode;
12463 int size;
12464 int restore_a4 = FALSE;
12466 /* Compute the registers we need to pop. */
12467 regs_to_pop = 0;
12468 pops_needed = 0;
12470 if (reg_containing_return_addr == -1)
12472 regs_to_pop |= 1 << LR_REGNUM;
12473 ++pops_needed;
12476 if (TARGET_BACKTRACE)
12478 /* Restore the (ARM) frame pointer and stack pointer. */
12479 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12480 pops_needed += 2;
12483 /* If there is nothing to pop then just emit the BX instruction and
12484 return. */
12485 if (pops_needed == 0)
12487 if (current_function_calls_eh_return)
12488 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12490 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12491 return;
12493 /* Otherwise if we are not supporting interworking and we have not created
12494 a backtrace structure and the function was not entered in ARM mode then
12495 just pop the return address straight into the PC. */
12496 else if (!TARGET_INTERWORK
12497 && !TARGET_BACKTRACE
12498 && !is_called_in_ARM_mode (current_function_decl)
12499 && !current_function_calls_eh_return)
12501 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12502 return;
12505 /* Find out how many of the (return) argument registers we can corrupt. */
12506 regs_available_for_popping = 0;
12508 /* If returning via __builtin_eh_return, the bottom three registers
12509 all contain information needed for the return. */
12510 if (current_function_calls_eh_return)
12511 size = 12;
12512 else
12514 /* If we can deduce the registers used from the function's
12515 return value. This is more reliable that examining
12516 regs_ever_live[] because that will be set if the register is
12517 ever used in the function, not just if the register is used
12518 to hold a return value. */
12520 if (current_function_return_rtx != 0)
12521 mode = GET_MODE (current_function_return_rtx);
12522 else
12523 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12525 size = GET_MODE_SIZE (mode);
12527 if (size == 0)
12529 /* In a void function we can use any argument register.
12530 In a function that returns a structure on the stack
12531 we can use the second and third argument registers. */
12532 if (mode == VOIDmode)
12533 regs_available_for_popping =
12534 (1 << ARG_REGISTER (1))
12535 | (1 << ARG_REGISTER (2))
12536 | (1 << ARG_REGISTER (3));
12537 else
12538 regs_available_for_popping =
12539 (1 << ARG_REGISTER (2))
12540 | (1 << ARG_REGISTER (3));
12542 else if (size <= 4)
12543 regs_available_for_popping =
12544 (1 << ARG_REGISTER (2))
12545 | (1 << ARG_REGISTER (3));
12546 else if (size <= 8)
12547 regs_available_for_popping =
12548 (1 << ARG_REGISTER (3));
12551 /* Match registers to be popped with registers into which we pop them. */
12552 for (available = regs_available_for_popping,
12553 required = regs_to_pop;
12554 required != 0 && available != 0;
12555 available &= ~(available & - available),
12556 required &= ~(required & - required))
12557 -- pops_needed;
12559 /* If we have any popping registers left over, remove them. */
12560 if (available > 0)
12561 regs_available_for_popping &= ~available;
12563 /* Otherwise if we need another popping register we can use
12564 the fourth argument register. */
12565 else if (pops_needed)
12567 /* If we have not found any free argument registers and
12568 reg a4 contains the return address, we must move it. */
12569 if (regs_available_for_popping == 0
12570 && reg_containing_return_addr == LAST_ARG_REGNUM)
12572 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12573 reg_containing_return_addr = LR_REGNUM;
12575 else if (size > 12)
12577 /* Register a4 is being used to hold part of the return value,
12578 but we have dire need of a free, low register. */
12579 restore_a4 = TRUE;
12581 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12584 if (reg_containing_return_addr != LAST_ARG_REGNUM)
12586 /* The fourth argument register is available. */
12587 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12589 --pops_needed;
12593 /* Pop as many registers as we can. */
12594 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12595 regs_available_for_popping);
12597 /* Process the registers we popped. */
12598 if (reg_containing_return_addr == -1)
12600 /* The return address was popped into the lowest numbered register. */
12601 regs_to_pop &= ~(1 << LR_REGNUM);
12603 reg_containing_return_addr =
12604 number_of_first_bit_set (regs_available_for_popping);
12606 /* Remove this register for the mask of available registers, so that
12607 the return address will not be corrupted by further pops. */
12608 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12611 /* If we popped other registers then handle them here. */
12612 if (regs_available_for_popping)
12614 int frame_pointer;
12616 /* Work out which register currently contains the frame pointer. */
12617 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12619 /* Move it into the correct place. */
12620 asm_fprintf (f, "\tmov\t%r, %r\n",
12621 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12623 /* (Temporarily) remove it from the mask of popped registers. */
12624 regs_available_for_popping &= ~(1 << frame_pointer);
12625 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12627 if (regs_available_for_popping)
12629 int stack_pointer;
12631 /* We popped the stack pointer as well,
12632 find the register that contains it. */
12633 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12635 /* Move it into the stack register. */
12636 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12638 /* At this point we have popped all necessary registers, so
12639 do not worry about restoring regs_available_for_popping
12640 to its correct value:
12642 assert (pops_needed == 0)
12643 assert (regs_available_for_popping == (1 << frame_pointer))
12644 assert (regs_to_pop == (1 << STACK_POINTER)) */
12646 else
12648 /* Since we have just move the popped value into the frame
12649 pointer, the popping register is available for reuse, and
12650 we know that we still have the stack pointer left to pop. */
12651 regs_available_for_popping |= (1 << frame_pointer);
12655 /* If we still have registers left on the stack, but we no longer have
12656 any registers into which we can pop them, then we must move the return
12657 address into the link register and make available the register that
12658 contained it. */
12659 if (regs_available_for_popping == 0 && pops_needed > 0)
12661 regs_available_for_popping |= 1 << reg_containing_return_addr;
12663 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12664 reg_containing_return_addr);
12666 reg_containing_return_addr = LR_REGNUM;
12669 /* If we have registers left on the stack then pop some more.
12670 We know that at most we will want to pop FP and SP. */
12671 if (pops_needed > 0)
12673 int popped_into;
12674 int move_to;
12676 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12677 regs_available_for_popping);
12679 /* We have popped either FP or SP.
12680 Move whichever one it is into the correct register. */
12681 popped_into = number_of_first_bit_set (regs_available_for_popping);
12682 move_to = number_of_first_bit_set (regs_to_pop);
12684 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12686 regs_to_pop &= ~(1 << move_to);
12688 --pops_needed;
12691 /* If we still have not popped everything then we must have only
12692 had one register available to us and we are now popping the SP. */
12693 if (pops_needed > 0)
12695 int popped_into;
12697 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12698 regs_available_for_popping);
12700 popped_into = number_of_first_bit_set (regs_available_for_popping);
12702 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12704 assert (regs_to_pop == (1 << STACK_POINTER))
12705 assert (pops_needed == 1)
12709 /* If necessary restore the a4 register. */
12710 if (restore_a4)
12712 if (reg_containing_return_addr != LR_REGNUM)
12714 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12715 reg_containing_return_addr = LR_REGNUM;
12718 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12721 if (current_function_calls_eh_return)
12722 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12724 /* Return to caller. */
12725 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12729 void
12730 thumb_final_prescan_insn (rtx insn)
12732 if (flag_print_asm_name)
12733 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12734 INSN_ADDRESSES (INSN_UID (insn)));
12738 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12740 unsigned HOST_WIDE_INT mask = 0xff;
12741 int i;
12743 if (val == 0) /* XXX */
12744 return 0;
12746 for (i = 0; i < 25; i++)
12747 if ((val & (mask << i)) == val)
12748 return 1;
12750 return 0;
12753 /* Returns nonzero if the current function contains,
12754 or might contain a far jump. */
12755 static int
12756 thumb_far_jump_used_p (void)
12758 rtx insn;
12760 /* This test is only important for leaf functions. */
12761 /* assert (!leaf_function_p ()); */
12763 /* If we have already decided that far jumps may be used,
12764 do not bother checking again, and always return true even if
12765 it turns out that they are not being used. Once we have made
12766 the decision that far jumps are present (and that hence the link
12767 register will be pushed onto the stack) we cannot go back on it. */
12768 if (cfun->machine->far_jump_used)
12769 return 1;
12771 /* If this function is not being called from the prologue/epilogue
12772 generation code then it must be being called from the
12773 INITIAL_ELIMINATION_OFFSET macro. */
12774 if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12776 /* In this case we know that we are being asked about the elimination
12777 of the arg pointer register. If that register is not being used,
12778 then there are no arguments on the stack, and we do not have to
12779 worry that a far jump might force the prologue to push the link
12780 register, changing the stack offsets. In this case we can just
12781 return false, since the presence of far jumps in the function will
12782 not affect stack offsets.
12784 If the arg pointer is live (or if it was live, but has now been
12785 eliminated and so set to dead) then we do have to test to see if
12786 the function might contain a far jump. This test can lead to some
12787 false negatives, since before reload is completed, then length of
12788 branch instructions is not known, so gcc defaults to returning their
12789 longest length, which in turn sets the far jump attribute to true.
12791 A false negative will not result in bad code being generated, but it
12792 will result in a needless push and pop of the link register. We
12793 hope that this does not occur too often.
12795 If we need doubleword stack alignment this could affect the other
12796 elimination offsets so we can't risk getting it wrong. */
12797 if (regs_ever_live [ARG_POINTER_REGNUM])
12798 cfun->machine->arg_pointer_live = 1;
12799 else if (!cfun->machine->arg_pointer_live)
12800 return 0;
12803 /* Check to see if the function contains a branch
12804 insn with the far jump attribute set. */
12805 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12807 if (GET_CODE (insn) == JUMP_INSN
12808 /* Ignore tablejump patterns. */
12809 && GET_CODE (PATTERN (insn)) != ADDR_VEC
12810 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12811 && get_attr_far_jump (insn) == FAR_JUMP_YES
12814 /* Record the fact that we have decided that
12815 the function does use far jumps. */
12816 cfun->machine->far_jump_used = 1;
12817 return 1;
12821 return 0;
12824 /* Return nonzero if FUNC must be entered in ARM mode. */
12826 is_called_in_ARM_mode (tree func)
12828 gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
12830 /* Ignore the problem about functions whose address is taken. */
12831 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12832 return TRUE;
12834 #ifdef ARM_PE
12835 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12836 #else
12837 return FALSE;
12838 #endif
12841 /* The bits which aren't usefully expanded as rtl. */
12842 const char *
12843 thumb_unexpanded_epilogue (void)
12845 int regno;
12846 unsigned long live_regs_mask = 0;
12847 int high_regs_pushed = 0;
12848 int had_to_push_lr;
12849 int size;
12850 int mode;
12852 if (return_used_this_function)
12853 return "";
12855 if (IS_NAKED (arm_current_func_type ()))
12856 return "";
12858 live_regs_mask = thumb_compute_save_reg_mask ();
12859 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
12861 /* If we can deduce the registers used from the function's return value.
12862 This is more reliable that examining regs_ever_live[] because that
12863 will be set if the register is ever used in the function, not just if
12864 the register is used to hold a return value. */
12866 if (current_function_return_rtx != 0)
12867 mode = GET_MODE (current_function_return_rtx);
12868 else
12869 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12871 size = GET_MODE_SIZE (mode);
12873 /* The prolog may have pushed some high registers to use as
12874 work registers. e.g. the testsuite file:
12875 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12876 compiles to produce:
12877 push {r4, r5, r6, r7, lr}
12878 mov r7, r9
12879 mov r6, r8
12880 push {r6, r7}
12881 as part of the prolog. We have to undo that pushing here. */
12883 if (high_regs_pushed)
12885 unsigned long mask = live_regs_mask & 0xff;
12886 int next_hi_reg;
12888 /* The available low registers depend on the size of the value we are
12889 returning. */
12890 if (size <= 12)
12891 mask |= 1 << 3;
12892 if (size <= 8)
12893 mask |= 1 << 2;
12895 if (mask == 0)
12896 /* Oh dear! We have no low registers into which we can pop
12897 high registers! */
12898 internal_error
12899 ("no low registers available for popping high registers");
12901 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12902 if (live_regs_mask & (1 << next_hi_reg))
12903 break;
12905 while (high_regs_pushed)
12907 /* Find lo register(s) into which the high register(s) can
12908 be popped. */
12909 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12911 if (mask & (1 << regno))
12912 high_regs_pushed--;
12913 if (high_regs_pushed == 0)
12914 break;
12917 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
12919 /* Pop the values into the low register(s). */
12920 thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
12922 /* Move the value(s) into the high registers. */
12923 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12925 if (mask & (1 << regno))
12927 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12928 regno);
12930 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
12931 if (live_regs_mask & (1 << next_hi_reg))
12932 break;
12936 live_regs_mask &= ~0x0f00;
12939 had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
12940 live_regs_mask &= 0xff;
12942 if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12944 /* Pop the return address into the PC. */
12945 if (had_to_push_lr)
12946 live_regs_mask |= 1 << PC_REGNUM;
12948 /* Either no argument registers were pushed or a backtrace
12949 structure was created which includes an adjusted stack
12950 pointer, so just pop everything. */
12951 if (live_regs_mask)
12952 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12953 live_regs_mask);
12955 /* We have either just popped the return address into the
12956 PC or it is was kept in LR for the entire function. */
12957 if (!had_to_push_lr)
12958 thumb_exit (asm_out_file, LR_REGNUM);
12960 else
12962 /* Pop everything but the return address. */
12963 if (live_regs_mask)
12964 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12965 live_regs_mask);
12967 if (had_to_push_lr)
12969 if (size > 12)
12971 /* We have no free low regs, so save one. */
12972 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
12973 LAST_ARG_REGNUM);
12976 /* Get the return address into a temporary register. */
12977 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
12978 1 << LAST_ARG_REGNUM);
12980 if (size > 12)
12982 /* Move the return address to lr. */
12983 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
12984 LAST_ARG_REGNUM);
12985 /* Restore the low register. */
12986 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
12987 IP_REGNUM);
12988 regno = LR_REGNUM;
12990 else
12991 regno = LAST_ARG_REGNUM;
12993 else
12994 regno = LR_REGNUM;
12996 /* Remove the argument registers that were pushed onto the stack. */
12997 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
12998 SP_REGNUM, SP_REGNUM,
12999 current_function_pretend_args_size);
13001 thumb_exit (asm_out_file, regno);
13004 return "";
13007 /* Functions to save and restore machine-specific function data. */
13008 static struct machine_function *
13009 arm_init_machine_status (void)
13011 struct machine_function *machine;
13012 machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13014 #if ARM_FT_UNKNOWN != 0
13015 machine->func_type = ARM_FT_UNKNOWN;
13016 #endif
13017 return machine;
13020 /* Return an RTX indicating where the return address to the
13021 calling function can be found. */
13023 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13025 if (count != 0)
13026 return NULL_RTX;
13028 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13031 /* Do anything needed before RTL is emitted for each function. */
13032 void
13033 arm_init_expanders (void)
13035 /* Arrange to initialize and mark the machine per-function status. */
13036 init_machine_status = arm_init_machine_status;
13038 /* This is to stop the combine pass optimizing away the alignment
13039 adjustment of va_arg. */
13040 /* ??? It is claimed that this should not be necessary. */
13041 if (cfun)
13042 mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
13046 /* Like arm_compute_initial_elimination offset. Simpler because
13047 THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer. */
13049 HOST_WIDE_INT
13050 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13052 arm_stack_offsets *offsets;
13054 offsets = arm_get_frame_offsets ();
13056 switch (from)
13058 case ARG_POINTER_REGNUM:
13059 switch (to)
13061 case STACK_POINTER_REGNUM:
13062 return offsets->outgoing_args - offsets->saved_args;
13064 case FRAME_POINTER_REGNUM:
13065 return offsets->soft_frame - offsets->saved_args;
13067 case THUMB_HARD_FRAME_POINTER_REGNUM:
13068 case ARM_HARD_FRAME_POINTER_REGNUM:
13069 return offsets->saved_regs - offsets->saved_args;
13071 default:
13072 gcc_unreachable ();
13074 break;
13076 case FRAME_POINTER_REGNUM:
13077 switch (to)
13079 case STACK_POINTER_REGNUM:
13080 return offsets->outgoing_args - offsets->soft_frame;
13082 case THUMB_HARD_FRAME_POINTER_REGNUM:
13083 case ARM_HARD_FRAME_POINTER_REGNUM:
13084 return offsets->saved_regs - offsets->soft_frame;
13086 default:
13087 gcc_unreachable ();
13089 break;
13091 default:
13092 gcc_unreachable ();
13097 /* Generate the rest of a function's prologue. */
13098 void
13099 thumb_expand_prologue (void)
13101 rtx insn, dwarf;
13103 HOST_WIDE_INT amount;
13104 arm_stack_offsets *offsets;
13105 unsigned long func_type;
13106 int regno;
13107 unsigned long live_regs_mask;
13109 func_type = arm_current_func_type ();
13111 /* Naked functions don't have prologues. */
13112 if (IS_NAKED (func_type))
13113 return;
13115 if (IS_INTERRUPT (func_type))
13117 error ("interrupt Service Routines cannot be coded in Thumb mode");
13118 return;
13121 live_regs_mask = thumb_compute_save_reg_mask ();
13122 /* Load the pic register before setting the frame pointer,
13123 so we can use r7 as a temporary work register. */
13124 if (flag_pic)
13125 arm_load_pic_register (thumb_find_work_register (live_regs_mask));
13127 offsets = arm_get_frame_offsets ();
13129 if (frame_pointer_needed)
13131 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
13132 stack_pointer_rtx));
13133 RTX_FRAME_RELATED_P (insn) = 1;
13135 else if (CALLER_INTERWORKING_SLOT_SIZE > 0)
13136 emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
13137 stack_pointer_rtx);
13139 amount = offsets->outgoing_args - offsets->saved_regs;
13140 if (amount)
13142 if (amount < 512)
13144 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13145 GEN_INT (- amount)));
13146 RTX_FRAME_RELATED_P (insn) = 1;
13148 else
13150 rtx reg;
13152 /* The stack decrement is too big for an immediate value in a single
13153 insn. In theory we could issue multiple subtracts, but after
13154 three of them it becomes more space efficient to place the full
13155 value in the constant pool and load into a register. (Also the
13156 ARM debugger really likes to see only one stack decrement per
13157 function). So instead we look for a scratch register into which
13158 we can load the decrement, and then we subtract this from the
13159 stack pointer. Unfortunately on the thumb the only available
13160 scratch registers are the argument registers, and we cannot use
13161 these as they may hold arguments to the function. Instead we
13162 attempt to locate a call preserved register which is used by this
13163 function. If we can find one, then we know that it will have
13164 been pushed at the start of the prologue and so we can corrupt
13165 it now. */
13166 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13167 if (live_regs_mask & (1 << regno)
13168 && !(frame_pointer_needed
13169 && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13170 break;
13172 if (regno > LAST_LO_REGNUM) /* Very unlikely. */
13174 rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13176 /* Choose an arbitrary, non-argument low register. */
13177 reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13179 /* Save it by copying it into a high, scratch register. */
13180 emit_insn (gen_movsi (spare, reg));
13181 /* Add a USE to stop propagate_one_insn() from barfing. */
13182 emit_insn (gen_prologue_use (spare));
13184 /* Decrement the stack. */
13185 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13186 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13187 stack_pointer_rtx, reg));
13188 RTX_FRAME_RELATED_P (insn) = 1;
13189 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13190 plus_constant (stack_pointer_rtx,
13191 -amount));
13192 RTX_FRAME_RELATED_P (dwarf) = 1;
13193 REG_NOTES (insn)
13194 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13195 REG_NOTES (insn));
13197 /* Restore the low register's original value. */
13198 emit_insn (gen_movsi (reg, spare));
13200 /* Emit a USE of the restored scratch register, so that flow
13201 analysis will not consider the restore redundant. The
13202 register won't be used again in this function and isn't
13203 restored by the epilogue. */
13204 emit_insn (gen_prologue_use (reg));
13206 else
13208 reg = gen_rtx_REG (SImode, regno);
13210 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13212 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13213 stack_pointer_rtx, reg));
13214 RTX_FRAME_RELATED_P (insn) = 1;
13215 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13216 plus_constant (stack_pointer_rtx,
13217 -amount));
13218 RTX_FRAME_RELATED_P (dwarf) = 1;
13219 REG_NOTES (insn)
13220 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13221 REG_NOTES (insn));
13224 /* If the frame pointer is needed, emit a special barrier that
13225 will prevent the scheduler from moving stores to the frame
13226 before the stack adjustment. */
13227 if (frame_pointer_needed)
13228 emit_insn (gen_stack_tie (stack_pointer_rtx,
13229 hard_frame_pointer_rtx));
13232 if (current_function_profile || !TARGET_SCHED_PROLOG)
13233 emit_insn (gen_blockage ());
13235 cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13236 if (live_regs_mask & 0xff)
13237 cfun->machine->lr_save_eliminated = 0;
13239 /* If the link register is being kept alive, with the return address in it,
13240 then make sure that it does not get reused by the ce2 pass. */
13241 if (cfun->machine->lr_save_eliminated)
13242 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13246 void
13247 thumb_expand_epilogue (void)
13249 HOST_WIDE_INT amount;
13250 arm_stack_offsets *offsets;
13251 int regno;
13253 /* Naked functions don't have prologues. */
13254 if (IS_NAKED (arm_current_func_type ()))
13255 return;
13257 offsets = arm_get_frame_offsets ();
13258 amount = offsets->outgoing_args - offsets->saved_regs;
13260 if (frame_pointer_needed)
13261 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13262 else if (amount)
13264 if (amount < 512)
13265 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13266 GEN_INT (amount)));
13267 else
13269 /* r3 is always free in the epilogue. */
13270 rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13272 emit_insn (gen_movsi (reg, GEN_INT (amount)));
13273 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13277 /* Emit a USE (stack_pointer_rtx), so that
13278 the stack adjustment will not be deleted. */
13279 emit_insn (gen_prologue_use (stack_pointer_rtx));
13281 if (current_function_profile || !TARGET_SCHED_PROLOG)
13282 emit_insn (gen_blockage ());
13284 /* Emit a clobber for each insn that will be restored in the epilogue,
13285 so that flow2 will get register lifetimes correct. */
13286 for (regno = 0; regno < 13; regno++)
13287 if (regs_ever_live[regno] && !call_used_regs[regno])
13288 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13290 if (! regs_ever_live[LR_REGNUM])
13291 emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13294 static void
13295 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13297 unsigned long live_regs_mask = 0;
13298 unsigned long l_mask;
13299 unsigned high_regs_pushed = 0;
13300 int cfa_offset = 0;
13301 int regno;
13303 if (IS_NAKED (arm_current_func_type ()))
13304 return;
13306 if (is_called_in_ARM_mode (current_function_decl))
13308 const char * name;
13310 gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
13311 gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
13312 == SYMBOL_REF);
13313 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
13315 /* Generate code sequence to switch us into Thumb mode. */
13316 /* The .code 32 directive has already been emitted by
13317 ASM_DECLARE_FUNCTION_NAME. */
13318 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13319 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13321 /* Generate a label, so that the debugger will notice the
13322 change in instruction sets. This label is also used by
13323 the assembler to bypass the ARM code when this function
13324 is called from a Thumb encoded function elsewhere in the
13325 same file. Hence the definition of STUB_NAME here must
13326 agree with the definition in gas/config/tc-arm.c. */
13328 #define STUB_NAME ".real_start_of"
13330 fprintf (f, "\t.code\t16\n");
13331 #ifdef ARM_PE
13332 if (arm_dllexport_name_p (name))
13333 name = arm_strip_name_encoding (name);
13334 #endif
13335 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13336 fprintf (f, "\t.thumb_func\n");
13337 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13340 if (current_function_pretend_args_size)
13342 if (cfun->machine->uses_anonymous_args)
13344 int num_pushes;
13346 fprintf (f, "\tpush\t{");
13348 num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13350 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13351 regno <= LAST_ARG_REGNUM;
13352 regno++)
13353 asm_fprintf (f, "%r%s", regno,
13354 regno == LAST_ARG_REGNUM ? "" : ", ");
13356 fprintf (f, "}\n");
13358 else
13359 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13360 SP_REGNUM, SP_REGNUM,
13361 current_function_pretend_args_size);
13363 /* We don't need to record the stores for unwinding (would it
13364 help the debugger any if we did?), but record the change in
13365 the stack pointer. */
13366 if (dwarf2out_do_frame ())
13368 char *l = dwarf2out_cfi_label ();
13370 cfa_offset = cfa_offset + current_function_pretend_args_size;
13371 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13375 /* Get the registers we are going to push. */
13376 live_regs_mask = thumb_compute_save_reg_mask ();
13377 /* Extract a mask of the ones we can give to the Thumb's push instruction. */
13378 l_mask = live_regs_mask & 0x40ff;
13379 /* Then count how many other high registers will need to be pushed. */
13380 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13382 if (TARGET_BACKTRACE)
13384 unsigned offset;
13385 unsigned work_register;
13387 /* We have been asked to create a stack backtrace structure.
13388 The code looks like this:
13390 0 .align 2
13391 0 func:
13392 0 sub SP, #16 Reserve space for 4 registers.
13393 2 push {R7} Push low registers.
13394 4 add R7, SP, #20 Get the stack pointer before the push.
13395 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
13396 8 mov R7, PC Get hold of the start of this code plus 12.
13397 10 str R7, [SP, #16] Store it.
13398 12 mov R7, FP Get hold of the current frame pointer.
13399 14 str R7, [SP, #4] Store it.
13400 16 mov R7, LR Get hold of the current return address.
13401 18 str R7, [SP, #12] Store it.
13402 20 add R7, SP, #16 Point at the start of the backtrace structure.
13403 22 mov FP, R7 Put this value into the frame pointer. */
13405 work_register = thumb_find_work_register (live_regs_mask);
13407 asm_fprintf
13408 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13409 SP_REGNUM, SP_REGNUM);
13411 if (dwarf2out_do_frame ())
13413 char *l = dwarf2out_cfi_label ();
13415 cfa_offset = cfa_offset + 16;
13416 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13419 if (l_mask)
13421 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13422 offset = bit_count (l_mask);
13424 else
13425 offset = 0;
13427 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13428 offset + 16 + current_function_pretend_args_size);
13430 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13431 offset + 4);
13433 /* Make sure that the instruction fetching the PC is in the right place
13434 to calculate "start of backtrace creation code + 12". */
13435 if (l_mask)
13437 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13438 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13439 offset + 12);
13440 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13441 ARM_HARD_FRAME_POINTER_REGNUM);
13442 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13443 offset);
13445 else
13447 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13448 ARM_HARD_FRAME_POINTER_REGNUM);
13449 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13450 offset);
13451 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13452 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13453 offset + 12);
13456 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13457 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13458 offset + 8);
13459 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13460 offset + 12);
13461 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13462 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13464 /* Optimisation: If we are not pushing any low registers but we are going
13465 to push some high registers then delay our first push. This will just
13466 be a push of LR and we can combine it with the push of the first high
13467 register. */
13468 else if ((l_mask & 0xff) != 0
13469 || (high_regs_pushed == 0 && l_mask))
13470 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13472 if (high_regs_pushed)
13474 unsigned pushable_regs;
13475 unsigned next_hi_reg;
13477 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13478 if (live_regs_mask & (1 << next_hi_reg))
13479 break;
13481 pushable_regs = l_mask & 0xff;
13483 if (pushable_regs == 0)
13484 pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
13486 while (high_regs_pushed > 0)
13488 unsigned long real_regs_mask = 0;
13490 for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
13492 if (pushable_regs & (1 << regno))
13494 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13496 high_regs_pushed --;
13497 real_regs_mask |= (1 << next_hi_reg);
13499 if (high_regs_pushed)
13501 for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
13502 next_hi_reg --)
13503 if (live_regs_mask & (1 << next_hi_reg))
13504 break;
13506 else
13508 pushable_regs &= ~((1 << regno) - 1);
13509 break;
13514 /* If we had to find a work register and we have not yet
13515 saved the LR then add it to the list of regs to push. */
13516 if (l_mask == (1 << LR_REGNUM))
13518 thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
13519 1, &cfa_offset,
13520 real_regs_mask | (1 << LR_REGNUM));
13521 l_mask = 0;
13523 else
13524 thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
13529 /* Handle the case of a double word load into a low register from
13530 a computed memory address. The computed address may involve a
13531 register which is overwritten by the load. */
13532 const char *
13533 thumb_load_double_from_address (rtx *operands)
13535 rtx addr;
13536 rtx base;
13537 rtx offset;
13538 rtx arg1;
13539 rtx arg2;
13541 gcc_assert (GET_CODE (operands[0]) == REG);
13542 gcc_assert (GET_CODE (operands[1]) == MEM);
13544 /* Get the memory address. */
13545 addr = XEXP (operands[1], 0);
13547 /* Work out how the memory address is computed. */
13548 switch (GET_CODE (addr))
13550 case REG:
13551 operands[2] = gen_rtx_MEM (SImode,
13552 plus_constant (XEXP (operands[1], 0), 4));
13554 if (REGNO (operands[0]) == REGNO (addr))
13556 output_asm_insn ("ldr\t%H0, %2", operands);
13557 output_asm_insn ("ldr\t%0, %1", operands);
13559 else
13561 output_asm_insn ("ldr\t%0, %1", operands);
13562 output_asm_insn ("ldr\t%H0, %2", operands);
13564 break;
13566 case CONST:
13567 /* Compute <address> + 4 for the high order load. */
13568 operands[2] = gen_rtx_MEM (SImode,
13569 plus_constant (XEXP (operands[1], 0), 4));
13571 output_asm_insn ("ldr\t%0, %1", operands);
13572 output_asm_insn ("ldr\t%H0, %2", operands);
13573 break;
13575 case PLUS:
13576 arg1 = XEXP (addr, 0);
13577 arg2 = XEXP (addr, 1);
13579 if (CONSTANT_P (arg1))
13580 base = arg2, offset = arg1;
13581 else
13582 base = arg1, offset = arg2;
13584 gcc_assert (GET_CODE (base) == REG);
13586 /* Catch the case of <address> = <reg> + <reg> */
13587 if (GET_CODE (offset) == REG)
13589 int reg_offset = REGNO (offset);
13590 int reg_base = REGNO (base);
13591 int reg_dest = REGNO (operands[0]);
13593 /* Add the base and offset registers together into the
13594 higher destination register. */
13595 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13596 reg_dest + 1, reg_base, reg_offset);
13598 /* Load the lower destination register from the address in
13599 the higher destination register. */
13600 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13601 reg_dest, reg_dest + 1);
13603 /* Load the higher destination register from its own address
13604 plus 4. */
13605 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13606 reg_dest + 1, reg_dest + 1);
13608 else
13610 /* Compute <address> + 4 for the high order load. */
13611 operands[2] = gen_rtx_MEM (SImode,
13612 plus_constant (XEXP (operands[1], 0), 4));
13614 /* If the computed address is held in the low order register
13615 then load the high order register first, otherwise always
13616 load the low order register first. */
13617 if (REGNO (operands[0]) == REGNO (base))
13619 output_asm_insn ("ldr\t%H0, %2", operands);
13620 output_asm_insn ("ldr\t%0, %1", operands);
13622 else
13624 output_asm_insn ("ldr\t%0, %1", operands);
13625 output_asm_insn ("ldr\t%H0, %2", operands);
13628 break;
13630 case LABEL_REF:
13631 /* With no registers to worry about we can just load the value
13632 directly. */
13633 operands[2] = gen_rtx_MEM (SImode,
13634 plus_constant (XEXP (operands[1], 0), 4));
13636 output_asm_insn ("ldr\t%H0, %2", operands);
13637 output_asm_insn ("ldr\t%0, %1", operands);
13638 break;
13640 default:
13641 gcc_unreachable ();
13644 return "";
13647 const char *
13648 thumb_output_move_mem_multiple (int n, rtx *operands)
13650 rtx tmp;
13652 switch (n)
13654 case 2:
13655 if (REGNO (operands[4]) > REGNO (operands[5]))
13657 tmp = operands[4];
13658 operands[4] = operands[5];
13659 operands[5] = tmp;
13661 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13662 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13663 break;
13665 case 3:
13666 if (REGNO (operands[4]) > REGNO (operands[5]))
13668 tmp = operands[4];
13669 operands[4] = operands[5];
13670 operands[5] = tmp;
13672 if (REGNO (operands[5]) > REGNO (operands[6]))
13674 tmp = operands[5];
13675 operands[5] = operands[6];
13676 operands[6] = tmp;
13678 if (REGNO (operands[4]) > REGNO (operands[5]))
13680 tmp = operands[4];
13681 operands[4] = operands[5];
13682 operands[5] = tmp;
13685 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13686 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13687 break;
13689 default:
13690 gcc_unreachable ();
13693 return "";
13696 /* Output a call-via instruction for thumb state. */
13697 const char *
13698 thumb_call_via_reg (rtx reg)
13700 int regno = REGNO (reg);
13701 rtx *labelp;
13703 gcc_assert (regno < LR_REGNUM);
13705 /* If we are in the normal text section we can use a single instance
13706 per compilation unit. If we are doing function sections, then we need
13707 an entry per section, since we can't rely on reachability. */
13708 if (in_text_section ())
13710 thumb_call_reg_needed = 1;
13712 if (thumb_call_via_label[regno] == NULL)
13713 thumb_call_via_label[regno] = gen_label_rtx ();
13714 labelp = thumb_call_via_label + regno;
13716 else
13718 if (cfun->machine->call_via[regno] == NULL)
13719 cfun->machine->call_via[regno] = gen_label_rtx ();
13720 labelp = cfun->machine->call_via + regno;
13723 output_asm_insn ("bl\t%a0", labelp);
13724 return "";
13727 /* Routines for generating rtl. */
13728 void
13729 thumb_expand_movmemqi (rtx *operands)
13731 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13732 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13733 HOST_WIDE_INT len = INTVAL (operands[2]);
13734 HOST_WIDE_INT offset = 0;
13736 while (len >= 12)
13738 emit_insn (gen_movmem12b (out, in, out, in));
13739 len -= 12;
13742 if (len >= 8)
13744 emit_insn (gen_movmem8b (out, in, out, in));
13745 len -= 8;
13748 if (len >= 4)
13750 rtx reg = gen_reg_rtx (SImode);
13751 emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13752 emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13753 len -= 4;
13754 offset += 4;
13757 if (len >= 2)
13759 rtx reg = gen_reg_rtx (HImode);
13760 emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13761 plus_constant (in, offset))));
13762 emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13763 reg));
13764 len -= 2;
13765 offset += 2;
13768 if (len)
13770 rtx reg = gen_reg_rtx (QImode);
13771 emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13772 plus_constant (in, offset))));
13773 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13774 reg));
13778 void
13779 thumb_reload_out_hi (rtx *operands)
13781 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13784 /* Handle reading a half-word from memory during reload. */
13785 void
13786 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13788 gcc_unreachable ();
13791 /* Return the length of a function name prefix
13792 that starts with the character 'c'. */
13793 static int
13794 arm_get_strip_length (int c)
13796 switch (c)
13798 ARM_NAME_ENCODING_LENGTHS
13799 default: return 0;
13803 /* Return a pointer to a function's name with any
13804 and all prefix encodings stripped from it. */
13805 const char *
13806 arm_strip_name_encoding (const char *name)
13808 int skip;
13810 while ((skip = arm_get_strip_length (* name)))
13811 name += skip;
13813 return name;
13816 /* If there is a '*' anywhere in the name's prefix, then
13817 emit the stripped name verbatim, otherwise prepend an
13818 underscore if leading underscores are being used. */
13819 void
13820 arm_asm_output_labelref (FILE *stream, const char *name)
13822 int skip;
13823 int verbatim = 0;
13825 while ((skip = arm_get_strip_length (* name)))
13827 verbatim |= (*name == '*');
13828 name += skip;
13831 if (verbatim)
13832 fputs (name, stream);
13833 else
13834 asm_fprintf (stream, "%U%s", name);
13837 static void
13838 arm_file_end (void)
13840 int regno;
13842 if (! thumb_call_reg_needed)
13843 return;
13845 text_section ();
13846 asm_fprintf (asm_out_file, "\t.code 16\n");
13847 ASM_OUTPUT_ALIGN (asm_out_file, 1);
13849 for (regno = 0; regno < LR_REGNUM; regno++)
13851 rtx label = thumb_call_via_label[regno];
13853 if (label != 0)
13855 targetm.asm_out.internal_label (asm_out_file, "L",
13856 CODE_LABEL_NUMBER (label));
13857 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
13862 rtx aof_pic_label;
13864 #ifdef AOF_ASSEMBLER
13865 /* Special functions only needed when producing AOF syntax assembler. */
13867 struct pic_chain
13869 struct pic_chain * next;
13870 const char * symname;
13873 static struct pic_chain * aof_pic_chain = NULL;
13876 aof_pic_entry (rtx x)
13878 struct pic_chain ** chainp;
13879 int offset;
13881 if (aof_pic_label == NULL_RTX)
13883 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13886 for (offset = 0, chainp = &aof_pic_chain; *chainp;
13887 offset += 4, chainp = &(*chainp)->next)
13888 if ((*chainp)->symname == XSTR (x, 0))
13889 return plus_constant (aof_pic_label, offset);
13891 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13892 (*chainp)->next = NULL;
13893 (*chainp)->symname = XSTR (x, 0);
13894 return plus_constant (aof_pic_label, offset);
13897 void
13898 aof_dump_pic_table (FILE *f)
13900 struct pic_chain * chain;
13902 if (aof_pic_chain == NULL)
13903 return;
13905 asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13906 PIC_OFFSET_TABLE_REGNUM,
13907 PIC_OFFSET_TABLE_REGNUM);
13908 fputs ("|x$adcons|\n", f);
13910 for (chain = aof_pic_chain; chain; chain = chain->next)
13912 fputs ("\tDCD\t", f);
13913 assemble_name (f, chain->symname);
13914 fputs ("\n", f);
13918 int arm_text_section_count = 1;
13920 char *
13921 aof_text_section (void )
13923 static char buf[100];
13924 sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13925 arm_text_section_count++);
13926 if (flag_pic)
13927 strcat (buf, ", PIC, REENTRANT");
13928 return buf;
13931 static int arm_data_section_count = 1;
13933 char *
13934 aof_data_section (void)
13936 static char buf[100];
13937 sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13938 return buf;
13941 /* The AOF assembler is religiously strict about declarations of
13942 imported and exported symbols, so that it is impossible to declare
13943 a function as imported near the beginning of the file, and then to
13944 export it later on. It is, however, possible to delay the decision
13945 until all the functions in the file have been compiled. To get
13946 around this, we maintain a list of the imports and exports, and
13947 delete from it any that are subsequently defined. At the end of
13948 compilation we spit the remainder of the list out before the END
13949 directive. */
13951 struct import
13953 struct import * next;
13954 const char * name;
13957 static struct import * imports_list = NULL;
13959 void
13960 aof_add_import (const char *name)
13962 struct import * new;
13964 for (new = imports_list; new; new = new->next)
13965 if (new->name == name)
13966 return;
13968 new = (struct import *) xmalloc (sizeof (struct import));
13969 new->next = imports_list;
13970 imports_list = new;
13971 new->name = name;
13974 void
13975 aof_delete_import (const char *name)
13977 struct import ** old;
13979 for (old = &imports_list; *old; old = & (*old)->next)
13981 if ((*old)->name == name)
13983 *old = (*old)->next;
13984 return;
13989 int arm_main_function = 0;
13991 static void
13992 aof_dump_imports (FILE *f)
13994 /* The AOF assembler needs this to cause the startup code to be extracted
13995 from the library. Brining in __main causes the whole thing to work
13996 automagically. */
13997 if (arm_main_function)
13999 text_section ();
14000 fputs ("\tIMPORT __main\n", f);
14001 fputs ("\tDCD __main\n", f);
14004 /* Now dump the remaining imports. */
14005 while (imports_list)
14007 fprintf (f, "\tIMPORT\t");
14008 assemble_name (f, imports_list->name);
14009 fputc ('\n', f);
14010 imports_list = imports_list->next;
14014 static void
14015 aof_globalize_label (FILE *stream, const char *name)
14017 default_globalize_label (stream, name);
14018 if (! strcmp (name, "main"))
14019 arm_main_function = 1;
14022 static void
14023 aof_file_start (void)
14025 fputs ("__r0\tRN\t0\n", asm_out_file);
14026 fputs ("__a1\tRN\t0\n", asm_out_file);
14027 fputs ("__a2\tRN\t1\n", asm_out_file);
14028 fputs ("__a3\tRN\t2\n", asm_out_file);
14029 fputs ("__a4\tRN\t3\n", asm_out_file);
14030 fputs ("__v1\tRN\t4\n", asm_out_file);
14031 fputs ("__v2\tRN\t5\n", asm_out_file);
14032 fputs ("__v3\tRN\t6\n", asm_out_file);
14033 fputs ("__v4\tRN\t7\n", asm_out_file);
14034 fputs ("__v5\tRN\t8\n", asm_out_file);
14035 fputs ("__v6\tRN\t9\n", asm_out_file);
14036 fputs ("__sl\tRN\t10\n", asm_out_file);
14037 fputs ("__fp\tRN\t11\n", asm_out_file);
14038 fputs ("__ip\tRN\t12\n", asm_out_file);
14039 fputs ("__sp\tRN\t13\n", asm_out_file);
14040 fputs ("__lr\tRN\t14\n", asm_out_file);
14041 fputs ("__pc\tRN\t15\n", asm_out_file);
14042 fputs ("__f0\tFN\t0\n", asm_out_file);
14043 fputs ("__f1\tFN\t1\n", asm_out_file);
14044 fputs ("__f2\tFN\t2\n", asm_out_file);
14045 fputs ("__f3\tFN\t3\n", asm_out_file);
14046 fputs ("__f4\tFN\t4\n", asm_out_file);
14047 fputs ("__f5\tFN\t5\n", asm_out_file);
14048 fputs ("__f6\tFN\t6\n", asm_out_file);
14049 fputs ("__f7\tFN\t7\n", asm_out_file);
14050 text_section ();
14053 static void
14054 aof_file_end (void)
14056 if (flag_pic)
14057 aof_dump_pic_table (asm_out_file);
14058 arm_file_end ();
14059 aof_dump_imports (asm_out_file);
14060 fputs ("\tEND\n", asm_out_file);
14062 #endif /* AOF_ASSEMBLER */
14064 #ifndef ARM_PE
14065 /* Symbols in the text segment can be accessed without indirecting via the
14066 constant pool; it may take an extra binary operation, but this is still
14067 faster than indirecting via memory. Don't do this when not optimizing,
14068 since we won't be calculating al of the offsets necessary to do this
14069 simplification. */
14071 static void
14072 arm_encode_section_info (tree decl, rtx rtl, int first)
14074 /* This doesn't work with AOF syntax, since the string table may be in
14075 a different AREA. */
14076 #ifndef AOF_ASSEMBLER
14077 if (optimize > 0 && TREE_CONSTANT (decl))
14078 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14079 #endif
14081 /* If we are referencing a function that is weak then encode a long call
14082 flag in the function name, otherwise if the function is static or
14083 or known to be defined in this file then encode a short call flag. */
14084 if (first && DECL_P (decl))
14086 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14087 arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14088 else if (! TREE_PUBLIC (decl))
14089 arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14092 #endif /* !ARM_PE */
14094 static void
14095 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14097 if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14098 && !strcmp (prefix, "L"))
14100 arm_ccfsm_state = 0;
14101 arm_target_insn = NULL;
14103 default_internal_label (stream, prefix, labelno);
14106 /* Output code to add DELTA to the first argument, and then jump
14107 to FUNCTION. Used for C++ multiple inheritance. */
14108 static void
14109 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14110 HOST_WIDE_INT delta,
14111 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14112 tree function)
14114 static int thunk_label = 0;
14115 char label[256];
14116 int mi_delta = delta;
14117 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14118 int shift = 0;
14119 int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14120 ? 1 : 0);
14121 if (mi_delta < 0)
14122 mi_delta = - mi_delta;
14123 if (TARGET_THUMB)
14125 int labelno = thunk_label++;
14126 ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14127 fputs ("\tldr\tr12, ", file);
14128 assemble_name (file, label);
14129 fputc ('\n', file);
14131 while (mi_delta != 0)
14133 if ((mi_delta & (3 << shift)) == 0)
14134 shift += 2;
14135 else
14137 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14138 mi_op, this_regno, this_regno,
14139 mi_delta & (0xff << shift));
14140 mi_delta &= ~(0xff << shift);
14141 shift += 8;
14144 if (TARGET_THUMB)
14146 fprintf (file, "\tbx\tr12\n");
14147 ASM_OUTPUT_ALIGN (file, 2);
14148 assemble_name (file, label);
14149 fputs (":\n", file);
14150 assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14152 else
14154 fputs ("\tb\t", file);
14155 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14156 if (NEED_PLT_RELOC)
14157 fputs ("(PLT)", file);
14158 fputc ('\n', file);
14163 arm_emit_vector_const (FILE *file, rtx x)
14165 int i;
14166 const char * pattern;
14168 gcc_assert (GET_CODE (x) == CONST_VECTOR);
14170 switch (GET_MODE (x))
14172 case V2SImode: pattern = "%08x"; break;
14173 case V4HImode: pattern = "%04x"; break;
14174 case V8QImode: pattern = "%02x"; break;
14175 default: gcc_unreachable ();
14178 fprintf (file, "0x");
14179 for (i = CONST_VECTOR_NUNITS (x); i--;)
14181 rtx element;
14183 element = CONST_VECTOR_ELT (x, i);
14184 fprintf (file, pattern, INTVAL (element));
14187 return 1;
14190 const char *
14191 arm_output_load_gr (rtx *operands)
14193 rtx reg;
14194 rtx offset;
14195 rtx wcgr;
14196 rtx sum;
14198 if (GET_CODE (operands [1]) != MEM
14199 || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14200 || GET_CODE (reg = XEXP (sum, 0)) != REG
14201 || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14202 || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14203 return "wldrw%?\t%0, %1";
14205 /* Fix up an out-of-range load of a GR register. */
14206 output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14207 wcgr = operands[0];
14208 operands[0] = reg;
14209 output_asm_insn ("ldr%?\t%0, %1", operands);
14211 operands[0] = wcgr;
14212 operands[1] = reg;
14213 output_asm_insn ("tmcr%?\t%0, %1", operands);
14214 output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14216 return "";
14219 static rtx
14220 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14221 int incoming ATTRIBUTE_UNUSED)
14223 #if 0
14224 /* FIXME: The ARM backend has special code to handle structure
14225 returns, and will reserve its own hidden first argument. So
14226 if this macro is enabled a *second* hidden argument will be
14227 reserved, which will break binary compatibility with old
14228 toolchains and also thunk handling. One day this should be
14229 fixed. */
14230 return 0;
14231 #else
14232 /* Register in which address to store a structure value
14233 is passed to a function. */
14234 return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14235 #endif
14238 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14240 On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14241 named arg and all anonymous args onto the stack.
14242 XXX I know the prologue shouldn't be pushing registers, but it is faster
14243 that way. */
14245 static void
14246 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14247 enum machine_mode mode ATTRIBUTE_UNUSED,
14248 tree type ATTRIBUTE_UNUSED,
14249 int *pretend_size,
14250 int second_time ATTRIBUTE_UNUSED)
14252 cfun->machine->uses_anonymous_args = 1;
14253 if (cum->nregs < NUM_ARG_REGS)
14254 *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14257 /* Return nonzero if the CONSUMER instruction (a store) does not need
14258 PRODUCER's value to calculate the address. */
14261 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14263 rtx value = PATTERN (producer);
14264 rtx addr = PATTERN (consumer);
14266 if (GET_CODE (value) == COND_EXEC)
14267 value = COND_EXEC_CODE (value);
14268 if (GET_CODE (value) == PARALLEL)
14269 value = XVECEXP (value, 0, 0);
14270 value = XEXP (value, 0);
14271 if (GET_CODE (addr) == COND_EXEC)
14272 addr = COND_EXEC_CODE (addr);
14273 if (GET_CODE (addr) == PARALLEL)
14274 addr = XVECEXP (addr, 0, 0);
14275 addr = XEXP (addr, 0);
14277 return !reg_overlap_mentioned_p (value, addr);
14280 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14281 have an early register shift value or amount dependency on the
14282 result of PRODUCER. */
14285 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14287 rtx value = PATTERN (producer);
14288 rtx op = PATTERN (consumer);
14289 rtx early_op;
14291 if (GET_CODE (value) == COND_EXEC)
14292 value = COND_EXEC_CODE (value);
14293 if (GET_CODE (value) == PARALLEL)
14294 value = XVECEXP (value, 0, 0);
14295 value = XEXP (value, 0);
14296 if (GET_CODE (op) == COND_EXEC)
14297 op = COND_EXEC_CODE (op);
14298 if (GET_CODE (op) == PARALLEL)
14299 op = XVECEXP (op, 0, 0);
14300 op = XEXP (op, 1);
14302 early_op = XEXP (op, 0);
14303 /* This is either an actual independent shift, or a shift applied to
14304 the first operand of another operation. We want the whole shift
14305 operation. */
14306 if (GET_CODE (early_op) == REG)
14307 early_op = op;
14309 return !reg_overlap_mentioned_p (value, early_op);
14312 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14313 have an early register shift value dependency on the result of
14314 PRODUCER. */
14317 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14319 rtx value = PATTERN (producer);
14320 rtx op = PATTERN (consumer);
14321 rtx early_op;
14323 if (GET_CODE (value) == COND_EXEC)
14324 value = COND_EXEC_CODE (value);
14325 if (GET_CODE (value) == PARALLEL)
14326 value = XVECEXP (value, 0, 0);
14327 value = XEXP (value, 0);
14328 if (GET_CODE (op) == COND_EXEC)
14329 op = COND_EXEC_CODE (op);
14330 if (GET_CODE (op) == PARALLEL)
14331 op = XVECEXP (op, 0, 0);
14332 op = XEXP (op, 1);
14334 early_op = XEXP (op, 0);
14336 /* This is either an actual independent shift, or a shift applied to
14337 the first operand of another operation. We want the value being
14338 shifted, in either case. */
14339 if (GET_CODE (early_op) != REG)
14340 early_op = XEXP (early_op, 0);
14342 return !reg_overlap_mentioned_p (value, early_op);
14345 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14346 have an early register mult dependency on the result of
14347 PRODUCER. */
14350 arm_no_early_mul_dep (rtx producer, rtx consumer)
14352 rtx value = PATTERN (producer);
14353 rtx op = PATTERN (consumer);
14355 if (GET_CODE (value) == COND_EXEC)
14356 value = COND_EXEC_CODE (value);
14357 if (GET_CODE (value) == PARALLEL)
14358 value = XVECEXP (value, 0, 0);
14359 value = XEXP (value, 0);
14360 if (GET_CODE (op) == COND_EXEC)
14361 op = COND_EXEC_CODE (op);
14362 if (GET_CODE (op) == PARALLEL)
14363 op = XVECEXP (op, 0, 0);
14364 op = XEXP (op, 1);
14366 return (GET_CODE (op) == PLUS
14367 && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14371 /* We can't rely on the caller doing the proper promotion when
14372 using APCS or ATPCS. */
14374 static bool
14375 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14377 return !TARGET_AAPCS_BASED;
14381 /* AAPCS based ABIs use short enums by default. */
14383 static bool
14384 arm_default_short_enums (void)
14386 return TARGET_AAPCS_BASED;
14390 /* AAPCS requires that anonymous bitfields affect structure alignment. */
14392 static bool
14393 arm_align_anon_bitfield (void)
14395 return TARGET_AAPCS_BASED;
14399 /* The generic C++ ABI says 64-bit (long long). The EABI says 32-bit. */
14401 static tree
14402 arm_cxx_guard_type (void)
14404 return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14408 /* The EABI says test the least significan bit of a guard variable. */
14410 static bool
14411 arm_cxx_guard_mask_bit (void)
14413 return TARGET_AAPCS_BASED;
14417 /* The EABI specifies that all array cookies are 8 bytes long. */
14419 static tree
14420 arm_get_cookie_size (tree type)
14422 tree size;
14424 if (!TARGET_AAPCS_BASED)
14425 return default_cxx_get_cookie_size (type);
14427 size = build_int_cst (sizetype, 8);
14428 return size;
14432 /* The EABI says that array cookies should also contain the element size. */
14434 static bool
14435 arm_cookie_has_size (void)
14437 return TARGET_AAPCS_BASED;
14441 /* The EABI says constructors and destructors should return a pointer to
14442 the object constructed/destroyed. */
14444 static bool
14445 arm_cxx_cdtor_returns_this (void)
14447 return TARGET_AAPCS_BASED;
14450 /* The EABI says that an inline function may never be the key
14451 method. */
14453 static bool
14454 arm_cxx_key_method_may_be_inline (void)
14456 return !TARGET_AAPCS_BASED;
14459 static void
14460 arm_cxx_determine_class_data_visibility (tree decl)
14462 if (!TARGET_AAPCS_BASED)
14463 return;
14465 /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
14466 is exported. However, on systems without dynamic vague linkage,
14467 \S 3.2.5.6 says that COMDAT class data has hidden linkage. */
14468 if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
14469 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
14470 else
14471 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
14472 DECL_VISIBILITY_SPECIFIED (decl) = 1;
14475 static bool
14476 arm_cxx_class_data_always_comdat (void)
14478 /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
14479 vague linkage if the class has no key function. */
14480 return !TARGET_AAPCS_BASED;
14484 /* The EABI says __aeabi_atexit should be used to register static
14485 destructors. */
14487 static bool
14488 arm_cxx_use_aeabi_atexit (void)
14490 return TARGET_AAPCS_BASED;
14494 void
14495 arm_set_return_address (rtx source, rtx scratch)
14497 arm_stack_offsets *offsets;
14498 HOST_WIDE_INT delta;
14499 rtx addr;
14500 unsigned long saved_regs;
14502 saved_regs = arm_compute_save_reg_mask ();
14504 if ((saved_regs & (1 << LR_REGNUM)) == 0)
14505 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14506 else
14508 if (frame_pointer_needed)
14509 addr = plus_constant(hard_frame_pointer_rtx, -4);
14510 else
14512 /* LR will be the first saved register. */
14513 offsets = arm_get_frame_offsets ();
14514 delta = offsets->outgoing_args - (offsets->frame + 4);
14517 if (delta >= 4096)
14519 emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
14520 GEN_INT (delta & ~4095)));
14521 addr = scratch;
14522 delta &= 4095;
14524 else
14525 addr = stack_pointer_rtx;
14527 addr = plus_constant (addr, delta);
14529 emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14534 void
14535 thumb_set_return_address (rtx source, rtx scratch)
14537 arm_stack_offsets *offsets;
14538 HOST_WIDE_INT delta;
14539 int reg;
14540 rtx addr;
14541 unsigned long mask;
14543 emit_insn (gen_rtx_USE (VOIDmode, source));
14545 mask = thumb_compute_save_reg_mask ();
14546 if (mask & (1 << LR_REGNUM))
14548 offsets = arm_get_frame_offsets ();
14550 /* Find the saved regs. */
14551 if (frame_pointer_needed)
14553 delta = offsets->soft_frame - offsets->saved_args;
14554 reg = THUMB_HARD_FRAME_POINTER_REGNUM;
14556 else
14558 delta = offsets->outgoing_args - offsets->saved_args;
14559 reg = SP_REGNUM;
14561 /* Allow for the stack frame. */
14562 if (TARGET_BACKTRACE)
14563 delta -= 16;
14564 /* The link register is always the first saved register. */
14565 delta -= 4;
14567 /* Construct the address. */
14568 addr = gen_rtx_REG (SImode, reg);
14569 if ((reg != SP_REGNUM && delta >= 128)
14570 || delta >= 1024)
14572 emit_insn (gen_movsi (scratch, GEN_INT (delta)));
14573 emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
14574 addr = scratch;
14576 else
14577 addr = plus_constant (addr, delta);
14579 emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14581 else
14582 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14585 /* Implements target hook vector_mode_supported_p. */
14586 bool
14587 arm_vector_mode_supported_p (enum machine_mode mode)
14589 if ((mode == V2SImode)
14590 || (mode == V4HImode)
14591 || (mode == V8QImode))
14592 return true;
14594 return false;
14597 /* Implement TARGET_SHIFT_TRUNCATION_MASK. SImode shifts use normal
14598 ARM insns and therefore guarantee that the shift count is modulo 256.
14599 DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
14600 guarantee no particular behavior for out-of-range counts. */
14602 static unsigned HOST_WIDE_INT
14603 arm_shift_truncation_mask (enum machine_mode mode)
14605 return mode == SImode ? 255 : 0;
14609 /* Map internal gcc register numbers to DWARF2 register numbers. */
14611 unsigned int
14612 arm_dbx_register_number (unsigned int regno)
14614 if (regno < 16)
14615 return regno;
14617 /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
14618 compatibility. The EABI defines them as registers 96-103. */
14619 if (IS_FPA_REGNUM (regno))
14620 return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
14622 if (IS_VFP_REGNUM (regno))
14623 return 64 + regno - FIRST_VFP_REGNUM;
14625 if (IS_IWMMXT_GR_REGNUM (regno))
14626 return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
14628 if (IS_IWMMXT_REGNUM (regno))
14629 return 112 + regno - FIRST_IWMMXT_REGNUM;
14631 gcc_unreachable ();