* target-def.h (TARGET_CXX_USE_AEABI_ATEXIT): Define.
[official-gcc.git] / gcc / config / arm / arm.c
blob8189909f2ebabf2fa7b27274c219aac1ad7bfc77
1 /* Output routines for GCC for ARM.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5 and Martin Simmons (@harleqn.co.uk).
6 More major hacks by Richard Earnshaw (rearnsha@arm.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published
12 by the Free Software Foundation; either version 2, or (at your
13 option) any later version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to
22 the Free Software Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "obstack.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "output.h"
38 #include "insn-attr.h"
39 #include "flags.h"
40 #include "reload.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "optabs.h"
44 #include "toplev.h"
45 #include "recog.h"
46 #include "ggc.h"
47 #include "except.h"
48 #include "c-pragma.h"
49 #include "integrate.h"
50 #include "tm_p.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "debug.h"
54 #include "langhooks.h"
56 /* Forward definitions of types. */
57 typedef struct minipool_node Mnode;
58 typedef struct minipool_fixup Mfix;
60 const struct attribute_spec arm_attribute_table[];
62 /* Forward function declarations. */
63 static arm_stack_offsets *arm_get_frame_offsets (void);
64 static void arm_add_gc_roots (void);
65 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
66 HOST_WIDE_INT, rtx, rtx, int, int);
67 static unsigned bit_count (unsigned long);
68 static int arm_address_register_rtx_p (rtx, int);
69 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
70 static int thumb_base_register_rtx_p (rtx, enum machine_mode, int);
71 inline static int thumb_index_register_rtx_p (rtx, int);
72 static int thumb_far_jump_used_p (void);
73 static bool thumb_force_lr_save (void);
74 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
75 static rtx emit_sfm (int, int);
76 #ifndef AOF_ASSEMBLER
77 static bool arm_assemble_integer (rtx, unsigned int, int);
78 #endif
79 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
80 static arm_cc get_arm_condition_code (rtx);
81 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
82 static rtx is_jump_table (rtx);
83 static const char *output_multi_immediate (rtx *, const char *, const char *,
84 int, HOST_WIDE_INT);
85 static const char *shift_op (rtx, HOST_WIDE_INT *);
86 static struct machine_function *arm_init_machine_status (void);
87 static void thumb_exit (FILE *, int);
88 static rtx is_jump_table (rtx);
89 static HOST_WIDE_INT get_jump_table_size (rtx);
90 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
91 static Mnode *add_minipool_forward_ref (Mfix *);
92 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
93 static Mnode *add_minipool_backward_ref (Mfix *);
94 static void assign_minipool_offsets (Mfix *);
95 static void arm_print_value (FILE *, rtx);
96 static void dump_minipool (rtx);
97 static int arm_barrier_cost (rtx);
98 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
99 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
100 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
101 rtx);
102 static void arm_reorg (void);
103 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
104 static int current_file_function_operand (rtx);
105 static unsigned long arm_compute_save_reg0_reg12_mask (void);
106 static unsigned long arm_compute_save_reg_mask (void);
107 static unsigned long arm_isr_value (tree);
108 static unsigned long arm_compute_func_type (void);
109 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
110 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
111 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
112 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
113 #endif
114 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
115 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
116 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
117 static int arm_comp_type_attributes (tree, tree);
118 static void arm_set_default_type_attributes (tree);
119 static int arm_adjust_cost (rtx, rtx, rtx, int);
120 static int count_insns_for_constant (HOST_WIDE_INT, int);
121 static int arm_get_strip_length (int);
122 static bool arm_function_ok_for_sibcall (tree, tree);
123 static void arm_internal_label (FILE *, const char *, unsigned long);
124 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
125 tree);
126 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
127 static bool arm_size_rtx_costs (rtx, int, int, int *);
128 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
129 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
130 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
131 static bool arm_9e_rtx_costs (rtx, int, int, int *);
132 static int arm_address_cost (rtx);
133 static bool arm_memory_load_p (rtx);
134 static bool arm_cirrus_insn_p (rtx);
135 static void cirrus_reorg (rtx);
136 static void arm_init_builtins (void);
137 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
138 static void arm_init_iwmmxt_builtins (void);
139 static rtx safe_vector_operand (rtx, enum machine_mode);
140 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
141 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
142 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
143 static void emit_constant_insn (rtx cond, rtx pattern);
144 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
145 tree, bool);
147 #ifndef ARM_PE
148 static void arm_encode_section_info (tree, rtx, int);
149 #endif
151 static void arm_file_end (void);
153 #ifdef AOF_ASSEMBLER
154 static void aof_globalize_label (FILE *, const char *);
155 static void aof_dump_imports (FILE *);
156 static void aof_dump_pic_table (FILE *);
157 static void aof_file_start (void);
158 static void aof_file_end (void);
159 #endif
160 static rtx arm_struct_value_rtx (tree, int);
161 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
162 tree, int *, int);
163 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
164 enum machine_mode, tree, bool);
165 static bool arm_promote_prototypes (tree);
166 static bool arm_default_short_enums (void);
167 static bool arm_align_anon_bitfield (void);
169 static tree arm_cxx_guard_type (void);
170 static bool arm_cxx_guard_mask_bit (void);
171 static tree arm_get_cookie_size (tree);
172 static bool arm_cookie_has_size (void);
173 static bool arm_cxx_cdtor_returns_this (void);
174 static bool arm_cxx_key_method_may_be_inline (void);
175 static void arm_cxx_determine_class_data_visibility (tree);
176 static bool arm_cxx_class_data_always_comdat (void);
177 static bool arm_cxx_use_aeabi_atexit (void);
178 static void arm_init_libfuncs (void);
179 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
181 /* Initialize the GCC target structure. */
182 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
183 #undef TARGET_MERGE_DECL_ATTRIBUTES
184 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
185 #endif
187 #undef TARGET_ATTRIBUTE_TABLE
188 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
190 #undef TARGET_ASM_FILE_END
191 #define TARGET_ASM_FILE_END arm_file_end
193 #ifdef AOF_ASSEMBLER
194 #undef TARGET_ASM_BYTE_OP
195 #define TARGET_ASM_BYTE_OP "\tDCB\t"
196 #undef TARGET_ASM_ALIGNED_HI_OP
197 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
198 #undef TARGET_ASM_ALIGNED_SI_OP
199 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
200 #undef TARGET_ASM_GLOBALIZE_LABEL
201 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
202 #undef TARGET_ASM_FILE_START
203 #define TARGET_ASM_FILE_START aof_file_start
204 #undef TARGET_ASM_FILE_END
205 #define TARGET_ASM_FILE_END aof_file_end
206 #else
207 #undef TARGET_ASM_ALIGNED_SI_OP
208 #define TARGET_ASM_ALIGNED_SI_OP NULL
209 #undef TARGET_ASM_INTEGER
210 #define TARGET_ASM_INTEGER arm_assemble_integer
211 #endif
213 #undef TARGET_ASM_FUNCTION_PROLOGUE
214 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
216 #undef TARGET_ASM_FUNCTION_EPILOGUE
217 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
219 #undef TARGET_COMP_TYPE_ATTRIBUTES
220 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
222 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
223 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
225 #undef TARGET_SCHED_ADJUST_COST
226 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
228 #undef TARGET_ENCODE_SECTION_INFO
229 #ifdef ARM_PE
230 #define TARGET_ENCODE_SECTION_INFO arm_pe_encode_section_info
231 #else
232 #define TARGET_ENCODE_SECTION_INFO arm_encode_section_info
233 #endif
235 #undef TARGET_STRIP_NAME_ENCODING
236 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
238 #undef TARGET_ASM_INTERNAL_LABEL
239 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
241 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
242 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
244 #undef TARGET_ASM_OUTPUT_MI_THUNK
245 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
246 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
247 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
249 /* This will be overridden in arm_override_options. */
250 #undef TARGET_RTX_COSTS
251 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
252 #undef TARGET_ADDRESS_COST
253 #define TARGET_ADDRESS_COST arm_address_cost
255 #undef TARGET_SHIFT_TRUNCATION_MASK
256 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
257 #undef TARGET_VECTOR_MODE_SUPPORTED_P
258 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
260 #undef TARGET_MACHINE_DEPENDENT_REORG
261 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
263 #undef TARGET_INIT_BUILTINS
264 #define TARGET_INIT_BUILTINS arm_init_builtins
265 #undef TARGET_EXPAND_BUILTIN
266 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
268 #undef TARGET_INIT_LIBFUNCS
269 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
271 #undef TARGET_PROMOTE_FUNCTION_ARGS
272 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
273 #undef TARGET_PROMOTE_FUNCTION_RETURN
274 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
275 #undef TARGET_PROMOTE_PROTOTYPES
276 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
277 #undef TARGET_PASS_BY_REFERENCE
278 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
279 #undef TARGET_ARG_PARTIAL_BYTES
280 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
282 #undef TARGET_STRUCT_VALUE_RTX
283 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
285 #undef TARGET_SETUP_INCOMING_VARARGS
286 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
288 #undef TARGET_DEFAULT_SHORT_ENUMS
289 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
291 #undef TARGET_ALIGN_ANON_BITFIELD
292 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
294 #undef TARGET_CXX_GUARD_TYPE
295 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
297 #undef TARGET_CXX_GUARD_MASK_BIT
298 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
300 #undef TARGET_CXX_GET_COOKIE_SIZE
301 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
303 #undef TARGET_CXX_COOKIE_HAS_SIZE
304 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
306 #undef TARGET_CXX_CDTOR_RETURNS_THIS
307 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
309 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
310 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
312 #undef TARGET_CXX_USE_AEABI_ATEXIT
313 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
315 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
316 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
317 arm_cxx_determine_class_data_visibility
319 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
320 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
322 struct gcc_target targetm = TARGET_INITIALIZER;
324 /* Obstack for minipool constant handling. */
325 static struct obstack minipool_obstack;
326 static char * minipool_startobj;
328 /* The maximum number of insns skipped which
329 will be conditionalised if possible. */
330 static int max_insns_skipped = 5;
332 extern FILE * asm_out_file;
334 /* True if we are currently building a constant table. */
335 int making_const_table;
337 /* Define the information needed to generate branch insns. This is
338 stored from the compare operation. */
339 rtx arm_compare_op0, arm_compare_op1;
341 /* The processor for which instructions should be scheduled. */
342 enum processor_type arm_tune = arm_none;
344 /* Which floating point model to use. */
345 enum arm_fp_model arm_fp_model;
347 /* Which floating point hardware is available. */
348 enum fputype arm_fpu_arch;
350 /* Which floating point hardware to schedule for. */
351 enum fputype arm_fpu_tune;
353 /* Whether to use floating point hardware. */
354 enum float_abi_type arm_float_abi;
356 /* Which ABI to use. */
357 enum arm_abi_type arm_abi;
359 /* Set by the -mfpu=... option. */
360 const char * target_fpu_name = NULL;
362 /* Set by the -mfpe=... option. */
363 const char * target_fpe_name = NULL;
365 /* Set by the -mfloat-abi=... option. */
366 const char * target_float_abi_name = NULL;
368 /* Set by the legacy -mhard-float and -msoft-float options. */
369 const char * target_float_switch = NULL;
371 /* Set by the -mabi=... option. */
372 const char * target_abi_name = NULL;
374 /* Used to parse -mstructure_size_boundary command line option. */
375 const char * structure_size_string = NULL;
376 int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
378 /* Used for Thumb call_via trampolines. */
379 rtx thumb_call_via_label[14];
380 static int thumb_call_reg_needed;
382 /* Bit values used to identify processor capabilities. */
383 #define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
384 #define FL_ARCH3M (1 << 1) /* Extended multiply */
385 #define FL_MODE26 (1 << 2) /* 26-bit mode support */
386 #define FL_MODE32 (1 << 3) /* 32-bit mode support */
387 #define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
388 #define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
389 #define FL_THUMB (1 << 6) /* Thumb aware */
390 #define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
391 #define FL_STRONG (1 << 8) /* StrongARM */
392 #define FL_ARCH5E (1 << 9) /* DSP extensions to v5 */
393 #define FL_XSCALE (1 << 10) /* XScale */
394 #define FL_CIRRUS (1 << 11) /* Cirrus/DSP. */
395 #define FL_ARCH6 (1 << 12) /* Architecture rel 6. Adds
396 media instructions. */
397 #define FL_VFPV2 (1 << 13) /* Vector Floating Point V2. */
398 #define FL_WBUF (1 << 14) /* Schedule for write buffer ops.
399 Note: ARM6 & 7 derivatives only. */
401 #define FL_IWMMXT (1 << 29) /* XScale v2 or "Intel Wireless MMX technology". */
403 #define FL_FOR_ARCH2 0
404 #define FL_FOR_ARCH3 FL_MODE32
405 #define FL_FOR_ARCH3M (FL_FOR_ARCH3 | FL_ARCH3M)
406 #define FL_FOR_ARCH4 (FL_FOR_ARCH3M | FL_ARCH4)
407 #define FL_FOR_ARCH4T (FL_FOR_ARCH4 | FL_THUMB)
408 #define FL_FOR_ARCH5 (FL_FOR_ARCH4 | FL_ARCH5)
409 #define FL_FOR_ARCH5T (FL_FOR_ARCH5 | FL_THUMB)
410 #define FL_FOR_ARCH5E (FL_FOR_ARCH5 | FL_ARCH5E)
411 #define FL_FOR_ARCH5TE (FL_FOR_ARCH5E | FL_THUMB)
412 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
413 #define FL_FOR_ARCH6 (FL_FOR_ARCH5TE | FL_ARCH6)
414 #define FL_FOR_ARCH6J FL_FOR_ARCH6
415 #define FL_FOR_ARCH6K FL_FOR_ARCH6
416 #define FL_FOR_ARCH6Z FL_FOR_ARCH6
417 #define FL_FOR_ARCH6ZK FL_FOR_ARCH6
419 /* The bits in this mask specify which
420 instructions we are allowed to generate. */
421 static unsigned long insn_flags = 0;
423 /* The bits in this mask specify which instruction scheduling options should
424 be used. */
425 static unsigned long tune_flags = 0;
427 /* The following are used in the arm.md file as equivalents to bits
428 in the above two flag variables. */
430 /* Nonzero if this chip supports the ARM Architecture 3M extensions. */
431 int arm_arch3m = 0;
433 /* Nonzero if this chip supports the ARM Architecture 4 extensions. */
434 int arm_arch4 = 0;
436 /* Nonzero if this chip supports the ARM Architecture 4t extensions. */
437 int arm_arch4t = 0;
439 /* Nonzero if this chip supports the ARM Architecture 5 extensions. */
440 int arm_arch5 = 0;
442 /* Nonzero if this chip supports the ARM Architecture 5E extensions. */
443 int arm_arch5e = 0;
445 /* Nonzero if this chip supports the ARM Architecture 6 extensions. */
446 int arm_arch6 = 0;
448 /* Nonzero if this chip can benefit from load scheduling. */
449 int arm_ld_sched = 0;
451 /* Nonzero if this chip is a StrongARM. */
452 int arm_tune_strongarm = 0;
454 /* Nonzero if this chip is a Cirrus variant. */
455 int arm_arch_cirrus = 0;
457 /* Nonzero if this chip supports Intel Wireless MMX technology. */
458 int arm_arch_iwmmxt = 0;
460 /* Nonzero if this chip is an XScale. */
461 int arm_arch_xscale = 0;
463 /* Nonzero if tuning for XScale */
464 int arm_tune_xscale = 0;
466 /* Nonzero if we want to tune for stores that access the write-buffer.
467 This typically means an ARM6 or ARM7 with MMU or MPU. */
468 int arm_tune_wbuf = 0;
470 /* Nonzero if generating Thumb instructions. */
471 int thumb_code = 0;
473 /* Nonzero if we should define __THUMB_INTERWORK__ in the
474 preprocessor.
475 XXX This is a bit of a hack, it's intended to help work around
476 problems in GLD which doesn't understand that armv5t code is
477 interworking clean. */
478 int arm_cpp_interwork = 0;
480 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
481 must report the mode of the memory reference from PRINT_OPERAND to
482 PRINT_OPERAND_ADDRESS. */
483 enum machine_mode output_memory_reference_mode;
485 /* The register number to be used for the PIC offset register. */
486 const char * arm_pic_register_string = NULL;
487 int arm_pic_register = INVALID_REGNUM;
489 /* Set to 1 when a return insn is output, this means that the epilogue
490 is not needed. */
491 int return_used_this_function;
493 /* Set to 1 after arm_reorg has started. Reset to start at the start of
494 the next function. */
495 static int after_arm_reorg = 0;
497 /* The maximum number of insns to be used when loading a constant. */
498 static int arm_constant_limit = 3;
500 /* For an explanation of these variables, see final_prescan_insn below. */
501 int arm_ccfsm_state;
502 enum arm_cond_code arm_current_cc;
503 rtx arm_target_insn;
504 int arm_target_label;
506 /* The condition codes of the ARM, and the inverse function. */
507 static const char * const arm_condition_codes[] =
509 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
510 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
513 #define streq(string1, string2) (strcmp (string1, string2) == 0)
515 /* Initialization code. */
517 struct processors
519 const char *const name;
520 enum processor_type core;
521 const char *arch;
522 const unsigned long flags;
523 bool (* rtx_costs) (rtx, int, int, int *);
526 /* Not all of these give usefully different compilation alternatives,
527 but there is no simple way of generalizing them. */
528 static const struct processors all_cores[] =
530 /* ARM Cores */
531 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
532 {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
533 #include "arm-cores.def"
534 #undef ARM_CORE
535 {NULL, arm_none, NULL, 0, NULL}
538 static const struct processors all_architectures[] =
540 /* ARM Architectures */
541 /* We don't specify rtx_costs here as it will be figured out
542 from the core. */
544 {"armv2", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
545 {"armv2a", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
546 {"armv3", arm6, "3", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
547 {"armv3m", arm7m, "3M", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
548 {"armv4", arm7tdmi, "4", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
549 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
550 implementations that support it, so we will leave it out for now. */
551 {"armv4t", arm7tdmi, "4T", FL_CO_PROC | FL_FOR_ARCH4T, NULL},
552 {"armv5", arm10tdmi, "5", FL_CO_PROC | FL_FOR_ARCH5, NULL},
553 {"armv5t", arm10tdmi, "5T", FL_CO_PROC | FL_FOR_ARCH5T, NULL},
554 {"armv5e", arm1026ejs, "5E", FL_CO_PROC | FL_FOR_ARCH5E, NULL},
555 {"armv5te", arm1026ejs, "5TE", FL_CO_PROC | FL_FOR_ARCH5TE, NULL},
556 {"armv6", arm1136js, "6", FL_CO_PROC | FL_FOR_ARCH6, NULL},
557 {"armv6j", arm1136js, "6J", FL_CO_PROC | FL_FOR_ARCH6J, NULL},
558 {"armv6k", mpcore, "6K", FL_CO_PROC | FL_FOR_ARCH6K, NULL},
559 {"armv6z", arm1176jzs, "6Z", FL_CO_PROC | FL_FOR_ARCH6Z, NULL},
560 {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC | FL_FOR_ARCH6ZK, NULL},
561 {"ep9312", ep9312, "4T", FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
562 {"iwmmxt", iwmmxt, "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
563 {NULL, arm_none, NULL, 0 , NULL}
566 /* This is a magic structure. The 'string' field is magically filled in
567 with a pointer to the value specified by the user on the command line
568 assuming that the user has specified such a value. */
570 struct arm_cpu_select arm_select[] =
572 /* string name processors */
573 { NULL, "-mcpu=", all_cores },
574 { NULL, "-march=", all_architectures },
575 { NULL, "-mtune=", all_cores }
579 /* The name of the proprocessor macro to define for this architecture. */
581 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
583 struct fpu_desc
585 const char * name;
586 enum fputype fpu;
590 /* Available values for for -mfpu=. */
592 static const struct fpu_desc all_fpus[] =
594 {"fpa", FPUTYPE_FPA},
595 {"fpe2", FPUTYPE_FPA_EMU2},
596 {"fpe3", FPUTYPE_FPA_EMU2},
597 {"maverick", FPUTYPE_MAVERICK},
598 {"vfp", FPUTYPE_VFP}
602 /* Floating point models used by the different hardware.
603 See fputype in arm.h. */
605 static const enum fputype fp_model_for_fpu[] =
607 /* No FP hardware. */
608 ARM_FP_MODEL_UNKNOWN, /* FPUTYPE_NONE */
609 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA */
610 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU2 */
611 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU3 */
612 ARM_FP_MODEL_MAVERICK, /* FPUTYPE_MAVERICK */
613 ARM_FP_MODEL_VFP /* FPUTYPE_VFP */
617 struct float_abi
619 const char * name;
620 enum float_abi_type abi_type;
624 /* Available values for -mfloat-abi=. */
626 static const struct float_abi all_float_abis[] =
628 {"soft", ARM_FLOAT_ABI_SOFT},
629 {"softfp", ARM_FLOAT_ABI_SOFTFP},
630 {"hard", ARM_FLOAT_ABI_HARD}
634 struct abi_name
636 const char *name;
637 enum arm_abi_type abi_type;
641 /* Available values for -mabi=. */
643 static const struct abi_name arm_all_abis[] =
645 {"apcs-gnu", ARM_ABI_APCS},
646 {"atpcs", ARM_ABI_ATPCS},
647 {"aapcs", ARM_ABI_AAPCS},
648 {"iwmmxt", ARM_ABI_IWMMXT}
651 /* Return the number of bits set in VALUE. */
652 static unsigned
653 bit_count (unsigned long value)
655 unsigned long count = 0;
657 while (value)
659 count++;
660 value &= value - 1; /* Clear the least-significant set bit. */
663 return count;
666 /* Set up library functions unique to ARM. */
668 static void
669 arm_init_libfuncs (void)
671 /* There are no special library functions unless we are using the
672 ARM BPABI. */
673 if (!TARGET_BPABI)
674 return;
676 /* The functions below are described in Section 4 of the "Run-Time
677 ABI for the ARM architecture", Version 1.0. */
679 /* Double-precision floating-point arithmetic. Table 2. */
680 set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
681 set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
682 set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
683 set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
684 set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
686 /* Double-precision comparisons. Table 3. */
687 set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
688 set_optab_libfunc (ne_optab, DFmode, NULL);
689 set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
690 set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
691 set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
692 set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
693 set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
695 /* Single-precision floating-point arithmetic. Table 4. */
696 set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
697 set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
698 set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
699 set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
700 set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
702 /* Single-precision comparisons. Table 5. */
703 set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
704 set_optab_libfunc (ne_optab, SFmode, NULL);
705 set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
706 set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
707 set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
708 set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
709 set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
711 /* Floating-point to integer conversions. Table 6. */
712 set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
713 set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
714 set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
715 set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
716 set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
717 set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
718 set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
719 set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
721 /* Conversions between floating types. Table 7. */
722 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
723 set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
725 /* Integer to floating-point conversions. Table 8. */
726 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
727 set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
728 set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
729 set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
730 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
731 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
732 set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
733 set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
735 /* Long long. Table 9. */
736 set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
737 set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
738 set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
739 set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
740 set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
741 set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
742 set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
743 set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
745 /* Integer (32/32->32) division. \S 4.3.1. */
746 set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
747 set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
749 /* The divmod functions are designed so that they can be used for
750 plain division, even though they return both the quotient and the
751 remainder. The quotient is returned in the usual location (i.e.,
752 r0 for SImode, {r0, r1} for DImode), just as would be expected
753 for an ordinary division routine. Because the AAPCS calling
754 conventions specify that all of { r0, r1, r2, r3 } are
755 callee-saved registers, there is no need to tell the compiler
756 explicitly that those registers are clobbered by these
757 routines. */
758 set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
759 set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
760 set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idivmod");
761 set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidivmod");
763 /* We don't have mod libcalls. Fortunately gcc knows how to use the
764 divmod libcalls instead. */
765 set_optab_libfunc (smod_optab, DImode, NULL);
766 set_optab_libfunc (umod_optab, DImode, NULL);
767 set_optab_libfunc (smod_optab, SImode, NULL);
768 set_optab_libfunc (umod_optab, SImode, NULL);
771 /* Fix up any incompatible options that the user has specified.
772 This has now turned into a maze. */
773 void
774 arm_override_options (void)
776 unsigned i;
778 /* Set up the flags based on the cpu/architecture selected by the user. */
779 for (i = ARRAY_SIZE (arm_select); i--;)
781 struct arm_cpu_select * ptr = arm_select + i;
783 if (ptr->string != NULL && ptr->string[0] != '\0')
785 const struct processors * sel;
787 for (sel = ptr->processors; sel->name != NULL; sel++)
788 if (streq (ptr->string, sel->name))
790 /* Set the architecture define. */
791 if (i != 2)
792 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
794 /* Determine the processor core for which we should
795 tune code-generation. */
796 if (/* -mcpu= is a sensible default. */
797 i == 0
798 /* If -march= is used, and -mcpu= has not been used,
799 assume that we should tune for a representative
800 CPU from that architecture. */
801 || i == 1
802 /* -mtune= overrides -mcpu= and -march=. */
803 || i == 2)
804 arm_tune = (enum processor_type) (sel - ptr->processors);
806 if (i != 2)
808 /* If we have been given an architecture and a processor
809 make sure that they are compatible. We only generate
810 a warning though, and we prefer the CPU over the
811 architecture. */
812 if (insn_flags != 0 && (insn_flags ^ sel->flags))
813 warning (0, "switch -mcpu=%s conflicts with -march= switch",
814 ptr->string);
816 insn_flags = sel->flags;
819 break;
822 if (sel->name == NULL)
823 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
827 /* If the user did not specify a processor, choose one for them. */
828 if (insn_flags == 0)
830 const struct processors * sel;
831 unsigned int sought;
832 enum processor_type cpu;
834 cpu = TARGET_CPU_DEFAULT;
835 if (cpu == arm_none)
837 #ifdef SUBTARGET_CPU_DEFAULT
838 /* Use the subtarget default CPU if none was specified by
839 configure. */
840 cpu = SUBTARGET_CPU_DEFAULT;
841 #endif
842 /* Default to ARM6. */
843 if (cpu == arm_none)
844 cpu = arm6;
846 sel = &all_cores[cpu];
848 insn_flags = sel->flags;
850 /* Now check to see if the user has specified some command line
851 switch that require certain abilities from the cpu. */
852 sought = 0;
854 if (TARGET_INTERWORK || TARGET_THUMB)
856 sought |= (FL_THUMB | FL_MODE32);
858 /* There are no ARM processors that support both APCS-26 and
859 interworking. Therefore we force FL_MODE26 to be removed
860 from insn_flags here (if it was set), so that the search
861 below will always be able to find a compatible processor. */
862 insn_flags &= ~FL_MODE26;
865 if (sought != 0 && ((sought & insn_flags) != sought))
867 /* Try to locate a CPU type that supports all of the abilities
868 of the default CPU, plus the extra abilities requested by
869 the user. */
870 for (sel = all_cores; sel->name != NULL; sel++)
871 if ((sel->flags & sought) == (sought | insn_flags))
872 break;
874 if (sel->name == NULL)
876 unsigned current_bit_count = 0;
877 const struct processors * best_fit = NULL;
879 /* Ideally we would like to issue an error message here
880 saying that it was not possible to find a CPU compatible
881 with the default CPU, but which also supports the command
882 line options specified by the programmer, and so they
883 ought to use the -mcpu=<name> command line option to
884 override the default CPU type.
886 If we cannot find a cpu that has both the
887 characteristics of the default cpu and the given
888 command line options we scan the array again looking
889 for a best match. */
890 for (sel = all_cores; sel->name != NULL; sel++)
891 if ((sel->flags & sought) == sought)
893 unsigned count;
895 count = bit_count (sel->flags & insn_flags);
897 if (count >= current_bit_count)
899 best_fit = sel;
900 current_bit_count = count;
904 if (best_fit == NULL)
905 abort ();
906 else
907 sel = best_fit;
910 insn_flags = sel->flags;
912 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
913 if (arm_tune == arm_none)
914 arm_tune = (enum processor_type) (sel - all_cores);
917 /* The processor for which we should tune should now have been
918 chosen. */
919 if (arm_tune == arm_none)
920 abort ();
922 tune_flags = all_cores[(int)arm_tune].flags;
923 if (optimize_size)
924 targetm.rtx_costs = arm_size_rtx_costs;
925 else
926 targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
928 /* Make sure that the processor choice does not conflict with any of the
929 other command line choices. */
930 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
932 warning (0, "target CPU does not support interworking" );
933 target_flags &= ~ARM_FLAG_INTERWORK;
936 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
938 warning (0, "target CPU does not support THUMB instructions");
939 target_flags &= ~ARM_FLAG_THUMB;
942 if (TARGET_APCS_FRAME && TARGET_THUMB)
944 /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
945 target_flags &= ~ARM_FLAG_APCS_FRAME;
948 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
949 from here where no function is being compiled currently. */
950 if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
951 && TARGET_ARM)
952 warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
954 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
955 warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
957 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
958 warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
960 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
962 warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
963 target_flags |= ARM_FLAG_APCS_FRAME;
966 if (TARGET_POKE_FUNCTION_NAME)
967 target_flags |= ARM_FLAG_APCS_FRAME;
969 if (TARGET_APCS_REENT && flag_pic)
970 error ("-fpic and -mapcs-reent are incompatible");
972 if (TARGET_APCS_REENT)
973 warning (0, "APCS reentrant code not supported. Ignored");
975 /* If this target is normally configured to use APCS frames, warn if they
976 are turned off and debugging is turned on. */
977 if (TARGET_ARM
978 && write_symbols != NO_DEBUG
979 && !TARGET_APCS_FRAME
980 && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
981 warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
983 /* If stack checking is disabled, we can use r10 as the PIC register,
984 which keeps r9 available. */
985 if (flag_pic)
986 arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
988 if (TARGET_APCS_FLOAT)
989 warning (0, "passing floating point arguments in fp regs not yet supported");
991 /* Initialize boolean versions of the flags, for use in the arm.md file. */
992 arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
993 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
994 arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
995 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
996 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
997 arm_arch6 = (insn_flags & FL_ARCH6) != 0;
998 arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
999 arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1001 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1002 arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1003 thumb_code = (TARGET_ARM == 0);
1004 arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1005 arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1006 arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1008 /* V5 code we generate is completely interworking capable, so we turn off
1009 TARGET_INTERWORK here to avoid many tests later on. */
1011 /* XXX However, we must pass the right pre-processor defines to CPP
1012 or GLD can get confused. This is a hack. */
1013 if (TARGET_INTERWORK)
1014 arm_cpp_interwork = 1;
1016 if (arm_arch5)
1017 target_flags &= ~ARM_FLAG_INTERWORK;
1019 if (target_abi_name)
1021 for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1023 if (streq (arm_all_abis[i].name, target_abi_name))
1025 arm_abi = arm_all_abis[i].abi_type;
1026 break;
1029 if (i == ARRAY_SIZE (arm_all_abis))
1030 error ("invalid ABI option: -mabi=%s", target_abi_name);
1032 else
1033 arm_abi = ARM_DEFAULT_ABI;
1035 if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1036 error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1038 if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1039 error ("iwmmxt abi requires an iwmmxt capable cpu");
1041 arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1042 if (target_fpu_name == NULL && target_fpe_name != NULL)
1044 if (streq (target_fpe_name, "2"))
1045 target_fpu_name = "fpe2";
1046 else if (streq (target_fpe_name, "3"))
1047 target_fpu_name = "fpe3";
1048 else
1049 error ("invalid floating point emulation option: -mfpe=%s",
1050 target_fpe_name);
1052 if (target_fpu_name != NULL)
1054 /* The user specified a FPU. */
1055 for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1057 if (streq (all_fpus[i].name, target_fpu_name))
1059 arm_fpu_arch = all_fpus[i].fpu;
1060 arm_fpu_tune = arm_fpu_arch;
1061 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1062 break;
1065 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1066 error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1068 else
1070 #ifdef FPUTYPE_DEFAULT
1071 /* Use the default if it is specified for this platform. */
1072 arm_fpu_arch = FPUTYPE_DEFAULT;
1073 arm_fpu_tune = FPUTYPE_DEFAULT;
1074 #else
1075 /* Pick one based on CPU type. */
1076 /* ??? Some targets assume FPA is the default.
1077 if ((insn_flags & FL_VFP) != 0)
1078 arm_fpu_arch = FPUTYPE_VFP;
1079 else
1081 if (arm_arch_cirrus)
1082 arm_fpu_arch = FPUTYPE_MAVERICK;
1083 else
1084 arm_fpu_arch = FPUTYPE_FPA_EMU2;
1085 #endif
1086 if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1087 arm_fpu_tune = FPUTYPE_FPA;
1088 else
1089 arm_fpu_tune = arm_fpu_arch;
1090 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1091 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1092 abort ();
1095 if (target_float_abi_name != NULL)
1097 /* The user specified a FP ABI. */
1098 for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1100 if (streq (all_float_abis[i].name, target_float_abi_name))
1102 arm_float_abi = all_float_abis[i].abi_type;
1103 break;
1106 if (i == ARRAY_SIZE (all_float_abis))
1107 error ("invalid floating point abi: -mfloat-abi=%s",
1108 target_float_abi_name);
1110 else if (target_float_switch)
1112 /* This is a bit of a hack to avoid needing target flags for these. */
1113 if (target_float_switch[0] == 'h')
1114 arm_float_abi = ARM_FLOAT_ABI_HARD;
1115 else
1116 arm_float_abi = ARM_FLOAT_ABI_SOFT;
1118 else
1119 arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1121 if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1122 sorry ("-mfloat-abi=hard and VFP");
1124 /* If soft-float is specified then don't use FPU. */
1125 if (TARGET_SOFT_FLOAT)
1126 arm_fpu_arch = FPUTYPE_NONE;
1128 /* For arm2/3 there is no need to do any scheduling if there is only
1129 a floating point emulator, or we are doing software floating-point. */
1130 if ((TARGET_SOFT_FLOAT
1131 || arm_fpu_tune == FPUTYPE_FPA_EMU2
1132 || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1133 && (tune_flags & FL_MODE32) == 0)
1134 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1136 /* Override the default structure alignment for AAPCS ABI. */
1137 if (arm_abi == ARM_ABI_AAPCS)
1138 arm_structure_size_boundary = 8;
1140 if (structure_size_string != NULL)
1142 int size = strtol (structure_size_string, NULL, 0);
1144 if (size == 8 || size == 32
1145 || (ARM_DOUBLEWORD_ALIGN && size == 64))
1146 arm_structure_size_boundary = size;
1147 else
1148 warning (0, "structure size boundary can only be set to %s",
1149 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1152 if (arm_pic_register_string != NULL)
1154 int pic_register = decode_reg_name (arm_pic_register_string);
1156 if (!flag_pic)
1157 warning (0, "-mpic-register= is useless without -fpic");
1159 /* Prevent the user from choosing an obviously stupid PIC register. */
1160 else if (pic_register < 0 || call_used_regs[pic_register]
1161 || pic_register == HARD_FRAME_POINTER_REGNUM
1162 || pic_register == STACK_POINTER_REGNUM
1163 || pic_register >= PC_REGNUM)
1164 error ("unable to use '%s' for PIC register", arm_pic_register_string);
1165 else
1166 arm_pic_register = pic_register;
1169 if (TARGET_THUMB && flag_schedule_insns)
1171 /* Don't warn since it's on by default in -O2. */
1172 flag_schedule_insns = 0;
1175 if (optimize_size)
1177 /* There's some dispute as to whether this should be 1 or 2. However,
1178 experiments seem to show that in pathological cases a setting of
1179 1 degrades less severely than a setting of 2. This could change if
1180 other parts of the compiler change their behavior. */
1181 arm_constant_limit = 1;
1183 /* If optimizing for size, bump the number of instructions that we
1184 are prepared to conditionally execute (even on a StrongARM). */
1185 max_insns_skipped = 6;
1187 else
1189 /* For processors with load scheduling, it never costs more than
1190 2 cycles to load a constant, and the load scheduler may well
1191 reduce that to 1. */
1192 if (arm_ld_sched)
1193 arm_constant_limit = 1;
1195 /* On XScale the longer latency of a load makes it more difficult
1196 to achieve a good schedule, so it's faster to synthesize
1197 constants that can be done in two insns. */
1198 if (arm_tune_xscale)
1199 arm_constant_limit = 2;
1201 /* StrongARM has early execution of branches, so a sequence
1202 that is worth skipping is shorter. */
1203 if (arm_tune_strongarm)
1204 max_insns_skipped = 3;
1207 /* Register global variables with the garbage collector. */
1208 arm_add_gc_roots ();
1211 static void
1212 arm_add_gc_roots (void)
1214 gcc_obstack_init(&minipool_obstack);
1215 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1218 /* A table of known ARM exception types.
1219 For use with the interrupt function attribute. */
1221 typedef struct
1223 const char *const arg;
1224 const unsigned long return_value;
1226 isr_attribute_arg;
1228 static const isr_attribute_arg isr_attribute_args [] =
1230 { "IRQ", ARM_FT_ISR },
1231 { "irq", ARM_FT_ISR },
1232 { "FIQ", ARM_FT_FIQ },
1233 { "fiq", ARM_FT_FIQ },
1234 { "ABORT", ARM_FT_ISR },
1235 { "abort", ARM_FT_ISR },
1236 { "ABORT", ARM_FT_ISR },
1237 { "abort", ARM_FT_ISR },
1238 { "UNDEF", ARM_FT_EXCEPTION },
1239 { "undef", ARM_FT_EXCEPTION },
1240 { "SWI", ARM_FT_EXCEPTION },
1241 { "swi", ARM_FT_EXCEPTION },
1242 { NULL, ARM_FT_NORMAL }
1245 /* Returns the (interrupt) function type of the current
1246 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
1248 static unsigned long
1249 arm_isr_value (tree argument)
1251 const isr_attribute_arg * ptr;
1252 const char * arg;
1254 /* No argument - default to IRQ. */
1255 if (argument == NULL_TREE)
1256 return ARM_FT_ISR;
1258 /* Get the value of the argument. */
1259 if (TREE_VALUE (argument) == NULL_TREE
1260 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1261 return ARM_FT_UNKNOWN;
1263 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1265 /* Check it against the list of known arguments. */
1266 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1267 if (streq (arg, ptr->arg))
1268 return ptr->return_value;
1270 /* An unrecognized interrupt type. */
1271 return ARM_FT_UNKNOWN;
1274 /* Computes the type of the current function. */
1276 static unsigned long
1277 arm_compute_func_type (void)
1279 unsigned long type = ARM_FT_UNKNOWN;
1280 tree a;
1281 tree attr;
1283 if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
1284 abort ();
1286 /* Decide if the current function is volatile. Such functions
1287 never return, and many memory cycles can be saved by not storing
1288 register values that will never be needed again. This optimization
1289 was added to speed up context switching in a kernel application. */
1290 if (optimize > 0
1291 && TREE_NOTHROW (current_function_decl)
1292 && TREE_THIS_VOLATILE (current_function_decl))
1293 type |= ARM_FT_VOLATILE;
1295 if (cfun->static_chain_decl != NULL)
1296 type |= ARM_FT_NESTED;
1298 attr = DECL_ATTRIBUTES (current_function_decl);
1300 a = lookup_attribute ("naked", attr);
1301 if (a != NULL_TREE)
1302 type |= ARM_FT_NAKED;
1304 a = lookup_attribute ("isr", attr);
1305 if (a == NULL_TREE)
1306 a = lookup_attribute ("interrupt", attr);
1308 if (a == NULL_TREE)
1309 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1310 else
1311 type |= arm_isr_value (TREE_VALUE (a));
1313 return type;
1316 /* Returns the type of the current function. */
1318 unsigned long
1319 arm_current_func_type (void)
1321 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1322 cfun->machine->func_type = arm_compute_func_type ();
1324 return cfun->machine->func_type;
1327 /* Return 1 if it is possible to return using a single instruction.
1328 If SIBLING is non-null, this is a test for a return before a sibling
1329 call. SIBLING is the call insn, so we can examine its register usage. */
1332 use_return_insn (int iscond, rtx sibling)
1334 int regno;
1335 unsigned int func_type;
1336 unsigned long saved_int_regs;
1337 unsigned HOST_WIDE_INT stack_adjust;
1338 arm_stack_offsets *offsets;
1340 /* Never use a return instruction before reload has run. */
1341 if (!reload_completed)
1342 return 0;
1344 func_type = arm_current_func_type ();
1346 /* Naked functions and volatile functions need special
1347 consideration. */
1348 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1349 return 0;
1351 /* So do interrupt functions that use the frame pointer. */
1352 if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1353 return 0;
1355 offsets = arm_get_frame_offsets ();
1356 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1358 /* As do variadic functions. */
1359 if (current_function_pretend_args_size
1360 || cfun->machine->uses_anonymous_args
1361 /* Or if the function calls __builtin_eh_return () */
1362 || current_function_calls_eh_return
1363 /* Or if the function calls alloca */
1364 || current_function_calls_alloca
1365 /* Or if there is a stack adjustment. However, if the stack pointer
1366 is saved on the stack, we can use a pre-incrementing stack load. */
1367 || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1368 return 0;
1370 saved_int_regs = arm_compute_save_reg_mask ();
1372 /* Unfortunately, the insn
1374 ldmib sp, {..., sp, ...}
1376 triggers a bug on most SA-110 based devices, such that the stack
1377 pointer won't be correctly restored if the instruction takes a
1378 page fault. We work around this problem by popping r3 along with
1379 the other registers, since that is never slower than executing
1380 another instruction.
1382 We test for !arm_arch5 here, because code for any architecture
1383 less than this could potentially be run on one of the buggy
1384 chips. */
1385 if (stack_adjust == 4 && !arm_arch5)
1387 /* Validate that r3 is a call-clobbered register (always true in
1388 the default abi) ... */
1389 if (!call_used_regs[3])
1390 return 0;
1392 /* ... that it isn't being used for a return value (always true
1393 until we implement return-in-regs), or for a tail-call
1394 argument ... */
1395 if (sibling)
1397 if (GET_CODE (sibling) != CALL_INSN)
1398 abort ();
1400 if (find_regno_fusage (sibling, USE, 3))
1401 return 0;
1404 /* ... and that there are no call-saved registers in r0-r2
1405 (always true in the default ABI). */
1406 if (saved_int_regs & 0x7)
1407 return 0;
1410 /* Can't be done if interworking with Thumb, and any registers have been
1411 stacked. */
1412 if (TARGET_INTERWORK && saved_int_regs != 0)
1413 return 0;
1415 /* On StrongARM, conditional returns are expensive if they aren't
1416 taken and multiple registers have been stacked. */
1417 if (iscond && arm_tune_strongarm)
1419 /* Conditional return when just the LR is stored is a simple
1420 conditional-load instruction, that's not expensive. */
1421 if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1422 return 0;
1424 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1425 return 0;
1428 /* If there are saved registers but the LR isn't saved, then we need
1429 two instructions for the return. */
1430 if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1431 return 0;
1433 /* Can't be done if any of the FPA regs are pushed,
1434 since this also requires an insn. */
1435 if (TARGET_HARD_FLOAT && TARGET_FPA)
1436 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1437 if (regs_ever_live[regno] && !call_used_regs[regno])
1438 return 0;
1440 /* Likewise VFP regs. */
1441 if (TARGET_HARD_FLOAT && TARGET_VFP)
1442 for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1443 if (regs_ever_live[regno] && !call_used_regs[regno])
1444 return 0;
1446 if (TARGET_REALLY_IWMMXT)
1447 for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1448 if (regs_ever_live[regno] && ! call_used_regs [regno])
1449 return 0;
1451 return 1;
1454 /* Return TRUE if int I is a valid immediate ARM constant. */
1457 const_ok_for_arm (HOST_WIDE_INT i)
1459 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1461 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1462 be all zero, or all one. */
1463 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1464 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1465 != ((~(unsigned HOST_WIDE_INT) 0)
1466 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1467 return FALSE;
1469 /* Fast return for 0 and powers of 2 */
1470 if ((i & (i - 1)) == 0)
1471 return TRUE;
1475 if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1476 return TRUE;
1477 mask =
1478 (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1479 >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1481 while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1483 return FALSE;
1486 /* Return true if I is a valid constant for the operation CODE. */
1487 static int
1488 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1490 if (const_ok_for_arm (i))
1491 return 1;
1493 switch (code)
1495 case PLUS:
1496 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1498 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
1499 case XOR:
1500 case IOR:
1501 return 0;
1503 case AND:
1504 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1506 default:
1507 abort ();
1511 /* Emit a sequence of insns to handle a large constant.
1512 CODE is the code of the operation required, it can be any of SET, PLUS,
1513 IOR, AND, XOR, MINUS;
1514 MODE is the mode in which the operation is being performed;
1515 VAL is the integer to operate on;
1516 SOURCE is the other operand (a register, or a null-pointer for SET);
1517 SUBTARGETS means it is safe to create scratch registers if that will
1518 either produce a simpler sequence, or we will want to cse the values.
1519 Return value is the number of insns emitted. */
1522 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1523 HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1525 rtx cond;
1527 if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1528 cond = COND_EXEC_TEST (PATTERN (insn));
1529 else
1530 cond = NULL_RTX;
1532 if (subtargets || code == SET
1533 || (GET_CODE (target) == REG && GET_CODE (source) == REG
1534 && REGNO (target) != REGNO (source)))
1536 /* After arm_reorg has been called, we can't fix up expensive
1537 constants by pushing them into memory so we must synthesize
1538 them in-line, regardless of the cost. This is only likely to
1539 be more costly on chips that have load delay slots and we are
1540 compiling without running the scheduler (so no splitting
1541 occurred before the final instruction emission).
1543 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1545 if (!after_arm_reorg
1546 && !cond
1547 && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1548 1, 0)
1549 > arm_constant_limit + (code != SET)))
1551 if (code == SET)
1553 /* Currently SET is the only monadic value for CODE, all
1554 the rest are diadic. */
1555 emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1556 return 1;
1558 else
1560 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1562 emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1563 /* For MINUS, the value is subtracted from, since we never
1564 have subtraction of a constant. */
1565 if (code == MINUS)
1566 emit_insn (gen_rtx_SET (VOIDmode, target,
1567 gen_rtx_MINUS (mode, temp, source)));
1568 else
1569 emit_insn (gen_rtx_SET (VOIDmode, target,
1570 gen_rtx_fmt_ee (code, mode, source, temp)));
1571 return 2;
1576 return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1580 static int
1581 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1583 HOST_WIDE_INT temp1;
1584 int num_insns = 0;
1587 int end;
1589 if (i <= 0)
1590 i += 32;
1591 if (remainder & (3 << (i - 2)))
1593 end = i - 8;
1594 if (end < 0)
1595 end += 32;
1596 temp1 = remainder & ((0x0ff << end)
1597 | ((i < end) ? (0xff >> (32 - end)) : 0));
1598 remainder &= ~temp1;
1599 num_insns++;
1600 i -= 6;
1602 i -= 2;
1603 } while (remainder);
1604 return num_insns;
1607 /* Emit an instruction with the indicated PATTERN. If COND is
1608 non-NULL, conditionalize the execution of the instruction on COND
1609 being true. */
1611 static void
1612 emit_constant_insn (rtx cond, rtx pattern)
1614 if (cond)
1615 pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1616 emit_insn (pattern);
1619 /* As above, but extra parameter GENERATE which, if clear, suppresses
1620 RTL generation. */
1622 static int
1623 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1624 HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1625 int generate)
1627 int can_invert = 0;
1628 int can_negate = 0;
1629 int can_negate_initial = 0;
1630 int can_shift = 0;
1631 int i;
1632 int num_bits_set = 0;
1633 int set_sign_bit_copies = 0;
1634 int clear_sign_bit_copies = 0;
1635 int clear_zero_bit_copies = 0;
1636 int set_zero_bit_copies = 0;
1637 int insns = 0;
1638 unsigned HOST_WIDE_INT temp1, temp2;
1639 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1641 /* Find out which operations are safe for a given CODE. Also do a quick
1642 check for degenerate cases; these can occur when DImode operations
1643 are split. */
1644 switch (code)
1646 case SET:
1647 can_invert = 1;
1648 can_shift = 1;
1649 can_negate = 1;
1650 break;
1652 case PLUS:
1653 can_negate = 1;
1654 can_negate_initial = 1;
1655 break;
1657 case IOR:
1658 if (remainder == 0xffffffff)
1660 if (generate)
1661 emit_constant_insn (cond,
1662 gen_rtx_SET (VOIDmode, target,
1663 GEN_INT (ARM_SIGN_EXTEND (val))));
1664 return 1;
1666 if (remainder == 0)
1668 if (reload_completed && rtx_equal_p (target, source))
1669 return 0;
1670 if (generate)
1671 emit_constant_insn (cond,
1672 gen_rtx_SET (VOIDmode, target, source));
1673 return 1;
1675 break;
1677 case AND:
1678 if (remainder == 0)
1680 if (generate)
1681 emit_constant_insn (cond,
1682 gen_rtx_SET (VOIDmode, target, const0_rtx));
1683 return 1;
1685 if (remainder == 0xffffffff)
1687 if (reload_completed && rtx_equal_p (target, source))
1688 return 0;
1689 if (generate)
1690 emit_constant_insn (cond,
1691 gen_rtx_SET (VOIDmode, target, source));
1692 return 1;
1694 can_invert = 1;
1695 break;
1697 case XOR:
1698 if (remainder == 0)
1700 if (reload_completed && rtx_equal_p (target, source))
1701 return 0;
1702 if (generate)
1703 emit_constant_insn (cond,
1704 gen_rtx_SET (VOIDmode, target, source));
1705 return 1;
1707 if (remainder == 0xffffffff)
1709 if (generate)
1710 emit_constant_insn (cond,
1711 gen_rtx_SET (VOIDmode, target,
1712 gen_rtx_NOT (mode, source)));
1713 return 1;
1716 /* We don't know how to handle this yet below. */
1717 abort ();
1719 case MINUS:
1720 /* We treat MINUS as (val - source), since (source - val) is always
1721 passed as (source + (-val)). */
1722 if (remainder == 0)
1724 if (generate)
1725 emit_constant_insn (cond,
1726 gen_rtx_SET (VOIDmode, target,
1727 gen_rtx_NEG (mode, source)));
1728 return 1;
1730 if (const_ok_for_arm (val))
1732 if (generate)
1733 emit_constant_insn (cond,
1734 gen_rtx_SET (VOIDmode, target,
1735 gen_rtx_MINUS (mode, GEN_INT (val),
1736 source)));
1737 return 1;
1739 can_negate = 1;
1741 break;
1743 default:
1744 abort ();
1747 /* If we can do it in one insn get out quickly. */
1748 if (const_ok_for_arm (val)
1749 || (can_negate_initial && const_ok_for_arm (-val))
1750 || (can_invert && const_ok_for_arm (~val)))
1752 if (generate)
1753 emit_constant_insn (cond,
1754 gen_rtx_SET (VOIDmode, target,
1755 (source
1756 ? gen_rtx_fmt_ee (code, mode, source,
1757 GEN_INT (val))
1758 : GEN_INT (val))));
1759 return 1;
1762 /* Calculate a few attributes that may be useful for specific
1763 optimizations. */
1764 for (i = 31; i >= 0; i--)
1766 if ((remainder & (1 << i)) == 0)
1767 clear_sign_bit_copies++;
1768 else
1769 break;
1772 for (i = 31; i >= 0; i--)
1774 if ((remainder & (1 << i)) != 0)
1775 set_sign_bit_copies++;
1776 else
1777 break;
1780 for (i = 0; i <= 31; i++)
1782 if ((remainder & (1 << i)) == 0)
1783 clear_zero_bit_copies++;
1784 else
1785 break;
1788 for (i = 0; i <= 31; i++)
1790 if ((remainder & (1 << i)) != 0)
1791 set_zero_bit_copies++;
1792 else
1793 break;
1796 switch (code)
1798 case SET:
1799 /* See if we can do this by sign_extending a constant that is known
1800 to be negative. This is a good, way of doing it, since the shift
1801 may well merge into a subsequent insn. */
1802 if (set_sign_bit_copies > 1)
1804 if (const_ok_for_arm
1805 (temp1 = ARM_SIGN_EXTEND (remainder
1806 << (set_sign_bit_copies - 1))))
1808 if (generate)
1810 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1811 emit_constant_insn (cond,
1812 gen_rtx_SET (VOIDmode, new_src,
1813 GEN_INT (temp1)));
1814 emit_constant_insn (cond,
1815 gen_ashrsi3 (target, new_src,
1816 GEN_INT (set_sign_bit_copies - 1)));
1818 return 2;
1820 /* For an inverted constant, we will need to set the low bits,
1821 these will be shifted out of harm's way. */
1822 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1823 if (const_ok_for_arm (~temp1))
1825 if (generate)
1827 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1828 emit_constant_insn (cond,
1829 gen_rtx_SET (VOIDmode, new_src,
1830 GEN_INT (temp1)));
1831 emit_constant_insn (cond,
1832 gen_ashrsi3 (target, new_src,
1833 GEN_INT (set_sign_bit_copies - 1)));
1835 return 2;
1839 /* See if we can generate this by setting the bottom (or the top)
1840 16 bits, and then shifting these into the other half of the
1841 word. We only look for the simplest cases, to do more would cost
1842 too much. Be careful, however, not to generate this when the
1843 alternative would take fewer insns. */
1844 if (val & 0xffff0000)
1846 temp1 = remainder & 0xffff0000;
1847 temp2 = remainder & 0x0000ffff;
1849 /* Overlaps outside this range are best done using other methods. */
1850 for (i = 9; i < 24; i++)
1852 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1853 && !const_ok_for_arm (temp2))
1855 rtx new_src = (subtargets
1856 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1857 : target);
1858 insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1859 source, subtargets, generate);
1860 source = new_src;
1861 if (generate)
1862 emit_constant_insn
1863 (cond,
1864 gen_rtx_SET
1865 (VOIDmode, target,
1866 gen_rtx_IOR (mode,
1867 gen_rtx_ASHIFT (mode, source,
1868 GEN_INT (i)),
1869 source)));
1870 return insns + 1;
1874 /* Don't duplicate cases already considered. */
1875 for (i = 17; i < 24; i++)
1877 if (((temp1 | (temp1 >> i)) == remainder)
1878 && !const_ok_for_arm (temp1))
1880 rtx new_src = (subtargets
1881 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1882 : target);
1883 insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1884 source, subtargets, generate);
1885 source = new_src;
1886 if (generate)
1887 emit_constant_insn
1888 (cond,
1889 gen_rtx_SET (VOIDmode, target,
1890 gen_rtx_IOR
1891 (mode,
1892 gen_rtx_LSHIFTRT (mode, source,
1893 GEN_INT (i)),
1894 source)));
1895 return insns + 1;
1899 break;
1901 case IOR:
1902 case XOR:
1903 /* If we have IOR or XOR, and the constant can be loaded in a
1904 single instruction, and we can find a temporary to put it in,
1905 then this can be done in two instructions instead of 3-4. */
1906 if (subtargets
1907 /* TARGET can't be NULL if SUBTARGETS is 0 */
1908 || (reload_completed && !reg_mentioned_p (target, source)))
1910 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1912 if (generate)
1914 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1916 emit_constant_insn (cond,
1917 gen_rtx_SET (VOIDmode, sub,
1918 GEN_INT (val)));
1919 emit_constant_insn (cond,
1920 gen_rtx_SET (VOIDmode, target,
1921 gen_rtx_fmt_ee (code, mode,
1922 source, sub)));
1924 return 2;
1928 if (code == XOR)
1929 break;
1931 if (set_sign_bit_copies > 8
1932 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1934 if (generate)
1936 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1937 rtx shift = GEN_INT (set_sign_bit_copies);
1939 emit_constant_insn
1940 (cond,
1941 gen_rtx_SET (VOIDmode, sub,
1942 gen_rtx_NOT (mode,
1943 gen_rtx_ASHIFT (mode,
1944 source,
1945 shift))));
1946 emit_constant_insn
1947 (cond,
1948 gen_rtx_SET (VOIDmode, target,
1949 gen_rtx_NOT (mode,
1950 gen_rtx_LSHIFTRT (mode, sub,
1951 shift))));
1953 return 2;
1956 if (set_zero_bit_copies > 8
1957 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1959 if (generate)
1961 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1962 rtx shift = GEN_INT (set_zero_bit_copies);
1964 emit_constant_insn
1965 (cond,
1966 gen_rtx_SET (VOIDmode, sub,
1967 gen_rtx_NOT (mode,
1968 gen_rtx_LSHIFTRT (mode,
1969 source,
1970 shift))));
1971 emit_constant_insn
1972 (cond,
1973 gen_rtx_SET (VOIDmode, target,
1974 gen_rtx_NOT (mode,
1975 gen_rtx_ASHIFT (mode, sub,
1976 shift))));
1978 return 2;
1981 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1983 if (generate)
1985 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1986 emit_constant_insn (cond,
1987 gen_rtx_SET (VOIDmode, sub,
1988 gen_rtx_NOT (mode, source)));
1989 source = sub;
1990 if (subtargets)
1991 sub = gen_reg_rtx (mode);
1992 emit_constant_insn (cond,
1993 gen_rtx_SET (VOIDmode, sub,
1994 gen_rtx_AND (mode, source,
1995 GEN_INT (temp1))));
1996 emit_constant_insn (cond,
1997 gen_rtx_SET (VOIDmode, target,
1998 gen_rtx_NOT (mode, sub)));
2000 return 3;
2002 break;
2004 case AND:
2005 /* See if two shifts will do 2 or more insn's worth of work. */
2006 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2008 HOST_WIDE_INT shift_mask = ((0xffffffff
2009 << (32 - clear_sign_bit_copies))
2010 & 0xffffffff);
2012 if ((remainder | shift_mask) != 0xffffffff)
2014 if (generate)
2016 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2017 insns = arm_gen_constant (AND, mode, cond,
2018 remainder | shift_mask,
2019 new_src, source, subtargets, 1);
2020 source = new_src;
2022 else
2024 rtx targ = subtargets ? NULL_RTX : target;
2025 insns = arm_gen_constant (AND, mode, cond,
2026 remainder | shift_mask,
2027 targ, source, subtargets, 0);
2031 if (generate)
2033 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2034 rtx shift = GEN_INT (clear_sign_bit_copies);
2036 emit_insn (gen_ashlsi3 (new_src, source, shift));
2037 emit_insn (gen_lshrsi3 (target, new_src, shift));
2040 return insns + 2;
2043 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2045 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2047 if ((remainder | shift_mask) != 0xffffffff)
2049 if (generate)
2051 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2053 insns = arm_gen_constant (AND, mode, cond,
2054 remainder | shift_mask,
2055 new_src, source, subtargets, 1);
2056 source = new_src;
2058 else
2060 rtx targ = subtargets ? NULL_RTX : target;
2062 insns = arm_gen_constant (AND, mode, cond,
2063 remainder | shift_mask,
2064 targ, source, subtargets, 0);
2068 if (generate)
2070 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2071 rtx shift = GEN_INT (clear_zero_bit_copies);
2073 emit_insn (gen_lshrsi3 (new_src, source, shift));
2074 emit_insn (gen_ashlsi3 (target, new_src, shift));
2077 return insns + 2;
2080 break;
2082 default:
2083 break;
2086 for (i = 0; i < 32; i++)
2087 if (remainder & (1 << i))
2088 num_bits_set++;
2090 if (code == AND || (can_invert && num_bits_set > 16))
2091 remainder = (~remainder) & 0xffffffff;
2092 else if (code == PLUS && num_bits_set > 16)
2093 remainder = (-remainder) & 0xffffffff;
2094 else
2096 can_invert = 0;
2097 can_negate = 0;
2100 /* Now try and find a way of doing the job in either two or three
2101 instructions.
2102 We start by looking for the largest block of zeros that are aligned on
2103 a 2-bit boundary, we then fill up the temps, wrapping around to the
2104 top of the word when we drop off the bottom.
2105 In the worst case this code should produce no more than four insns. */
2107 int best_start = 0;
2108 int best_consecutive_zeros = 0;
2110 for (i = 0; i < 32; i += 2)
2112 int consecutive_zeros = 0;
2114 if (!(remainder & (3 << i)))
2116 while ((i < 32) && !(remainder & (3 << i)))
2118 consecutive_zeros += 2;
2119 i += 2;
2121 if (consecutive_zeros > best_consecutive_zeros)
2123 best_consecutive_zeros = consecutive_zeros;
2124 best_start = i - consecutive_zeros;
2126 i -= 2;
2130 /* So long as it won't require any more insns to do so, it's
2131 desirable to emit a small constant (in bits 0...9) in the last
2132 insn. This way there is more chance that it can be combined with
2133 a later addressing insn to form a pre-indexed load or store
2134 operation. Consider:
2136 *((volatile int *)0xe0000100) = 1;
2137 *((volatile int *)0xe0000110) = 2;
2139 We want this to wind up as:
2141 mov rA, #0xe0000000
2142 mov rB, #1
2143 str rB, [rA, #0x100]
2144 mov rB, #2
2145 str rB, [rA, #0x110]
2147 rather than having to synthesize both large constants from scratch.
2149 Therefore, we calculate how many insns would be required to emit
2150 the constant starting from `best_start', and also starting from
2151 zero (i.e. with bit 31 first to be output). If `best_start' doesn't
2152 yield a shorter sequence, we may as well use zero. */
2153 if (best_start != 0
2154 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2155 && (count_insns_for_constant (remainder, 0) <=
2156 count_insns_for_constant (remainder, best_start)))
2157 best_start = 0;
2159 /* Now start emitting the insns. */
2160 i = best_start;
2163 int end;
2165 if (i <= 0)
2166 i += 32;
2167 if (remainder & (3 << (i - 2)))
2169 end = i - 8;
2170 if (end < 0)
2171 end += 32;
2172 temp1 = remainder & ((0x0ff << end)
2173 | ((i < end) ? (0xff >> (32 - end)) : 0));
2174 remainder &= ~temp1;
2176 if (generate)
2178 rtx new_src, temp1_rtx;
2180 if (code == SET || code == MINUS)
2182 new_src = (subtargets ? gen_reg_rtx (mode) : target);
2183 if (can_invert && code != MINUS)
2184 temp1 = ~temp1;
2186 else
2188 if (remainder && subtargets)
2189 new_src = gen_reg_rtx (mode);
2190 else
2191 new_src = target;
2192 if (can_invert)
2193 temp1 = ~temp1;
2194 else if (can_negate)
2195 temp1 = -temp1;
2198 temp1 = trunc_int_for_mode (temp1, mode);
2199 temp1_rtx = GEN_INT (temp1);
2201 if (code == SET)
2203 else if (code == MINUS)
2204 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2205 else
2206 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2208 emit_constant_insn (cond,
2209 gen_rtx_SET (VOIDmode, new_src,
2210 temp1_rtx));
2211 source = new_src;
2214 if (code == SET)
2216 can_invert = 0;
2217 code = PLUS;
2219 else if (code == MINUS)
2220 code = PLUS;
2222 insns++;
2223 i -= 6;
2225 i -= 2;
2227 while (remainder);
2230 return insns;
2233 /* Canonicalize a comparison so that we are more likely to recognize it.
2234 This can be done for a few constant compares, where we can make the
2235 immediate value easier to load. */
2237 enum rtx_code
2238 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2240 unsigned HOST_WIDE_INT i = INTVAL (*op1);
2242 switch (code)
2244 case EQ:
2245 case NE:
2246 return code;
2248 case GT:
2249 case LE:
2250 if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2251 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2253 *op1 = GEN_INT (i + 1);
2254 return code == GT ? GE : LT;
2256 break;
2258 case GE:
2259 case LT:
2260 if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2261 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2263 *op1 = GEN_INT (i - 1);
2264 return code == GE ? GT : LE;
2266 break;
2268 case GTU:
2269 case LEU:
2270 if (i != ~((unsigned HOST_WIDE_INT) 0)
2271 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2273 *op1 = GEN_INT (i + 1);
2274 return code == GTU ? GEU : LTU;
2276 break;
2278 case GEU:
2279 case LTU:
2280 if (i != 0
2281 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2283 *op1 = GEN_INT (i - 1);
2284 return code == GEU ? GTU : LEU;
2286 break;
2288 default:
2289 abort ();
2292 return code;
2296 /* Define how to find the value returned by a function. */
2299 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2301 enum machine_mode mode;
2302 int unsignedp ATTRIBUTE_UNUSED;
2303 rtx r ATTRIBUTE_UNUSED;
2306 mode = TYPE_MODE (type);
2307 /* Promote integer types. */
2308 if (INTEGRAL_TYPE_P (type))
2309 PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2310 return LIBCALL_VALUE(mode);
2313 /* Determine the amount of memory needed to store the possible return
2314 registers of an untyped call. */
2316 arm_apply_result_size (void)
2318 int size = 16;
2320 if (TARGET_ARM)
2322 if (TARGET_HARD_FLOAT_ABI)
2324 if (TARGET_FPA)
2325 size += 12;
2326 if (TARGET_MAVERICK)
2327 size += 8;
2329 if (TARGET_IWMMXT_ABI)
2330 size += 8;
2333 return size;
2336 /* Decide whether a type should be returned in memory (true)
2337 or in a register (false). This is called by the macro
2338 RETURN_IN_MEMORY. */
2340 arm_return_in_memory (tree type)
2342 HOST_WIDE_INT size;
2344 if (!AGGREGATE_TYPE_P (type) &&
2345 !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2346 /* All simple types are returned in registers.
2347 For AAPCS, complex types are treated the same as aggregates. */
2348 return 0;
2350 size = int_size_in_bytes (type);
2352 if (arm_abi != ARM_ABI_APCS)
2354 /* ATPCS and later return aggregate types in memory only if they are
2355 larger than a word (or are variable size). */
2356 return (size < 0 || size > UNITS_PER_WORD);
2359 /* For the arm-wince targets we choose to be compatible with Microsoft's
2360 ARM and Thumb compilers, which always return aggregates in memory. */
2361 #ifndef ARM_WINCE
2362 /* All structures/unions bigger than one word are returned in memory.
2363 Also catch the case where int_size_in_bytes returns -1. In this case
2364 the aggregate is either huge or of variable size, and in either case
2365 we will want to return it via memory and not in a register. */
2366 if (size < 0 || size > UNITS_PER_WORD)
2367 return 1;
2369 if (TREE_CODE (type) == RECORD_TYPE)
2371 tree field;
2373 /* For a struct the APCS says that we only return in a register
2374 if the type is 'integer like' and every addressable element
2375 has an offset of zero. For practical purposes this means
2376 that the structure can have at most one non bit-field element
2377 and that this element must be the first one in the structure. */
2379 /* Find the first field, ignoring non FIELD_DECL things which will
2380 have been created by C++. */
2381 for (field = TYPE_FIELDS (type);
2382 field && TREE_CODE (field) != FIELD_DECL;
2383 field = TREE_CHAIN (field))
2384 continue;
2386 if (field == NULL)
2387 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
2389 /* Check that the first field is valid for returning in a register. */
2391 /* ... Floats are not allowed */
2392 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2393 return 1;
2395 /* ... Aggregates that are not themselves valid for returning in
2396 a register are not allowed. */
2397 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2398 return 1;
2400 /* Now check the remaining fields, if any. Only bitfields are allowed,
2401 since they are not addressable. */
2402 for (field = TREE_CHAIN (field);
2403 field;
2404 field = TREE_CHAIN (field))
2406 if (TREE_CODE (field) != FIELD_DECL)
2407 continue;
2409 if (!DECL_BIT_FIELD_TYPE (field))
2410 return 1;
2413 return 0;
2416 if (TREE_CODE (type) == UNION_TYPE)
2418 tree field;
2420 /* Unions can be returned in registers if every element is
2421 integral, or can be returned in an integer register. */
2422 for (field = TYPE_FIELDS (type);
2423 field;
2424 field = TREE_CHAIN (field))
2426 if (TREE_CODE (field) != FIELD_DECL)
2427 continue;
2429 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2430 return 1;
2432 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2433 return 1;
2436 return 0;
2438 #endif /* not ARM_WINCE */
2440 /* Return all other types in memory. */
2441 return 1;
2444 /* Indicate whether or not words of a double are in big-endian order. */
2447 arm_float_words_big_endian (void)
2449 if (TARGET_MAVERICK)
2450 return 0;
2452 /* For FPA, float words are always big-endian. For VFP, floats words
2453 follow the memory system mode. */
2455 if (TARGET_FPA)
2457 return 1;
2460 if (TARGET_VFP)
2461 return (TARGET_BIG_END ? 1 : 0);
2463 return 1;
2466 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2467 for a call to a function whose data type is FNTYPE.
2468 For a library call, FNTYPE is NULL. */
2469 void
2470 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2471 rtx libname ATTRIBUTE_UNUSED,
2472 tree fndecl ATTRIBUTE_UNUSED)
2474 /* On the ARM, the offset starts at 0. */
2475 pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2476 pcum->iwmmxt_nregs = 0;
2477 pcum->can_split = true;
2479 pcum->call_cookie = CALL_NORMAL;
2481 if (TARGET_LONG_CALLS)
2482 pcum->call_cookie = CALL_LONG;
2484 /* Check for long call/short call attributes. The attributes
2485 override any command line option. */
2486 if (fntype)
2488 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2489 pcum->call_cookie = CALL_SHORT;
2490 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2491 pcum->call_cookie = CALL_LONG;
2494 /* Varargs vectors are treated the same as long long.
2495 named_count avoids having to change the way arm handles 'named' */
2496 pcum->named_count = 0;
2497 pcum->nargs = 0;
2499 if (TARGET_REALLY_IWMMXT && fntype)
2501 tree fn_arg;
2503 for (fn_arg = TYPE_ARG_TYPES (fntype);
2504 fn_arg;
2505 fn_arg = TREE_CHAIN (fn_arg))
2506 pcum->named_count += 1;
2508 if (! pcum->named_count)
2509 pcum->named_count = INT_MAX;
2514 /* Return true if mode/type need doubleword alignment. */
2515 bool
2516 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2518 return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2519 || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2523 /* Determine where to put an argument to a function.
2524 Value is zero to push the argument on the stack,
2525 or a hard register in which to store the argument.
2527 MODE is the argument's machine mode.
2528 TYPE is the data type of the argument (as a tree).
2529 This is null for libcalls where that information may
2530 not be available.
2531 CUM is a variable of type CUMULATIVE_ARGS which gives info about
2532 the preceding args and about the function being called.
2533 NAMED is nonzero if this argument is a named parameter
2534 (otherwise it is an extra parameter matching an ellipsis). */
2537 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2538 tree type, int named)
2540 int nregs;
2542 /* Varargs vectors are treated the same as long long.
2543 named_count avoids having to change the way arm handles 'named' */
2544 if (TARGET_IWMMXT_ABI
2545 && arm_vector_mode_supported_p (mode)
2546 && pcum->named_count > pcum->nargs + 1)
2548 if (pcum->iwmmxt_nregs <= 9)
2549 return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2550 else
2552 pcum->can_split = false;
2553 return NULL_RTX;
2557 /* Put doubleword aligned quantities in even register pairs. */
2558 if (pcum->nregs & 1
2559 && ARM_DOUBLEWORD_ALIGN
2560 && arm_needs_doubleword_align (mode, type))
2561 pcum->nregs++;
2563 if (mode == VOIDmode)
2564 /* Compute operand 2 of the call insn. */
2565 return GEN_INT (pcum->call_cookie);
2567 /* Only allow splitting an arg between regs and memory if all preceding
2568 args were allocated to regs. For args passed by reference we only count
2569 the reference pointer. */
2570 if (pcum->can_split)
2571 nregs = 1;
2572 else
2573 nregs = ARM_NUM_REGS2 (mode, type);
2575 if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2576 return NULL_RTX;
2578 return gen_rtx_REG (mode, pcum->nregs);
2581 static int
2582 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2583 tree type, bool named ATTRIBUTE_UNUSED)
2585 int nregs = pcum->nregs;
2587 if (arm_vector_mode_supported_p (mode))
2588 return 0;
2590 if (NUM_ARG_REGS > nregs
2591 && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2592 && pcum->can_split)
2593 return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2595 return 0;
2598 /* Variable sized types are passed by reference. This is a GCC
2599 extension to the ARM ABI. */
2601 static bool
2602 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2603 enum machine_mode mode ATTRIBUTE_UNUSED,
2604 tree type, bool named ATTRIBUTE_UNUSED)
2606 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2609 /* Encode the current state of the #pragma [no_]long_calls. */
2610 typedef enum
2612 OFF, /* No #pramgma [no_]long_calls is in effect. */
2613 LONG, /* #pragma long_calls is in effect. */
2614 SHORT /* #pragma no_long_calls is in effect. */
2615 } arm_pragma_enum;
2617 static arm_pragma_enum arm_pragma_long_calls = OFF;
2619 void
2620 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2622 arm_pragma_long_calls = LONG;
2625 void
2626 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2628 arm_pragma_long_calls = SHORT;
2631 void
2632 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2634 arm_pragma_long_calls = OFF;
2637 /* Table of machine attributes. */
2638 const struct attribute_spec arm_attribute_table[] =
2640 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2641 /* Function calls made to this symbol must be done indirectly, because
2642 it may lie outside of the 26 bit addressing range of a normal function
2643 call. */
2644 { "long_call", 0, 0, false, true, true, NULL },
2645 /* Whereas these functions are always known to reside within the 26 bit
2646 addressing range. */
2647 { "short_call", 0, 0, false, true, true, NULL },
2648 /* Interrupt Service Routines have special prologue and epilogue requirements. */
2649 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
2650 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
2651 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2652 #ifdef ARM_PE
2653 /* ARM/PE has three new attributes:
2654 interfacearm - ?
2655 dllexport - for exporting a function/variable that will live in a dll
2656 dllimport - for importing a function/variable from a dll
2658 Microsoft allows multiple declspecs in one __declspec, separating
2659 them with spaces. We do NOT support this. Instead, use __declspec
2660 multiple times.
2662 { "dllimport", 0, 0, true, false, false, NULL },
2663 { "dllexport", 0, 0, true, false, false, NULL },
2664 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2665 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2666 { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
2667 { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
2668 { "notshared", 0, 0, false, true, false, arm_handle_notshared_attribute },
2669 #endif
2670 { NULL, 0, 0, false, false, false, NULL }
2673 /* Handle an attribute requiring a FUNCTION_DECL;
2674 arguments as in struct attribute_spec.handler. */
2675 static tree
2676 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2677 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2679 if (TREE_CODE (*node) != FUNCTION_DECL)
2681 warning (0, "%qs attribute only applies to functions",
2682 IDENTIFIER_POINTER (name));
2683 *no_add_attrs = true;
2686 return NULL_TREE;
2689 /* Handle an "interrupt" or "isr" attribute;
2690 arguments as in struct attribute_spec.handler. */
2691 static tree
2692 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2693 bool *no_add_attrs)
2695 if (DECL_P (*node))
2697 if (TREE_CODE (*node) != FUNCTION_DECL)
2699 warning (0, "%qs attribute only applies to functions",
2700 IDENTIFIER_POINTER (name));
2701 *no_add_attrs = true;
2703 /* FIXME: the argument if any is checked for type attributes;
2704 should it be checked for decl ones? */
2706 else
2708 if (TREE_CODE (*node) == FUNCTION_TYPE
2709 || TREE_CODE (*node) == METHOD_TYPE)
2711 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2713 warning (0, "%qs attribute ignored", IDENTIFIER_POINTER (name));
2714 *no_add_attrs = true;
2717 else if (TREE_CODE (*node) == POINTER_TYPE
2718 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2719 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2720 && arm_isr_value (args) != ARM_FT_UNKNOWN)
2722 *node = build_variant_type_copy (*node);
2723 TREE_TYPE (*node) = build_type_attribute_variant
2724 (TREE_TYPE (*node),
2725 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2726 *no_add_attrs = true;
2728 else
2730 /* Possibly pass this attribute on from the type to a decl. */
2731 if (flags & ((int) ATTR_FLAG_DECL_NEXT
2732 | (int) ATTR_FLAG_FUNCTION_NEXT
2733 | (int) ATTR_FLAG_ARRAY_NEXT))
2735 *no_add_attrs = true;
2736 return tree_cons (name, args, NULL_TREE);
2738 else
2740 warning (0, "%qs attribute ignored", IDENTIFIER_POINTER (name));
2745 return NULL_TREE;
2748 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2749 /* Handle the "notshared" attribute. This attribute is another way of
2750 requesting hidden visibility. ARM's compiler supports
2751 "__declspec(notshared)"; we support the same thing via an
2752 attribute. */
2754 static tree
2755 arm_handle_notshared_attribute (tree *node,
2756 tree name ATTRIBUTE_UNUSED,
2757 tree args ATTRIBUTE_UNUSED,
2758 int flags ATTRIBUTE_UNUSED,
2759 bool *no_add_attrs)
2761 tree decl = TYPE_NAME (*node);
2763 if (decl)
2765 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2766 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2767 *no_add_attrs = false;
2769 return NULL_TREE;
2771 #endif
2773 /* Return 0 if the attributes for two types are incompatible, 1 if they
2774 are compatible, and 2 if they are nearly compatible (which causes a
2775 warning to be generated). */
2776 static int
2777 arm_comp_type_attributes (tree type1, tree type2)
2779 int l1, l2, s1, s2;
2781 /* Check for mismatch of non-default calling convention. */
2782 if (TREE_CODE (type1) != FUNCTION_TYPE)
2783 return 1;
2785 /* Check for mismatched call attributes. */
2786 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2787 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2788 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2789 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2791 /* Only bother to check if an attribute is defined. */
2792 if (l1 | l2 | s1 | s2)
2794 /* If one type has an attribute, the other must have the same attribute. */
2795 if ((l1 != l2) || (s1 != s2))
2796 return 0;
2798 /* Disallow mixed attributes. */
2799 if ((l1 & s2) || (l2 & s1))
2800 return 0;
2803 /* Check for mismatched ISR attribute. */
2804 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2805 if (! l1)
2806 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2807 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2808 if (! l2)
2809 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2810 if (l1 != l2)
2811 return 0;
2813 return 1;
2816 /* Encode long_call or short_call attribute by prefixing
2817 symbol name in DECL with a special character FLAG. */
2818 void
2819 arm_encode_call_attribute (tree decl, int flag)
2821 const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2822 int len = strlen (str);
2823 char * newstr;
2825 /* Do not allow weak functions to be treated as short call. */
2826 if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2827 return;
2829 newstr = alloca (len + 2);
2830 newstr[0] = flag;
2831 strcpy (newstr + 1, str);
2833 newstr = (char *) ggc_alloc_string (newstr, len + 1);
2834 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2837 /* Assigns default attributes to newly defined type. This is used to
2838 set short_call/long_call attributes for function types of
2839 functions defined inside corresponding #pragma scopes. */
2840 static void
2841 arm_set_default_type_attributes (tree type)
2843 /* Add __attribute__ ((long_call)) to all functions, when
2844 inside #pragma long_calls or __attribute__ ((short_call)),
2845 when inside #pragma no_long_calls. */
2846 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2848 tree type_attr_list, attr_name;
2849 type_attr_list = TYPE_ATTRIBUTES (type);
2851 if (arm_pragma_long_calls == LONG)
2852 attr_name = get_identifier ("long_call");
2853 else if (arm_pragma_long_calls == SHORT)
2854 attr_name = get_identifier ("short_call");
2855 else
2856 return;
2858 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2859 TYPE_ATTRIBUTES (type) = type_attr_list;
2863 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2864 defined within the current compilation unit. If this cannot be
2865 determined, then 0 is returned. */
2866 static int
2867 current_file_function_operand (rtx sym_ref)
2869 /* This is a bit of a fib. A function will have a short call flag
2870 applied to its name if it has the short call attribute, or it has
2871 already been defined within the current compilation unit. */
2872 if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2873 return 1;
2875 /* The current function is always defined within the current compilation
2876 unit. If it s a weak definition however, then this may not be the real
2877 definition of the function, and so we have to say no. */
2878 if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2879 && !DECL_WEAK (current_function_decl))
2880 return 1;
2882 /* We cannot make the determination - default to returning 0. */
2883 return 0;
2886 /* Return nonzero if a 32 bit "long_call" should be generated for
2887 this call. We generate a long_call if the function:
2889 a. has an __attribute__((long call))
2890 or b. is within the scope of a #pragma long_calls
2891 or c. the -mlong-calls command line switch has been specified
2892 . and either:
2893 1. -ffunction-sections is in effect
2894 or 2. the current function has __attribute__ ((section))
2895 or 3. the target function has __attribute__ ((section))
2897 However we do not generate a long call if the function:
2899 d. has an __attribute__ ((short_call))
2900 or e. is inside the scope of a #pragma no_long_calls
2901 or f. is defined within the current compilation unit.
2903 This function will be called by C fragments contained in the machine
2904 description file. SYM_REF and CALL_COOKIE correspond to the matched
2905 rtl operands. CALL_SYMBOL is used to distinguish between
2906 two different callers of the function. It is set to 1 in the
2907 "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2908 and "call_value" patterns. This is because of the difference in the
2909 SYM_REFs passed by these patterns. */
2911 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2913 if (!call_symbol)
2915 if (GET_CODE (sym_ref) != MEM)
2916 return 0;
2918 sym_ref = XEXP (sym_ref, 0);
2921 if (GET_CODE (sym_ref) != SYMBOL_REF)
2922 return 0;
2924 if (call_cookie & CALL_SHORT)
2925 return 0;
2927 if (TARGET_LONG_CALLS)
2929 if (flag_function_sections
2930 || DECL_SECTION_NAME (current_function_decl))
2931 /* c.3 is handled by the definition of the
2932 ARM_DECLARE_FUNCTION_SIZE macro. */
2933 return 1;
2936 if (current_file_function_operand (sym_ref))
2937 return 0;
2939 return (call_cookie & CALL_LONG)
2940 || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2941 || TARGET_LONG_CALLS;
2944 /* Return nonzero if it is ok to make a tail-call to DECL. */
2945 static bool
2946 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
2948 int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2950 if (cfun->machine->sibcall_blocked)
2951 return false;
2953 /* Never tailcall something for which we have no decl, or if we
2954 are in Thumb mode. */
2955 if (decl == NULL || TARGET_THUMB)
2956 return false;
2958 /* Get the calling method. */
2959 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2960 call_type = CALL_SHORT;
2961 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2962 call_type = CALL_LONG;
2964 /* Cannot tail-call to long calls, since these are out of range of
2965 a branch instruction. However, if not compiling PIC, we know
2966 we can reach the symbol if it is in this compilation unit. */
2967 if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2968 return false;
2970 /* If we are interworking and the function is not declared static
2971 then we can't tail-call it unless we know that it exists in this
2972 compilation unit (since it might be a Thumb routine). */
2973 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2974 return false;
2976 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
2977 if (IS_INTERRUPT (arm_current_func_type ()))
2978 return false;
2980 /* Everything else is ok. */
2981 return true;
2985 /* Addressing mode support functions. */
2987 /* Return nonzero if X is a legitimate immediate operand when compiling
2988 for PIC. */
2990 legitimate_pic_operand_p (rtx x)
2992 if (CONSTANT_P (x)
2993 && flag_pic
2994 && (GET_CODE (x) == SYMBOL_REF
2995 || (GET_CODE (x) == CONST
2996 && GET_CODE (XEXP (x, 0)) == PLUS
2997 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2998 return 0;
3000 return 1;
3004 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3006 if (GET_CODE (orig) == SYMBOL_REF
3007 || GET_CODE (orig) == LABEL_REF)
3009 #ifndef AOF_ASSEMBLER
3010 rtx pic_ref, address;
3011 #endif
3012 rtx insn;
3013 int subregs = 0;
3015 if (reg == 0)
3017 if (no_new_pseudos)
3018 abort ();
3019 else
3020 reg = gen_reg_rtx (Pmode);
3022 subregs = 1;
3025 #ifdef AOF_ASSEMBLER
3026 /* The AOF assembler can generate relocations for these directly, and
3027 understands that the PIC register has to be added into the offset. */
3028 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3029 #else
3030 if (subregs)
3031 address = gen_reg_rtx (Pmode);
3032 else
3033 address = reg;
3035 if (TARGET_ARM)
3036 emit_insn (gen_pic_load_addr_arm (address, orig));
3037 else
3038 emit_insn (gen_pic_load_addr_thumb (address, orig));
3040 if ((GET_CODE (orig) == LABEL_REF
3041 || (GET_CODE (orig) == SYMBOL_REF &&
3042 SYMBOL_REF_LOCAL_P (orig)))
3043 && NEED_GOT_RELOC)
3044 pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
3045 else
3047 pic_ref = gen_const_mem (Pmode,
3048 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3049 address));
3052 insn = emit_move_insn (reg, pic_ref);
3053 #endif
3054 current_function_uses_pic_offset_table = 1;
3055 /* Put a REG_EQUAL note on this insn, so that it can be optimized
3056 by loop. */
3057 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3058 REG_NOTES (insn));
3059 return reg;
3061 else if (GET_CODE (orig) == CONST)
3063 rtx base, offset;
3065 if (GET_CODE (XEXP (orig, 0)) == PLUS
3066 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3067 return orig;
3069 if (reg == 0)
3071 if (no_new_pseudos)
3072 abort ();
3073 else
3074 reg = gen_reg_rtx (Pmode);
3077 if (GET_CODE (XEXP (orig, 0)) == PLUS)
3079 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3080 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3081 base == reg ? 0 : reg);
3083 else
3084 abort ();
3086 if (GET_CODE (offset) == CONST_INT)
3088 /* The base register doesn't really matter, we only want to
3089 test the index for the appropriate mode. */
3090 if (!arm_legitimate_index_p (mode, offset, SET, 0))
3092 if (!no_new_pseudos)
3093 offset = force_reg (Pmode, offset);
3094 else
3095 abort ();
3098 if (GET_CODE (offset) == CONST_INT)
3099 return plus_constant (base, INTVAL (offset));
3102 if (GET_MODE_SIZE (mode) > 4
3103 && (GET_MODE_CLASS (mode) == MODE_INT
3104 || TARGET_SOFT_FLOAT))
3106 emit_insn (gen_addsi3 (reg, base, offset));
3107 return reg;
3110 return gen_rtx_PLUS (Pmode, base, offset);
3113 return orig;
3117 /* Find a spare low register to use during the prolog of a function. */
3119 static int
3120 thumb_find_work_register (unsigned long pushed_regs_mask)
3122 int reg;
3124 /* Check the argument registers first as these are call-used. The
3125 register allocation order means that sometimes r3 might be used
3126 but earlier argument registers might not, so check them all. */
3127 for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3128 if (!regs_ever_live[reg])
3129 return reg;
3131 /* Before going on to check the call-saved registers we can try a couple
3132 more ways of deducing that r3 is available. The first is when we are
3133 pushing anonymous arguments onto the stack and we have less than 4
3134 registers worth of fixed arguments(*). In this case r3 will be part of
3135 the variable argument list and so we can be sure that it will be
3136 pushed right at the start of the function. Hence it will be available
3137 for the rest of the prologue.
3138 (*): ie current_function_pretend_args_size is greater than 0. */
3139 if (cfun->machine->uses_anonymous_args
3140 && current_function_pretend_args_size > 0)
3141 return LAST_ARG_REGNUM;
3143 /* The other case is when we have fixed arguments but less than 4 registers
3144 worth. In this case r3 might be used in the body of the function, but
3145 it is not being used to convey an argument into the function. In theory
3146 we could just check current_function_args_size to see how many bytes are
3147 being passed in argument registers, but it seems that it is unreliable.
3148 Sometimes it will have the value 0 when in fact arguments are being
3149 passed. (See testcase execute/20021111-1.c for an example). So we also
3150 check the args_info.nregs field as well. The problem with this field is
3151 that it makes no allowances for arguments that are passed to the
3152 function but which are not used. Hence we could miss an opportunity
3153 when a function has an unused argument in r3. But it is better to be
3154 safe than to be sorry. */
3155 if (! cfun->machine->uses_anonymous_args
3156 && current_function_args_size >= 0
3157 && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3158 && cfun->args_info.nregs < 4)
3159 return LAST_ARG_REGNUM;
3161 /* Otherwise look for a call-saved register that is going to be pushed. */
3162 for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3163 if (pushed_regs_mask & (1 << reg))
3164 return reg;
3166 /* Something went wrong - thumb_compute_save_reg_mask()
3167 should have arranged for a suitable register to be pushed. */
3168 abort ();
3172 /* Generate code to load the PIC register. In thumb mode SCRATCH is a
3173 low register. */
3175 void
3176 arm_load_pic_register (unsigned int scratch)
3178 #ifndef AOF_ASSEMBLER
3179 rtx l1, pic_tmp, pic_tmp2, pic_rtx;
3180 rtx global_offset_table;
3182 if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3183 return;
3185 if (!flag_pic)
3186 abort ();
3188 l1 = gen_label_rtx ();
3190 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3191 /* On the ARM the PC register contains 'dot + 8' at the time of the
3192 addition, on the Thumb it is 'dot + 4'. */
3193 pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
3194 if (GOT_PCREL)
3195 pic_tmp2 = gen_rtx_CONST (VOIDmode,
3196 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3197 else
3198 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3200 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3202 if (TARGET_ARM)
3204 emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3205 emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
3207 else
3209 if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3211 /* We will have pushed the pic register, so should always be
3212 able to find a work register. */
3213 pic_tmp = gen_rtx_REG (SImode, scratch);
3214 emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3215 emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3217 else
3218 emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3219 emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
3222 /* Need to emit this whether or not we obey regdecls,
3223 since setjmp/longjmp can cause life info to screw up. */
3224 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3225 #endif /* AOF_ASSEMBLER */
3229 /* Return nonzero if X is valid as an ARM state addressing register. */
3230 static int
3231 arm_address_register_rtx_p (rtx x, int strict_p)
3233 int regno;
3235 if (GET_CODE (x) != REG)
3236 return 0;
3238 regno = REGNO (x);
3240 if (strict_p)
3241 return ARM_REGNO_OK_FOR_BASE_P (regno);
3243 return (regno <= LAST_ARM_REGNUM
3244 || regno >= FIRST_PSEUDO_REGISTER
3245 || regno == FRAME_POINTER_REGNUM
3246 || regno == ARG_POINTER_REGNUM);
3249 /* Return nonzero if X is a valid ARM state address operand. */
3251 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3252 int strict_p)
3254 bool use_ldrd;
3255 enum rtx_code code = GET_CODE (x);
3257 if (arm_address_register_rtx_p (x, strict_p))
3258 return 1;
3260 use_ldrd = (TARGET_LDRD
3261 && (mode == DImode
3262 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3264 if (code == POST_INC || code == PRE_DEC
3265 || ((code == PRE_INC || code == POST_DEC)
3266 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3267 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3269 else if ((code == POST_MODIFY || code == PRE_MODIFY)
3270 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3271 && GET_CODE (XEXP (x, 1)) == PLUS
3272 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3274 rtx addend = XEXP (XEXP (x, 1), 1);
3276 /* Don't allow ldrd post increment by register because it's hard
3277 to fixup invalid register choices. */
3278 if (use_ldrd
3279 && GET_CODE (x) == POST_MODIFY
3280 && GET_CODE (addend) == REG)
3281 return 0;
3283 return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3284 && arm_legitimate_index_p (mode, addend, outer, strict_p));
3287 /* After reload constants split into minipools will have addresses
3288 from a LABEL_REF. */
3289 else if (reload_completed
3290 && (code == LABEL_REF
3291 || (code == CONST
3292 && GET_CODE (XEXP (x, 0)) == PLUS
3293 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3294 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3295 return 1;
3297 else if (mode == TImode)
3298 return 0;
3300 else if (code == PLUS)
3302 rtx xop0 = XEXP (x, 0);
3303 rtx xop1 = XEXP (x, 1);
3305 return ((arm_address_register_rtx_p (xop0, strict_p)
3306 && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3307 || (arm_address_register_rtx_p (xop1, strict_p)
3308 && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3311 #if 0
3312 /* Reload currently can't handle MINUS, so disable this for now */
3313 else if (GET_CODE (x) == MINUS)
3315 rtx xop0 = XEXP (x, 0);
3316 rtx xop1 = XEXP (x, 1);
3318 return (arm_address_register_rtx_p (xop0, strict_p)
3319 && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3321 #endif
3323 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3324 && code == SYMBOL_REF
3325 && CONSTANT_POOL_ADDRESS_P (x)
3326 && ! (flag_pic
3327 && symbol_mentioned_p (get_pool_constant (x))))
3328 return 1;
3330 return 0;
3333 /* Return nonzero if INDEX is valid for an address index operand in
3334 ARM state. */
3335 static int
3336 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3337 int strict_p)
3339 HOST_WIDE_INT range;
3340 enum rtx_code code = GET_CODE (index);
3342 /* Standard coprocessor addressing modes. */
3343 if (TARGET_HARD_FLOAT
3344 && (TARGET_FPA || TARGET_MAVERICK)
3345 && (GET_MODE_CLASS (mode) == MODE_FLOAT
3346 || (TARGET_MAVERICK && mode == DImode)))
3347 return (code == CONST_INT && INTVAL (index) < 1024
3348 && INTVAL (index) > -1024
3349 && (INTVAL (index) & 3) == 0);
3351 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3352 return (code == CONST_INT
3353 && INTVAL (index) < 1024
3354 && INTVAL (index) > -1024
3355 && (INTVAL (index) & 3) == 0);
3357 if (arm_address_register_rtx_p (index, strict_p)
3358 && (GET_MODE_SIZE (mode) <= 4))
3359 return 1;
3361 if (mode == DImode || mode == DFmode)
3363 if (code == CONST_INT)
3365 HOST_WIDE_INT val = INTVAL (index);
3367 if (TARGET_LDRD)
3368 return val > -256 && val < 256;
3369 else
3370 return val > -4096 && val < 4092;
3373 return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3376 if (GET_MODE_SIZE (mode) <= 4
3377 && ! (arm_arch4
3378 && (mode == HImode
3379 || (mode == QImode && outer == SIGN_EXTEND))))
3381 if (code == MULT)
3383 rtx xiop0 = XEXP (index, 0);
3384 rtx xiop1 = XEXP (index, 1);
3386 return ((arm_address_register_rtx_p (xiop0, strict_p)
3387 && power_of_two_operand (xiop1, SImode))
3388 || (arm_address_register_rtx_p (xiop1, strict_p)
3389 && power_of_two_operand (xiop0, SImode)));
3391 else if (code == LSHIFTRT || code == ASHIFTRT
3392 || code == ASHIFT || code == ROTATERT)
3394 rtx op = XEXP (index, 1);
3396 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3397 && GET_CODE (op) == CONST_INT
3398 && INTVAL (op) > 0
3399 && INTVAL (op) <= 31);
3403 /* For ARM v4 we may be doing a sign-extend operation during the
3404 load. */
3405 if (arm_arch4)
3407 if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3408 range = 256;
3409 else
3410 range = 4096;
3412 else
3413 range = (mode == HImode) ? 4095 : 4096;
3415 return (code == CONST_INT
3416 && INTVAL (index) < range
3417 && INTVAL (index) > -range);
3420 /* Return nonzero if X is valid as a Thumb state base register. */
3421 static int
3422 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3424 int regno;
3426 if (GET_CODE (x) != REG)
3427 return 0;
3429 regno = REGNO (x);
3431 if (strict_p)
3432 return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3434 return (regno <= LAST_LO_REGNUM
3435 || regno > LAST_VIRTUAL_REGISTER
3436 || regno == FRAME_POINTER_REGNUM
3437 || (GET_MODE_SIZE (mode) >= 4
3438 && (regno == STACK_POINTER_REGNUM
3439 || regno >= FIRST_PSEUDO_REGISTER
3440 || x == hard_frame_pointer_rtx
3441 || x == arg_pointer_rtx)));
3444 /* Return nonzero if x is a legitimate index register. This is the case
3445 for any base register that can access a QImode object. */
3446 inline static int
3447 thumb_index_register_rtx_p (rtx x, int strict_p)
3449 return thumb_base_register_rtx_p (x, QImode, strict_p);
3452 /* Return nonzero if x is a legitimate Thumb-state address.
3454 The AP may be eliminated to either the SP or the FP, so we use the
3455 least common denominator, e.g. SImode, and offsets from 0 to 64.
3457 ??? Verify whether the above is the right approach.
3459 ??? Also, the FP may be eliminated to the SP, so perhaps that
3460 needs special handling also.
3462 ??? Look at how the mips16 port solves this problem. It probably uses
3463 better ways to solve some of these problems.
3465 Although it is not incorrect, we don't accept QImode and HImode
3466 addresses based on the frame pointer or arg pointer until the
3467 reload pass starts. This is so that eliminating such addresses
3468 into stack based ones won't produce impossible code. */
3470 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3472 /* ??? Not clear if this is right. Experiment. */
3473 if (GET_MODE_SIZE (mode) < 4
3474 && !(reload_in_progress || reload_completed)
3475 && (reg_mentioned_p (frame_pointer_rtx, x)
3476 || reg_mentioned_p (arg_pointer_rtx, x)
3477 || reg_mentioned_p (virtual_incoming_args_rtx, x)
3478 || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3479 || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3480 || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3481 return 0;
3483 /* Accept any base register. SP only in SImode or larger. */
3484 else if (thumb_base_register_rtx_p (x, mode, strict_p))
3485 return 1;
3487 /* This is PC relative data before arm_reorg runs. */
3488 else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3489 && GET_CODE (x) == SYMBOL_REF
3490 && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3491 return 1;
3493 /* This is PC relative data after arm_reorg runs. */
3494 else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3495 && (GET_CODE (x) == LABEL_REF
3496 || (GET_CODE (x) == CONST
3497 && GET_CODE (XEXP (x, 0)) == PLUS
3498 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3499 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3500 return 1;
3502 /* Post-inc indexing only supported for SImode and larger. */
3503 else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3504 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3505 return 1;
3507 else if (GET_CODE (x) == PLUS)
3509 /* REG+REG address can be any two index registers. */
3510 /* We disallow FRAME+REG addressing since we know that FRAME
3511 will be replaced with STACK, and SP relative addressing only
3512 permits SP+OFFSET. */
3513 if (GET_MODE_SIZE (mode) <= 4
3514 && XEXP (x, 0) != frame_pointer_rtx
3515 && XEXP (x, 1) != frame_pointer_rtx
3516 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3517 && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3518 return 1;
3520 /* REG+const has 5-7 bit offset for non-SP registers. */
3521 else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3522 || XEXP (x, 0) == arg_pointer_rtx)
3523 && GET_CODE (XEXP (x, 1)) == CONST_INT
3524 && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3525 return 1;
3527 /* REG+const has 10 bit offset for SP, but only SImode and
3528 larger is supported. */
3529 /* ??? Should probably check for DI/DFmode overflow here
3530 just like GO_IF_LEGITIMATE_OFFSET does. */
3531 else if (GET_CODE (XEXP (x, 0)) == REG
3532 && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3533 && GET_MODE_SIZE (mode) >= 4
3534 && GET_CODE (XEXP (x, 1)) == CONST_INT
3535 && INTVAL (XEXP (x, 1)) >= 0
3536 && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3537 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3538 return 1;
3540 else if (GET_CODE (XEXP (x, 0)) == REG
3541 && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3542 && GET_MODE_SIZE (mode) >= 4
3543 && GET_CODE (XEXP (x, 1)) == CONST_INT
3544 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3545 return 1;
3548 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3549 && GET_MODE_SIZE (mode) == 4
3550 && GET_CODE (x) == SYMBOL_REF
3551 && CONSTANT_POOL_ADDRESS_P (x)
3552 && !(flag_pic
3553 && symbol_mentioned_p (get_pool_constant (x))))
3554 return 1;
3556 return 0;
3559 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3560 instruction of mode MODE. */
3562 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3564 switch (GET_MODE_SIZE (mode))
3566 case 1:
3567 return val >= 0 && val < 32;
3569 case 2:
3570 return val >= 0 && val < 64 && (val & 1) == 0;
3572 default:
3573 return (val >= 0
3574 && (val + GET_MODE_SIZE (mode)) <= 128
3575 && (val & 3) == 0);
3579 /* Try machine-dependent ways of modifying an illegitimate address
3580 to be legitimate. If we find one, return the new, valid address. */
3582 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3584 if (GET_CODE (x) == PLUS)
3586 rtx xop0 = XEXP (x, 0);
3587 rtx xop1 = XEXP (x, 1);
3589 if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3590 xop0 = force_reg (SImode, xop0);
3592 if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3593 xop1 = force_reg (SImode, xop1);
3595 if (ARM_BASE_REGISTER_RTX_P (xop0)
3596 && GET_CODE (xop1) == CONST_INT)
3598 HOST_WIDE_INT n, low_n;
3599 rtx base_reg, val;
3600 n = INTVAL (xop1);
3602 /* VFP addressing modes actually allow greater offsets, but for
3603 now we just stick with the lowest common denominator. */
3604 if (mode == DImode
3605 || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3607 low_n = n & 0x0f;
3608 n &= ~0x0f;
3609 if (low_n > 4)
3611 n += 16;
3612 low_n -= 16;
3615 else
3617 low_n = ((mode) == TImode ? 0
3618 : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3619 n -= low_n;
3622 base_reg = gen_reg_rtx (SImode);
3623 val = force_operand (gen_rtx_PLUS (SImode, xop0,
3624 GEN_INT (n)), NULL_RTX);
3625 emit_move_insn (base_reg, val);
3626 x = (low_n == 0 ? base_reg
3627 : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3629 else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3630 x = gen_rtx_PLUS (SImode, xop0, xop1);
3633 /* XXX We don't allow MINUS any more -- see comment in
3634 arm_legitimate_address_p (). */
3635 else if (GET_CODE (x) == MINUS)
3637 rtx xop0 = XEXP (x, 0);
3638 rtx xop1 = XEXP (x, 1);
3640 if (CONSTANT_P (xop0))
3641 xop0 = force_reg (SImode, xop0);
3643 if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3644 xop1 = force_reg (SImode, xop1);
3646 if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3647 x = gen_rtx_MINUS (SImode, xop0, xop1);
3650 if (flag_pic)
3652 /* We need to find and carefully transform any SYMBOL and LABEL
3653 references; so go back to the original address expression. */
3654 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3656 if (new_x != orig_x)
3657 x = new_x;
3660 return x;
3664 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3665 to be legitimate. If we find one, return the new, valid address. */
3667 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3669 if (GET_CODE (x) == PLUS
3670 && GET_CODE (XEXP (x, 1)) == CONST_INT
3671 && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3672 || INTVAL (XEXP (x, 1)) < 0))
3674 rtx xop0 = XEXP (x, 0);
3675 rtx xop1 = XEXP (x, 1);
3676 HOST_WIDE_INT offset = INTVAL (xop1);
3678 /* Try and fold the offset into a biasing of the base register and
3679 then offsetting that. Don't do this when optimizing for space
3680 since it can cause too many CSEs. */
3681 if (optimize_size && offset >= 0
3682 && offset < 256 + 31 * GET_MODE_SIZE (mode))
3684 HOST_WIDE_INT delta;
3686 if (offset >= 256)
3687 delta = offset - (256 - GET_MODE_SIZE (mode));
3688 else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3689 delta = 31 * GET_MODE_SIZE (mode);
3690 else
3691 delta = offset & (~31 * GET_MODE_SIZE (mode));
3693 xop0 = force_operand (plus_constant (xop0, offset - delta),
3694 NULL_RTX);
3695 x = plus_constant (xop0, delta);
3697 else if (offset < 0 && offset > -256)
3698 /* Small negative offsets are best done with a subtract before the
3699 dereference, forcing these into a register normally takes two
3700 instructions. */
3701 x = force_operand (x, NULL_RTX);
3702 else
3704 /* For the remaining cases, force the constant into a register. */
3705 xop1 = force_reg (SImode, xop1);
3706 x = gen_rtx_PLUS (SImode, xop0, xop1);
3709 else if (GET_CODE (x) == PLUS
3710 && s_register_operand (XEXP (x, 1), SImode)
3711 && !s_register_operand (XEXP (x, 0), SImode))
3713 rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3715 x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3718 if (flag_pic)
3720 /* We need to find and carefully transform any SYMBOL and LABEL
3721 references; so go back to the original address expression. */
3722 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3724 if (new_x != orig_x)
3725 x = new_x;
3728 return x;
3733 #define REG_OR_SUBREG_REG(X) \
3734 (GET_CODE (X) == REG \
3735 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3737 #define REG_OR_SUBREG_RTX(X) \
3738 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3740 #ifndef COSTS_N_INSNS
3741 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3742 #endif
3743 static inline int
3744 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3746 enum machine_mode mode = GET_MODE (x);
3748 switch (code)
3750 case ASHIFT:
3751 case ASHIFTRT:
3752 case LSHIFTRT:
3753 case ROTATERT:
3754 case PLUS:
3755 case MINUS:
3756 case COMPARE:
3757 case NEG:
3758 case NOT:
3759 return COSTS_N_INSNS (1);
3761 case MULT:
3762 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3764 int cycles = 0;
3765 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3767 while (i)
3769 i >>= 2;
3770 cycles++;
3772 return COSTS_N_INSNS (2) + cycles;
3774 return COSTS_N_INSNS (1) + 16;
3776 case SET:
3777 return (COSTS_N_INSNS (1)
3778 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3779 + GET_CODE (SET_DEST (x)) == MEM));
3781 case CONST_INT:
3782 if (outer == SET)
3784 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3785 return 0;
3786 if (thumb_shiftable_const (INTVAL (x)))
3787 return COSTS_N_INSNS (2);
3788 return COSTS_N_INSNS (3);
3790 else if ((outer == PLUS || outer == COMPARE)
3791 && INTVAL (x) < 256 && INTVAL (x) > -256)
3792 return 0;
3793 else if (outer == AND
3794 && INTVAL (x) < 256 && INTVAL (x) >= -256)
3795 return COSTS_N_INSNS (1);
3796 else if (outer == ASHIFT || outer == ASHIFTRT
3797 || outer == LSHIFTRT)
3798 return 0;
3799 return COSTS_N_INSNS (2);
3801 case CONST:
3802 case CONST_DOUBLE:
3803 case LABEL_REF:
3804 case SYMBOL_REF:
3805 return COSTS_N_INSNS (3);
3807 case UDIV:
3808 case UMOD:
3809 case DIV:
3810 case MOD:
3811 return 100;
3813 case TRUNCATE:
3814 return 99;
3816 case AND:
3817 case XOR:
3818 case IOR:
3819 /* XXX guess. */
3820 return 8;
3822 case MEM:
3823 /* XXX another guess. */
3824 /* Memory costs quite a lot for the first word, but subsequent words
3825 load at the equivalent of a single insn each. */
3826 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3827 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3828 ? 4 : 0));
3830 case IF_THEN_ELSE:
3831 /* XXX a guess. */
3832 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3833 return 14;
3834 return 2;
3836 case ZERO_EXTEND:
3837 /* XXX still guessing. */
3838 switch (GET_MODE (XEXP (x, 0)))
3840 case QImode:
3841 return (1 + (mode == DImode ? 4 : 0)
3842 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3844 case HImode:
3845 return (4 + (mode == DImode ? 4 : 0)
3846 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3848 case SImode:
3849 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3851 default:
3852 return 99;
3855 default:
3856 return 99;
3861 /* Worker routine for arm_rtx_costs. */
3862 static inline int
3863 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3865 enum machine_mode mode = GET_MODE (x);
3866 enum rtx_code subcode;
3867 int extra_cost;
3869 switch (code)
3871 case MEM:
3872 /* Memory costs quite a lot for the first word, but subsequent words
3873 load at the equivalent of a single insn each. */
3874 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3875 + (GET_CODE (x) == SYMBOL_REF
3876 && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3878 case DIV:
3879 case MOD:
3880 case UDIV:
3881 case UMOD:
3882 return optimize_size ? COSTS_N_INSNS (2) : 100;
3884 case ROTATE:
3885 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3886 return 4;
3887 /* Fall through */
3888 case ROTATERT:
3889 if (mode != SImode)
3890 return 8;
3891 /* Fall through */
3892 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3893 if (mode == DImode)
3894 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3895 + ((GET_CODE (XEXP (x, 0)) == REG
3896 || (GET_CODE (XEXP (x, 0)) == SUBREG
3897 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3898 ? 0 : 8));
3899 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3900 || (GET_CODE (XEXP (x, 0)) == SUBREG
3901 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3902 ? 0 : 4)
3903 + ((GET_CODE (XEXP (x, 1)) == REG
3904 || (GET_CODE (XEXP (x, 1)) == SUBREG
3905 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3906 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3907 ? 0 : 4));
3909 case MINUS:
3910 if (mode == DImode)
3911 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3912 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3913 || (GET_CODE (XEXP (x, 0)) == CONST_INT
3914 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3915 ? 0 : 8));
3917 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3918 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3919 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3920 && arm_const_double_rtx (XEXP (x, 1))))
3921 ? 0 : 8)
3922 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3923 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3924 && arm_const_double_rtx (XEXP (x, 0))))
3925 ? 0 : 8));
3927 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3928 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3929 && REG_OR_SUBREG_REG (XEXP (x, 1))))
3930 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3931 || subcode == ASHIFTRT || subcode == LSHIFTRT
3932 || subcode == ROTATE || subcode == ROTATERT
3933 || (subcode == MULT
3934 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3935 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3936 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3937 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3938 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3939 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3940 && REG_OR_SUBREG_REG (XEXP (x, 0))))
3941 return 1;
3942 /* Fall through */
3944 case PLUS:
3945 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3946 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3947 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3948 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3949 && arm_const_double_rtx (XEXP (x, 1))))
3950 ? 0 : 8));
3952 /* Fall through */
3953 case AND: case XOR: case IOR:
3954 extra_cost = 0;
3956 /* Normally the frame registers will be spilt into reg+const during
3957 reload, so it is a bad idea to combine them with other instructions,
3958 since then they might not be moved outside of loops. As a compromise
3959 we allow integration with ops that have a constant as their second
3960 operand. */
3961 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3962 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3963 && GET_CODE (XEXP (x, 1)) != CONST_INT)
3964 || (REG_OR_SUBREG_REG (XEXP (x, 0))
3965 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3966 extra_cost = 4;
3968 if (mode == DImode)
3969 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3970 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3971 || (GET_CODE (XEXP (x, 1)) == CONST_INT
3972 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3973 ? 0 : 8));
3975 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3976 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3977 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3978 || (GET_CODE (XEXP (x, 1)) == CONST_INT
3979 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3980 ? 0 : 4));
3982 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3983 return (1 + extra_cost
3984 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3985 || subcode == LSHIFTRT || subcode == ASHIFTRT
3986 || subcode == ROTATE || subcode == ROTATERT
3987 || (subcode == MULT
3988 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3989 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3990 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3991 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3992 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3993 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3994 ? 0 : 4));
3996 return 8;
3998 case MULT:
3999 /* This should have been handled by the CPU specific routines. */
4000 abort ();
4002 case TRUNCATE:
4003 if (arm_arch3m && mode == SImode
4004 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4005 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4006 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
4007 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4008 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4009 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
4010 return 8;
4011 return 99;
4013 case NEG:
4014 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4015 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4016 /* Fall through */
4017 case NOT:
4018 if (mode == DImode)
4019 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4021 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4023 case IF_THEN_ELSE:
4024 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4025 return 14;
4026 return 2;
4028 case COMPARE:
4029 return 1;
4031 case ABS:
4032 return 4 + (mode == DImode ? 4 : 0);
4034 case SIGN_EXTEND:
4035 if (GET_MODE (XEXP (x, 0)) == QImode)
4036 return (4 + (mode == DImode ? 4 : 0)
4037 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4038 /* Fall through */
4039 case ZERO_EXTEND:
4040 switch (GET_MODE (XEXP (x, 0)))
4042 case QImode:
4043 return (1 + (mode == DImode ? 4 : 0)
4044 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4046 case HImode:
4047 return (4 + (mode == DImode ? 4 : 0)
4048 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4050 case SImode:
4051 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4053 case V8QImode:
4054 case V4HImode:
4055 case V2SImode:
4056 case V4QImode:
4057 case V2HImode:
4058 return 1;
4060 default:
4061 break;
4063 abort ();
4065 case CONST_INT:
4066 if (const_ok_for_arm (INTVAL (x)))
4067 return outer == SET ? 2 : -1;
4068 else if (outer == AND
4069 && const_ok_for_arm (~INTVAL (x)))
4070 return -1;
4071 else if ((outer == COMPARE
4072 || outer == PLUS || outer == MINUS)
4073 && const_ok_for_arm (-INTVAL (x)))
4074 return -1;
4075 else
4076 return 5;
4078 case CONST:
4079 case LABEL_REF:
4080 case SYMBOL_REF:
4081 return 6;
4083 case CONST_DOUBLE:
4084 if (arm_const_double_rtx (x))
4085 return outer == SET ? 2 : -1;
4086 else if ((outer == COMPARE || outer == PLUS)
4087 && neg_const_double_rtx_ok_for_fpa (x))
4088 return -1;
4089 return 7;
4091 default:
4092 return 99;
4096 /* RTX costs when optimizing for size. */
4097 static bool
4098 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4100 enum machine_mode mode = GET_MODE (x);
4102 if (TARGET_THUMB)
4104 /* XXX TBD. For now, use the standard costs. */
4105 *total = thumb_rtx_costs (x, code, outer_code);
4106 return true;
4109 switch (code)
4111 case MEM:
4112 /* A memory access costs 1 insn if the mode is small, or the address is
4113 a single register, otherwise it costs one insn per word. */
4114 if (REG_P (XEXP (x, 0)))
4115 *total = COSTS_N_INSNS (1);
4116 else
4117 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4118 return true;
4120 case DIV:
4121 case MOD:
4122 case UDIV:
4123 case UMOD:
4124 /* Needs a libcall, so it costs about this. */
4125 *total = COSTS_N_INSNS (2);
4126 return false;
4128 case ROTATE:
4129 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4131 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4132 return true;
4134 /* Fall through */
4135 case ROTATERT:
4136 case ASHIFT:
4137 case LSHIFTRT:
4138 case ASHIFTRT:
4139 if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4141 *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4142 return true;
4144 else if (mode == SImode)
4146 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4147 /* Slightly disparage register shifts, but not by much. */
4148 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4149 *total += 1 + rtx_cost (XEXP (x, 1), code);
4150 return true;
4153 /* Needs a libcall. */
4154 *total = COSTS_N_INSNS (2);
4155 return false;
4157 case MINUS:
4158 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4160 *total = COSTS_N_INSNS (1);
4161 return false;
4164 if (mode == SImode)
4166 enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4167 enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4169 if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4170 || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4171 || subcode1 == ROTATE || subcode1 == ROTATERT
4172 || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4173 || subcode1 == ASHIFTRT)
4175 /* It's just the cost of the two operands. */
4176 *total = 0;
4177 return false;
4180 *total = COSTS_N_INSNS (1);
4181 return false;
4184 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4185 return false;
4187 case PLUS:
4188 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4190 *total = COSTS_N_INSNS (1);
4191 return false;
4194 /* Fall through */
4195 case AND: case XOR: case IOR:
4196 if (mode == SImode)
4198 enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4200 if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4201 || subcode == LSHIFTRT || subcode == ASHIFTRT
4202 || (code == AND && subcode == NOT))
4204 /* It's just the cost of the two operands. */
4205 *total = 0;
4206 return false;
4210 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4211 return false;
4213 case MULT:
4214 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4215 return false;
4217 case NEG:
4218 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4219 *total = COSTS_N_INSNS (1);
4220 /* Fall through */
4221 case NOT:
4222 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4224 return false;
4226 case IF_THEN_ELSE:
4227 *total = 0;
4228 return false;
4230 case COMPARE:
4231 if (cc_register (XEXP (x, 0), VOIDmode))
4232 * total = 0;
4233 else
4234 *total = COSTS_N_INSNS (1);
4235 return false;
4237 case ABS:
4238 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4239 *total = COSTS_N_INSNS (1);
4240 else
4241 *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4242 return false;
4244 case SIGN_EXTEND:
4245 *total = 0;
4246 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4248 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4249 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4251 if (mode == DImode)
4252 *total += COSTS_N_INSNS (1);
4253 return false;
4255 case ZERO_EXTEND:
4256 *total = 0;
4257 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4259 switch (GET_MODE (XEXP (x, 0)))
4261 case QImode:
4262 *total += COSTS_N_INSNS (1);
4263 break;
4265 case HImode:
4266 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4268 case SImode:
4269 break;
4271 default:
4272 *total += COSTS_N_INSNS (2);
4276 if (mode == DImode)
4277 *total += COSTS_N_INSNS (1);
4279 return false;
4281 case CONST_INT:
4282 if (const_ok_for_arm (INTVAL (x)))
4283 *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4284 else if (const_ok_for_arm (~INTVAL (x)))
4285 *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4286 else if (const_ok_for_arm (-INTVAL (x)))
4288 if (outer_code == COMPARE || outer_code == PLUS
4289 || outer_code == MINUS)
4290 *total = 0;
4291 else
4292 *total = COSTS_N_INSNS (1);
4294 else
4295 *total = COSTS_N_INSNS (2);
4296 return true;
4298 case CONST:
4299 case LABEL_REF:
4300 case SYMBOL_REF:
4301 *total = COSTS_N_INSNS (2);
4302 return true;
4304 case CONST_DOUBLE:
4305 *total = COSTS_N_INSNS (4);
4306 return true;
4308 default:
4309 if (mode != VOIDmode)
4310 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4311 else
4312 *total = COSTS_N_INSNS (4); /* How knows? */
4313 return false;
4317 /* RTX costs for cores with a slow MUL implementation. */
4319 static bool
4320 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4322 enum machine_mode mode = GET_MODE (x);
4324 if (TARGET_THUMB)
4326 *total = thumb_rtx_costs (x, code, outer_code);
4327 return true;
4330 switch (code)
4332 case MULT:
4333 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4334 || mode == DImode)
4336 *total = 30;
4337 return true;
4340 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4342 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4343 & (unsigned HOST_WIDE_INT) 0xffffffff);
4344 int cost, const_ok = const_ok_for_arm (i);
4345 int j, booth_unit_size;
4347 /* Tune as appropriate. */
4348 cost = const_ok ? 4 : 8;
4349 booth_unit_size = 2;
4350 for (j = 0; i && j < 32; j += booth_unit_size)
4352 i >>= booth_unit_size;
4353 cost += 2;
4356 *total = cost;
4357 return true;
4360 *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4361 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4362 return true;
4364 default:
4365 *total = arm_rtx_costs_1 (x, code, outer_code);
4366 return true;
4371 /* RTX cost for cores with a fast multiply unit (M variants). */
4373 static bool
4374 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4376 enum machine_mode mode = GET_MODE (x);
4378 if (TARGET_THUMB)
4380 *total = thumb_rtx_costs (x, code, outer_code);
4381 return true;
4384 switch (code)
4386 case MULT:
4387 /* There is no point basing this on the tuning, since it is always the
4388 fast variant if it exists at all. */
4389 if (mode == DImode
4390 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4391 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4392 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4394 *total = 8;
4395 return true;
4399 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4400 || mode == DImode)
4402 *total = 30;
4403 return true;
4406 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4408 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4409 & (unsigned HOST_WIDE_INT) 0xffffffff);
4410 int cost, const_ok = const_ok_for_arm (i);
4411 int j, booth_unit_size;
4413 /* Tune as appropriate. */
4414 cost = const_ok ? 4 : 8;
4415 booth_unit_size = 8;
4416 for (j = 0; i && j < 32; j += booth_unit_size)
4418 i >>= booth_unit_size;
4419 cost += 2;
4422 *total = cost;
4423 return true;
4426 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4427 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4428 return true;
4430 default:
4431 *total = arm_rtx_costs_1 (x, code, outer_code);
4432 return true;
4437 /* RTX cost for XScale CPUs. */
4439 static bool
4440 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4442 enum machine_mode mode = GET_MODE (x);
4444 if (TARGET_THUMB)
4446 *total = thumb_rtx_costs (x, code, outer_code);
4447 return true;
4450 switch (code)
4452 case MULT:
4453 /* There is no point basing this on the tuning, since it is always the
4454 fast variant if it exists at all. */
4455 if (mode == DImode
4456 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4457 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4458 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4460 *total = 8;
4461 return true;
4465 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4466 || mode == DImode)
4468 *total = 30;
4469 return true;
4472 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4474 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4475 & (unsigned HOST_WIDE_INT) 0xffffffff);
4476 int cost, const_ok = const_ok_for_arm (i);
4477 unsigned HOST_WIDE_INT masked_const;
4479 /* The cost will be related to two insns.
4480 First a load of the constant (MOV or LDR), then a multiply. */
4481 cost = 2;
4482 if (! const_ok)
4483 cost += 1; /* LDR is probably more expensive because
4484 of longer result latency. */
4485 masked_const = i & 0xffff8000;
4486 if (masked_const != 0 && masked_const != 0xffff8000)
4488 masked_const = i & 0xf8000000;
4489 if (masked_const == 0 || masked_const == 0xf8000000)
4490 cost += 1;
4491 else
4492 cost += 2;
4494 *total = cost;
4495 return true;
4498 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4499 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4500 return true;
4502 case COMPARE:
4503 /* A COMPARE of a MULT is slow on XScale; the muls instruction
4504 will stall until the multiplication is complete. */
4505 if (GET_CODE (XEXP (x, 0)) == MULT)
4506 *total = 4 + rtx_cost (XEXP (x, 0), code);
4507 else
4508 *total = arm_rtx_costs_1 (x, code, outer_code);
4509 return true;
4511 default:
4512 *total = arm_rtx_costs_1 (x, code, outer_code);
4513 return true;
4518 /* RTX costs for 9e (and later) cores. */
4520 static bool
4521 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4523 enum machine_mode mode = GET_MODE (x);
4524 int nonreg_cost;
4525 int cost;
4527 if (TARGET_THUMB)
4529 switch (code)
4531 case MULT:
4532 *total = COSTS_N_INSNS (3);
4533 return true;
4535 default:
4536 *total = thumb_rtx_costs (x, code, outer_code);
4537 return true;
4541 switch (code)
4543 case MULT:
4544 /* There is no point basing this on the tuning, since it is always the
4545 fast variant if it exists at all. */
4546 if (mode == DImode
4547 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4548 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4549 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4551 *total = 3;
4552 return true;
4556 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4558 *total = 30;
4559 return true;
4561 if (mode == DImode)
4563 cost = 7;
4564 nonreg_cost = 8;
4566 else
4568 cost = 2;
4569 nonreg_cost = 4;
4573 *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4574 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4575 return true;
4577 default:
4578 *total = arm_rtx_costs_1 (x, code, outer_code);
4579 return true;
4582 /* All address computations that can be done are free, but rtx cost returns
4583 the same for practically all of them. So we weight the different types
4584 of address here in the order (most pref first):
4585 PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
4586 static inline int
4587 arm_arm_address_cost (rtx x)
4589 enum rtx_code c = GET_CODE (x);
4591 if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4592 return 0;
4593 if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4594 return 10;
4596 if (c == PLUS || c == MINUS)
4598 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4599 return 2;
4601 if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4602 return 3;
4604 return 4;
4607 return 6;
4610 static inline int
4611 arm_thumb_address_cost (rtx x)
4613 enum rtx_code c = GET_CODE (x);
4615 if (c == REG)
4616 return 1;
4617 if (c == PLUS
4618 && GET_CODE (XEXP (x, 0)) == REG
4619 && GET_CODE (XEXP (x, 1)) == CONST_INT)
4620 return 1;
4622 return 2;
4625 static int
4626 arm_address_cost (rtx x)
4628 return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4631 static int
4632 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4634 rtx i_pat, d_pat;
4636 /* Some true dependencies can have a higher cost depending
4637 on precisely how certain input operands are used. */
4638 if (arm_tune_xscale
4639 && REG_NOTE_KIND (link) == 0
4640 && recog_memoized (insn) >= 0
4641 && recog_memoized (dep) >= 0)
4643 int shift_opnum = get_attr_shift (insn);
4644 enum attr_type attr_type = get_attr_type (dep);
4646 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4647 operand for INSN. If we have a shifted input operand and the
4648 instruction we depend on is another ALU instruction, then we may
4649 have to account for an additional stall. */
4650 if (shift_opnum != 0
4651 && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4653 rtx shifted_operand;
4654 int opno;
4656 /* Get the shifted operand. */
4657 extract_insn (insn);
4658 shifted_operand = recog_data.operand[shift_opnum];
4660 /* Iterate over all the operands in DEP. If we write an operand
4661 that overlaps with SHIFTED_OPERAND, then we have increase the
4662 cost of this dependency. */
4663 extract_insn (dep);
4664 preprocess_constraints ();
4665 for (opno = 0; opno < recog_data.n_operands; opno++)
4667 /* We can ignore strict inputs. */
4668 if (recog_data.operand_type[opno] == OP_IN)
4669 continue;
4671 if (reg_overlap_mentioned_p (recog_data.operand[opno],
4672 shifted_operand))
4673 return 2;
4678 /* XXX This is not strictly true for the FPA. */
4679 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4680 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4681 return 0;
4683 /* Call insns don't incur a stall, even if they follow a load. */
4684 if (REG_NOTE_KIND (link) == 0
4685 && GET_CODE (insn) == CALL_INSN)
4686 return 1;
4688 if ((i_pat = single_set (insn)) != NULL
4689 && GET_CODE (SET_SRC (i_pat)) == MEM
4690 && (d_pat = single_set (dep)) != NULL
4691 && GET_CODE (SET_DEST (d_pat)) == MEM)
4693 rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4694 /* This is a load after a store, there is no conflict if the load reads
4695 from a cached area. Assume that loads from the stack, and from the
4696 constant pool are cached, and that others will miss. This is a
4697 hack. */
4699 if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4700 || reg_mentioned_p (stack_pointer_rtx, src_mem)
4701 || reg_mentioned_p (frame_pointer_rtx, src_mem)
4702 || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4703 return 1;
4706 return cost;
4709 static int fp_consts_inited = 0;
4711 /* Only zero is valid for VFP. Other values are also valid for FPA. */
4712 static const char * const strings_fp[8] =
4714 "0", "1", "2", "3",
4715 "4", "5", "0.5", "10"
4718 static REAL_VALUE_TYPE values_fp[8];
4720 static void
4721 init_fp_table (void)
4723 int i;
4724 REAL_VALUE_TYPE r;
4726 if (TARGET_VFP)
4727 fp_consts_inited = 1;
4728 else
4729 fp_consts_inited = 8;
4731 for (i = 0; i < fp_consts_inited; i++)
4733 r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4734 values_fp[i] = r;
4738 /* Return TRUE if rtx X is a valid immediate FP constant. */
4740 arm_const_double_rtx (rtx x)
4742 REAL_VALUE_TYPE r;
4743 int i;
4745 if (!fp_consts_inited)
4746 init_fp_table ();
4748 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4749 if (REAL_VALUE_MINUS_ZERO (r))
4750 return 0;
4752 for (i = 0; i < fp_consts_inited; i++)
4753 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4754 return 1;
4756 return 0;
4759 /* Return TRUE if rtx X is a valid immediate FPA constant. */
4761 neg_const_double_rtx_ok_for_fpa (rtx x)
4763 REAL_VALUE_TYPE r;
4764 int i;
4766 if (!fp_consts_inited)
4767 init_fp_table ();
4769 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4770 r = REAL_VALUE_NEGATE (r);
4771 if (REAL_VALUE_MINUS_ZERO (r))
4772 return 0;
4774 for (i = 0; i < 8; i++)
4775 if (REAL_VALUES_EQUAL (r, values_fp[i]))
4776 return 1;
4778 return 0;
4781 /* Predicates for `match_operand' and `match_operator'. */
4783 /* Return nonzero if OP is a valid Cirrus memory address pattern. */
4785 cirrus_memory_offset (rtx op)
4787 /* Reject eliminable registers. */
4788 if (! (reload_in_progress || reload_completed)
4789 && ( reg_mentioned_p (frame_pointer_rtx, op)
4790 || reg_mentioned_p (arg_pointer_rtx, op)
4791 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4792 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4793 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4794 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4795 return 0;
4797 if (GET_CODE (op) == MEM)
4799 rtx ind;
4801 ind = XEXP (op, 0);
4803 /* Match: (mem (reg)). */
4804 if (GET_CODE (ind) == REG)
4805 return 1;
4807 /* Match:
4808 (mem (plus (reg)
4809 (const))). */
4810 if (GET_CODE (ind) == PLUS
4811 && GET_CODE (XEXP (ind, 0)) == REG
4812 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4813 && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4814 return 1;
4817 return 0;
4820 /* Return TRUE if OP is a valid VFP memory address pattern.
4821 WB if true if writeback address modes are allowed. */
4824 arm_coproc_mem_operand (rtx op, bool wb)
4826 rtx ind;
4828 /* Reject eliminable registers. */
4829 if (! (reload_in_progress || reload_completed)
4830 && ( reg_mentioned_p (frame_pointer_rtx, op)
4831 || reg_mentioned_p (arg_pointer_rtx, op)
4832 || reg_mentioned_p (virtual_incoming_args_rtx, op)
4833 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4834 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4835 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4836 return FALSE;
4838 /* Constants are converted into offsets from labels. */
4839 if (GET_CODE (op) != MEM)
4840 return FALSE;
4842 ind = XEXP (op, 0);
4844 if (reload_completed
4845 && (GET_CODE (ind) == LABEL_REF
4846 || (GET_CODE (ind) == CONST
4847 && GET_CODE (XEXP (ind, 0)) == PLUS
4848 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4849 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4850 return TRUE;
4852 /* Match: (mem (reg)). */
4853 if (GET_CODE (ind) == REG)
4854 return arm_address_register_rtx_p (ind, 0);
4856 /* Autoincremment addressing modes. */
4857 if (wb
4858 && (GET_CODE (ind) == PRE_INC
4859 || GET_CODE (ind) == POST_INC
4860 || GET_CODE (ind) == PRE_DEC
4861 || GET_CODE (ind) == POST_DEC))
4862 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
4864 if (wb
4865 && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
4866 && arm_address_register_rtx_p (XEXP (ind, 0), 0)
4867 && GET_CODE (XEXP (ind, 1)) == PLUS
4868 && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
4869 ind = XEXP (ind, 1);
4871 /* Match:
4872 (plus (reg)
4873 (const)). */
4874 if (GET_CODE (ind) == PLUS
4875 && GET_CODE (XEXP (ind, 0)) == REG
4876 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4877 && GET_CODE (XEXP (ind, 1)) == CONST_INT
4878 && INTVAL (XEXP (ind, 1)) > -1024
4879 && INTVAL (XEXP (ind, 1)) < 1024
4880 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
4881 return TRUE;
4883 return FALSE;
4886 /* Return true if X is a register that will be eliminated later on. */
4888 arm_eliminable_register (rtx x)
4890 return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
4891 || REGNO (x) == ARG_POINTER_REGNUM
4892 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
4893 && REGNO (x) <= LAST_VIRTUAL_REGISTER));
4896 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4897 VFP registers. Otherwise return NO_REGS. */
4899 enum reg_class
4900 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4902 if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
4903 return NO_REGS;
4905 return GENERAL_REGS;
4909 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4910 Use by the Cirrus Maverick code which has to workaround
4911 a hardware bug triggered by such instructions. */
4912 static bool
4913 arm_memory_load_p (rtx insn)
4915 rtx body, lhs, rhs;;
4917 if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4918 return false;
4920 body = PATTERN (insn);
4922 if (GET_CODE (body) != SET)
4923 return false;
4925 lhs = XEXP (body, 0);
4926 rhs = XEXP (body, 1);
4928 lhs = REG_OR_SUBREG_RTX (lhs);
4930 /* If the destination is not a general purpose
4931 register we do not have to worry. */
4932 if (GET_CODE (lhs) != REG
4933 || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4934 return false;
4936 /* As well as loads from memory we also have to react
4937 to loads of invalid constants which will be turned
4938 into loads from the minipool. */
4939 return (GET_CODE (rhs) == MEM
4940 || GET_CODE (rhs) == SYMBOL_REF
4941 || note_invalid_constants (insn, -1, false));
4944 /* Return TRUE if INSN is a Cirrus instruction. */
4945 static bool
4946 arm_cirrus_insn_p (rtx insn)
4948 enum attr_cirrus attr;
4950 /* get_attr aborts on USE and CLOBBER. */
4951 if (!insn
4952 || GET_CODE (insn) != INSN
4953 || GET_CODE (PATTERN (insn)) == USE
4954 || GET_CODE (PATTERN (insn)) == CLOBBER)
4955 return 0;
4957 attr = get_attr_cirrus (insn);
4959 return attr != CIRRUS_NOT;
4962 /* Cirrus reorg for invalid instruction combinations. */
4963 static void
4964 cirrus_reorg (rtx first)
4966 enum attr_cirrus attr;
4967 rtx body = PATTERN (first);
4968 rtx t;
4969 int nops;
4971 /* Any branch must be followed by 2 non Cirrus instructions. */
4972 if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4974 nops = 0;
4975 t = next_nonnote_insn (first);
4977 if (arm_cirrus_insn_p (t))
4978 ++ nops;
4980 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4981 ++ nops;
4983 while (nops --)
4984 emit_insn_after (gen_nop (), first);
4986 return;
4989 /* (float (blah)) is in parallel with a clobber. */
4990 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4991 body = XVECEXP (body, 0, 0);
4993 if (GET_CODE (body) == SET)
4995 rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4997 /* cfldrd, cfldr64, cfstrd, cfstr64 must
4998 be followed by a non Cirrus insn. */
4999 if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
5001 if (arm_cirrus_insn_p (next_nonnote_insn (first)))
5002 emit_insn_after (gen_nop (), first);
5004 return;
5006 else if (arm_memory_load_p (first))
5008 unsigned int arm_regno;
5010 /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
5011 ldr/cfmv64hr combination where the Rd field is the same
5012 in both instructions must be split with a non Cirrus
5013 insn. Example:
5015 ldr r0, blah
5017 cfmvsr mvf0, r0. */
5019 /* Get Arm register number for ldr insn. */
5020 if (GET_CODE (lhs) == REG)
5021 arm_regno = REGNO (lhs);
5022 else if (GET_CODE (rhs) == REG)
5023 arm_regno = REGNO (rhs);
5024 else
5025 abort ();
5027 /* Next insn. */
5028 first = next_nonnote_insn (first);
5030 if (! arm_cirrus_insn_p (first))
5031 return;
5033 body = PATTERN (first);
5035 /* (float (blah)) is in parallel with a clobber. */
5036 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5037 body = XVECEXP (body, 0, 0);
5039 if (GET_CODE (body) == FLOAT)
5040 body = XEXP (body, 0);
5042 if (get_attr_cirrus (first) == CIRRUS_MOVE
5043 && GET_CODE (XEXP (body, 1)) == REG
5044 && arm_regno == REGNO (XEXP (body, 1)))
5045 emit_insn_after (gen_nop (), first);
5047 return;
5051 /* get_attr aborts on USE and CLOBBER. */
5052 if (!first
5053 || GET_CODE (first) != INSN
5054 || GET_CODE (PATTERN (first)) == USE
5055 || GET_CODE (PATTERN (first)) == CLOBBER)
5056 return;
5058 attr = get_attr_cirrus (first);
5060 /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5061 must be followed by a non-coprocessor instruction. */
5062 if (attr == CIRRUS_COMPARE)
5064 nops = 0;
5066 t = next_nonnote_insn (first);
5068 if (arm_cirrus_insn_p (t))
5069 ++ nops;
5071 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5072 ++ nops;
5074 while (nops --)
5075 emit_insn_after (gen_nop (), first);
5077 return;
5081 /* Return TRUE if X references a SYMBOL_REF. */
5083 symbol_mentioned_p (rtx x)
5085 const char * fmt;
5086 int i;
5088 if (GET_CODE (x) == SYMBOL_REF)
5089 return 1;
5091 fmt = GET_RTX_FORMAT (GET_CODE (x));
5093 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5095 if (fmt[i] == 'E')
5097 int j;
5099 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5100 if (symbol_mentioned_p (XVECEXP (x, i, j)))
5101 return 1;
5103 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5104 return 1;
5107 return 0;
5110 /* Return TRUE if X references a LABEL_REF. */
5112 label_mentioned_p (rtx x)
5114 const char * fmt;
5115 int i;
5117 if (GET_CODE (x) == LABEL_REF)
5118 return 1;
5120 fmt = GET_RTX_FORMAT (GET_CODE (x));
5121 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5123 if (fmt[i] == 'E')
5125 int j;
5127 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5128 if (label_mentioned_p (XVECEXP (x, i, j)))
5129 return 1;
5131 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5132 return 1;
5135 return 0;
5138 enum rtx_code
5139 minmax_code (rtx x)
5141 enum rtx_code code = GET_CODE (x);
5143 if (code == SMAX)
5144 return GE;
5145 else if (code == SMIN)
5146 return LE;
5147 else if (code == UMIN)
5148 return LEU;
5149 else if (code == UMAX)
5150 return GEU;
5152 abort ();
5155 /* Return 1 if memory locations are adjacent. */
5157 adjacent_mem_locations (rtx a, rtx b)
5159 /* We don't guarantee to preserve the order of these memory refs. */
5160 if (volatile_refs_p (a) || volatile_refs_p (b))
5161 return 0;
5163 if ((GET_CODE (XEXP (a, 0)) == REG
5164 || (GET_CODE (XEXP (a, 0)) == PLUS
5165 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5166 && (GET_CODE (XEXP (b, 0)) == REG
5167 || (GET_CODE (XEXP (b, 0)) == PLUS
5168 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5170 HOST_WIDE_INT val0 = 0, val1 = 0;
5171 rtx reg0, reg1;
5172 int val_diff;
5174 if (GET_CODE (XEXP (a, 0)) == PLUS)
5176 reg0 = XEXP (XEXP (a, 0), 0);
5177 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5179 else
5180 reg0 = XEXP (a, 0);
5182 if (GET_CODE (XEXP (b, 0)) == PLUS)
5184 reg1 = XEXP (XEXP (b, 0), 0);
5185 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5187 else
5188 reg1 = XEXP (b, 0);
5190 /* Don't accept any offset that will require multiple
5191 instructions to handle, since this would cause the
5192 arith_adjacentmem pattern to output an overlong sequence. */
5193 if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5194 return 0;
5196 /* Don't allow an eliminable register: register elimination can make
5197 the offset too large. */
5198 if (arm_eliminable_register (reg0))
5199 return 0;
5201 val_diff = val1 - val0;
5203 if (arm_ld_sched)
5205 /* If the target has load delay slots, then there's no benefit
5206 to using an ldm instruction unless the offset is zero and
5207 we are optimizing for size. */
5208 return (optimize_size && (REGNO (reg0) == REGNO (reg1))
5209 && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
5210 && (val_diff == 4 || val_diff == -4));
5213 return ((REGNO (reg0) == REGNO (reg1))
5214 && (val_diff == 4 || val_diff == -4));
5217 return 0;
5221 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5222 HOST_WIDE_INT *load_offset)
5224 int unsorted_regs[4];
5225 HOST_WIDE_INT unsorted_offsets[4];
5226 int order[4];
5227 int base_reg = -1;
5228 int i;
5230 /* Can only handle 2, 3, or 4 insns at present,
5231 though could be easily extended if required. */
5232 if (nops < 2 || nops > 4)
5233 abort ();
5235 /* Loop over the operands and check that the memory references are
5236 suitable (i.e. immediate offsets from the same base register). At
5237 the same time, extract the target register, and the memory
5238 offsets. */
5239 for (i = 0; i < nops; i++)
5241 rtx reg;
5242 rtx offset;
5244 /* Convert a subreg of a mem into the mem itself. */
5245 if (GET_CODE (operands[nops + i]) == SUBREG)
5246 operands[nops + i] = alter_subreg (operands + (nops + i));
5248 if (GET_CODE (operands[nops + i]) != MEM)
5249 abort ();
5251 /* Don't reorder volatile memory references; it doesn't seem worth
5252 looking for the case where the order is ok anyway. */
5253 if (MEM_VOLATILE_P (operands[nops + i]))
5254 return 0;
5256 offset = const0_rtx;
5258 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5259 || (GET_CODE (reg) == SUBREG
5260 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5261 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5262 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5263 == REG)
5264 || (GET_CODE (reg) == SUBREG
5265 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5266 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5267 == CONST_INT)))
5269 if (i == 0)
5271 base_reg = REGNO (reg);
5272 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5273 ? REGNO (operands[i])
5274 : REGNO (SUBREG_REG (operands[i])));
5275 order[0] = 0;
5277 else
5279 if (base_reg != (int) REGNO (reg))
5280 /* Not addressed from the same base register. */
5281 return 0;
5283 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5284 ? REGNO (operands[i])
5285 : REGNO (SUBREG_REG (operands[i])));
5286 if (unsorted_regs[i] < unsorted_regs[order[0]])
5287 order[0] = i;
5290 /* If it isn't an integer register, or if it overwrites the
5291 base register but isn't the last insn in the list, then
5292 we can't do this. */
5293 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5294 || (i != nops - 1 && unsorted_regs[i] == base_reg))
5295 return 0;
5297 unsorted_offsets[i] = INTVAL (offset);
5299 else
5300 /* Not a suitable memory address. */
5301 return 0;
5304 /* All the useful information has now been extracted from the
5305 operands into unsorted_regs and unsorted_offsets; additionally,
5306 order[0] has been set to the lowest numbered register in the
5307 list. Sort the registers into order, and check that the memory
5308 offsets are ascending and adjacent. */
5310 for (i = 1; i < nops; i++)
5312 int j;
5314 order[i] = order[i - 1];
5315 for (j = 0; j < nops; j++)
5316 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5317 && (order[i] == order[i - 1]
5318 || unsorted_regs[j] < unsorted_regs[order[i]]))
5319 order[i] = j;
5321 /* Have we found a suitable register? if not, one must be used more
5322 than once. */
5323 if (order[i] == order[i - 1])
5324 return 0;
5326 /* Is the memory address adjacent and ascending? */
5327 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5328 return 0;
5331 if (base)
5333 *base = base_reg;
5335 for (i = 0; i < nops; i++)
5336 regs[i] = unsorted_regs[order[i]];
5338 *load_offset = unsorted_offsets[order[0]];
5341 if (unsorted_offsets[order[0]] == 0)
5342 return 1; /* ldmia */
5344 if (unsorted_offsets[order[0]] == 4)
5345 return 2; /* ldmib */
5347 if (unsorted_offsets[order[nops - 1]] == 0)
5348 return 3; /* ldmda */
5350 if (unsorted_offsets[order[nops - 1]] == -4)
5351 return 4; /* ldmdb */
5353 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5354 if the offset isn't small enough. The reason 2 ldrs are faster
5355 is because these ARMs are able to do more than one cache access
5356 in a single cycle. The ARM9 and StrongARM have Harvard caches,
5357 whilst the ARM8 has a double bandwidth cache. This means that
5358 these cores can do both an instruction fetch and a data fetch in
5359 a single cycle, so the trick of calculating the address into a
5360 scratch register (one of the result regs) and then doing a load
5361 multiple actually becomes slower (and no smaller in code size).
5362 That is the transformation
5364 ldr rd1, [rbase + offset]
5365 ldr rd2, [rbase + offset + 4]
5369 add rd1, rbase, offset
5370 ldmia rd1, {rd1, rd2}
5372 produces worse code -- '3 cycles + any stalls on rd2' instead of
5373 '2 cycles + any stalls on rd2'. On ARMs with only one cache
5374 access per cycle, the first sequence could never complete in less
5375 than 6 cycles, whereas the ldm sequence would only take 5 and
5376 would make better use of sequential accesses if not hitting the
5377 cache.
5379 We cheat here and test 'arm_ld_sched' which we currently know to
5380 only be true for the ARM8, ARM9 and StrongARM. If this ever
5381 changes, then the test below needs to be reworked. */
5382 if (nops == 2 && arm_ld_sched)
5383 return 0;
5385 /* Can't do it without setting up the offset, only do this if it takes
5386 no more than one insn. */
5387 return (const_ok_for_arm (unsorted_offsets[order[0]])
5388 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5391 const char *
5392 emit_ldm_seq (rtx *operands, int nops)
5394 int regs[4];
5395 int base_reg;
5396 HOST_WIDE_INT offset;
5397 char buf[100];
5398 int i;
5400 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5402 case 1:
5403 strcpy (buf, "ldm%?ia\t");
5404 break;
5406 case 2:
5407 strcpy (buf, "ldm%?ib\t");
5408 break;
5410 case 3:
5411 strcpy (buf, "ldm%?da\t");
5412 break;
5414 case 4:
5415 strcpy (buf, "ldm%?db\t");
5416 break;
5418 case 5:
5419 if (offset >= 0)
5420 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5421 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5422 (long) offset);
5423 else
5424 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5425 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5426 (long) -offset);
5427 output_asm_insn (buf, operands);
5428 base_reg = regs[0];
5429 strcpy (buf, "ldm%?ia\t");
5430 break;
5432 default:
5433 abort ();
5436 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5437 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5439 for (i = 1; i < nops; i++)
5440 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5441 reg_names[regs[i]]);
5443 strcat (buf, "}\t%@ phole ldm");
5445 output_asm_insn (buf, operands);
5446 return "";
5450 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5451 HOST_WIDE_INT * load_offset)
5453 int unsorted_regs[4];
5454 HOST_WIDE_INT unsorted_offsets[4];
5455 int order[4];
5456 int base_reg = -1;
5457 int i;
5459 /* Can only handle 2, 3, or 4 insns at present, though could be easily
5460 extended if required. */
5461 if (nops < 2 || nops > 4)
5462 abort ();
5464 /* Loop over the operands and check that the memory references are
5465 suitable (i.e. immediate offsets from the same base register). At
5466 the same time, extract the target register, and the memory
5467 offsets. */
5468 for (i = 0; i < nops; i++)
5470 rtx reg;
5471 rtx offset;
5473 /* Convert a subreg of a mem into the mem itself. */
5474 if (GET_CODE (operands[nops + i]) == SUBREG)
5475 operands[nops + i] = alter_subreg (operands + (nops + i));
5477 if (GET_CODE (operands[nops + i]) != MEM)
5478 abort ();
5480 /* Don't reorder volatile memory references; it doesn't seem worth
5481 looking for the case where the order is ok anyway. */
5482 if (MEM_VOLATILE_P (operands[nops + i]))
5483 return 0;
5485 offset = const0_rtx;
5487 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5488 || (GET_CODE (reg) == SUBREG
5489 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5490 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5491 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5492 == REG)
5493 || (GET_CODE (reg) == SUBREG
5494 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5495 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5496 == CONST_INT)))
5498 if (i == 0)
5500 base_reg = REGNO (reg);
5501 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5502 ? REGNO (operands[i])
5503 : REGNO (SUBREG_REG (operands[i])));
5504 order[0] = 0;
5506 else
5508 if (base_reg != (int) REGNO (reg))
5509 /* Not addressed from the same base register. */
5510 return 0;
5512 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5513 ? REGNO (operands[i])
5514 : REGNO (SUBREG_REG (operands[i])));
5515 if (unsorted_regs[i] < unsorted_regs[order[0]])
5516 order[0] = i;
5519 /* If it isn't an integer register, then we can't do this. */
5520 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5521 return 0;
5523 unsorted_offsets[i] = INTVAL (offset);
5525 else
5526 /* Not a suitable memory address. */
5527 return 0;
5530 /* All the useful information has now been extracted from the
5531 operands into unsorted_regs and unsorted_offsets; additionally,
5532 order[0] has been set to the lowest numbered register in the
5533 list. Sort the registers into order, and check that the memory
5534 offsets are ascending and adjacent. */
5536 for (i = 1; i < nops; i++)
5538 int j;
5540 order[i] = order[i - 1];
5541 for (j = 0; j < nops; j++)
5542 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5543 && (order[i] == order[i - 1]
5544 || unsorted_regs[j] < unsorted_regs[order[i]]))
5545 order[i] = j;
5547 /* Have we found a suitable register? if not, one must be used more
5548 than once. */
5549 if (order[i] == order[i - 1])
5550 return 0;
5552 /* Is the memory address adjacent and ascending? */
5553 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5554 return 0;
5557 if (base)
5559 *base = base_reg;
5561 for (i = 0; i < nops; i++)
5562 regs[i] = unsorted_regs[order[i]];
5564 *load_offset = unsorted_offsets[order[0]];
5567 if (unsorted_offsets[order[0]] == 0)
5568 return 1; /* stmia */
5570 if (unsorted_offsets[order[0]] == 4)
5571 return 2; /* stmib */
5573 if (unsorted_offsets[order[nops - 1]] == 0)
5574 return 3; /* stmda */
5576 if (unsorted_offsets[order[nops - 1]] == -4)
5577 return 4; /* stmdb */
5579 return 0;
5582 const char *
5583 emit_stm_seq (rtx *operands, int nops)
5585 int regs[4];
5586 int base_reg;
5587 HOST_WIDE_INT offset;
5588 char buf[100];
5589 int i;
5591 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5593 case 1:
5594 strcpy (buf, "stm%?ia\t");
5595 break;
5597 case 2:
5598 strcpy (buf, "stm%?ib\t");
5599 break;
5601 case 3:
5602 strcpy (buf, "stm%?da\t");
5603 break;
5605 case 4:
5606 strcpy (buf, "stm%?db\t");
5607 break;
5609 default:
5610 abort ();
5613 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5614 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5616 for (i = 1; i < nops; i++)
5617 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5618 reg_names[regs[i]]);
5620 strcat (buf, "}\t%@ phole stm");
5622 output_asm_insn (buf, operands);
5623 return "";
5627 /* Routines for use in generating RTL. */
5630 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5631 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5633 HOST_WIDE_INT offset = *offsetp;
5634 int i = 0, j;
5635 rtx result;
5636 int sign = up ? 1 : -1;
5637 rtx mem, addr;
5639 /* XScale has load-store double instructions, but they have stricter
5640 alignment requirements than load-store multiple, so we cannot
5641 use them.
5643 For XScale ldm requires 2 + NREGS cycles to complete and blocks
5644 the pipeline until completion.
5646 NREGS CYCLES
5652 An ldr instruction takes 1-3 cycles, but does not block the
5653 pipeline.
5655 NREGS CYCLES
5656 1 1-3
5657 2 2-6
5658 3 3-9
5659 4 4-12
5661 Best case ldr will always win. However, the more ldr instructions
5662 we issue, the less likely we are to be able to schedule them well.
5663 Using ldr instructions also increases code size.
5665 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5666 for counts of 3 or 4 regs. */
5667 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5669 rtx seq;
5671 start_sequence ();
5673 for (i = 0; i < count; i++)
5675 addr = plus_constant (from, i * 4 * sign);
5676 mem = adjust_automodify_address (basemem, SImode, addr, offset);
5677 emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5678 offset += 4 * sign;
5681 if (write_back)
5683 emit_move_insn (from, plus_constant (from, count * 4 * sign));
5684 *offsetp = offset;
5687 seq = get_insns ();
5688 end_sequence ();
5690 return seq;
5693 result = gen_rtx_PARALLEL (VOIDmode,
5694 rtvec_alloc (count + (write_back ? 1 : 0)));
5695 if (write_back)
5697 XVECEXP (result, 0, 0)
5698 = gen_rtx_SET (GET_MODE (from), from,
5699 plus_constant (from, count * 4 * sign));
5700 i = 1;
5701 count++;
5704 for (j = 0; i < count; i++, j++)
5706 addr = plus_constant (from, j * 4 * sign);
5707 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5708 XVECEXP (result, 0, i)
5709 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5710 offset += 4 * sign;
5713 if (write_back)
5714 *offsetp = offset;
5716 return result;
5720 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5721 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5723 HOST_WIDE_INT offset = *offsetp;
5724 int i = 0, j;
5725 rtx result;
5726 int sign = up ? 1 : -1;
5727 rtx mem, addr;
5729 /* See arm_gen_load_multiple for discussion of
5730 the pros/cons of ldm/stm usage for XScale. */
5731 if (arm_tune_xscale && count <= 2 && ! optimize_size)
5733 rtx seq;
5735 start_sequence ();
5737 for (i = 0; i < count; i++)
5739 addr = plus_constant (to, i * 4 * sign);
5740 mem = adjust_automodify_address (basemem, SImode, addr, offset);
5741 emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5742 offset += 4 * sign;
5745 if (write_back)
5747 emit_move_insn (to, plus_constant (to, count * 4 * sign));
5748 *offsetp = offset;
5751 seq = get_insns ();
5752 end_sequence ();
5754 return seq;
5757 result = gen_rtx_PARALLEL (VOIDmode,
5758 rtvec_alloc (count + (write_back ? 1 : 0)));
5759 if (write_back)
5761 XVECEXP (result, 0, 0)
5762 = gen_rtx_SET (GET_MODE (to), to,
5763 plus_constant (to, count * 4 * sign));
5764 i = 1;
5765 count++;
5768 for (j = 0; i < count; i++, j++)
5770 addr = plus_constant (to, j * 4 * sign);
5771 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5772 XVECEXP (result, 0, i)
5773 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5774 offset += 4 * sign;
5777 if (write_back)
5778 *offsetp = offset;
5780 return result;
5784 arm_gen_movmemqi (rtx *operands)
5786 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5787 HOST_WIDE_INT srcoffset, dstoffset;
5788 int i;
5789 rtx src, dst, srcbase, dstbase;
5790 rtx part_bytes_reg = NULL;
5791 rtx mem;
5793 if (GET_CODE (operands[2]) != CONST_INT
5794 || GET_CODE (operands[3]) != CONST_INT
5795 || INTVAL (operands[2]) > 64
5796 || INTVAL (operands[3]) & 3)
5797 return 0;
5799 dstbase = operands[0];
5800 srcbase = operands[1];
5802 dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
5803 src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
5805 in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5806 out_words_to_go = INTVAL (operands[2]) / 4;
5807 last_bytes = INTVAL (operands[2]) & 3;
5808 dstoffset = srcoffset = 0;
5810 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5811 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5813 for (i = 0; in_words_to_go >= 2; i+=4)
5815 if (in_words_to_go > 4)
5816 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5817 srcbase, &srcoffset));
5818 else
5819 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
5820 FALSE, srcbase, &srcoffset));
5822 if (out_words_to_go)
5824 if (out_words_to_go > 4)
5825 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5826 dstbase, &dstoffset));
5827 else if (out_words_to_go != 1)
5828 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5829 dst, TRUE,
5830 (last_bytes == 0
5831 ? FALSE : TRUE),
5832 dstbase, &dstoffset));
5833 else
5835 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5836 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5837 if (last_bytes != 0)
5839 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5840 dstoffset += 4;
5845 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5846 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
5849 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
5850 if (out_words_to_go)
5852 rtx sreg;
5854 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5855 sreg = copy_to_reg (mem);
5857 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5858 emit_move_insn (mem, sreg);
5859 in_words_to_go--;
5861 if (in_words_to_go) /* Sanity check */
5862 abort ();
5865 if (in_words_to_go)
5867 if (in_words_to_go < 0)
5868 abort ();
5870 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5871 part_bytes_reg = copy_to_mode_reg (SImode, mem);
5874 if (last_bytes && part_bytes_reg == NULL)
5875 abort ();
5877 if (BYTES_BIG_ENDIAN && last_bytes)
5879 rtx tmp = gen_reg_rtx (SImode);
5881 /* The bytes we want are in the top end of the word. */
5882 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
5883 GEN_INT (8 * (4 - last_bytes))));
5884 part_bytes_reg = tmp;
5886 while (last_bytes)
5888 mem = adjust_automodify_address (dstbase, QImode,
5889 plus_constant (dst, last_bytes - 1),
5890 dstoffset + last_bytes - 1);
5891 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5893 if (--last_bytes)
5895 tmp = gen_reg_rtx (SImode);
5896 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
5897 part_bytes_reg = tmp;
5902 else
5904 if (last_bytes > 1)
5906 mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
5907 emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
5908 last_bytes -= 2;
5909 if (last_bytes)
5911 rtx tmp = gen_reg_rtx (SImode);
5912 emit_insn (gen_addsi3 (dst, dst, const2_rtx));
5913 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
5914 part_bytes_reg = tmp;
5915 dstoffset += 2;
5919 if (last_bytes)
5921 mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
5922 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5926 return 1;
5929 /* Generate a memory reference for a half word, such that it will be loaded
5930 into the top 16 bits of the word. We can assume that the address is
5931 known to be alignable and of the form reg, or plus (reg, const). */
5934 arm_gen_rotated_half_load (rtx memref)
5936 HOST_WIDE_INT offset = 0;
5937 rtx base = XEXP (memref, 0);
5939 if (GET_CODE (base) == PLUS)
5941 offset = INTVAL (XEXP (base, 1));
5942 base = XEXP (base, 0);
5945 /* If we aren't allowed to generate unaligned addresses, then fail. */
5946 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0))
5947 return NULL;
5949 base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
5951 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
5952 return base;
5954 return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
5957 /* Select a dominance comparison mode if possible for a test of the general
5958 form (OP (COND_OR (X) (Y)) (const_int 0)). We support three forms.
5959 COND_OR == DOM_CC_X_AND_Y => (X && Y)
5960 COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
5961 COND_OR == DOM_CC_X_OR_Y => (X || Y)
5962 In all cases OP will be either EQ or NE, but we don't need to know which
5963 here. If we are unable to support a dominance comparison we return
5964 CC mode. This will then fail to match for the RTL expressions that
5965 generate this call. */
5966 enum machine_mode
5967 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
5969 enum rtx_code cond1, cond2;
5970 int swapped = 0;
5972 /* Currently we will probably get the wrong result if the individual
5973 comparisons are not simple. This also ensures that it is safe to
5974 reverse a comparison if necessary. */
5975 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
5976 != CCmode)
5977 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
5978 != CCmode))
5979 return CCmode;
5981 /* The if_then_else variant of this tests the second condition if the
5982 first passes, but is true if the first fails. Reverse the first
5983 condition to get a true "inclusive-or" expression. */
5984 if (cond_or == DOM_CC_NX_OR_Y)
5985 cond1 = reverse_condition (cond1);
5987 /* If the comparisons are not equal, and one doesn't dominate the other,
5988 then we can't do this. */
5989 if (cond1 != cond2
5990 && !comparison_dominates_p (cond1, cond2)
5991 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
5992 return CCmode;
5994 if (swapped)
5996 enum rtx_code temp = cond1;
5997 cond1 = cond2;
5998 cond2 = temp;
6001 switch (cond1)
6003 case EQ:
6004 if (cond2 == EQ || cond_or == DOM_CC_X_AND_Y)
6005 return CC_DEQmode;
6007 switch (cond2)
6009 case LE: return CC_DLEmode;
6010 case LEU: return CC_DLEUmode;
6011 case GE: return CC_DGEmode;
6012 case GEU: return CC_DGEUmode;
6013 default: break;
6016 break;
6018 case LT:
6019 if (cond2 == LT || cond_or == DOM_CC_X_AND_Y)
6020 return CC_DLTmode;
6021 if (cond2 == LE)
6022 return CC_DLEmode;
6023 if (cond2 == NE)
6024 return CC_DNEmode;
6025 break;
6027 case GT:
6028 if (cond2 == GT || cond_or == DOM_CC_X_AND_Y)
6029 return CC_DGTmode;
6030 if (cond2 == GE)
6031 return CC_DGEmode;
6032 if (cond2 == NE)
6033 return CC_DNEmode;
6034 break;
6036 case LTU:
6037 if (cond2 == LTU || cond_or == DOM_CC_X_AND_Y)
6038 return CC_DLTUmode;
6039 if (cond2 == LEU)
6040 return CC_DLEUmode;
6041 if (cond2 == NE)
6042 return CC_DNEmode;
6043 break;
6045 case GTU:
6046 if (cond2 == GTU || cond_or == DOM_CC_X_AND_Y)
6047 return CC_DGTUmode;
6048 if (cond2 == GEU)
6049 return CC_DGEUmode;
6050 if (cond2 == NE)
6051 return CC_DNEmode;
6052 break;
6054 /* The remaining cases only occur when both comparisons are the
6055 same. */
6056 case NE:
6057 return CC_DNEmode;
6059 case LE:
6060 return CC_DLEmode;
6062 case GE:
6063 return CC_DGEmode;
6065 case LEU:
6066 return CC_DLEUmode;
6068 case GEU:
6069 return CC_DGEUmode;
6071 default:
6072 break;
6075 abort ();
6078 enum machine_mode
6079 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6081 /* All floating point compares return CCFP if it is an equality
6082 comparison, and CCFPE otherwise. */
6083 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6085 switch (op)
6087 case EQ:
6088 case NE:
6089 case UNORDERED:
6090 case ORDERED:
6091 case UNLT:
6092 case UNLE:
6093 case UNGT:
6094 case UNGE:
6095 case UNEQ:
6096 case LTGT:
6097 return CCFPmode;
6099 case LT:
6100 case LE:
6101 case GT:
6102 case GE:
6103 if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6104 return CCFPmode;
6105 return CCFPEmode;
6107 default:
6108 abort ();
6112 /* A compare with a shifted operand. Because of canonicalization, the
6113 comparison will have to be swapped when we emit the assembler. */
6114 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6115 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6116 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6117 || GET_CODE (x) == ROTATERT))
6118 return CC_SWPmode;
6120 /* This operation is performed swapped, but since we only rely on the Z
6121 flag we don't need an additional mode. */
6122 if (GET_MODE (y) == SImode && REG_P (y)
6123 && GET_CODE (x) == NEG
6124 && (op == EQ || op == NE))
6125 return CC_Zmode;
6127 /* This is a special case that is used by combine to allow a
6128 comparison of a shifted byte load to be split into a zero-extend
6129 followed by a comparison of the shifted integer (only valid for
6130 equalities and unsigned inequalities). */
6131 if (GET_MODE (x) == SImode
6132 && GET_CODE (x) == ASHIFT
6133 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6134 && GET_CODE (XEXP (x, 0)) == SUBREG
6135 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6136 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6137 && (op == EQ || op == NE
6138 || op == GEU || op == GTU || op == LTU || op == LEU)
6139 && GET_CODE (y) == CONST_INT)
6140 return CC_Zmode;
6142 /* A construct for a conditional compare, if the false arm contains
6143 0, then both conditions must be true, otherwise either condition
6144 must be true. Not all conditions are possible, so CCmode is
6145 returned if it can't be done. */
6146 if (GET_CODE (x) == IF_THEN_ELSE
6147 && (XEXP (x, 2) == const0_rtx
6148 || XEXP (x, 2) == const1_rtx)
6149 && COMPARISON_P (XEXP (x, 0))
6150 && COMPARISON_P (XEXP (x, 1)))
6151 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6152 INTVAL (XEXP (x, 2)));
6154 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
6155 if (GET_CODE (x) == AND
6156 && COMPARISON_P (XEXP (x, 0))
6157 && COMPARISON_P (XEXP (x, 1)))
6158 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6159 DOM_CC_X_AND_Y);
6161 if (GET_CODE (x) == IOR
6162 && COMPARISON_P (XEXP (x, 0))
6163 && COMPARISON_P (XEXP (x, 1)))
6164 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6165 DOM_CC_X_OR_Y);
6167 /* An operation (on Thumb) where we want to test for a single bit.
6168 This is done by shifting that bit up into the top bit of a
6169 scratch register; we can then branch on the sign bit. */
6170 if (TARGET_THUMB
6171 && GET_MODE (x) == SImode
6172 && (op == EQ || op == NE)
6173 && (GET_CODE (x) == ZERO_EXTRACT))
6174 return CC_Nmode;
6176 /* An operation that sets the condition codes as a side-effect, the
6177 V flag is not set correctly, so we can only use comparisons where
6178 this doesn't matter. (For LT and GE we can use "mi" and "pl"
6179 instead.) */
6180 if (GET_MODE (x) == SImode
6181 && y == const0_rtx
6182 && (op == EQ || op == NE || op == LT || op == GE)
6183 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6184 || GET_CODE (x) == AND || GET_CODE (x) == IOR
6185 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6186 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6187 || GET_CODE (x) == LSHIFTRT
6188 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6189 || GET_CODE (x) == ROTATERT
6190 || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6191 return CC_NOOVmode;
6193 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6194 return CC_Zmode;
6196 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6197 && GET_CODE (x) == PLUS
6198 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6199 return CC_Cmode;
6201 return CCmode;
6204 /* X and Y are two things to compare using CODE. Emit the compare insn and
6205 return the rtx for register 0 in the proper mode. FP means this is a
6206 floating point compare: I don't think that it is needed on the arm. */
6208 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6210 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6211 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6213 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6214 gen_rtx_COMPARE (mode, x, y)));
6216 return cc_reg;
6219 /* Generate a sequence of insns that will generate the correct return
6220 address mask depending on the physical architecture that the program
6221 is running on. */
6223 arm_gen_return_addr_mask (void)
6225 rtx reg = gen_reg_rtx (Pmode);
6227 emit_insn (gen_return_addr_mask (reg));
6228 return reg;
6231 void
6232 arm_reload_in_hi (rtx *operands)
6234 rtx ref = operands[1];
6235 rtx base, scratch;
6236 HOST_WIDE_INT offset = 0;
6238 if (GET_CODE (ref) == SUBREG)
6240 offset = SUBREG_BYTE (ref);
6241 ref = SUBREG_REG (ref);
6244 if (GET_CODE (ref) == REG)
6246 /* We have a pseudo which has been spilt onto the stack; there
6247 are two cases here: the first where there is a simple
6248 stack-slot replacement and a second where the stack-slot is
6249 out of range, or is used as a subreg. */
6250 if (reg_equiv_mem[REGNO (ref)])
6252 ref = reg_equiv_mem[REGNO (ref)];
6253 base = find_replacement (&XEXP (ref, 0));
6255 else
6256 /* The slot is out of range, or was dressed up in a SUBREG. */
6257 base = reg_equiv_address[REGNO (ref)];
6259 else
6260 base = find_replacement (&XEXP (ref, 0));
6262 /* Handle the case where the address is too complex to be offset by 1. */
6263 if (GET_CODE (base) == MINUS
6264 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6266 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6268 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6269 base = base_plus;
6271 else if (GET_CODE (base) == PLUS)
6273 /* The addend must be CONST_INT, or we would have dealt with it above. */
6274 HOST_WIDE_INT hi, lo;
6276 offset += INTVAL (XEXP (base, 1));
6277 base = XEXP (base, 0);
6279 /* Rework the address into a legal sequence of insns. */
6280 /* Valid range for lo is -4095 -> 4095 */
6281 lo = (offset >= 0
6282 ? (offset & 0xfff)
6283 : -((-offset) & 0xfff));
6285 /* Corner case, if lo is the max offset then we would be out of range
6286 once we have added the additional 1 below, so bump the msb into the
6287 pre-loading insn(s). */
6288 if (lo == 4095)
6289 lo &= 0x7ff;
6291 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6292 ^ (HOST_WIDE_INT) 0x80000000)
6293 - (HOST_WIDE_INT) 0x80000000);
6295 if (hi + lo != offset)
6296 abort ();
6298 if (hi != 0)
6300 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6302 /* Get the base address; addsi3 knows how to handle constants
6303 that require more than one insn. */
6304 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6305 base = base_plus;
6306 offset = lo;
6310 /* Operands[2] may overlap operands[0] (though it won't overlap
6311 operands[1]), that's why we asked for a DImode reg -- so we can
6312 use the bit that does not overlap. */
6313 if (REGNO (operands[2]) == REGNO (operands[0]))
6314 scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6315 else
6316 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6318 emit_insn (gen_zero_extendqisi2 (scratch,
6319 gen_rtx_MEM (QImode,
6320 plus_constant (base,
6321 offset))));
6322 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6323 gen_rtx_MEM (QImode,
6324 plus_constant (base,
6325 offset + 1))));
6326 if (!BYTES_BIG_ENDIAN)
6327 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6328 gen_rtx_IOR (SImode,
6329 gen_rtx_ASHIFT
6330 (SImode,
6331 gen_rtx_SUBREG (SImode, operands[0], 0),
6332 GEN_INT (8)),
6333 scratch)));
6334 else
6335 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6336 gen_rtx_IOR (SImode,
6337 gen_rtx_ASHIFT (SImode, scratch,
6338 GEN_INT (8)),
6339 gen_rtx_SUBREG (SImode, operands[0],
6340 0))));
6343 /* Handle storing a half-word to memory during reload by synthesizing as two
6344 byte stores. Take care not to clobber the input values until after we
6345 have moved them somewhere safe. This code assumes that if the DImode
6346 scratch in operands[2] overlaps either the input value or output address
6347 in some way, then that value must die in this insn (we absolutely need
6348 two scratch registers for some corner cases). */
6349 void
6350 arm_reload_out_hi (rtx *operands)
6352 rtx ref = operands[0];
6353 rtx outval = operands[1];
6354 rtx base, scratch;
6355 HOST_WIDE_INT offset = 0;
6357 if (GET_CODE (ref) == SUBREG)
6359 offset = SUBREG_BYTE (ref);
6360 ref = SUBREG_REG (ref);
6363 if (GET_CODE (ref) == REG)
6365 /* We have a pseudo which has been spilt onto the stack; there
6366 are two cases here: the first where there is a simple
6367 stack-slot replacement and a second where the stack-slot is
6368 out of range, or is used as a subreg. */
6369 if (reg_equiv_mem[REGNO (ref)])
6371 ref = reg_equiv_mem[REGNO (ref)];
6372 base = find_replacement (&XEXP (ref, 0));
6374 else
6375 /* The slot is out of range, or was dressed up in a SUBREG. */
6376 base = reg_equiv_address[REGNO (ref)];
6378 else
6379 base = find_replacement (&XEXP (ref, 0));
6381 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6383 /* Handle the case where the address is too complex to be offset by 1. */
6384 if (GET_CODE (base) == MINUS
6385 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6387 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6389 /* Be careful not to destroy OUTVAL. */
6390 if (reg_overlap_mentioned_p (base_plus, outval))
6392 /* Updating base_plus might destroy outval, see if we can
6393 swap the scratch and base_plus. */
6394 if (!reg_overlap_mentioned_p (scratch, outval))
6396 rtx tmp = scratch;
6397 scratch = base_plus;
6398 base_plus = tmp;
6400 else
6402 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6404 /* Be conservative and copy OUTVAL into the scratch now,
6405 this should only be necessary if outval is a subreg
6406 of something larger than a word. */
6407 /* XXX Might this clobber base? I can't see how it can,
6408 since scratch is known to overlap with OUTVAL, and
6409 must be wider than a word. */
6410 emit_insn (gen_movhi (scratch_hi, outval));
6411 outval = scratch_hi;
6415 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6416 base = base_plus;
6418 else if (GET_CODE (base) == PLUS)
6420 /* The addend must be CONST_INT, or we would have dealt with it above. */
6421 HOST_WIDE_INT hi, lo;
6423 offset += INTVAL (XEXP (base, 1));
6424 base = XEXP (base, 0);
6426 /* Rework the address into a legal sequence of insns. */
6427 /* Valid range for lo is -4095 -> 4095 */
6428 lo = (offset >= 0
6429 ? (offset & 0xfff)
6430 : -((-offset) & 0xfff));
6432 /* Corner case, if lo is the max offset then we would be out of range
6433 once we have added the additional 1 below, so bump the msb into the
6434 pre-loading insn(s). */
6435 if (lo == 4095)
6436 lo &= 0x7ff;
6438 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6439 ^ (HOST_WIDE_INT) 0x80000000)
6440 - (HOST_WIDE_INT) 0x80000000);
6442 if (hi + lo != offset)
6443 abort ();
6445 if (hi != 0)
6447 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6449 /* Be careful not to destroy OUTVAL. */
6450 if (reg_overlap_mentioned_p (base_plus, outval))
6452 /* Updating base_plus might destroy outval, see if we
6453 can swap the scratch and base_plus. */
6454 if (!reg_overlap_mentioned_p (scratch, outval))
6456 rtx tmp = scratch;
6457 scratch = base_plus;
6458 base_plus = tmp;
6460 else
6462 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6464 /* Be conservative and copy outval into scratch now,
6465 this should only be necessary if outval is a
6466 subreg of something larger than a word. */
6467 /* XXX Might this clobber base? I can't see how it
6468 can, since scratch is known to overlap with
6469 outval. */
6470 emit_insn (gen_movhi (scratch_hi, outval));
6471 outval = scratch_hi;
6475 /* Get the base address; addsi3 knows how to handle constants
6476 that require more than one insn. */
6477 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6478 base = base_plus;
6479 offset = lo;
6483 if (BYTES_BIG_ENDIAN)
6485 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6486 plus_constant (base, offset + 1)),
6487 gen_lowpart (QImode, outval)));
6488 emit_insn (gen_lshrsi3 (scratch,
6489 gen_rtx_SUBREG (SImode, outval, 0),
6490 GEN_INT (8)));
6491 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6492 gen_lowpart (QImode, scratch)));
6494 else
6496 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6497 gen_lowpart (QImode, outval)));
6498 emit_insn (gen_lshrsi3 (scratch,
6499 gen_rtx_SUBREG (SImode, outval, 0),
6500 GEN_INT (8)));
6501 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6502 plus_constant (base, offset + 1)),
6503 gen_lowpart (QImode, scratch)));
6507 /* Print a symbolic form of X to the debug file, F. */
6508 static void
6509 arm_print_value (FILE *f, rtx x)
6511 switch (GET_CODE (x))
6513 case CONST_INT:
6514 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6515 return;
6517 case CONST_DOUBLE:
6518 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6519 return;
6521 case CONST_VECTOR:
6523 int i;
6525 fprintf (f, "<");
6526 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6528 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6529 if (i < (CONST_VECTOR_NUNITS (x) - 1))
6530 fputc (',', f);
6532 fprintf (f, ">");
6534 return;
6536 case CONST_STRING:
6537 fprintf (f, "\"%s\"", XSTR (x, 0));
6538 return;
6540 case SYMBOL_REF:
6541 fprintf (f, "`%s'", XSTR (x, 0));
6542 return;
6544 case LABEL_REF:
6545 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6546 return;
6548 case CONST:
6549 arm_print_value (f, XEXP (x, 0));
6550 return;
6552 case PLUS:
6553 arm_print_value (f, XEXP (x, 0));
6554 fprintf (f, "+");
6555 arm_print_value (f, XEXP (x, 1));
6556 return;
6558 case PC:
6559 fprintf (f, "pc");
6560 return;
6562 default:
6563 fprintf (f, "????");
6564 return;
6568 /* Routines for manipulation of the constant pool. */
6570 /* Arm instructions cannot load a large constant directly into a
6571 register; they have to come from a pc relative load. The constant
6572 must therefore be placed in the addressable range of the pc
6573 relative load. Depending on the precise pc relative load
6574 instruction the range is somewhere between 256 bytes and 4k. This
6575 means that we often have to dump a constant inside a function, and
6576 generate code to branch around it.
6578 It is important to minimize this, since the branches will slow
6579 things down and make the code larger.
6581 Normally we can hide the table after an existing unconditional
6582 branch so that there is no interruption of the flow, but in the
6583 worst case the code looks like this:
6585 ldr rn, L1
6587 b L2
6588 align
6589 L1: .long value
6593 ldr rn, L3
6595 b L4
6596 align
6597 L3: .long value
6601 We fix this by performing a scan after scheduling, which notices
6602 which instructions need to have their operands fetched from the
6603 constant table and builds the table.
6605 The algorithm starts by building a table of all the constants that
6606 need fixing up and all the natural barriers in the function (places
6607 where a constant table can be dropped without breaking the flow).
6608 For each fixup we note how far the pc-relative replacement will be
6609 able to reach and the offset of the instruction into the function.
6611 Having built the table we then group the fixes together to form
6612 tables that are as large as possible (subject to addressing
6613 constraints) and emit each table of constants after the last
6614 barrier that is within range of all the instructions in the group.
6615 If a group does not contain a barrier, then we forcibly create one
6616 by inserting a jump instruction into the flow. Once the table has
6617 been inserted, the insns are then modified to reference the
6618 relevant entry in the pool.
6620 Possible enhancements to the algorithm (not implemented) are:
6622 1) For some processors and object formats, there may be benefit in
6623 aligning the pools to the start of cache lines; this alignment
6624 would need to be taken into account when calculating addressability
6625 of a pool. */
6627 /* These typedefs are located at the start of this file, so that
6628 they can be used in the prototypes there. This comment is to
6629 remind readers of that fact so that the following structures
6630 can be understood more easily.
6632 typedef struct minipool_node Mnode;
6633 typedef struct minipool_fixup Mfix; */
6635 struct minipool_node
6637 /* Doubly linked chain of entries. */
6638 Mnode * next;
6639 Mnode * prev;
6640 /* The maximum offset into the code that this entry can be placed. While
6641 pushing fixes for forward references, all entries are sorted in order
6642 of increasing max_address. */
6643 HOST_WIDE_INT max_address;
6644 /* Similarly for an entry inserted for a backwards ref. */
6645 HOST_WIDE_INT min_address;
6646 /* The number of fixes referencing this entry. This can become zero
6647 if we "unpush" an entry. In this case we ignore the entry when we
6648 come to emit the code. */
6649 int refcount;
6650 /* The offset from the start of the minipool. */
6651 HOST_WIDE_INT offset;
6652 /* The value in table. */
6653 rtx value;
6654 /* The mode of value. */
6655 enum machine_mode mode;
6656 /* The size of the value. With iWMMXt enabled
6657 sizes > 4 also imply an alignment of 8-bytes. */
6658 int fix_size;
6661 struct minipool_fixup
6663 Mfix * next;
6664 rtx insn;
6665 HOST_WIDE_INT address;
6666 rtx * loc;
6667 enum machine_mode mode;
6668 int fix_size;
6669 rtx value;
6670 Mnode * minipool;
6671 HOST_WIDE_INT forwards;
6672 HOST_WIDE_INT backwards;
6675 /* Fixes less than a word need padding out to a word boundary. */
6676 #define MINIPOOL_FIX_SIZE(mode) \
6677 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6679 static Mnode * minipool_vector_head;
6680 static Mnode * minipool_vector_tail;
6681 static rtx minipool_vector_label;
6683 /* The linked list of all minipool fixes required for this function. */
6684 Mfix * minipool_fix_head;
6685 Mfix * minipool_fix_tail;
6686 /* The fix entry for the current minipool, once it has been placed. */
6687 Mfix * minipool_barrier;
6689 /* Determines if INSN is the start of a jump table. Returns the end
6690 of the TABLE or NULL_RTX. */
6691 static rtx
6692 is_jump_table (rtx insn)
6694 rtx table;
6696 if (GET_CODE (insn) == JUMP_INSN
6697 && JUMP_LABEL (insn) != NULL
6698 && ((table = next_real_insn (JUMP_LABEL (insn)))
6699 == next_real_insn (insn))
6700 && table != NULL
6701 && GET_CODE (table) == JUMP_INSN
6702 && (GET_CODE (PATTERN (table)) == ADDR_VEC
6703 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6704 return table;
6706 return NULL_RTX;
6709 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6710 #define JUMP_TABLES_IN_TEXT_SECTION 0
6711 #endif
6713 static HOST_WIDE_INT
6714 get_jump_table_size (rtx insn)
6716 /* ADDR_VECs only take room if read-only data does into the text
6717 section. */
6718 if (JUMP_TABLES_IN_TEXT_SECTION
6719 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6720 || 1
6721 #endif
6724 rtx body = PATTERN (insn);
6725 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6727 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6730 return 0;
6733 /* Move a minipool fix MP from its current location to before MAX_MP.
6734 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6735 constraints may need updating. */
6736 static Mnode *
6737 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6738 HOST_WIDE_INT max_address)
6740 /* This should never be true and the code below assumes these are
6741 different. */
6742 if (mp == max_mp)
6743 abort ();
6745 if (max_mp == NULL)
6747 if (max_address < mp->max_address)
6748 mp->max_address = max_address;
6750 else
6752 if (max_address > max_mp->max_address - mp->fix_size)
6753 mp->max_address = max_mp->max_address - mp->fix_size;
6754 else
6755 mp->max_address = max_address;
6757 /* Unlink MP from its current position. Since max_mp is non-null,
6758 mp->prev must be non-null. */
6759 mp->prev->next = mp->next;
6760 if (mp->next != NULL)
6761 mp->next->prev = mp->prev;
6762 else
6763 minipool_vector_tail = mp->prev;
6765 /* Re-insert it before MAX_MP. */
6766 mp->next = max_mp;
6767 mp->prev = max_mp->prev;
6768 max_mp->prev = mp;
6770 if (mp->prev != NULL)
6771 mp->prev->next = mp;
6772 else
6773 minipool_vector_head = mp;
6776 /* Save the new entry. */
6777 max_mp = mp;
6779 /* Scan over the preceding entries and adjust their addresses as
6780 required. */
6781 while (mp->prev != NULL
6782 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6784 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6785 mp = mp->prev;
6788 return max_mp;
6791 /* Add a constant to the minipool for a forward reference. Returns the
6792 node added or NULL if the constant will not fit in this pool. */
6793 static Mnode *
6794 add_minipool_forward_ref (Mfix *fix)
6796 /* If set, max_mp is the first pool_entry that has a lower
6797 constraint than the one we are trying to add. */
6798 Mnode * max_mp = NULL;
6799 HOST_WIDE_INT max_address = fix->address + fix->forwards;
6800 Mnode * mp;
6802 /* If this fix's address is greater than the address of the first
6803 entry, then we can't put the fix in this pool. We subtract the
6804 size of the current fix to ensure that if the table is fully
6805 packed we still have enough room to insert this value by suffling
6806 the other fixes forwards. */
6807 if (minipool_vector_head &&
6808 fix->address >= minipool_vector_head->max_address - fix->fix_size)
6809 return NULL;
6811 /* Scan the pool to see if a constant with the same value has
6812 already been added. While we are doing this, also note the
6813 location where we must insert the constant if it doesn't already
6814 exist. */
6815 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6817 if (GET_CODE (fix->value) == GET_CODE (mp->value)
6818 && fix->mode == mp->mode
6819 && (GET_CODE (fix->value) != CODE_LABEL
6820 || (CODE_LABEL_NUMBER (fix->value)
6821 == CODE_LABEL_NUMBER (mp->value)))
6822 && rtx_equal_p (fix->value, mp->value))
6824 /* More than one fix references this entry. */
6825 mp->refcount++;
6826 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6829 /* Note the insertion point if necessary. */
6830 if (max_mp == NULL
6831 && mp->max_address > max_address)
6832 max_mp = mp;
6834 /* If we are inserting an 8-bytes aligned quantity and
6835 we have not already found an insertion point, then
6836 make sure that all such 8-byte aligned quantities are
6837 placed at the start of the pool. */
6838 if (ARM_DOUBLEWORD_ALIGN
6839 && max_mp == NULL
6840 && fix->fix_size == 8
6841 && mp->fix_size != 8)
6843 max_mp = mp;
6844 max_address = mp->max_address;
6848 /* The value is not currently in the minipool, so we need to create
6849 a new entry for it. If MAX_MP is NULL, the entry will be put on
6850 the end of the list since the placement is less constrained than
6851 any existing entry. Otherwise, we insert the new fix before
6852 MAX_MP and, if necessary, adjust the constraints on the other
6853 entries. */
6854 mp = xmalloc (sizeof (* mp));
6855 mp->fix_size = fix->fix_size;
6856 mp->mode = fix->mode;
6857 mp->value = fix->value;
6858 mp->refcount = 1;
6859 /* Not yet required for a backwards ref. */
6860 mp->min_address = -65536;
6862 if (max_mp == NULL)
6864 mp->max_address = max_address;
6865 mp->next = NULL;
6866 mp->prev = minipool_vector_tail;
6868 if (mp->prev == NULL)
6870 minipool_vector_head = mp;
6871 minipool_vector_label = gen_label_rtx ();
6873 else
6874 mp->prev->next = mp;
6876 minipool_vector_tail = mp;
6878 else
6880 if (max_address > max_mp->max_address - mp->fix_size)
6881 mp->max_address = max_mp->max_address - mp->fix_size;
6882 else
6883 mp->max_address = max_address;
6885 mp->next = max_mp;
6886 mp->prev = max_mp->prev;
6887 max_mp->prev = mp;
6888 if (mp->prev != NULL)
6889 mp->prev->next = mp;
6890 else
6891 minipool_vector_head = mp;
6894 /* Save the new entry. */
6895 max_mp = mp;
6897 /* Scan over the preceding entries and adjust their addresses as
6898 required. */
6899 while (mp->prev != NULL
6900 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6902 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6903 mp = mp->prev;
6906 return max_mp;
6909 static Mnode *
6910 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
6911 HOST_WIDE_INT min_address)
6913 HOST_WIDE_INT offset;
6915 /* This should never be true, and the code below assumes these are
6916 different. */
6917 if (mp == min_mp)
6918 abort ();
6920 if (min_mp == NULL)
6922 if (min_address > mp->min_address)
6923 mp->min_address = min_address;
6925 else
6927 /* We will adjust this below if it is too loose. */
6928 mp->min_address = min_address;
6930 /* Unlink MP from its current position. Since min_mp is non-null,
6931 mp->next must be non-null. */
6932 mp->next->prev = mp->prev;
6933 if (mp->prev != NULL)
6934 mp->prev->next = mp->next;
6935 else
6936 minipool_vector_head = mp->next;
6938 /* Reinsert it after MIN_MP. */
6939 mp->prev = min_mp;
6940 mp->next = min_mp->next;
6941 min_mp->next = mp;
6942 if (mp->next != NULL)
6943 mp->next->prev = mp;
6944 else
6945 minipool_vector_tail = mp;
6948 min_mp = mp;
6950 offset = 0;
6951 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6953 mp->offset = offset;
6954 if (mp->refcount > 0)
6955 offset += mp->fix_size;
6957 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
6958 mp->next->min_address = mp->min_address + mp->fix_size;
6961 return min_mp;
6964 /* Add a constant to the minipool for a backward reference. Returns the
6965 node added or NULL if the constant will not fit in this pool.
6967 Note that the code for insertion for a backwards reference can be
6968 somewhat confusing because the calculated offsets for each fix do
6969 not take into account the size of the pool (which is still under
6970 construction. */
6971 static Mnode *
6972 add_minipool_backward_ref (Mfix *fix)
6974 /* If set, min_mp is the last pool_entry that has a lower constraint
6975 than the one we are trying to add. */
6976 Mnode *min_mp = NULL;
6977 /* This can be negative, since it is only a constraint. */
6978 HOST_WIDE_INT min_address = fix->address - fix->backwards;
6979 Mnode *mp;
6981 /* If we can't reach the current pool from this insn, or if we can't
6982 insert this entry at the end of the pool without pushing other
6983 fixes out of range, then we don't try. This ensures that we
6984 can't fail later on. */
6985 if (min_address >= minipool_barrier->address
6986 || (minipool_vector_tail->min_address + fix->fix_size
6987 >= minipool_barrier->address))
6988 return NULL;
6990 /* Scan the pool to see if a constant with the same value has
6991 already been added. While we are doing this, also note the
6992 location where we must insert the constant if it doesn't already
6993 exist. */
6994 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
6996 if (GET_CODE (fix->value) == GET_CODE (mp->value)
6997 && fix->mode == mp->mode
6998 && (GET_CODE (fix->value) != CODE_LABEL
6999 || (CODE_LABEL_NUMBER (fix->value)
7000 == CODE_LABEL_NUMBER (mp->value)))
7001 && rtx_equal_p (fix->value, mp->value)
7002 /* Check that there is enough slack to move this entry to the
7003 end of the table (this is conservative). */
7004 && (mp->max_address
7005 > (minipool_barrier->address
7006 + minipool_vector_tail->offset
7007 + minipool_vector_tail->fix_size)))
7009 mp->refcount++;
7010 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7013 if (min_mp != NULL)
7014 mp->min_address += fix->fix_size;
7015 else
7017 /* Note the insertion point if necessary. */
7018 if (mp->min_address < min_address)
7020 /* For now, we do not allow the insertion of 8-byte alignment
7021 requiring nodes anywhere but at the start of the pool. */
7022 if (ARM_DOUBLEWORD_ALIGN
7023 && fix->fix_size == 8 && mp->fix_size != 8)
7024 return NULL;
7025 else
7026 min_mp = mp;
7028 else if (mp->max_address
7029 < minipool_barrier->address + mp->offset + fix->fix_size)
7031 /* Inserting before this entry would push the fix beyond
7032 its maximum address (which can happen if we have
7033 re-located a forwards fix); force the new fix to come
7034 after it. */
7035 min_mp = mp;
7036 min_address = mp->min_address + fix->fix_size;
7038 /* If we are inserting an 8-bytes aligned quantity and
7039 we have not already found an insertion point, then
7040 make sure that all such 8-byte aligned quantities are
7041 placed at the start of the pool. */
7042 else if (ARM_DOUBLEWORD_ALIGN
7043 && min_mp == NULL
7044 && fix->fix_size == 8
7045 && mp->fix_size < 8)
7047 min_mp = mp;
7048 min_address = mp->min_address + fix->fix_size;
7053 /* We need to create a new entry. */
7054 mp = xmalloc (sizeof (* mp));
7055 mp->fix_size = fix->fix_size;
7056 mp->mode = fix->mode;
7057 mp->value = fix->value;
7058 mp->refcount = 1;
7059 mp->max_address = minipool_barrier->address + 65536;
7061 mp->min_address = min_address;
7063 if (min_mp == NULL)
7065 mp->prev = NULL;
7066 mp->next = minipool_vector_head;
7068 if (mp->next == NULL)
7070 minipool_vector_tail = mp;
7071 minipool_vector_label = gen_label_rtx ();
7073 else
7074 mp->next->prev = mp;
7076 minipool_vector_head = mp;
7078 else
7080 mp->next = min_mp->next;
7081 mp->prev = min_mp;
7082 min_mp->next = mp;
7084 if (mp->next != NULL)
7085 mp->next->prev = mp;
7086 else
7087 minipool_vector_tail = mp;
7090 /* Save the new entry. */
7091 min_mp = mp;
7093 if (mp->prev)
7094 mp = mp->prev;
7095 else
7096 mp->offset = 0;
7098 /* Scan over the following entries and adjust their offsets. */
7099 while (mp->next != NULL)
7101 if (mp->next->min_address < mp->min_address + mp->fix_size)
7102 mp->next->min_address = mp->min_address + mp->fix_size;
7104 if (mp->refcount)
7105 mp->next->offset = mp->offset + mp->fix_size;
7106 else
7107 mp->next->offset = mp->offset;
7109 mp = mp->next;
7112 return min_mp;
7115 static void
7116 assign_minipool_offsets (Mfix *barrier)
7118 HOST_WIDE_INT offset = 0;
7119 Mnode *mp;
7121 minipool_barrier = barrier;
7123 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7125 mp->offset = offset;
7127 if (mp->refcount > 0)
7128 offset += mp->fix_size;
7132 /* Output the literal table */
7133 static void
7134 dump_minipool (rtx scan)
7136 Mnode * mp;
7137 Mnode * nmp;
7138 int align64 = 0;
7140 if (ARM_DOUBLEWORD_ALIGN)
7141 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7142 if (mp->refcount > 0 && mp->fix_size == 8)
7144 align64 = 1;
7145 break;
7148 if (dump_file)
7149 fprintf (dump_file,
7150 ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7151 INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7153 scan = emit_label_after (gen_label_rtx (), scan);
7154 scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7155 scan = emit_label_after (minipool_vector_label, scan);
7157 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7159 if (mp->refcount > 0)
7161 if (dump_file)
7163 fprintf (dump_file,
7164 ";; Offset %u, min %ld, max %ld ",
7165 (unsigned) mp->offset, (unsigned long) mp->min_address,
7166 (unsigned long) mp->max_address);
7167 arm_print_value (dump_file, mp->value);
7168 fputc ('\n', dump_file);
7171 switch (mp->fix_size)
7173 #ifdef HAVE_consttable_1
7174 case 1:
7175 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7176 break;
7178 #endif
7179 #ifdef HAVE_consttable_2
7180 case 2:
7181 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7182 break;
7184 #endif
7185 #ifdef HAVE_consttable_4
7186 case 4:
7187 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7188 break;
7190 #endif
7191 #ifdef HAVE_consttable_8
7192 case 8:
7193 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7194 break;
7196 #endif
7197 default:
7198 abort ();
7199 break;
7203 nmp = mp->next;
7204 free (mp);
7207 minipool_vector_head = minipool_vector_tail = NULL;
7208 scan = emit_insn_after (gen_consttable_end (), scan);
7209 scan = emit_barrier_after (scan);
7212 /* Return the cost of forcibly inserting a barrier after INSN. */
7213 static int
7214 arm_barrier_cost (rtx insn)
7216 /* Basing the location of the pool on the loop depth is preferable,
7217 but at the moment, the basic block information seems to be
7218 corrupt by this stage of the compilation. */
7219 int base_cost = 50;
7220 rtx next = next_nonnote_insn (insn);
7222 if (next != NULL && GET_CODE (next) == CODE_LABEL)
7223 base_cost -= 20;
7225 switch (GET_CODE (insn))
7227 case CODE_LABEL:
7228 /* It will always be better to place the table before the label, rather
7229 than after it. */
7230 return 50;
7232 case INSN:
7233 case CALL_INSN:
7234 return base_cost;
7236 case JUMP_INSN:
7237 return base_cost - 10;
7239 default:
7240 return base_cost + 10;
7244 /* Find the best place in the insn stream in the range
7245 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7246 Create the barrier by inserting a jump and add a new fix entry for
7247 it. */
7248 static Mfix *
7249 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7251 HOST_WIDE_INT count = 0;
7252 rtx barrier;
7253 rtx from = fix->insn;
7254 rtx selected = from;
7255 int selected_cost;
7256 HOST_WIDE_INT selected_address;
7257 Mfix * new_fix;
7258 HOST_WIDE_INT max_count = max_address - fix->address;
7259 rtx label = gen_label_rtx ();
7261 selected_cost = arm_barrier_cost (from);
7262 selected_address = fix->address;
7264 while (from && count < max_count)
7266 rtx tmp;
7267 int new_cost;
7269 /* This code shouldn't have been called if there was a natural barrier
7270 within range. */
7271 if (GET_CODE (from) == BARRIER)
7272 abort ();
7274 /* Count the length of this insn. */
7275 count += get_attr_length (from);
7277 /* If there is a jump table, add its length. */
7278 tmp = is_jump_table (from);
7279 if (tmp != NULL)
7281 count += get_jump_table_size (tmp);
7283 /* Jump tables aren't in a basic block, so base the cost on
7284 the dispatch insn. If we select this location, we will
7285 still put the pool after the table. */
7286 new_cost = arm_barrier_cost (from);
7288 if (count < max_count && new_cost <= selected_cost)
7290 selected = tmp;
7291 selected_cost = new_cost;
7292 selected_address = fix->address + count;
7295 /* Continue after the dispatch table. */
7296 from = NEXT_INSN (tmp);
7297 continue;
7300 new_cost = arm_barrier_cost (from);
7302 if (count < max_count && new_cost <= selected_cost)
7304 selected = from;
7305 selected_cost = new_cost;
7306 selected_address = fix->address + count;
7309 from = NEXT_INSN (from);
7312 /* Create a new JUMP_INSN that branches around a barrier. */
7313 from = emit_jump_insn_after (gen_jump (label), selected);
7314 JUMP_LABEL (from) = label;
7315 barrier = emit_barrier_after (from);
7316 emit_label_after (label, barrier);
7318 /* Create a minipool barrier entry for the new barrier. */
7319 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7320 new_fix->insn = barrier;
7321 new_fix->address = selected_address;
7322 new_fix->next = fix->next;
7323 fix->next = new_fix;
7325 return new_fix;
7328 /* Record that there is a natural barrier in the insn stream at
7329 ADDRESS. */
7330 static void
7331 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7333 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7335 fix->insn = insn;
7336 fix->address = address;
7338 fix->next = NULL;
7339 if (minipool_fix_head != NULL)
7340 minipool_fix_tail->next = fix;
7341 else
7342 minipool_fix_head = fix;
7344 minipool_fix_tail = fix;
7347 /* Record INSN, which will need fixing up to load a value from the
7348 minipool. ADDRESS is the offset of the insn since the start of the
7349 function; LOC is a pointer to the part of the insn which requires
7350 fixing; VALUE is the constant that must be loaded, which is of type
7351 MODE. */
7352 static void
7353 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7354 enum machine_mode mode, rtx value)
7356 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7358 #ifdef AOF_ASSEMBLER
7359 /* PIC symbol references need to be converted into offsets into the
7360 based area. */
7361 /* XXX This shouldn't be done here. */
7362 if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7363 value = aof_pic_entry (value);
7364 #endif /* AOF_ASSEMBLER */
7366 fix->insn = insn;
7367 fix->address = address;
7368 fix->loc = loc;
7369 fix->mode = mode;
7370 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7371 fix->value = value;
7372 fix->forwards = get_attr_pool_range (insn);
7373 fix->backwards = get_attr_neg_pool_range (insn);
7374 fix->minipool = NULL;
7376 /* If an insn doesn't have a range defined for it, then it isn't
7377 expecting to be reworked by this code. Better to abort now than
7378 to generate duff assembly code. */
7379 if (fix->forwards == 0 && fix->backwards == 0)
7380 abort ();
7382 /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7383 So there might be an empty word before the start of the pool.
7384 Hence we reduce the forward range by 4 to allow for this
7385 possibility. */
7386 if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7387 fix->forwards -= 4;
7389 if (dump_file)
7391 fprintf (dump_file,
7392 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7393 GET_MODE_NAME (mode),
7394 INSN_UID (insn), (unsigned long) address,
7395 -1 * (long)fix->backwards, (long)fix->forwards);
7396 arm_print_value (dump_file, fix->value);
7397 fprintf (dump_file, "\n");
7400 /* Add it to the chain of fixes. */
7401 fix->next = NULL;
7403 if (minipool_fix_head != NULL)
7404 minipool_fix_tail->next = fix;
7405 else
7406 minipool_fix_head = fix;
7408 minipool_fix_tail = fix;
7411 /* Return the cost of synthesizing a 64-bit constant VAL inline.
7412 Returns the number of insns needed, or 99 if we don't know how to
7413 do it. */
7415 arm_const_double_inline_cost (rtx val)
7417 rtx lowpart, highpart;
7418 enum machine_mode mode;
7420 mode = GET_MODE (val);
7422 if (mode == VOIDmode)
7423 mode = DImode;
7425 gcc_assert (GET_MODE_SIZE (mode) == 8);
7427 lowpart = gen_lowpart (SImode, val);
7428 highpart = gen_highpart_mode (SImode, mode, val);
7430 gcc_assert (GET_CODE (lowpart) == CONST_INT);
7431 gcc_assert (GET_CODE (highpart) == CONST_INT);
7433 return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
7434 NULL_RTX, NULL_RTX, 0, 0)
7435 + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
7436 NULL_RTX, NULL_RTX, 0, 0));
7439 /* Return true if it is worthwhile to split a 64-bit constant into two
7440 32-bit operations. This is the case if optimizing for size, or
7441 if we have load delay slots, or if one 32-bit part can be done with
7442 a single data operation. */
7443 bool
7444 arm_const_double_by_parts (rtx val)
7446 enum machine_mode mode = GET_MODE (val);
7447 rtx part;
7449 if (optimize_size || arm_ld_sched)
7450 return true;
7452 if (mode == VOIDmode)
7453 mode = DImode;
7455 part = gen_highpart_mode (SImode, mode, val);
7457 gcc_assert (GET_CODE (part) == CONST_INT);
7459 if (const_ok_for_arm (INTVAL (part))
7460 || const_ok_for_arm (~INTVAL (part)))
7461 return true;
7463 part = gen_lowpart (SImode, val);
7465 gcc_assert (GET_CODE (part) == CONST_INT);
7467 if (const_ok_for_arm (INTVAL (part))
7468 || const_ok_for_arm (~INTVAL (part)))
7469 return true;
7471 return false;
7474 /* Scan INSN and note any of its operands that need fixing.
7475 If DO_PUSHES is false we do not actually push any of the fixups
7476 needed. The function returns TRUE if any fixups were needed/pushed.
7477 This is used by arm_memory_load_p() which needs to know about loads
7478 of constants that will be converted into minipool loads. */
7479 static bool
7480 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7482 bool result = false;
7483 int opno;
7485 extract_insn (insn);
7487 if (!constrain_operands (1))
7488 fatal_insn_not_found (insn);
7490 if (recog_data.n_alternatives == 0)
7491 return false;
7493 /* Fill in recog_op_alt with information about the constraints of
7494 this insn. */
7495 preprocess_constraints ();
7497 for (opno = 0; opno < recog_data.n_operands; opno++)
7499 /* Things we need to fix can only occur in inputs. */
7500 if (recog_data.operand_type[opno] != OP_IN)
7501 continue;
7503 /* If this alternative is a memory reference, then any mention
7504 of constants in this alternative is really to fool reload
7505 into allowing us to accept one there. We need to fix them up
7506 now so that we output the right code. */
7507 if (recog_op_alt[opno][which_alternative].memory_ok)
7509 rtx op = recog_data.operand[opno];
7511 if (CONSTANT_P (op))
7513 if (do_pushes)
7514 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7515 recog_data.operand_mode[opno], op);
7516 result = true;
7518 else if (GET_CODE (op) == MEM
7519 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7520 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7522 if (do_pushes)
7524 rtx cop = avoid_constant_pool_reference (op);
7526 /* Casting the address of something to a mode narrower
7527 than a word can cause avoid_constant_pool_reference()
7528 to return the pool reference itself. That's no good to
7529 us here. Lets just hope that we can use the
7530 constant pool value directly. */
7531 if (op == cop)
7532 cop = get_pool_constant (XEXP (op, 0));
7534 push_minipool_fix (insn, address,
7535 recog_data.operand_loc[opno],
7536 recog_data.operand_mode[opno], cop);
7539 result = true;
7544 return result;
7547 /* Gcc puts the pool in the wrong place for ARM, since we can only
7548 load addresses a limited distance around the pc. We do some
7549 special munging to move the constant pool values to the correct
7550 point in the code. */
7551 static void
7552 arm_reorg (void)
7554 rtx insn;
7555 HOST_WIDE_INT address = 0;
7556 Mfix * fix;
7558 minipool_fix_head = minipool_fix_tail = NULL;
7560 /* The first insn must always be a note, or the code below won't
7561 scan it properly. */
7562 insn = get_insns ();
7563 if (GET_CODE (insn) != NOTE)
7564 abort ();
7566 /* Scan all the insns and record the operands that will need fixing. */
7567 for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7569 if (TARGET_CIRRUS_FIX_INVALID_INSNS
7570 && (arm_cirrus_insn_p (insn)
7571 || GET_CODE (insn) == JUMP_INSN
7572 || arm_memory_load_p (insn)))
7573 cirrus_reorg (insn);
7575 if (GET_CODE (insn) == BARRIER)
7576 push_minipool_barrier (insn, address);
7577 else if (INSN_P (insn))
7579 rtx table;
7581 note_invalid_constants (insn, address, true);
7582 address += get_attr_length (insn);
7584 /* If the insn is a vector jump, add the size of the table
7585 and skip the table. */
7586 if ((table = is_jump_table (insn)) != NULL)
7588 address += get_jump_table_size (table);
7589 insn = table;
7594 fix = minipool_fix_head;
7596 /* Now scan the fixups and perform the required changes. */
7597 while (fix)
7599 Mfix * ftmp;
7600 Mfix * fdel;
7601 Mfix * last_added_fix;
7602 Mfix * last_barrier = NULL;
7603 Mfix * this_fix;
7605 /* Skip any further barriers before the next fix. */
7606 while (fix && GET_CODE (fix->insn) == BARRIER)
7607 fix = fix->next;
7609 /* No more fixes. */
7610 if (fix == NULL)
7611 break;
7613 last_added_fix = NULL;
7615 for (ftmp = fix; ftmp; ftmp = ftmp->next)
7617 if (GET_CODE (ftmp->insn) == BARRIER)
7619 if (ftmp->address >= minipool_vector_head->max_address)
7620 break;
7622 last_barrier = ftmp;
7624 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7625 break;
7627 last_added_fix = ftmp; /* Keep track of the last fix added. */
7630 /* If we found a barrier, drop back to that; any fixes that we
7631 could have reached but come after the barrier will now go in
7632 the next mini-pool. */
7633 if (last_barrier != NULL)
7635 /* Reduce the refcount for those fixes that won't go into this
7636 pool after all. */
7637 for (fdel = last_barrier->next;
7638 fdel && fdel != ftmp;
7639 fdel = fdel->next)
7641 fdel->minipool->refcount--;
7642 fdel->minipool = NULL;
7645 ftmp = last_barrier;
7647 else
7649 /* ftmp is first fix that we can't fit into this pool and
7650 there no natural barriers that we could use. Insert a
7651 new barrier in the code somewhere between the previous
7652 fix and this one, and arrange to jump around it. */
7653 HOST_WIDE_INT max_address;
7655 /* The last item on the list of fixes must be a barrier, so
7656 we can never run off the end of the list of fixes without
7657 last_barrier being set. */
7658 if (ftmp == NULL)
7659 abort ();
7661 max_address = minipool_vector_head->max_address;
7662 /* Check that there isn't another fix that is in range that
7663 we couldn't fit into this pool because the pool was
7664 already too large: we need to put the pool before such an
7665 instruction. */
7666 if (ftmp->address < max_address)
7667 max_address = ftmp->address;
7669 last_barrier = create_fix_barrier (last_added_fix, max_address);
7672 assign_minipool_offsets (last_barrier);
7674 while (ftmp)
7676 if (GET_CODE (ftmp->insn) != BARRIER
7677 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7678 == NULL))
7679 break;
7681 ftmp = ftmp->next;
7684 /* Scan over the fixes we have identified for this pool, fixing them
7685 up and adding the constants to the pool itself. */
7686 for (this_fix = fix; this_fix && ftmp != this_fix;
7687 this_fix = this_fix->next)
7688 if (GET_CODE (this_fix->insn) != BARRIER)
7690 rtx addr
7691 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
7692 minipool_vector_label),
7693 this_fix->minipool->offset);
7694 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7697 dump_minipool (last_barrier->insn);
7698 fix = ftmp;
7701 /* From now on we must synthesize any constants that we can't handle
7702 directly. This can happen if the RTL gets split during final
7703 instruction generation. */
7704 after_arm_reorg = 1;
7706 /* Free the minipool memory. */
7707 obstack_free (&minipool_obstack, minipool_startobj);
7710 /* Routines to output assembly language. */
7712 /* If the rtx is the correct value then return the string of the number.
7713 In this way we can ensure that valid double constants are generated even
7714 when cross compiling. */
7715 const char *
7716 fp_immediate_constant (rtx x)
7718 REAL_VALUE_TYPE r;
7719 int i;
7721 if (!fp_consts_inited)
7722 init_fp_table ();
7724 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7725 for (i = 0; i < 8; i++)
7726 if (REAL_VALUES_EQUAL (r, values_fp[i]))
7727 return strings_fp[i];
7729 abort ();
7732 /* As for fp_immediate_constant, but value is passed directly, not in rtx. */
7733 static const char *
7734 fp_const_from_val (REAL_VALUE_TYPE *r)
7736 int i;
7738 if (!fp_consts_inited)
7739 init_fp_table ();
7741 for (i = 0; i < 8; i++)
7742 if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7743 return strings_fp[i];
7745 abort ();
7748 /* Output the operands of a LDM/STM instruction to STREAM.
7749 MASK is the ARM register set mask of which only bits 0-15 are important.
7750 REG is the base register, either the frame pointer or the stack pointer,
7751 INSTR is the possibly suffixed load or store instruction. */
7753 static void
7754 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
7755 unsigned long mask)
7757 unsigned i;
7758 bool not_first = FALSE;
7760 fputc ('\t', stream);
7761 asm_fprintf (stream, instr, reg);
7762 fputs (", {", stream);
7764 for (i = 0; i <= LAST_ARM_REGNUM; i++)
7765 if (mask & (1 << i))
7767 if (not_first)
7768 fprintf (stream, ", ");
7770 asm_fprintf (stream, "%r", i);
7771 not_first = TRUE;
7774 fprintf (stream, "}\n");
7778 /* Output a FLDMX instruction to STREAM.
7779 BASE if the register containing the address.
7780 REG and COUNT specify the register range.
7781 Extra registers may be added to avoid hardware bugs. */
7783 static void
7784 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7786 int i;
7788 /* Workaround ARM10 VFPr1 bug. */
7789 if (count == 2 && !arm_arch6)
7791 if (reg == 15)
7792 reg--;
7793 count++;
7796 fputc ('\t', stream);
7797 asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7799 for (i = reg; i < reg + count; i++)
7801 if (i > reg)
7802 fputs (", ", stream);
7803 asm_fprintf (stream, "d%d", i);
7805 fputs ("}\n", stream);
7810 /* Output the assembly for a store multiple. */
7812 const char *
7813 vfp_output_fstmx (rtx * operands)
7815 char pattern[100];
7816 int p;
7817 int base;
7818 int i;
7820 strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7821 p = strlen (pattern);
7823 if (GET_CODE (operands[1]) != REG)
7824 abort ();
7826 base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7827 for (i = 1; i < XVECLEN (operands[2], 0); i++)
7829 p += sprintf (&pattern[p], ", d%d", base + i);
7831 strcpy (&pattern[p], "}");
7833 output_asm_insn (pattern, operands);
7834 return "";
7838 /* Emit RTL to save block of VFP register pairs to the stack. Returns the
7839 number of bytes pushed. */
7841 static int
7842 vfp_emit_fstmx (int base_reg, int count)
7844 rtx par;
7845 rtx dwarf;
7846 rtx tmp, reg;
7847 int i;
7849 /* Workaround ARM10 VFPr1 bug. Data corruption can occur when exactly two
7850 register pairs are stored by a store multiple insn. We avoid this
7851 by pushing an extra pair. */
7852 if (count == 2 && !arm_arch6)
7854 if (base_reg == LAST_VFP_REGNUM - 3)
7855 base_reg -= 2;
7856 count++;
7859 /* ??? The frame layout is implementation defined. We describe
7860 standard format 1 (equivalent to a FSTMD insn and unused pad word).
7861 We really need some way of representing the whole block so that the
7862 unwinder can figure it out at runtime. */
7863 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7864 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
7866 reg = gen_rtx_REG (DFmode, base_reg);
7867 base_reg += 2;
7869 XVECEXP (par, 0, 0)
7870 = gen_rtx_SET (VOIDmode,
7871 gen_rtx_MEM (BLKmode,
7872 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7873 gen_rtx_UNSPEC (BLKmode,
7874 gen_rtvec (1, reg),
7875 UNSPEC_PUSH_MULT));
7877 tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7878 gen_rtx_PLUS (SImode, stack_pointer_rtx,
7879 GEN_INT (-(count * 8 + 4))));
7880 RTX_FRAME_RELATED_P (tmp) = 1;
7881 XVECEXP (dwarf, 0, 0) = tmp;
7883 tmp = gen_rtx_SET (VOIDmode,
7884 gen_rtx_MEM (DFmode, stack_pointer_rtx),
7885 reg);
7886 RTX_FRAME_RELATED_P (tmp) = 1;
7887 XVECEXP (dwarf, 0, 1) = tmp;
7889 for (i = 1; i < count; i++)
7891 reg = gen_rtx_REG (DFmode, base_reg);
7892 base_reg += 2;
7893 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
7895 tmp = gen_rtx_SET (VOIDmode,
7896 gen_rtx_MEM (DFmode,
7897 gen_rtx_PLUS (SImode,
7898 stack_pointer_rtx,
7899 GEN_INT (i * 8))),
7900 reg);
7901 RTX_FRAME_RELATED_P (tmp) = 1;
7902 XVECEXP (dwarf, 0, i + 1) = tmp;
7905 par = emit_insn (par);
7906 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7907 REG_NOTES (par));
7908 RTX_FRAME_RELATED_P (par) = 1;
7910 return count * 8 + 4;
7914 /* Output a 'call' insn. */
7915 const char *
7916 output_call (rtx *operands)
7918 if (arm_arch5)
7919 abort (); /* Patterns should call blx <reg> directly. */
7921 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
7922 if (REGNO (operands[0]) == LR_REGNUM)
7924 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
7925 output_asm_insn ("mov%?\t%0, %|lr", operands);
7928 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7930 if (TARGET_INTERWORK || arm_arch4t)
7931 output_asm_insn ("bx%?\t%0", operands);
7932 else
7933 output_asm_insn ("mov%?\t%|pc, %0", operands);
7935 return "";
7938 /* Output a 'call' insn that is a reference in memory. */
7939 const char *
7940 output_call_mem (rtx *operands)
7942 if (TARGET_INTERWORK && !arm_arch5)
7944 output_asm_insn ("ldr%?\t%|ip, %0", operands);
7945 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7946 output_asm_insn ("bx%?\t%|ip", operands);
7948 else if (regno_use_in (LR_REGNUM, operands[0]))
7950 /* LR is used in the memory address. We load the address in the
7951 first instruction. It's safe to use IP as the target of the
7952 load since the call will kill it anyway. */
7953 output_asm_insn ("ldr%?\t%|ip, %0", operands);
7954 if (arm_arch5)
7955 output_asm_insn ("blx%?\t%|ip", operands);
7956 else
7958 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7959 if (arm_arch4t)
7960 output_asm_insn ("bx%?\t%|ip", operands);
7961 else
7962 output_asm_insn ("mov%?\t%|pc, %|ip", operands);
7965 else
7967 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7968 output_asm_insn ("ldr%?\t%|pc, %0", operands);
7971 return "";
7975 /* Output a move from arm registers to an fpa registers.
7976 OPERANDS[0] is an fpa register.
7977 OPERANDS[1] is the first registers of an arm register pair. */
7978 const char *
7979 output_mov_long_double_fpa_from_arm (rtx *operands)
7981 int arm_reg0 = REGNO (operands[1]);
7982 rtx ops[3];
7984 if (arm_reg0 == IP_REGNUM)
7985 abort ();
7987 ops[0] = gen_rtx_REG (SImode, arm_reg0);
7988 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7989 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
7991 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
7992 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
7994 return "";
7997 /* Output a move from an fpa register to arm registers.
7998 OPERANDS[0] is the first registers of an arm register pair.
7999 OPERANDS[1] is an fpa register. */
8000 const char *
8001 output_mov_long_double_arm_from_fpa (rtx *operands)
8003 int arm_reg0 = REGNO (operands[0]);
8004 rtx ops[3];
8006 if (arm_reg0 == IP_REGNUM)
8007 abort ();
8009 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8010 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8011 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8013 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8014 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8015 return "";
8018 /* Output a move from arm registers to arm registers of a long double
8019 OPERANDS[0] is the destination.
8020 OPERANDS[1] is the source. */
8021 const char *
8022 output_mov_long_double_arm_from_arm (rtx *operands)
8024 /* We have to be careful here because the two might overlap. */
8025 int dest_start = REGNO (operands[0]);
8026 int src_start = REGNO (operands[1]);
8027 rtx ops[2];
8028 int i;
8030 if (dest_start < src_start)
8032 for (i = 0; i < 3; i++)
8034 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8035 ops[1] = gen_rtx_REG (SImode, src_start + i);
8036 output_asm_insn ("mov%?\t%0, %1", ops);
8039 else
8041 for (i = 2; i >= 0; i--)
8043 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8044 ops[1] = gen_rtx_REG (SImode, src_start + i);
8045 output_asm_insn ("mov%?\t%0, %1", ops);
8049 return "";
8053 /* Output a move from arm registers to an fpa registers.
8054 OPERANDS[0] is an fpa register.
8055 OPERANDS[1] is the first registers of an arm register pair. */
8056 const char *
8057 output_mov_double_fpa_from_arm (rtx *operands)
8059 int arm_reg0 = REGNO (operands[1]);
8060 rtx ops[2];
8062 if (arm_reg0 == IP_REGNUM)
8063 abort ();
8065 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8066 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8067 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8068 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8069 return "";
8072 /* Output a move from an fpa register to arm registers.
8073 OPERANDS[0] is the first registers of an arm register pair.
8074 OPERANDS[1] is an fpa register. */
8075 const char *
8076 output_mov_double_arm_from_fpa (rtx *operands)
8078 int arm_reg0 = REGNO (operands[0]);
8079 rtx ops[2];
8081 if (arm_reg0 == IP_REGNUM)
8082 abort ();
8084 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8085 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8086 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8087 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8088 return "";
8091 /* Output a move between double words.
8092 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8093 or MEM<-REG and all MEMs must be offsettable addresses. */
8094 const char *
8095 output_move_double (rtx *operands)
8097 enum rtx_code code0 = GET_CODE (operands[0]);
8098 enum rtx_code code1 = GET_CODE (operands[1]);
8099 rtx otherops[3];
8101 if (code0 == REG)
8103 int reg0 = REGNO (operands[0]);
8105 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8107 if (code1 == MEM)
8109 switch (GET_CODE (XEXP (operands[1], 0)))
8111 case REG:
8112 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8113 break;
8115 case PRE_INC:
8116 if (!TARGET_LDRD)
8117 abort (); /* Should never happen now. */
8118 output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8119 break;
8121 case PRE_DEC:
8122 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8123 break;
8125 case POST_INC:
8126 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8127 break;
8129 case POST_DEC:
8130 if (!TARGET_LDRD)
8131 abort (); /* Should never happen now. */
8132 output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8133 break;
8135 case PRE_MODIFY:
8136 case POST_MODIFY:
8137 otherops[0] = operands[0];
8138 otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8139 otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8141 if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8143 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8145 /* Registers overlap so split out the increment. */
8146 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8147 output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8149 else
8150 output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8152 else
8154 /* We only allow constant increments, so this is safe. */
8155 output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8157 break;
8159 case LABEL_REF:
8160 case CONST:
8161 output_asm_insn ("adr%?\t%0, %1", operands);
8162 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8163 break;
8165 default:
8166 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8167 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8169 otherops[0] = operands[0];
8170 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8171 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8173 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8175 if (GET_CODE (otherops[2]) == CONST_INT)
8177 switch ((int) INTVAL (otherops[2]))
8179 case -8:
8180 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8181 return "";
8182 case -4:
8183 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8184 return "";
8185 case 4:
8186 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8187 return "";
8190 if (TARGET_LDRD
8191 && (GET_CODE (otherops[2]) == REG
8192 || (GET_CODE (otherops[2]) == CONST_INT
8193 && INTVAL (otherops[2]) > -256
8194 && INTVAL (otherops[2]) < 256)))
8196 if (reg_overlap_mentioned_p (otherops[0],
8197 otherops[2]))
8199 /* Swap base and index registers over to
8200 avoid a conflict. */
8201 otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8202 otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8205 /* If both registers conflict, it will usually
8206 have been fixed by a splitter. */
8207 if (reg_overlap_mentioned_p (otherops[0],
8208 otherops[2]))
8210 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8211 output_asm_insn ("ldr%?d\t%0, [%1]",
8212 otherops);
8213 return "";
8215 else
8217 output_asm_insn ("ldr%?d\t%0, [%1, %2]",
8218 otherops);
8219 return "";
8222 if (GET_CODE (otherops[2]) == CONST_INT)
8224 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8225 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8226 else
8227 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8229 else
8230 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8232 else
8233 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8235 return "ldm%?ia\t%0, %M0";
8237 else
8239 otherops[1] = adjust_address (operands[1], SImode, 4);
8240 /* Take care of overlapping base/data reg. */
8241 if (reg_mentioned_p (operands[0], operands[1]))
8243 output_asm_insn ("ldr%?\t%0, %1", otherops);
8244 output_asm_insn ("ldr%?\t%0, %1", operands);
8246 else
8248 output_asm_insn ("ldr%?\t%0, %1", operands);
8249 output_asm_insn ("ldr%?\t%0, %1", otherops);
8254 else
8255 abort (); /* Constraints should prevent this. */
8257 else if (code0 == MEM && code1 == REG)
8259 if (REGNO (operands[1]) == IP_REGNUM)
8260 abort ();
8262 switch (GET_CODE (XEXP (operands[0], 0)))
8264 case REG:
8265 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8266 break;
8268 case PRE_INC:
8269 if (!TARGET_LDRD)
8270 abort (); /* Should never happen now. */
8271 output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8272 break;
8274 case PRE_DEC:
8275 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8276 break;
8278 case POST_INC:
8279 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8280 break;
8282 case POST_DEC:
8283 if (!TARGET_LDRD)
8284 abort (); /* Should never happen now. */
8285 output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8286 break;
8288 case PRE_MODIFY:
8289 case POST_MODIFY:
8290 otherops[0] = operands[1];
8291 otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8292 otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8294 if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8295 output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8296 else
8297 output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8298 break;
8300 case PLUS:
8301 otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8302 if (GET_CODE (otherops[2]) == CONST_INT)
8304 switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8306 case -8:
8307 output_asm_insn ("stm%?db\t%m0, %M1", operands);
8308 return "";
8310 case -4:
8311 output_asm_insn ("stm%?da\t%m0, %M1", operands);
8312 return "";
8314 case 4:
8315 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8316 return "";
8319 if (TARGET_LDRD
8320 && (GET_CODE (otherops[2]) == REG
8321 || (GET_CODE (otherops[2]) == CONST_INT
8322 && INTVAL (otherops[2]) > -256
8323 && INTVAL (otherops[2]) < 256)))
8325 otherops[0] = operands[1];
8326 otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8327 output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8328 return "";
8330 /* Fall through */
8332 default:
8333 otherops[0] = adjust_address (operands[0], SImode, 4);
8334 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8335 output_asm_insn ("str%?\t%1, %0", operands);
8336 output_asm_insn ("str%?\t%1, %0", otherops);
8339 else
8340 /* Constraints should prevent this. */
8341 abort ();
8343 return "";
8346 /* Output an ADD r, s, #n where n may be too big for one instruction.
8347 If adding zero to one register, output nothing. */
8348 const char *
8349 output_add_immediate (rtx *operands)
8351 HOST_WIDE_INT n = INTVAL (operands[2]);
8353 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8355 if (n < 0)
8356 output_multi_immediate (operands,
8357 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8358 -n);
8359 else
8360 output_multi_immediate (operands,
8361 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8365 return "";
8368 /* Output a multiple immediate operation.
8369 OPERANDS is the vector of operands referred to in the output patterns.
8370 INSTR1 is the output pattern to use for the first constant.
8371 INSTR2 is the output pattern to use for subsequent constants.
8372 IMMED_OP is the index of the constant slot in OPERANDS.
8373 N is the constant value. */
8374 static const char *
8375 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8376 int immed_op, HOST_WIDE_INT n)
8378 #if HOST_BITS_PER_WIDE_INT > 32
8379 n &= 0xffffffff;
8380 #endif
8382 if (n == 0)
8384 /* Quick and easy output. */
8385 operands[immed_op] = const0_rtx;
8386 output_asm_insn (instr1, operands);
8388 else
8390 int i;
8391 const char * instr = instr1;
8393 /* Note that n is never zero here (which would give no output). */
8394 for (i = 0; i < 32; i += 2)
8396 if (n & (3 << i))
8398 operands[immed_op] = GEN_INT (n & (255 << i));
8399 output_asm_insn (instr, operands);
8400 instr = instr2;
8401 i += 6;
8406 return "";
8409 /* Return the appropriate ARM instruction for the operation code.
8410 The returned result should not be overwritten. OP is the rtx of the
8411 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8412 was shifted. */
8413 const char *
8414 arithmetic_instr (rtx op, int shift_first_arg)
8416 switch (GET_CODE (op))
8418 case PLUS:
8419 return "add";
8421 case MINUS:
8422 return shift_first_arg ? "rsb" : "sub";
8424 case IOR:
8425 return "orr";
8427 case XOR:
8428 return "eor";
8430 case AND:
8431 return "and";
8433 default:
8434 abort ();
8438 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8439 for the operation code. The returned result should not be overwritten.
8440 OP is the rtx code of the shift.
8441 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8442 shift. */
8443 static const char *
8444 shift_op (rtx op, HOST_WIDE_INT *amountp)
8446 const char * mnem;
8447 enum rtx_code code = GET_CODE (op);
8449 if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
8450 *amountp = -1;
8451 else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
8452 *amountp = INTVAL (XEXP (op, 1));
8453 else
8454 abort ();
8456 switch (code)
8458 case ASHIFT:
8459 mnem = "asl";
8460 break;
8462 case ASHIFTRT:
8463 mnem = "asr";
8464 break;
8466 case LSHIFTRT:
8467 mnem = "lsr";
8468 break;
8470 case ROTATE:
8471 if (*amountp == -1)
8472 abort ();
8473 *amountp = 32 - *amountp;
8475 /* Fall through. */
8477 case ROTATERT:
8478 mnem = "ror";
8479 break;
8481 case MULT:
8482 /* We never have to worry about the amount being other than a
8483 power of 2, since this case can never be reloaded from a reg. */
8484 if (*amountp != -1)
8485 *amountp = int_log2 (*amountp);
8486 else
8487 abort ();
8488 return "asl";
8490 default:
8491 abort ();
8494 if (*amountp != -1)
8496 /* This is not 100% correct, but follows from the desire to merge
8497 multiplication by a power of 2 with the recognizer for a
8498 shift. >=32 is not a valid shift for "asl", so we must try and
8499 output a shift that produces the correct arithmetical result.
8500 Using lsr #32 is identical except for the fact that the carry bit
8501 is not set correctly if we set the flags; but we never use the
8502 carry bit from such an operation, so we can ignore that. */
8503 if (code == ROTATERT)
8504 /* Rotate is just modulo 32. */
8505 *amountp &= 31;
8506 else if (*amountp != (*amountp & 31))
8508 if (code == ASHIFT)
8509 mnem = "lsr";
8510 *amountp = 32;
8513 /* Shifts of 0 are no-ops. */
8514 if (*amountp == 0)
8515 return NULL;
8518 return mnem;
8521 /* Obtain the shift from the POWER of two. */
8523 static HOST_WIDE_INT
8524 int_log2 (HOST_WIDE_INT power)
8526 HOST_WIDE_INT shift = 0;
8528 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8530 if (shift > 31)
8531 abort ();
8532 shift++;
8535 return shift;
8538 /* Output a .ascii pseudo-op, keeping track of lengths. This is
8539 because /bin/as is horribly restrictive. The judgement about
8540 whether or not each character is 'printable' (and can be output as
8541 is) or not (and must be printed with an octal escape) must be made
8542 with reference to the *host* character set -- the situation is
8543 similar to that discussed in the comments above pp_c_char in
8544 c-pretty-print.c. */
8546 #define MAX_ASCII_LEN 51
8548 void
8549 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8551 int i;
8552 int len_so_far = 0;
8554 fputs ("\t.ascii\t\"", stream);
8556 for (i = 0; i < len; i++)
8558 int c = p[i];
8560 if (len_so_far >= MAX_ASCII_LEN)
8562 fputs ("\"\n\t.ascii\t\"", stream);
8563 len_so_far = 0;
8566 if (ISPRINT (c))
8568 if (c == '\\' || c == '\"')
8570 putc ('\\', stream);
8571 len_so_far++;
8573 putc (c, stream);
8574 len_so_far++;
8576 else
8578 fprintf (stream, "\\%03o", c);
8579 len_so_far += 4;
8583 fputs ("\"\n", stream);
8586 /* Compute the register save mask for registers 0 through 12
8587 inclusive. This code is used by arm_compute_save_reg_mask. */
8589 static unsigned long
8590 arm_compute_save_reg0_reg12_mask (void)
8592 unsigned long func_type = arm_current_func_type ();
8593 unsigned long save_reg_mask = 0;
8594 unsigned int reg;
8596 if (IS_INTERRUPT (func_type))
8598 unsigned int max_reg;
8599 /* Interrupt functions must not corrupt any registers,
8600 even call clobbered ones. If this is a leaf function
8601 we can just examine the registers used by the RTL, but
8602 otherwise we have to assume that whatever function is
8603 called might clobber anything, and so we have to save
8604 all the call-clobbered registers as well. */
8605 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8606 /* FIQ handlers have registers r8 - r12 banked, so
8607 we only need to check r0 - r7, Normal ISRs only
8608 bank r14 and r15, so we must check up to r12.
8609 r13 is the stack pointer which is always preserved,
8610 so we do not need to consider it here. */
8611 max_reg = 7;
8612 else
8613 max_reg = 12;
8615 for (reg = 0; reg <= max_reg; reg++)
8616 if (regs_ever_live[reg]
8617 || (! current_function_is_leaf && call_used_regs [reg]))
8618 save_reg_mask |= (1 << reg);
8620 /* Also save the pic base register if necessary. */
8621 if (flag_pic
8622 && !TARGET_SINGLE_PIC_BASE
8623 && current_function_uses_pic_offset_table)
8624 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8626 else
8628 /* In the normal case we only need to save those registers
8629 which are call saved and which are used by this function. */
8630 for (reg = 0; reg <= 10; reg++)
8631 if (regs_ever_live[reg] && ! call_used_regs [reg])
8632 save_reg_mask |= (1 << reg);
8634 /* Handle the frame pointer as a special case. */
8635 if (! TARGET_APCS_FRAME
8636 && ! frame_pointer_needed
8637 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8638 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8639 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8641 /* If we aren't loading the PIC register,
8642 don't stack it even though it may be live. */
8643 if (flag_pic
8644 && !TARGET_SINGLE_PIC_BASE
8645 && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
8646 || current_function_uses_pic_offset_table))
8647 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8650 /* Save registers so the exception handler can modify them. */
8651 if (current_function_calls_eh_return)
8653 unsigned int i;
8655 for (i = 0; ; i++)
8657 reg = EH_RETURN_DATA_REGNO (i);
8658 if (reg == INVALID_REGNUM)
8659 break;
8660 save_reg_mask |= 1 << reg;
8664 return save_reg_mask;
8667 /* Compute a bit mask of which registers need to be
8668 saved on the stack for the current function. */
8670 static unsigned long
8671 arm_compute_save_reg_mask (void)
8673 unsigned int save_reg_mask = 0;
8674 unsigned long func_type = arm_current_func_type ();
8676 if (IS_NAKED (func_type))
8677 /* This should never really happen. */
8678 return 0;
8680 /* If we are creating a stack frame, then we must save the frame pointer,
8681 IP (which will hold the old stack pointer), LR and the PC. */
8682 if (frame_pointer_needed)
8683 save_reg_mask |=
8684 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8685 | (1 << IP_REGNUM)
8686 | (1 << LR_REGNUM)
8687 | (1 << PC_REGNUM);
8689 /* Volatile functions do not return, so there
8690 is no need to save any other registers. */
8691 if (IS_VOLATILE (func_type))
8692 return save_reg_mask;
8694 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8696 /* Decide if we need to save the link register.
8697 Interrupt routines have their own banked link register,
8698 so they never need to save it.
8699 Otherwise if we do not use the link register we do not need to save
8700 it. If we are pushing other registers onto the stack however, we
8701 can save an instruction in the epilogue by pushing the link register
8702 now and then popping it back into the PC. This incurs extra memory
8703 accesses though, so we only do it when optimizing for size, and only
8704 if we know that we will not need a fancy return sequence. */
8705 if (regs_ever_live [LR_REGNUM]
8706 || (save_reg_mask
8707 && optimize_size
8708 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
8709 && !current_function_calls_eh_return))
8710 save_reg_mask |= 1 << LR_REGNUM;
8712 if (cfun->machine->lr_save_eliminated)
8713 save_reg_mask &= ~ (1 << LR_REGNUM);
8715 if (TARGET_REALLY_IWMMXT
8716 && ((bit_count (save_reg_mask)
8717 + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8719 unsigned int reg;
8721 /* The total number of registers that are going to be pushed
8722 onto the stack is odd. We need to ensure that the stack
8723 is 64-bit aligned before we start to save iWMMXt registers,
8724 and also before we start to create locals. (A local variable
8725 might be a double or long long which we will load/store using
8726 an iWMMXt instruction). Therefore we need to push another
8727 ARM register, so that the stack will be 64-bit aligned. We
8728 try to avoid using the arg registers (r0 -r3) as they might be
8729 used to pass values in a tail call. */
8730 for (reg = 4; reg <= 12; reg++)
8731 if ((save_reg_mask & (1 << reg)) == 0)
8732 break;
8734 if (reg <= 12)
8735 save_reg_mask |= (1 << reg);
8736 else
8738 cfun->machine->sibcall_blocked = 1;
8739 save_reg_mask |= (1 << 3);
8743 return save_reg_mask;
8747 /* Compute a bit mask of which registers need to be
8748 saved on the stack for the current function. */
8749 static unsigned long
8750 thumb_compute_save_reg_mask (void)
8752 unsigned long mask;
8753 unsigned reg;
8755 mask = 0;
8756 for (reg = 0; reg < 12; reg ++)
8757 if (regs_ever_live[reg] && !call_used_regs[reg])
8758 mask |= 1 << reg;
8760 if (flag_pic && !TARGET_SINGLE_PIC_BASE)
8761 mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
8763 if (TARGET_SINGLE_PIC_BASE)
8764 mask &= ~(1 << arm_pic_register);
8766 /* See if we might need r11 for calls to _interwork_r11_call_via_rN(). */
8767 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
8768 mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
8770 /* LR will also be pushed if any lo regs are pushed. */
8771 if (mask & 0xff || thumb_force_lr_save ())
8772 mask |= (1 << LR_REGNUM);
8774 /* Make sure we have a low work register if we need one.
8775 We will need one if we are going to push a high register,
8776 but we are not currently intending to push a low register. */
8777 if ((mask & 0xff) == 0
8778 && ((mask & 0x0f00) || TARGET_BACKTRACE))
8780 /* Use thumb_find_work_register to choose which register
8781 we will use. If the register is live then we will
8782 have to push it. Use LAST_LO_REGNUM as our fallback
8783 choice for the register to select. */
8784 reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
8786 if (! call_used_regs[reg])
8787 mask |= 1 << reg;
8790 return mask;
8794 /* Return the number of bytes required to save VFP registers. */
8795 static int
8796 arm_get_vfp_saved_size (void)
8798 unsigned int regno;
8799 int count;
8800 int saved;
8802 saved = 0;
8803 /* Space for saved VFP registers. */
8804 if (TARGET_HARD_FLOAT && TARGET_VFP)
8806 count = 0;
8807 for (regno = FIRST_VFP_REGNUM;
8808 regno < LAST_VFP_REGNUM;
8809 regno += 2)
8811 if ((!regs_ever_live[regno] || call_used_regs[regno])
8812 && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8814 if (count > 0)
8816 /* Workaround ARM10 VFPr1 bug. */
8817 if (count == 2 && !arm_arch6)
8818 count++;
8819 saved += count * 8 + 4;
8821 count = 0;
8823 else
8824 count++;
8826 if (count > 0)
8828 if (count == 2 && !arm_arch6)
8829 count++;
8830 saved += count * 8 + 4;
8833 return saved;
8837 /* Generate a function exit sequence. If REALLY_RETURN is false, then do
8838 everything bar the final return instruction. */
8839 const char *
8840 output_return_instruction (rtx operand, int really_return, int reverse)
8842 char conditional[10];
8843 char instr[100];
8844 unsigned reg;
8845 unsigned long live_regs_mask;
8846 unsigned long func_type;
8847 arm_stack_offsets *offsets;
8849 func_type = arm_current_func_type ();
8851 if (IS_NAKED (func_type))
8852 return "";
8854 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
8856 /* If this function was declared non-returning, and we have
8857 found a tail call, then we have to trust that the called
8858 function won't return. */
8859 if (really_return)
8861 rtx ops[2];
8863 /* Otherwise, trap an attempted return by aborting. */
8864 ops[0] = operand;
8865 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
8866 : "abort");
8867 assemble_external_libcall (ops[1]);
8868 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
8871 return "";
8874 if (current_function_calls_alloca && !really_return)
8875 abort ();
8877 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
8879 return_used_this_function = 1;
8881 live_regs_mask = arm_compute_save_reg_mask ();
8883 if (live_regs_mask)
8885 const char * return_reg;
8887 /* If we do not have any special requirements for function exit
8888 (e.g. interworking, or ISR) then we can load the return address
8889 directly into the PC. Otherwise we must load it into LR. */
8890 if (really_return
8891 && ! TARGET_INTERWORK)
8892 return_reg = reg_names[PC_REGNUM];
8893 else
8894 return_reg = reg_names[LR_REGNUM];
8896 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
8898 /* There are three possible reasons for the IP register
8899 being saved. 1) a stack frame was created, in which case
8900 IP contains the old stack pointer, or 2) an ISR routine
8901 corrupted it, or 3) it was saved to align the stack on
8902 iWMMXt. In case 1, restore IP into SP, otherwise just
8903 restore IP. */
8904 if (frame_pointer_needed)
8906 live_regs_mask &= ~ (1 << IP_REGNUM);
8907 live_regs_mask |= (1 << SP_REGNUM);
8909 else
8911 if (! IS_INTERRUPT (func_type)
8912 && ! TARGET_REALLY_IWMMXT)
8913 abort ();
8917 /* On some ARM architectures it is faster to use LDR rather than
8918 LDM to load a single register. On other architectures, the
8919 cost is the same. In 26 bit mode, or for exception handlers,
8920 we have to use LDM to load the PC so that the CPSR is also
8921 restored. */
8922 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
8923 if (live_regs_mask == (1U << reg))
8924 break;
8926 if (reg <= LAST_ARM_REGNUM
8927 && (reg != LR_REGNUM
8928 || ! really_return
8929 || ! IS_INTERRUPT (func_type)))
8931 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
8932 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
8934 else
8936 char *p;
8937 int first = 1;
8939 /* Generate the load multiple instruction to restore the
8940 registers. Note we can get here, even if
8941 frame_pointer_needed is true, but only if sp already
8942 points to the base of the saved core registers. */
8943 if (live_regs_mask & (1 << SP_REGNUM))
8945 unsigned HOST_WIDE_INT stack_adjust;
8947 offsets = arm_get_frame_offsets ();
8948 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
8949 if (stack_adjust != 0 && stack_adjust != 4)
8950 abort ();
8952 if (stack_adjust && arm_arch5)
8953 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
8954 else
8956 /* If we can't use ldmib (SA110 bug),
8957 then try to pop r3 instead. */
8958 if (stack_adjust)
8959 live_regs_mask |= 1 << 3;
8960 sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
8963 else
8964 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
8966 p = instr + strlen (instr);
8968 for (reg = 0; reg <= SP_REGNUM; reg++)
8969 if (live_regs_mask & (1 << reg))
8971 int l = strlen (reg_names[reg]);
8973 if (first)
8974 first = 0;
8975 else
8977 memcpy (p, ", ", 2);
8978 p += 2;
8981 memcpy (p, "%|", 2);
8982 memcpy (p + 2, reg_names[reg], l);
8983 p += l + 2;
8986 if (live_regs_mask & (1 << LR_REGNUM))
8988 sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
8989 /* If returning from an interrupt, restore the CPSR. */
8990 if (IS_INTERRUPT (func_type))
8991 strcat (p, "^");
8993 else
8994 strcpy (p, "}");
8997 output_asm_insn (instr, & operand);
8999 /* See if we need to generate an extra instruction to
9000 perform the actual function return. */
9001 if (really_return
9002 && func_type != ARM_FT_INTERWORKED
9003 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9005 /* The return has already been handled
9006 by loading the LR into the PC. */
9007 really_return = 0;
9011 if (really_return)
9013 switch ((int) ARM_FUNC_TYPE (func_type))
9015 case ARM_FT_ISR:
9016 case ARM_FT_FIQ:
9017 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9018 break;
9020 case ARM_FT_INTERWORKED:
9021 sprintf (instr, "bx%s\t%%|lr", conditional);
9022 break;
9024 case ARM_FT_EXCEPTION:
9025 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9026 break;
9028 default:
9029 /* Use bx if it's available. */
9030 if (arm_arch5 || arm_arch4t)
9031 sprintf (instr, "bx%s\t%%|lr", conditional);
9032 else
9033 sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9034 break;
9037 output_asm_insn (instr, & operand);
9040 return "";
9043 /* Write the function name into the code section, directly preceding
9044 the function prologue.
9046 Code will be output similar to this:
9048 .ascii "arm_poke_function_name", 0
9049 .align
9051 .word 0xff000000 + (t1 - t0)
9052 arm_poke_function_name
9053 mov ip, sp
9054 stmfd sp!, {fp, ip, lr, pc}
9055 sub fp, ip, #4
9057 When performing a stack backtrace, code can inspect the value
9058 of 'pc' stored at 'fp' + 0. If the trace function then looks
9059 at location pc - 12 and the top 8 bits are set, then we know
9060 that there is a function name embedded immediately preceding this
9061 location and has length ((pc[-3]) & 0xff000000).
9063 We assume that pc is declared as a pointer to an unsigned long.
9065 It is of no benefit to output the function name if we are assembling
9066 a leaf function. These function types will not contain a stack
9067 backtrace structure, therefore it is not possible to determine the
9068 function name. */
9069 void
9070 arm_poke_function_name (FILE *stream, const char *name)
9072 unsigned long alignlength;
9073 unsigned long length;
9074 rtx x;
9076 length = strlen (name) + 1;
9077 alignlength = ROUND_UP_WORD (length);
9079 ASM_OUTPUT_ASCII (stream, name, length);
9080 ASM_OUTPUT_ALIGN (stream, 2);
9081 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9082 assemble_aligned_integer (UNITS_PER_WORD, x);
9085 /* Place some comments into the assembler stream
9086 describing the current function. */
9087 static void
9088 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9090 unsigned long func_type;
9092 if (!TARGET_ARM)
9094 thumb_output_function_prologue (f, frame_size);
9095 return;
9098 /* Sanity check. */
9099 if (arm_ccfsm_state || arm_target_insn)
9100 abort ();
9102 func_type = arm_current_func_type ();
9104 switch ((int) ARM_FUNC_TYPE (func_type))
9106 default:
9107 case ARM_FT_NORMAL:
9108 break;
9109 case ARM_FT_INTERWORKED:
9110 asm_fprintf (f, "\t%@ Function supports interworking.\n");
9111 break;
9112 case ARM_FT_ISR:
9113 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9114 break;
9115 case ARM_FT_FIQ:
9116 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9117 break;
9118 case ARM_FT_EXCEPTION:
9119 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9120 break;
9123 if (IS_NAKED (func_type))
9124 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9126 if (IS_VOLATILE (func_type))
9127 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9129 if (IS_NESTED (func_type))
9130 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9132 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9133 current_function_args_size,
9134 current_function_pretend_args_size, frame_size);
9136 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9137 frame_pointer_needed,
9138 cfun->machine->uses_anonymous_args);
9140 if (cfun->machine->lr_save_eliminated)
9141 asm_fprintf (f, "\t%@ link register save eliminated.\n");
9143 if (current_function_calls_eh_return)
9144 asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9146 #ifdef AOF_ASSEMBLER
9147 if (flag_pic)
9148 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9149 #endif
9151 return_used_this_function = 0;
9154 const char *
9155 arm_output_epilogue (rtx sibling)
9157 int reg;
9158 unsigned long saved_regs_mask;
9159 unsigned long func_type;
9160 /* Floats_offset is the offset from the "virtual" frame. In an APCS
9161 frame that is $fp + 4 for a non-variadic function. */
9162 int floats_offset = 0;
9163 rtx operands[3];
9164 FILE * f = asm_out_file;
9165 unsigned int lrm_count = 0;
9166 int really_return = (sibling == NULL);
9167 int start_reg;
9168 arm_stack_offsets *offsets;
9170 /* If we have already generated the return instruction
9171 then it is futile to generate anything else. */
9172 if (use_return_insn (FALSE, sibling) && return_used_this_function)
9173 return "";
9175 func_type = arm_current_func_type ();
9177 if (IS_NAKED (func_type))
9178 /* Naked functions don't have epilogues. */
9179 return "";
9181 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9183 rtx op;
9185 /* A volatile function should never return. Call abort. */
9186 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9187 assemble_external_libcall (op);
9188 output_asm_insn ("bl\t%a0", &op);
9190 return "";
9193 if (current_function_calls_eh_return
9194 && ! really_return)
9195 /* If we are throwing an exception, then we really must
9196 be doing a return, so we can't tail-call. */
9197 abort ();
9199 offsets = arm_get_frame_offsets ();
9200 saved_regs_mask = arm_compute_save_reg_mask ();
9202 if (TARGET_IWMMXT)
9203 lrm_count = bit_count (saved_regs_mask);
9205 floats_offset = offsets->saved_args;
9206 /* Compute how far away the floats will be. */
9207 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9208 if (saved_regs_mask & (1 << reg))
9209 floats_offset += 4;
9211 if (frame_pointer_needed)
9213 /* This variable is for the Virtual Frame Pointer, not VFP regs. */
9214 int vfp_offset = offsets->frame;
9216 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9218 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9219 if (regs_ever_live[reg] && !call_used_regs[reg])
9221 floats_offset += 12;
9222 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9223 reg, FP_REGNUM, floats_offset - vfp_offset);
9226 else
9228 start_reg = LAST_FPA_REGNUM;
9230 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9232 if (regs_ever_live[reg] && !call_used_regs[reg])
9234 floats_offset += 12;
9236 /* We can't unstack more than four registers at once. */
9237 if (start_reg - reg == 3)
9239 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9240 reg, FP_REGNUM, floats_offset - vfp_offset);
9241 start_reg = reg - 1;
9244 else
9246 if (reg != start_reg)
9247 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9248 reg + 1, start_reg - reg,
9249 FP_REGNUM, floats_offset - vfp_offset);
9250 start_reg = reg - 1;
9254 /* Just in case the last register checked also needs unstacking. */
9255 if (reg != start_reg)
9256 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9257 reg + 1, start_reg - reg,
9258 FP_REGNUM, floats_offset - vfp_offset);
9261 if (TARGET_HARD_FLOAT && TARGET_VFP)
9263 int saved_size;
9265 /* The fldmx insn does not have base+offset addressing modes,
9266 so we use IP to hold the address. */
9267 saved_size = arm_get_vfp_saved_size ();
9269 if (saved_size > 0)
9271 floats_offset += saved_size;
9272 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9273 FP_REGNUM, floats_offset - vfp_offset);
9275 start_reg = FIRST_VFP_REGNUM;
9276 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9278 if ((!regs_ever_live[reg] || call_used_regs[reg])
9279 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9281 if (start_reg != reg)
9282 arm_output_fldmx (f, IP_REGNUM,
9283 (start_reg - FIRST_VFP_REGNUM) / 2,
9284 (reg - start_reg) / 2);
9285 start_reg = reg + 2;
9288 if (start_reg != reg)
9289 arm_output_fldmx (f, IP_REGNUM,
9290 (start_reg - FIRST_VFP_REGNUM) / 2,
9291 (reg - start_reg) / 2);
9294 if (TARGET_IWMMXT)
9296 /* The frame pointer is guaranteed to be non-double-word aligned.
9297 This is because it is set to (old_stack_pointer - 4) and the
9298 old_stack_pointer was double word aligned. Thus the offset to
9299 the iWMMXt registers to be loaded must also be non-double-word
9300 sized, so that the resultant address *is* double-word aligned.
9301 We can ignore floats_offset since that was already included in
9302 the live_regs_mask. */
9303 lrm_count += (lrm_count % 2 ? 2 : 1);
9305 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9306 if (regs_ever_live[reg] && !call_used_regs[reg])
9308 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9309 reg, FP_REGNUM, lrm_count * 4);
9310 lrm_count += 2;
9314 /* saved_regs_mask should contain the IP, which at the time of stack
9315 frame generation actually contains the old stack pointer. So a
9316 quick way to unwind the stack is just pop the IP register directly
9317 into the stack pointer. */
9318 if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
9319 abort ();
9320 saved_regs_mask &= ~ (1 << IP_REGNUM);
9321 saved_regs_mask |= (1 << SP_REGNUM);
9323 /* There are two registers left in saved_regs_mask - LR and PC. We
9324 only need to restore the LR register (the return address), but to
9325 save time we can load it directly into the PC, unless we need a
9326 special function exit sequence, or we are not really returning. */
9327 if (really_return
9328 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9329 && !current_function_calls_eh_return)
9330 /* Delete the LR from the register mask, so that the LR on
9331 the stack is loaded into the PC in the register mask. */
9332 saved_regs_mask &= ~ (1 << LR_REGNUM);
9333 else
9334 saved_regs_mask &= ~ (1 << PC_REGNUM);
9336 /* We must use SP as the base register, because SP is one of the
9337 registers being restored. If an interrupt or page fault
9338 happens in the ldm instruction, the SP might or might not
9339 have been restored. That would be bad, as then SP will no
9340 longer indicate the safe area of stack, and we can get stack
9341 corruption. Using SP as the base register means that it will
9342 be reset correctly to the original value, should an interrupt
9343 occur. If the stack pointer already points at the right
9344 place, then omit the subtraction. */
9345 if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9346 || current_function_calls_alloca)
9347 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9348 4 * bit_count (saved_regs_mask));
9349 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9351 if (IS_INTERRUPT (func_type))
9352 /* Interrupt handlers will have pushed the
9353 IP onto the stack, so restore it now. */
9354 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9356 else
9358 /* Restore stack pointer if necessary. */
9359 if (offsets->outgoing_args != offsets->saved_regs)
9361 operands[0] = operands[1] = stack_pointer_rtx;
9362 operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9363 output_add_immediate (operands);
9366 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9368 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9369 if (regs_ever_live[reg] && !call_used_regs[reg])
9370 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9371 reg, SP_REGNUM);
9373 else
9375 start_reg = FIRST_FPA_REGNUM;
9377 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9379 if (regs_ever_live[reg] && !call_used_regs[reg])
9381 if (reg - start_reg == 3)
9383 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9384 start_reg, SP_REGNUM);
9385 start_reg = reg + 1;
9388 else
9390 if (reg != start_reg)
9391 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9392 start_reg, reg - start_reg,
9393 SP_REGNUM);
9395 start_reg = reg + 1;
9399 /* Just in case the last register checked also needs unstacking. */
9400 if (reg != start_reg)
9401 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9402 start_reg, reg - start_reg, SP_REGNUM);
9405 if (TARGET_HARD_FLOAT && TARGET_VFP)
9407 start_reg = FIRST_VFP_REGNUM;
9408 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9410 if ((!regs_ever_live[reg] || call_used_regs[reg])
9411 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9413 if (start_reg != reg)
9414 arm_output_fldmx (f, SP_REGNUM,
9415 (start_reg - FIRST_VFP_REGNUM) / 2,
9416 (reg - start_reg) / 2);
9417 start_reg = reg + 2;
9420 if (start_reg != reg)
9421 arm_output_fldmx (f, SP_REGNUM,
9422 (start_reg - FIRST_VFP_REGNUM) / 2,
9423 (reg - start_reg) / 2);
9425 if (TARGET_IWMMXT)
9426 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9427 if (regs_ever_live[reg] && !call_used_regs[reg])
9428 asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9430 /* If we can, restore the LR into the PC. */
9431 if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9432 && really_return
9433 && current_function_pretend_args_size == 0
9434 && saved_regs_mask & (1 << LR_REGNUM)
9435 && !current_function_calls_eh_return)
9437 saved_regs_mask &= ~ (1 << LR_REGNUM);
9438 saved_regs_mask |= (1 << PC_REGNUM);
9441 /* Load the registers off the stack. If we only have one register
9442 to load use the LDR instruction - it is faster. */
9443 if (saved_regs_mask == (1 << LR_REGNUM))
9445 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9447 else if (saved_regs_mask)
9449 if (saved_regs_mask & (1 << SP_REGNUM))
9450 /* Note - write back to the stack register is not enabled
9451 (i.e. "ldmfd sp!..."). We know that the stack pointer is
9452 in the list of registers and if we add writeback the
9453 instruction becomes UNPREDICTABLE. */
9454 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9455 else
9456 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9459 if (current_function_pretend_args_size)
9461 /* Unwind the pre-pushed regs. */
9462 operands[0] = operands[1] = stack_pointer_rtx;
9463 operands[2] = GEN_INT (current_function_pretend_args_size);
9464 output_add_immediate (operands);
9468 /* We may have already restored PC directly from the stack. */
9469 if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
9470 return "";
9472 /* Stack adjustment for exception handler. */
9473 if (current_function_calls_eh_return)
9474 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
9475 ARM_EH_STACKADJ_REGNUM);
9477 /* Generate the return instruction. */
9478 switch ((int) ARM_FUNC_TYPE (func_type))
9480 case ARM_FT_ISR:
9481 case ARM_FT_FIQ:
9482 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9483 break;
9485 case ARM_FT_EXCEPTION:
9486 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9487 break;
9489 case ARM_FT_INTERWORKED:
9490 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9491 break;
9493 default:
9494 if (arm_arch5 || arm_arch4t)
9495 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9496 else
9497 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9498 break;
9501 return "";
9504 static void
9505 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9506 HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9508 arm_stack_offsets *offsets;
9510 if (TARGET_THUMB)
9512 int regno;
9514 /* Emit any call-via-reg trampolines that are needed for v4t support
9515 of call_reg and call_value_reg type insns. */
9516 for (regno = 0; regno < LR_REGNUM; regno++)
9518 rtx label = cfun->machine->call_via[regno];
9520 if (label != NULL)
9522 function_section (current_function_decl);
9523 targetm.asm_out.internal_label (asm_out_file, "L",
9524 CODE_LABEL_NUMBER (label));
9525 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
9529 /* ??? Probably not safe to set this here, since it assumes that a
9530 function will be emitted as assembly immediately after we generate
9531 RTL for it. This does not happen for inline functions. */
9532 return_used_this_function = 0;
9534 else
9536 /* We need to take into account any stack-frame rounding. */
9537 offsets = arm_get_frame_offsets ();
9539 if (use_return_insn (FALSE, NULL)
9540 && return_used_this_function
9541 && offsets->saved_regs != offsets->outgoing_args
9542 && !frame_pointer_needed)
9543 abort ();
9545 /* Reset the ARM-specific per-function variables. */
9546 after_arm_reorg = 0;
9550 /* Generate and emit an insn that we will recognize as a push_multi.
9551 Unfortunately, since this insn does not reflect very well the actual
9552 semantics of the operation, we need to annotate the insn for the benefit
9553 of DWARF2 frame unwind information. */
9554 static rtx
9555 emit_multi_reg_push (unsigned long mask)
9557 int num_regs = 0;
9558 int num_dwarf_regs;
9559 int i, j;
9560 rtx par;
9561 rtx dwarf;
9562 int dwarf_par_index;
9563 rtx tmp, reg;
9565 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9566 if (mask & (1 << i))
9567 num_regs++;
9569 if (num_regs == 0 || num_regs > 16)
9570 abort ();
9572 /* We don't record the PC in the dwarf frame information. */
9573 num_dwarf_regs = num_regs;
9574 if (mask & (1 << PC_REGNUM))
9575 num_dwarf_regs--;
9577 /* For the body of the insn we are going to generate an UNSPEC in
9578 parallel with several USEs. This allows the insn to be recognized
9579 by the push_multi pattern in the arm.md file. The insn looks
9580 something like this:
9582 (parallel [
9583 (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9584 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9585 (use (reg:SI 11 fp))
9586 (use (reg:SI 12 ip))
9587 (use (reg:SI 14 lr))
9588 (use (reg:SI 15 pc))
9591 For the frame note however, we try to be more explicit and actually
9592 show each register being stored into the stack frame, plus a (single)
9593 decrement of the stack pointer. We do it this way in order to be
9594 friendly to the stack unwinding code, which only wants to see a single
9595 stack decrement per instruction. The RTL we generate for the note looks
9596 something like this:
9598 (sequence [
9599 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9600 (set (mem:SI (reg:SI sp)) (reg:SI r4))
9601 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9602 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9603 (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9606 This sequence is used both by the code to support stack unwinding for
9607 exceptions handlers and the code to generate dwarf2 frame debugging. */
9609 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9610 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9611 dwarf_par_index = 1;
9613 for (i = 0; i <= LAST_ARM_REGNUM; i++)
9615 if (mask & (1 << i))
9617 reg = gen_rtx_REG (SImode, i);
9619 XVECEXP (par, 0, 0)
9620 = gen_rtx_SET (VOIDmode,
9621 gen_rtx_MEM (BLKmode,
9622 gen_rtx_PRE_DEC (BLKmode,
9623 stack_pointer_rtx)),
9624 gen_rtx_UNSPEC (BLKmode,
9625 gen_rtvec (1, reg),
9626 UNSPEC_PUSH_MULT));
9628 if (i != PC_REGNUM)
9630 tmp = gen_rtx_SET (VOIDmode,
9631 gen_rtx_MEM (SImode, stack_pointer_rtx),
9632 reg);
9633 RTX_FRAME_RELATED_P (tmp) = 1;
9634 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9635 dwarf_par_index++;
9638 break;
9642 for (j = 1, i++; j < num_regs; i++)
9644 if (mask & (1 << i))
9646 reg = gen_rtx_REG (SImode, i);
9648 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9650 if (i != PC_REGNUM)
9652 tmp = gen_rtx_SET (VOIDmode,
9653 gen_rtx_MEM (SImode,
9654 plus_constant (stack_pointer_rtx,
9655 4 * j)),
9656 reg);
9657 RTX_FRAME_RELATED_P (tmp) = 1;
9658 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9661 j++;
9665 par = emit_insn (par);
9667 tmp = gen_rtx_SET (SImode,
9668 stack_pointer_rtx,
9669 gen_rtx_PLUS (SImode,
9670 stack_pointer_rtx,
9671 GEN_INT (-4 * num_regs)));
9672 RTX_FRAME_RELATED_P (tmp) = 1;
9673 XVECEXP (dwarf, 0, 0) = tmp;
9675 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9676 REG_NOTES (par));
9677 return par;
9680 static rtx
9681 emit_sfm (int base_reg, int count)
9683 rtx par;
9684 rtx dwarf;
9685 rtx tmp, reg;
9686 int i;
9688 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9689 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9691 reg = gen_rtx_REG (XFmode, base_reg++);
9693 XVECEXP (par, 0, 0)
9694 = gen_rtx_SET (VOIDmode,
9695 gen_rtx_MEM (BLKmode,
9696 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9697 gen_rtx_UNSPEC (BLKmode,
9698 gen_rtvec (1, reg),
9699 UNSPEC_PUSH_MULT));
9700 tmp = gen_rtx_SET (VOIDmode,
9701 gen_rtx_MEM (XFmode, stack_pointer_rtx), reg);
9702 RTX_FRAME_RELATED_P (tmp) = 1;
9703 XVECEXP (dwarf, 0, 1) = tmp;
9705 for (i = 1; i < count; i++)
9707 reg = gen_rtx_REG (XFmode, base_reg++);
9708 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9710 tmp = gen_rtx_SET (VOIDmode,
9711 gen_rtx_MEM (XFmode,
9712 plus_constant (stack_pointer_rtx,
9713 i * 12)),
9714 reg);
9715 RTX_FRAME_RELATED_P (tmp) = 1;
9716 XVECEXP (dwarf, 0, i + 1) = tmp;
9719 tmp = gen_rtx_SET (VOIDmode,
9720 stack_pointer_rtx,
9721 gen_rtx_PLUS (SImode,
9722 stack_pointer_rtx,
9723 GEN_INT (-12 * count)));
9724 RTX_FRAME_RELATED_P (tmp) = 1;
9725 XVECEXP (dwarf, 0, 0) = tmp;
9727 par = emit_insn (par);
9728 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9729 REG_NOTES (par));
9730 return par;
9734 /* Return true if the current function needs to save/restore LR. */
9736 static bool
9737 thumb_force_lr_save (void)
9739 return !cfun->machine->lr_save_eliminated
9740 && (!leaf_function_p ()
9741 || thumb_far_jump_used_p ()
9742 || regs_ever_live [LR_REGNUM]);
9746 /* Compute the distance from register FROM to register TO.
9747 These can be the arg pointer (26), the soft frame pointer (25),
9748 the stack pointer (13) or the hard frame pointer (11).
9749 In thumb mode r7 is used as the soft frame pointer, if needed.
9750 Typical stack layout looks like this:
9752 old stack pointer -> | |
9753 ----
9754 | | \
9755 | | saved arguments for
9756 | | vararg functions
9757 | | /
9759 hard FP & arg pointer -> | | \
9760 | | stack
9761 | | frame
9762 | | /
9764 | | \
9765 | | call saved
9766 | | registers
9767 soft frame pointer -> | | /
9769 | | \
9770 | | local
9771 | | variables
9772 | | /
9774 | | \
9775 | | outgoing
9776 | | arguments
9777 current stack pointer -> | | /
9780 For a given function some or all of these stack components
9781 may not be needed, giving rise to the possibility of
9782 eliminating some of the registers.
9784 The values returned by this function must reflect the behavior
9785 of arm_expand_prologue() and arm_compute_save_reg_mask().
9787 The sign of the number returned reflects the direction of stack
9788 growth, so the values are positive for all eliminations except
9789 from the soft frame pointer to the hard frame pointer.
9791 SFP may point just inside the local variables block to ensure correct
9792 alignment. */
9795 /* Calculate stack offsets. These are used to calculate register elimination
9796 offsets and in prologue/epilogue code. */
9798 static arm_stack_offsets *
9799 arm_get_frame_offsets (void)
9801 struct arm_stack_offsets *offsets;
9802 unsigned long func_type;
9803 int leaf;
9804 int saved;
9805 HOST_WIDE_INT frame_size;
9807 offsets = &cfun->machine->stack_offsets;
9809 /* We need to know if we are a leaf function. Unfortunately, it
9810 is possible to be called after start_sequence has been called,
9811 which causes get_insns to return the insns for the sequence,
9812 not the function, which will cause leaf_function_p to return
9813 the incorrect result.
9815 to know about leaf functions once reload has completed, and the
9816 frame size cannot be changed after that time, so we can safely
9817 use the cached value. */
9819 if (reload_completed)
9820 return offsets;
9822 /* Initially this is the size of the local variables. It will translated
9823 into an offset once we have determined the size of preceding data. */
9824 frame_size = ROUND_UP_WORD (get_frame_size ());
9826 leaf = leaf_function_p ();
9828 /* Space for variadic functions. */
9829 offsets->saved_args = current_function_pretend_args_size;
9831 offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
9833 if (TARGET_ARM)
9835 unsigned int regno;
9837 saved = bit_count (arm_compute_save_reg_mask ()) * 4;
9839 /* We know that SP will be doubleword aligned on entry, and we must
9840 preserve that condition at any subroutine call. We also require the
9841 soft frame pointer to be doubleword aligned. */
9843 if (TARGET_REALLY_IWMMXT)
9845 /* Check for the call-saved iWMMXt registers. */
9846 for (regno = FIRST_IWMMXT_REGNUM;
9847 regno <= LAST_IWMMXT_REGNUM;
9848 regno++)
9849 if (regs_ever_live [regno] && ! call_used_regs [regno])
9850 saved += 8;
9853 func_type = arm_current_func_type ();
9854 if (! IS_VOLATILE (func_type))
9856 /* Space for saved FPA registers. */
9857 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
9858 if (regs_ever_live[regno] && ! call_used_regs[regno])
9859 saved += 12;
9861 /* Space for saved VFP registers. */
9862 if (TARGET_HARD_FLOAT && TARGET_VFP)
9863 saved += arm_get_vfp_saved_size ();
9866 else /* TARGET_THUMB */
9868 saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
9869 if (TARGET_BACKTRACE)
9870 saved += 16;
9873 /* Saved registers include the stack frame. */
9874 offsets->saved_regs = offsets->saved_args + saved;
9875 offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
9876 /* A leaf function does not need any stack alignment if it has nothing
9877 on the stack. */
9878 if (leaf && frame_size == 0)
9880 offsets->outgoing_args = offsets->soft_frame;
9881 return offsets;
9884 /* Ensure SFP has the correct alignment. */
9885 if (ARM_DOUBLEWORD_ALIGN
9886 && (offsets->soft_frame & 7))
9887 offsets->soft_frame += 4;
9889 offsets->outgoing_args = offsets->soft_frame + frame_size
9890 + current_function_outgoing_args_size;
9892 if (ARM_DOUBLEWORD_ALIGN)
9894 /* Ensure SP remains doubleword aligned. */
9895 if (offsets->outgoing_args & 7)
9896 offsets->outgoing_args += 4;
9897 if (offsets->outgoing_args & 7)
9898 abort ();
9901 return offsets;
9905 /* Calculate the relative offsets for the different stack pointers. Positive
9906 offsets are in the direction of stack growth. */
9908 HOST_WIDE_INT
9909 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
9911 arm_stack_offsets *offsets;
9913 offsets = arm_get_frame_offsets ();
9915 /* OK, now we have enough information to compute the distances.
9916 There must be an entry in these switch tables for each pair
9917 of registers in ELIMINABLE_REGS, even if some of the entries
9918 seem to be redundant or useless. */
9919 switch (from)
9921 case ARG_POINTER_REGNUM:
9922 switch (to)
9924 case THUMB_HARD_FRAME_POINTER_REGNUM:
9925 return 0;
9927 case FRAME_POINTER_REGNUM:
9928 /* This is the reverse of the soft frame pointer
9929 to hard frame pointer elimination below. */
9930 return offsets->soft_frame - offsets->saved_args;
9932 case ARM_HARD_FRAME_POINTER_REGNUM:
9933 /* If there is no stack frame then the hard
9934 frame pointer and the arg pointer coincide. */
9935 if (offsets->frame == offsets->saved_regs)
9936 return 0;
9937 /* FIXME: Not sure about this. Maybe we should always return 0 ? */
9938 return (frame_pointer_needed
9939 && cfun->static_chain_decl != NULL
9940 && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
9942 case STACK_POINTER_REGNUM:
9943 /* If nothing has been pushed on the stack at all
9944 then this will return -4. This *is* correct! */
9945 return offsets->outgoing_args - (offsets->saved_args + 4);
9947 default:
9948 abort ();
9950 break;
9952 case FRAME_POINTER_REGNUM:
9953 switch (to)
9955 case THUMB_HARD_FRAME_POINTER_REGNUM:
9956 return 0;
9958 case ARM_HARD_FRAME_POINTER_REGNUM:
9959 /* The hard frame pointer points to the top entry in the
9960 stack frame. The soft frame pointer to the bottom entry
9961 in the stack frame. If there is no stack frame at all,
9962 then they are identical. */
9964 return offsets->frame - offsets->soft_frame;
9966 case STACK_POINTER_REGNUM:
9967 return offsets->outgoing_args - offsets->soft_frame;
9969 default:
9970 abort ();
9972 break;
9974 default:
9975 /* You cannot eliminate from the stack pointer.
9976 In theory you could eliminate from the hard frame
9977 pointer to the stack pointer, but this will never
9978 happen, since if a stack frame is not needed the
9979 hard frame pointer will never be used. */
9980 abort ();
9985 /* Generate the prologue instructions for entry into an ARM function. */
9986 void
9987 arm_expand_prologue (void)
9989 int reg;
9990 rtx amount;
9991 rtx insn;
9992 rtx ip_rtx;
9993 unsigned long live_regs_mask;
9994 unsigned long func_type;
9995 int fp_offset = 0;
9996 int saved_pretend_args = 0;
9997 int saved_regs = 0;
9998 unsigned HOST_WIDE_INT args_to_push;
9999 arm_stack_offsets *offsets;
10001 func_type = arm_current_func_type ();
10003 /* Naked functions don't have prologues. */
10004 if (IS_NAKED (func_type))
10005 return;
10007 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
10008 args_to_push = current_function_pretend_args_size;
10010 /* Compute which register we will have to save onto the stack. */
10011 live_regs_mask = arm_compute_save_reg_mask ();
10013 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10015 if (frame_pointer_needed)
10017 if (IS_INTERRUPT (func_type))
10019 /* Interrupt functions must not corrupt any registers.
10020 Creating a frame pointer however, corrupts the IP
10021 register, so we must push it first. */
10022 insn = emit_multi_reg_push (1 << IP_REGNUM);
10024 /* Do not set RTX_FRAME_RELATED_P on this insn.
10025 The dwarf stack unwinding code only wants to see one
10026 stack decrement per function, and this is not it. If
10027 this instruction is labeled as being part of the frame
10028 creation sequence then dwarf2out_frame_debug_expr will
10029 abort when it encounters the assignment of IP to FP
10030 later on, since the use of SP here establishes SP as
10031 the CFA register and not IP.
10033 Anyway this instruction is not really part of the stack
10034 frame creation although it is part of the prologue. */
10036 else if (IS_NESTED (func_type))
10038 /* The Static chain register is the same as the IP register
10039 used as a scratch register during stack frame creation.
10040 To get around this need to find somewhere to store IP
10041 whilst the frame is being created. We try the following
10042 places in order:
10044 1. The last argument register.
10045 2. A slot on the stack above the frame. (This only
10046 works if the function is not a varargs function).
10047 3. Register r3, after pushing the argument registers
10048 onto the stack.
10050 Note - we only need to tell the dwarf2 backend about the SP
10051 adjustment in the second variant; the static chain register
10052 doesn't need to be unwound, as it doesn't contain a value
10053 inherited from the caller. */
10055 if (regs_ever_live[3] == 0)
10057 insn = gen_rtx_REG (SImode, 3);
10058 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10059 insn = emit_insn (insn);
10061 else if (args_to_push == 0)
10063 rtx dwarf;
10064 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10065 insn = gen_rtx_MEM (SImode, insn);
10066 insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10067 insn = emit_insn (insn);
10069 fp_offset = 4;
10071 /* Just tell the dwarf backend that we adjusted SP. */
10072 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10073 gen_rtx_PLUS (SImode, stack_pointer_rtx,
10074 GEN_INT (-fp_offset)));
10075 RTX_FRAME_RELATED_P (insn) = 1;
10076 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10077 dwarf, REG_NOTES (insn));
10079 else
10081 /* Store the args on the stack. */
10082 if (cfun->machine->uses_anonymous_args)
10083 insn = emit_multi_reg_push
10084 ((0xf0 >> (args_to_push / 4)) & 0xf);
10085 else
10086 insn = emit_insn
10087 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10088 GEN_INT (- args_to_push)));
10090 RTX_FRAME_RELATED_P (insn) = 1;
10092 saved_pretend_args = 1;
10093 fp_offset = args_to_push;
10094 args_to_push = 0;
10096 /* Now reuse r3 to preserve IP. */
10097 insn = gen_rtx_REG (SImode, 3);
10098 insn = gen_rtx_SET (SImode, insn, ip_rtx);
10099 (void) emit_insn (insn);
10103 if (fp_offset)
10105 insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10106 insn = gen_rtx_SET (SImode, ip_rtx, insn);
10108 else
10109 insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10111 insn = emit_insn (insn);
10112 RTX_FRAME_RELATED_P (insn) = 1;
10115 if (args_to_push)
10117 /* Push the argument registers, or reserve space for them. */
10118 if (cfun->machine->uses_anonymous_args)
10119 insn = emit_multi_reg_push
10120 ((0xf0 >> (args_to_push / 4)) & 0xf);
10121 else
10122 insn = emit_insn
10123 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10124 GEN_INT (- args_to_push)));
10125 RTX_FRAME_RELATED_P (insn) = 1;
10128 /* If this is an interrupt service routine, and the link register
10129 is going to be pushed, and we are not creating a stack frame,
10130 (which would involve an extra push of IP and a pop in the epilogue)
10131 subtracting four from LR now will mean that the function return
10132 can be done with a single instruction. */
10133 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10134 && (live_regs_mask & (1 << LR_REGNUM)) != 0
10135 && ! frame_pointer_needed)
10136 emit_insn (gen_rtx_SET (SImode,
10137 gen_rtx_REG (SImode, LR_REGNUM),
10138 gen_rtx_PLUS (SImode,
10139 gen_rtx_REG (SImode, LR_REGNUM),
10140 GEN_INT (-4))));
10142 if (live_regs_mask)
10144 insn = emit_multi_reg_push (live_regs_mask);
10145 saved_regs += bit_count (live_regs_mask) * 4;
10146 RTX_FRAME_RELATED_P (insn) = 1;
10149 if (TARGET_IWMMXT)
10150 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10151 if (regs_ever_live[reg] && ! call_used_regs [reg])
10153 insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10154 insn = gen_rtx_MEM (V2SImode, insn);
10155 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10156 gen_rtx_REG (V2SImode, reg)));
10157 RTX_FRAME_RELATED_P (insn) = 1;
10158 saved_regs += 8;
10161 if (! IS_VOLATILE (func_type))
10163 int start_reg;
10165 /* Save any floating point call-saved registers used by this
10166 function. */
10167 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10169 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10170 if (regs_ever_live[reg] && !call_used_regs[reg])
10172 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10173 insn = gen_rtx_MEM (XFmode, insn);
10174 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10175 gen_rtx_REG (XFmode, reg)));
10176 RTX_FRAME_RELATED_P (insn) = 1;
10177 saved_regs += 12;
10180 else
10182 start_reg = LAST_FPA_REGNUM;
10184 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10186 if (regs_ever_live[reg] && !call_used_regs[reg])
10188 if (start_reg - reg == 3)
10190 insn = emit_sfm (reg, 4);
10191 RTX_FRAME_RELATED_P (insn) = 1;
10192 saved_regs += 48;
10193 start_reg = reg - 1;
10196 else
10198 if (start_reg != reg)
10200 insn = emit_sfm (reg + 1, start_reg - reg);
10201 RTX_FRAME_RELATED_P (insn) = 1;
10202 saved_regs += (start_reg - reg) * 12;
10204 start_reg = reg - 1;
10208 if (start_reg != reg)
10210 insn = emit_sfm (reg + 1, start_reg - reg);
10211 saved_regs += (start_reg - reg) * 12;
10212 RTX_FRAME_RELATED_P (insn) = 1;
10215 if (TARGET_HARD_FLOAT && TARGET_VFP)
10217 start_reg = FIRST_VFP_REGNUM;
10219 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10221 if ((!regs_ever_live[reg] || call_used_regs[reg])
10222 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10224 if (start_reg != reg)
10225 saved_regs += vfp_emit_fstmx (start_reg,
10226 (reg - start_reg) / 2);
10227 start_reg = reg + 2;
10230 if (start_reg != reg)
10231 saved_regs += vfp_emit_fstmx (start_reg,
10232 (reg - start_reg) / 2);
10236 if (frame_pointer_needed)
10238 /* Create the new frame pointer. */
10239 insn = GEN_INT (-(4 + args_to_push + fp_offset));
10240 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10241 RTX_FRAME_RELATED_P (insn) = 1;
10243 if (IS_NESTED (func_type))
10245 /* Recover the static chain register. */
10246 if (regs_ever_live [3] == 0
10247 || saved_pretend_args)
10248 insn = gen_rtx_REG (SImode, 3);
10249 else /* if (current_function_pretend_args_size == 0) */
10251 insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10252 GEN_INT (4));
10253 insn = gen_rtx_MEM (SImode, insn);
10256 emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10257 /* Add a USE to stop propagate_one_insn() from barfing. */
10258 emit_insn (gen_prologue_use (ip_rtx));
10262 offsets = arm_get_frame_offsets ();
10263 if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10265 /* This add can produce multiple insns for a large constant, so we
10266 need to get tricky. */
10267 rtx last = get_last_insn ();
10269 amount = GEN_INT (offsets->saved_args + saved_regs
10270 - offsets->outgoing_args);
10272 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10273 amount));
10276 last = last ? NEXT_INSN (last) : get_insns ();
10277 RTX_FRAME_RELATED_P (last) = 1;
10279 while (last != insn);
10281 /* If the frame pointer is needed, emit a special barrier that
10282 will prevent the scheduler from moving stores to the frame
10283 before the stack adjustment. */
10284 if (frame_pointer_needed)
10285 insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10286 hard_frame_pointer_rtx));
10290 if (flag_pic)
10291 arm_load_pic_register (INVALID_REGNUM);
10293 /* If we are profiling, make sure no instructions are scheduled before
10294 the call to mcount. Similarly if the user has requested no
10295 scheduling in the prolog. */
10296 if (current_function_profile || TARGET_NO_SCHED_PRO)
10297 emit_insn (gen_blockage ());
10299 /* If the link register is being kept alive, with the return address in it,
10300 then make sure that it does not get reused by the ce2 pass. */
10301 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10303 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10304 cfun->machine->lr_save_eliminated = 1;
10308 /* If CODE is 'd', then the X is a condition operand and the instruction
10309 should only be executed if the condition is true.
10310 if CODE is 'D', then the X is a condition operand and the instruction
10311 should only be executed if the condition is false: however, if the mode
10312 of the comparison is CCFPEmode, then always execute the instruction -- we
10313 do this because in these circumstances !GE does not necessarily imply LT;
10314 in these cases the instruction pattern will take care to make sure that
10315 an instruction containing %d will follow, thereby undoing the effects of
10316 doing this instruction unconditionally.
10317 If CODE is 'N' then X is a floating point operand that must be negated
10318 before output.
10319 If CODE is 'B' then output a bitwise inverted value of X (a const int).
10320 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
10321 void
10322 arm_print_operand (FILE *stream, rtx x, int code)
10324 switch (code)
10326 case '@':
10327 fputs (ASM_COMMENT_START, stream);
10328 return;
10330 case '_':
10331 fputs (user_label_prefix, stream);
10332 return;
10334 case '|':
10335 fputs (REGISTER_PREFIX, stream);
10336 return;
10338 case '?':
10339 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10341 if (TARGET_THUMB)
10343 output_operand_lossage ("predicated Thumb instruction");
10344 break;
10346 if (current_insn_predicate != NULL)
10348 output_operand_lossage
10349 ("predicated instruction in conditional sequence");
10350 break;
10353 fputs (arm_condition_codes[arm_current_cc], stream);
10355 else if (current_insn_predicate)
10357 enum arm_cond_code code;
10359 if (TARGET_THUMB)
10361 output_operand_lossage ("predicated Thumb instruction");
10362 break;
10365 code = get_arm_condition_code (current_insn_predicate);
10366 fputs (arm_condition_codes[code], stream);
10368 return;
10370 case 'N':
10372 REAL_VALUE_TYPE r;
10373 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10374 r = REAL_VALUE_NEGATE (r);
10375 fprintf (stream, "%s", fp_const_from_val (&r));
10377 return;
10379 case 'B':
10380 if (GET_CODE (x) == CONST_INT)
10382 HOST_WIDE_INT val;
10383 val = ARM_SIGN_EXTEND (~INTVAL (x));
10384 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10386 else
10388 putc ('~', stream);
10389 output_addr_const (stream, x);
10391 return;
10393 case 'i':
10394 fprintf (stream, "%s", arithmetic_instr (x, 1));
10395 return;
10397 /* Truncate Cirrus shift counts. */
10398 case 's':
10399 if (GET_CODE (x) == CONST_INT)
10401 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10402 return;
10404 arm_print_operand (stream, x, 0);
10405 return;
10407 case 'I':
10408 fprintf (stream, "%s", arithmetic_instr (x, 0));
10409 return;
10411 case 'S':
10413 HOST_WIDE_INT val;
10414 const char * shift = shift_op (x, &val);
10416 if (shift)
10418 fprintf (stream, ", %s ", shift_op (x, &val));
10419 if (val == -1)
10420 arm_print_operand (stream, XEXP (x, 1), 0);
10421 else
10422 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10425 return;
10427 /* An explanation of the 'Q', 'R' and 'H' register operands:
10429 In a pair of registers containing a DI or DF value the 'Q'
10430 operand returns the register number of the register containing
10431 the least significant part of the value. The 'R' operand returns
10432 the register number of the register containing the most
10433 significant part of the value.
10435 The 'H' operand returns the higher of the two register numbers.
10436 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10437 same as the 'Q' operand, since the most significant part of the
10438 value is held in the lower number register. The reverse is true
10439 on systems where WORDS_BIG_ENDIAN is false.
10441 The purpose of these operands is to distinguish between cases
10442 where the endian-ness of the values is important (for example
10443 when they are added together), and cases where the endian-ness
10444 is irrelevant, but the order of register operations is important.
10445 For example when loading a value from memory into a register
10446 pair, the endian-ness does not matter. Provided that the value
10447 from the lower memory address is put into the lower numbered
10448 register, and the value from the higher address is put into the
10449 higher numbered register, the load will work regardless of whether
10450 the value being loaded is big-wordian or little-wordian. The
10451 order of the two register loads can matter however, if the address
10452 of the memory location is actually held in one of the registers
10453 being overwritten by the load. */
10454 case 'Q':
10455 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10457 output_operand_lossage ("invalid operand for code '%c'", code);
10458 return;
10461 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10462 return;
10464 case 'R':
10465 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10467 output_operand_lossage ("invalid operand for code '%c'", code);
10468 return;
10471 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10472 return;
10474 case 'H':
10475 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10477 output_operand_lossage ("invalid operand for code '%c'", code);
10478 return;
10481 asm_fprintf (stream, "%r", REGNO (x) + 1);
10482 return;
10484 case 'm':
10485 asm_fprintf (stream, "%r",
10486 GET_CODE (XEXP (x, 0)) == REG
10487 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10488 return;
10490 case 'M':
10491 asm_fprintf (stream, "{%r-%r}",
10492 REGNO (x),
10493 REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10494 return;
10496 case 'd':
10497 /* CONST_TRUE_RTX means always -- that's the default. */
10498 if (x == const_true_rtx)
10499 return;
10501 if (!COMPARISON_P (x))
10503 output_operand_lossage ("invalid operand for code '%c'", code);
10504 return;
10507 fputs (arm_condition_codes[get_arm_condition_code (x)],
10508 stream);
10509 return;
10511 case 'D':
10512 /* CONST_TRUE_RTX means not always -- i.e. never. We shouldn't ever
10513 want to do that. */
10514 if (x == const_true_rtx)
10516 output_operand_lossage ("instruction never exectued");
10517 return;
10519 if (!COMPARISON_P (x))
10521 output_operand_lossage ("invalid operand for code '%c'", code);
10522 return;
10525 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10526 (get_arm_condition_code (x))],
10527 stream);
10528 return;
10530 /* Cirrus registers can be accessed in a variety of ways:
10531 single floating point (f)
10532 double floating point (d)
10533 32bit integer (fx)
10534 64bit integer (dx). */
10535 case 'W': /* Cirrus register in F mode. */
10536 case 'X': /* Cirrus register in D mode. */
10537 case 'Y': /* Cirrus register in FX mode. */
10538 case 'Z': /* Cirrus register in DX mode. */
10539 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10540 abort ();
10542 fprintf (stream, "mv%s%s",
10543 code == 'W' ? "f"
10544 : code == 'X' ? "d"
10545 : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10547 return;
10549 /* Print cirrus register in the mode specified by the register's mode. */
10550 case 'V':
10552 int mode = GET_MODE (x);
10554 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10556 output_operand_lossage ("invalid operand for code '%c'", code);
10557 return;
10560 fprintf (stream, "mv%s%s",
10561 mode == DFmode ? "d"
10562 : mode == SImode ? "fx"
10563 : mode == DImode ? "dx"
10564 : "f", reg_names[REGNO (x)] + 2);
10566 return;
10569 case 'U':
10570 if (GET_CODE (x) != REG
10571 || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10572 || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10573 /* Bad value for wCG register number. */
10575 output_operand_lossage ("invalid operand for code '%c'", code);
10576 return;
10579 else
10580 fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10581 return;
10583 /* Print an iWMMXt control register name. */
10584 case 'w':
10585 if (GET_CODE (x) != CONST_INT
10586 || INTVAL (x) < 0
10587 || INTVAL (x) >= 16)
10588 /* Bad value for wC register number. */
10590 output_operand_lossage ("invalid operand for code '%c'", code);
10591 return;
10594 else
10596 static const char * wc_reg_names [16] =
10598 "wCID", "wCon", "wCSSF", "wCASF",
10599 "wC4", "wC5", "wC6", "wC7",
10600 "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10601 "wC12", "wC13", "wC14", "wC15"
10604 fprintf (stream, wc_reg_names [INTVAL (x)]);
10606 return;
10608 /* Print a VFP double precision register name. */
10609 case 'P':
10611 int mode = GET_MODE (x);
10612 int num;
10614 if (mode != DImode && mode != DFmode)
10616 output_operand_lossage ("invalid operand for code '%c'", code);
10617 return;
10620 if (GET_CODE (x) != REG
10621 || !IS_VFP_REGNUM (REGNO (x)))
10623 output_operand_lossage ("invalid operand for code '%c'", code);
10624 return;
10627 num = REGNO(x) - FIRST_VFP_REGNUM;
10628 if (num & 1)
10630 output_operand_lossage ("invalid operand for code '%c'", code);
10631 return;
10634 fprintf (stream, "d%d", num >> 1);
10636 return;
10638 default:
10639 if (x == 0)
10641 output_operand_lossage ("missing operand");
10642 return;
10645 if (GET_CODE (x) == REG)
10646 asm_fprintf (stream, "%r", REGNO (x));
10647 else if (GET_CODE (x) == MEM)
10649 output_memory_reference_mode = GET_MODE (x);
10650 output_address (XEXP (x, 0));
10652 else if (GET_CODE (x) == CONST_DOUBLE)
10653 fprintf (stream, "#%s", fp_immediate_constant (x));
10654 else if (GET_CODE (x) == NEG)
10655 abort (); /* This should never happen now. */
10656 else
10658 fputc ('#', stream);
10659 output_addr_const (stream, x);
10664 #ifndef AOF_ASSEMBLER
10665 /* Target hook for assembling integer objects. The ARM version needs to
10666 handle word-sized values specially. */
10667 static bool
10668 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10670 if (size == UNITS_PER_WORD && aligned_p)
10672 fputs ("\t.word\t", asm_out_file);
10673 output_addr_const (asm_out_file, x);
10675 /* Mark symbols as position independent. We only do this in the
10676 .text segment, not in the .data segment. */
10677 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10678 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10680 if (GET_CODE (x) == SYMBOL_REF
10681 && (CONSTANT_POOL_ADDRESS_P (x)
10682 || SYMBOL_REF_LOCAL_P (x)))
10683 fputs ("(GOTOFF)", asm_out_file);
10684 else if (GET_CODE (x) == LABEL_REF)
10685 fputs ("(GOTOFF)", asm_out_file);
10686 else
10687 fputs ("(GOT)", asm_out_file);
10689 fputc ('\n', asm_out_file);
10690 return true;
10693 if (arm_vector_mode_supported_p (GET_MODE (x)))
10695 int i, units;
10697 if (GET_CODE (x) != CONST_VECTOR)
10698 abort ();
10700 units = CONST_VECTOR_NUNITS (x);
10702 switch (GET_MODE (x))
10704 case V2SImode: size = 4; break;
10705 case V4HImode: size = 2; break;
10706 case V8QImode: size = 1; break;
10707 default:
10708 abort ();
10711 for (i = 0; i < units; i++)
10713 rtx elt;
10715 elt = CONST_VECTOR_ELT (x, i);
10716 assemble_integer
10717 (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10720 return true;
10723 return default_assemble_integer (x, size, aligned_p);
10725 #endif
10727 /* A finite state machine takes care of noticing whether or not instructions
10728 can be conditionally executed, and thus decrease execution time and code
10729 size by deleting branch instructions. The fsm is controlled by
10730 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
10732 /* The state of the fsm controlling condition codes are:
10733 0: normal, do nothing special
10734 1: make ASM_OUTPUT_OPCODE not output this instruction
10735 2: make ASM_OUTPUT_OPCODE not output this instruction
10736 3: make instructions conditional
10737 4: make instructions conditional
10739 State transitions (state->state by whom under condition):
10740 0 -> 1 final_prescan_insn if the `target' is a label
10741 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10742 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10743 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10744 3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10745 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10746 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10747 (the target insn is arm_target_insn).
10749 If the jump clobbers the conditions then we use states 2 and 4.
10751 A similar thing can be done with conditional return insns.
10753 XXX In case the `target' is an unconditional branch, this conditionalising
10754 of the instructions always reduces code size, but not always execution
10755 time. But then, I want to reduce the code size to somewhere near what
10756 /bin/cc produces. */
10758 /* Returns the index of the ARM condition code string in
10759 `arm_condition_codes'. COMPARISON should be an rtx like
10760 `(eq (...) (...))'. */
10761 static enum arm_cond_code
10762 get_arm_condition_code (rtx comparison)
10764 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10765 int code;
10766 enum rtx_code comp_code = GET_CODE (comparison);
10768 if (GET_MODE_CLASS (mode) != MODE_CC)
10769 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10770 XEXP (comparison, 1));
10772 switch (mode)
10774 case CC_DNEmode: code = ARM_NE; goto dominance;
10775 case CC_DEQmode: code = ARM_EQ; goto dominance;
10776 case CC_DGEmode: code = ARM_GE; goto dominance;
10777 case CC_DGTmode: code = ARM_GT; goto dominance;
10778 case CC_DLEmode: code = ARM_LE; goto dominance;
10779 case CC_DLTmode: code = ARM_LT; goto dominance;
10780 case CC_DGEUmode: code = ARM_CS; goto dominance;
10781 case CC_DGTUmode: code = ARM_HI; goto dominance;
10782 case CC_DLEUmode: code = ARM_LS; goto dominance;
10783 case CC_DLTUmode: code = ARM_CC;
10785 dominance:
10786 if (comp_code != EQ && comp_code != NE)
10787 abort ();
10789 if (comp_code == EQ)
10790 return ARM_INVERSE_CONDITION_CODE (code);
10791 return code;
10793 case CC_NOOVmode:
10794 switch (comp_code)
10796 case NE: return ARM_NE;
10797 case EQ: return ARM_EQ;
10798 case GE: return ARM_PL;
10799 case LT: return ARM_MI;
10800 default: abort ();
10803 case CC_Zmode:
10804 switch (comp_code)
10806 case NE: return ARM_NE;
10807 case EQ: return ARM_EQ;
10808 default: abort ();
10811 case CC_Nmode:
10812 switch (comp_code)
10814 case NE: return ARM_MI;
10815 case EQ: return ARM_PL;
10816 default: abort ();
10819 case CCFPEmode:
10820 case CCFPmode:
10821 /* These encodings assume that AC=1 in the FPA system control
10822 byte. This allows us to handle all cases except UNEQ and
10823 LTGT. */
10824 switch (comp_code)
10826 case GE: return ARM_GE;
10827 case GT: return ARM_GT;
10828 case LE: return ARM_LS;
10829 case LT: return ARM_MI;
10830 case NE: return ARM_NE;
10831 case EQ: return ARM_EQ;
10832 case ORDERED: return ARM_VC;
10833 case UNORDERED: return ARM_VS;
10834 case UNLT: return ARM_LT;
10835 case UNLE: return ARM_LE;
10836 case UNGT: return ARM_HI;
10837 case UNGE: return ARM_PL;
10838 /* UNEQ and LTGT do not have a representation. */
10839 case UNEQ: /* Fall through. */
10840 case LTGT: /* Fall through. */
10841 default: abort ();
10844 case CC_SWPmode:
10845 switch (comp_code)
10847 case NE: return ARM_NE;
10848 case EQ: return ARM_EQ;
10849 case GE: return ARM_LE;
10850 case GT: return ARM_LT;
10851 case LE: return ARM_GE;
10852 case LT: return ARM_GT;
10853 case GEU: return ARM_LS;
10854 case GTU: return ARM_CC;
10855 case LEU: return ARM_CS;
10856 case LTU: return ARM_HI;
10857 default: abort ();
10860 case CC_Cmode:
10861 switch (comp_code)
10863 case LTU: return ARM_CS;
10864 case GEU: return ARM_CC;
10865 default: abort ();
10868 case CCmode:
10869 switch (comp_code)
10871 case NE: return ARM_NE;
10872 case EQ: return ARM_EQ;
10873 case GE: return ARM_GE;
10874 case GT: return ARM_GT;
10875 case LE: return ARM_LE;
10876 case LT: return ARM_LT;
10877 case GEU: return ARM_CS;
10878 case GTU: return ARM_HI;
10879 case LEU: return ARM_LS;
10880 case LTU: return ARM_CC;
10881 default: abort ();
10884 default: abort ();
10887 abort ();
10890 void
10891 arm_final_prescan_insn (rtx insn)
10893 /* BODY will hold the body of INSN. */
10894 rtx body = PATTERN (insn);
10896 /* This will be 1 if trying to repeat the trick, and things need to be
10897 reversed if it appears to fail. */
10898 int reverse = 0;
10900 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
10901 taken are clobbered, even if the rtl suggests otherwise. It also
10902 means that we have to grub around within the jump expression to find
10903 out what the conditions are when the jump isn't taken. */
10904 int jump_clobbers = 0;
10906 /* If we start with a return insn, we only succeed if we find another one. */
10907 int seeking_return = 0;
10909 /* START_INSN will hold the insn from where we start looking. This is the
10910 first insn after the following code_label if REVERSE is true. */
10911 rtx start_insn = insn;
10913 /* If in state 4, check if the target branch is reached, in order to
10914 change back to state 0. */
10915 if (arm_ccfsm_state == 4)
10917 if (insn == arm_target_insn)
10919 arm_target_insn = NULL;
10920 arm_ccfsm_state = 0;
10922 return;
10925 /* If in state 3, it is possible to repeat the trick, if this insn is an
10926 unconditional branch to a label, and immediately following this branch
10927 is the previous target label which is only used once, and the label this
10928 branch jumps to is not too far off. */
10929 if (arm_ccfsm_state == 3)
10931 if (simplejump_p (insn))
10933 start_insn = next_nonnote_insn (start_insn);
10934 if (GET_CODE (start_insn) == BARRIER)
10936 /* XXX Isn't this always a barrier? */
10937 start_insn = next_nonnote_insn (start_insn);
10939 if (GET_CODE (start_insn) == CODE_LABEL
10940 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
10941 && LABEL_NUSES (start_insn) == 1)
10942 reverse = TRUE;
10943 else
10944 return;
10946 else if (GET_CODE (body) == RETURN)
10948 start_insn = next_nonnote_insn (start_insn);
10949 if (GET_CODE (start_insn) == BARRIER)
10950 start_insn = next_nonnote_insn (start_insn);
10951 if (GET_CODE (start_insn) == CODE_LABEL
10952 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
10953 && LABEL_NUSES (start_insn) == 1)
10955 reverse = TRUE;
10956 seeking_return = 1;
10958 else
10959 return;
10961 else
10962 return;
10965 if (arm_ccfsm_state != 0 && !reverse)
10966 abort ();
10967 if (GET_CODE (insn) != JUMP_INSN)
10968 return;
10970 /* This jump might be paralleled with a clobber of the condition codes
10971 the jump should always come first */
10972 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
10973 body = XVECEXP (body, 0, 0);
10975 if (reverse
10976 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
10977 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
10979 int insns_skipped;
10980 int fail = FALSE, succeed = FALSE;
10981 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
10982 int then_not_else = TRUE;
10983 rtx this_insn = start_insn, label = 0;
10985 /* If the jump cannot be done with one instruction, we cannot
10986 conditionally execute the instruction in the inverse case. */
10987 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
10989 jump_clobbers = 1;
10990 return;
10993 /* Register the insn jumped to. */
10994 if (reverse)
10996 if (!seeking_return)
10997 label = XEXP (SET_SRC (body), 0);
10999 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11000 label = XEXP (XEXP (SET_SRC (body), 1), 0);
11001 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11003 label = XEXP (XEXP (SET_SRC (body), 2), 0);
11004 then_not_else = FALSE;
11006 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11007 seeking_return = 1;
11008 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11010 seeking_return = 1;
11011 then_not_else = FALSE;
11013 else
11014 abort ();
11016 /* See how many insns this branch skips, and what kind of insns. If all
11017 insns are okay, and the label or unconditional branch to the same
11018 label is not too far away, succeed. */
11019 for (insns_skipped = 0;
11020 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11022 rtx scanbody;
11024 this_insn = next_nonnote_insn (this_insn);
11025 if (!this_insn)
11026 break;
11028 switch (GET_CODE (this_insn))
11030 case CODE_LABEL:
11031 /* Succeed if it is the target label, otherwise fail since
11032 control falls in from somewhere else. */
11033 if (this_insn == label)
11035 if (jump_clobbers)
11037 arm_ccfsm_state = 2;
11038 this_insn = next_nonnote_insn (this_insn);
11040 else
11041 arm_ccfsm_state = 1;
11042 succeed = TRUE;
11044 else
11045 fail = TRUE;
11046 break;
11048 case BARRIER:
11049 /* Succeed if the following insn is the target label.
11050 Otherwise fail.
11051 If return insns are used then the last insn in a function
11052 will be a barrier. */
11053 this_insn = next_nonnote_insn (this_insn);
11054 if (this_insn && this_insn == label)
11056 if (jump_clobbers)
11058 arm_ccfsm_state = 2;
11059 this_insn = next_nonnote_insn (this_insn);
11061 else
11062 arm_ccfsm_state = 1;
11063 succeed = TRUE;
11065 else
11066 fail = TRUE;
11067 break;
11069 case CALL_INSN:
11070 /* The AAPCS says that conditional calls should not be
11071 used since they make interworking inefficient (the
11072 linker can't transform BL<cond> into BLX). That's
11073 only a problem if the machine has BLX. */
11074 if (arm_arch5)
11076 fail = TRUE;
11077 break;
11080 /* Succeed if the following insn is the target label, or
11081 if the following two insns are a barrier and the
11082 target label. */
11083 this_insn = next_nonnote_insn (this_insn);
11084 if (this_insn && GET_CODE (this_insn) == BARRIER)
11085 this_insn = next_nonnote_insn (this_insn);
11087 if (this_insn && this_insn == label
11088 && insns_skipped < max_insns_skipped)
11090 if (jump_clobbers)
11092 arm_ccfsm_state = 2;
11093 this_insn = next_nonnote_insn (this_insn);
11095 else
11096 arm_ccfsm_state = 1;
11097 succeed = TRUE;
11099 else
11100 fail = TRUE;
11101 break;
11103 case JUMP_INSN:
11104 /* If this is an unconditional branch to the same label, succeed.
11105 If it is to another label, do nothing. If it is conditional,
11106 fail. */
11107 /* XXX Probably, the tests for SET and the PC are
11108 unnecessary. */
11110 scanbody = PATTERN (this_insn);
11111 if (GET_CODE (scanbody) == SET
11112 && GET_CODE (SET_DEST (scanbody)) == PC)
11114 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11115 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11117 arm_ccfsm_state = 2;
11118 succeed = TRUE;
11120 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11121 fail = TRUE;
11123 /* Fail if a conditional return is undesirable (e.g. on a
11124 StrongARM), but still allow this if optimizing for size. */
11125 else if (GET_CODE (scanbody) == RETURN
11126 && !use_return_insn (TRUE, NULL)
11127 && !optimize_size)
11128 fail = TRUE;
11129 else if (GET_CODE (scanbody) == RETURN
11130 && seeking_return)
11132 arm_ccfsm_state = 2;
11133 succeed = TRUE;
11135 else if (GET_CODE (scanbody) == PARALLEL)
11137 switch (get_attr_conds (this_insn))
11139 case CONDS_NOCOND:
11140 break;
11141 default:
11142 fail = TRUE;
11143 break;
11146 else
11147 fail = TRUE; /* Unrecognized jump (e.g. epilogue). */
11149 break;
11151 case INSN:
11152 /* Instructions using or affecting the condition codes make it
11153 fail. */
11154 scanbody = PATTERN (this_insn);
11155 if (!(GET_CODE (scanbody) == SET
11156 || GET_CODE (scanbody) == PARALLEL)
11157 || get_attr_conds (this_insn) != CONDS_NOCOND)
11158 fail = TRUE;
11160 /* A conditional cirrus instruction must be followed by
11161 a non Cirrus instruction. However, since we
11162 conditionalize instructions in this function and by
11163 the time we get here we can't add instructions
11164 (nops), because shorten_branches() has already been
11165 called, we will disable conditionalizing Cirrus
11166 instructions to be safe. */
11167 if (GET_CODE (scanbody) != USE
11168 && GET_CODE (scanbody) != CLOBBER
11169 && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11170 fail = TRUE;
11171 break;
11173 default:
11174 break;
11177 if (succeed)
11179 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11180 arm_target_label = CODE_LABEL_NUMBER (label);
11181 else if (seeking_return || arm_ccfsm_state == 2)
11183 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11185 this_insn = next_nonnote_insn (this_insn);
11186 if (this_insn && (GET_CODE (this_insn) == BARRIER
11187 || GET_CODE (this_insn) == CODE_LABEL))
11188 abort ();
11190 if (!this_insn)
11192 /* Oh, dear! we ran off the end.. give up. */
11193 recog (PATTERN (insn), insn, NULL);
11194 arm_ccfsm_state = 0;
11195 arm_target_insn = NULL;
11196 return;
11198 arm_target_insn = this_insn;
11200 else
11201 abort ();
11202 if (jump_clobbers)
11204 if (reverse)
11205 abort ();
11206 arm_current_cc =
11207 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11208 0), 0), 1));
11209 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11210 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11211 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11212 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11214 else
11216 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11217 what it was. */
11218 if (!reverse)
11219 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11220 0));
11223 if (reverse || then_not_else)
11224 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11227 /* Restore recog_data (getting the attributes of other insns can
11228 destroy this array, but final.c assumes that it remains intact
11229 across this call; since the insn has been recognized already we
11230 call recog direct). */
11231 recog (PATTERN (insn), insn, NULL);
11235 /* Returns true if REGNO is a valid register
11236 for holding a quantity of type MODE. */
11238 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11240 if (GET_MODE_CLASS (mode) == MODE_CC)
11241 return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11243 if (TARGET_THUMB)
11244 /* For the Thumb we only allow values bigger than SImode in
11245 registers 0 - 6, so that there is always a second low
11246 register available to hold the upper part of the value.
11247 We probably we ought to ensure that the register is the
11248 start of an even numbered register pair. */
11249 return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11251 if (IS_CIRRUS_REGNUM (regno))
11252 /* We have outlawed SI values in Cirrus registers because they
11253 reside in the lower 32 bits, but SF values reside in the
11254 upper 32 bits. This causes gcc all sorts of grief. We can't
11255 even split the registers into pairs because Cirrus SI values
11256 get sign extended to 64bits-- aldyh. */
11257 return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11259 if (IS_VFP_REGNUM (regno))
11261 if (mode == SFmode || mode == SImode)
11262 return TRUE;
11264 /* DFmode values are only valid in even register pairs. */
11265 if (mode == DFmode)
11266 return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11267 return FALSE;
11270 if (IS_IWMMXT_GR_REGNUM (regno))
11271 return mode == SImode;
11273 if (IS_IWMMXT_REGNUM (regno))
11274 return VALID_IWMMXT_REG_MODE (mode);
11276 /* We allow any value to be stored in the general registers.
11277 Restrict doubleword quantities to even register pairs so that we can
11278 use ldrd. */
11279 if (regno <= LAST_ARM_REGNUM)
11280 return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11282 if ( regno == FRAME_POINTER_REGNUM
11283 || regno == ARG_POINTER_REGNUM)
11284 /* We only allow integers in the fake hard registers. */
11285 return GET_MODE_CLASS (mode) == MODE_INT;
11287 /* The only registers left are the FPA registers
11288 which we only allow to hold FP values. */
11289 return GET_MODE_CLASS (mode) == MODE_FLOAT
11290 && regno >= FIRST_FPA_REGNUM
11291 && regno <= LAST_FPA_REGNUM;
11295 arm_regno_class (int regno)
11297 if (TARGET_THUMB)
11299 if (regno == STACK_POINTER_REGNUM)
11300 return STACK_REG;
11301 if (regno == CC_REGNUM)
11302 return CC_REG;
11303 if (regno < 8)
11304 return LO_REGS;
11305 return HI_REGS;
11308 if ( regno <= LAST_ARM_REGNUM
11309 || regno == FRAME_POINTER_REGNUM
11310 || regno == ARG_POINTER_REGNUM)
11311 return GENERAL_REGS;
11313 if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11314 return NO_REGS;
11316 if (IS_CIRRUS_REGNUM (regno))
11317 return CIRRUS_REGS;
11319 if (IS_VFP_REGNUM (regno))
11320 return VFP_REGS;
11322 if (IS_IWMMXT_REGNUM (regno))
11323 return IWMMXT_REGS;
11325 if (IS_IWMMXT_GR_REGNUM (regno))
11326 return IWMMXT_GR_REGS;
11328 return FPA_REGS;
11331 /* Handle a special case when computing the offset
11332 of an argument from the frame pointer. */
11334 arm_debugger_arg_offset (int value, rtx addr)
11336 rtx insn;
11338 /* We are only interested if dbxout_parms() failed to compute the offset. */
11339 if (value != 0)
11340 return 0;
11342 /* We can only cope with the case where the address is held in a register. */
11343 if (GET_CODE (addr) != REG)
11344 return 0;
11346 /* If we are using the frame pointer to point at the argument, then
11347 an offset of 0 is correct. */
11348 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11349 return 0;
11351 /* If we are using the stack pointer to point at the
11352 argument, then an offset of 0 is correct. */
11353 if ((TARGET_THUMB || !frame_pointer_needed)
11354 && REGNO (addr) == SP_REGNUM)
11355 return 0;
11357 /* Oh dear. The argument is pointed to by a register rather
11358 than being held in a register, or being stored at a known
11359 offset from the frame pointer. Since GDB only understands
11360 those two kinds of argument we must translate the address
11361 held in the register into an offset from the frame pointer.
11362 We do this by searching through the insns for the function
11363 looking to see where this register gets its value. If the
11364 register is initialized from the frame pointer plus an offset
11365 then we are in luck and we can continue, otherwise we give up.
11367 This code is exercised by producing debugging information
11368 for a function with arguments like this:
11370 double func (double a, double b, int c, double d) {return d;}
11372 Without this code the stab for parameter 'd' will be set to
11373 an offset of 0 from the frame pointer, rather than 8. */
11375 /* The if() statement says:
11377 If the insn is a normal instruction
11378 and if the insn is setting the value in a register
11379 and if the register being set is the register holding the address of the argument
11380 and if the address is computing by an addition
11381 that involves adding to a register
11382 which is the frame pointer
11383 a constant integer
11385 then... */
11387 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11389 if ( GET_CODE (insn) == INSN
11390 && GET_CODE (PATTERN (insn)) == SET
11391 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11392 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11393 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11394 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11395 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11398 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11400 break;
11404 if (value == 0)
11406 debug_rtx (addr);
11407 warning (0, "unable to compute real location of stacked parameter");
11408 value = 8; /* XXX magic hack */
11411 return value;
11414 #define def_mbuiltin(MASK, NAME, TYPE, CODE) \
11415 do \
11417 if ((MASK) & insn_flags) \
11418 lang_hooks.builtin_function ((NAME), (TYPE), (CODE), \
11419 BUILT_IN_MD, NULL, NULL_TREE); \
11421 while (0)
11423 struct builtin_description
11425 const unsigned int mask;
11426 const enum insn_code icode;
11427 const char * const name;
11428 const enum arm_builtins code;
11429 const enum rtx_code comparison;
11430 const unsigned int flag;
11433 static const struct builtin_description bdesc_2arg[] =
11435 #define IWMMXT_BUILTIN(code, string, builtin) \
11436 { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11437 ARM_BUILTIN_##builtin, 0, 0 },
11439 IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11440 IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11441 IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11442 IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11443 IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11444 IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11445 IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11446 IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11447 IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11448 IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11449 IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11450 IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11451 IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11452 IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11453 IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11454 IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11455 IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11456 IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11457 IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11458 IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11459 IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11460 IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11461 IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11462 IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11463 IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11464 IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11465 IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11466 IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11467 IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11468 IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11469 IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11470 IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11471 IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11472 IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11473 IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11474 IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11475 IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11476 IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11477 IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11478 IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11479 IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11480 IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11481 IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11482 IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11483 IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11484 IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11485 IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11486 IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11487 IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11488 IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11489 IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11490 IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11491 IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11492 IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11493 IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11494 IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11495 IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11496 IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11498 #define IWMMXT_BUILTIN2(code, builtin) \
11499 { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11501 IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11502 IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11503 IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11504 IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11505 IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11506 IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11507 IWMMXT_BUILTIN2 (ashlv4hi3_di, WSLLH)
11508 IWMMXT_BUILTIN2 (ashlv4hi3, WSLLHI)
11509 IWMMXT_BUILTIN2 (ashlv2si3_di, WSLLW)
11510 IWMMXT_BUILTIN2 (ashlv2si3, WSLLWI)
11511 IWMMXT_BUILTIN2 (ashldi3_di, WSLLD)
11512 IWMMXT_BUILTIN2 (ashldi3_iwmmxt, WSLLDI)
11513 IWMMXT_BUILTIN2 (lshrv4hi3_di, WSRLH)
11514 IWMMXT_BUILTIN2 (lshrv4hi3, WSRLHI)
11515 IWMMXT_BUILTIN2 (lshrv2si3_di, WSRLW)
11516 IWMMXT_BUILTIN2 (lshrv2si3, WSRLWI)
11517 IWMMXT_BUILTIN2 (lshrdi3_di, WSRLD)
11518 IWMMXT_BUILTIN2 (lshrdi3_iwmmxt, WSRLDI)
11519 IWMMXT_BUILTIN2 (ashrv4hi3_di, WSRAH)
11520 IWMMXT_BUILTIN2 (ashrv4hi3, WSRAHI)
11521 IWMMXT_BUILTIN2 (ashrv2si3_di, WSRAW)
11522 IWMMXT_BUILTIN2 (ashrv2si3, WSRAWI)
11523 IWMMXT_BUILTIN2 (ashrdi3_di, WSRAD)
11524 IWMMXT_BUILTIN2 (ashrdi3_iwmmxt, WSRADI)
11525 IWMMXT_BUILTIN2 (rorv4hi3_di, WRORH)
11526 IWMMXT_BUILTIN2 (rorv4hi3, WRORHI)
11527 IWMMXT_BUILTIN2 (rorv2si3_di, WRORW)
11528 IWMMXT_BUILTIN2 (rorv2si3, WRORWI)
11529 IWMMXT_BUILTIN2 (rordi3_di, WRORD)
11530 IWMMXT_BUILTIN2 (rordi3, WRORDI)
11531 IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
11532 IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
11535 static const struct builtin_description bdesc_1arg[] =
11537 IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11538 IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11539 IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11540 IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11541 IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11542 IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11543 IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11544 IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11545 IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11546 IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11547 IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11548 IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11549 IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11550 IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11551 IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11552 IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11553 IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11554 IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11557 /* Set up all the iWMMXt builtins. This is
11558 not called if TARGET_IWMMXT is zero. */
11560 static void
11561 arm_init_iwmmxt_builtins (void)
11563 const struct builtin_description * d;
11564 size_t i;
11565 tree endlink = void_list_node;
11567 tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11568 tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11569 tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11571 tree int_ftype_int
11572 = build_function_type (integer_type_node,
11573 tree_cons (NULL_TREE, integer_type_node, endlink));
11574 tree v8qi_ftype_v8qi_v8qi_int
11575 = build_function_type (V8QI_type_node,
11576 tree_cons (NULL_TREE, V8QI_type_node,
11577 tree_cons (NULL_TREE, V8QI_type_node,
11578 tree_cons (NULL_TREE,
11579 integer_type_node,
11580 endlink))));
11581 tree v4hi_ftype_v4hi_int
11582 = build_function_type (V4HI_type_node,
11583 tree_cons (NULL_TREE, V4HI_type_node,
11584 tree_cons (NULL_TREE, integer_type_node,
11585 endlink)));
11586 tree v2si_ftype_v2si_int
11587 = build_function_type (V2SI_type_node,
11588 tree_cons (NULL_TREE, V2SI_type_node,
11589 tree_cons (NULL_TREE, integer_type_node,
11590 endlink)));
11591 tree v2si_ftype_di_di
11592 = build_function_type (V2SI_type_node,
11593 tree_cons (NULL_TREE, long_long_integer_type_node,
11594 tree_cons (NULL_TREE, long_long_integer_type_node,
11595 endlink)));
11596 tree di_ftype_di_int
11597 = build_function_type (long_long_integer_type_node,
11598 tree_cons (NULL_TREE, long_long_integer_type_node,
11599 tree_cons (NULL_TREE, integer_type_node,
11600 endlink)));
11601 tree di_ftype_di_int_int
11602 = build_function_type (long_long_integer_type_node,
11603 tree_cons (NULL_TREE, long_long_integer_type_node,
11604 tree_cons (NULL_TREE, integer_type_node,
11605 tree_cons (NULL_TREE,
11606 integer_type_node,
11607 endlink))));
11608 tree int_ftype_v8qi
11609 = build_function_type (integer_type_node,
11610 tree_cons (NULL_TREE, V8QI_type_node,
11611 endlink));
11612 tree int_ftype_v4hi
11613 = build_function_type (integer_type_node,
11614 tree_cons (NULL_TREE, V4HI_type_node,
11615 endlink));
11616 tree int_ftype_v2si
11617 = build_function_type (integer_type_node,
11618 tree_cons (NULL_TREE, V2SI_type_node,
11619 endlink));
11620 tree int_ftype_v8qi_int
11621 = build_function_type (integer_type_node,
11622 tree_cons (NULL_TREE, V8QI_type_node,
11623 tree_cons (NULL_TREE, integer_type_node,
11624 endlink)));
11625 tree int_ftype_v4hi_int
11626 = build_function_type (integer_type_node,
11627 tree_cons (NULL_TREE, V4HI_type_node,
11628 tree_cons (NULL_TREE, integer_type_node,
11629 endlink)));
11630 tree int_ftype_v2si_int
11631 = build_function_type (integer_type_node,
11632 tree_cons (NULL_TREE, V2SI_type_node,
11633 tree_cons (NULL_TREE, integer_type_node,
11634 endlink)));
11635 tree v8qi_ftype_v8qi_int_int
11636 = build_function_type (V8QI_type_node,
11637 tree_cons (NULL_TREE, V8QI_type_node,
11638 tree_cons (NULL_TREE, integer_type_node,
11639 tree_cons (NULL_TREE,
11640 integer_type_node,
11641 endlink))));
11642 tree v4hi_ftype_v4hi_int_int
11643 = build_function_type (V4HI_type_node,
11644 tree_cons (NULL_TREE, V4HI_type_node,
11645 tree_cons (NULL_TREE, integer_type_node,
11646 tree_cons (NULL_TREE,
11647 integer_type_node,
11648 endlink))));
11649 tree v2si_ftype_v2si_int_int
11650 = build_function_type (V2SI_type_node,
11651 tree_cons (NULL_TREE, V2SI_type_node,
11652 tree_cons (NULL_TREE, integer_type_node,
11653 tree_cons (NULL_TREE,
11654 integer_type_node,
11655 endlink))));
11656 /* Miscellaneous. */
11657 tree v8qi_ftype_v4hi_v4hi
11658 = build_function_type (V8QI_type_node,
11659 tree_cons (NULL_TREE, V4HI_type_node,
11660 tree_cons (NULL_TREE, V4HI_type_node,
11661 endlink)));
11662 tree v4hi_ftype_v2si_v2si
11663 = build_function_type (V4HI_type_node,
11664 tree_cons (NULL_TREE, V2SI_type_node,
11665 tree_cons (NULL_TREE, V2SI_type_node,
11666 endlink)));
11667 tree v2si_ftype_v4hi_v4hi
11668 = build_function_type (V2SI_type_node,
11669 tree_cons (NULL_TREE, V4HI_type_node,
11670 tree_cons (NULL_TREE, V4HI_type_node,
11671 endlink)));
11672 tree v2si_ftype_v8qi_v8qi
11673 = build_function_type (V2SI_type_node,
11674 tree_cons (NULL_TREE, V8QI_type_node,
11675 tree_cons (NULL_TREE, V8QI_type_node,
11676 endlink)));
11677 tree v4hi_ftype_v4hi_di
11678 = build_function_type (V4HI_type_node,
11679 tree_cons (NULL_TREE, V4HI_type_node,
11680 tree_cons (NULL_TREE,
11681 long_long_integer_type_node,
11682 endlink)));
11683 tree v2si_ftype_v2si_di
11684 = build_function_type (V2SI_type_node,
11685 tree_cons (NULL_TREE, V2SI_type_node,
11686 tree_cons (NULL_TREE,
11687 long_long_integer_type_node,
11688 endlink)));
11689 tree void_ftype_int_int
11690 = build_function_type (void_type_node,
11691 tree_cons (NULL_TREE, integer_type_node,
11692 tree_cons (NULL_TREE, integer_type_node,
11693 endlink)));
11694 tree di_ftype_void
11695 = build_function_type (long_long_unsigned_type_node, endlink);
11696 tree di_ftype_v8qi
11697 = build_function_type (long_long_integer_type_node,
11698 tree_cons (NULL_TREE, V8QI_type_node,
11699 endlink));
11700 tree di_ftype_v4hi
11701 = build_function_type (long_long_integer_type_node,
11702 tree_cons (NULL_TREE, V4HI_type_node,
11703 endlink));
11704 tree di_ftype_v2si
11705 = build_function_type (long_long_integer_type_node,
11706 tree_cons (NULL_TREE, V2SI_type_node,
11707 endlink));
11708 tree v2si_ftype_v4hi
11709 = build_function_type (V2SI_type_node,
11710 tree_cons (NULL_TREE, V4HI_type_node,
11711 endlink));
11712 tree v4hi_ftype_v8qi
11713 = build_function_type (V4HI_type_node,
11714 tree_cons (NULL_TREE, V8QI_type_node,
11715 endlink));
11717 tree di_ftype_di_v4hi_v4hi
11718 = build_function_type (long_long_unsigned_type_node,
11719 tree_cons (NULL_TREE,
11720 long_long_unsigned_type_node,
11721 tree_cons (NULL_TREE, V4HI_type_node,
11722 tree_cons (NULL_TREE,
11723 V4HI_type_node,
11724 endlink))));
11726 tree di_ftype_v4hi_v4hi
11727 = build_function_type (long_long_unsigned_type_node,
11728 tree_cons (NULL_TREE, V4HI_type_node,
11729 tree_cons (NULL_TREE, V4HI_type_node,
11730 endlink)));
11732 /* Normal vector binops. */
11733 tree v8qi_ftype_v8qi_v8qi
11734 = build_function_type (V8QI_type_node,
11735 tree_cons (NULL_TREE, V8QI_type_node,
11736 tree_cons (NULL_TREE, V8QI_type_node,
11737 endlink)));
11738 tree v4hi_ftype_v4hi_v4hi
11739 = build_function_type (V4HI_type_node,
11740 tree_cons (NULL_TREE, V4HI_type_node,
11741 tree_cons (NULL_TREE, V4HI_type_node,
11742 endlink)));
11743 tree v2si_ftype_v2si_v2si
11744 = build_function_type (V2SI_type_node,
11745 tree_cons (NULL_TREE, V2SI_type_node,
11746 tree_cons (NULL_TREE, V2SI_type_node,
11747 endlink)));
11748 tree di_ftype_di_di
11749 = build_function_type (long_long_unsigned_type_node,
11750 tree_cons (NULL_TREE, long_long_unsigned_type_node,
11751 tree_cons (NULL_TREE,
11752 long_long_unsigned_type_node,
11753 endlink)));
11755 /* Add all builtins that are more or less simple operations on two
11756 operands. */
11757 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11759 /* Use one of the operands; the target can have a different mode for
11760 mask-generating compares. */
11761 enum machine_mode mode;
11762 tree type;
11764 if (d->name == 0)
11765 continue;
11767 mode = insn_data[d->icode].operand[1].mode;
11769 switch (mode)
11771 case V8QImode:
11772 type = v8qi_ftype_v8qi_v8qi;
11773 break;
11774 case V4HImode:
11775 type = v4hi_ftype_v4hi_v4hi;
11776 break;
11777 case V2SImode:
11778 type = v2si_ftype_v2si_v2si;
11779 break;
11780 case DImode:
11781 type = di_ftype_di_di;
11782 break;
11784 default:
11785 abort ();
11788 def_mbuiltin (d->mask, d->name, type, d->code);
11791 /* Add the remaining MMX insns with somewhat more complicated types. */
11792 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11793 def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11794 def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11796 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11797 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11798 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11799 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11800 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11801 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11803 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11804 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11805 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11806 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11807 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11808 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11810 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11811 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11812 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11813 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11814 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11815 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11817 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11818 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11819 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11820 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11821 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11822 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11824 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11826 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11827 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11828 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11829 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11831 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11832 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11833 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11834 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11835 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11836 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11837 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11838 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11839 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11841 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11842 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11843 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11845 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11846 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11847 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
11849 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
11850 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
11851 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
11852 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
11853 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
11854 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
11856 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
11857 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
11858 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
11859 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
11860 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
11861 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
11862 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
11863 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
11864 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
11865 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
11866 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
11867 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
11869 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
11870 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
11871 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
11872 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
11874 def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
11875 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
11876 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
11877 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
11878 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
11879 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
11880 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
11883 static void
11884 arm_init_builtins (void)
11886 if (TARGET_REALLY_IWMMXT)
11887 arm_init_iwmmxt_builtins ();
11890 /* Errors in the source file can cause expand_expr to return const0_rtx
11891 where we expect a vector. To avoid crashing, use one of the vector
11892 clear instructions. */
11894 static rtx
11895 safe_vector_operand (rtx x, enum machine_mode mode)
11897 if (x != const0_rtx)
11898 return x;
11899 x = gen_reg_rtx (mode);
11901 emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
11902 : gen_rtx_SUBREG (DImode, x, 0)));
11903 return x;
11906 /* Subroutine of arm_expand_builtin to take care of binop insns. */
11908 static rtx
11909 arm_expand_binop_builtin (enum insn_code icode,
11910 tree arglist, rtx target)
11912 rtx pat;
11913 tree arg0 = TREE_VALUE (arglist);
11914 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
11915 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11916 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
11917 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11918 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11919 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11921 if (VECTOR_MODE_P (mode0))
11922 op0 = safe_vector_operand (op0, mode0);
11923 if (VECTOR_MODE_P (mode1))
11924 op1 = safe_vector_operand (op1, mode1);
11926 if (! target
11927 || GET_MODE (target) != tmode
11928 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11929 target = gen_reg_rtx (tmode);
11931 /* In case the insn wants input operands in modes different from
11932 the result, abort. */
11933 if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
11934 abort ();
11936 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11937 op0 = copy_to_mode_reg (mode0, op0);
11938 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11939 op1 = copy_to_mode_reg (mode1, op1);
11941 pat = GEN_FCN (icode) (target, op0, op1);
11942 if (! pat)
11943 return 0;
11944 emit_insn (pat);
11945 return target;
11948 /* Subroutine of arm_expand_builtin to take care of unop insns. */
11950 static rtx
11951 arm_expand_unop_builtin (enum insn_code icode,
11952 tree arglist, rtx target, int do_load)
11954 rtx pat;
11955 tree arg0 = TREE_VALUE (arglist);
11956 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
11957 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11958 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11960 if (! target
11961 || GET_MODE (target) != tmode
11962 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11963 target = gen_reg_rtx (tmode);
11964 if (do_load)
11965 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11966 else
11968 if (VECTOR_MODE_P (mode0))
11969 op0 = safe_vector_operand (op0, mode0);
11971 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11972 op0 = copy_to_mode_reg (mode0, op0);
11975 pat = GEN_FCN (icode) (target, op0);
11976 if (! pat)
11977 return 0;
11978 emit_insn (pat);
11979 return target;
11982 /* Expand an expression EXP that calls a built-in function,
11983 with result going to TARGET if that's convenient
11984 (and in mode MODE if that's convenient).
11985 SUBTARGET may be used as the target for computing one of EXP's operands.
11986 IGNORE is nonzero if the value is to be ignored. */
11988 static rtx
11989 arm_expand_builtin (tree exp,
11990 rtx target,
11991 rtx subtarget ATTRIBUTE_UNUSED,
11992 enum machine_mode mode ATTRIBUTE_UNUSED,
11993 int ignore ATTRIBUTE_UNUSED)
11995 const struct builtin_description * d;
11996 enum insn_code icode;
11997 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
11998 tree arglist = TREE_OPERAND (exp, 1);
11999 tree arg0;
12000 tree arg1;
12001 tree arg2;
12002 rtx op0;
12003 rtx op1;
12004 rtx op2;
12005 rtx pat;
12006 int fcode = DECL_FUNCTION_CODE (fndecl);
12007 size_t i;
12008 enum machine_mode tmode;
12009 enum machine_mode mode0;
12010 enum machine_mode mode1;
12011 enum machine_mode mode2;
12013 switch (fcode)
12015 case ARM_BUILTIN_TEXTRMSB:
12016 case ARM_BUILTIN_TEXTRMUB:
12017 case ARM_BUILTIN_TEXTRMSH:
12018 case ARM_BUILTIN_TEXTRMUH:
12019 case ARM_BUILTIN_TEXTRMSW:
12020 case ARM_BUILTIN_TEXTRMUW:
12021 icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12022 : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12023 : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12024 : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12025 : CODE_FOR_iwmmxt_textrmw);
12027 arg0 = TREE_VALUE (arglist);
12028 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12029 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12030 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12031 tmode = insn_data[icode].operand[0].mode;
12032 mode0 = insn_data[icode].operand[1].mode;
12033 mode1 = insn_data[icode].operand[2].mode;
12035 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12036 op0 = copy_to_mode_reg (mode0, op0);
12037 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12039 /* @@@ better error message */
12040 error ("selector must be an immediate");
12041 return gen_reg_rtx (tmode);
12043 if (target == 0
12044 || GET_MODE (target) != tmode
12045 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12046 target = gen_reg_rtx (tmode);
12047 pat = GEN_FCN (icode) (target, op0, op1);
12048 if (! pat)
12049 return 0;
12050 emit_insn (pat);
12051 return target;
12053 case ARM_BUILTIN_TINSRB:
12054 case ARM_BUILTIN_TINSRH:
12055 case ARM_BUILTIN_TINSRW:
12056 icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12057 : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12058 : CODE_FOR_iwmmxt_tinsrw);
12059 arg0 = TREE_VALUE (arglist);
12060 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12061 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12062 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12063 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12064 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12065 tmode = insn_data[icode].operand[0].mode;
12066 mode0 = insn_data[icode].operand[1].mode;
12067 mode1 = insn_data[icode].operand[2].mode;
12068 mode2 = insn_data[icode].operand[3].mode;
12070 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12071 op0 = copy_to_mode_reg (mode0, op0);
12072 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12073 op1 = copy_to_mode_reg (mode1, op1);
12074 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12076 /* @@@ better error message */
12077 error ("selector must be an immediate");
12078 return const0_rtx;
12080 if (target == 0
12081 || GET_MODE (target) != tmode
12082 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12083 target = gen_reg_rtx (tmode);
12084 pat = GEN_FCN (icode) (target, op0, op1, op2);
12085 if (! pat)
12086 return 0;
12087 emit_insn (pat);
12088 return target;
12090 case ARM_BUILTIN_SETWCX:
12091 arg0 = TREE_VALUE (arglist);
12092 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12093 op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12094 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12095 emit_insn (gen_iwmmxt_tmcr (op1, op0));
12096 return 0;
12098 case ARM_BUILTIN_GETWCX:
12099 arg0 = TREE_VALUE (arglist);
12100 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12101 target = gen_reg_rtx (SImode);
12102 emit_insn (gen_iwmmxt_tmrc (target, op0));
12103 return target;
12105 case ARM_BUILTIN_WSHUFH:
12106 icode = CODE_FOR_iwmmxt_wshufh;
12107 arg0 = TREE_VALUE (arglist);
12108 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12109 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12110 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12111 tmode = insn_data[icode].operand[0].mode;
12112 mode1 = insn_data[icode].operand[1].mode;
12113 mode2 = insn_data[icode].operand[2].mode;
12115 if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12116 op0 = copy_to_mode_reg (mode1, op0);
12117 if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12119 /* @@@ better error message */
12120 error ("mask must be an immediate");
12121 return const0_rtx;
12123 if (target == 0
12124 || GET_MODE (target) != tmode
12125 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12126 target = gen_reg_rtx (tmode);
12127 pat = GEN_FCN (icode) (target, op0, op1);
12128 if (! pat)
12129 return 0;
12130 emit_insn (pat);
12131 return target;
12133 case ARM_BUILTIN_WSADB:
12134 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12135 case ARM_BUILTIN_WSADH:
12136 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12137 case ARM_BUILTIN_WSADBZ:
12138 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12139 case ARM_BUILTIN_WSADHZ:
12140 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12142 /* Several three-argument builtins. */
12143 case ARM_BUILTIN_WMACS:
12144 case ARM_BUILTIN_WMACU:
12145 case ARM_BUILTIN_WALIGN:
12146 case ARM_BUILTIN_TMIA:
12147 case ARM_BUILTIN_TMIAPH:
12148 case ARM_BUILTIN_TMIATT:
12149 case ARM_BUILTIN_TMIATB:
12150 case ARM_BUILTIN_TMIABT:
12151 case ARM_BUILTIN_TMIABB:
12152 icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12153 : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12154 : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12155 : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12156 : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12157 : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12158 : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12159 : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12160 : CODE_FOR_iwmmxt_walign);
12161 arg0 = TREE_VALUE (arglist);
12162 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12163 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12164 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12165 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12166 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12167 tmode = insn_data[icode].operand[0].mode;
12168 mode0 = insn_data[icode].operand[1].mode;
12169 mode1 = insn_data[icode].operand[2].mode;
12170 mode2 = insn_data[icode].operand[3].mode;
12172 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12173 op0 = copy_to_mode_reg (mode0, op0);
12174 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12175 op1 = copy_to_mode_reg (mode1, op1);
12176 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12177 op2 = copy_to_mode_reg (mode2, op2);
12178 if (target == 0
12179 || GET_MODE (target) != tmode
12180 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12181 target = gen_reg_rtx (tmode);
12182 pat = GEN_FCN (icode) (target, op0, op1, op2);
12183 if (! pat)
12184 return 0;
12185 emit_insn (pat);
12186 return target;
12188 case ARM_BUILTIN_WZERO:
12189 target = gen_reg_rtx (DImode);
12190 emit_insn (gen_iwmmxt_clrdi (target));
12191 return target;
12193 default:
12194 break;
12197 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12198 if (d->code == (const enum arm_builtins) fcode)
12199 return arm_expand_binop_builtin (d->icode, arglist, target);
12201 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12202 if (d->code == (const enum arm_builtins) fcode)
12203 return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12205 /* @@@ Should really do something sensible here. */
12206 return NULL_RTX;
12209 /* Return the number (counting from 0) of
12210 the least significant set bit in MASK. */
12212 inline static int
12213 number_of_first_bit_set (unsigned mask)
12215 int bit;
12217 for (bit = 0;
12218 (mask & (1 << bit)) == 0;
12219 ++bit)
12220 continue;
12222 return bit;
12225 /* Emit code to push or pop registers to or from the stack. F is the
12226 assembly file. MASK is the registers to push or pop. PUSH is
12227 nonzero if we should push, and zero if we should pop. For debugging
12228 output, if pushing, adjust CFA_OFFSET by the amount of space added
12229 to the stack. REAL_REGS should have the same number of bits set as
12230 MASK, and will be used instead (in the same order) to describe which
12231 registers were saved - this is used to mark the save slots when we
12232 push high registers after moving them to low registers. */
12233 static void
12234 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
12235 unsigned long real_regs)
12237 int regno;
12238 int lo_mask = mask & 0xFF;
12239 int pushed_words = 0;
12241 if (mask == 0)
12242 abort ();
12244 if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12246 /* Special case. Do not generate a POP PC statement here, do it in
12247 thumb_exit() */
12248 thumb_exit (f, -1);
12249 return;
12252 fprintf (f, "\t%s\t{", push ? "push" : "pop");
12254 /* Look at the low registers first. */
12255 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12257 if (lo_mask & 1)
12259 asm_fprintf (f, "%r", regno);
12261 if ((lo_mask & ~1) != 0)
12262 fprintf (f, ", ");
12264 pushed_words++;
12268 if (push && (mask & (1 << LR_REGNUM)))
12270 /* Catch pushing the LR. */
12271 if (mask & 0xFF)
12272 fprintf (f, ", ");
12274 asm_fprintf (f, "%r", LR_REGNUM);
12276 pushed_words++;
12278 else if (!push && (mask & (1 << PC_REGNUM)))
12280 /* Catch popping the PC. */
12281 if (TARGET_INTERWORK || TARGET_BACKTRACE
12282 || current_function_calls_eh_return)
12284 /* The PC is never poped directly, instead
12285 it is popped into r3 and then BX is used. */
12286 fprintf (f, "}\n");
12288 thumb_exit (f, -1);
12290 return;
12292 else
12294 if (mask & 0xFF)
12295 fprintf (f, ", ");
12297 asm_fprintf (f, "%r", PC_REGNUM);
12301 fprintf (f, "}\n");
12303 if (push && pushed_words && dwarf2out_do_frame ())
12305 char *l = dwarf2out_cfi_label ();
12306 int pushed_mask = real_regs;
12308 *cfa_offset += pushed_words * 4;
12309 dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12311 pushed_words = 0;
12312 pushed_mask = real_regs;
12313 for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12315 if (pushed_mask & 1)
12316 dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12321 /* Generate code to return from a thumb function.
12322 If 'reg_containing_return_addr' is -1, then the return address is
12323 actually on the stack, at the stack pointer. */
12324 static void
12325 thumb_exit (FILE *f, int reg_containing_return_addr)
12327 unsigned regs_available_for_popping;
12328 unsigned regs_to_pop;
12329 int pops_needed;
12330 unsigned available;
12331 unsigned required;
12332 int mode;
12333 int size;
12334 int restore_a4 = FALSE;
12336 /* Compute the registers we need to pop. */
12337 regs_to_pop = 0;
12338 pops_needed = 0;
12340 if (reg_containing_return_addr == -1)
12342 regs_to_pop |= 1 << LR_REGNUM;
12343 ++pops_needed;
12346 if (TARGET_BACKTRACE)
12348 /* Restore the (ARM) frame pointer and stack pointer. */
12349 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12350 pops_needed += 2;
12353 /* If there is nothing to pop then just emit the BX instruction and
12354 return. */
12355 if (pops_needed == 0)
12357 if (current_function_calls_eh_return)
12358 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12360 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12361 return;
12363 /* Otherwise if we are not supporting interworking and we have not created
12364 a backtrace structure and the function was not entered in ARM mode then
12365 just pop the return address straight into the PC. */
12366 else if (!TARGET_INTERWORK
12367 && !TARGET_BACKTRACE
12368 && !is_called_in_ARM_mode (current_function_decl)
12369 && !current_function_calls_eh_return)
12371 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12372 return;
12375 /* Find out how many of the (return) argument registers we can corrupt. */
12376 regs_available_for_popping = 0;
12378 /* If returning via __builtin_eh_return, the bottom three registers
12379 all contain information needed for the return. */
12380 if (current_function_calls_eh_return)
12381 size = 12;
12382 else
12384 /* If we can deduce the registers used from the function's
12385 return value. This is more reliable that examining
12386 regs_ever_live[] because that will be set if the register is
12387 ever used in the function, not just if the register is used
12388 to hold a return value. */
12390 if (current_function_return_rtx != 0)
12391 mode = GET_MODE (current_function_return_rtx);
12392 else
12393 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12395 size = GET_MODE_SIZE (mode);
12397 if (size == 0)
12399 /* In a void function we can use any argument register.
12400 In a function that returns a structure on the stack
12401 we can use the second and third argument registers. */
12402 if (mode == VOIDmode)
12403 regs_available_for_popping =
12404 (1 << ARG_REGISTER (1))
12405 | (1 << ARG_REGISTER (2))
12406 | (1 << ARG_REGISTER (3));
12407 else
12408 regs_available_for_popping =
12409 (1 << ARG_REGISTER (2))
12410 | (1 << ARG_REGISTER (3));
12412 else if (size <= 4)
12413 regs_available_for_popping =
12414 (1 << ARG_REGISTER (2))
12415 | (1 << ARG_REGISTER (3));
12416 else if (size <= 8)
12417 regs_available_for_popping =
12418 (1 << ARG_REGISTER (3));
12421 /* Match registers to be popped with registers into which we pop them. */
12422 for (available = regs_available_for_popping,
12423 required = regs_to_pop;
12424 required != 0 && available != 0;
12425 available &= ~(available & - available),
12426 required &= ~(required & - required))
12427 -- pops_needed;
12429 /* If we have any popping registers left over, remove them. */
12430 if (available > 0)
12431 regs_available_for_popping &= ~available;
12433 /* Otherwise if we need another popping register we can use
12434 the fourth argument register. */
12435 else if (pops_needed)
12437 /* If we have not found any free argument registers and
12438 reg a4 contains the return address, we must move it. */
12439 if (regs_available_for_popping == 0
12440 && reg_containing_return_addr == LAST_ARG_REGNUM)
12442 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12443 reg_containing_return_addr = LR_REGNUM;
12445 else if (size > 12)
12447 /* Register a4 is being used to hold part of the return value,
12448 but we have dire need of a free, low register. */
12449 restore_a4 = TRUE;
12451 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12454 if (reg_containing_return_addr != LAST_ARG_REGNUM)
12456 /* The fourth argument register is available. */
12457 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12459 --pops_needed;
12463 /* Pop as many registers as we can. */
12464 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12465 regs_available_for_popping);
12467 /* Process the registers we popped. */
12468 if (reg_containing_return_addr == -1)
12470 /* The return address was popped into the lowest numbered register. */
12471 regs_to_pop &= ~(1 << LR_REGNUM);
12473 reg_containing_return_addr =
12474 number_of_first_bit_set (regs_available_for_popping);
12476 /* Remove this register for the mask of available registers, so that
12477 the return address will not be corrupted by further pops. */
12478 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12481 /* If we popped other registers then handle them here. */
12482 if (regs_available_for_popping)
12484 int frame_pointer;
12486 /* Work out which register currently contains the frame pointer. */
12487 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12489 /* Move it into the correct place. */
12490 asm_fprintf (f, "\tmov\t%r, %r\n",
12491 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12493 /* (Temporarily) remove it from the mask of popped registers. */
12494 regs_available_for_popping &= ~(1 << frame_pointer);
12495 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12497 if (regs_available_for_popping)
12499 int stack_pointer;
12501 /* We popped the stack pointer as well,
12502 find the register that contains it. */
12503 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12505 /* Move it into the stack register. */
12506 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12508 /* At this point we have popped all necessary registers, so
12509 do not worry about restoring regs_available_for_popping
12510 to its correct value:
12512 assert (pops_needed == 0)
12513 assert (regs_available_for_popping == (1 << frame_pointer))
12514 assert (regs_to_pop == (1 << STACK_POINTER)) */
12516 else
12518 /* Since we have just move the popped value into the frame
12519 pointer, the popping register is available for reuse, and
12520 we know that we still have the stack pointer left to pop. */
12521 regs_available_for_popping |= (1 << frame_pointer);
12525 /* If we still have registers left on the stack, but we no longer have
12526 any registers into which we can pop them, then we must move the return
12527 address into the link register and make available the register that
12528 contained it. */
12529 if (regs_available_for_popping == 0 && pops_needed > 0)
12531 regs_available_for_popping |= 1 << reg_containing_return_addr;
12533 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12534 reg_containing_return_addr);
12536 reg_containing_return_addr = LR_REGNUM;
12539 /* If we have registers left on the stack then pop some more.
12540 We know that at most we will want to pop FP and SP. */
12541 if (pops_needed > 0)
12543 int popped_into;
12544 int move_to;
12546 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12547 regs_available_for_popping);
12549 /* We have popped either FP or SP.
12550 Move whichever one it is into the correct register. */
12551 popped_into = number_of_first_bit_set (regs_available_for_popping);
12552 move_to = number_of_first_bit_set (regs_to_pop);
12554 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12556 regs_to_pop &= ~(1 << move_to);
12558 --pops_needed;
12561 /* If we still have not popped everything then we must have only
12562 had one register available to us and we are now popping the SP. */
12563 if (pops_needed > 0)
12565 int popped_into;
12567 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12568 regs_available_for_popping);
12570 popped_into = number_of_first_bit_set (regs_available_for_popping);
12572 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12574 assert (regs_to_pop == (1 << STACK_POINTER))
12575 assert (pops_needed == 1)
12579 /* If necessary restore the a4 register. */
12580 if (restore_a4)
12582 if (reg_containing_return_addr != LR_REGNUM)
12584 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12585 reg_containing_return_addr = LR_REGNUM;
12588 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12591 if (current_function_calls_eh_return)
12592 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12594 /* Return to caller. */
12595 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12599 void
12600 thumb_final_prescan_insn (rtx insn)
12602 if (flag_print_asm_name)
12603 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12604 INSN_ADDRESSES (INSN_UID (insn)));
12608 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12610 unsigned HOST_WIDE_INT mask = 0xff;
12611 int i;
12613 if (val == 0) /* XXX */
12614 return 0;
12616 for (i = 0; i < 25; i++)
12617 if ((val & (mask << i)) == val)
12618 return 1;
12620 return 0;
12623 /* Returns nonzero if the current function contains,
12624 or might contain a far jump. */
12625 static int
12626 thumb_far_jump_used_p (void)
12628 rtx insn;
12630 /* This test is only important for leaf functions. */
12631 /* assert (!leaf_function_p ()); */
12633 /* If we have already decided that far jumps may be used,
12634 do not bother checking again, and always return true even if
12635 it turns out that they are not being used. Once we have made
12636 the decision that far jumps are present (and that hence the link
12637 register will be pushed onto the stack) we cannot go back on it. */
12638 if (cfun->machine->far_jump_used)
12639 return 1;
12641 /* If this function is not being called from the prologue/epilogue
12642 generation code then it must be being called from the
12643 INITIAL_ELIMINATION_OFFSET macro. */
12644 if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12646 /* In this case we know that we are being asked about the elimination
12647 of the arg pointer register. If that register is not being used,
12648 then there are no arguments on the stack, and we do not have to
12649 worry that a far jump might force the prologue to push the link
12650 register, changing the stack offsets. In this case we can just
12651 return false, since the presence of far jumps in the function will
12652 not affect stack offsets.
12654 If the arg pointer is live (or if it was live, but has now been
12655 eliminated and so set to dead) then we do have to test to see if
12656 the function might contain a far jump. This test can lead to some
12657 false negatives, since before reload is completed, then length of
12658 branch instructions is not known, so gcc defaults to returning their
12659 longest length, which in turn sets the far jump attribute to true.
12661 A false negative will not result in bad code being generated, but it
12662 will result in a needless push and pop of the link register. We
12663 hope that this does not occur too often.
12665 If we need doubleword stack alignment this could affect the other
12666 elimination offsets so we can't risk getting it wrong. */
12667 if (regs_ever_live [ARG_POINTER_REGNUM])
12668 cfun->machine->arg_pointer_live = 1;
12669 else if (!cfun->machine->arg_pointer_live)
12670 return 0;
12673 /* Check to see if the function contains a branch
12674 insn with the far jump attribute set. */
12675 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12677 if (GET_CODE (insn) == JUMP_INSN
12678 /* Ignore tablejump patterns. */
12679 && GET_CODE (PATTERN (insn)) != ADDR_VEC
12680 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12681 && get_attr_far_jump (insn) == FAR_JUMP_YES
12684 /* Record the fact that we have decided that
12685 the function does use far jumps. */
12686 cfun->machine->far_jump_used = 1;
12687 return 1;
12691 return 0;
12694 /* Return nonzero if FUNC must be entered in ARM mode. */
12696 is_called_in_ARM_mode (tree func)
12698 if (TREE_CODE (func) != FUNCTION_DECL)
12699 abort ();
12701 /* Ignore the problem about functions whose address is taken. */
12702 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12703 return TRUE;
12705 #ifdef ARM_PE
12706 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12707 #else
12708 return FALSE;
12709 #endif
12712 /* The bits which aren't usefully expanded as rtl. */
12713 const char *
12714 thumb_unexpanded_epilogue (void)
12716 int regno;
12717 unsigned long live_regs_mask = 0;
12718 int high_regs_pushed = 0;
12719 int had_to_push_lr;
12720 int size;
12721 int mode;
12723 if (return_used_this_function)
12724 return "";
12726 if (IS_NAKED (arm_current_func_type ()))
12727 return "";
12729 live_regs_mask = thumb_compute_save_reg_mask ();
12730 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
12732 /* If we can deduce the registers used from the function's return value.
12733 This is more reliable that examining regs_ever_live[] because that
12734 will be set if the register is ever used in the function, not just if
12735 the register is used to hold a return value. */
12737 if (current_function_return_rtx != 0)
12738 mode = GET_MODE (current_function_return_rtx);
12739 else
12740 mode = DECL_MODE (DECL_RESULT (current_function_decl));
12742 size = GET_MODE_SIZE (mode);
12744 /* The prolog may have pushed some high registers to use as
12745 work registers. e.g. the testsuite file:
12746 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12747 compiles to produce:
12748 push {r4, r5, r6, r7, lr}
12749 mov r7, r9
12750 mov r6, r8
12751 push {r6, r7}
12752 as part of the prolog. We have to undo that pushing here. */
12754 if (high_regs_pushed)
12756 unsigned long mask = live_regs_mask & 0xff;
12757 int next_hi_reg;
12759 /* The available low registers depend on the size of the value we are
12760 returning. */
12761 if (size <= 12)
12762 mask |= 1 << 3;
12763 if (size <= 8)
12764 mask |= 1 << 2;
12766 if (mask == 0)
12767 /* Oh dear! We have no low registers into which we can pop
12768 high registers! */
12769 internal_error
12770 ("no low registers available for popping high registers");
12772 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12773 if (live_regs_mask & (1 << next_hi_reg))
12774 break;
12776 while (high_regs_pushed)
12778 /* Find lo register(s) into which the high register(s) can
12779 be popped. */
12780 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12782 if (mask & (1 << regno))
12783 high_regs_pushed--;
12784 if (high_regs_pushed == 0)
12785 break;
12788 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
12790 /* Pop the values into the low register(s). */
12791 thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
12793 /* Move the value(s) into the high registers. */
12794 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12796 if (mask & (1 << regno))
12798 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12799 regno);
12801 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
12802 if (live_regs_mask & (1 << next_hi_reg))
12803 break;
12807 live_regs_mask &= ~0x0f00;
12810 had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
12811 live_regs_mask &= 0xff;
12813 if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12815 /* Pop the return address into the PC. */
12816 if (had_to_push_lr)
12817 live_regs_mask |= 1 << PC_REGNUM;
12819 /* Either no argument registers were pushed or a backtrace
12820 structure was created which includes an adjusted stack
12821 pointer, so just pop everything. */
12822 if (live_regs_mask)
12823 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12824 live_regs_mask);
12826 /* We have either just popped the return address into the
12827 PC or it is was kept in LR for the entire function. */
12828 if (!had_to_push_lr)
12829 thumb_exit (asm_out_file, LR_REGNUM);
12831 else
12833 /* Pop everything but the return address. */
12834 if (live_regs_mask)
12835 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12836 live_regs_mask);
12838 if (had_to_push_lr)
12840 if (size > 12)
12842 /* We have no free low regs, so save one. */
12843 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
12844 LAST_ARG_REGNUM);
12847 /* Get the return address into a temporary register. */
12848 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
12849 1 << LAST_ARG_REGNUM);
12851 if (size > 12)
12853 /* Move the return address to lr. */
12854 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
12855 LAST_ARG_REGNUM);
12856 /* Restore the low register. */
12857 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
12858 IP_REGNUM);
12859 regno = LR_REGNUM;
12861 else
12862 regno = LAST_ARG_REGNUM;
12864 else
12865 regno = LR_REGNUM;
12867 /* Remove the argument registers that were pushed onto the stack. */
12868 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
12869 SP_REGNUM, SP_REGNUM,
12870 current_function_pretend_args_size);
12872 thumb_exit (asm_out_file, regno);
12875 return "";
12878 /* Functions to save and restore machine-specific function data. */
12879 static struct machine_function *
12880 arm_init_machine_status (void)
12882 struct machine_function *machine;
12883 machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
12885 #if ARM_FT_UNKNOWN != 0
12886 machine->func_type = ARM_FT_UNKNOWN;
12887 #endif
12888 return machine;
12891 /* Return an RTX indicating where the return address to the
12892 calling function can be found. */
12894 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
12896 if (count != 0)
12897 return NULL_RTX;
12899 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
12902 /* Do anything needed before RTL is emitted for each function. */
12903 void
12904 arm_init_expanders (void)
12906 /* Arrange to initialize and mark the machine per-function status. */
12907 init_machine_status = arm_init_machine_status;
12909 /* This is to stop the combine pass optimizing away the alignment
12910 adjustment of va_arg. */
12911 /* ??? It is claimed that this should not be necessary. */
12912 if (cfun)
12913 mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
12917 /* Like arm_compute_initial_elimination offset. Simpler because
12918 THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer. */
12920 HOST_WIDE_INT
12921 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
12923 arm_stack_offsets *offsets;
12925 offsets = arm_get_frame_offsets ();
12927 switch (from)
12929 case ARG_POINTER_REGNUM:
12930 switch (to)
12932 case STACK_POINTER_REGNUM:
12933 return offsets->outgoing_args - offsets->saved_args;
12935 case FRAME_POINTER_REGNUM:
12936 return offsets->soft_frame - offsets->saved_args;
12938 case THUMB_HARD_FRAME_POINTER_REGNUM:
12939 case ARM_HARD_FRAME_POINTER_REGNUM:
12940 return offsets->saved_regs - offsets->saved_args;
12942 default:
12943 abort();
12945 break;
12947 case FRAME_POINTER_REGNUM:
12948 switch (to)
12950 case STACK_POINTER_REGNUM:
12951 return offsets->outgoing_args - offsets->soft_frame;
12953 case THUMB_HARD_FRAME_POINTER_REGNUM:
12954 case ARM_HARD_FRAME_POINTER_REGNUM:
12955 return offsets->saved_regs - offsets->soft_frame;
12957 default:
12958 abort();
12960 break;
12962 default:
12963 abort ();
12968 /* Generate the rest of a function's prologue. */
12969 void
12970 thumb_expand_prologue (void)
12972 rtx insn, dwarf;
12974 HOST_WIDE_INT amount;
12975 arm_stack_offsets *offsets;
12976 unsigned long func_type;
12977 int regno;
12978 unsigned long live_regs_mask;
12980 func_type = arm_current_func_type ();
12982 /* Naked functions don't have prologues. */
12983 if (IS_NAKED (func_type))
12984 return;
12986 if (IS_INTERRUPT (func_type))
12988 error ("interrupt Service Routines cannot be coded in Thumb mode");
12989 return;
12992 live_regs_mask = thumb_compute_save_reg_mask ();
12993 /* Load the pic register before setting the frame pointer,
12994 so we can use r7 as a temporary work register. */
12995 if (flag_pic)
12996 arm_load_pic_register (thumb_find_work_register (live_regs_mask));
12998 offsets = arm_get_frame_offsets ();
13000 if (frame_pointer_needed)
13002 insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
13003 stack_pointer_rtx));
13004 RTX_FRAME_RELATED_P (insn) = 1;
13006 else if (CALLER_INTERWORKING_SLOT_SIZE > 0)
13007 emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
13008 stack_pointer_rtx);
13010 amount = offsets->outgoing_args - offsets->saved_regs;
13011 if (amount)
13013 if (amount < 512)
13015 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13016 GEN_INT (- amount)));
13017 RTX_FRAME_RELATED_P (insn) = 1;
13019 else
13021 rtx reg;
13023 /* The stack decrement is too big for an immediate value in a single
13024 insn. In theory we could issue multiple subtracts, but after
13025 three of them it becomes more space efficient to place the full
13026 value in the constant pool and load into a register. (Also the
13027 ARM debugger really likes to see only one stack decrement per
13028 function). So instead we look for a scratch register into which
13029 we can load the decrement, and then we subtract this from the
13030 stack pointer. Unfortunately on the thumb the only available
13031 scratch registers are the argument registers, and we cannot use
13032 these as they may hold arguments to the function. Instead we
13033 attempt to locate a call preserved register which is used by this
13034 function. If we can find one, then we know that it will have
13035 been pushed at the start of the prologue and so we can corrupt
13036 it now. */
13037 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13038 if (live_regs_mask & (1 << regno)
13039 && !(frame_pointer_needed
13040 && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13041 break;
13043 if (regno > LAST_LO_REGNUM) /* Very unlikely. */
13045 rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13047 /* Choose an arbitrary, non-argument low register. */
13048 reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13050 /* Save it by copying it into a high, scratch register. */
13051 emit_insn (gen_movsi (spare, reg));
13052 /* Add a USE to stop propagate_one_insn() from barfing. */
13053 emit_insn (gen_prologue_use (spare));
13055 /* Decrement the stack. */
13056 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13057 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13058 stack_pointer_rtx, reg));
13059 RTX_FRAME_RELATED_P (insn) = 1;
13060 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13061 plus_constant (stack_pointer_rtx,
13062 -amount));
13063 RTX_FRAME_RELATED_P (dwarf) = 1;
13064 REG_NOTES (insn)
13065 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13066 REG_NOTES (insn));
13068 /* Restore the low register's original value. */
13069 emit_insn (gen_movsi (reg, spare));
13071 /* Emit a USE of the restored scratch register, so that flow
13072 analysis will not consider the restore redundant. The
13073 register won't be used again in this function and isn't
13074 restored by the epilogue. */
13075 emit_insn (gen_prologue_use (reg));
13077 else
13079 reg = gen_rtx_REG (SImode, regno);
13081 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13083 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13084 stack_pointer_rtx, reg));
13085 RTX_FRAME_RELATED_P (insn) = 1;
13086 dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13087 plus_constant (stack_pointer_rtx,
13088 -amount));
13089 RTX_FRAME_RELATED_P (dwarf) = 1;
13090 REG_NOTES (insn)
13091 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13092 REG_NOTES (insn));
13095 /* If the frame pointer is needed, emit a special barrier that
13096 will prevent the scheduler from moving stores to the frame
13097 before the stack adjustment. */
13098 if (frame_pointer_needed)
13099 emit_insn (gen_stack_tie (stack_pointer_rtx,
13100 hard_frame_pointer_rtx));
13103 if (current_function_profile || TARGET_NO_SCHED_PRO)
13104 emit_insn (gen_blockage ());
13106 cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13107 if (live_regs_mask & 0xff)
13108 cfun->machine->lr_save_eliminated = 0;
13110 /* If the link register is being kept alive, with the return address in it,
13111 then make sure that it does not get reused by the ce2 pass. */
13112 if (cfun->machine->lr_save_eliminated)
13113 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13117 void
13118 thumb_expand_epilogue (void)
13120 HOST_WIDE_INT amount;
13121 arm_stack_offsets *offsets;
13122 int regno;
13124 /* Naked functions don't have prologues. */
13125 if (IS_NAKED (arm_current_func_type ()))
13126 return;
13128 offsets = arm_get_frame_offsets ();
13129 amount = offsets->outgoing_args - offsets->saved_regs;
13131 if (frame_pointer_needed)
13132 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13133 else if (amount)
13135 if (amount < 512)
13136 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13137 GEN_INT (amount)));
13138 else
13140 /* r3 is always free in the epilogue. */
13141 rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13143 emit_insn (gen_movsi (reg, GEN_INT (amount)));
13144 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13148 /* Emit a USE (stack_pointer_rtx), so that
13149 the stack adjustment will not be deleted. */
13150 emit_insn (gen_prologue_use (stack_pointer_rtx));
13152 if (current_function_profile || TARGET_NO_SCHED_PRO)
13153 emit_insn (gen_blockage ());
13155 /* Emit a clobber for each insn that will be restored in the epilogue,
13156 so that flow2 will get register lifetimes correct. */
13157 for (regno = 0; regno < 13; regno++)
13158 if (regs_ever_live[regno] && !call_used_regs[regno])
13159 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13161 if (! regs_ever_live[LR_REGNUM])
13162 emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13165 static void
13166 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13168 unsigned long live_regs_mask = 0;
13169 unsigned long l_mask;
13170 unsigned high_regs_pushed = 0;
13171 int cfa_offset = 0;
13172 int regno;
13174 if (IS_NAKED (arm_current_func_type ()))
13175 return;
13177 if (is_called_in_ARM_mode (current_function_decl))
13179 const char * name;
13181 if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
13182 abort ();
13183 if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
13184 abort ();
13185 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
13187 /* Generate code sequence to switch us into Thumb mode. */
13188 /* The .code 32 directive has already been emitted by
13189 ASM_DECLARE_FUNCTION_NAME. */
13190 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13191 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13193 /* Generate a label, so that the debugger will notice the
13194 change in instruction sets. This label is also used by
13195 the assembler to bypass the ARM code when this function
13196 is called from a Thumb encoded function elsewhere in the
13197 same file. Hence the definition of STUB_NAME here must
13198 agree with the definition in gas/config/tc-arm.c. */
13200 #define STUB_NAME ".real_start_of"
13202 fprintf (f, "\t.code\t16\n");
13203 #ifdef ARM_PE
13204 if (arm_dllexport_name_p (name))
13205 name = arm_strip_name_encoding (name);
13206 #endif
13207 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13208 fprintf (f, "\t.thumb_func\n");
13209 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13212 if (current_function_pretend_args_size)
13214 if (cfun->machine->uses_anonymous_args)
13216 int num_pushes;
13218 fprintf (f, "\tpush\t{");
13220 num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13222 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13223 regno <= LAST_ARG_REGNUM;
13224 regno++)
13225 asm_fprintf (f, "%r%s", regno,
13226 regno == LAST_ARG_REGNUM ? "" : ", ");
13228 fprintf (f, "}\n");
13230 else
13231 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13232 SP_REGNUM, SP_REGNUM,
13233 current_function_pretend_args_size);
13235 /* We don't need to record the stores for unwinding (would it
13236 help the debugger any if we did?), but record the change in
13237 the stack pointer. */
13238 if (dwarf2out_do_frame ())
13240 char *l = dwarf2out_cfi_label ();
13242 cfa_offset = cfa_offset + current_function_pretend_args_size;
13243 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13247 /* Get the registers we are going to push. */
13248 live_regs_mask = thumb_compute_save_reg_mask ();
13249 /* Extract a mask of the ones we can give to the Thumb's push instruction. */
13250 l_mask = live_regs_mask & 0x40ff;
13251 /* Then count how many other high registers will need to be pushed. */
13252 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13254 if (TARGET_BACKTRACE)
13256 unsigned offset;
13257 unsigned work_register;
13259 /* We have been asked to create a stack backtrace structure.
13260 The code looks like this:
13262 0 .align 2
13263 0 func:
13264 0 sub SP, #16 Reserve space for 4 registers.
13265 2 push {R7} Push low registers.
13266 4 add R7, SP, #20 Get the stack pointer before the push.
13267 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
13268 8 mov R7, PC Get hold of the start of this code plus 12.
13269 10 str R7, [SP, #16] Store it.
13270 12 mov R7, FP Get hold of the current frame pointer.
13271 14 str R7, [SP, #4] Store it.
13272 16 mov R7, LR Get hold of the current return address.
13273 18 str R7, [SP, #12] Store it.
13274 20 add R7, SP, #16 Point at the start of the backtrace structure.
13275 22 mov FP, R7 Put this value into the frame pointer. */
13277 work_register = thumb_find_work_register (live_regs_mask);
13279 asm_fprintf
13280 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13281 SP_REGNUM, SP_REGNUM);
13283 if (dwarf2out_do_frame ())
13285 char *l = dwarf2out_cfi_label ();
13287 cfa_offset = cfa_offset + 16;
13288 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13291 if (l_mask)
13293 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13294 offset = bit_count (l_mask);
13296 else
13297 offset = 0;
13299 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13300 offset + 16 + current_function_pretend_args_size);
13302 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13303 offset + 4);
13305 /* Make sure that the instruction fetching the PC is in the right place
13306 to calculate "start of backtrace creation code + 12". */
13307 if (l_mask)
13309 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13310 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13311 offset + 12);
13312 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13313 ARM_HARD_FRAME_POINTER_REGNUM);
13314 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13315 offset);
13317 else
13319 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13320 ARM_HARD_FRAME_POINTER_REGNUM);
13321 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13322 offset);
13323 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13324 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13325 offset + 12);
13328 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13329 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13330 offset + 8);
13331 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13332 offset + 12);
13333 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13334 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13336 /* Optimisation: If we are not pushing any low registers but we are going
13337 to push some high registers then delay our first push. This will just
13338 be a push of LR and we can combine it with the push of the first high
13339 register. */
13340 else if ((l_mask & 0xff) != 0
13341 || (high_regs_pushed == 0 && l_mask))
13342 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13344 if (high_regs_pushed)
13346 unsigned pushable_regs;
13347 unsigned next_hi_reg;
13349 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13350 if (live_regs_mask & (1 << next_hi_reg))
13351 break;
13353 pushable_regs = l_mask & 0xff;
13355 if (pushable_regs == 0)
13356 pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
13358 while (high_regs_pushed > 0)
13360 unsigned long real_regs_mask = 0;
13362 for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
13364 if (pushable_regs & (1 << regno))
13366 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13368 high_regs_pushed --;
13369 real_regs_mask |= (1 << next_hi_reg);
13371 if (high_regs_pushed)
13373 for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
13374 next_hi_reg --)
13375 if (live_regs_mask & (1 << next_hi_reg))
13376 break;
13378 else
13380 pushable_regs &= ~((1 << regno) - 1);
13381 break;
13386 /* If we had to find a work register and we have not yet
13387 saved the LR then add it to the list of regs to push. */
13388 if (l_mask == (1 << LR_REGNUM))
13390 thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
13391 1, &cfa_offset,
13392 real_regs_mask | (1 << LR_REGNUM));
13393 l_mask = 0;
13395 else
13396 thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
13401 /* Handle the case of a double word load into a low register from
13402 a computed memory address. The computed address may involve a
13403 register which is overwritten by the load. */
13404 const char *
13405 thumb_load_double_from_address (rtx *operands)
13407 rtx addr;
13408 rtx base;
13409 rtx offset;
13410 rtx arg1;
13411 rtx arg2;
13413 if (GET_CODE (operands[0]) != REG)
13414 abort ();
13416 if (GET_CODE (operands[1]) != MEM)
13417 abort ();
13419 /* Get the memory address. */
13420 addr = XEXP (operands[1], 0);
13422 /* Work out how the memory address is computed. */
13423 switch (GET_CODE (addr))
13425 case REG:
13426 operands[2] = gen_rtx_MEM (SImode,
13427 plus_constant (XEXP (operands[1], 0), 4));
13429 if (REGNO (operands[0]) == REGNO (addr))
13431 output_asm_insn ("ldr\t%H0, %2", operands);
13432 output_asm_insn ("ldr\t%0, %1", operands);
13434 else
13436 output_asm_insn ("ldr\t%0, %1", operands);
13437 output_asm_insn ("ldr\t%H0, %2", operands);
13439 break;
13441 case CONST:
13442 /* Compute <address> + 4 for the high order load. */
13443 operands[2] = gen_rtx_MEM (SImode,
13444 plus_constant (XEXP (operands[1], 0), 4));
13446 output_asm_insn ("ldr\t%0, %1", operands);
13447 output_asm_insn ("ldr\t%H0, %2", operands);
13448 break;
13450 case PLUS:
13451 arg1 = XEXP (addr, 0);
13452 arg2 = XEXP (addr, 1);
13454 if (CONSTANT_P (arg1))
13455 base = arg2, offset = arg1;
13456 else
13457 base = arg1, offset = arg2;
13459 if (GET_CODE (base) != REG)
13460 abort ();
13462 /* Catch the case of <address> = <reg> + <reg> */
13463 if (GET_CODE (offset) == REG)
13465 int reg_offset = REGNO (offset);
13466 int reg_base = REGNO (base);
13467 int reg_dest = REGNO (operands[0]);
13469 /* Add the base and offset registers together into the
13470 higher destination register. */
13471 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13472 reg_dest + 1, reg_base, reg_offset);
13474 /* Load the lower destination register from the address in
13475 the higher destination register. */
13476 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13477 reg_dest, reg_dest + 1);
13479 /* Load the higher destination register from its own address
13480 plus 4. */
13481 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13482 reg_dest + 1, reg_dest + 1);
13484 else
13486 /* Compute <address> + 4 for the high order load. */
13487 operands[2] = gen_rtx_MEM (SImode,
13488 plus_constant (XEXP (operands[1], 0), 4));
13490 /* If the computed address is held in the low order register
13491 then load the high order register first, otherwise always
13492 load the low order register first. */
13493 if (REGNO (operands[0]) == REGNO (base))
13495 output_asm_insn ("ldr\t%H0, %2", operands);
13496 output_asm_insn ("ldr\t%0, %1", operands);
13498 else
13500 output_asm_insn ("ldr\t%0, %1", operands);
13501 output_asm_insn ("ldr\t%H0, %2", operands);
13504 break;
13506 case LABEL_REF:
13507 /* With no registers to worry about we can just load the value
13508 directly. */
13509 operands[2] = gen_rtx_MEM (SImode,
13510 plus_constant (XEXP (operands[1], 0), 4));
13512 output_asm_insn ("ldr\t%H0, %2", operands);
13513 output_asm_insn ("ldr\t%0, %1", operands);
13514 break;
13516 default:
13517 abort ();
13518 break;
13521 return "";
13524 const char *
13525 thumb_output_move_mem_multiple (int n, rtx *operands)
13527 rtx tmp;
13529 switch (n)
13531 case 2:
13532 if (REGNO (operands[4]) > REGNO (operands[5]))
13534 tmp = operands[4];
13535 operands[4] = operands[5];
13536 operands[5] = tmp;
13538 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13539 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13540 break;
13542 case 3:
13543 if (REGNO (operands[4]) > REGNO (operands[5]))
13545 tmp = operands[4];
13546 operands[4] = operands[5];
13547 operands[5] = tmp;
13549 if (REGNO (operands[5]) > REGNO (operands[6]))
13551 tmp = operands[5];
13552 operands[5] = operands[6];
13553 operands[6] = tmp;
13555 if (REGNO (operands[4]) > REGNO (operands[5]))
13557 tmp = operands[4];
13558 operands[4] = operands[5];
13559 operands[5] = tmp;
13562 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13563 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13564 break;
13566 default:
13567 abort ();
13570 return "";
13573 /* Output a call-via instruction for thumb state. */
13574 const char *
13575 thumb_call_via_reg (rtx reg)
13577 int regno = REGNO (reg);
13578 rtx *labelp;
13580 gcc_assert (regno < LR_REGNUM);
13582 /* If we are in the normal text section we can use a single instance
13583 per compilation unit. If we are doing function sections, then we need
13584 an entry per section, since we can't rely on reachability. */
13585 if (in_text_section ())
13587 thumb_call_reg_needed = 1;
13589 if (thumb_call_via_label[regno] == NULL)
13590 thumb_call_via_label[regno] = gen_label_rtx ();
13591 labelp = thumb_call_via_label + regno;
13593 else
13595 if (cfun->machine->call_via[regno] == NULL)
13596 cfun->machine->call_via[regno] = gen_label_rtx ();
13597 labelp = cfun->machine->call_via + regno;
13600 output_asm_insn ("bl\t%a0", labelp);
13601 return "";
13604 /* Routines for generating rtl. */
13605 void
13606 thumb_expand_movmemqi (rtx *operands)
13608 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13609 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13610 HOST_WIDE_INT len = INTVAL (operands[2]);
13611 HOST_WIDE_INT offset = 0;
13613 while (len >= 12)
13615 emit_insn (gen_movmem12b (out, in, out, in));
13616 len -= 12;
13619 if (len >= 8)
13621 emit_insn (gen_movmem8b (out, in, out, in));
13622 len -= 8;
13625 if (len >= 4)
13627 rtx reg = gen_reg_rtx (SImode);
13628 emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13629 emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13630 len -= 4;
13631 offset += 4;
13634 if (len >= 2)
13636 rtx reg = gen_reg_rtx (HImode);
13637 emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13638 plus_constant (in, offset))));
13639 emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13640 reg));
13641 len -= 2;
13642 offset += 2;
13645 if (len)
13647 rtx reg = gen_reg_rtx (QImode);
13648 emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13649 plus_constant (in, offset))));
13650 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13651 reg));
13655 void
13656 thumb_reload_out_hi (rtx *operands)
13658 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13661 /* Handle reading a half-word from memory during reload. */
13662 void
13663 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13665 abort ();
13668 /* Return the length of a function name prefix
13669 that starts with the character 'c'. */
13670 static int
13671 arm_get_strip_length (int c)
13673 switch (c)
13675 ARM_NAME_ENCODING_LENGTHS
13676 default: return 0;
13680 /* Return a pointer to a function's name with any
13681 and all prefix encodings stripped from it. */
13682 const char *
13683 arm_strip_name_encoding (const char *name)
13685 int skip;
13687 while ((skip = arm_get_strip_length (* name)))
13688 name += skip;
13690 return name;
13693 /* If there is a '*' anywhere in the name's prefix, then
13694 emit the stripped name verbatim, otherwise prepend an
13695 underscore if leading underscores are being used. */
13696 void
13697 arm_asm_output_labelref (FILE *stream, const char *name)
13699 int skip;
13700 int verbatim = 0;
13702 while ((skip = arm_get_strip_length (* name)))
13704 verbatim |= (*name == '*');
13705 name += skip;
13708 if (verbatim)
13709 fputs (name, stream);
13710 else
13711 asm_fprintf (stream, "%U%s", name);
13714 static void
13715 arm_file_end (void)
13717 int regno;
13719 if (! thumb_call_reg_needed)
13720 return;
13722 text_section ();
13723 asm_fprintf (asm_out_file, "\t.code 16\n");
13724 ASM_OUTPUT_ALIGN (asm_out_file, 1);
13726 for (regno = 0; regno < LR_REGNUM; regno++)
13728 rtx label = thumb_call_via_label[regno];
13730 if (label != 0)
13732 targetm.asm_out.internal_label (asm_out_file, "L",
13733 CODE_LABEL_NUMBER (label));
13734 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
13739 rtx aof_pic_label;
13741 #ifdef AOF_ASSEMBLER
13742 /* Special functions only needed when producing AOF syntax assembler. */
13744 struct pic_chain
13746 struct pic_chain * next;
13747 const char * symname;
13750 static struct pic_chain * aof_pic_chain = NULL;
13753 aof_pic_entry (rtx x)
13755 struct pic_chain ** chainp;
13756 int offset;
13758 if (aof_pic_label == NULL_RTX)
13760 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13763 for (offset = 0, chainp = &aof_pic_chain; *chainp;
13764 offset += 4, chainp = &(*chainp)->next)
13765 if ((*chainp)->symname == XSTR (x, 0))
13766 return plus_constant (aof_pic_label, offset);
13768 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13769 (*chainp)->next = NULL;
13770 (*chainp)->symname = XSTR (x, 0);
13771 return plus_constant (aof_pic_label, offset);
13774 void
13775 aof_dump_pic_table (FILE *f)
13777 struct pic_chain * chain;
13779 if (aof_pic_chain == NULL)
13780 return;
13782 asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13783 PIC_OFFSET_TABLE_REGNUM,
13784 PIC_OFFSET_TABLE_REGNUM);
13785 fputs ("|x$adcons|\n", f);
13787 for (chain = aof_pic_chain; chain; chain = chain->next)
13789 fputs ("\tDCD\t", f);
13790 assemble_name (f, chain->symname);
13791 fputs ("\n", f);
13795 int arm_text_section_count = 1;
13797 char *
13798 aof_text_section (void )
13800 static char buf[100];
13801 sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13802 arm_text_section_count++);
13803 if (flag_pic)
13804 strcat (buf, ", PIC, REENTRANT");
13805 return buf;
13808 static int arm_data_section_count = 1;
13810 char *
13811 aof_data_section (void)
13813 static char buf[100];
13814 sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13815 return buf;
13818 /* The AOF assembler is religiously strict about declarations of
13819 imported and exported symbols, so that it is impossible to declare
13820 a function as imported near the beginning of the file, and then to
13821 export it later on. It is, however, possible to delay the decision
13822 until all the functions in the file have been compiled. To get
13823 around this, we maintain a list of the imports and exports, and
13824 delete from it any that are subsequently defined. At the end of
13825 compilation we spit the remainder of the list out before the END
13826 directive. */
13828 struct import
13830 struct import * next;
13831 const char * name;
13834 static struct import * imports_list = NULL;
13836 void
13837 aof_add_import (const char *name)
13839 struct import * new;
13841 for (new = imports_list; new; new = new->next)
13842 if (new->name == name)
13843 return;
13845 new = (struct import *) xmalloc (sizeof (struct import));
13846 new->next = imports_list;
13847 imports_list = new;
13848 new->name = name;
13851 void
13852 aof_delete_import (const char *name)
13854 struct import ** old;
13856 for (old = &imports_list; *old; old = & (*old)->next)
13858 if ((*old)->name == name)
13860 *old = (*old)->next;
13861 return;
13866 int arm_main_function = 0;
13868 static void
13869 aof_dump_imports (FILE *f)
13871 /* The AOF assembler needs this to cause the startup code to be extracted
13872 from the library. Brining in __main causes the whole thing to work
13873 automagically. */
13874 if (arm_main_function)
13876 text_section ();
13877 fputs ("\tIMPORT __main\n", f);
13878 fputs ("\tDCD __main\n", f);
13881 /* Now dump the remaining imports. */
13882 while (imports_list)
13884 fprintf (f, "\tIMPORT\t");
13885 assemble_name (f, imports_list->name);
13886 fputc ('\n', f);
13887 imports_list = imports_list->next;
13891 static void
13892 aof_globalize_label (FILE *stream, const char *name)
13894 default_globalize_label (stream, name);
13895 if (! strcmp (name, "main"))
13896 arm_main_function = 1;
13899 static void
13900 aof_file_start (void)
13902 fputs ("__r0\tRN\t0\n", asm_out_file);
13903 fputs ("__a1\tRN\t0\n", asm_out_file);
13904 fputs ("__a2\tRN\t1\n", asm_out_file);
13905 fputs ("__a3\tRN\t2\n", asm_out_file);
13906 fputs ("__a4\tRN\t3\n", asm_out_file);
13907 fputs ("__v1\tRN\t4\n", asm_out_file);
13908 fputs ("__v2\tRN\t5\n", asm_out_file);
13909 fputs ("__v3\tRN\t6\n", asm_out_file);
13910 fputs ("__v4\tRN\t7\n", asm_out_file);
13911 fputs ("__v5\tRN\t8\n", asm_out_file);
13912 fputs ("__v6\tRN\t9\n", asm_out_file);
13913 fputs ("__sl\tRN\t10\n", asm_out_file);
13914 fputs ("__fp\tRN\t11\n", asm_out_file);
13915 fputs ("__ip\tRN\t12\n", asm_out_file);
13916 fputs ("__sp\tRN\t13\n", asm_out_file);
13917 fputs ("__lr\tRN\t14\n", asm_out_file);
13918 fputs ("__pc\tRN\t15\n", asm_out_file);
13919 fputs ("__f0\tFN\t0\n", asm_out_file);
13920 fputs ("__f1\tFN\t1\n", asm_out_file);
13921 fputs ("__f2\tFN\t2\n", asm_out_file);
13922 fputs ("__f3\tFN\t3\n", asm_out_file);
13923 fputs ("__f4\tFN\t4\n", asm_out_file);
13924 fputs ("__f5\tFN\t5\n", asm_out_file);
13925 fputs ("__f6\tFN\t6\n", asm_out_file);
13926 fputs ("__f7\tFN\t7\n", asm_out_file);
13927 text_section ();
13930 static void
13931 aof_file_end (void)
13933 if (flag_pic)
13934 aof_dump_pic_table (asm_out_file);
13935 arm_file_end ();
13936 aof_dump_imports (asm_out_file);
13937 fputs ("\tEND\n", asm_out_file);
13939 #endif /* AOF_ASSEMBLER */
13941 #ifndef ARM_PE
13942 /* Symbols in the text segment can be accessed without indirecting via the
13943 constant pool; it may take an extra binary operation, but this is still
13944 faster than indirecting via memory. Don't do this when not optimizing,
13945 since we won't be calculating al of the offsets necessary to do this
13946 simplification. */
13948 static void
13949 arm_encode_section_info (tree decl, rtx rtl, int first)
13951 /* This doesn't work with AOF syntax, since the string table may be in
13952 a different AREA. */
13953 #ifndef AOF_ASSEMBLER
13954 if (optimize > 0 && TREE_CONSTANT (decl))
13955 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
13956 #endif
13958 /* If we are referencing a function that is weak then encode a long call
13959 flag in the function name, otherwise if the function is static or
13960 or known to be defined in this file then encode a short call flag. */
13961 if (first && DECL_P (decl))
13963 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
13964 arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
13965 else if (! TREE_PUBLIC (decl))
13966 arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
13969 #endif /* !ARM_PE */
13971 static void
13972 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
13974 if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
13975 && !strcmp (prefix, "L"))
13977 arm_ccfsm_state = 0;
13978 arm_target_insn = NULL;
13980 default_internal_label (stream, prefix, labelno);
13983 /* Output code to add DELTA to the first argument, and then jump
13984 to FUNCTION. Used for C++ multiple inheritance. */
13985 static void
13986 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
13987 HOST_WIDE_INT delta,
13988 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
13989 tree function)
13991 static int thunk_label = 0;
13992 char label[256];
13993 int mi_delta = delta;
13994 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
13995 int shift = 0;
13996 int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
13997 ? 1 : 0);
13998 if (mi_delta < 0)
13999 mi_delta = - mi_delta;
14000 if (TARGET_THUMB)
14002 int labelno = thunk_label++;
14003 ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14004 fputs ("\tldr\tr12, ", file);
14005 assemble_name (file, label);
14006 fputc ('\n', file);
14008 while (mi_delta != 0)
14010 if ((mi_delta & (3 << shift)) == 0)
14011 shift += 2;
14012 else
14014 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14015 mi_op, this_regno, this_regno,
14016 mi_delta & (0xff << shift));
14017 mi_delta &= ~(0xff << shift);
14018 shift += 8;
14021 if (TARGET_THUMB)
14023 fprintf (file, "\tbx\tr12\n");
14024 ASM_OUTPUT_ALIGN (file, 2);
14025 assemble_name (file, label);
14026 fputs (":\n", file);
14027 assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14029 else
14031 fputs ("\tb\t", file);
14032 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14033 if (NEED_PLT_RELOC)
14034 fputs ("(PLT)", file);
14035 fputc ('\n', file);
14040 arm_emit_vector_const (FILE *file, rtx x)
14042 int i;
14043 const char * pattern;
14045 if (GET_CODE (x) != CONST_VECTOR)
14046 abort ();
14048 switch (GET_MODE (x))
14050 case V2SImode: pattern = "%08x"; break;
14051 case V4HImode: pattern = "%04x"; break;
14052 case V8QImode: pattern = "%02x"; break;
14053 default: abort ();
14056 fprintf (file, "0x");
14057 for (i = CONST_VECTOR_NUNITS (x); i--;)
14059 rtx element;
14061 element = CONST_VECTOR_ELT (x, i);
14062 fprintf (file, pattern, INTVAL (element));
14065 return 1;
14068 const char *
14069 arm_output_load_gr (rtx *operands)
14071 rtx reg;
14072 rtx offset;
14073 rtx wcgr;
14074 rtx sum;
14076 if (GET_CODE (operands [1]) != MEM
14077 || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14078 || GET_CODE (reg = XEXP (sum, 0)) != REG
14079 || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14080 || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14081 return "wldrw%?\t%0, %1";
14083 /* Fix up an out-of-range load of a GR register. */
14084 output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14085 wcgr = operands[0];
14086 operands[0] = reg;
14087 output_asm_insn ("ldr%?\t%0, %1", operands);
14089 operands[0] = wcgr;
14090 operands[1] = reg;
14091 output_asm_insn ("tmcr%?\t%0, %1", operands);
14092 output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14094 return "";
14097 static rtx
14098 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14099 int incoming ATTRIBUTE_UNUSED)
14101 #if 0
14102 /* FIXME: The ARM backend has special code to handle structure
14103 returns, and will reserve its own hidden first argument. So
14104 if this macro is enabled a *second* hidden argument will be
14105 reserved, which will break binary compatibility with old
14106 toolchains and also thunk handling. One day this should be
14107 fixed. */
14108 return 0;
14109 #else
14110 /* Register in which address to store a structure value
14111 is passed to a function. */
14112 return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14113 #endif
14116 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14118 On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14119 named arg and all anonymous args onto the stack.
14120 XXX I know the prologue shouldn't be pushing registers, but it is faster
14121 that way. */
14123 static void
14124 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14125 enum machine_mode mode ATTRIBUTE_UNUSED,
14126 tree type ATTRIBUTE_UNUSED,
14127 int *pretend_size,
14128 int second_time ATTRIBUTE_UNUSED)
14130 cfun->machine->uses_anonymous_args = 1;
14131 if (cum->nregs < NUM_ARG_REGS)
14132 *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14135 /* Return nonzero if the CONSUMER instruction (a store) does not need
14136 PRODUCER's value to calculate the address. */
14139 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14141 rtx value = PATTERN (producer);
14142 rtx addr = PATTERN (consumer);
14144 if (GET_CODE (value) == COND_EXEC)
14145 value = COND_EXEC_CODE (value);
14146 if (GET_CODE (value) == PARALLEL)
14147 value = XVECEXP (value, 0, 0);
14148 value = XEXP (value, 0);
14149 if (GET_CODE (addr) == COND_EXEC)
14150 addr = COND_EXEC_CODE (addr);
14151 if (GET_CODE (addr) == PARALLEL)
14152 addr = XVECEXP (addr, 0, 0);
14153 addr = XEXP (addr, 0);
14155 return !reg_overlap_mentioned_p (value, addr);
14158 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14159 have an early register shift value or amount dependency on the
14160 result of PRODUCER. */
14163 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14165 rtx value = PATTERN (producer);
14166 rtx op = PATTERN (consumer);
14167 rtx early_op;
14169 if (GET_CODE (value) == COND_EXEC)
14170 value = COND_EXEC_CODE (value);
14171 if (GET_CODE (value) == PARALLEL)
14172 value = XVECEXP (value, 0, 0);
14173 value = XEXP (value, 0);
14174 if (GET_CODE (op) == COND_EXEC)
14175 op = COND_EXEC_CODE (op);
14176 if (GET_CODE (op) == PARALLEL)
14177 op = XVECEXP (op, 0, 0);
14178 op = XEXP (op, 1);
14180 early_op = XEXP (op, 0);
14181 /* This is either an actual independent shift, or a shift applied to
14182 the first operand of another operation. We want the whole shift
14183 operation. */
14184 if (GET_CODE (early_op) == REG)
14185 early_op = op;
14187 return !reg_overlap_mentioned_p (value, early_op);
14190 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14191 have an early register shift value dependency on the result of
14192 PRODUCER. */
14195 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14197 rtx value = PATTERN (producer);
14198 rtx op = PATTERN (consumer);
14199 rtx early_op;
14201 if (GET_CODE (value) == COND_EXEC)
14202 value = COND_EXEC_CODE (value);
14203 if (GET_CODE (value) == PARALLEL)
14204 value = XVECEXP (value, 0, 0);
14205 value = XEXP (value, 0);
14206 if (GET_CODE (op) == COND_EXEC)
14207 op = COND_EXEC_CODE (op);
14208 if (GET_CODE (op) == PARALLEL)
14209 op = XVECEXP (op, 0, 0);
14210 op = XEXP (op, 1);
14212 early_op = XEXP (op, 0);
14214 /* This is either an actual independent shift, or a shift applied to
14215 the first operand of another operation. We want the value being
14216 shifted, in either case. */
14217 if (GET_CODE (early_op) != REG)
14218 early_op = XEXP (early_op, 0);
14220 return !reg_overlap_mentioned_p (value, early_op);
14223 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14224 have an early register mult dependency on the result of
14225 PRODUCER. */
14228 arm_no_early_mul_dep (rtx producer, rtx consumer)
14230 rtx value = PATTERN (producer);
14231 rtx op = PATTERN (consumer);
14233 if (GET_CODE (value) == COND_EXEC)
14234 value = COND_EXEC_CODE (value);
14235 if (GET_CODE (value) == PARALLEL)
14236 value = XVECEXP (value, 0, 0);
14237 value = XEXP (value, 0);
14238 if (GET_CODE (op) == COND_EXEC)
14239 op = COND_EXEC_CODE (op);
14240 if (GET_CODE (op) == PARALLEL)
14241 op = XVECEXP (op, 0, 0);
14242 op = XEXP (op, 1);
14244 return (GET_CODE (op) == PLUS
14245 && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14249 /* We can't rely on the caller doing the proper promotion when
14250 using APCS or ATPCS. */
14252 static bool
14253 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14255 return !TARGET_AAPCS_BASED;
14259 /* AAPCS based ABIs use short enums by default. */
14261 static bool
14262 arm_default_short_enums (void)
14264 return TARGET_AAPCS_BASED;
14268 /* AAPCS requires that anonymous bitfields affect structure alignment. */
14270 static bool
14271 arm_align_anon_bitfield (void)
14273 return TARGET_AAPCS_BASED;
14277 /* The generic C++ ABI says 64-bit (long long). The EABI says 32-bit. */
14279 static tree
14280 arm_cxx_guard_type (void)
14282 return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14286 /* The EABI says test the least significan bit of a guard variable. */
14288 static bool
14289 arm_cxx_guard_mask_bit (void)
14291 return TARGET_AAPCS_BASED;
14295 /* The EABI specifies that all array cookies are 8 bytes long. */
14297 static tree
14298 arm_get_cookie_size (tree type)
14300 tree size;
14302 if (!TARGET_AAPCS_BASED)
14303 return default_cxx_get_cookie_size (type);
14305 size = build_int_cst (sizetype, 8);
14306 return size;
14310 /* The EABI says that array cookies should also contain the element size. */
14312 static bool
14313 arm_cookie_has_size (void)
14315 return TARGET_AAPCS_BASED;
14319 /* The EABI says constructors and destructors should return a pointer to
14320 the object constructed/destroyed. */
14322 static bool
14323 arm_cxx_cdtor_returns_this (void)
14325 return TARGET_AAPCS_BASED;
14328 /* The EABI says that an inline function may never be the key
14329 method. */
14331 static bool
14332 arm_cxx_key_method_may_be_inline (void)
14334 return !TARGET_AAPCS_BASED;
14337 static void
14338 arm_cxx_determine_class_data_visibility (tree decl)
14340 if (!TARGET_AAPCS_BASED)
14341 return;
14343 /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
14344 is exported. However, on systems without dynamic vague linkage,
14345 \S 3.2.5.6 says that COMDAT class data has hidden linkage. */
14346 if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
14347 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
14348 else
14349 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
14350 DECL_VISIBILITY_SPECIFIED (decl) = 1;
14353 static bool
14354 arm_cxx_class_data_always_comdat (void)
14356 /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
14357 vague linkage if the class has no key function. */
14358 return !TARGET_AAPCS_BASED;
14362 /* The EABI says __aeabi_atexit should be used to register static
14363 destructors. */
14365 static bool
14366 arm_cxx_use_aeabi_atexit (void)
14368 return TARGET_AAPCS_BASED;
14372 void
14373 arm_set_return_address (rtx source, rtx scratch)
14375 arm_stack_offsets *offsets;
14376 HOST_WIDE_INT delta;
14377 rtx addr;
14378 unsigned long saved_regs;
14380 saved_regs = arm_compute_save_reg_mask ();
14382 if ((saved_regs & (1 << LR_REGNUM)) == 0)
14383 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14384 else
14386 if (frame_pointer_needed)
14387 addr = plus_constant(hard_frame_pointer_rtx, -4);
14388 else
14390 /* LR will be the first saved register. */
14391 offsets = arm_get_frame_offsets ();
14392 delta = offsets->outgoing_args - (offsets->frame + 4);
14395 if (delta >= 4096)
14397 emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
14398 GEN_INT (delta & ~4095)));
14399 addr = scratch;
14400 delta &= 4095;
14402 else
14403 addr = stack_pointer_rtx;
14405 addr = plus_constant (addr, delta);
14407 emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14412 void
14413 thumb_set_return_address (rtx source, rtx scratch)
14415 arm_stack_offsets *offsets;
14416 HOST_WIDE_INT delta;
14417 int reg;
14418 rtx addr;
14419 unsigned long mask;
14421 emit_insn (gen_rtx_USE (VOIDmode, source));
14423 mask = thumb_compute_save_reg_mask ();
14424 if (mask & (1 << LR_REGNUM))
14426 offsets = arm_get_frame_offsets ();
14428 /* Find the saved regs. */
14429 if (frame_pointer_needed)
14431 delta = offsets->soft_frame - offsets->saved_args;
14432 reg = THUMB_HARD_FRAME_POINTER_REGNUM;
14434 else
14436 delta = offsets->outgoing_args - offsets->saved_args;
14437 reg = SP_REGNUM;
14439 /* Allow for the stack frame. */
14440 if (TARGET_BACKTRACE)
14441 delta -= 16;
14442 /* The link register is always the first saved register. */
14443 delta -= 4;
14445 /* Construct the address. */
14446 addr = gen_rtx_REG (SImode, reg);
14447 if ((reg != SP_REGNUM && delta >= 128)
14448 || delta >= 1024)
14450 emit_insn (gen_movsi (scratch, GEN_INT (delta)));
14451 emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
14452 addr = scratch;
14454 else
14455 addr = plus_constant (addr, delta);
14457 emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14459 else
14460 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14463 /* Implements target hook vector_mode_supported_p. */
14464 bool
14465 arm_vector_mode_supported_p (enum machine_mode mode)
14467 if ((mode == V2SImode)
14468 || (mode == V4HImode)
14469 || (mode == V8QImode))
14470 return true;
14472 return false;
14475 /* Implement TARGET_SHIFT_TRUNCATION_MASK. SImode shifts use normal
14476 ARM insns and therefore guarantee that the shift count is modulo 256.
14477 DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
14478 guarantee no particular behavior for out-of-range counts. */
14480 static unsigned HOST_WIDE_INT
14481 arm_shift_truncation_mask (enum machine_mode mode)
14483 return mode == SImode ? 255 : 0;
14487 /* Map internal gcc register numbers to DWARF2 register numbers. */
14489 unsigned int
14490 arm_dbx_register_number (unsigned int regno)
14492 if (regno < 16)
14493 return regno;
14495 /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
14496 compatibility. The EABI defines them as registers 96-103. */
14497 if (IS_FPA_REGNUM (regno))
14498 return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
14500 if (IS_VFP_REGNUM (regno))
14501 return 64 + regno - FIRST_VFP_REGNUM;
14503 if (IS_IWMMXT_GR_REGNUM (regno))
14504 return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
14506 if (IS_IWMMXT_REGNUM (regno))
14507 return 112 + regno - FIRST_IWMMXT_REGNUM;
14509 abort ();