2006-05-02 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / config / arm / arm.c
blob5a5c7c0e07655bc7b78a6987c08a159c5d571424
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, 2006 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, 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, 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 static int arm_size_return_regs (void);
77 #ifndef AOF_ASSEMBLER
78 static bool arm_assemble_integer (rtx, unsigned int, int);
79 #endif
80 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
81 static arm_cc get_arm_condition_code (rtx);
82 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
83 static rtx is_jump_table (rtx);
84 static const char *output_multi_immediate (rtx *, const char *, const char *,
85 int, HOST_WIDE_INT);
86 static const char *shift_op (rtx, HOST_WIDE_INT *);
87 static struct machine_function *arm_init_machine_status (void);
88 static void thumb_exit (FILE *, int);
89 static rtx is_jump_table (rtx);
90 static HOST_WIDE_INT get_jump_table_size (rtx);
91 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
92 static Mnode *add_minipool_forward_ref (Mfix *);
93 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
94 static Mnode *add_minipool_backward_ref (Mfix *);
95 static void assign_minipool_offsets (Mfix *);
96 static void arm_print_value (FILE *, rtx);
97 static void dump_minipool (rtx);
98 static int arm_barrier_cost (rtx);
99 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
100 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
101 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
102 rtx);
103 static void arm_reorg (void);
104 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
105 static int current_file_function_operand (rtx);
106 static unsigned long arm_compute_save_reg0_reg12_mask (void);
107 static unsigned long arm_compute_save_reg_mask (void);
108 static unsigned long arm_isr_value (tree);
109 static unsigned long arm_compute_func_type (void);
110 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
111 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
112 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
113 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
114 #endif
115 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
116 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
117 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
118 static int arm_comp_type_attributes (tree, tree);
119 static void arm_set_default_type_attributes (tree);
120 static int arm_adjust_cost (rtx, rtx, rtx, int);
121 static int count_insns_for_constant (HOST_WIDE_INT, int);
122 static int arm_get_strip_length (int);
123 static bool arm_function_ok_for_sibcall (tree, tree);
124 static void arm_internal_label (FILE *, const char *, unsigned long);
125 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
126 tree);
127 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
128 static bool arm_size_rtx_costs (rtx, int, int, int *);
129 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
130 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
131 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
132 static bool arm_9e_rtx_costs (rtx, int, int, int *);
133 static int arm_address_cost (rtx);
134 static bool arm_memory_load_p (rtx);
135 static bool arm_cirrus_insn_p (rtx);
136 static void cirrus_reorg (rtx);
137 static void arm_init_builtins (void);
138 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
139 static void arm_init_iwmmxt_builtins (void);
140 static rtx safe_vector_operand (rtx, enum machine_mode);
141 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
142 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
143 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
144 static void emit_constant_insn (rtx cond, rtx pattern);
145 static rtx emit_set_insn (rtx, rtx);
146 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
147 tree, bool);
149 #ifdef OBJECT_FORMAT_ELF
150 static void arm_elf_asm_constructor (rtx, int);
151 #endif
152 #ifndef ARM_PE
153 static void arm_encode_section_info (tree, rtx, int);
154 #endif
156 static void arm_file_end (void);
158 #ifdef AOF_ASSEMBLER
159 static void aof_globalize_label (FILE *, const char *);
160 static void aof_dump_imports (FILE *);
161 static void aof_dump_pic_table (FILE *);
162 static void aof_file_start (void);
163 static void aof_file_end (void);
164 static void aof_asm_init_sections (void);
165 #endif
166 static rtx arm_struct_value_rtx (tree, int);
167 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
168 tree, int *, int);
169 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
170 enum machine_mode, tree, bool);
171 static bool arm_promote_prototypes (tree);
172 static bool arm_default_short_enums (void);
173 static bool arm_align_anon_bitfield (void);
174 static bool arm_return_in_msb (tree);
175 static bool arm_must_pass_in_stack (enum machine_mode, tree);
176 #ifdef TARGET_UNWIND_INFO
177 static void arm_unwind_emit (FILE *, rtx);
178 static bool arm_output_ttype (rtx);
179 #endif
181 static tree arm_cxx_guard_type (void);
182 static bool arm_cxx_guard_mask_bit (void);
183 static tree arm_get_cookie_size (tree);
184 static bool arm_cookie_has_size (void);
185 static bool arm_cxx_cdtor_returns_this (void);
186 static bool arm_cxx_key_method_may_be_inline (void);
187 static void arm_cxx_determine_class_data_visibility (tree);
188 static bool arm_cxx_class_data_always_comdat (void);
189 static bool arm_cxx_use_aeabi_atexit (void);
190 static void arm_init_libfuncs (void);
191 static bool arm_handle_option (size_t, const char *, int);
192 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
193 static bool arm_cannot_copy_insn_p (rtx);
194 static bool arm_tls_symbol_p (rtx x);
197 /* Initialize the GCC target structure. */
198 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
199 #undef TARGET_MERGE_DECL_ATTRIBUTES
200 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
201 #endif
203 #undef TARGET_ATTRIBUTE_TABLE
204 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
206 #undef TARGET_ASM_FILE_END
207 #define TARGET_ASM_FILE_END arm_file_end
209 #ifdef AOF_ASSEMBLER
210 #undef TARGET_ASM_BYTE_OP
211 #define TARGET_ASM_BYTE_OP "\tDCB\t"
212 #undef TARGET_ASM_ALIGNED_HI_OP
213 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
214 #undef TARGET_ASM_ALIGNED_SI_OP
215 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
216 #undef TARGET_ASM_GLOBALIZE_LABEL
217 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
218 #undef TARGET_ASM_FILE_START
219 #define TARGET_ASM_FILE_START aof_file_start
220 #undef TARGET_ASM_FILE_END
221 #define TARGET_ASM_FILE_END aof_file_end
222 #else
223 #undef TARGET_ASM_ALIGNED_SI_OP
224 #define TARGET_ASM_ALIGNED_SI_OP NULL
225 #undef TARGET_ASM_INTEGER
226 #define TARGET_ASM_INTEGER arm_assemble_integer
227 #endif
229 #undef TARGET_ASM_FUNCTION_PROLOGUE
230 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
232 #undef TARGET_ASM_FUNCTION_EPILOGUE
233 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
235 #undef TARGET_DEFAULT_TARGET_FLAGS
236 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
237 #undef TARGET_HANDLE_OPTION
238 #define TARGET_HANDLE_OPTION arm_handle_option
240 #undef TARGET_COMP_TYPE_ATTRIBUTES
241 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
243 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
244 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
246 #undef TARGET_SCHED_ADJUST_COST
247 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
249 #undef TARGET_ENCODE_SECTION_INFO
250 #ifdef ARM_PE
251 #define TARGET_ENCODE_SECTION_INFO arm_pe_encode_section_info
252 #else
253 #define TARGET_ENCODE_SECTION_INFO arm_encode_section_info
254 #endif
256 #undef TARGET_STRIP_NAME_ENCODING
257 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
259 #undef TARGET_ASM_INTERNAL_LABEL
260 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
262 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
263 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
265 #undef TARGET_ASM_OUTPUT_MI_THUNK
266 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
267 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
268 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
270 /* This will be overridden in arm_override_options. */
271 #undef TARGET_RTX_COSTS
272 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
273 #undef TARGET_ADDRESS_COST
274 #define TARGET_ADDRESS_COST arm_address_cost
276 #undef TARGET_SHIFT_TRUNCATION_MASK
277 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
278 #undef TARGET_VECTOR_MODE_SUPPORTED_P
279 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
281 #undef TARGET_MACHINE_DEPENDENT_REORG
282 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
284 #undef TARGET_INIT_BUILTINS
285 #define TARGET_INIT_BUILTINS arm_init_builtins
286 #undef TARGET_EXPAND_BUILTIN
287 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
289 #undef TARGET_INIT_LIBFUNCS
290 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
292 #undef TARGET_PROMOTE_FUNCTION_ARGS
293 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
294 #undef TARGET_PROMOTE_FUNCTION_RETURN
295 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
296 #undef TARGET_PROMOTE_PROTOTYPES
297 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
298 #undef TARGET_PASS_BY_REFERENCE
299 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
300 #undef TARGET_ARG_PARTIAL_BYTES
301 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
303 #undef TARGET_STRUCT_VALUE_RTX
304 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
306 #undef TARGET_SETUP_INCOMING_VARARGS
307 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
309 #undef TARGET_DEFAULT_SHORT_ENUMS
310 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
312 #undef TARGET_ALIGN_ANON_BITFIELD
313 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
315 #undef TARGET_NARROW_VOLATILE_BITFIELD
316 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
318 #undef TARGET_CXX_GUARD_TYPE
319 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
321 #undef TARGET_CXX_GUARD_MASK_BIT
322 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
324 #undef TARGET_CXX_GET_COOKIE_SIZE
325 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
327 #undef TARGET_CXX_COOKIE_HAS_SIZE
328 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
330 #undef TARGET_CXX_CDTOR_RETURNS_THIS
331 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
333 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
334 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
336 #undef TARGET_CXX_USE_AEABI_ATEXIT
337 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
339 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
340 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
341 arm_cxx_determine_class_data_visibility
343 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
344 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
346 #undef TARGET_RETURN_IN_MSB
347 #define TARGET_RETURN_IN_MSB arm_return_in_msb
349 #undef TARGET_MUST_PASS_IN_STACK
350 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
352 #ifdef TARGET_UNWIND_INFO
353 #undef TARGET_UNWIND_EMIT
354 #define TARGET_UNWIND_EMIT arm_unwind_emit
356 /* EABI unwinding tables use a different format for the typeinfo tables. */
357 #undef TARGET_ASM_TTYPE
358 #define TARGET_ASM_TTYPE arm_output_ttype
360 #undef TARGET_ARM_EABI_UNWINDER
361 #define TARGET_ARM_EABI_UNWINDER true
362 #endif /* TARGET_UNWIND_INFO */
364 #undef TARGET_CANNOT_COPY_INSN_P
365 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
367 #ifdef HAVE_AS_TLS
368 #undef TARGET_HAVE_TLS
369 #define TARGET_HAVE_TLS true
370 #endif
372 #undef TARGET_CANNOT_FORCE_CONST_MEM
373 #define TARGET_CANNOT_FORCE_CONST_MEM arm_tls_referenced_p
375 struct gcc_target targetm = TARGET_INITIALIZER;
377 /* Obstack for minipool constant handling. */
378 static struct obstack minipool_obstack;
379 static char * minipool_startobj;
381 /* The maximum number of insns skipped which
382 will be conditionalised if possible. */
383 static int max_insns_skipped = 5;
385 extern FILE * asm_out_file;
387 /* True if we are currently building a constant table. */
388 int making_const_table;
390 /* Define the information needed to generate branch insns. This is
391 stored from the compare operation. */
392 rtx arm_compare_op0, arm_compare_op1;
394 /* The processor for which instructions should be scheduled. */
395 enum processor_type arm_tune = arm_none;
397 /* Which floating point model to use. */
398 enum arm_fp_model arm_fp_model;
400 /* Which floating point hardware is available. */
401 enum fputype arm_fpu_arch;
403 /* Which floating point hardware to schedule for. */
404 enum fputype arm_fpu_tune;
406 /* Whether to use floating point hardware. */
407 enum float_abi_type arm_float_abi;
409 /* Which ABI to use. */
410 enum arm_abi_type arm_abi;
412 /* Which thread pointer model to use. */
413 enum arm_tp_type target_thread_pointer = TP_AUTO;
415 /* Used to parse -mstructure_size_boundary command line option. */
416 int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
418 /* Used for Thumb call_via trampolines. */
419 rtx thumb_call_via_label[14];
420 static int thumb_call_reg_needed;
422 /* Bit values used to identify processor capabilities. */
423 #define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
424 #define FL_ARCH3M (1 << 1) /* Extended multiply */
425 #define FL_MODE26 (1 << 2) /* 26-bit mode support */
426 #define FL_MODE32 (1 << 3) /* 32-bit mode support */
427 #define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
428 #define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
429 #define FL_THUMB (1 << 6) /* Thumb aware */
430 #define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
431 #define FL_STRONG (1 << 8) /* StrongARM */
432 #define FL_ARCH5E (1 << 9) /* DSP extensions to v5 */
433 #define FL_XSCALE (1 << 10) /* XScale */
434 #define FL_CIRRUS (1 << 11) /* Cirrus/DSP. */
435 #define FL_ARCH6 (1 << 12) /* Architecture rel 6. Adds
436 media instructions. */
437 #define FL_VFPV2 (1 << 13) /* Vector Floating Point V2. */
438 #define FL_WBUF (1 << 14) /* Schedule for write buffer ops.
439 Note: ARM6 & 7 derivatives only. */
440 #define FL_ARCH6K (1 << 15) /* Architecture rel 6 K extensions. */
442 #define FL_IWMMXT (1 << 29) /* XScale v2 or "Intel Wireless MMX technology". */
444 #define FL_FOR_ARCH2 0
445 #define FL_FOR_ARCH3 FL_MODE32
446 #define FL_FOR_ARCH3M (FL_FOR_ARCH3 | FL_ARCH3M)
447 #define FL_FOR_ARCH4 (FL_FOR_ARCH3M | FL_ARCH4)
448 #define FL_FOR_ARCH4T (FL_FOR_ARCH4 | FL_THUMB)
449 #define FL_FOR_ARCH5 (FL_FOR_ARCH4 | FL_ARCH5)
450 #define FL_FOR_ARCH5T (FL_FOR_ARCH5 | FL_THUMB)
451 #define FL_FOR_ARCH5E (FL_FOR_ARCH5 | FL_ARCH5E)
452 #define FL_FOR_ARCH5TE (FL_FOR_ARCH5E | FL_THUMB)
453 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
454 #define FL_FOR_ARCH6 (FL_FOR_ARCH5TE | FL_ARCH6)
455 #define FL_FOR_ARCH6J FL_FOR_ARCH6
456 #define FL_FOR_ARCH6K (FL_FOR_ARCH6 | FL_ARCH6K)
457 #define FL_FOR_ARCH6Z FL_FOR_ARCH6
458 #define FL_FOR_ARCH6ZK FL_FOR_ARCH6K
460 /* The bits in this mask specify which
461 instructions we are allowed to generate. */
462 static unsigned long insn_flags = 0;
464 /* The bits in this mask specify which instruction scheduling options should
465 be used. */
466 static unsigned long tune_flags = 0;
468 /* The following are used in the arm.md file as equivalents to bits
469 in the above two flag variables. */
471 /* Nonzero if this chip supports the ARM Architecture 3M extensions. */
472 int arm_arch3m = 0;
474 /* Nonzero if this chip supports the ARM Architecture 4 extensions. */
475 int arm_arch4 = 0;
477 /* Nonzero if this chip supports the ARM Architecture 4t extensions. */
478 int arm_arch4t = 0;
480 /* Nonzero if this chip supports the ARM Architecture 5 extensions. */
481 int arm_arch5 = 0;
483 /* Nonzero if this chip supports the ARM Architecture 5E extensions. */
484 int arm_arch5e = 0;
486 /* Nonzero if this chip supports the ARM Architecture 6 extensions. */
487 int arm_arch6 = 0;
489 /* Nonzero if this chip supports the ARM 6K extensions. */
490 int arm_arch6k = 0;
492 /* Nonzero if this chip can benefit from load scheduling. */
493 int arm_ld_sched = 0;
495 /* Nonzero if this chip is a StrongARM. */
496 int arm_tune_strongarm = 0;
498 /* Nonzero if this chip is a Cirrus variant. */
499 int arm_arch_cirrus = 0;
501 /* Nonzero if this chip supports Intel Wireless MMX technology. */
502 int arm_arch_iwmmxt = 0;
504 /* Nonzero if this chip is an XScale. */
505 int arm_arch_xscale = 0;
507 /* Nonzero if tuning for XScale */
508 int arm_tune_xscale = 0;
510 /* Nonzero if we want to tune for stores that access the write-buffer.
511 This typically means an ARM6 or ARM7 with MMU or MPU. */
512 int arm_tune_wbuf = 0;
514 /* Nonzero if generating Thumb instructions. */
515 int thumb_code = 0;
517 /* Nonzero if we should define __THUMB_INTERWORK__ in the
518 preprocessor.
519 XXX This is a bit of a hack, it's intended to help work around
520 problems in GLD which doesn't understand that armv5t code is
521 interworking clean. */
522 int arm_cpp_interwork = 0;
524 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
525 must report the mode of the memory reference from PRINT_OPERAND to
526 PRINT_OPERAND_ADDRESS. */
527 enum machine_mode output_memory_reference_mode;
529 /* The register number to be used for the PIC offset register. */
530 unsigned arm_pic_register = INVALID_REGNUM;
532 /* Set to 1 when a return insn is output, this means that the epilogue
533 is not needed. */
534 int return_used_this_function;
536 /* Set to 1 after arm_reorg has started. Reset to start at the start of
537 the next function. */
538 static int after_arm_reorg = 0;
540 /* The maximum number of insns to be used when loading a constant. */
541 static int arm_constant_limit = 3;
543 /* For an explanation of these variables, see final_prescan_insn below. */
544 int arm_ccfsm_state;
545 enum arm_cond_code arm_current_cc;
546 rtx arm_target_insn;
547 int arm_target_label;
549 /* The condition codes of the ARM, and the inverse function. */
550 static const char * const arm_condition_codes[] =
552 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
553 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
556 #define streq(string1, string2) (strcmp (string1, string2) == 0)
558 /* Initialization code. */
560 struct processors
562 const char *const name;
563 enum processor_type core;
564 const char *arch;
565 const unsigned long flags;
566 bool (* rtx_costs) (rtx, int, int, int *);
569 /* Not all of these give usefully different compilation alternatives,
570 but there is no simple way of generalizing them. */
571 static const struct processors all_cores[] =
573 /* ARM Cores */
574 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
575 {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
576 #include "arm-cores.def"
577 #undef ARM_CORE
578 {NULL, arm_none, NULL, 0, NULL}
581 static const struct processors all_architectures[] =
583 /* ARM Architectures */
584 /* We don't specify rtx_costs here as it will be figured out
585 from the core. */
587 {"armv2", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
588 {"armv2a", arm2, "2", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
589 {"armv3", arm6, "3", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
590 {"armv3m", arm7m, "3M", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
591 {"armv4", arm7tdmi, "4", FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
592 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
593 implementations that support it, so we will leave it out for now. */
594 {"armv4t", arm7tdmi, "4T", FL_CO_PROC | FL_FOR_ARCH4T, NULL},
595 {"armv5", arm10tdmi, "5", FL_CO_PROC | FL_FOR_ARCH5, NULL},
596 {"armv5t", arm10tdmi, "5T", FL_CO_PROC | FL_FOR_ARCH5T, NULL},
597 {"armv5e", arm1026ejs, "5E", FL_CO_PROC | FL_FOR_ARCH5E, NULL},
598 {"armv5te", arm1026ejs, "5TE", FL_CO_PROC | FL_FOR_ARCH5TE, NULL},
599 {"armv6", arm1136js, "6", FL_CO_PROC | FL_FOR_ARCH6, NULL},
600 {"armv6j", arm1136js, "6J", FL_CO_PROC | FL_FOR_ARCH6J, NULL},
601 {"armv6k", mpcore, "6K", FL_CO_PROC | FL_FOR_ARCH6K, NULL},
602 {"armv6z", arm1176jzs, "6Z", FL_CO_PROC | FL_FOR_ARCH6Z, NULL},
603 {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC | FL_FOR_ARCH6ZK, NULL},
604 {"ep9312", ep9312, "4T", FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
605 {"iwmmxt", iwmmxt, "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
606 {NULL, arm_none, NULL, 0 , NULL}
609 struct arm_cpu_select
611 const char * string;
612 const char * name;
613 const struct processors * processors;
616 /* This is a magic structure. The 'string' field is magically filled in
617 with a pointer to the value specified by the user on the command line
618 assuming that the user has specified such a value. */
620 static struct arm_cpu_select arm_select[] =
622 /* string name processors */
623 { NULL, "-mcpu=", all_cores },
624 { NULL, "-march=", all_architectures },
625 { NULL, "-mtune=", all_cores }
628 /* Defines representing the indexes into the above table. */
629 #define ARM_OPT_SET_CPU 0
630 #define ARM_OPT_SET_ARCH 1
631 #define ARM_OPT_SET_TUNE 2
633 /* The name of the proprocessor macro to define for this architecture. */
635 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
637 struct fpu_desc
639 const char * name;
640 enum fputype fpu;
644 /* Available values for -mfpu=. */
646 static const struct fpu_desc all_fpus[] =
648 {"fpa", FPUTYPE_FPA},
649 {"fpe2", FPUTYPE_FPA_EMU2},
650 {"fpe3", FPUTYPE_FPA_EMU2},
651 {"maverick", FPUTYPE_MAVERICK},
652 {"vfp", FPUTYPE_VFP}
656 /* Floating point models used by the different hardware.
657 See fputype in arm.h. */
659 static const enum fputype fp_model_for_fpu[] =
661 /* No FP hardware. */
662 ARM_FP_MODEL_UNKNOWN, /* FPUTYPE_NONE */
663 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA */
664 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU2 */
665 ARM_FP_MODEL_FPA, /* FPUTYPE_FPA_EMU3 */
666 ARM_FP_MODEL_MAVERICK, /* FPUTYPE_MAVERICK */
667 ARM_FP_MODEL_VFP /* FPUTYPE_VFP */
671 struct float_abi
673 const char * name;
674 enum float_abi_type abi_type;
678 /* Available values for -mfloat-abi=. */
680 static const struct float_abi all_float_abis[] =
682 {"soft", ARM_FLOAT_ABI_SOFT},
683 {"softfp", ARM_FLOAT_ABI_SOFTFP},
684 {"hard", ARM_FLOAT_ABI_HARD}
688 struct abi_name
690 const char *name;
691 enum arm_abi_type abi_type;
695 /* Available values for -mabi=. */
697 static const struct abi_name arm_all_abis[] =
699 {"apcs-gnu", ARM_ABI_APCS},
700 {"atpcs", ARM_ABI_ATPCS},
701 {"aapcs", ARM_ABI_AAPCS},
702 {"iwmmxt", ARM_ABI_IWMMXT},
703 {"aapcs-linux", ARM_ABI_AAPCS_LINUX}
706 /* Supported TLS relocations. */
708 enum tls_reloc {
709 TLS_GD32,
710 TLS_LDM32,
711 TLS_LDO32,
712 TLS_IE32,
713 TLS_LE32
716 /* Emit an insn that's a simple single-set. Both the operands must be known
717 to be valid. */
718 inline static rtx
719 emit_set_insn (rtx x, rtx y)
721 return emit_insn (gen_rtx_SET (VOIDmode, x, y));
724 /* Return the number of bits set in VALUE. */
725 static unsigned
726 bit_count (unsigned long value)
728 unsigned long count = 0;
730 while (value)
732 count++;
733 value &= value - 1; /* Clear the least-significant set bit. */
736 return count;
739 /* Set up library functions unique to ARM. */
741 static void
742 arm_init_libfuncs (void)
744 /* There are no special library functions unless we are using the
745 ARM BPABI. */
746 if (!TARGET_BPABI)
747 return;
749 /* The functions below are described in Section 4 of the "Run-Time
750 ABI for the ARM architecture", Version 1.0. */
752 /* Double-precision floating-point arithmetic. Table 2. */
753 set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
754 set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
755 set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
756 set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
757 set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
759 /* Double-precision comparisons. Table 3. */
760 set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
761 set_optab_libfunc (ne_optab, DFmode, NULL);
762 set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
763 set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
764 set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
765 set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
766 set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
768 /* Single-precision floating-point arithmetic. Table 4. */
769 set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
770 set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
771 set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
772 set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
773 set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
775 /* Single-precision comparisons. Table 5. */
776 set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
777 set_optab_libfunc (ne_optab, SFmode, NULL);
778 set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
779 set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
780 set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
781 set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
782 set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
784 /* Floating-point to integer conversions. Table 6. */
785 set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
786 set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
787 set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
788 set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
789 set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
790 set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
791 set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
792 set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
794 /* Conversions between floating types. Table 7. */
795 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
796 set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
798 /* Integer to floating-point conversions. Table 8. */
799 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
800 set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
801 set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
802 set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
803 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
804 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
805 set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
806 set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
808 /* Long long. Table 9. */
809 set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
810 set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
811 set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
812 set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
813 set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
814 set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
815 set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
816 set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
818 /* Integer (32/32->32) division. \S 4.3.1. */
819 set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
820 set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
822 /* The divmod functions are designed so that they can be used for
823 plain division, even though they return both the quotient and the
824 remainder. The quotient is returned in the usual location (i.e.,
825 r0 for SImode, {r0, r1} for DImode), just as would be expected
826 for an ordinary division routine. Because the AAPCS calling
827 conventions specify that all of { r0, r1, r2, r3 } are
828 callee-saved registers, there is no need to tell the compiler
829 explicitly that those registers are clobbered by these
830 routines. */
831 set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
832 set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
834 /* For SImode division the ABI provides div-without-mod routines,
835 which are faster. */
836 set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
837 set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
839 /* We don't have mod libcalls. Fortunately gcc knows how to use the
840 divmod libcalls instead. */
841 set_optab_libfunc (smod_optab, DImode, NULL);
842 set_optab_libfunc (umod_optab, DImode, NULL);
843 set_optab_libfunc (smod_optab, SImode, NULL);
844 set_optab_libfunc (umod_optab, SImode, NULL);
847 /* Implement TARGET_HANDLE_OPTION. */
849 static bool
850 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
852 switch (code)
854 case OPT_march_:
855 arm_select[1].string = arg;
856 return true;
858 case OPT_mcpu_:
859 arm_select[0].string = arg;
860 return true;
862 case OPT_mhard_float:
863 target_float_abi_name = "hard";
864 return true;
866 case OPT_msoft_float:
867 target_float_abi_name = "soft";
868 return true;
870 case OPT_mtune_:
871 arm_select[2].string = arg;
872 return true;
874 default:
875 return true;
879 /* Fix up any incompatible options that the user has specified.
880 This has now turned into a maze. */
881 void
882 arm_override_options (void)
884 unsigned i;
885 enum processor_type target_arch_cpu = arm_none;
887 /* Set up the flags based on the cpu/architecture selected by the user. */
888 for (i = ARRAY_SIZE (arm_select); i--;)
890 struct arm_cpu_select * ptr = arm_select + i;
892 if (ptr->string != NULL && ptr->string[0] != '\0')
894 const struct processors * sel;
896 for (sel = ptr->processors; sel->name != NULL; sel++)
897 if (streq (ptr->string, sel->name))
899 /* Set the architecture define. */
900 if (i != ARM_OPT_SET_TUNE)
901 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
903 /* Determine the processor core for which we should
904 tune code-generation. */
905 if (/* -mcpu= is a sensible default. */
906 i == ARM_OPT_SET_CPU
907 /* -mtune= overrides -mcpu= and -march=. */
908 || i == ARM_OPT_SET_TUNE)
909 arm_tune = (enum processor_type) (sel - ptr->processors);
911 /* Remember the CPU associated with this architecture.
912 If no other option is used to set the CPU type,
913 we'll use this to guess the most suitable tuning
914 options. */
915 if (i == ARM_OPT_SET_ARCH)
916 target_arch_cpu = sel->core;
918 if (i != ARM_OPT_SET_TUNE)
920 /* If we have been given an architecture and a processor
921 make sure that they are compatible. We only generate
922 a warning though, and we prefer the CPU over the
923 architecture. */
924 if (insn_flags != 0 && (insn_flags ^ sel->flags))
925 warning (0, "switch -mcpu=%s conflicts with -march= switch",
926 ptr->string);
928 insn_flags = sel->flags;
931 break;
934 if (sel->name == NULL)
935 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
939 /* Guess the tuning options from the architecture if necessary. */
940 if (arm_tune == arm_none)
941 arm_tune = target_arch_cpu;
943 /* If the user did not specify a processor, choose one for them. */
944 if (insn_flags == 0)
946 const struct processors * sel;
947 unsigned int sought;
948 enum processor_type cpu;
950 cpu = TARGET_CPU_DEFAULT;
951 if (cpu == arm_none)
953 #ifdef SUBTARGET_CPU_DEFAULT
954 /* Use the subtarget default CPU if none was specified by
955 configure. */
956 cpu = SUBTARGET_CPU_DEFAULT;
957 #endif
958 /* Default to ARM6. */
959 if (cpu == arm_none)
960 cpu = arm6;
962 sel = &all_cores[cpu];
964 insn_flags = sel->flags;
966 /* Now check to see if the user has specified some command line
967 switch that require certain abilities from the cpu. */
968 sought = 0;
970 if (TARGET_INTERWORK || TARGET_THUMB)
972 sought |= (FL_THUMB | FL_MODE32);
974 /* There are no ARM processors that support both APCS-26 and
975 interworking. Therefore we force FL_MODE26 to be removed
976 from insn_flags here (if it was set), so that the search
977 below will always be able to find a compatible processor. */
978 insn_flags &= ~FL_MODE26;
981 if (sought != 0 && ((sought & insn_flags) != sought))
983 /* Try to locate a CPU type that supports all of the abilities
984 of the default CPU, plus the extra abilities requested by
985 the user. */
986 for (sel = all_cores; sel->name != NULL; sel++)
987 if ((sel->flags & sought) == (sought | insn_flags))
988 break;
990 if (sel->name == NULL)
992 unsigned current_bit_count = 0;
993 const struct processors * best_fit = NULL;
995 /* Ideally we would like to issue an error message here
996 saying that it was not possible to find a CPU compatible
997 with the default CPU, but which also supports the command
998 line options specified by the programmer, and so they
999 ought to use the -mcpu=<name> command line option to
1000 override the default CPU type.
1002 If we cannot find a cpu that has both the
1003 characteristics of the default cpu and the given
1004 command line options we scan the array again looking
1005 for a best match. */
1006 for (sel = all_cores; sel->name != NULL; sel++)
1007 if ((sel->flags & sought) == sought)
1009 unsigned count;
1011 count = bit_count (sel->flags & insn_flags);
1013 if (count >= current_bit_count)
1015 best_fit = sel;
1016 current_bit_count = count;
1020 gcc_assert (best_fit);
1021 sel = best_fit;
1024 insn_flags = sel->flags;
1026 sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1027 if (arm_tune == arm_none)
1028 arm_tune = (enum processor_type) (sel - all_cores);
1031 /* The processor for which we should tune should now have been
1032 chosen. */
1033 gcc_assert (arm_tune != arm_none);
1035 tune_flags = all_cores[(int)arm_tune].flags;
1036 if (optimize_size)
1037 targetm.rtx_costs = arm_size_rtx_costs;
1038 else
1039 targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
1041 /* Make sure that the processor choice does not conflict with any of the
1042 other command line choices. */
1043 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
1045 warning (0, "target CPU does not support interworking" );
1046 target_flags &= ~MASK_INTERWORK;
1049 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1051 warning (0, "target CPU does not support THUMB instructions");
1052 target_flags &= ~MASK_THUMB;
1055 if (TARGET_APCS_FRAME && TARGET_THUMB)
1057 /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1058 target_flags &= ~MASK_APCS_FRAME;
1061 /* Callee super interworking implies thumb interworking. Adding
1062 this to the flags here simplifies the logic elsewhere. */
1063 if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1064 target_flags |= MASK_INTERWORK;
1066 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1067 from here where no function is being compiled currently. */
1068 if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1069 warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1071 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1072 warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1074 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1075 warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1077 if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1079 warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1080 target_flags |= MASK_APCS_FRAME;
1083 if (TARGET_POKE_FUNCTION_NAME)
1084 target_flags |= MASK_APCS_FRAME;
1086 if (TARGET_APCS_REENT && flag_pic)
1087 error ("-fpic and -mapcs-reent are incompatible");
1089 if (TARGET_APCS_REENT)
1090 warning (0, "APCS reentrant code not supported. Ignored");
1092 /* If this target is normally configured to use APCS frames, warn if they
1093 are turned off and debugging is turned on. */
1094 if (TARGET_ARM
1095 && write_symbols != NO_DEBUG
1096 && !TARGET_APCS_FRAME
1097 && (TARGET_DEFAULT & MASK_APCS_FRAME))
1098 warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1100 /* If stack checking is disabled, we can use r10 as the PIC register,
1101 which keeps r9 available. */
1102 if (flag_pic && TARGET_SINGLE_PIC_BASE)
1103 arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
1105 if (TARGET_APCS_FLOAT)
1106 warning (0, "passing floating point arguments in fp regs not yet supported");
1108 /* Initialize boolean versions of the flags, for use in the arm.md file. */
1109 arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1110 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1111 arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1112 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1113 arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1114 arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1115 arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1116 arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1117 arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1119 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1120 arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1121 thumb_code = (TARGET_ARM == 0);
1122 arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1123 arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1124 arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1126 /* V5 code we generate is completely interworking capable, so we turn off
1127 TARGET_INTERWORK here to avoid many tests later on. */
1129 /* XXX However, we must pass the right pre-processor defines to CPP
1130 or GLD can get confused. This is a hack. */
1131 if (TARGET_INTERWORK)
1132 arm_cpp_interwork = 1;
1134 if (arm_arch5)
1135 target_flags &= ~MASK_INTERWORK;
1137 if (target_abi_name)
1139 for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1141 if (streq (arm_all_abis[i].name, target_abi_name))
1143 arm_abi = arm_all_abis[i].abi_type;
1144 break;
1147 if (i == ARRAY_SIZE (arm_all_abis))
1148 error ("invalid ABI option: -mabi=%s", target_abi_name);
1150 else
1151 arm_abi = ARM_DEFAULT_ABI;
1153 if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1154 error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1156 if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1157 error ("iwmmxt abi requires an iwmmxt capable cpu");
1159 arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1160 if (target_fpu_name == NULL && target_fpe_name != NULL)
1162 if (streq (target_fpe_name, "2"))
1163 target_fpu_name = "fpe2";
1164 else if (streq (target_fpe_name, "3"))
1165 target_fpu_name = "fpe3";
1166 else
1167 error ("invalid floating point emulation option: -mfpe=%s",
1168 target_fpe_name);
1170 if (target_fpu_name != NULL)
1172 /* The user specified a FPU. */
1173 for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1175 if (streq (all_fpus[i].name, target_fpu_name))
1177 arm_fpu_arch = all_fpus[i].fpu;
1178 arm_fpu_tune = arm_fpu_arch;
1179 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1180 break;
1183 if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1184 error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1186 else
1188 #ifdef FPUTYPE_DEFAULT
1189 /* Use the default if it is specified for this platform. */
1190 arm_fpu_arch = FPUTYPE_DEFAULT;
1191 arm_fpu_tune = FPUTYPE_DEFAULT;
1192 #else
1193 /* Pick one based on CPU type. */
1194 /* ??? Some targets assume FPA is the default.
1195 if ((insn_flags & FL_VFP) != 0)
1196 arm_fpu_arch = FPUTYPE_VFP;
1197 else
1199 if (arm_arch_cirrus)
1200 arm_fpu_arch = FPUTYPE_MAVERICK;
1201 else
1202 arm_fpu_arch = FPUTYPE_FPA_EMU2;
1203 #endif
1204 if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1205 arm_fpu_tune = FPUTYPE_FPA;
1206 else
1207 arm_fpu_tune = arm_fpu_arch;
1208 arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1209 gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1212 if (target_float_abi_name != NULL)
1214 /* The user specified a FP ABI. */
1215 for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1217 if (streq (all_float_abis[i].name, target_float_abi_name))
1219 arm_float_abi = all_float_abis[i].abi_type;
1220 break;
1223 if (i == ARRAY_SIZE (all_float_abis))
1224 error ("invalid floating point abi: -mfloat-abi=%s",
1225 target_float_abi_name);
1227 else
1228 arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1230 if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1231 sorry ("-mfloat-abi=hard and VFP");
1233 /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1234 VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1235 will ever exist. GCC makes no attempt to support this combination. */
1236 if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1237 sorry ("iWMMXt and hardware floating point");
1239 /* If soft-float is specified then don't use FPU. */
1240 if (TARGET_SOFT_FLOAT)
1241 arm_fpu_arch = FPUTYPE_NONE;
1243 /* For arm2/3 there is no need to do any scheduling if there is only
1244 a floating point emulator, or we are doing software floating-point. */
1245 if ((TARGET_SOFT_FLOAT
1246 || arm_fpu_tune == FPUTYPE_FPA_EMU2
1247 || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1248 && (tune_flags & FL_MODE32) == 0)
1249 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1251 if (target_thread_switch)
1253 if (strcmp (target_thread_switch, "soft") == 0)
1254 target_thread_pointer = TP_SOFT;
1255 else if (strcmp (target_thread_switch, "auto") == 0)
1256 target_thread_pointer = TP_AUTO;
1257 else if (strcmp (target_thread_switch, "cp15") == 0)
1258 target_thread_pointer = TP_CP15;
1259 else
1260 error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1263 /* Use the cp15 method if it is available. */
1264 if (target_thread_pointer == TP_AUTO)
1266 if (arm_arch6k && !TARGET_THUMB)
1267 target_thread_pointer = TP_CP15;
1268 else
1269 target_thread_pointer = TP_SOFT;
1272 if (TARGET_HARD_TP && TARGET_THUMB)
1273 error ("can not use -mtp=cp15 with -mthumb");
1275 /* Override the default structure alignment for AAPCS ABI. */
1276 if (TARGET_AAPCS_BASED)
1277 arm_structure_size_boundary = 8;
1279 if (structure_size_string != NULL)
1281 int size = strtol (structure_size_string, NULL, 0);
1283 if (size == 8 || size == 32
1284 || (ARM_DOUBLEWORD_ALIGN && size == 64))
1285 arm_structure_size_boundary = size;
1286 else
1287 warning (0, "structure size boundary can only be set to %s",
1288 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1291 if (arm_pic_register_string != NULL)
1293 int pic_register = decode_reg_name (arm_pic_register_string);
1295 if (!flag_pic)
1296 warning (0, "-mpic-register= is useless without -fpic");
1298 /* Prevent the user from choosing an obviously stupid PIC register. */
1299 else if (pic_register < 0 || call_used_regs[pic_register]
1300 || pic_register == HARD_FRAME_POINTER_REGNUM
1301 || pic_register == STACK_POINTER_REGNUM
1302 || pic_register >= PC_REGNUM)
1303 error ("unable to use '%s' for PIC register", arm_pic_register_string);
1304 else
1305 arm_pic_register = pic_register;
1308 if (TARGET_THUMB && flag_schedule_insns)
1310 /* Don't warn since it's on by default in -O2. */
1311 flag_schedule_insns = 0;
1314 if (optimize_size)
1316 arm_constant_limit = 1;
1318 /* If optimizing for size, bump the number of instructions that we
1319 are prepared to conditionally execute (even on a StrongARM). */
1320 max_insns_skipped = 6;
1322 else
1324 /* For processors with load scheduling, it never costs more than
1325 2 cycles to load a constant, and the load scheduler may well
1326 reduce that to 1. */
1327 if (arm_ld_sched)
1328 arm_constant_limit = 1;
1330 /* On XScale the longer latency of a load makes it more difficult
1331 to achieve a good schedule, so it's faster to synthesize
1332 constants that can be done in two insns. */
1333 if (arm_tune_xscale)
1334 arm_constant_limit = 2;
1336 /* StrongARM has early execution of branches, so a sequence
1337 that is worth skipping is shorter. */
1338 if (arm_tune_strongarm)
1339 max_insns_skipped = 3;
1342 /* Register global variables with the garbage collector. */
1343 arm_add_gc_roots ();
1346 static void
1347 arm_add_gc_roots (void)
1349 gcc_obstack_init(&minipool_obstack);
1350 minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1353 /* A table of known ARM exception types.
1354 For use with the interrupt function attribute. */
1356 typedef struct
1358 const char *const arg;
1359 const unsigned long return_value;
1361 isr_attribute_arg;
1363 static const isr_attribute_arg isr_attribute_args [] =
1365 { "IRQ", ARM_FT_ISR },
1366 { "irq", ARM_FT_ISR },
1367 { "FIQ", ARM_FT_FIQ },
1368 { "fiq", ARM_FT_FIQ },
1369 { "ABORT", ARM_FT_ISR },
1370 { "abort", ARM_FT_ISR },
1371 { "ABORT", ARM_FT_ISR },
1372 { "abort", ARM_FT_ISR },
1373 { "UNDEF", ARM_FT_EXCEPTION },
1374 { "undef", ARM_FT_EXCEPTION },
1375 { "SWI", ARM_FT_EXCEPTION },
1376 { "swi", ARM_FT_EXCEPTION },
1377 { NULL, ARM_FT_NORMAL }
1380 /* Returns the (interrupt) function type of the current
1381 function, or ARM_FT_UNKNOWN if the type cannot be determined. */
1383 static unsigned long
1384 arm_isr_value (tree argument)
1386 const isr_attribute_arg * ptr;
1387 const char * arg;
1389 /* No argument - default to IRQ. */
1390 if (argument == NULL_TREE)
1391 return ARM_FT_ISR;
1393 /* Get the value of the argument. */
1394 if (TREE_VALUE (argument) == NULL_TREE
1395 || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1396 return ARM_FT_UNKNOWN;
1398 arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1400 /* Check it against the list of known arguments. */
1401 for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1402 if (streq (arg, ptr->arg))
1403 return ptr->return_value;
1405 /* An unrecognized interrupt type. */
1406 return ARM_FT_UNKNOWN;
1409 /* Computes the type of the current function. */
1411 static unsigned long
1412 arm_compute_func_type (void)
1414 unsigned long type = ARM_FT_UNKNOWN;
1415 tree a;
1416 tree attr;
1418 gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1420 /* Decide if the current function is volatile. Such functions
1421 never return, and many memory cycles can be saved by not storing
1422 register values that will never be needed again. This optimization
1423 was added to speed up context switching in a kernel application. */
1424 if (optimize > 0
1425 && (TREE_NOTHROW (current_function_decl)
1426 || !(flag_unwind_tables
1427 || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
1428 && TREE_THIS_VOLATILE (current_function_decl))
1429 type |= ARM_FT_VOLATILE;
1431 if (cfun->static_chain_decl != NULL)
1432 type |= ARM_FT_NESTED;
1434 attr = DECL_ATTRIBUTES (current_function_decl);
1436 a = lookup_attribute ("naked", attr);
1437 if (a != NULL_TREE)
1438 type |= ARM_FT_NAKED;
1440 a = lookup_attribute ("isr", attr);
1441 if (a == NULL_TREE)
1442 a = lookup_attribute ("interrupt", attr);
1444 if (a == NULL_TREE)
1445 type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1446 else
1447 type |= arm_isr_value (TREE_VALUE (a));
1449 return type;
1452 /* Returns the type of the current function. */
1454 unsigned long
1455 arm_current_func_type (void)
1457 if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1458 cfun->machine->func_type = arm_compute_func_type ();
1460 return cfun->machine->func_type;
1463 /* Return 1 if it is possible to return using a single instruction.
1464 If SIBLING is non-null, this is a test for a return before a sibling
1465 call. SIBLING is the call insn, so we can examine its register usage. */
1468 use_return_insn (int iscond, rtx sibling)
1470 int regno;
1471 unsigned int func_type;
1472 unsigned long saved_int_regs;
1473 unsigned HOST_WIDE_INT stack_adjust;
1474 arm_stack_offsets *offsets;
1476 /* Never use a return instruction before reload has run. */
1477 if (!reload_completed)
1478 return 0;
1480 func_type = arm_current_func_type ();
1482 /* Naked functions and volatile functions need special
1483 consideration. */
1484 if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1485 return 0;
1487 /* So do interrupt functions that use the frame pointer. */
1488 if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1489 return 0;
1491 offsets = arm_get_frame_offsets ();
1492 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1494 /* As do variadic functions. */
1495 if (current_function_pretend_args_size
1496 || cfun->machine->uses_anonymous_args
1497 /* Or if the function calls __builtin_eh_return () */
1498 || current_function_calls_eh_return
1499 /* Or if the function calls alloca */
1500 || current_function_calls_alloca
1501 /* Or if there is a stack adjustment. However, if the stack pointer
1502 is saved on the stack, we can use a pre-incrementing stack load. */
1503 || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1504 return 0;
1506 saved_int_regs = arm_compute_save_reg_mask ();
1508 /* Unfortunately, the insn
1510 ldmib sp, {..., sp, ...}
1512 triggers a bug on most SA-110 based devices, such that the stack
1513 pointer won't be correctly restored if the instruction takes a
1514 page fault. We work around this problem by popping r3 along with
1515 the other registers, since that is never slower than executing
1516 another instruction.
1518 We test for !arm_arch5 here, because code for any architecture
1519 less than this could potentially be run on one of the buggy
1520 chips. */
1521 if (stack_adjust == 4 && !arm_arch5)
1523 /* Validate that r3 is a call-clobbered register (always true in
1524 the default abi) ... */
1525 if (!call_used_regs[3])
1526 return 0;
1528 /* ... that it isn't being used for a return value ... */
1529 if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1530 return 0;
1532 /* ... or for a tail-call argument ... */
1533 if (sibling)
1535 gcc_assert (GET_CODE (sibling) == CALL_INSN);
1537 if (find_regno_fusage (sibling, USE, 3))
1538 return 0;
1541 /* ... and that there are no call-saved registers in r0-r2
1542 (always true in the default ABI). */
1543 if (saved_int_regs & 0x7)
1544 return 0;
1547 /* Can't be done if interworking with Thumb, and any registers have been
1548 stacked. */
1549 if (TARGET_INTERWORK && saved_int_regs != 0)
1550 return 0;
1552 /* On StrongARM, conditional returns are expensive if they aren't
1553 taken and multiple registers have been stacked. */
1554 if (iscond && arm_tune_strongarm)
1556 /* Conditional return when just the LR is stored is a simple
1557 conditional-load instruction, that's not expensive. */
1558 if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1559 return 0;
1561 if (flag_pic
1562 && arm_pic_register != INVALID_REGNUM
1563 && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1564 return 0;
1567 /* If there are saved registers but the LR isn't saved, then we need
1568 two instructions for the return. */
1569 if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1570 return 0;
1572 /* Can't be done if any of the FPA regs are pushed,
1573 since this also requires an insn. */
1574 if (TARGET_HARD_FLOAT && TARGET_FPA)
1575 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1576 if (regs_ever_live[regno] && !call_used_regs[regno])
1577 return 0;
1579 /* Likewise VFP regs. */
1580 if (TARGET_HARD_FLOAT && TARGET_VFP)
1581 for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1582 if (regs_ever_live[regno] && !call_used_regs[regno])
1583 return 0;
1585 if (TARGET_REALLY_IWMMXT)
1586 for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1587 if (regs_ever_live[regno] && ! call_used_regs [regno])
1588 return 0;
1590 return 1;
1593 /* Return TRUE if int I is a valid immediate ARM constant. */
1596 const_ok_for_arm (HOST_WIDE_INT i)
1598 int lowbit;
1600 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1601 be all zero, or all one. */
1602 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1603 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1604 != ((~(unsigned HOST_WIDE_INT) 0)
1605 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1606 return FALSE;
1608 i &= (unsigned HOST_WIDE_INT) 0xffffffff;
1610 /* Fast return for 0 and small values. We must do this for zero, since
1611 the code below can't handle that one case. */
1612 if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
1613 return TRUE;
1615 /* Get the number of trailing zeros, rounded down to the nearest even
1616 number. */
1617 lowbit = (ffs ((int) i) - 1) & ~1;
1619 if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1620 return TRUE;
1621 else if (lowbit <= 4
1622 && ((i & ~0xc000003f) == 0
1623 || (i & ~0xf000000f) == 0
1624 || (i & ~0xfc000003) == 0))
1625 return TRUE;
1627 return FALSE;
1630 /* Return true if I is a valid constant for the operation CODE. */
1631 static int
1632 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1634 if (const_ok_for_arm (i))
1635 return 1;
1637 switch (code)
1639 case PLUS:
1640 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1642 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
1643 case XOR:
1644 case IOR:
1645 return 0;
1647 case AND:
1648 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1650 default:
1651 gcc_unreachable ();
1655 /* Emit a sequence of insns to handle a large constant.
1656 CODE is the code of the operation required, it can be any of SET, PLUS,
1657 IOR, AND, XOR, MINUS;
1658 MODE is the mode in which the operation is being performed;
1659 VAL is the integer to operate on;
1660 SOURCE is the other operand (a register, or a null-pointer for SET);
1661 SUBTARGETS means it is safe to create scratch registers if that will
1662 either produce a simpler sequence, or we will want to cse the values.
1663 Return value is the number of insns emitted. */
1666 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1667 HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1669 rtx cond;
1671 if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1672 cond = COND_EXEC_TEST (PATTERN (insn));
1673 else
1674 cond = NULL_RTX;
1676 if (subtargets || code == SET
1677 || (GET_CODE (target) == REG && GET_CODE (source) == REG
1678 && REGNO (target) != REGNO (source)))
1680 /* After arm_reorg has been called, we can't fix up expensive
1681 constants by pushing them into memory so we must synthesize
1682 them in-line, regardless of the cost. This is only likely to
1683 be more costly on chips that have load delay slots and we are
1684 compiling without running the scheduler (so no splitting
1685 occurred before the final instruction emission).
1687 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1689 if (!after_arm_reorg
1690 && !cond
1691 && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1692 1, 0)
1693 > arm_constant_limit + (code != SET)))
1695 if (code == SET)
1697 /* Currently SET is the only monadic value for CODE, all
1698 the rest are diadic. */
1699 emit_set_insn (target, GEN_INT (val));
1700 return 1;
1702 else
1704 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1706 emit_set_insn (temp, GEN_INT (val));
1707 /* For MINUS, the value is subtracted from, since we never
1708 have subtraction of a constant. */
1709 if (code == MINUS)
1710 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
1711 else
1712 emit_set_insn (target,
1713 gen_rtx_fmt_ee (code, mode, source, temp));
1714 return 2;
1719 return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1723 static int
1724 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1726 HOST_WIDE_INT temp1;
1727 int num_insns = 0;
1730 int end;
1732 if (i <= 0)
1733 i += 32;
1734 if (remainder & (3 << (i - 2)))
1736 end = i - 8;
1737 if (end < 0)
1738 end += 32;
1739 temp1 = remainder & ((0x0ff << end)
1740 | ((i < end) ? (0xff >> (32 - end)) : 0));
1741 remainder &= ~temp1;
1742 num_insns++;
1743 i -= 6;
1745 i -= 2;
1746 } while (remainder);
1747 return num_insns;
1750 /* Emit an instruction with the indicated PATTERN. If COND is
1751 non-NULL, conditionalize the execution of the instruction on COND
1752 being true. */
1754 static void
1755 emit_constant_insn (rtx cond, rtx pattern)
1757 if (cond)
1758 pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1759 emit_insn (pattern);
1762 /* As above, but extra parameter GENERATE which, if clear, suppresses
1763 RTL generation. */
1765 static int
1766 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1767 HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1768 int generate)
1770 int can_invert = 0;
1771 int can_negate = 0;
1772 int can_negate_initial = 0;
1773 int can_shift = 0;
1774 int i;
1775 int num_bits_set = 0;
1776 int set_sign_bit_copies = 0;
1777 int clear_sign_bit_copies = 0;
1778 int clear_zero_bit_copies = 0;
1779 int set_zero_bit_copies = 0;
1780 int insns = 0;
1781 unsigned HOST_WIDE_INT temp1, temp2;
1782 unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1784 /* Find out which operations are safe for a given CODE. Also do a quick
1785 check for degenerate cases; these can occur when DImode operations
1786 are split. */
1787 switch (code)
1789 case SET:
1790 can_invert = 1;
1791 can_shift = 1;
1792 can_negate = 1;
1793 break;
1795 case PLUS:
1796 can_negate = 1;
1797 can_negate_initial = 1;
1798 break;
1800 case IOR:
1801 if (remainder == 0xffffffff)
1803 if (generate)
1804 emit_constant_insn (cond,
1805 gen_rtx_SET (VOIDmode, target,
1806 GEN_INT (ARM_SIGN_EXTEND (val))));
1807 return 1;
1809 if (remainder == 0)
1811 if (reload_completed && rtx_equal_p (target, source))
1812 return 0;
1813 if (generate)
1814 emit_constant_insn (cond,
1815 gen_rtx_SET (VOIDmode, target, source));
1816 return 1;
1818 break;
1820 case AND:
1821 if (remainder == 0)
1823 if (generate)
1824 emit_constant_insn (cond,
1825 gen_rtx_SET (VOIDmode, target, const0_rtx));
1826 return 1;
1828 if (remainder == 0xffffffff)
1830 if (reload_completed && rtx_equal_p (target, source))
1831 return 0;
1832 if (generate)
1833 emit_constant_insn (cond,
1834 gen_rtx_SET (VOIDmode, target, source));
1835 return 1;
1837 can_invert = 1;
1838 break;
1840 case XOR:
1841 if (remainder == 0)
1843 if (reload_completed && rtx_equal_p (target, source))
1844 return 0;
1845 if (generate)
1846 emit_constant_insn (cond,
1847 gen_rtx_SET (VOIDmode, target, source));
1848 return 1;
1851 /* We don't know how to handle other cases yet. */
1852 gcc_assert (remainder == 0xffffffff);
1854 if (generate)
1855 emit_constant_insn (cond,
1856 gen_rtx_SET (VOIDmode, target,
1857 gen_rtx_NOT (mode, source)));
1858 return 1;
1860 case MINUS:
1861 /* We treat MINUS as (val - source), since (source - val) is always
1862 passed as (source + (-val)). */
1863 if (remainder == 0)
1865 if (generate)
1866 emit_constant_insn (cond,
1867 gen_rtx_SET (VOIDmode, target,
1868 gen_rtx_NEG (mode, source)));
1869 return 1;
1871 if (const_ok_for_arm (val))
1873 if (generate)
1874 emit_constant_insn (cond,
1875 gen_rtx_SET (VOIDmode, target,
1876 gen_rtx_MINUS (mode, GEN_INT (val),
1877 source)));
1878 return 1;
1880 can_negate = 1;
1882 break;
1884 default:
1885 gcc_unreachable ();
1888 /* If we can do it in one insn get out quickly. */
1889 if (const_ok_for_arm (val)
1890 || (can_negate_initial && const_ok_for_arm (-val))
1891 || (can_invert && const_ok_for_arm (~val)))
1893 if (generate)
1894 emit_constant_insn (cond,
1895 gen_rtx_SET (VOIDmode, target,
1896 (source
1897 ? gen_rtx_fmt_ee (code, mode, source,
1898 GEN_INT (val))
1899 : GEN_INT (val))));
1900 return 1;
1903 /* Calculate a few attributes that may be useful for specific
1904 optimizations. */
1905 for (i = 31; i >= 0; i--)
1907 if ((remainder & (1 << i)) == 0)
1908 clear_sign_bit_copies++;
1909 else
1910 break;
1913 for (i = 31; i >= 0; i--)
1915 if ((remainder & (1 << i)) != 0)
1916 set_sign_bit_copies++;
1917 else
1918 break;
1921 for (i = 0; i <= 31; i++)
1923 if ((remainder & (1 << i)) == 0)
1924 clear_zero_bit_copies++;
1925 else
1926 break;
1929 for (i = 0; i <= 31; i++)
1931 if ((remainder & (1 << i)) != 0)
1932 set_zero_bit_copies++;
1933 else
1934 break;
1937 switch (code)
1939 case SET:
1940 /* See if we can do this by sign_extending a constant that is known
1941 to be negative. This is a good, way of doing it, since the shift
1942 may well merge into a subsequent insn. */
1943 if (set_sign_bit_copies > 1)
1945 if (const_ok_for_arm
1946 (temp1 = ARM_SIGN_EXTEND (remainder
1947 << (set_sign_bit_copies - 1))))
1949 if (generate)
1951 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1952 emit_constant_insn (cond,
1953 gen_rtx_SET (VOIDmode, new_src,
1954 GEN_INT (temp1)));
1955 emit_constant_insn (cond,
1956 gen_ashrsi3 (target, new_src,
1957 GEN_INT (set_sign_bit_copies - 1)));
1959 return 2;
1961 /* For an inverted constant, we will need to set the low bits,
1962 these will be shifted out of harm's way. */
1963 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1964 if (const_ok_for_arm (~temp1))
1966 if (generate)
1968 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1969 emit_constant_insn (cond,
1970 gen_rtx_SET (VOIDmode, new_src,
1971 GEN_INT (temp1)));
1972 emit_constant_insn (cond,
1973 gen_ashrsi3 (target, new_src,
1974 GEN_INT (set_sign_bit_copies - 1)));
1976 return 2;
1980 /* See if we can calculate the value as the difference between two
1981 valid immediates. */
1982 if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
1984 int topshift = clear_sign_bit_copies & ~1;
1986 temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
1987 & (0xff000000 >> topshift));
1989 /* If temp1 is zero, then that means the 9 most significant
1990 bits of remainder were 1 and we've caused it to overflow.
1991 When topshift is 0 we don't need to do anything since we
1992 can borrow from 'bit 32'. */
1993 if (temp1 == 0 && topshift != 0)
1994 temp1 = 0x80000000 >> (topshift - 1);
1996 temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
1998 if (const_ok_for_arm (temp2))
2000 if (generate)
2002 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2003 emit_constant_insn (cond,
2004 gen_rtx_SET (VOIDmode, new_src,
2005 GEN_INT (temp1)));
2006 emit_constant_insn (cond,
2007 gen_addsi3 (target, new_src,
2008 GEN_INT (-temp2)));
2011 return 2;
2015 /* See if we can generate this by setting the bottom (or the top)
2016 16 bits, and then shifting these into the other half of the
2017 word. We only look for the simplest cases, to do more would cost
2018 too much. Be careful, however, not to generate this when the
2019 alternative would take fewer insns. */
2020 if (val & 0xffff0000)
2022 temp1 = remainder & 0xffff0000;
2023 temp2 = remainder & 0x0000ffff;
2025 /* Overlaps outside this range are best done using other methods. */
2026 for (i = 9; i < 24; i++)
2028 if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2029 && !const_ok_for_arm (temp2))
2031 rtx new_src = (subtargets
2032 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2033 : target);
2034 insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2035 source, subtargets, generate);
2036 source = new_src;
2037 if (generate)
2038 emit_constant_insn
2039 (cond,
2040 gen_rtx_SET
2041 (VOIDmode, target,
2042 gen_rtx_IOR (mode,
2043 gen_rtx_ASHIFT (mode, source,
2044 GEN_INT (i)),
2045 source)));
2046 return insns + 1;
2050 /* Don't duplicate cases already considered. */
2051 for (i = 17; i < 24; i++)
2053 if (((temp1 | (temp1 >> i)) == remainder)
2054 && !const_ok_for_arm (temp1))
2056 rtx new_src = (subtargets
2057 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2058 : target);
2059 insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2060 source, subtargets, generate);
2061 source = new_src;
2062 if (generate)
2063 emit_constant_insn
2064 (cond,
2065 gen_rtx_SET (VOIDmode, target,
2066 gen_rtx_IOR
2067 (mode,
2068 gen_rtx_LSHIFTRT (mode, source,
2069 GEN_INT (i)),
2070 source)));
2071 return insns + 1;
2075 break;
2077 case IOR:
2078 case XOR:
2079 /* If we have IOR or XOR, and the constant can be loaded in a
2080 single instruction, and we can find a temporary to put it in,
2081 then this can be done in two instructions instead of 3-4. */
2082 if (subtargets
2083 /* TARGET can't be NULL if SUBTARGETS is 0 */
2084 || (reload_completed && !reg_mentioned_p (target, source)))
2086 if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2088 if (generate)
2090 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2092 emit_constant_insn (cond,
2093 gen_rtx_SET (VOIDmode, sub,
2094 GEN_INT (val)));
2095 emit_constant_insn (cond,
2096 gen_rtx_SET (VOIDmode, target,
2097 gen_rtx_fmt_ee (code, mode,
2098 source, sub)));
2100 return 2;
2104 if (code == XOR)
2105 break;
2107 if (set_sign_bit_copies > 8
2108 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2110 if (generate)
2112 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2113 rtx shift = GEN_INT (set_sign_bit_copies);
2115 emit_constant_insn
2116 (cond,
2117 gen_rtx_SET (VOIDmode, sub,
2118 gen_rtx_NOT (mode,
2119 gen_rtx_ASHIFT (mode,
2120 source,
2121 shift))));
2122 emit_constant_insn
2123 (cond,
2124 gen_rtx_SET (VOIDmode, target,
2125 gen_rtx_NOT (mode,
2126 gen_rtx_LSHIFTRT (mode, sub,
2127 shift))));
2129 return 2;
2132 if (set_zero_bit_copies > 8
2133 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2135 if (generate)
2137 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2138 rtx shift = GEN_INT (set_zero_bit_copies);
2140 emit_constant_insn
2141 (cond,
2142 gen_rtx_SET (VOIDmode, sub,
2143 gen_rtx_NOT (mode,
2144 gen_rtx_LSHIFTRT (mode,
2145 source,
2146 shift))));
2147 emit_constant_insn
2148 (cond,
2149 gen_rtx_SET (VOIDmode, target,
2150 gen_rtx_NOT (mode,
2151 gen_rtx_ASHIFT (mode, sub,
2152 shift))));
2154 return 2;
2157 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2159 if (generate)
2161 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2162 emit_constant_insn (cond,
2163 gen_rtx_SET (VOIDmode, sub,
2164 gen_rtx_NOT (mode, source)));
2165 source = sub;
2166 if (subtargets)
2167 sub = gen_reg_rtx (mode);
2168 emit_constant_insn (cond,
2169 gen_rtx_SET (VOIDmode, sub,
2170 gen_rtx_AND (mode, source,
2171 GEN_INT (temp1))));
2172 emit_constant_insn (cond,
2173 gen_rtx_SET (VOIDmode, target,
2174 gen_rtx_NOT (mode, sub)));
2176 return 3;
2178 break;
2180 case AND:
2181 /* See if two shifts will do 2 or more insn's worth of work. */
2182 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2184 HOST_WIDE_INT shift_mask = ((0xffffffff
2185 << (32 - clear_sign_bit_copies))
2186 & 0xffffffff);
2188 if ((remainder | shift_mask) != 0xffffffff)
2190 if (generate)
2192 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2193 insns = arm_gen_constant (AND, mode, cond,
2194 remainder | shift_mask,
2195 new_src, source, subtargets, 1);
2196 source = new_src;
2198 else
2200 rtx targ = subtargets ? NULL_RTX : target;
2201 insns = arm_gen_constant (AND, mode, cond,
2202 remainder | shift_mask,
2203 targ, source, subtargets, 0);
2207 if (generate)
2209 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2210 rtx shift = GEN_INT (clear_sign_bit_copies);
2212 emit_insn (gen_ashlsi3 (new_src, source, shift));
2213 emit_insn (gen_lshrsi3 (target, new_src, shift));
2216 return insns + 2;
2219 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2221 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2223 if ((remainder | shift_mask) != 0xffffffff)
2225 if (generate)
2227 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2229 insns = arm_gen_constant (AND, mode, cond,
2230 remainder | shift_mask,
2231 new_src, source, subtargets, 1);
2232 source = new_src;
2234 else
2236 rtx targ = subtargets ? NULL_RTX : target;
2238 insns = arm_gen_constant (AND, mode, cond,
2239 remainder | shift_mask,
2240 targ, source, subtargets, 0);
2244 if (generate)
2246 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2247 rtx shift = GEN_INT (clear_zero_bit_copies);
2249 emit_insn (gen_lshrsi3 (new_src, source, shift));
2250 emit_insn (gen_ashlsi3 (target, new_src, shift));
2253 return insns + 2;
2256 break;
2258 default:
2259 break;
2262 for (i = 0; i < 32; i++)
2263 if (remainder & (1 << i))
2264 num_bits_set++;
2266 if (code == AND || (can_invert && num_bits_set > 16))
2267 remainder = (~remainder) & 0xffffffff;
2268 else if (code == PLUS && num_bits_set > 16)
2269 remainder = (-remainder) & 0xffffffff;
2270 else
2272 can_invert = 0;
2273 can_negate = 0;
2276 /* Now try and find a way of doing the job in either two or three
2277 instructions.
2278 We start by looking for the largest block of zeros that are aligned on
2279 a 2-bit boundary, we then fill up the temps, wrapping around to the
2280 top of the word when we drop off the bottom.
2281 In the worst case this code should produce no more than four insns. */
2283 int best_start = 0;
2284 int best_consecutive_zeros = 0;
2286 for (i = 0; i < 32; i += 2)
2288 int consecutive_zeros = 0;
2290 if (!(remainder & (3 << i)))
2292 while ((i < 32) && !(remainder & (3 << i)))
2294 consecutive_zeros += 2;
2295 i += 2;
2297 if (consecutive_zeros > best_consecutive_zeros)
2299 best_consecutive_zeros = consecutive_zeros;
2300 best_start = i - consecutive_zeros;
2302 i -= 2;
2306 /* So long as it won't require any more insns to do so, it's
2307 desirable to emit a small constant (in bits 0...9) in the last
2308 insn. This way there is more chance that it can be combined with
2309 a later addressing insn to form a pre-indexed load or store
2310 operation. Consider:
2312 *((volatile int *)0xe0000100) = 1;
2313 *((volatile int *)0xe0000110) = 2;
2315 We want this to wind up as:
2317 mov rA, #0xe0000000
2318 mov rB, #1
2319 str rB, [rA, #0x100]
2320 mov rB, #2
2321 str rB, [rA, #0x110]
2323 rather than having to synthesize both large constants from scratch.
2325 Therefore, we calculate how many insns would be required to emit
2326 the constant starting from `best_start', and also starting from
2327 zero (i.e. with bit 31 first to be output). If `best_start' doesn't
2328 yield a shorter sequence, we may as well use zero. */
2329 if (best_start != 0
2330 && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2331 && (count_insns_for_constant (remainder, 0) <=
2332 count_insns_for_constant (remainder, best_start)))
2333 best_start = 0;
2335 /* Now start emitting the insns. */
2336 i = best_start;
2339 int end;
2341 if (i <= 0)
2342 i += 32;
2343 if (remainder & (3 << (i - 2)))
2345 end = i - 8;
2346 if (end < 0)
2347 end += 32;
2348 temp1 = remainder & ((0x0ff << end)
2349 | ((i < end) ? (0xff >> (32 - end)) : 0));
2350 remainder &= ~temp1;
2352 if (generate)
2354 rtx new_src, temp1_rtx;
2356 if (code == SET || code == MINUS)
2358 new_src = (subtargets ? gen_reg_rtx (mode) : target);
2359 if (can_invert && code != MINUS)
2360 temp1 = ~temp1;
2362 else
2364 if (remainder && subtargets)
2365 new_src = gen_reg_rtx (mode);
2366 else
2367 new_src = target;
2368 if (can_invert)
2369 temp1 = ~temp1;
2370 else if (can_negate)
2371 temp1 = -temp1;
2374 temp1 = trunc_int_for_mode (temp1, mode);
2375 temp1_rtx = GEN_INT (temp1);
2377 if (code == SET)
2379 else if (code == MINUS)
2380 temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2381 else
2382 temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2384 emit_constant_insn (cond,
2385 gen_rtx_SET (VOIDmode, new_src,
2386 temp1_rtx));
2387 source = new_src;
2390 if (code == SET)
2392 can_invert = 0;
2393 code = PLUS;
2395 else if (code == MINUS)
2396 code = PLUS;
2398 insns++;
2399 i -= 6;
2401 i -= 2;
2403 while (remainder);
2406 return insns;
2409 /* Canonicalize a comparison so that we are more likely to recognize it.
2410 This can be done for a few constant compares, where we can make the
2411 immediate value easier to load. */
2413 enum rtx_code
2414 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2415 rtx * op1)
2417 unsigned HOST_WIDE_INT i = INTVAL (*op1);
2418 unsigned HOST_WIDE_INT maxval;
2419 maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2421 switch (code)
2423 case EQ:
2424 case NE:
2425 return code;
2427 case GT:
2428 case LE:
2429 if (i != maxval
2430 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2432 *op1 = GEN_INT (i + 1);
2433 return code == GT ? GE : LT;
2435 break;
2437 case GE:
2438 case LT:
2439 if (i != ~maxval
2440 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2442 *op1 = GEN_INT (i - 1);
2443 return code == GE ? GT : LE;
2445 break;
2447 case GTU:
2448 case LEU:
2449 if (i != ~((unsigned HOST_WIDE_INT) 0)
2450 && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2452 *op1 = GEN_INT (i + 1);
2453 return code == GTU ? GEU : LTU;
2455 break;
2457 case GEU:
2458 case LTU:
2459 if (i != 0
2460 && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2462 *op1 = GEN_INT (i - 1);
2463 return code == GEU ? GTU : LEU;
2465 break;
2467 default:
2468 gcc_unreachable ();
2471 return code;
2475 /* Define how to find the value returned by a function. */
2478 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2480 enum machine_mode mode;
2481 int unsignedp ATTRIBUTE_UNUSED;
2482 rtx r ATTRIBUTE_UNUSED;
2484 mode = TYPE_MODE (type);
2485 /* Promote integer types. */
2486 if (INTEGRAL_TYPE_P (type))
2487 PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2489 /* Promotes small structs returned in a register to full-word size
2490 for big-endian AAPCS. */
2491 if (arm_return_in_msb (type))
2493 HOST_WIDE_INT size = int_size_in_bytes (type);
2494 if (size % UNITS_PER_WORD != 0)
2496 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2497 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2501 return LIBCALL_VALUE(mode);
2504 /* Determine the amount of memory needed to store the possible return
2505 registers of an untyped call. */
2507 arm_apply_result_size (void)
2509 int size = 16;
2511 if (TARGET_ARM)
2513 if (TARGET_HARD_FLOAT_ABI)
2515 if (TARGET_FPA)
2516 size += 12;
2517 if (TARGET_MAVERICK)
2518 size += 8;
2520 if (TARGET_IWMMXT_ABI)
2521 size += 8;
2524 return size;
2527 /* Decide whether a type should be returned in memory (true)
2528 or in a register (false). This is called by the macro
2529 RETURN_IN_MEMORY. */
2531 arm_return_in_memory (tree type)
2533 HOST_WIDE_INT size;
2535 if (!AGGREGATE_TYPE_P (type) &&
2536 (TREE_CODE (type) != VECTOR_TYPE) &&
2537 !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2538 /* All simple types are returned in registers.
2539 For AAPCS, complex types are treated the same as aggregates. */
2540 return 0;
2542 size = int_size_in_bytes (type);
2544 if (arm_abi != ARM_ABI_APCS)
2546 /* ATPCS and later return aggregate types in memory only if they are
2547 larger than a word (or are variable size). */
2548 return (size < 0 || size > UNITS_PER_WORD);
2551 /* To maximize backwards compatibility with previous versions of gcc,
2552 return vectors up to 4 words in registers. */
2553 if (TREE_CODE (type) == VECTOR_TYPE)
2554 return (size < 0 || size > (4 * UNITS_PER_WORD));
2556 /* For the arm-wince targets we choose to be compatible with Microsoft's
2557 ARM and Thumb compilers, which always return aggregates in memory. */
2558 #ifndef ARM_WINCE
2559 /* All structures/unions bigger than one word are returned in memory.
2560 Also catch the case where int_size_in_bytes returns -1. In this case
2561 the aggregate is either huge or of variable size, and in either case
2562 we will want to return it via memory and not in a register. */
2563 if (size < 0 || size > UNITS_PER_WORD)
2564 return 1;
2566 if (TREE_CODE (type) == RECORD_TYPE)
2568 tree field;
2570 /* For a struct the APCS says that we only return in a register
2571 if the type is 'integer like' and every addressable element
2572 has an offset of zero. For practical purposes this means
2573 that the structure can have at most one non bit-field element
2574 and that this element must be the first one in the structure. */
2576 /* Find the first field, ignoring non FIELD_DECL things which will
2577 have been created by C++. */
2578 for (field = TYPE_FIELDS (type);
2579 field && TREE_CODE (field) != FIELD_DECL;
2580 field = TREE_CHAIN (field))
2581 continue;
2583 if (field == NULL)
2584 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
2586 /* Check that the first field is valid for returning in a register. */
2588 /* ... Floats are not allowed */
2589 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2590 return 1;
2592 /* ... Aggregates that are not themselves valid for returning in
2593 a register are not allowed. */
2594 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2595 return 1;
2597 /* Now check the remaining fields, if any. Only bitfields are allowed,
2598 since they are not addressable. */
2599 for (field = TREE_CHAIN (field);
2600 field;
2601 field = TREE_CHAIN (field))
2603 if (TREE_CODE (field) != FIELD_DECL)
2604 continue;
2606 if (!DECL_BIT_FIELD_TYPE (field))
2607 return 1;
2610 return 0;
2613 if (TREE_CODE (type) == UNION_TYPE)
2615 tree field;
2617 /* Unions can be returned in registers if every element is
2618 integral, or can be returned in an integer register. */
2619 for (field = TYPE_FIELDS (type);
2620 field;
2621 field = TREE_CHAIN (field))
2623 if (TREE_CODE (field) != FIELD_DECL)
2624 continue;
2626 if (FLOAT_TYPE_P (TREE_TYPE (field)))
2627 return 1;
2629 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2630 return 1;
2633 return 0;
2635 #endif /* not ARM_WINCE */
2637 /* Return all other types in memory. */
2638 return 1;
2641 /* Indicate whether or not words of a double are in big-endian order. */
2644 arm_float_words_big_endian (void)
2646 if (TARGET_MAVERICK)
2647 return 0;
2649 /* For FPA, float words are always big-endian. For VFP, floats words
2650 follow the memory system mode. */
2652 if (TARGET_FPA)
2654 return 1;
2657 if (TARGET_VFP)
2658 return (TARGET_BIG_END ? 1 : 0);
2660 return 1;
2663 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2664 for a call to a function whose data type is FNTYPE.
2665 For a library call, FNTYPE is NULL. */
2666 void
2667 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2668 rtx libname ATTRIBUTE_UNUSED,
2669 tree fndecl ATTRIBUTE_UNUSED)
2671 /* On the ARM, the offset starts at 0. */
2672 pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2673 pcum->iwmmxt_nregs = 0;
2674 pcum->can_split = true;
2676 pcum->call_cookie = CALL_NORMAL;
2678 if (TARGET_LONG_CALLS)
2679 pcum->call_cookie = CALL_LONG;
2681 /* Check for long call/short call attributes. The attributes
2682 override any command line option. */
2683 if (fntype)
2685 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2686 pcum->call_cookie = CALL_SHORT;
2687 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2688 pcum->call_cookie = CALL_LONG;
2691 /* Varargs vectors are treated the same as long long.
2692 named_count avoids having to change the way arm handles 'named' */
2693 pcum->named_count = 0;
2694 pcum->nargs = 0;
2696 if (TARGET_REALLY_IWMMXT && fntype)
2698 tree fn_arg;
2700 for (fn_arg = TYPE_ARG_TYPES (fntype);
2701 fn_arg;
2702 fn_arg = TREE_CHAIN (fn_arg))
2703 pcum->named_count += 1;
2705 if (! pcum->named_count)
2706 pcum->named_count = INT_MAX;
2711 /* Return true if mode/type need doubleword alignment. */
2712 bool
2713 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2715 return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2716 || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2720 /* Determine where to put an argument to a function.
2721 Value is zero to push the argument on the stack,
2722 or a hard register in which to store the argument.
2724 MODE is the argument's machine mode.
2725 TYPE is the data type of the argument (as a tree).
2726 This is null for libcalls where that information may
2727 not be available.
2728 CUM is a variable of type CUMULATIVE_ARGS which gives info about
2729 the preceding args and about the function being called.
2730 NAMED is nonzero if this argument is a named parameter
2731 (otherwise it is an extra parameter matching an ellipsis). */
2734 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2735 tree type, int named)
2737 int nregs;
2739 /* Varargs vectors are treated the same as long long.
2740 named_count avoids having to change the way arm handles 'named' */
2741 if (TARGET_IWMMXT_ABI
2742 && arm_vector_mode_supported_p (mode)
2743 && pcum->named_count > pcum->nargs + 1)
2745 if (pcum->iwmmxt_nregs <= 9)
2746 return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2747 else
2749 pcum->can_split = false;
2750 return NULL_RTX;
2754 /* Put doubleword aligned quantities in even register pairs. */
2755 if (pcum->nregs & 1
2756 && ARM_DOUBLEWORD_ALIGN
2757 && arm_needs_doubleword_align (mode, type))
2758 pcum->nregs++;
2760 if (mode == VOIDmode)
2761 /* Compute operand 2 of the call insn. */
2762 return GEN_INT (pcum->call_cookie);
2764 /* Only allow splitting an arg between regs and memory if all preceding
2765 args were allocated to regs. For args passed by reference we only count
2766 the reference pointer. */
2767 if (pcum->can_split)
2768 nregs = 1;
2769 else
2770 nregs = ARM_NUM_REGS2 (mode, type);
2772 if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2773 return NULL_RTX;
2775 return gen_rtx_REG (mode, pcum->nregs);
2778 static int
2779 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2780 tree type, bool named ATTRIBUTE_UNUSED)
2782 int nregs = pcum->nregs;
2784 if (arm_vector_mode_supported_p (mode))
2785 return 0;
2787 if (NUM_ARG_REGS > nregs
2788 && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2789 && pcum->can_split)
2790 return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2792 return 0;
2795 /* Variable sized types are passed by reference. This is a GCC
2796 extension to the ARM ABI. */
2798 static bool
2799 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2800 enum machine_mode mode ATTRIBUTE_UNUSED,
2801 tree type, bool named ATTRIBUTE_UNUSED)
2803 return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2806 /* Encode the current state of the #pragma [no_]long_calls. */
2807 typedef enum
2809 OFF, /* No #pramgma [no_]long_calls is in effect. */
2810 LONG, /* #pragma long_calls is in effect. */
2811 SHORT /* #pragma no_long_calls is in effect. */
2812 } arm_pragma_enum;
2814 static arm_pragma_enum arm_pragma_long_calls = OFF;
2816 void
2817 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2819 arm_pragma_long_calls = LONG;
2822 void
2823 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2825 arm_pragma_long_calls = SHORT;
2828 void
2829 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2831 arm_pragma_long_calls = OFF;
2834 /* Table of machine attributes. */
2835 const struct attribute_spec arm_attribute_table[] =
2837 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2838 /* Function calls made to this symbol must be done indirectly, because
2839 it may lie outside of the 26 bit addressing range of a normal function
2840 call. */
2841 { "long_call", 0, 0, false, true, true, NULL },
2842 /* Whereas these functions are always known to reside within the 26 bit
2843 addressing range. */
2844 { "short_call", 0, 0, false, true, true, NULL },
2845 /* Interrupt Service Routines have special prologue and epilogue requirements. */
2846 { "isr", 0, 1, false, false, false, arm_handle_isr_attribute },
2847 { "interrupt", 0, 1, false, false, false, arm_handle_isr_attribute },
2848 { "naked", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2849 #ifdef ARM_PE
2850 /* ARM/PE has three new attributes:
2851 interfacearm - ?
2852 dllexport - for exporting a function/variable that will live in a dll
2853 dllimport - for importing a function/variable from a dll
2855 Microsoft allows multiple declspecs in one __declspec, separating
2856 them with spaces. We do NOT support this. Instead, use __declspec
2857 multiple times.
2859 { "dllimport", 0, 0, true, false, false, NULL },
2860 { "dllexport", 0, 0, true, false, false, NULL },
2861 { "interfacearm", 0, 0, true, false, false, arm_handle_fndecl_attribute },
2862 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2863 { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
2864 { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
2865 { "notshared", 0, 0, false, true, false, arm_handle_notshared_attribute },
2866 #endif
2867 { NULL, 0, 0, false, false, false, NULL }
2870 /* Handle an attribute requiring a FUNCTION_DECL;
2871 arguments as in struct attribute_spec.handler. */
2872 static tree
2873 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2874 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2876 if (TREE_CODE (*node) != FUNCTION_DECL)
2878 warning (OPT_Wattributes, "%qs attribute only applies to functions",
2879 IDENTIFIER_POINTER (name));
2880 *no_add_attrs = true;
2883 return NULL_TREE;
2886 /* Handle an "interrupt" or "isr" attribute;
2887 arguments as in struct attribute_spec.handler. */
2888 static tree
2889 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2890 bool *no_add_attrs)
2892 if (DECL_P (*node))
2894 if (TREE_CODE (*node) != FUNCTION_DECL)
2896 warning (OPT_Wattributes, "%qs attribute only applies to functions",
2897 IDENTIFIER_POINTER (name));
2898 *no_add_attrs = true;
2900 /* FIXME: the argument if any is checked for type attributes;
2901 should it be checked for decl ones? */
2903 else
2905 if (TREE_CODE (*node) == FUNCTION_TYPE
2906 || TREE_CODE (*node) == METHOD_TYPE)
2908 if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2910 warning (OPT_Wattributes, "%qs attribute ignored",
2911 IDENTIFIER_POINTER (name));
2912 *no_add_attrs = true;
2915 else if (TREE_CODE (*node) == POINTER_TYPE
2916 && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2917 || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2918 && arm_isr_value (args) != ARM_FT_UNKNOWN)
2920 *node = build_variant_type_copy (*node);
2921 TREE_TYPE (*node) = build_type_attribute_variant
2922 (TREE_TYPE (*node),
2923 tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2924 *no_add_attrs = true;
2926 else
2928 /* Possibly pass this attribute on from the type to a decl. */
2929 if (flags & ((int) ATTR_FLAG_DECL_NEXT
2930 | (int) ATTR_FLAG_FUNCTION_NEXT
2931 | (int) ATTR_FLAG_ARRAY_NEXT))
2933 *no_add_attrs = true;
2934 return tree_cons (name, args, NULL_TREE);
2936 else
2938 warning (OPT_Wattributes, "%qs attribute ignored",
2939 IDENTIFIER_POINTER (name));
2944 return NULL_TREE;
2947 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2948 /* Handle the "notshared" attribute. This attribute is another way of
2949 requesting hidden visibility. ARM's compiler supports
2950 "__declspec(notshared)"; we support the same thing via an
2951 attribute. */
2953 static tree
2954 arm_handle_notshared_attribute (tree *node,
2955 tree name ATTRIBUTE_UNUSED,
2956 tree args ATTRIBUTE_UNUSED,
2957 int flags ATTRIBUTE_UNUSED,
2958 bool *no_add_attrs)
2960 tree decl = TYPE_NAME (*node);
2962 if (decl)
2964 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2965 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2966 *no_add_attrs = false;
2968 return NULL_TREE;
2970 #endif
2972 /* Return 0 if the attributes for two types are incompatible, 1 if they
2973 are compatible, and 2 if they are nearly compatible (which causes a
2974 warning to be generated). */
2975 static int
2976 arm_comp_type_attributes (tree type1, tree type2)
2978 int l1, l2, s1, s2;
2980 /* Check for mismatch of non-default calling convention. */
2981 if (TREE_CODE (type1) != FUNCTION_TYPE)
2982 return 1;
2984 /* Check for mismatched call attributes. */
2985 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2986 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2987 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2988 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2990 /* Only bother to check if an attribute is defined. */
2991 if (l1 | l2 | s1 | s2)
2993 /* If one type has an attribute, the other must have the same attribute. */
2994 if ((l1 != l2) || (s1 != s2))
2995 return 0;
2997 /* Disallow mixed attributes. */
2998 if ((l1 & s2) || (l2 & s1))
2999 return 0;
3002 /* Check for mismatched ISR attribute. */
3003 l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
3004 if (! l1)
3005 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
3006 l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
3007 if (! l2)
3008 l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
3009 if (l1 != l2)
3010 return 0;
3012 return 1;
3015 /* Encode long_call or short_call attribute by prefixing
3016 symbol name in DECL with a special character FLAG. */
3017 void
3018 arm_encode_call_attribute (tree decl, int flag)
3020 const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
3021 int len = strlen (str);
3022 char * newstr;
3024 /* Do not allow weak functions to be treated as short call. */
3025 if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
3026 return;
3028 newstr = alloca (len + 2);
3029 newstr[0] = flag;
3030 strcpy (newstr + 1, str);
3032 newstr = (char *) ggc_alloc_string (newstr, len + 1);
3033 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
3036 /* Assigns default attributes to newly defined type. This is used to
3037 set short_call/long_call attributes for function types of
3038 functions defined inside corresponding #pragma scopes. */
3039 static void
3040 arm_set_default_type_attributes (tree type)
3042 /* Add __attribute__ ((long_call)) to all functions, when
3043 inside #pragma long_calls or __attribute__ ((short_call)),
3044 when inside #pragma no_long_calls. */
3045 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3047 tree type_attr_list, attr_name;
3048 type_attr_list = TYPE_ATTRIBUTES (type);
3050 if (arm_pragma_long_calls == LONG)
3051 attr_name = get_identifier ("long_call");
3052 else if (arm_pragma_long_calls == SHORT)
3053 attr_name = get_identifier ("short_call");
3054 else
3055 return;
3057 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3058 TYPE_ATTRIBUTES (type) = type_attr_list;
3062 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
3063 defined within the current compilation unit. If this cannot be
3064 determined, then 0 is returned. */
3065 static int
3066 current_file_function_operand (rtx sym_ref)
3068 /* This is a bit of a fib. A function will have a short call flag
3069 applied to its name if it has the short call attribute, or it has
3070 already been defined within the current compilation unit. */
3071 if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
3072 return 1;
3074 /* The current function is always defined within the current compilation
3075 unit. If it s a weak definition however, then this may not be the real
3076 definition of the function, and so we have to say no. */
3077 if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
3078 && !DECL_WEAK (current_function_decl))
3079 return 1;
3081 /* We cannot make the determination - default to returning 0. */
3082 return 0;
3085 /* Return nonzero if a 32 bit "long_call" should be generated for
3086 this call. We generate a long_call if the function:
3088 a. has an __attribute__((long call))
3089 or b. is within the scope of a #pragma long_calls
3090 or c. the -mlong-calls command line switch has been specified
3091 . and either:
3092 1. -ffunction-sections is in effect
3093 or 2. the current function has __attribute__ ((section))
3094 or 3. the target function has __attribute__ ((section))
3096 However we do not generate a long call if the function:
3098 d. has an __attribute__ ((short_call))
3099 or e. is inside the scope of a #pragma no_long_calls
3100 or f. is defined within the current compilation unit.
3102 This function will be called by C fragments contained in the machine
3103 description file. SYM_REF and CALL_COOKIE correspond to the matched
3104 rtl operands. CALL_SYMBOL is used to distinguish between
3105 two different callers of the function. It is set to 1 in the
3106 "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
3107 and "call_value" patterns. This is because of the difference in the
3108 SYM_REFs passed by these patterns. */
3110 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
3112 if (!call_symbol)
3114 if (GET_CODE (sym_ref) != MEM)
3115 return 0;
3117 sym_ref = XEXP (sym_ref, 0);
3120 if (GET_CODE (sym_ref) != SYMBOL_REF)
3121 return 0;
3123 if (call_cookie & CALL_SHORT)
3124 return 0;
3126 if (TARGET_LONG_CALLS)
3128 if (flag_function_sections
3129 || DECL_SECTION_NAME (current_function_decl))
3130 /* c.3 is handled by the definition of the
3131 ARM_DECLARE_FUNCTION_SIZE macro. */
3132 return 1;
3135 if (current_file_function_operand (sym_ref))
3136 return 0;
3138 return (call_cookie & CALL_LONG)
3139 || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
3140 || TARGET_LONG_CALLS;
3143 /* Return nonzero if it is ok to make a tail-call to DECL. */
3144 static bool
3145 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3147 int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
3149 if (cfun->machine->sibcall_blocked)
3150 return false;
3152 /* Never tailcall something for which we have no decl, or if we
3153 are in Thumb mode. */
3154 if (decl == NULL || TARGET_THUMB)
3155 return false;
3157 /* Get the calling method. */
3158 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3159 call_type = CALL_SHORT;
3160 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3161 call_type = CALL_LONG;
3163 /* Cannot tail-call to long calls, since these are out of range of
3164 a branch instruction. However, if not compiling PIC, we know
3165 we can reach the symbol if it is in this compilation unit. */
3166 if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
3167 return false;
3169 /* If we are interworking and the function is not declared static
3170 then we can't tail-call it unless we know that it exists in this
3171 compilation unit (since it might be a Thumb routine). */
3172 if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3173 return false;
3175 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
3176 if (IS_INTERRUPT (arm_current_func_type ()))
3177 return false;
3179 /* Everything else is ok. */
3180 return true;
3184 /* Addressing mode support functions. */
3186 /* Return nonzero if X is a legitimate immediate operand when compiling
3187 for PIC. We know that X satisfies CONSTANT_P and flag_pic is true. */
3189 legitimate_pic_operand_p (rtx x)
3191 if (GET_CODE (x) == SYMBOL_REF
3192 || (GET_CODE (x) == CONST
3193 && GET_CODE (XEXP (x, 0)) == PLUS
3194 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
3195 return 0;
3197 return 1;
3201 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3203 if (GET_CODE (orig) == SYMBOL_REF
3204 || GET_CODE (orig) == LABEL_REF)
3206 #ifndef AOF_ASSEMBLER
3207 rtx pic_ref, address;
3208 #endif
3209 rtx insn;
3210 int subregs = 0;
3212 /* If this function doesn't have a pic register, create one now.
3213 A lot of the logic here is made obscure by the fact that this
3214 routine gets called as part of the rtx cost estimation
3215 process. We don't want those calls to affect any assumptions
3216 about the real function; and further, we can't call
3217 entry_of_function() until we start the real expansion
3218 process. */
3219 if (!current_function_uses_pic_offset_table)
3221 gcc_assert (!no_new_pseudos);
3222 if (arm_pic_register != INVALID_REGNUM)
3224 cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
3226 /* Play games to avoid marking the function as needing pic
3227 if we are being called as part of the cost-estimation
3228 process. */
3229 if (!ir_type())
3230 current_function_uses_pic_offset_table = 1;
3232 else
3234 rtx seq;
3236 cfun->machine->pic_reg = gen_reg_rtx (Pmode);
3238 /* Play games to avoid marking the function as needing pic
3239 if we are being called as part of the cost-estimation
3240 process. */
3241 if (!ir_type())
3243 current_function_uses_pic_offset_table = 1;
3244 start_sequence ();
3246 arm_load_pic_register (0UL);
3248 seq = get_insns ();
3249 end_sequence ();
3250 emit_insn_after (seq, entry_of_function ());
3255 if (reg == 0)
3257 gcc_assert (!no_new_pseudos);
3258 reg = gen_reg_rtx (Pmode);
3260 subregs = 1;
3263 #ifdef AOF_ASSEMBLER
3264 /* The AOF assembler can generate relocations for these directly, and
3265 understands that the PIC register has to be added into the offset. */
3266 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3267 #else
3268 if (subregs)
3269 address = gen_reg_rtx (Pmode);
3270 else
3271 address = reg;
3273 if (TARGET_ARM)
3274 emit_insn (gen_pic_load_addr_arm (address, orig));
3275 else
3276 emit_insn (gen_pic_load_addr_thumb (address, orig));
3278 if ((GET_CODE (orig) == LABEL_REF
3279 || (GET_CODE (orig) == SYMBOL_REF &&
3280 SYMBOL_REF_LOCAL_P (orig)))
3281 && NEED_GOT_RELOC)
3282 pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
3283 else
3285 pic_ref = gen_const_mem (Pmode,
3286 gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
3287 address));
3290 insn = emit_move_insn (reg, pic_ref);
3291 #endif
3292 /* Put a REG_EQUAL note on this insn, so that it can be optimized
3293 by loop. */
3294 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3295 REG_NOTES (insn));
3296 return reg;
3298 else if (GET_CODE (orig) == CONST)
3300 rtx base, offset;
3302 if (GET_CODE (XEXP (orig, 0)) == PLUS
3303 && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
3304 return orig;
3306 if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3307 && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3308 return orig;
3310 if (reg == 0)
3312 gcc_assert (!no_new_pseudos);
3313 reg = gen_reg_rtx (Pmode);
3316 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3318 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3319 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3320 base == reg ? 0 : reg);
3322 if (GET_CODE (offset) == CONST_INT)
3324 /* The base register doesn't really matter, we only want to
3325 test the index for the appropriate mode. */
3326 if (!arm_legitimate_index_p (mode, offset, SET, 0))
3328 gcc_assert (!no_new_pseudos);
3329 offset = force_reg (Pmode, offset);
3332 if (GET_CODE (offset) == CONST_INT)
3333 return plus_constant (base, INTVAL (offset));
3336 if (GET_MODE_SIZE (mode) > 4
3337 && (GET_MODE_CLASS (mode) == MODE_INT
3338 || TARGET_SOFT_FLOAT))
3340 emit_insn (gen_addsi3 (reg, base, offset));
3341 return reg;
3344 return gen_rtx_PLUS (Pmode, base, offset);
3347 return orig;
3351 /* Find a spare low register to use during the prolog of a function. */
3353 static int
3354 thumb_find_work_register (unsigned long pushed_regs_mask)
3356 int reg;
3358 /* Check the argument registers first as these are call-used. The
3359 register allocation order means that sometimes r3 might be used
3360 but earlier argument registers might not, so check them all. */
3361 for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3362 if (!regs_ever_live[reg])
3363 return reg;
3365 /* Before going on to check the call-saved registers we can try a couple
3366 more ways of deducing that r3 is available. The first is when we are
3367 pushing anonymous arguments onto the stack and we have less than 4
3368 registers worth of fixed arguments(*). In this case r3 will be part of
3369 the variable argument list and so we can be sure that it will be
3370 pushed right at the start of the function. Hence it will be available
3371 for the rest of the prologue.
3372 (*): ie current_function_pretend_args_size is greater than 0. */
3373 if (cfun->machine->uses_anonymous_args
3374 && current_function_pretend_args_size > 0)
3375 return LAST_ARG_REGNUM;
3377 /* The other case is when we have fixed arguments but less than 4 registers
3378 worth. In this case r3 might be used in the body of the function, but
3379 it is not being used to convey an argument into the function. In theory
3380 we could just check current_function_args_size to see how many bytes are
3381 being passed in argument registers, but it seems that it is unreliable.
3382 Sometimes it will have the value 0 when in fact arguments are being
3383 passed. (See testcase execute/20021111-1.c for an example). So we also
3384 check the args_info.nregs field as well. The problem with this field is
3385 that it makes no allowances for arguments that are passed to the
3386 function but which are not used. Hence we could miss an opportunity
3387 when a function has an unused argument in r3. But it is better to be
3388 safe than to be sorry. */
3389 if (! cfun->machine->uses_anonymous_args
3390 && current_function_args_size >= 0
3391 && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3392 && cfun->args_info.nregs < 4)
3393 return LAST_ARG_REGNUM;
3395 /* Otherwise look for a call-saved register that is going to be pushed. */
3396 for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3397 if (pushed_regs_mask & (1 << reg))
3398 return reg;
3400 /* Something went wrong - thumb_compute_save_reg_mask()
3401 should have arranged for a suitable register to be pushed. */
3402 gcc_unreachable ();
3405 static GTY(()) int pic_labelno;
3407 /* Generate code to load the PIC register. In thumb mode SCRATCH is a
3408 low register. */
3410 void
3411 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
3413 #ifndef AOF_ASSEMBLER
3414 rtx l1, labelno, pic_tmp, pic_tmp2, pic_rtx;
3415 rtx global_offset_table;
3417 if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3418 return;
3420 gcc_assert (flag_pic);
3422 /* We use an UNSPEC rather than a LABEL_REF because this label never appears
3423 in the code stream. */
3425 labelno = GEN_INT (pic_labelno++);
3426 l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3427 l1 = gen_rtx_CONST (VOIDmode, l1);
3429 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3430 /* On the ARM the PC register contains 'dot + 8' at the time of the
3431 addition, on the Thumb it is 'dot + 4'. */
3432 pic_tmp = plus_constant (l1, TARGET_ARM ? 8 : 4);
3433 if (GOT_PCREL)
3434 pic_tmp2 = gen_rtx_CONST (VOIDmode,
3435 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3436 else
3437 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3439 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3441 if (TARGET_ARM)
3443 emit_insn (gen_pic_load_addr_arm (cfun->machine->pic_reg, pic_rtx));
3444 emit_insn (gen_pic_add_dot_plus_eight (cfun->machine->pic_reg,
3445 cfun->machine->pic_reg, labelno));
3447 else
3449 if (arm_pic_register != INVALID_REGNUM
3450 && REGNO (cfun->machine->pic_reg) > LAST_LO_REGNUM)
3452 /* We will have pushed the pic register, so we should always be
3453 able to find a work register. */
3454 pic_tmp = gen_rtx_REG (SImode,
3455 thumb_find_work_register (saved_regs));
3456 emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3457 emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3459 else
3460 emit_insn (gen_pic_load_addr_thumb (cfun->machine->pic_reg, pic_rtx));
3461 emit_insn (gen_pic_add_dot_plus_four (cfun->machine->pic_reg,
3462 cfun->machine->pic_reg, labelno));
3465 /* Need to emit this whether or not we obey regdecls,
3466 since setjmp/longjmp can cause life info to screw up. */
3467 emit_insn (gen_rtx_USE (VOIDmode, cfun->machine->pic_reg));
3468 #endif /* AOF_ASSEMBLER */
3472 /* Return nonzero if X is valid as an ARM state addressing register. */
3473 static int
3474 arm_address_register_rtx_p (rtx x, int strict_p)
3476 int regno;
3478 if (GET_CODE (x) != REG)
3479 return 0;
3481 regno = REGNO (x);
3483 if (strict_p)
3484 return ARM_REGNO_OK_FOR_BASE_P (regno);
3486 return (regno <= LAST_ARM_REGNUM
3487 || regno >= FIRST_PSEUDO_REGISTER
3488 || regno == FRAME_POINTER_REGNUM
3489 || regno == ARG_POINTER_REGNUM);
3492 /* Return TRUE if this rtx is the difference of a symbol and a label,
3493 and will reduce to a PC-relative relocation in the object file.
3494 Expressions like this can be left alone when generating PIC, rather
3495 than forced through the GOT. */
3496 static int
3497 pcrel_constant_p (rtx x)
3499 if (GET_CODE (x) == MINUS)
3500 return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
3502 return FALSE;
3505 /* Return nonzero if X is a valid ARM state address operand. */
3507 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3508 int strict_p)
3510 bool use_ldrd;
3511 enum rtx_code code = GET_CODE (x);
3513 if (arm_address_register_rtx_p (x, strict_p))
3514 return 1;
3516 use_ldrd = (TARGET_LDRD
3517 && (mode == DImode
3518 || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3520 if (code == POST_INC || code == PRE_DEC
3521 || ((code == PRE_INC || code == POST_DEC)
3522 && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3523 return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3525 else if ((code == POST_MODIFY || code == PRE_MODIFY)
3526 && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3527 && GET_CODE (XEXP (x, 1)) == PLUS
3528 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3530 rtx addend = XEXP (XEXP (x, 1), 1);
3532 /* Don't allow ldrd post increment by register because it's hard
3533 to fixup invalid register choices. */
3534 if (use_ldrd
3535 && GET_CODE (x) == POST_MODIFY
3536 && GET_CODE (addend) == REG)
3537 return 0;
3539 return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3540 && arm_legitimate_index_p (mode, addend, outer, strict_p));
3543 /* After reload constants split into minipools will have addresses
3544 from a LABEL_REF. */
3545 else if (reload_completed
3546 && (code == LABEL_REF
3547 || (code == CONST
3548 && GET_CODE (XEXP (x, 0)) == PLUS
3549 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3550 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3551 return 1;
3553 else if (mode == TImode)
3554 return 0;
3556 else if (code == PLUS)
3558 rtx xop0 = XEXP (x, 0);
3559 rtx xop1 = XEXP (x, 1);
3561 return ((arm_address_register_rtx_p (xop0, strict_p)
3562 && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3563 || (arm_address_register_rtx_p (xop1, strict_p)
3564 && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3567 #if 0
3568 /* Reload currently can't handle MINUS, so disable this for now */
3569 else if (GET_CODE (x) == MINUS)
3571 rtx xop0 = XEXP (x, 0);
3572 rtx xop1 = XEXP (x, 1);
3574 return (arm_address_register_rtx_p (xop0, strict_p)
3575 && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3577 #endif
3579 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3580 && code == SYMBOL_REF
3581 && CONSTANT_POOL_ADDRESS_P (x)
3582 && ! (flag_pic
3583 && symbol_mentioned_p (get_pool_constant (x))
3584 && ! pcrel_constant_p (get_pool_constant (x))))
3585 return 1;
3587 return 0;
3590 /* Return nonzero if INDEX is valid for an address index operand in
3591 ARM state. */
3592 static int
3593 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3594 int strict_p)
3596 HOST_WIDE_INT range;
3597 enum rtx_code code = GET_CODE (index);
3599 /* Standard coprocessor addressing modes. */
3600 if (TARGET_HARD_FLOAT
3601 && (TARGET_FPA || TARGET_MAVERICK)
3602 && (GET_MODE_CLASS (mode) == MODE_FLOAT
3603 || (TARGET_MAVERICK && mode == DImode)))
3604 return (code == CONST_INT && INTVAL (index) < 1024
3605 && INTVAL (index) > -1024
3606 && (INTVAL (index) & 3) == 0);
3608 if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3609 return (code == CONST_INT
3610 && INTVAL (index) < 1024
3611 && INTVAL (index) > -1024
3612 && (INTVAL (index) & 3) == 0);
3614 if (arm_address_register_rtx_p (index, strict_p)
3615 && (GET_MODE_SIZE (mode) <= 4))
3616 return 1;
3618 if (mode == DImode || mode == DFmode)
3620 if (code == CONST_INT)
3622 HOST_WIDE_INT val = INTVAL (index);
3624 if (TARGET_LDRD)
3625 return val > -256 && val < 256;
3626 else
3627 return val > -4096 && val < 4092;
3630 return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3633 if (GET_MODE_SIZE (mode) <= 4
3634 && ! (arm_arch4
3635 && (mode == HImode
3636 || (mode == QImode && outer == SIGN_EXTEND))))
3638 if (code == MULT)
3640 rtx xiop0 = XEXP (index, 0);
3641 rtx xiop1 = XEXP (index, 1);
3643 return ((arm_address_register_rtx_p (xiop0, strict_p)
3644 && power_of_two_operand (xiop1, SImode))
3645 || (arm_address_register_rtx_p (xiop1, strict_p)
3646 && power_of_two_operand (xiop0, SImode)));
3648 else if (code == LSHIFTRT || code == ASHIFTRT
3649 || code == ASHIFT || code == ROTATERT)
3651 rtx op = XEXP (index, 1);
3653 return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3654 && GET_CODE (op) == CONST_INT
3655 && INTVAL (op) > 0
3656 && INTVAL (op) <= 31);
3660 /* For ARM v4 we may be doing a sign-extend operation during the
3661 load. */
3662 if (arm_arch4)
3664 if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3665 range = 256;
3666 else
3667 range = 4096;
3669 else
3670 range = (mode == HImode) ? 4095 : 4096;
3672 return (code == CONST_INT
3673 && INTVAL (index) < range
3674 && INTVAL (index) > -range);
3677 /* Return nonzero if X is valid as a Thumb state base register. */
3678 static int
3679 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3681 int regno;
3683 if (GET_CODE (x) != REG)
3684 return 0;
3686 regno = REGNO (x);
3688 if (strict_p)
3689 return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3691 return (regno <= LAST_LO_REGNUM
3692 || regno > LAST_VIRTUAL_REGISTER
3693 || regno == FRAME_POINTER_REGNUM
3694 || (GET_MODE_SIZE (mode) >= 4
3695 && (regno == STACK_POINTER_REGNUM
3696 || regno >= FIRST_PSEUDO_REGISTER
3697 || x == hard_frame_pointer_rtx
3698 || x == arg_pointer_rtx)));
3701 /* Return nonzero if x is a legitimate index register. This is the case
3702 for any base register that can access a QImode object. */
3703 inline static int
3704 thumb_index_register_rtx_p (rtx x, int strict_p)
3706 return thumb_base_register_rtx_p (x, QImode, strict_p);
3709 /* Return nonzero if x is a legitimate Thumb-state address.
3711 The AP may be eliminated to either the SP or the FP, so we use the
3712 least common denominator, e.g. SImode, and offsets from 0 to 64.
3714 ??? Verify whether the above is the right approach.
3716 ??? Also, the FP may be eliminated to the SP, so perhaps that
3717 needs special handling also.
3719 ??? Look at how the mips16 port solves this problem. It probably uses
3720 better ways to solve some of these problems.
3722 Although it is not incorrect, we don't accept QImode and HImode
3723 addresses based on the frame pointer or arg pointer until the
3724 reload pass starts. This is so that eliminating such addresses
3725 into stack based ones won't produce impossible code. */
3727 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3729 /* ??? Not clear if this is right. Experiment. */
3730 if (GET_MODE_SIZE (mode) < 4
3731 && !(reload_in_progress || reload_completed)
3732 && (reg_mentioned_p (frame_pointer_rtx, x)
3733 || reg_mentioned_p (arg_pointer_rtx, x)
3734 || reg_mentioned_p (virtual_incoming_args_rtx, x)
3735 || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3736 || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3737 || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3738 return 0;
3740 /* Accept any base register. SP only in SImode or larger. */
3741 else if (thumb_base_register_rtx_p (x, mode, strict_p))
3742 return 1;
3744 /* This is PC relative data before arm_reorg runs. */
3745 else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3746 && GET_CODE (x) == SYMBOL_REF
3747 && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
3748 return 1;
3750 /* This is PC relative data after arm_reorg runs. */
3751 else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3752 && (GET_CODE (x) == LABEL_REF
3753 || (GET_CODE (x) == CONST
3754 && GET_CODE (XEXP (x, 0)) == PLUS
3755 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3756 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3757 return 1;
3759 /* Post-inc indexing only supported for SImode and larger. */
3760 else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3761 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3762 return 1;
3764 else if (GET_CODE (x) == PLUS)
3766 /* REG+REG address can be any two index registers. */
3767 /* We disallow FRAME+REG addressing since we know that FRAME
3768 will be replaced with STACK, and SP relative addressing only
3769 permits SP+OFFSET. */
3770 if (GET_MODE_SIZE (mode) <= 4
3771 && XEXP (x, 0) != frame_pointer_rtx
3772 && XEXP (x, 1) != frame_pointer_rtx
3773 && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3774 && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3775 return 1;
3777 /* REG+const has 5-7 bit offset for non-SP registers. */
3778 else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3779 || XEXP (x, 0) == arg_pointer_rtx)
3780 && GET_CODE (XEXP (x, 1)) == CONST_INT
3781 && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3782 return 1;
3784 /* REG+const has 10 bit offset for SP, but only SImode and
3785 larger is supported. */
3786 /* ??? Should probably check for DI/DFmode overflow here
3787 just like GO_IF_LEGITIMATE_OFFSET does. */
3788 else if (GET_CODE (XEXP (x, 0)) == REG
3789 && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3790 && GET_MODE_SIZE (mode) >= 4
3791 && GET_CODE (XEXP (x, 1)) == CONST_INT
3792 && INTVAL (XEXP (x, 1)) >= 0
3793 && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3794 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3795 return 1;
3797 else if (GET_CODE (XEXP (x, 0)) == REG
3798 && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3799 && GET_MODE_SIZE (mode) >= 4
3800 && GET_CODE (XEXP (x, 1)) == CONST_INT
3801 && (INTVAL (XEXP (x, 1)) & 3) == 0)
3802 return 1;
3805 else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3806 && GET_MODE_SIZE (mode) == 4
3807 && GET_CODE (x) == SYMBOL_REF
3808 && CONSTANT_POOL_ADDRESS_P (x)
3809 && ! (flag_pic
3810 && symbol_mentioned_p (get_pool_constant (x))
3811 && ! pcrel_constant_p (get_pool_constant (x))))
3812 return 1;
3814 return 0;
3817 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3818 instruction of mode MODE. */
3820 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3822 switch (GET_MODE_SIZE (mode))
3824 case 1:
3825 return val >= 0 && val < 32;
3827 case 2:
3828 return val >= 0 && val < 64 && (val & 1) == 0;
3830 default:
3831 return (val >= 0
3832 && (val + GET_MODE_SIZE (mode)) <= 128
3833 && (val & 3) == 0);
3837 /* Build the SYMBOL_REF for __tls_get_addr. */
3839 static GTY(()) rtx tls_get_addr_libfunc;
3841 static rtx
3842 get_tls_get_addr (void)
3844 if (!tls_get_addr_libfunc)
3845 tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
3846 return tls_get_addr_libfunc;
3849 static rtx
3850 arm_load_tp (rtx target)
3852 if (!target)
3853 target = gen_reg_rtx (SImode);
3855 if (TARGET_HARD_TP)
3857 /* Can return in any reg. */
3858 emit_insn (gen_load_tp_hard (target));
3860 else
3862 /* Always returned in r0. Immediately copy the result into a pseudo,
3863 otherwise other uses of r0 (e.g. setting up function arguments) may
3864 clobber the value. */
3866 rtx tmp;
3868 emit_insn (gen_load_tp_soft ());
3870 tmp = gen_rtx_REG (SImode, 0);
3871 emit_move_insn (target, tmp);
3873 return target;
3876 static rtx
3877 load_tls_operand (rtx x, rtx reg)
3879 rtx tmp;
3881 if (reg == NULL_RTX)
3882 reg = gen_reg_rtx (SImode);
3884 tmp = gen_rtx_CONST (SImode, x);
3886 emit_move_insn (reg, tmp);
3888 return reg;
3891 static rtx
3892 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
3894 rtx insns, label, labelno, sum;
3896 start_sequence ();
3898 labelno = GEN_INT (pic_labelno++);
3899 label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3900 label = gen_rtx_CONST (VOIDmode, label);
3902 sum = gen_rtx_UNSPEC (Pmode,
3903 gen_rtvec (4, x, GEN_INT (reloc), label,
3904 GEN_INT (TARGET_ARM ? 8 : 4)),
3905 UNSPEC_TLS);
3906 reg = load_tls_operand (sum, reg);
3908 if (TARGET_ARM)
3909 emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
3910 else
3911 emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
3913 *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST? */
3914 Pmode, 1, reg, Pmode);
3916 insns = get_insns ();
3917 end_sequence ();
3919 return insns;
3923 legitimize_tls_address (rtx x, rtx reg)
3925 rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
3926 unsigned int model = SYMBOL_REF_TLS_MODEL (x);
3928 switch (model)
3930 case TLS_MODEL_GLOBAL_DYNAMIC:
3931 insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
3932 dest = gen_reg_rtx (Pmode);
3933 emit_libcall_block (insns, dest, ret, x);
3934 return dest;
3936 case TLS_MODEL_LOCAL_DYNAMIC:
3937 insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
3939 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
3940 share the LDM result with other LD model accesses. */
3941 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
3942 UNSPEC_TLS);
3943 dest = gen_reg_rtx (Pmode);
3944 emit_libcall_block (insns, dest, ret, eqv);
3946 /* Load the addend. */
3947 addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
3948 UNSPEC_TLS);
3949 addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
3950 return gen_rtx_PLUS (Pmode, dest, addend);
3952 case TLS_MODEL_INITIAL_EXEC:
3953 labelno = GEN_INT (pic_labelno++);
3954 label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3955 label = gen_rtx_CONST (VOIDmode, label);
3956 sum = gen_rtx_UNSPEC (Pmode,
3957 gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
3958 GEN_INT (TARGET_ARM ? 8 : 4)),
3959 UNSPEC_TLS);
3960 reg = load_tls_operand (sum, reg);
3962 if (TARGET_ARM)
3963 emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
3964 else
3966 emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
3967 emit_move_insn (reg, gen_const_mem (SImode, reg));
3970 tp = arm_load_tp (NULL_RTX);
3972 return gen_rtx_PLUS (Pmode, tp, reg);
3974 case TLS_MODEL_LOCAL_EXEC:
3975 tp = arm_load_tp (NULL_RTX);
3977 reg = gen_rtx_UNSPEC (Pmode,
3978 gen_rtvec (2, x, GEN_INT (TLS_LE32)),
3979 UNSPEC_TLS);
3980 reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
3982 return gen_rtx_PLUS (Pmode, tp, reg);
3984 default:
3985 abort ();
3989 /* Try machine-dependent ways of modifying an illegitimate address
3990 to be legitimate. If we find one, return the new, valid address. */
3992 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3994 if (arm_tls_symbol_p (x))
3995 return legitimize_tls_address (x, NULL_RTX);
3997 if (GET_CODE (x) == PLUS)
3999 rtx xop0 = XEXP (x, 0);
4000 rtx xop1 = XEXP (x, 1);
4002 if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
4003 xop0 = force_reg (SImode, xop0);
4005 if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
4006 xop1 = force_reg (SImode, xop1);
4008 if (ARM_BASE_REGISTER_RTX_P (xop0)
4009 && GET_CODE (xop1) == CONST_INT)
4011 HOST_WIDE_INT n, low_n;
4012 rtx base_reg, val;
4013 n = INTVAL (xop1);
4015 /* VFP addressing modes actually allow greater offsets, but for
4016 now we just stick with the lowest common denominator. */
4017 if (mode == DImode
4018 || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
4020 low_n = n & 0x0f;
4021 n &= ~0x0f;
4022 if (low_n > 4)
4024 n += 16;
4025 low_n -= 16;
4028 else
4030 low_n = ((mode) == TImode ? 0
4031 : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
4032 n -= low_n;
4035 base_reg = gen_reg_rtx (SImode);
4036 val = force_operand (plus_constant (xop0, n), NULL_RTX);
4037 emit_move_insn (base_reg, val);
4038 x = plus_constant (base_reg, low_n);
4040 else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4041 x = gen_rtx_PLUS (SImode, xop0, xop1);
4044 /* XXX We don't allow MINUS any more -- see comment in
4045 arm_legitimate_address_p (). */
4046 else if (GET_CODE (x) == MINUS)
4048 rtx xop0 = XEXP (x, 0);
4049 rtx xop1 = XEXP (x, 1);
4051 if (CONSTANT_P (xop0))
4052 xop0 = force_reg (SImode, xop0);
4054 if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
4055 xop1 = force_reg (SImode, xop1);
4057 if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4058 x = gen_rtx_MINUS (SImode, xop0, xop1);
4061 /* Make sure to take full advantage of the pre-indexed addressing mode
4062 with absolute addresses which often allows for the base register to
4063 be factorized for multiple adjacent memory references, and it might
4064 even allows for the mini pool to be avoided entirely. */
4065 else if (GET_CODE (x) == CONST_INT && optimize > 0)
4067 unsigned int bits;
4068 HOST_WIDE_INT mask, base, index;
4069 rtx base_reg;
4071 /* ldr and ldrb can use a 12 bit index, ldrsb and the rest can only
4072 use a 8 bit index. So let's use a 12 bit index for SImode only and
4073 hope that arm_gen_constant will enable ldrb to use more bits. */
4074 bits = (mode == SImode) ? 12 : 8;
4075 mask = (1 << bits) - 1;
4076 base = INTVAL (x) & ~mask;
4077 index = INTVAL (x) & mask;
4078 if (bit_count (base & 0xffffffff) > (32 - bits)/2)
4080 /* It'll most probably be more efficient to generate the base
4081 with more bits set and use a negative index instead. */
4082 base |= mask;
4083 index -= mask;
4085 base_reg = force_reg (SImode, GEN_INT (base));
4086 x = plus_constant (base_reg, index);
4089 if (flag_pic)
4091 /* We need to find and carefully transform any SYMBOL and LABEL
4092 references; so go back to the original address expression. */
4093 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4095 if (new_x != orig_x)
4096 x = new_x;
4099 return x;
4103 /* Try machine-dependent ways of modifying an illegitimate Thumb address
4104 to be legitimate. If we find one, return the new, valid address. */
4106 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4108 if (arm_tls_symbol_p (x))
4109 return legitimize_tls_address (x, NULL_RTX);
4111 if (GET_CODE (x) == PLUS
4112 && GET_CODE (XEXP (x, 1)) == CONST_INT
4113 && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4114 || INTVAL (XEXP (x, 1)) < 0))
4116 rtx xop0 = XEXP (x, 0);
4117 rtx xop1 = XEXP (x, 1);
4118 HOST_WIDE_INT offset = INTVAL (xop1);
4120 /* Try and fold the offset into a biasing of the base register and
4121 then offsetting that. Don't do this when optimizing for space
4122 since it can cause too many CSEs. */
4123 if (optimize_size && offset >= 0
4124 && offset < 256 + 31 * GET_MODE_SIZE (mode))
4126 HOST_WIDE_INT delta;
4128 if (offset >= 256)
4129 delta = offset - (256 - GET_MODE_SIZE (mode));
4130 else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4131 delta = 31 * GET_MODE_SIZE (mode);
4132 else
4133 delta = offset & (~31 * GET_MODE_SIZE (mode));
4135 xop0 = force_operand (plus_constant (xop0, offset - delta),
4136 NULL_RTX);
4137 x = plus_constant (xop0, delta);
4139 else if (offset < 0 && offset > -256)
4140 /* Small negative offsets are best done with a subtract before the
4141 dereference, forcing these into a register normally takes two
4142 instructions. */
4143 x = force_operand (x, NULL_RTX);
4144 else
4146 /* For the remaining cases, force the constant into a register. */
4147 xop1 = force_reg (SImode, xop1);
4148 x = gen_rtx_PLUS (SImode, xop0, xop1);
4151 else if (GET_CODE (x) == PLUS
4152 && s_register_operand (XEXP (x, 1), SImode)
4153 && !s_register_operand (XEXP (x, 0), SImode))
4155 rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4157 x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4160 if (flag_pic)
4162 /* We need to find and carefully transform any SYMBOL and LABEL
4163 references; so go back to the original address expression. */
4164 rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4166 if (new_x != orig_x)
4167 x = new_x;
4170 return x;
4174 thumb_legitimize_reload_address (rtx *x_p,
4175 enum machine_mode mode,
4176 int opnum, int type,
4177 int ind_levels ATTRIBUTE_UNUSED)
4179 rtx x = *x_p;
4181 if (GET_CODE (x) == PLUS
4182 && GET_MODE_SIZE (mode) < 4
4183 && REG_P (XEXP (x, 0))
4184 && XEXP (x, 0) == stack_pointer_rtx
4185 && GET_CODE (XEXP (x, 1)) == CONST_INT
4186 && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4188 rtx orig_x = x;
4190 x = copy_rtx (x);
4191 push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4192 Pmode, VOIDmode, 0, 0, opnum, type);
4193 return x;
4196 /* If both registers are hi-regs, then it's better to reload the
4197 entire expression rather than each register individually. That
4198 only requires one reload register rather than two. */
4199 if (GET_CODE (x) == PLUS
4200 && REG_P (XEXP (x, 0))
4201 && REG_P (XEXP (x, 1))
4202 && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4203 && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4205 rtx orig_x = x;
4207 x = copy_rtx (x);
4208 push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4209 Pmode, VOIDmode, 0, 0, opnum, type);
4210 return x;
4213 return NULL;
4216 /* Test for various thread-local symbols. */
4218 /* Return TRUE if X is a thread-local symbol. */
4220 static bool
4221 arm_tls_symbol_p (rtx x)
4223 if (! TARGET_HAVE_TLS)
4224 return false;
4226 if (GET_CODE (x) != SYMBOL_REF)
4227 return false;
4229 return SYMBOL_REF_TLS_MODEL (x) != 0;
4232 /* Helper for arm_tls_referenced_p. */
4234 static int
4235 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
4237 if (GET_CODE (*x) == SYMBOL_REF)
4238 return SYMBOL_REF_TLS_MODEL (*x) != 0;
4240 /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
4241 TLS offsets, not real symbol references. */
4242 if (GET_CODE (*x) == UNSPEC
4243 && XINT (*x, 1) == UNSPEC_TLS)
4244 return -1;
4246 return 0;
4249 /* Return TRUE if X contains any TLS symbol references. */
4251 bool
4252 arm_tls_referenced_p (rtx x)
4254 if (! TARGET_HAVE_TLS)
4255 return false;
4257 return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
4260 #define REG_OR_SUBREG_REG(X) \
4261 (GET_CODE (X) == REG \
4262 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
4264 #define REG_OR_SUBREG_RTX(X) \
4265 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
4267 #ifndef COSTS_N_INSNS
4268 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
4269 #endif
4270 static inline int
4271 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
4273 enum machine_mode mode = GET_MODE (x);
4275 switch (code)
4277 case ASHIFT:
4278 case ASHIFTRT:
4279 case LSHIFTRT:
4280 case ROTATERT:
4281 case PLUS:
4282 case MINUS:
4283 case COMPARE:
4284 case NEG:
4285 case NOT:
4286 return COSTS_N_INSNS (1);
4288 case MULT:
4289 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4291 int cycles = 0;
4292 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
4294 while (i)
4296 i >>= 2;
4297 cycles++;
4299 return COSTS_N_INSNS (2) + cycles;
4301 return COSTS_N_INSNS (1) + 16;
4303 case SET:
4304 return (COSTS_N_INSNS (1)
4305 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
4306 + GET_CODE (SET_DEST (x)) == MEM));
4308 case CONST_INT:
4309 if (outer == SET)
4311 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
4312 return 0;
4313 if (thumb_shiftable_const (INTVAL (x)))
4314 return COSTS_N_INSNS (2);
4315 return COSTS_N_INSNS (3);
4317 else if ((outer == PLUS || outer == COMPARE)
4318 && INTVAL (x) < 256 && INTVAL (x) > -256)
4319 return 0;
4320 else if (outer == AND
4321 && INTVAL (x) < 256 && INTVAL (x) >= -256)
4322 return COSTS_N_INSNS (1);
4323 else if (outer == ASHIFT || outer == ASHIFTRT
4324 || outer == LSHIFTRT)
4325 return 0;
4326 return COSTS_N_INSNS (2);
4328 case CONST:
4329 case CONST_DOUBLE:
4330 case LABEL_REF:
4331 case SYMBOL_REF:
4332 return COSTS_N_INSNS (3);
4334 case UDIV:
4335 case UMOD:
4336 case DIV:
4337 case MOD:
4338 return 100;
4340 case TRUNCATE:
4341 return 99;
4343 case AND:
4344 case XOR:
4345 case IOR:
4346 /* XXX guess. */
4347 return 8;
4349 case MEM:
4350 /* XXX another guess. */
4351 /* Memory costs quite a lot for the first word, but subsequent words
4352 load at the equivalent of a single insn each. */
4353 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4354 + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
4355 ? 4 : 0));
4357 case IF_THEN_ELSE:
4358 /* XXX a guess. */
4359 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4360 return 14;
4361 return 2;
4363 case ZERO_EXTEND:
4364 /* XXX still guessing. */
4365 switch (GET_MODE (XEXP (x, 0)))
4367 case QImode:
4368 return (1 + (mode == DImode ? 4 : 0)
4369 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4371 case HImode:
4372 return (4 + (mode == DImode ? 4 : 0)
4373 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4375 case SImode:
4376 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4378 default:
4379 return 99;
4382 default:
4383 return 99;
4388 /* Worker routine for arm_rtx_costs. */
4389 static inline int
4390 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
4392 enum machine_mode mode = GET_MODE (x);
4393 enum rtx_code subcode;
4394 int extra_cost;
4396 switch (code)
4398 case MEM:
4399 /* Memory costs quite a lot for the first word, but subsequent words
4400 load at the equivalent of a single insn each. */
4401 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4402 + (GET_CODE (x) == SYMBOL_REF
4403 && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
4405 case DIV:
4406 case MOD:
4407 case UDIV:
4408 case UMOD:
4409 return optimize_size ? COSTS_N_INSNS (2) : 100;
4411 case ROTATE:
4412 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4413 return 4;
4414 /* Fall through */
4415 case ROTATERT:
4416 if (mode != SImode)
4417 return 8;
4418 /* Fall through */
4419 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4420 if (mode == DImode)
4421 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
4422 + ((GET_CODE (XEXP (x, 0)) == REG
4423 || (GET_CODE (XEXP (x, 0)) == SUBREG
4424 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4425 ? 0 : 8));
4426 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
4427 || (GET_CODE (XEXP (x, 0)) == SUBREG
4428 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4429 ? 0 : 4)
4430 + ((GET_CODE (XEXP (x, 1)) == REG
4431 || (GET_CODE (XEXP (x, 1)) == SUBREG
4432 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
4433 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
4434 ? 0 : 4));
4436 case MINUS:
4437 if (mode == DImode)
4438 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
4439 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4440 || (GET_CODE (XEXP (x, 0)) == CONST_INT
4441 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
4442 ? 0 : 8));
4444 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4445 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4446 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4447 && arm_const_double_rtx (XEXP (x, 1))))
4448 ? 0 : 8)
4449 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4450 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
4451 && arm_const_double_rtx (XEXP (x, 0))))
4452 ? 0 : 8));
4454 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
4455 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
4456 && REG_OR_SUBREG_REG (XEXP (x, 1))))
4457 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
4458 || subcode == ASHIFTRT || subcode == LSHIFTRT
4459 || subcode == ROTATE || subcode == ROTATERT
4460 || (subcode == MULT
4461 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4462 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
4463 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4464 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4465 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4466 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4467 && REG_OR_SUBREG_REG (XEXP (x, 0))))
4468 return 1;
4469 /* Fall through */
4471 case PLUS:
4472 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4473 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4474 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4475 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4476 && arm_const_double_rtx (XEXP (x, 1))))
4477 ? 0 : 8));
4479 /* Fall through */
4480 case AND: case XOR: case IOR:
4481 extra_cost = 0;
4483 /* Normally the frame registers will be spilt into reg+const during
4484 reload, so it is a bad idea to combine them with other instructions,
4485 since then they might not be moved outside of loops. As a compromise
4486 we allow integration with ops that have a constant as their second
4487 operand. */
4488 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
4489 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
4490 && GET_CODE (XEXP (x, 1)) != CONST_INT)
4491 || (REG_OR_SUBREG_REG (XEXP (x, 0))
4492 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
4493 extra_cost = 4;
4495 if (mode == DImode)
4496 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4497 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4498 || (GET_CODE (XEXP (x, 1)) == CONST_INT
4499 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4500 ? 0 : 8));
4502 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
4503 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
4504 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4505 || (GET_CODE (XEXP (x, 1)) == CONST_INT
4506 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4507 ? 0 : 4));
4509 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
4510 return (1 + extra_cost
4511 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
4512 || subcode == LSHIFTRT || subcode == ASHIFTRT
4513 || subcode == ROTATE || subcode == ROTATERT
4514 || (subcode == MULT
4515 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4516 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
4517 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
4518 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
4519 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
4520 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4521 ? 0 : 4));
4523 return 8;
4525 case MULT:
4526 /* This should have been handled by the CPU specific routines. */
4527 gcc_unreachable ();
4529 case TRUNCATE:
4530 if (arm_arch3m && mode == SImode
4531 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4532 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4533 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
4534 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4535 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4536 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
4537 return 8;
4538 return 99;
4540 case NEG:
4541 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4542 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4543 /* Fall through */
4544 case NOT:
4545 if (mode == DImode)
4546 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4548 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4550 case IF_THEN_ELSE:
4551 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4552 return 14;
4553 return 2;
4555 case COMPARE:
4556 return 1;
4558 case ABS:
4559 return 4 + (mode == DImode ? 4 : 0);
4561 case SIGN_EXTEND:
4562 if (GET_MODE (XEXP (x, 0)) == QImode)
4563 return (4 + (mode == DImode ? 4 : 0)
4564 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4565 /* Fall through */
4566 case ZERO_EXTEND:
4567 switch (GET_MODE (XEXP (x, 0)))
4569 case QImode:
4570 return (1 + (mode == DImode ? 4 : 0)
4571 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4573 case HImode:
4574 return (4 + (mode == DImode ? 4 : 0)
4575 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4577 case SImode:
4578 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4580 case V8QImode:
4581 case V4HImode:
4582 case V2SImode:
4583 case V4QImode:
4584 case V2HImode:
4585 return 1;
4587 default:
4588 gcc_unreachable ();
4590 gcc_unreachable ();
4592 case CONST_INT:
4593 if (const_ok_for_arm (INTVAL (x)))
4594 return outer == SET ? 2 : -1;
4595 else if (outer == AND
4596 && const_ok_for_arm (~INTVAL (x)))
4597 return -1;
4598 else if ((outer == COMPARE
4599 || outer == PLUS || outer == MINUS)
4600 && const_ok_for_arm (-INTVAL (x)))
4601 return -1;
4602 else
4603 return 5;
4605 case CONST:
4606 case LABEL_REF:
4607 case SYMBOL_REF:
4608 return 6;
4610 case CONST_DOUBLE:
4611 if (arm_const_double_rtx (x))
4612 return outer == SET ? 2 : -1;
4613 else if ((outer == COMPARE || outer == PLUS)
4614 && neg_const_double_rtx_ok_for_fpa (x))
4615 return -1;
4616 return 7;
4618 default:
4619 return 99;
4623 /* RTX costs when optimizing for size. */
4624 static bool
4625 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4627 enum machine_mode mode = GET_MODE (x);
4629 if (TARGET_THUMB)
4631 /* XXX TBD. For now, use the standard costs. */
4632 *total = thumb_rtx_costs (x, code, outer_code);
4633 return true;
4636 switch (code)
4638 case MEM:
4639 /* A memory access costs 1 insn if the mode is small, or the address is
4640 a single register, otherwise it costs one insn per word. */
4641 if (REG_P (XEXP (x, 0)))
4642 *total = COSTS_N_INSNS (1);
4643 else
4644 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4645 return true;
4647 case DIV:
4648 case MOD:
4649 case UDIV:
4650 case UMOD:
4651 /* Needs a libcall, so it costs about this. */
4652 *total = COSTS_N_INSNS (2);
4653 return false;
4655 case ROTATE:
4656 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4658 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4659 return true;
4661 /* Fall through */
4662 case ROTATERT:
4663 case ASHIFT:
4664 case LSHIFTRT:
4665 case ASHIFTRT:
4666 if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4668 *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4669 return true;
4671 else if (mode == SImode)
4673 *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4674 /* Slightly disparage register shifts, but not by much. */
4675 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4676 *total += 1 + rtx_cost (XEXP (x, 1), code);
4677 return true;
4680 /* Needs a libcall. */
4681 *total = COSTS_N_INSNS (2);
4682 return false;
4684 case MINUS:
4685 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4687 *total = COSTS_N_INSNS (1);
4688 return false;
4691 if (mode == SImode)
4693 enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4694 enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4696 if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4697 || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4698 || subcode1 == ROTATE || subcode1 == ROTATERT
4699 || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4700 || subcode1 == ASHIFTRT)
4702 /* It's just the cost of the two operands. */
4703 *total = 0;
4704 return false;
4707 *total = COSTS_N_INSNS (1);
4708 return false;
4711 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4712 return false;
4714 case PLUS:
4715 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4717 *total = COSTS_N_INSNS (1);
4718 return false;
4721 /* Fall through */
4722 case AND: case XOR: case IOR:
4723 if (mode == SImode)
4725 enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4727 if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4728 || subcode == LSHIFTRT || subcode == ASHIFTRT
4729 || (code == AND && subcode == NOT))
4731 /* It's just the cost of the two operands. */
4732 *total = 0;
4733 return false;
4737 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4738 return false;
4740 case MULT:
4741 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4742 return false;
4744 case NEG:
4745 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4746 *total = COSTS_N_INSNS (1);
4747 /* Fall through */
4748 case NOT:
4749 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4751 return false;
4753 case IF_THEN_ELSE:
4754 *total = 0;
4755 return false;
4757 case COMPARE:
4758 if (cc_register (XEXP (x, 0), VOIDmode))
4759 * total = 0;
4760 else
4761 *total = COSTS_N_INSNS (1);
4762 return false;
4764 case ABS:
4765 if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4766 *total = COSTS_N_INSNS (1);
4767 else
4768 *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4769 return false;
4771 case SIGN_EXTEND:
4772 *total = 0;
4773 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4775 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4776 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4778 if (mode == DImode)
4779 *total += COSTS_N_INSNS (1);
4780 return false;
4782 case ZERO_EXTEND:
4783 *total = 0;
4784 if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4786 switch (GET_MODE (XEXP (x, 0)))
4788 case QImode:
4789 *total += COSTS_N_INSNS (1);
4790 break;
4792 case HImode:
4793 *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4795 case SImode:
4796 break;
4798 default:
4799 *total += COSTS_N_INSNS (2);
4803 if (mode == DImode)
4804 *total += COSTS_N_INSNS (1);
4806 return false;
4808 case CONST_INT:
4809 if (const_ok_for_arm (INTVAL (x)))
4810 *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4811 else if (const_ok_for_arm (~INTVAL (x)))
4812 *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4813 else if (const_ok_for_arm (-INTVAL (x)))
4815 if (outer_code == COMPARE || outer_code == PLUS
4816 || outer_code == MINUS)
4817 *total = 0;
4818 else
4819 *total = COSTS_N_INSNS (1);
4821 else
4822 *total = COSTS_N_INSNS (2);
4823 return true;
4825 case CONST:
4826 case LABEL_REF:
4827 case SYMBOL_REF:
4828 *total = COSTS_N_INSNS (2);
4829 return true;
4831 case CONST_DOUBLE:
4832 *total = COSTS_N_INSNS (4);
4833 return true;
4835 default:
4836 if (mode != VOIDmode)
4837 *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4838 else
4839 *total = COSTS_N_INSNS (4); /* How knows? */
4840 return false;
4844 /* RTX costs for cores with a slow MUL implementation. */
4846 static bool
4847 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4849 enum machine_mode mode = GET_MODE (x);
4851 if (TARGET_THUMB)
4853 *total = thumb_rtx_costs (x, code, outer_code);
4854 return true;
4857 switch (code)
4859 case MULT:
4860 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4861 || mode == DImode)
4863 *total = 30;
4864 return true;
4867 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4869 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4870 & (unsigned HOST_WIDE_INT) 0xffffffff);
4871 int cost, const_ok = const_ok_for_arm (i);
4872 int j, booth_unit_size;
4874 /* Tune as appropriate. */
4875 cost = const_ok ? 4 : 8;
4876 booth_unit_size = 2;
4877 for (j = 0; i && j < 32; j += booth_unit_size)
4879 i >>= booth_unit_size;
4880 cost += 2;
4883 *total = cost;
4884 return true;
4887 *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4888 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4889 return true;
4891 default:
4892 *total = arm_rtx_costs_1 (x, code, outer_code);
4893 return true;
4898 /* RTX cost for cores with a fast multiply unit (M variants). */
4900 static bool
4901 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4903 enum machine_mode mode = GET_MODE (x);
4905 if (TARGET_THUMB)
4907 *total = thumb_rtx_costs (x, code, outer_code);
4908 return true;
4911 switch (code)
4913 case MULT:
4914 /* There is no point basing this on the tuning, since it is always the
4915 fast variant if it exists at all. */
4916 if (mode == DImode
4917 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4918 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4919 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4921 *total = 8;
4922 return true;
4926 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4927 || mode == DImode)
4929 *total = 30;
4930 return true;
4933 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4935 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4936 & (unsigned HOST_WIDE_INT) 0xffffffff);
4937 int cost, const_ok = const_ok_for_arm (i);
4938 int j, booth_unit_size;
4940 /* Tune as appropriate. */
4941 cost = const_ok ? 4 : 8;
4942 booth_unit_size = 8;
4943 for (j = 0; i && j < 32; j += booth_unit_size)
4945 i >>= booth_unit_size;
4946 cost += 2;
4949 *total = cost;
4950 return true;
4953 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4954 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4955 return true;
4957 default:
4958 *total = arm_rtx_costs_1 (x, code, outer_code);
4959 return true;
4964 /* RTX cost for XScale CPUs. */
4966 static bool
4967 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4969 enum machine_mode mode = GET_MODE (x);
4971 if (TARGET_THUMB)
4973 *total = thumb_rtx_costs (x, code, outer_code);
4974 return true;
4977 switch (code)
4979 case MULT:
4980 /* There is no point basing this on the tuning, since it is always the
4981 fast variant if it exists at all. */
4982 if (mode == DImode
4983 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4984 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4985 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4987 *total = 8;
4988 return true;
4992 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4993 || mode == DImode)
4995 *total = 30;
4996 return true;
4999 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5001 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5002 & (unsigned HOST_WIDE_INT) 0xffffffff);
5003 int cost, const_ok = const_ok_for_arm (i);
5004 unsigned HOST_WIDE_INT masked_const;
5006 /* The cost will be related to two insns.
5007 First a load of the constant (MOV or LDR), then a multiply. */
5008 cost = 2;
5009 if (! const_ok)
5010 cost += 1; /* LDR is probably more expensive because
5011 of longer result latency. */
5012 masked_const = i & 0xffff8000;
5013 if (masked_const != 0 && masked_const != 0xffff8000)
5015 masked_const = i & 0xf8000000;
5016 if (masked_const == 0 || masked_const == 0xf8000000)
5017 cost += 1;
5018 else
5019 cost += 2;
5021 *total = cost;
5022 return true;
5025 *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5026 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5027 return true;
5029 case COMPARE:
5030 /* A COMPARE of a MULT is slow on XScale; the muls instruction
5031 will stall until the multiplication is complete. */
5032 if (GET_CODE (XEXP (x, 0)) == MULT)
5033 *total = 4 + rtx_cost (XEXP (x, 0), code);
5034 else
5035 *total = arm_rtx_costs_1 (x, code, outer_code);
5036 return true;
5038 default:
5039 *total = arm_rtx_costs_1 (x, code, outer_code);
5040 return true;
5045 /* RTX costs for 9e (and later) cores. */
5047 static bool
5048 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
5050 enum machine_mode mode = GET_MODE (x);
5051 int nonreg_cost;
5052 int cost;
5054 if (TARGET_THUMB)
5056 switch (code)
5058 case MULT:
5059 *total = COSTS_N_INSNS (3);
5060 return true;
5062 default:
5063 *total = thumb_rtx_costs (x, code, outer_code);
5064 return true;
5068 switch (code)
5070 case MULT:
5071 /* There is no point basing this on the tuning, since it is always the
5072 fast variant if it exists at all. */
5073 if (mode == DImode
5074 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5075 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5076 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5078 *total = 3;
5079 return true;
5083 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5085 *total = 30;
5086 return true;
5088 if (mode == DImode)
5090 cost = 7;
5091 nonreg_cost = 8;
5093 else
5095 cost = 2;
5096 nonreg_cost = 4;
5100 *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
5101 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
5102 return true;
5104 default:
5105 *total = arm_rtx_costs_1 (x, code, outer_code);
5106 return true;
5109 /* All address computations that can be done are free, but rtx cost returns
5110 the same for practically all of them. So we weight the different types
5111 of address here in the order (most pref first):
5112 PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL. */
5113 static inline int
5114 arm_arm_address_cost (rtx x)
5116 enum rtx_code c = GET_CODE (x);
5118 if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
5119 return 0;
5120 if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
5121 return 10;
5123 if (c == PLUS || c == MINUS)
5125 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5126 return 2;
5128 if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
5129 return 3;
5131 return 4;
5134 return 6;
5137 static inline int
5138 arm_thumb_address_cost (rtx x)
5140 enum rtx_code c = GET_CODE (x);
5142 if (c == REG)
5143 return 1;
5144 if (c == PLUS
5145 && GET_CODE (XEXP (x, 0)) == REG
5146 && GET_CODE (XEXP (x, 1)) == CONST_INT)
5147 return 1;
5149 return 2;
5152 static int
5153 arm_address_cost (rtx x)
5155 return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
5158 static int
5159 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
5161 rtx i_pat, d_pat;
5163 /* Some true dependencies can have a higher cost depending
5164 on precisely how certain input operands are used. */
5165 if (arm_tune_xscale
5166 && REG_NOTE_KIND (link) == 0
5167 && recog_memoized (insn) >= 0
5168 && recog_memoized (dep) >= 0)
5170 int shift_opnum = get_attr_shift (insn);
5171 enum attr_type attr_type = get_attr_type (dep);
5173 /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
5174 operand for INSN. If we have a shifted input operand and the
5175 instruction we depend on is another ALU instruction, then we may
5176 have to account for an additional stall. */
5177 if (shift_opnum != 0
5178 && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
5180 rtx shifted_operand;
5181 int opno;
5183 /* Get the shifted operand. */
5184 extract_insn (insn);
5185 shifted_operand = recog_data.operand[shift_opnum];
5187 /* Iterate over all the operands in DEP. If we write an operand
5188 that overlaps with SHIFTED_OPERAND, then we have increase the
5189 cost of this dependency. */
5190 extract_insn (dep);
5191 preprocess_constraints ();
5192 for (opno = 0; opno < recog_data.n_operands; opno++)
5194 /* We can ignore strict inputs. */
5195 if (recog_data.operand_type[opno] == OP_IN)
5196 continue;
5198 if (reg_overlap_mentioned_p (recog_data.operand[opno],
5199 shifted_operand))
5200 return 2;
5205 /* XXX This is not strictly true for the FPA. */
5206 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
5207 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
5208 return 0;
5210 /* Call insns don't incur a stall, even if they follow a load. */
5211 if (REG_NOTE_KIND (link) == 0
5212 && GET_CODE (insn) == CALL_INSN)
5213 return 1;
5215 if ((i_pat = single_set (insn)) != NULL
5216 && GET_CODE (SET_SRC (i_pat)) == MEM
5217 && (d_pat = single_set (dep)) != NULL
5218 && GET_CODE (SET_DEST (d_pat)) == MEM)
5220 rtx src_mem = XEXP (SET_SRC (i_pat), 0);
5221 /* This is a load after a store, there is no conflict if the load reads
5222 from a cached area. Assume that loads from the stack, and from the
5223 constant pool are cached, and that others will miss. This is a
5224 hack. */
5226 if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
5227 || reg_mentioned_p (stack_pointer_rtx, src_mem)
5228 || reg_mentioned_p (frame_pointer_rtx, src_mem)
5229 || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
5230 return 1;
5233 return cost;
5236 static int fp_consts_inited = 0;
5238 /* Only zero is valid for VFP. Other values are also valid for FPA. */
5239 static const char * const strings_fp[8] =
5241 "0", "1", "2", "3",
5242 "4", "5", "0.5", "10"
5245 static REAL_VALUE_TYPE values_fp[8];
5247 static void
5248 init_fp_table (void)
5250 int i;
5251 REAL_VALUE_TYPE r;
5253 if (TARGET_VFP)
5254 fp_consts_inited = 1;
5255 else
5256 fp_consts_inited = 8;
5258 for (i = 0; i < fp_consts_inited; i++)
5260 r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
5261 values_fp[i] = r;
5265 /* Return TRUE if rtx X is a valid immediate FP constant. */
5267 arm_const_double_rtx (rtx x)
5269 REAL_VALUE_TYPE r;
5270 int i;
5272 if (!fp_consts_inited)
5273 init_fp_table ();
5275 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5276 if (REAL_VALUE_MINUS_ZERO (r))
5277 return 0;
5279 for (i = 0; i < fp_consts_inited; i++)
5280 if (REAL_VALUES_EQUAL (r, values_fp[i]))
5281 return 1;
5283 return 0;
5286 /* Return TRUE if rtx X is a valid immediate FPA constant. */
5288 neg_const_double_rtx_ok_for_fpa (rtx x)
5290 REAL_VALUE_TYPE r;
5291 int i;
5293 if (!fp_consts_inited)
5294 init_fp_table ();
5296 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5297 r = REAL_VALUE_NEGATE (r);
5298 if (REAL_VALUE_MINUS_ZERO (r))
5299 return 0;
5301 for (i = 0; i < 8; i++)
5302 if (REAL_VALUES_EQUAL (r, values_fp[i]))
5303 return 1;
5305 return 0;
5308 /* Predicates for `match_operand' and `match_operator'. */
5310 /* Return nonzero if OP is a valid Cirrus memory address pattern. */
5312 cirrus_memory_offset (rtx op)
5314 /* Reject eliminable registers. */
5315 if (! (reload_in_progress || reload_completed)
5316 && ( reg_mentioned_p (frame_pointer_rtx, op)
5317 || reg_mentioned_p (arg_pointer_rtx, op)
5318 || reg_mentioned_p (virtual_incoming_args_rtx, op)
5319 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5320 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5321 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5322 return 0;
5324 if (GET_CODE (op) == MEM)
5326 rtx ind;
5328 ind = XEXP (op, 0);
5330 /* Match: (mem (reg)). */
5331 if (GET_CODE (ind) == REG)
5332 return 1;
5334 /* Match:
5335 (mem (plus (reg)
5336 (const))). */
5337 if (GET_CODE (ind) == PLUS
5338 && GET_CODE (XEXP (ind, 0)) == REG
5339 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5340 && GET_CODE (XEXP (ind, 1)) == CONST_INT)
5341 return 1;
5344 return 0;
5347 /* Return TRUE if OP is a valid coprocessor memory address pattern.
5348 WB if true if writeback address modes are allowed. */
5351 arm_coproc_mem_operand (rtx op, bool wb)
5353 rtx ind;
5355 /* Reject eliminable registers. */
5356 if (! (reload_in_progress || reload_completed)
5357 && ( reg_mentioned_p (frame_pointer_rtx, op)
5358 || reg_mentioned_p (arg_pointer_rtx, op)
5359 || reg_mentioned_p (virtual_incoming_args_rtx, op)
5360 || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5361 || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5362 || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5363 return FALSE;
5365 /* Constants are converted into offsets from labels. */
5366 if (GET_CODE (op) != MEM)
5367 return FALSE;
5369 ind = XEXP (op, 0);
5371 if (reload_completed
5372 && (GET_CODE (ind) == LABEL_REF
5373 || (GET_CODE (ind) == CONST
5374 && GET_CODE (XEXP (ind, 0)) == PLUS
5375 && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
5376 && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
5377 return TRUE;
5379 /* Match: (mem (reg)). */
5380 if (GET_CODE (ind) == REG)
5381 return arm_address_register_rtx_p (ind, 0);
5383 /* Autoincremment addressing modes. */
5384 if (wb
5385 && (GET_CODE (ind) == PRE_INC
5386 || GET_CODE (ind) == POST_INC
5387 || GET_CODE (ind) == PRE_DEC
5388 || GET_CODE (ind) == POST_DEC))
5389 return arm_address_register_rtx_p (XEXP (ind, 0), 0);
5391 if (wb
5392 && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
5393 && arm_address_register_rtx_p (XEXP (ind, 0), 0)
5394 && GET_CODE (XEXP (ind, 1)) == PLUS
5395 && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
5396 ind = XEXP (ind, 1);
5398 /* Match:
5399 (plus (reg)
5400 (const)). */
5401 if (GET_CODE (ind) == PLUS
5402 && GET_CODE (XEXP (ind, 0)) == REG
5403 && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5404 && GET_CODE (XEXP (ind, 1)) == CONST_INT
5405 && INTVAL (XEXP (ind, 1)) > -1024
5406 && INTVAL (XEXP (ind, 1)) < 1024
5407 && (INTVAL (XEXP (ind, 1)) & 3) == 0)
5408 return TRUE;
5410 return FALSE;
5413 /* Return true if X is a register that will be eliminated later on. */
5415 arm_eliminable_register (rtx x)
5417 return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
5418 || REGNO (x) == ARG_POINTER_REGNUM
5419 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
5420 && REGNO (x) <= LAST_VIRTUAL_REGISTER));
5423 /* Return GENERAL_REGS if a scratch register required to reload x to/from
5424 VFP registers. Otherwise return NO_REGS. */
5426 enum reg_class
5427 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
5429 if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
5430 return NO_REGS;
5432 return GENERAL_REGS;
5435 /* Values which must be returned in the most-significant end of the return
5436 register. */
5438 static bool
5439 arm_return_in_msb (tree valtype)
5441 return (TARGET_AAPCS_BASED
5442 && BYTES_BIG_ENDIAN
5443 && (AGGREGATE_TYPE_P (valtype)
5444 || TREE_CODE (valtype) == COMPLEX_TYPE));
5447 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
5448 Use by the Cirrus Maverick code which has to workaround
5449 a hardware bug triggered by such instructions. */
5450 static bool
5451 arm_memory_load_p (rtx insn)
5453 rtx body, lhs, rhs;;
5455 if (insn == NULL_RTX || GET_CODE (insn) != INSN)
5456 return false;
5458 body = PATTERN (insn);
5460 if (GET_CODE (body) != SET)
5461 return false;
5463 lhs = XEXP (body, 0);
5464 rhs = XEXP (body, 1);
5466 lhs = REG_OR_SUBREG_RTX (lhs);
5468 /* If the destination is not a general purpose
5469 register we do not have to worry. */
5470 if (GET_CODE (lhs) != REG
5471 || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
5472 return false;
5474 /* As well as loads from memory we also have to react
5475 to loads of invalid constants which will be turned
5476 into loads from the minipool. */
5477 return (GET_CODE (rhs) == MEM
5478 || GET_CODE (rhs) == SYMBOL_REF
5479 || note_invalid_constants (insn, -1, false));
5482 /* Return TRUE if INSN is a Cirrus instruction. */
5483 static bool
5484 arm_cirrus_insn_p (rtx insn)
5486 enum attr_cirrus attr;
5488 /* get_attr cannot accept USE or CLOBBER. */
5489 if (!insn
5490 || GET_CODE (insn) != INSN
5491 || GET_CODE (PATTERN (insn)) == USE
5492 || GET_CODE (PATTERN (insn)) == CLOBBER)
5493 return 0;
5495 attr = get_attr_cirrus (insn);
5497 return attr != CIRRUS_NOT;
5500 /* Cirrus reorg for invalid instruction combinations. */
5501 static void
5502 cirrus_reorg (rtx first)
5504 enum attr_cirrus attr;
5505 rtx body = PATTERN (first);
5506 rtx t;
5507 int nops;
5509 /* Any branch must be followed by 2 non Cirrus instructions. */
5510 if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
5512 nops = 0;
5513 t = next_nonnote_insn (first);
5515 if (arm_cirrus_insn_p (t))
5516 ++ nops;
5518 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5519 ++ nops;
5521 while (nops --)
5522 emit_insn_after (gen_nop (), first);
5524 return;
5527 /* (float (blah)) is in parallel with a clobber. */
5528 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
5529 body = XVECEXP (body, 0, 0);
5531 if (GET_CODE (body) == SET)
5533 rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
5535 /* cfldrd, cfldr64, cfstrd, cfstr64 must
5536 be followed by a non Cirrus insn. */
5537 if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
5539 if (arm_cirrus_insn_p (next_nonnote_insn (first)))
5540 emit_insn_after (gen_nop (), first);
5542 return;
5544 else if (arm_memory_load_p (first))
5546 unsigned int arm_regno;
5548 /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
5549 ldr/cfmv64hr combination where the Rd field is the same
5550 in both instructions must be split with a non Cirrus
5551 insn. Example:
5553 ldr r0, blah
5555 cfmvsr mvf0, r0. */
5557 /* Get Arm register number for ldr insn. */
5558 if (GET_CODE (lhs) == REG)
5559 arm_regno = REGNO (lhs);
5560 else
5562 gcc_assert (GET_CODE (rhs) == REG);
5563 arm_regno = REGNO (rhs);
5566 /* Next insn. */
5567 first = next_nonnote_insn (first);
5569 if (! arm_cirrus_insn_p (first))
5570 return;
5572 body = PATTERN (first);
5574 /* (float (blah)) is in parallel with a clobber. */
5575 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5576 body = XVECEXP (body, 0, 0);
5578 if (GET_CODE (body) == FLOAT)
5579 body = XEXP (body, 0);
5581 if (get_attr_cirrus (first) == CIRRUS_MOVE
5582 && GET_CODE (XEXP (body, 1)) == REG
5583 && arm_regno == REGNO (XEXP (body, 1)))
5584 emit_insn_after (gen_nop (), first);
5586 return;
5590 /* get_attr cannot accept USE or CLOBBER. */
5591 if (!first
5592 || GET_CODE (first) != INSN
5593 || GET_CODE (PATTERN (first)) == USE
5594 || GET_CODE (PATTERN (first)) == CLOBBER)
5595 return;
5597 attr = get_attr_cirrus (first);
5599 /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5600 must be followed by a non-coprocessor instruction. */
5601 if (attr == CIRRUS_COMPARE)
5603 nops = 0;
5605 t = next_nonnote_insn (first);
5607 if (arm_cirrus_insn_p (t))
5608 ++ nops;
5610 if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5611 ++ nops;
5613 while (nops --)
5614 emit_insn_after (gen_nop (), first);
5616 return;
5620 /* Return TRUE if X references a SYMBOL_REF. */
5622 symbol_mentioned_p (rtx x)
5624 const char * fmt;
5625 int i;
5627 if (GET_CODE (x) == SYMBOL_REF)
5628 return 1;
5630 /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
5631 are constant offsets, not symbols. */
5632 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
5633 return 0;
5635 fmt = GET_RTX_FORMAT (GET_CODE (x));
5637 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5639 if (fmt[i] == 'E')
5641 int j;
5643 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5644 if (symbol_mentioned_p (XVECEXP (x, i, j)))
5645 return 1;
5647 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5648 return 1;
5651 return 0;
5654 /* Return TRUE if X references a LABEL_REF. */
5656 label_mentioned_p (rtx x)
5658 const char * fmt;
5659 int i;
5661 if (GET_CODE (x) == LABEL_REF)
5662 return 1;
5664 /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
5665 instruction, but they are constant offsets, not symbols. */
5666 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
5667 return 0;
5669 fmt = GET_RTX_FORMAT (GET_CODE (x));
5670 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5672 if (fmt[i] == 'E')
5674 int j;
5676 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5677 if (label_mentioned_p (XVECEXP (x, i, j)))
5678 return 1;
5680 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5681 return 1;
5684 return 0;
5688 tls_mentioned_p (rtx x)
5690 switch (GET_CODE (x))
5692 case CONST:
5693 return tls_mentioned_p (XEXP (x, 0));
5695 case UNSPEC:
5696 if (XINT (x, 1) == UNSPEC_TLS)
5697 return 1;
5699 default:
5700 return 0;
5704 /* Must not copy a SET whose source operand is PC-relative. */
5706 static bool
5707 arm_cannot_copy_insn_p (rtx insn)
5709 rtx pat = PATTERN (insn);
5711 if (GET_CODE (pat) == PARALLEL
5712 && GET_CODE (XVECEXP (pat, 0, 0)) == SET)
5714 rtx rhs = SET_SRC (XVECEXP (pat, 0, 0));
5716 if (GET_CODE (rhs) == UNSPEC
5717 && XINT (rhs, 1) == UNSPEC_PIC_BASE)
5718 return TRUE;
5720 if (GET_CODE (rhs) == MEM
5721 && GET_CODE (XEXP (rhs, 0)) == UNSPEC
5722 && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
5723 return TRUE;
5726 return FALSE;
5729 enum rtx_code
5730 minmax_code (rtx x)
5732 enum rtx_code code = GET_CODE (x);
5734 switch (code)
5736 case SMAX:
5737 return GE;
5738 case SMIN:
5739 return LE;
5740 case UMIN:
5741 return LEU;
5742 case UMAX:
5743 return GEU;
5744 default:
5745 gcc_unreachable ();
5749 /* Return 1 if memory locations are adjacent. */
5751 adjacent_mem_locations (rtx a, rtx b)
5753 /* We don't guarantee to preserve the order of these memory refs. */
5754 if (volatile_refs_p (a) || volatile_refs_p (b))
5755 return 0;
5757 if ((GET_CODE (XEXP (a, 0)) == REG
5758 || (GET_CODE (XEXP (a, 0)) == PLUS
5759 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5760 && (GET_CODE (XEXP (b, 0)) == REG
5761 || (GET_CODE (XEXP (b, 0)) == PLUS
5762 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5764 HOST_WIDE_INT val0 = 0, val1 = 0;
5765 rtx reg0, reg1;
5766 int val_diff;
5768 if (GET_CODE (XEXP (a, 0)) == PLUS)
5770 reg0 = XEXP (XEXP (a, 0), 0);
5771 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5773 else
5774 reg0 = XEXP (a, 0);
5776 if (GET_CODE (XEXP (b, 0)) == PLUS)
5778 reg1 = XEXP (XEXP (b, 0), 0);
5779 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5781 else
5782 reg1 = XEXP (b, 0);
5784 /* Don't accept any offset that will require multiple
5785 instructions to handle, since this would cause the
5786 arith_adjacentmem pattern to output an overlong sequence. */
5787 if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5788 return 0;
5790 /* Don't allow an eliminable register: register elimination can make
5791 the offset too large. */
5792 if (arm_eliminable_register (reg0))
5793 return 0;
5795 val_diff = val1 - val0;
5797 if (arm_ld_sched)
5799 /* If the target has load delay slots, then there's no benefit
5800 to using an ldm instruction unless the offset is zero and
5801 we are optimizing for size. */
5802 return (optimize_size && (REGNO (reg0) == REGNO (reg1))
5803 && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
5804 && (val_diff == 4 || val_diff == -4));
5807 return ((REGNO (reg0) == REGNO (reg1))
5808 && (val_diff == 4 || val_diff == -4));
5811 return 0;
5815 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5816 HOST_WIDE_INT *load_offset)
5818 int unsorted_regs[4];
5819 HOST_WIDE_INT unsorted_offsets[4];
5820 int order[4];
5821 int base_reg = -1;
5822 int i;
5824 /* Can only handle 2, 3, or 4 insns at present,
5825 though could be easily extended if required. */
5826 gcc_assert (nops >= 2 && nops <= 4);
5828 /* Loop over the operands and check that the memory references are
5829 suitable (i.e. immediate offsets from the same base register). At
5830 the same time, extract the target register, and the memory
5831 offsets. */
5832 for (i = 0; i < nops; i++)
5834 rtx reg;
5835 rtx offset;
5837 /* Convert a subreg of a mem into the mem itself. */
5838 if (GET_CODE (operands[nops + i]) == SUBREG)
5839 operands[nops + i] = alter_subreg (operands + (nops + i));
5841 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
5843 /* Don't reorder volatile memory references; it doesn't seem worth
5844 looking for the case where the order is ok anyway. */
5845 if (MEM_VOLATILE_P (operands[nops + i]))
5846 return 0;
5848 offset = const0_rtx;
5850 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5851 || (GET_CODE (reg) == SUBREG
5852 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5853 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5854 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5855 == REG)
5856 || (GET_CODE (reg) == SUBREG
5857 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5858 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5859 == CONST_INT)))
5861 if (i == 0)
5863 base_reg = REGNO (reg);
5864 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5865 ? REGNO (operands[i])
5866 : REGNO (SUBREG_REG (operands[i])));
5867 order[0] = 0;
5869 else
5871 if (base_reg != (int) REGNO (reg))
5872 /* Not addressed from the same base register. */
5873 return 0;
5875 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5876 ? REGNO (operands[i])
5877 : REGNO (SUBREG_REG (operands[i])));
5878 if (unsorted_regs[i] < unsorted_regs[order[0]])
5879 order[0] = i;
5882 /* If it isn't an integer register, or if it overwrites the
5883 base register but isn't the last insn in the list, then
5884 we can't do this. */
5885 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5886 || (i != nops - 1 && unsorted_regs[i] == base_reg))
5887 return 0;
5889 unsorted_offsets[i] = INTVAL (offset);
5891 else
5892 /* Not a suitable memory address. */
5893 return 0;
5896 /* All the useful information has now been extracted from the
5897 operands into unsorted_regs and unsorted_offsets; additionally,
5898 order[0] has been set to the lowest numbered register in the
5899 list. Sort the registers into order, and check that the memory
5900 offsets are ascending and adjacent. */
5902 for (i = 1; i < nops; i++)
5904 int j;
5906 order[i] = order[i - 1];
5907 for (j = 0; j < nops; j++)
5908 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5909 && (order[i] == order[i - 1]
5910 || unsorted_regs[j] < unsorted_regs[order[i]]))
5911 order[i] = j;
5913 /* Have we found a suitable register? if not, one must be used more
5914 than once. */
5915 if (order[i] == order[i - 1])
5916 return 0;
5918 /* Is the memory address adjacent and ascending? */
5919 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5920 return 0;
5923 if (base)
5925 *base = base_reg;
5927 for (i = 0; i < nops; i++)
5928 regs[i] = unsorted_regs[order[i]];
5930 *load_offset = unsorted_offsets[order[0]];
5933 if (unsorted_offsets[order[0]] == 0)
5934 return 1; /* ldmia */
5936 if (unsorted_offsets[order[0]] == 4)
5937 return 2; /* ldmib */
5939 if (unsorted_offsets[order[nops - 1]] == 0)
5940 return 3; /* ldmda */
5942 if (unsorted_offsets[order[nops - 1]] == -4)
5943 return 4; /* ldmdb */
5945 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5946 if the offset isn't small enough. The reason 2 ldrs are faster
5947 is because these ARMs are able to do more than one cache access
5948 in a single cycle. The ARM9 and StrongARM have Harvard caches,
5949 whilst the ARM8 has a double bandwidth cache. This means that
5950 these cores can do both an instruction fetch and a data fetch in
5951 a single cycle, so the trick of calculating the address into a
5952 scratch register (one of the result regs) and then doing a load
5953 multiple actually becomes slower (and no smaller in code size).
5954 That is the transformation
5956 ldr rd1, [rbase + offset]
5957 ldr rd2, [rbase + offset + 4]
5961 add rd1, rbase, offset
5962 ldmia rd1, {rd1, rd2}
5964 produces worse code -- '3 cycles + any stalls on rd2' instead of
5965 '2 cycles + any stalls on rd2'. On ARMs with only one cache
5966 access per cycle, the first sequence could never complete in less
5967 than 6 cycles, whereas the ldm sequence would only take 5 and
5968 would make better use of sequential accesses if not hitting the
5969 cache.
5971 We cheat here and test 'arm_ld_sched' which we currently know to
5972 only be true for the ARM8, ARM9 and StrongARM. If this ever
5973 changes, then the test below needs to be reworked. */
5974 if (nops == 2 && arm_ld_sched)
5975 return 0;
5977 /* Can't do it without setting up the offset, only do this if it takes
5978 no more than one insn. */
5979 return (const_ok_for_arm (unsorted_offsets[order[0]])
5980 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5983 const char *
5984 emit_ldm_seq (rtx *operands, int nops)
5986 int regs[4];
5987 int base_reg;
5988 HOST_WIDE_INT offset;
5989 char buf[100];
5990 int i;
5992 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5994 case 1:
5995 strcpy (buf, "ldm%?ia\t");
5996 break;
5998 case 2:
5999 strcpy (buf, "ldm%?ib\t");
6000 break;
6002 case 3:
6003 strcpy (buf, "ldm%?da\t");
6004 break;
6006 case 4:
6007 strcpy (buf, "ldm%?db\t");
6008 break;
6010 case 5:
6011 if (offset >= 0)
6012 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6013 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6014 (long) offset);
6015 else
6016 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6017 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6018 (long) -offset);
6019 output_asm_insn (buf, operands);
6020 base_reg = regs[0];
6021 strcpy (buf, "ldm%?ia\t");
6022 break;
6024 default:
6025 gcc_unreachable ();
6028 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6029 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6031 for (i = 1; i < nops; i++)
6032 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6033 reg_names[regs[i]]);
6035 strcat (buf, "}\t%@ phole ldm");
6037 output_asm_insn (buf, operands);
6038 return "";
6042 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
6043 HOST_WIDE_INT * load_offset)
6045 int unsorted_regs[4];
6046 HOST_WIDE_INT unsorted_offsets[4];
6047 int order[4];
6048 int base_reg = -1;
6049 int i;
6051 /* Can only handle 2, 3, or 4 insns at present, though could be easily
6052 extended if required. */
6053 gcc_assert (nops >= 2 && nops <= 4);
6055 /* Loop over the operands and check that the memory references are
6056 suitable (i.e. immediate offsets from the same base register). At
6057 the same time, extract the target register, and the memory
6058 offsets. */
6059 for (i = 0; i < nops; i++)
6061 rtx reg;
6062 rtx offset;
6064 /* Convert a subreg of a mem into the mem itself. */
6065 if (GET_CODE (operands[nops + i]) == SUBREG)
6066 operands[nops + i] = alter_subreg (operands + (nops + i));
6068 gcc_assert (GET_CODE (operands[nops + i]) == MEM);
6070 /* Don't reorder volatile memory references; it doesn't seem worth
6071 looking for the case where the order is ok anyway. */
6072 if (MEM_VOLATILE_P (operands[nops + i]))
6073 return 0;
6075 offset = const0_rtx;
6077 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
6078 || (GET_CODE (reg) == SUBREG
6079 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6080 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
6081 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
6082 == REG)
6083 || (GET_CODE (reg) == SUBREG
6084 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6085 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
6086 == CONST_INT)))
6088 if (i == 0)
6090 base_reg = REGNO (reg);
6091 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
6092 ? REGNO (operands[i])
6093 : REGNO (SUBREG_REG (operands[i])));
6094 order[0] = 0;
6096 else
6098 if (base_reg != (int) REGNO (reg))
6099 /* Not addressed from the same base register. */
6100 return 0;
6102 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
6103 ? REGNO (operands[i])
6104 : REGNO (SUBREG_REG (operands[i])));
6105 if (unsorted_regs[i] < unsorted_regs[order[0]])
6106 order[0] = i;
6109 /* If it isn't an integer register, then we can't do this. */
6110 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
6111 return 0;
6113 unsorted_offsets[i] = INTVAL (offset);
6115 else
6116 /* Not a suitable memory address. */
6117 return 0;
6120 /* All the useful information has now been extracted from the
6121 operands into unsorted_regs and unsorted_offsets; additionally,
6122 order[0] has been set to the lowest numbered register in the
6123 list. Sort the registers into order, and check that the memory
6124 offsets are ascending and adjacent. */
6126 for (i = 1; i < nops; i++)
6128 int j;
6130 order[i] = order[i - 1];
6131 for (j = 0; j < nops; j++)
6132 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
6133 && (order[i] == order[i - 1]
6134 || unsorted_regs[j] < unsorted_regs[order[i]]))
6135 order[i] = j;
6137 /* Have we found a suitable register? if not, one must be used more
6138 than once. */
6139 if (order[i] == order[i - 1])
6140 return 0;
6142 /* Is the memory address adjacent and ascending? */
6143 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
6144 return 0;
6147 if (base)
6149 *base = base_reg;
6151 for (i = 0; i < nops; i++)
6152 regs[i] = unsorted_regs[order[i]];
6154 *load_offset = unsorted_offsets[order[0]];
6157 if (unsorted_offsets[order[0]] == 0)
6158 return 1; /* stmia */
6160 if (unsorted_offsets[order[0]] == 4)
6161 return 2; /* stmib */
6163 if (unsorted_offsets[order[nops - 1]] == 0)
6164 return 3; /* stmda */
6166 if (unsorted_offsets[order[nops - 1]] == -4)
6167 return 4; /* stmdb */
6169 return 0;
6172 const char *
6173 emit_stm_seq (rtx *operands, int nops)
6175 int regs[4];
6176 int base_reg;
6177 HOST_WIDE_INT offset;
6178 char buf[100];
6179 int i;
6181 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
6183 case 1:
6184 strcpy (buf, "stm%?ia\t");
6185 break;
6187 case 2:
6188 strcpy (buf, "stm%?ib\t");
6189 break;
6191 case 3:
6192 strcpy (buf, "stm%?da\t");
6193 break;
6195 case 4:
6196 strcpy (buf, "stm%?db\t");
6197 break;
6199 default:
6200 gcc_unreachable ();
6203 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6204 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6206 for (i = 1; i < nops; i++)
6207 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6208 reg_names[regs[i]]);
6210 strcat (buf, "}\t%@ phole stm");
6212 output_asm_insn (buf, operands);
6213 return "";
6216 /* Routines for use in generating RTL. */
6219 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
6220 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6222 HOST_WIDE_INT offset = *offsetp;
6223 int i = 0, j;
6224 rtx result;
6225 int sign = up ? 1 : -1;
6226 rtx mem, addr;
6228 /* XScale has load-store double instructions, but they have stricter
6229 alignment requirements than load-store multiple, so we cannot
6230 use them.
6232 For XScale ldm requires 2 + NREGS cycles to complete and blocks
6233 the pipeline until completion.
6235 NREGS CYCLES
6241 An ldr instruction takes 1-3 cycles, but does not block the
6242 pipeline.
6244 NREGS CYCLES
6245 1 1-3
6246 2 2-6
6247 3 3-9
6248 4 4-12
6250 Best case ldr will always win. However, the more ldr instructions
6251 we issue, the less likely we are to be able to schedule them well.
6252 Using ldr instructions also increases code size.
6254 As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
6255 for counts of 3 or 4 regs. */
6256 if (arm_tune_xscale && count <= 2 && ! optimize_size)
6258 rtx seq;
6260 start_sequence ();
6262 for (i = 0; i < count; i++)
6264 addr = plus_constant (from, i * 4 * sign);
6265 mem = adjust_automodify_address (basemem, SImode, addr, offset);
6266 emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
6267 offset += 4 * sign;
6270 if (write_back)
6272 emit_move_insn (from, plus_constant (from, count * 4 * sign));
6273 *offsetp = offset;
6276 seq = get_insns ();
6277 end_sequence ();
6279 return seq;
6282 result = gen_rtx_PARALLEL (VOIDmode,
6283 rtvec_alloc (count + (write_back ? 1 : 0)));
6284 if (write_back)
6286 XVECEXP (result, 0, 0)
6287 = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
6288 i = 1;
6289 count++;
6292 for (j = 0; i < count; i++, j++)
6294 addr = plus_constant (from, j * 4 * sign);
6295 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6296 XVECEXP (result, 0, i)
6297 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
6298 offset += 4 * sign;
6301 if (write_back)
6302 *offsetp = offset;
6304 return result;
6308 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
6309 int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6311 HOST_WIDE_INT offset = *offsetp;
6312 int i = 0, j;
6313 rtx result;
6314 int sign = up ? 1 : -1;
6315 rtx mem, addr;
6317 /* See arm_gen_load_multiple for discussion of
6318 the pros/cons of ldm/stm usage for XScale. */
6319 if (arm_tune_xscale && count <= 2 && ! optimize_size)
6321 rtx seq;
6323 start_sequence ();
6325 for (i = 0; i < count; i++)
6327 addr = plus_constant (to, i * 4 * sign);
6328 mem = adjust_automodify_address (basemem, SImode, addr, offset);
6329 emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
6330 offset += 4 * sign;
6333 if (write_back)
6335 emit_move_insn (to, plus_constant (to, count * 4 * sign));
6336 *offsetp = offset;
6339 seq = get_insns ();
6340 end_sequence ();
6342 return seq;
6345 result = gen_rtx_PARALLEL (VOIDmode,
6346 rtvec_alloc (count + (write_back ? 1 : 0)));
6347 if (write_back)
6349 XVECEXP (result, 0, 0)
6350 = gen_rtx_SET (VOIDmode, to,
6351 plus_constant (to, count * 4 * sign));
6352 i = 1;
6353 count++;
6356 for (j = 0; i < count; i++, j++)
6358 addr = plus_constant (to, j * 4 * sign);
6359 mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6360 XVECEXP (result, 0, i)
6361 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
6362 offset += 4 * sign;
6365 if (write_back)
6366 *offsetp = offset;
6368 return result;
6372 arm_gen_movmemqi (rtx *operands)
6374 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
6375 HOST_WIDE_INT srcoffset, dstoffset;
6376 int i;
6377 rtx src, dst, srcbase, dstbase;
6378 rtx part_bytes_reg = NULL;
6379 rtx mem;
6381 if (GET_CODE (operands[2]) != CONST_INT
6382 || GET_CODE (operands[3]) != CONST_INT
6383 || INTVAL (operands[2]) > 64
6384 || INTVAL (operands[3]) & 3)
6385 return 0;
6387 dstbase = operands[0];
6388 srcbase = operands[1];
6390 dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
6391 src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
6393 in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
6394 out_words_to_go = INTVAL (operands[2]) / 4;
6395 last_bytes = INTVAL (operands[2]) & 3;
6396 dstoffset = srcoffset = 0;
6398 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
6399 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
6401 for (i = 0; in_words_to_go >= 2; i+=4)
6403 if (in_words_to_go > 4)
6404 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
6405 srcbase, &srcoffset));
6406 else
6407 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
6408 FALSE, srcbase, &srcoffset));
6410 if (out_words_to_go)
6412 if (out_words_to_go > 4)
6413 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
6414 dstbase, &dstoffset));
6415 else if (out_words_to_go != 1)
6416 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
6417 dst, TRUE,
6418 (last_bytes == 0
6419 ? FALSE : TRUE),
6420 dstbase, &dstoffset));
6421 else
6423 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6424 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
6425 if (last_bytes != 0)
6427 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
6428 dstoffset += 4;
6433 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
6434 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
6437 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
6438 if (out_words_to_go)
6440 rtx sreg;
6442 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6443 sreg = copy_to_reg (mem);
6445 mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6446 emit_move_insn (mem, sreg);
6447 in_words_to_go--;
6449 gcc_assert (!in_words_to_go); /* Sanity check */
6452 if (in_words_to_go)
6454 gcc_assert (in_words_to_go > 0);
6456 mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6457 part_bytes_reg = copy_to_mode_reg (SImode, mem);
6460 gcc_assert (!last_bytes || part_bytes_reg);
6462 if (BYTES_BIG_ENDIAN && last_bytes)
6464 rtx tmp = gen_reg_rtx (SImode);
6466 /* The bytes we want are in the top end of the word. */
6467 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6468 GEN_INT (8 * (4 - last_bytes))));
6469 part_bytes_reg = tmp;
6471 while (last_bytes)
6473 mem = adjust_automodify_address (dstbase, QImode,
6474 plus_constant (dst, last_bytes - 1),
6475 dstoffset + last_bytes - 1);
6476 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6478 if (--last_bytes)
6480 tmp = gen_reg_rtx (SImode);
6481 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6482 part_bytes_reg = tmp;
6487 else
6489 if (last_bytes > 1)
6491 mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
6492 emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
6493 last_bytes -= 2;
6494 if (last_bytes)
6496 rtx tmp = gen_reg_rtx (SImode);
6497 emit_insn (gen_addsi3 (dst, dst, const2_rtx));
6498 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
6499 part_bytes_reg = tmp;
6500 dstoffset += 2;
6504 if (last_bytes)
6506 mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
6507 emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6511 return 1;
6514 /* Select a dominance comparison mode if possible for a test of the general
6515 form (OP (COND_OR (X) (Y)) (const_int 0)). We support three forms.
6516 COND_OR == DOM_CC_X_AND_Y => (X && Y)
6517 COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6518 COND_OR == DOM_CC_X_OR_Y => (X || Y)
6519 In all cases OP will be either EQ or NE, but we don't need to know which
6520 here. If we are unable to support a dominance comparison we return
6521 CC mode. This will then fail to match for the RTL expressions that
6522 generate this call. */
6523 enum machine_mode
6524 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6526 enum rtx_code cond1, cond2;
6527 int swapped = 0;
6529 /* Currently we will probably get the wrong result if the individual
6530 comparisons are not simple. This also ensures that it is safe to
6531 reverse a comparison if necessary. */
6532 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6533 != CCmode)
6534 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6535 != CCmode))
6536 return CCmode;
6538 /* The if_then_else variant of this tests the second condition if the
6539 first passes, but is true if the first fails. Reverse the first
6540 condition to get a true "inclusive-or" expression. */
6541 if (cond_or == DOM_CC_NX_OR_Y)
6542 cond1 = reverse_condition (cond1);
6544 /* If the comparisons are not equal, and one doesn't dominate the other,
6545 then we can't do this. */
6546 if (cond1 != cond2
6547 && !comparison_dominates_p (cond1, cond2)
6548 && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6549 return CCmode;
6551 if (swapped)
6553 enum rtx_code temp = cond1;
6554 cond1 = cond2;
6555 cond2 = temp;
6558 switch (cond1)
6560 case EQ:
6561 if (cond_or == DOM_CC_X_AND_Y)
6562 return CC_DEQmode;
6564 switch (cond2)
6566 case EQ: return CC_DEQmode;
6567 case LE: return CC_DLEmode;
6568 case LEU: return CC_DLEUmode;
6569 case GE: return CC_DGEmode;
6570 case GEU: return CC_DGEUmode;
6571 default: gcc_unreachable ();
6574 case LT:
6575 if (cond_or == DOM_CC_X_AND_Y)
6576 return CC_DLTmode;
6578 switch (cond2)
6580 case LT:
6581 return CC_DLTmode;
6582 case LE:
6583 return CC_DLEmode;
6584 case NE:
6585 return CC_DNEmode;
6586 default:
6587 gcc_unreachable ();
6590 case GT:
6591 if (cond_or == DOM_CC_X_AND_Y)
6592 return CC_DGTmode;
6594 switch (cond2)
6596 case GT:
6597 return CC_DGTmode;
6598 case GE:
6599 return CC_DGEmode;
6600 case NE:
6601 return CC_DNEmode;
6602 default:
6603 gcc_unreachable ();
6606 case LTU:
6607 if (cond_or == DOM_CC_X_AND_Y)
6608 return CC_DLTUmode;
6610 switch (cond2)
6612 case LTU:
6613 return CC_DLTUmode;
6614 case LEU:
6615 return CC_DLEUmode;
6616 case NE:
6617 return CC_DNEmode;
6618 default:
6619 gcc_unreachable ();
6622 case GTU:
6623 if (cond_or == DOM_CC_X_AND_Y)
6624 return CC_DGTUmode;
6626 switch (cond2)
6628 case GTU:
6629 return CC_DGTUmode;
6630 case GEU:
6631 return CC_DGEUmode;
6632 case NE:
6633 return CC_DNEmode;
6634 default:
6635 gcc_unreachable ();
6638 /* The remaining cases only occur when both comparisons are the
6639 same. */
6640 case NE:
6641 gcc_assert (cond1 == cond2);
6642 return CC_DNEmode;
6644 case LE:
6645 gcc_assert (cond1 == cond2);
6646 return CC_DLEmode;
6648 case GE:
6649 gcc_assert (cond1 == cond2);
6650 return CC_DGEmode;
6652 case LEU:
6653 gcc_assert (cond1 == cond2);
6654 return CC_DLEUmode;
6656 case GEU:
6657 gcc_assert (cond1 == cond2);
6658 return CC_DGEUmode;
6660 default:
6661 gcc_unreachable ();
6665 enum machine_mode
6666 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6668 /* All floating point compares return CCFP if it is an equality
6669 comparison, and CCFPE otherwise. */
6670 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6672 switch (op)
6674 case EQ:
6675 case NE:
6676 case UNORDERED:
6677 case ORDERED:
6678 case UNLT:
6679 case UNLE:
6680 case UNGT:
6681 case UNGE:
6682 case UNEQ:
6683 case LTGT:
6684 return CCFPmode;
6686 case LT:
6687 case LE:
6688 case GT:
6689 case GE:
6690 if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6691 return CCFPmode;
6692 return CCFPEmode;
6694 default:
6695 gcc_unreachable ();
6699 /* A compare with a shifted operand. Because of canonicalization, the
6700 comparison will have to be swapped when we emit the assembler. */
6701 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6702 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6703 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6704 || GET_CODE (x) == ROTATERT))
6705 return CC_SWPmode;
6707 /* This operation is performed swapped, but since we only rely on the Z
6708 flag we don't need an additional mode. */
6709 if (GET_MODE (y) == SImode && REG_P (y)
6710 && GET_CODE (x) == NEG
6711 && (op == EQ || op == NE))
6712 return CC_Zmode;
6714 /* This is a special case that is used by combine to allow a
6715 comparison of a shifted byte load to be split into a zero-extend
6716 followed by a comparison of the shifted integer (only valid for
6717 equalities and unsigned inequalities). */
6718 if (GET_MODE (x) == SImode
6719 && GET_CODE (x) == ASHIFT
6720 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6721 && GET_CODE (XEXP (x, 0)) == SUBREG
6722 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6723 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6724 && (op == EQ || op == NE
6725 || op == GEU || op == GTU || op == LTU || op == LEU)
6726 && GET_CODE (y) == CONST_INT)
6727 return CC_Zmode;
6729 /* A construct for a conditional compare, if the false arm contains
6730 0, then both conditions must be true, otherwise either condition
6731 must be true. Not all conditions are possible, so CCmode is
6732 returned if it can't be done. */
6733 if (GET_CODE (x) == IF_THEN_ELSE
6734 && (XEXP (x, 2) == const0_rtx
6735 || XEXP (x, 2) == const1_rtx)
6736 && COMPARISON_P (XEXP (x, 0))
6737 && COMPARISON_P (XEXP (x, 1)))
6738 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6739 INTVAL (XEXP (x, 2)));
6741 /* Alternate canonicalizations of the above. These are somewhat cleaner. */
6742 if (GET_CODE (x) == AND
6743 && COMPARISON_P (XEXP (x, 0))
6744 && COMPARISON_P (XEXP (x, 1)))
6745 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6746 DOM_CC_X_AND_Y);
6748 if (GET_CODE (x) == IOR
6749 && COMPARISON_P (XEXP (x, 0))
6750 && COMPARISON_P (XEXP (x, 1)))
6751 return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6752 DOM_CC_X_OR_Y);
6754 /* An operation (on Thumb) where we want to test for a single bit.
6755 This is done by shifting that bit up into the top bit of a
6756 scratch register; we can then branch on the sign bit. */
6757 if (TARGET_THUMB
6758 && GET_MODE (x) == SImode
6759 && (op == EQ || op == NE)
6760 && GET_CODE (x) == ZERO_EXTRACT
6761 && XEXP (x, 1) == const1_rtx)
6762 return CC_Nmode;
6764 /* An operation that sets the condition codes as a side-effect, the
6765 V flag is not set correctly, so we can only use comparisons where
6766 this doesn't matter. (For LT and GE we can use "mi" and "pl"
6767 instead.) */
6768 if (GET_MODE (x) == SImode
6769 && y == const0_rtx
6770 && (op == EQ || op == NE || op == LT || op == GE)
6771 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6772 || GET_CODE (x) == AND || GET_CODE (x) == IOR
6773 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6774 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6775 || GET_CODE (x) == LSHIFTRT
6776 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6777 || GET_CODE (x) == ROTATERT
6778 || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6779 return CC_NOOVmode;
6781 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6782 return CC_Zmode;
6784 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6785 && GET_CODE (x) == PLUS
6786 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6787 return CC_Cmode;
6789 return CCmode;
6792 /* X and Y are two things to compare using CODE. Emit the compare insn and
6793 return the rtx for register 0 in the proper mode. FP means this is a
6794 floating point compare: I don't think that it is needed on the arm. */
6796 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6798 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6799 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6801 emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
6803 return cc_reg;
6806 /* Generate a sequence of insns that will generate the correct return
6807 address mask depending on the physical architecture that the program
6808 is running on. */
6810 arm_gen_return_addr_mask (void)
6812 rtx reg = gen_reg_rtx (Pmode);
6814 emit_insn (gen_return_addr_mask (reg));
6815 return reg;
6818 void
6819 arm_reload_in_hi (rtx *operands)
6821 rtx ref = operands[1];
6822 rtx base, scratch;
6823 HOST_WIDE_INT offset = 0;
6825 if (GET_CODE (ref) == SUBREG)
6827 offset = SUBREG_BYTE (ref);
6828 ref = SUBREG_REG (ref);
6831 if (GET_CODE (ref) == REG)
6833 /* We have a pseudo which has been spilt onto the stack; there
6834 are two cases here: the first where there is a simple
6835 stack-slot replacement and a second where the stack-slot is
6836 out of range, or is used as a subreg. */
6837 if (reg_equiv_mem[REGNO (ref)])
6839 ref = reg_equiv_mem[REGNO (ref)];
6840 base = find_replacement (&XEXP (ref, 0));
6842 else
6843 /* The slot is out of range, or was dressed up in a SUBREG. */
6844 base = reg_equiv_address[REGNO (ref)];
6846 else
6847 base = find_replacement (&XEXP (ref, 0));
6849 /* Handle the case where the address is too complex to be offset by 1. */
6850 if (GET_CODE (base) == MINUS
6851 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6853 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6855 emit_set_insn (base_plus, base);
6856 base = base_plus;
6858 else if (GET_CODE (base) == PLUS)
6860 /* The addend must be CONST_INT, or we would have dealt with it above. */
6861 HOST_WIDE_INT hi, lo;
6863 offset += INTVAL (XEXP (base, 1));
6864 base = XEXP (base, 0);
6866 /* Rework the address into a legal sequence of insns. */
6867 /* Valid range for lo is -4095 -> 4095 */
6868 lo = (offset >= 0
6869 ? (offset & 0xfff)
6870 : -((-offset) & 0xfff));
6872 /* Corner case, if lo is the max offset then we would be out of range
6873 once we have added the additional 1 below, so bump the msb into the
6874 pre-loading insn(s). */
6875 if (lo == 4095)
6876 lo &= 0x7ff;
6878 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6879 ^ (HOST_WIDE_INT) 0x80000000)
6880 - (HOST_WIDE_INT) 0x80000000);
6882 gcc_assert (hi + lo == offset);
6884 if (hi != 0)
6886 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6888 /* Get the base address; addsi3 knows how to handle constants
6889 that require more than one insn. */
6890 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6891 base = base_plus;
6892 offset = lo;
6896 /* Operands[2] may overlap operands[0] (though it won't overlap
6897 operands[1]), that's why we asked for a DImode reg -- so we can
6898 use the bit that does not overlap. */
6899 if (REGNO (operands[2]) == REGNO (operands[0]))
6900 scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6901 else
6902 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6904 emit_insn (gen_zero_extendqisi2 (scratch,
6905 gen_rtx_MEM (QImode,
6906 plus_constant (base,
6907 offset))));
6908 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6909 gen_rtx_MEM (QImode,
6910 plus_constant (base,
6911 offset + 1))));
6912 if (!BYTES_BIG_ENDIAN)
6913 emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
6914 gen_rtx_IOR (SImode,
6915 gen_rtx_ASHIFT
6916 (SImode,
6917 gen_rtx_SUBREG (SImode, operands[0], 0),
6918 GEN_INT (8)),
6919 scratch));
6920 else
6921 emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
6922 gen_rtx_IOR (SImode,
6923 gen_rtx_ASHIFT (SImode, scratch,
6924 GEN_INT (8)),
6925 gen_rtx_SUBREG (SImode, operands[0], 0)));
6928 /* Handle storing a half-word to memory during reload by synthesizing as two
6929 byte stores. Take care not to clobber the input values until after we
6930 have moved them somewhere safe. This code assumes that if the DImode
6931 scratch in operands[2] overlaps either the input value or output address
6932 in some way, then that value must die in this insn (we absolutely need
6933 two scratch registers for some corner cases). */
6934 void
6935 arm_reload_out_hi (rtx *operands)
6937 rtx ref = operands[0];
6938 rtx outval = operands[1];
6939 rtx base, scratch;
6940 HOST_WIDE_INT offset = 0;
6942 if (GET_CODE (ref) == SUBREG)
6944 offset = SUBREG_BYTE (ref);
6945 ref = SUBREG_REG (ref);
6948 if (GET_CODE (ref) == REG)
6950 /* We have a pseudo which has been spilt onto the stack; there
6951 are two cases here: the first where there is a simple
6952 stack-slot replacement and a second where the stack-slot is
6953 out of range, or is used as a subreg. */
6954 if (reg_equiv_mem[REGNO (ref)])
6956 ref = reg_equiv_mem[REGNO (ref)];
6957 base = find_replacement (&XEXP (ref, 0));
6959 else
6960 /* The slot is out of range, or was dressed up in a SUBREG. */
6961 base = reg_equiv_address[REGNO (ref)];
6963 else
6964 base = find_replacement (&XEXP (ref, 0));
6966 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6968 /* Handle the case where the address is too complex to be offset by 1. */
6969 if (GET_CODE (base) == MINUS
6970 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6972 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6974 /* Be careful not to destroy OUTVAL. */
6975 if (reg_overlap_mentioned_p (base_plus, outval))
6977 /* Updating base_plus might destroy outval, see if we can
6978 swap the scratch and base_plus. */
6979 if (!reg_overlap_mentioned_p (scratch, outval))
6981 rtx tmp = scratch;
6982 scratch = base_plus;
6983 base_plus = tmp;
6985 else
6987 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6989 /* Be conservative and copy OUTVAL into the scratch now,
6990 this should only be necessary if outval is a subreg
6991 of something larger than a word. */
6992 /* XXX Might this clobber base? I can't see how it can,
6993 since scratch is known to overlap with OUTVAL, and
6994 must be wider than a word. */
6995 emit_insn (gen_movhi (scratch_hi, outval));
6996 outval = scratch_hi;
7000 emit_set_insn (base_plus, base);
7001 base = base_plus;
7003 else if (GET_CODE (base) == PLUS)
7005 /* The addend must be CONST_INT, or we would have dealt with it above. */
7006 HOST_WIDE_INT hi, lo;
7008 offset += INTVAL (XEXP (base, 1));
7009 base = XEXP (base, 0);
7011 /* Rework the address into a legal sequence of insns. */
7012 /* Valid range for lo is -4095 -> 4095 */
7013 lo = (offset >= 0
7014 ? (offset & 0xfff)
7015 : -((-offset) & 0xfff));
7017 /* Corner case, if lo is the max offset then we would be out of range
7018 once we have added the additional 1 below, so bump the msb into the
7019 pre-loading insn(s). */
7020 if (lo == 4095)
7021 lo &= 0x7ff;
7023 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
7024 ^ (HOST_WIDE_INT) 0x80000000)
7025 - (HOST_WIDE_INT) 0x80000000);
7027 gcc_assert (hi + lo == offset);
7029 if (hi != 0)
7031 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7033 /* Be careful not to destroy OUTVAL. */
7034 if (reg_overlap_mentioned_p (base_plus, outval))
7036 /* Updating base_plus might destroy outval, see if we
7037 can swap the scratch and base_plus. */
7038 if (!reg_overlap_mentioned_p (scratch, outval))
7040 rtx tmp = scratch;
7041 scratch = base_plus;
7042 base_plus = tmp;
7044 else
7046 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
7048 /* Be conservative and copy outval into scratch now,
7049 this should only be necessary if outval is a
7050 subreg of something larger than a word. */
7051 /* XXX Might this clobber base? I can't see how it
7052 can, since scratch is known to overlap with
7053 outval. */
7054 emit_insn (gen_movhi (scratch_hi, outval));
7055 outval = scratch_hi;
7059 /* Get the base address; addsi3 knows how to handle constants
7060 that require more than one insn. */
7061 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
7062 base = base_plus;
7063 offset = lo;
7067 if (BYTES_BIG_ENDIAN)
7069 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7070 plus_constant (base, offset + 1)),
7071 gen_lowpart (QImode, outval)));
7072 emit_insn (gen_lshrsi3 (scratch,
7073 gen_rtx_SUBREG (SImode, outval, 0),
7074 GEN_INT (8)));
7075 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7076 gen_lowpart (QImode, scratch)));
7078 else
7080 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7081 gen_lowpart (QImode, outval)));
7082 emit_insn (gen_lshrsi3 (scratch,
7083 gen_rtx_SUBREG (SImode, outval, 0),
7084 GEN_INT (8)));
7085 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7086 plus_constant (base, offset + 1)),
7087 gen_lowpart (QImode, scratch)));
7091 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
7092 (padded to the size of a word) should be passed in a register. */
7094 static bool
7095 arm_must_pass_in_stack (enum machine_mode mode, tree type)
7097 if (TARGET_AAPCS_BASED)
7098 return must_pass_in_stack_var_size (mode, type);
7099 else
7100 return must_pass_in_stack_var_size_or_pad (mode, type);
7104 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
7105 Return true if an argument passed on the stack should be padded upwards,
7106 i.e. if the least-significant byte has useful data.
7107 For legacy APCS ABIs we use the default. For AAPCS based ABIs small
7108 aggregate types are placed in the lowest memory address. */
7110 bool
7111 arm_pad_arg_upward (enum machine_mode mode, tree type)
7113 if (!TARGET_AAPCS_BASED)
7114 return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
7116 if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
7117 return false;
7119 return true;
7123 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
7124 For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
7125 byte of the register has useful data, and return the opposite if the
7126 most significant byte does.
7127 For AAPCS, small aggregates and small complex types are always padded
7128 upwards. */
7130 bool
7131 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
7132 tree type, int first ATTRIBUTE_UNUSED)
7134 if (TARGET_AAPCS_BASED
7135 && BYTES_BIG_ENDIAN
7136 && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
7137 && int_size_in_bytes (type) <= 4)
7138 return true;
7140 /* Otherwise, use default padding. */
7141 return !BYTES_BIG_ENDIAN;
7145 /* Print a symbolic form of X to the debug file, F. */
7146 static void
7147 arm_print_value (FILE *f, rtx x)
7149 switch (GET_CODE (x))
7151 case CONST_INT:
7152 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
7153 return;
7155 case CONST_DOUBLE:
7156 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
7157 return;
7159 case CONST_VECTOR:
7161 int i;
7163 fprintf (f, "<");
7164 for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
7166 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
7167 if (i < (CONST_VECTOR_NUNITS (x) - 1))
7168 fputc (',', f);
7170 fprintf (f, ">");
7172 return;
7174 case CONST_STRING:
7175 fprintf (f, "\"%s\"", XSTR (x, 0));
7176 return;
7178 case SYMBOL_REF:
7179 fprintf (f, "`%s'", XSTR (x, 0));
7180 return;
7182 case LABEL_REF:
7183 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
7184 return;
7186 case CONST:
7187 arm_print_value (f, XEXP (x, 0));
7188 return;
7190 case PLUS:
7191 arm_print_value (f, XEXP (x, 0));
7192 fprintf (f, "+");
7193 arm_print_value (f, XEXP (x, 1));
7194 return;
7196 case PC:
7197 fprintf (f, "pc");
7198 return;
7200 default:
7201 fprintf (f, "????");
7202 return;
7206 /* Routines for manipulation of the constant pool. */
7208 /* Arm instructions cannot load a large constant directly into a
7209 register; they have to come from a pc relative load. The constant
7210 must therefore be placed in the addressable range of the pc
7211 relative load. Depending on the precise pc relative load
7212 instruction the range is somewhere between 256 bytes and 4k. This
7213 means that we often have to dump a constant inside a function, and
7214 generate code to branch around it.
7216 It is important to minimize this, since the branches will slow
7217 things down and make the code larger.
7219 Normally we can hide the table after an existing unconditional
7220 branch so that there is no interruption of the flow, but in the
7221 worst case the code looks like this:
7223 ldr rn, L1
7225 b L2
7226 align
7227 L1: .long value
7231 ldr rn, L3
7233 b L4
7234 align
7235 L3: .long value
7239 We fix this by performing a scan after scheduling, which notices
7240 which instructions need to have their operands fetched from the
7241 constant table and builds the table.
7243 The algorithm starts by building a table of all the constants that
7244 need fixing up and all the natural barriers in the function (places
7245 where a constant table can be dropped without breaking the flow).
7246 For each fixup we note how far the pc-relative replacement will be
7247 able to reach and the offset of the instruction into the function.
7249 Having built the table we then group the fixes together to form
7250 tables that are as large as possible (subject to addressing
7251 constraints) and emit each table of constants after the last
7252 barrier that is within range of all the instructions in the group.
7253 If a group does not contain a barrier, then we forcibly create one
7254 by inserting a jump instruction into the flow. Once the table has
7255 been inserted, the insns are then modified to reference the
7256 relevant entry in the pool.
7258 Possible enhancements to the algorithm (not implemented) are:
7260 1) For some processors and object formats, there may be benefit in
7261 aligning the pools to the start of cache lines; this alignment
7262 would need to be taken into account when calculating addressability
7263 of a pool. */
7265 /* These typedefs are located at the start of this file, so that
7266 they can be used in the prototypes there. This comment is to
7267 remind readers of that fact so that the following structures
7268 can be understood more easily.
7270 typedef struct minipool_node Mnode;
7271 typedef struct minipool_fixup Mfix; */
7273 struct minipool_node
7275 /* Doubly linked chain of entries. */
7276 Mnode * next;
7277 Mnode * prev;
7278 /* The maximum offset into the code that this entry can be placed. While
7279 pushing fixes for forward references, all entries are sorted in order
7280 of increasing max_address. */
7281 HOST_WIDE_INT max_address;
7282 /* Similarly for an entry inserted for a backwards ref. */
7283 HOST_WIDE_INT min_address;
7284 /* The number of fixes referencing this entry. This can become zero
7285 if we "unpush" an entry. In this case we ignore the entry when we
7286 come to emit the code. */
7287 int refcount;
7288 /* The offset from the start of the minipool. */
7289 HOST_WIDE_INT offset;
7290 /* The value in table. */
7291 rtx value;
7292 /* The mode of value. */
7293 enum machine_mode mode;
7294 /* The size of the value. With iWMMXt enabled
7295 sizes > 4 also imply an alignment of 8-bytes. */
7296 int fix_size;
7299 struct minipool_fixup
7301 Mfix * next;
7302 rtx insn;
7303 HOST_WIDE_INT address;
7304 rtx * loc;
7305 enum machine_mode mode;
7306 int fix_size;
7307 rtx value;
7308 Mnode * minipool;
7309 HOST_WIDE_INT forwards;
7310 HOST_WIDE_INT backwards;
7313 /* Fixes less than a word need padding out to a word boundary. */
7314 #define MINIPOOL_FIX_SIZE(mode) \
7315 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
7317 static Mnode * minipool_vector_head;
7318 static Mnode * minipool_vector_tail;
7319 static rtx minipool_vector_label;
7320 static int minipool_pad;
7322 /* The linked list of all minipool fixes required for this function. */
7323 Mfix * minipool_fix_head;
7324 Mfix * minipool_fix_tail;
7325 /* The fix entry for the current minipool, once it has been placed. */
7326 Mfix * minipool_barrier;
7328 /* Determines if INSN is the start of a jump table. Returns the end
7329 of the TABLE or NULL_RTX. */
7330 static rtx
7331 is_jump_table (rtx insn)
7333 rtx table;
7335 if (GET_CODE (insn) == JUMP_INSN
7336 && JUMP_LABEL (insn) != NULL
7337 && ((table = next_real_insn (JUMP_LABEL (insn)))
7338 == next_real_insn (insn))
7339 && table != NULL
7340 && GET_CODE (table) == JUMP_INSN
7341 && (GET_CODE (PATTERN (table)) == ADDR_VEC
7342 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
7343 return table;
7345 return NULL_RTX;
7348 #ifndef JUMP_TABLES_IN_TEXT_SECTION
7349 #define JUMP_TABLES_IN_TEXT_SECTION 0
7350 #endif
7352 static HOST_WIDE_INT
7353 get_jump_table_size (rtx insn)
7355 /* ADDR_VECs only take room if read-only data does into the text
7356 section. */
7357 if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
7359 rtx body = PATTERN (insn);
7360 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
7362 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
7365 return 0;
7368 /* Move a minipool fix MP from its current location to before MAX_MP.
7369 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
7370 constraints may need updating. */
7371 static Mnode *
7372 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
7373 HOST_WIDE_INT max_address)
7375 /* The code below assumes these are different. */
7376 gcc_assert (mp != max_mp);
7378 if (max_mp == NULL)
7380 if (max_address < mp->max_address)
7381 mp->max_address = max_address;
7383 else
7385 if (max_address > max_mp->max_address - mp->fix_size)
7386 mp->max_address = max_mp->max_address - mp->fix_size;
7387 else
7388 mp->max_address = max_address;
7390 /* Unlink MP from its current position. Since max_mp is non-null,
7391 mp->prev must be non-null. */
7392 mp->prev->next = mp->next;
7393 if (mp->next != NULL)
7394 mp->next->prev = mp->prev;
7395 else
7396 minipool_vector_tail = mp->prev;
7398 /* Re-insert it before MAX_MP. */
7399 mp->next = max_mp;
7400 mp->prev = max_mp->prev;
7401 max_mp->prev = mp;
7403 if (mp->prev != NULL)
7404 mp->prev->next = mp;
7405 else
7406 minipool_vector_head = mp;
7409 /* Save the new entry. */
7410 max_mp = mp;
7412 /* Scan over the preceding entries and adjust their addresses as
7413 required. */
7414 while (mp->prev != NULL
7415 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7417 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7418 mp = mp->prev;
7421 return max_mp;
7424 /* Add a constant to the minipool for a forward reference. Returns the
7425 node added or NULL if the constant will not fit in this pool. */
7426 static Mnode *
7427 add_minipool_forward_ref (Mfix *fix)
7429 /* If set, max_mp is the first pool_entry that has a lower
7430 constraint than the one we are trying to add. */
7431 Mnode * max_mp = NULL;
7432 HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
7433 Mnode * mp;
7435 /* If the minipool starts before the end of FIX->INSN then this FIX
7436 can not be placed into the current pool. Furthermore, adding the
7437 new constant pool entry may cause the pool to start FIX_SIZE bytes
7438 earlier. */
7439 if (minipool_vector_head &&
7440 (fix->address + get_attr_length (fix->insn)
7441 >= minipool_vector_head->max_address - fix->fix_size))
7442 return NULL;
7444 /* Scan the pool to see if a constant with the same value has
7445 already been added. While we are doing this, also note the
7446 location where we must insert the constant if it doesn't already
7447 exist. */
7448 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7450 if (GET_CODE (fix->value) == GET_CODE (mp->value)
7451 && fix->mode == mp->mode
7452 && (GET_CODE (fix->value) != CODE_LABEL
7453 || (CODE_LABEL_NUMBER (fix->value)
7454 == CODE_LABEL_NUMBER (mp->value)))
7455 && rtx_equal_p (fix->value, mp->value))
7457 /* More than one fix references this entry. */
7458 mp->refcount++;
7459 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
7462 /* Note the insertion point if necessary. */
7463 if (max_mp == NULL
7464 && mp->max_address > max_address)
7465 max_mp = mp;
7467 /* If we are inserting an 8-bytes aligned quantity and
7468 we have not already found an insertion point, then
7469 make sure that all such 8-byte aligned quantities are
7470 placed at the start of the pool. */
7471 if (ARM_DOUBLEWORD_ALIGN
7472 && max_mp == NULL
7473 && fix->fix_size == 8
7474 && mp->fix_size != 8)
7476 max_mp = mp;
7477 max_address = mp->max_address;
7481 /* The value is not currently in the minipool, so we need to create
7482 a new entry for it. If MAX_MP is NULL, the entry will be put on
7483 the end of the list since the placement is less constrained than
7484 any existing entry. Otherwise, we insert the new fix before
7485 MAX_MP and, if necessary, adjust the constraints on the other
7486 entries. */
7487 mp = XNEW (Mnode);
7488 mp->fix_size = fix->fix_size;
7489 mp->mode = fix->mode;
7490 mp->value = fix->value;
7491 mp->refcount = 1;
7492 /* Not yet required for a backwards ref. */
7493 mp->min_address = -65536;
7495 if (max_mp == NULL)
7497 mp->max_address = max_address;
7498 mp->next = NULL;
7499 mp->prev = minipool_vector_tail;
7501 if (mp->prev == NULL)
7503 minipool_vector_head = mp;
7504 minipool_vector_label = gen_label_rtx ();
7506 else
7507 mp->prev->next = mp;
7509 minipool_vector_tail = mp;
7511 else
7513 if (max_address > max_mp->max_address - mp->fix_size)
7514 mp->max_address = max_mp->max_address - mp->fix_size;
7515 else
7516 mp->max_address = max_address;
7518 mp->next = max_mp;
7519 mp->prev = max_mp->prev;
7520 max_mp->prev = mp;
7521 if (mp->prev != NULL)
7522 mp->prev->next = mp;
7523 else
7524 minipool_vector_head = mp;
7527 /* Save the new entry. */
7528 max_mp = mp;
7530 /* Scan over the preceding entries and adjust their addresses as
7531 required. */
7532 while (mp->prev != NULL
7533 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7535 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7536 mp = mp->prev;
7539 return max_mp;
7542 static Mnode *
7543 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7544 HOST_WIDE_INT min_address)
7546 HOST_WIDE_INT offset;
7548 /* The code below assumes these are different. */
7549 gcc_assert (mp != min_mp);
7551 if (min_mp == NULL)
7553 if (min_address > mp->min_address)
7554 mp->min_address = min_address;
7556 else
7558 /* We will adjust this below if it is too loose. */
7559 mp->min_address = min_address;
7561 /* Unlink MP from its current position. Since min_mp is non-null,
7562 mp->next must be non-null. */
7563 mp->next->prev = mp->prev;
7564 if (mp->prev != NULL)
7565 mp->prev->next = mp->next;
7566 else
7567 minipool_vector_head = mp->next;
7569 /* Reinsert it after MIN_MP. */
7570 mp->prev = min_mp;
7571 mp->next = min_mp->next;
7572 min_mp->next = mp;
7573 if (mp->next != NULL)
7574 mp->next->prev = mp;
7575 else
7576 minipool_vector_tail = mp;
7579 min_mp = mp;
7581 offset = 0;
7582 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7584 mp->offset = offset;
7585 if (mp->refcount > 0)
7586 offset += mp->fix_size;
7588 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7589 mp->next->min_address = mp->min_address + mp->fix_size;
7592 return min_mp;
7595 /* Add a constant to the minipool for a backward reference. Returns the
7596 node added or NULL if the constant will not fit in this pool.
7598 Note that the code for insertion for a backwards reference can be
7599 somewhat confusing because the calculated offsets for each fix do
7600 not take into account the size of the pool (which is still under
7601 construction. */
7602 static Mnode *
7603 add_minipool_backward_ref (Mfix *fix)
7605 /* If set, min_mp is the last pool_entry that has a lower constraint
7606 than the one we are trying to add. */
7607 Mnode *min_mp = NULL;
7608 /* This can be negative, since it is only a constraint. */
7609 HOST_WIDE_INT min_address = fix->address - fix->backwards;
7610 Mnode *mp;
7612 /* If we can't reach the current pool from this insn, or if we can't
7613 insert this entry at the end of the pool without pushing other
7614 fixes out of range, then we don't try. This ensures that we
7615 can't fail later on. */
7616 if (min_address >= minipool_barrier->address
7617 || (minipool_vector_tail->min_address + fix->fix_size
7618 >= minipool_barrier->address))
7619 return NULL;
7621 /* Scan the pool to see if a constant with the same value has
7622 already been added. While we are doing this, also note the
7623 location where we must insert the constant if it doesn't already
7624 exist. */
7625 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7627 if (GET_CODE (fix->value) == GET_CODE (mp->value)
7628 && fix->mode == mp->mode
7629 && (GET_CODE (fix->value) != CODE_LABEL
7630 || (CODE_LABEL_NUMBER (fix->value)
7631 == CODE_LABEL_NUMBER (mp->value)))
7632 && rtx_equal_p (fix->value, mp->value)
7633 /* Check that there is enough slack to move this entry to the
7634 end of the table (this is conservative). */
7635 && (mp->max_address
7636 > (minipool_barrier->address
7637 + minipool_vector_tail->offset
7638 + minipool_vector_tail->fix_size)))
7640 mp->refcount++;
7641 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7644 if (min_mp != NULL)
7645 mp->min_address += fix->fix_size;
7646 else
7648 /* Note the insertion point if necessary. */
7649 if (mp->min_address < min_address)
7651 /* For now, we do not allow the insertion of 8-byte alignment
7652 requiring nodes anywhere but at the start of the pool. */
7653 if (ARM_DOUBLEWORD_ALIGN
7654 && fix->fix_size == 8 && mp->fix_size != 8)
7655 return NULL;
7656 else
7657 min_mp = mp;
7659 else if (mp->max_address
7660 < minipool_barrier->address + mp->offset + fix->fix_size)
7662 /* Inserting before this entry would push the fix beyond
7663 its maximum address (which can happen if we have
7664 re-located a forwards fix); force the new fix to come
7665 after it. */
7666 min_mp = mp;
7667 min_address = mp->min_address + fix->fix_size;
7669 /* If we are inserting an 8-bytes aligned quantity and
7670 we have not already found an insertion point, then
7671 make sure that all such 8-byte aligned quantities are
7672 placed at the start of the pool. */
7673 else if (ARM_DOUBLEWORD_ALIGN
7674 && min_mp == NULL
7675 && fix->fix_size == 8
7676 && mp->fix_size < 8)
7678 min_mp = mp;
7679 min_address = mp->min_address + fix->fix_size;
7684 /* We need to create a new entry. */
7685 mp = XNEW (Mnode);
7686 mp->fix_size = fix->fix_size;
7687 mp->mode = fix->mode;
7688 mp->value = fix->value;
7689 mp->refcount = 1;
7690 mp->max_address = minipool_barrier->address + 65536;
7692 mp->min_address = min_address;
7694 if (min_mp == NULL)
7696 mp->prev = NULL;
7697 mp->next = minipool_vector_head;
7699 if (mp->next == NULL)
7701 minipool_vector_tail = mp;
7702 minipool_vector_label = gen_label_rtx ();
7704 else
7705 mp->next->prev = mp;
7707 minipool_vector_head = mp;
7709 else
7711 mp->next = min_mp->next;
7712 mp->prev = min_mp;
7713 min_mp->next = mp;
7715 if (mp->next != NULL)
7716 mp->next->prev = mp;
7717 else
7718 minipool_vector_tail = mp;
7721 /* Save the new entry. */
7722 min_mp = mp;
7724 if (mp->prev)
7725 mp = mp->prev;
7726 else
7727 mp->offset = 0;
7729 /* Scan over the following entries and adjust their offsets. */
7730 while (mp->next != NULL)
7732 if (mp->next->min_address < mp->min_address + mp->fix_size)
7733 mp->next->min_address = mp->min_address + mp->fix_size;
7735 if (mp->refcount)
7736 mp->next->offset = mp->offset + mp->fix_size;
7737 else
7738 mp->next->offset = mp->offset;
7740 mp = mp->next;
7743 return min_mp;
7746 static void
7747 assign_minipool_offsets (Mfix *barrier)
7749 HOST_WIDE_INT offset = 0;
7750 Mnode *mp;
7752 minipool_barrier = barrier;
7754 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7756 mp->offset = offset;
7758 if (mp->refcount > 0)
7759 offset += mp->fix_size;
7763 /* Output the literal table */
7764 static void
7765 dump_minipool (rtx scan)
7767 Mnode * mp;
7768 Mnode * nmp;
7769 int align64 = 0;
7771 if (ARM_DOUBLEWORD_ALIGN)
7772 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7773 if (mp->refcount > 0 && mp->fix_size == 8)
7775 align64 = 1;
7776 break;
7779 if (dump_file)
7780 fprintf (dump_file,
7781 ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7782 INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7784 scan = emit_label_after (gen_label_rtx (), scan);
7785 scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7786 scan = emit_label_after (minipool_vector_label, scan);
7788 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7790 if (mp->refcount > 0)
7792 if (dump_file)
7794 fprintf (dump_file,
7795 ";; Offset %u, min %ld, max %ld ",
7796 (unsigned) mp->offset, (unsigned long) mp->min_address,
7797 (unsigned long) mp->max_address);
7798 arm_print_value (dump_file, mp->value);
7799 fputc ('\n', dump_file);
7802 switch (mp->fix_size)
7804 #ifdef HAVE_consttable_1
7805 case 1:
7806 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7807 break;
7809 #endif
7810 #ifdef HAVE_consttable_2
7811 case 2:
7812 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7813 break;
7815 #endif
7816 #ifdef HAVE_consttable_4
7817 case 4:
7818 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7819 break;
7821 #endif
7822 #ifdef HAVE_consttable_8
7823 case 8:
7824 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7825 break;
7827 #endif
7828 default:
7829 gcc_unreachable ();
7833 nmp = mp->next;
7834 free (mp);
7837 minipool_vector_head = minipool_vector_tail = NULL;
7838 scan = emit_insn_after (gen_consttable_end (), scan);
7839 scan = emit_barrier_after (scan);
7842 /* Return the cost of forcibly inserting a barrier after INSN. */
7843 static int
7844 arm_barrier_cost (rtx insn)
7846 /* Basing the location of the pool on the loop depth is preferable,
7847 but at the moment, the basic block information seems to be
7848 corrupt by this stage of the compilation. */
7849 int base_cost = 50;
7850 rtx next = next_nonnote_insn (insn);
7852 if (next != NULL && GET_CODE (next) == CODE_LABEL)
7853 base_cost -= 20;
7855 switch (GET_CODE (insn))
7857 case CODE_LABEL:
7858 /* It will always be better to place the table before the label, rather
7859 than after it. */
7860 return 50;
7862 case INSN:
7863 case CALL_INSN:
7864 return base_cost;
7866 case JUMP_INSN:
7867 return base_cost - 10;
7869 default:
7870 return base_cost + 10;
7874 /* Find the best place in the insn stream in the range
7875 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7876 Create the barrier by inserting a jump and add a new fix entry for
7877 it. */
7878 static Mfix *
7879 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7881 HOST_WIDE_INT count = 0;
7882 rtx barrier;
7883 rtx from = fix->insn;
7884 /* The instruction after which we will insert the jump. */
7885 rtx selected = NULL;
7886 int selected_cost;
7887 /* The address at which the jump instruction will be placed. */
7888 HOST_WIDE_INT selected_address;
7889 Mfix * new_fix;
7890 HOST_WIDE_INT max_count = max_address - fix->address;
7891 rtx label = gen_label_rtx ();
7893 selected_cost = arm_barrier_cost (from);
7894 selected_address = fix->address;
7896 while (from && count < max_count)
7898 rtx tmp;
7899 int new_cost;
7901 /* This code shouldn't have been called if there was a natural barrier
7902 within range. */
7903 gcc_assert (GET_CODE (from) != BARRIER);
7905 /* Count the length of this insn. */
7906 count += get_attr_length (from);
7908 /* If there is a jump table, add its length. */
7909 tmp = is_jump_table (from);
7910 if (tmp != NULL)
7912 count += get_jump_table_size (tmp);
7914 /* Jump tables aren't in a basic block, so base the cost on
7915 the dispatch insn. If we select this location, we will
7916 still put the pool after the table. */
7917 new_cost = arm_barrier_cost (from);
7919 if (count < max_count
7920 && (!selected || new_cost <= selected_cost))
7922 selected = tmp;
7923 selected_cost = new_cost;
7924 selected_address = fix->address + count;
7927 /* Continue after the dispatch table. */
7928 from = NEXT_INSN (tmp);
7929 continue;
7932 new_cost = arm_barrier_cost (from);
7934 if (count < max_count
7935 && (!selected || new_cost <= selected_cost))
7937 selected = from;
7938 selected_cost = new_cost;
7939 selected_address = fix->address + count;
7942 from = NEXT_INSN (from);
7945 /* Make sure that we found a place to insert the jump. */
7946 gcc_assert (selected);
7948 /* Create a new JUMP_INSN that branches around a barrier. */
7949 from = emit_jump_insn_after (gen_jump (label), selected);
7950 JUMP_LABEL (from) = label;
7951 barrier = emit_barrier_after (from);
7952 emit_label_after (label, barrier);
7954 /* Create a minipool barrier entry for the new barrier. */
7955 new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7956 new_fix->insn = barrier;
7957 new_fix->address = selected_address;
7958 new_fix->next = fix->next;
7959 fix->next = new_fix;
7961 return new_fix;
7964 /* Record that there is a natural barrier in the insn stream at
7965 ADDRESS. */
7966 static void
7967 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7969 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7971 fix->insn = insn;
7972 fix->address = address;
7974 fix->next = NULL;
7975 if (minipool_fix_head != NULL)
7976 minipool_fix_tail->next = fix;
7977 else
7978 minipool_fix_head = fix;
7980 minipool_fix_tail = fix;
7983 /* Record INSN, which will need fixing up to load a value from the
7984 minipool. ADDRESS is the offset of the insn since the start of the
7985 function; LOC is a pointer to the part of the insn which requires
7986 fixing; VALUE is the constant that must be loaded, which is of type
7987 MODE. */
7988 static void
7989 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7990 enum machine_mode mode, rtx value)
7992 Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7994 #ifdef AOF_ASSEMBLER
7995 /* PIC symbol references need to be converted into offsets into the
7996 based area. */
7997 /* XXX This shouldn't be done here. */
7998 if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7999 value = aof_pic_entry (value);
8000 #endif /* AOF_ASSEMBLER */
8002 fix->insn = insn;
8003 fix->address = address;
8004 fix->loc = loc;
8005 fix->mode = mode;
8006 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
8007 fix->value = value;
8008 fix->forwards = get_attr_pool_range (insn);
8009 fix->backwards = get_attr_neg_pool_range (insn);
8010 fix->minipool = NULL;
8012 /* If an insn doesn't have a range defined for it, then it isn't
8013 expecting to be reworked by this code. Better to stop now than
8014 to generate duff assembly code. */
8015 gcc_assert (fix->forwards || fix->backwards);
8017 /* If an entry requires 8-byte alignment then assume all constant pools
8018 require 4 bytes of padding. Trying to do this later on a per-pool
8019 basis is awkward because existing pool entries have to be modified. */
8020 if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
8021 minipool_pad = 4;
8023 if (dump_file)
8025 fprintf (dump_file,
8026 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
8027 GET_MODE_NAME (mode),
8028 INSN_UID (insn), (unsigned long) address,
8029 -1 * (long)fix->backwards, (long)fix->forwards);
8030 arm_print_value (dump_file, fix->value);
8031 fprintf (dump_file, "\n");
8034 /* Add it to the chain of fixes. */
8035 fix->next = NULL;
8037 if (minipool_fix_head != NULL)
8038 minipool_fix_tail->next = fix;
8039 else
8040 minipool_fix_head = fix;
8042 minipool_fix_tail = fix;
8045 /* Return the cost of synthesizing a 64-bit constant VAL inline.
8046 Returns the number of insns needed, or 99 if we don't know how to
8047 do it. */
8049 arm_const_double_inline_cost (rtx val)
8051 rtx lowpart, highpart;
8052 enum machine_mode mode;
8054 mode = GET_MODE (val);
8056 if (mode == VOIDmode)
8057 mode = DImode;
8059 gcc_assert (GET_MODE_SIZE (mode) == 8);
8061 lowpart = gen_lowpart (SImode, val);
8062 highpart = gen_highpart_mode (SImode, mode, val);
8064 gcc_assert (GET_CODE (lowpart) == CONST_INT);
8065 gcc_assert (GET_CODE (highpart) == CONST_INT);
8067 return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
8068 NULL_RTX, NULL_RTX, 0, 0)
8069 + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
8070 NULL_RTX, NULL_RTX, 0, 0));
8073 /* Return true if it is worthwhile to split a 64-bit constant into two
8074 32-bit operations. This is the case if optimizing for size, or
8075 if we have load delay slots, or if one 32-bit part can be done with
8076 a single data operation. */
8077 bool
8078 arm_const_double_by_parts (rtx val)
8080 enum machine_mode mode = GET_MODE (val);
8081 rtx part;
8083 if (optimize_size || arm_ld_sched)
8084 return true;
8086 if (mode == VOIDmode)
8087 mode = DImode;
8089 part = gen_highpart_mode (SImode, mode, val);
8091 gcc_assert (GET_CODE (part) == CONST_INT);
8093 if (const_ok_for_arm (INTVAL (part))
8094 || const_ok_for_arm (~INTVAL (part)))
8095 return true;
8097 part = gen_lowpart (SImode, val);
8099 gcc_assert (GET_CODE (part) == CONST_INT);
8101 if (const_ok_for_arm (INTVAL (part))
8102 || const_ok_for_arm (~INTVAL (part)))
8103 return true;
8105 return false;
8108 /* Scan INSN and note any of its operands that need fixing.
8109 If DO_PUSHES is false we do not actually push any of the fixups
8110 needed. The function returns TRUE if any fixups were needed/pushed.
8111 This is used by arm_memory_load_p() which needs to know about loads
8112 of constants that will be converted into minipool loads. */
8113 static bool
8114 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
8116 bool result = false;
8117 int opno;
8119 extract_insn (insn);
8121 if (!constrain_operands (1))
8122 fatal_insn_not_found (insn);
8124 if (recog_data.n_alternatives == 0)
8125 return false;
8127 /* Fill in recog_op_alt with information about the constraints of
8128 this insn. */
8129 preprocess_constraints ();
8131 for (opno = 0; opno < recog_data.n_operands; opno++)
8133 /* Things we need to fix can only occur in inputs. */
8134 if (recog_data.operand_type[opno] != OP_IN)
8135 continue;
8137 /* If this alternative is a memory reference, then any mention
8138 of constants in this alternative is really to fool reload
8139 into allowing us to accept one there. We need to fix them up
8140 now so that we output the right code. */
8141 if (recog_op_alt[opno][which_alternative].memory_ok)
8143 rtx op = recog_data.operand[opno];
8145 if (CONSTANT_P (op))
8147 if (do_pushes)
8148 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
8149 recog_data.operand_mode[opno], op);
8150 result = true;
8152 else if (GET_CODE (op) == MEM
8153 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
8154 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
8156 if (do_pushes)
8158 rtx cop = avoid_constant_pool_reference (op);
8160 /* Casting the address of something to a mode narrower
8161 than a word can cause avoid_constant_pool_reference()
8162 to return the pool reference itself. That's no good to
8163 us here. Lets just hope that we can use the
8164 constant pool value directly. */
8165 if (op == cop)
8166 cop = get_pool_constant (XEXP (op, 0));
8168 push_minipool_fix (insn, address,
8169 recog_data.operand_loc[opno],
8170 recog_data.operand_mode[opno], cop);
8173 result = true;
8178 return result;
8181 /* Gcc puts the pool in the wrong place for ARM, since we can only
8182 load addresses a limited distance around the pc. We do some
8183 special munging to move the constant pool values to the correct
8184 point in the code. */
8185 static void
8186 arm_reorg (void)
8188 rtx insn;
8189 HOST_WIDE_INT address = 0;
8190 Mfix * fix;
8192 minipool_fix_head = minipool_fix_tail = NULL;
8194 /* The first insn must always be a note, or the code below won't
8195 scan it properly. */
8196 insn = get_insns ();
8197 gcc_assert (GET_CODE (insn) == NOTE);
8198 minipool_pad = 0;
8200 /* Scan all the insns and record the operands that will need fixing. */
8201 for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
8203 if (TARGET_CIRRUS_FIX_INVALID_INSNS
8204 && (arm_cirrus_insn_p (insn)
8205 || GET_CODE (insn) == JUMP_INSN
8206 || arm_memory_load_p (insn)))
8207 cirrus_reorg (insn);
8209 if (GET_CODE (insn) == BARRIER)
8210 push_minipool_barrier (insn, address);
8211 else if (INSN_P (insn))
8213 rtx table;
8215 note_invalid_constants (insn, address, true);
8216 address += get_attr_length (insn);
8218 /* If the insn is a vector jump, add the size of the table
8219 and skip the table. */
8220 if ((table = is_jump_table (insn)) != NULL)
8222 address += get_jump_table_size (table);
8223 insn = table;
8228 fix = minipool_fix_head;
8230 /* Now scan the fixups and perform the required changes. */
8231 while (fix)
8233 Mfix * ftmp;
8234 Mfix * fdel;
8235 Mfix * last_added_fix;
8236 Mfix * last_barrier = NULL;
8237 Mfix * this_fix;
8239 /* Skip any further barriers before the next fix. */
8240 while (fix && GET_CODE (fix->insn) == BARRIER)
8241 fix = fix->next;
8243 /* No more fixes. */
8244 if (fix == NULL)
8245 break;
8247 last_added_fix = NULL;
8249 for (ftmp = fix; ftmp; ftmp = ftmp->next)
8251 if (GET_CODE (ftmp->insn) == BARRIER)
8253 if (ftmp->address >= minipool_vector_head->max_address)
8254 break;
8256 last_barrier = ftmp;
8258 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
8259 break;
8261 last_added_fix = ftmp; /* Keep track of the last fix added. */
8264 /* If we found a barrier, drop back to that; any fixes that we
8265 could have reached but come after the barrier will now go in
8266 the next mini-pool. */
8267 if (last_barrier != NULL)
8269 /* Reduce the refcount for those fixes that won't go into this
8270 pool after all. */
8271 for (fdel = last_barrier->next;
8272 fdel && fdel != ftmp;
8273 fdel = fdel->next)
8275 fdel->minipool->refcount--;
8276 fdel->minipool = NULL;
8279 ftmp = last_barrier;
8281 else
8283 /* ftmp is first fix that we can't fit into this pool and
8284 there no natural barriers that we could use. Insert a
8285 new barrier in the code somewhere between the previous
8286 fix and this one, and arrange to jump around it. */
8287 HOST_WIDE_INT max_address;
8289 /* The last item on the list of fixes must be a barrier, so
8290 we can never run off the end of the list of fixes without
8291 last_barrier being set. */
8292 gcc_assert (ftmp);
8294 max_address = minipool_vector_head->max_address;
8295 /* Check that there isn't another fix that is in range that
8296 we couldn't fit into this pool because the pool was
8297 already too large: we need to put the pool before such an
8298 instruction. The pool itself may come just after the
8299 fix because create_fix_barrier also allows space for a
8300 jump instruction. */
8301 if (ftmp->address < max_address)
8302 max_address = ftmp->address + 1;
8304 last_barrier = create_fix_barrier (last_added_fix, max_address);
8307 assign_minipool_offsets (last_barrier);
8309 while (ftmp)
8311 if (GET_CODE (ftmp->insn) != BARRIER
8312 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
8313 == NULL))
8314 break;
8316 ftmp = ftmp->next;
8319 /* Scan over the fixes we have identified for this pool, fixing them
8320 up and adding the constants to the pool itself. */
8321 for (this_fix = fix; this_fix && ftmp != this_fix;
8322 this_fix = this_fix->next)
8323 if (GET_CODE (this_fix->insn) != BARRIER)
8325 rtx addr
8326 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
8327 minipool_vector_label),
8328 this_fix->minipool->offset);
8329 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
8332 dump_minipool (last_barrier->insn);
8333 fix = ftmp;
8336 /* From now on we must synthesize any constants that we can't handle
8337 directly. This can happen if the RTL gets split during final
8338 instruction generation. */
8339 after_arm_reorg = 1;
8341 /* Free the minipool memory. */
8342 obstack_free (&minipool_obstack, minipool_startobj);
8345 /* Routines to output assembly language. */
8347 /* If the rtx is the correct value then return the string of the number.
8348 In this way we can ensure that valid double constants are generated even
8349 when cross compiling. */
8350 const char *
8351 fp_immediate_constant (rtx x)
8353 REAL_VALUE_TYPE r;
8354 int i;
8356 if (!fp_consts_inited)
8357 init_fp_table ();
8359 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8360 for (i = 0; i < 8; i++)
8361 if (REAL_VALUES_EQUAL (r, values_fp[i]))
8362 return strings_fp[i];
8364 gcc_unreachable ();
8367 /* As for fp_immediate_constant, but value is passed directly, not in rtx. */
8368 static const char *
8369 fp_const_from_val (REAL_VALUE_TYPE *r)
8371 int i;
8373 if (!fp_consts_inited)
8374 init_fp_table ();
8376 for (i = 0; i < 8; i++)
8377 if (REAL_VALUES_EQUAL (*r, values_fp[i]))
8378 return strings_fp[i];
8380 gcc_unreachable ();
8383 /* Output the operands of a LDM/STM instruction to STREAM.
8384 MASK is the ARM register set mask of which only bits 0-15 are important.
8385 REG is the base register, either the frame pointer or the stack pointer,
8386 INSTR is the possibly suffixed load or store instruction. */
8388 static void
8389 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
8390 unsigned long mask)
8392 unsigned i;
8393 bool not_first = FALSE;
8395 fputc ('\t', stream);
8396 asm_fprintf (stream, instr, reg);
8397 fputs (", {", stream);
8399 for (i = 0; i <= LAST_ARM_REGNUM; i++)
8400 if (mask & (1 << i))
8402 if (not_first)
8403 fprintf (stream, ", ");
8405 asm_fprintf (stream, "%r", i);
8406 not_first = TRUE;
8409 fprintf (stream, "}\n");
8413 /* Output a FLDMX instruction to STREAM.
8414 BASE if the register containing the address.
8415 REG and COUNT specify the register range.
8416 Extra registers may be added to avoid hardware bugs. */
8418 static void
8419 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
8421 int i;
8423 /* Workaround ARM10 VFPr1 bug. */
8424 if (count == 2 && !arm_arch6)
8426 if (reg == 15)
8427 reg--;
8428 count++;
8431 fputc ('\t', stream);
8432 asm_fprintf (stream, "fldmfdx\t%r!, {", base);
8434 for (i = reg; i < reg + count; i++)
8436 if (i > reg)
8437 fputs (", ", stream);
8438 asm_fprintf (stream, "d%d", i);
8440 fputs ("}\n", stream);
8445 /* Output the assembly for a store multiple. */
8447 const char *
8448 vfp_output_fstmx (rtx * operands)
8450 char pattern[100];
8451 int p;
8452 int base;
8453 int i;
8455 strcpy (pattern, "fstmfdx\t%m0!, {%P1");
8456 p = strlen (pattern);
8458 gcc_assert (GET_CODE (operands[1]) == REG);
8460 base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
8461 for (i = 1; i < XVECLEN (operands[2], 0); i++)
8463 p += sprintf (&pattern[p], ", d%d", base + i);
8465 strcpy (&pattern[p], "}");
8467 output_asm_insn (pattern, operands);
8468 return "";
8472 /* Emit RTL to save block of VFP register pairs to the stack. Returns the
8473 number of bytes pushed. */
8475 static int
8476 vfp_emit_fstmx (int base_reg, int count)
8478 rtx par;
8479 rtx dwarf;
8480 rtx tmp, reg;
8481 int i;
8483 /* Workaround ARM10 VFPr1 bug. Data corruption can occur when exactly two
8484 register pairs are stored by a store multiple insn. We avoid this
8485 by pushing an extra pair. */
8486 if (count == 2 && !arm_arch6)
8488 if (base_reg == LAST_VFP_REGNUM - 3)
8489 base_reg -= 2;
8490 count++;
8493 /* ??? The frame layout is implementation defined. We describe
8494 standard format 1 (equivalent to a FSTMD insn and unused pad word).
8495 We really need some way of representing the whole block so that the
8496 unwinder can figure it out at runtime. */
8497 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8498 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
8500 reg = gen_rtx_REG (DFmode, base_reg);
8501 base_reg += 2;
8503 XVECEXP (par, 0, 0)
8504 = gen_rtx_SET (VOIDmode,
8505 gen_frame_mem (BLKmode,
8506 gen_rtx_PRE_DEC (BLKmode,
8507 stack_pointer_rtx)),
8508 gen_rtx_UNSPEC (BLKmode,
8509 gen_rtvec (1, reg),
8510 UNSPEC_PUSH_MULT));
8512 tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8513 plus_constant (stack_pointer_rtx, -(count * 8 + 4)));
8514 RTX_FRAME_RELATED_P (tmp) = 1;
8515 XVECEXP (dwarf, 0, 0) = tmp;
8517 tmp = gen_rtx_SET (VOIDmode,
8518 gen_frame_mem (DFmode, stack_pointer_rtx),
8519 reg);
8520 RTX_FRAME_RELATED_P (tmp) = 1;
8521 XVECEXP (dwarf, 0, 1) = tmp;
8523 for (i = 1; i < count; i++)
8525 reg = gen_rtx_REG (DFmode, base_reg);
8526 base_reg += 2;
8527 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8529 tmp = gen_rtx_SET (VOIDmode,
8530 gen_frame_mem (DFmode,
8531 plus_constant (stack_pointer_rtx,
8532 i * 8)),
8533 reg);
8534 RTX_FRAME_RELATED_P (tmp) = 1;
8535 XVECEXP (dwarf, 0, i + 1) = tmp;
8538 par = emit_insn (par);
8539 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8540 REG_NOTES (par));
8541 RTX_FRAME_RELATED_P (par) = 1;
8543 return count * 8 + 4;
8547 /* Output a 'call' insn. */
8548 const char *
8549 output_call (rtx *operands)
8551 gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly. */
8553 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
8554 if (REGNO (operands[0]) == LR_REGNUM)
8556 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8557 output_asm_insn ("mov%?\t%0, %|lr", operands);
8560 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8562 if (TARGET_INTERWORK || arm_arch4t)
8563 output_asm_insn ("bx%?\t%0", operands);
8564 else
8565 output_asm_insn ("mov%?\t%|pc, %0", operands);
8567 return "";
8570 /* Output a 'call' insn that is a reference in memory. */
8571 const char *
8572 output_call_mem (rtx *operands)
8574 if (TARGET_INTERWORK && !arm_arch5)
8576 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8577 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8578 output_asm_insn ("bx%?\t%|ip", operands);
8580 else if (regno_use_in (LR_REGNUM, operands[0]))
8582 /* LR is used in the memory address. We load the address in the
8583 first instruction. It's safe to use IP as the target of the
8584 load since the call will kill it anyway. */
8585 output_asm_insn ("ldr%?\t%|ip, %0", operands);
8586 if (arm_arch5)
8587 output_asm_insn ("blx%?\t%|ip", operands);
8588 else
8590 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8591 if (arm_arch4t)
8592 output_asm_insn ("bx%?\t%|ip", operands);
8593 else
8594 output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8597 else
8599 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8600 output_asm_insn ("ldr%?\t%|pc, %0", operands);
8603 return "";
8607 /* Output a move from arm registers to an fpa registers.
8608 OPERANDS[0] is an fpa register.
8609 OPERANDS[1] is the first registers of an arm register pair. */
8610 const char *
8611 output_mov_long_double_fpa_from_arm (rtx *operands)
8613 int arm_reg0 = REGNO (operands[1]);
8614 rtx ops[3];
8616 gcc_assert (arm_reg0 != IP_REGNUM);
8618 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8619 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8620 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8622 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8623 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8625 return "";
8628 /* Output a move from an fpa register to arm registers.
8629 OPERANDS[0] is the first registers of an arm register pair.
8630 OPERANDS[1] is an fpa register. */
8631 const char *
8632 output_mov_long_double_arm_from_fpa (rtx *operands)
8634 int arm_reg0 = REGNO (operands[0]);
8635 rtx ops[3];
8637 gcc_assert (arm_reg0 != IP_REGNUM);
8639 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8640 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8641 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8643 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8644 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8645 return "";
8648 /* Output a move from arm registers to arm registers of a long double
8649 OPERANDS[0] is the destination.
8650 OPERANDS[1] is the source. */
8651 const char *
8652 output_mov_long_double_arm_from_arm (rtx *operands)
8654 /* We have to be careful here because the two might overlap. */
8655 int dest_start = REGNO (operands[0]);
8656 int src_start = REGNO (operands[1]);
8657 rtx ops[2];
8658 int i;
8660 if (dest_start < src_start)
8662 for (i = 0; i < 3; i++)
8664 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8665 ops[1] = gen_rtx_REG (SImode, src_start + i);
8666 output_asm_insn ("mov%?\t%0, %1", ops);
8669 else
8671 for (i = 2; i >= 0; i--)
8673 ops[0] = gen_rtx_REG (SImode, dest_start + i);
8674 ops[1] = gen_rtx_REG (SImode, src_start + i);
8675 output_asm_insn ("mov%?\t%0, %1", ops);
8679 return "";
8683 /* Output a move from arm registers to an fpa registers.
8684 OPERANDS[0] is an fpa register.
8685 OPERANDS[1] is the first registers of an arm register pair. */
8686 const char *
8687 output_mov_double_fpa_from_arm (rtx *operands)
8689 int arm_reg0 = REGNO (operands[1]);
8690 rtx ops[2];
8692 gcc_assert (arm_reg0 != IP_REGNUM);
8694 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8695 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8696 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8697 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8698 return "";
8701 /* Output a move from an fpa register to arm registers.
8702 OPERANDS[0] is the first registers of an arm register pair.
8703 OPERANDS[1] is an fpa register. */
8704 const char *
8705 output_mov_double_arm_from_fpa (rtx *operands)
8707 int arm_reg0 = REGNO (operands[0]);
8708 rtx ops[2];
8710 gcc_assert (arm_reg0 != IP_REGNUM);
8712 ops[0] = gen_rtx_REG (SImode, arm_reg0);
8713 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8714 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8715 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8716 return "";
8719 /* Output a move between double words.
8720 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8721 or MEM<-REG and all MEMs must be offsettable addresses. */
8722 const char *
8723 output_move_double (rtx *operands)
8725 enum rtx_code code0 = GET_CODE (operands[0]);
8726 enum rtx_code code1 = GET_CODE (operands[1]);
8727 rtx otherops[3];
8729 if (code0 == REG)
8731 int reg0 = REGNO (operands[0]);
8733 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8735 gcc_assert (code1 == MEM); /* Constraints should ensure this. */
8737 switch (GET_CODE (XEXP (operands[1], 0)))
8739 case REG:
8740 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8741 break;
8743 case PRE_INC:
8744 gcc_assert (TARGET_LDRD);
8745 output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8746 break;
8748 case PRE_DEC:
8749 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8750 break;
8752 case POST_INC:
8753 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8754 break;
8756 case POST_DEC:
8757 gcc_assert (TARGET_LDRD);
8758 output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8759 break;
8761 case PRE_MODIFY:
8762 case POST_MODIFY:
8763 otherops[0] = operands[0];
8764 otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8765 otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8767 if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8769 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8771 /* Registers overlap so split out the increment. */
8772 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8773 output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8775 else
8776 output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8778 else
8780 /* We only allow constant increments, so this is safe. */
8781 output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8783 break;
8785 case LABEL_REF:
8786 case CONST:
8787 output_asm_insn ("adr%?\t%0, %1", operands);
8788 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8789 break;
8791 default:
8792 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8793 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8795 otherops[0] = operands[0];
8796 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8797 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8799 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8801 if (GET_CODE (otherops[2]) == CONST_INT)
8803 switch ((int) INTVAL (otherops[2]))
8805 case -8:
8806 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8807 return "";
8808 case -4:
8809 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8810 return "";
8811 case 4:
8812 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8813 return "";
8816 if (TARGET_LDRD
8817 && (GET_CODE (otherops[2]) == REG
8818 || (GET_CODE (otherops[2]) == CONST_INT
8819 && INTVAL (otherops[2]) > -256
8820 && INTVAL (otherops[2]) < 256)))
8822 if (reg_overlap_mentioned_p (otherops[0],
8823 otherops[2]))
8825 /* Swap base and index registers over to
8826 avoid a conflict. */
8827 otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8828 otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8830 /* If both registers conflict, it will usually
8831 have been fixed by a splitter. */
8832 if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8834 output_asm_insn ("add%?\t%1, %1, %2", otherops);
8835 output_asm_insn ("ldr%?d\t%0, [%1]",
8836 otherops);
8838 else
8839 output_asm_insn ("ldr%?d\t%0, [%1, %2]", otherops);
8840 return "";
8843 if (GET_CODE (otherops[2]) == CONST_INT)
8845 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8846 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8847 else
8848 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8850 else
8851 output_asm_insn ("add%?\t%0, %1, %2", otherops);
8853 else
8854 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8856 return "ldm%?ia\t%0, %M0";
8858 else
8860 otherops[1] = adjust_address (operands[1], SImode, 4);
8861 /* Take care of overlapping base/data reg. */
8862 if (reg_mentioned_p (operands[0], operands[1]))
8864 output_asm_insn ("ldr%?\t%0, %1", otherops);
8865 output_asm_insn ("ldr%?\t%0, %1", operands);
8867 else
8869 output_asm_insn ("ldr%?\t%0, %1", operands);
8870 output_asm_insn ("ldr%?\t%0, %1", otherops);
8875 else
8877 /* Constraints should ensure this. */
8878 gcc_assert (code0 == MEM && code1 == REG);
8879 gcc_assert (REGNO (operands[1]) != IP_REGNUM);
8881 switch (GET_CODE (XEXP (operands[0], 0)))
8883 case REG:
8884 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8885 break;
8887 case PRE_INC:
8888 gcc_assert (TARGET_LDRD);
8889 output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8890 break;
8892 case PRE_DEC:
8893 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8894 break;
8896 case POST_INC:
8897 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8898 break;
8900 case POST_DEC:
8901 gcc_assert (TARGET_LDRD);
8902 output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8903 break;
8905 case PRE_MODIFY:
8906 case POST_MODIFY:
8907 otherops[0] = operands[1];
8908 otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8909 otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8911 if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8912 output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8913 else
8914 output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8915 break;
8917 case PLUS:
8918 otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8919 if (GET_CODE (otherops[2]) == CONST_INT)
8921 switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8923 case -8:
8924 output_asm_insn ("stm%?db\t%m0, %M1", operands);
8925 return "";
8927 case -4:
8928 output_asm_insn ("stm%?da\t%m0, %M1", operands);
8929 return "";
8931 case 4:
8932 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8933 return "";
8936 if (TARGET_LDRD
8937 && (GET_CODE (otherops[2]) == REG
8938 || (GET_CODE (otherops[2]) == CONST_INT
8939 && INTVAL (otherops[2]) > -256
8940 && INTVAL (otherops[2]) < 256)))
8942 otherops[0] = operands[1];
8943 otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8944 output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8945 return "";
8947 /* Fall through */
8949 default:
8950 otherops[0] = adjust_address (operands[0], SImode, 4);
8951 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8952 output_asm_insn ("str%?\t%1, %0", operands);
8953 output_asm_insn ("str%?\t%1, %0", otherops);
8957 return "";
8960 /* Output an ADD r, s, #n where n may be too big for one instruction.
8961 If adding zero to one register, output nothing. */
8962 const char *
8963 output_add_immediate (rtx *operands)
8965 HOST_WIDE_INT n = INTVAL (operands[2]);
8967 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8969 if (n < 0)
8970 output_multi_immediate (operands,
8971 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8972 -n);
8973 else
8974 output_multi_immediate (operands,
8975 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8979 return "";
8982 /* Output a multiple immediate operation.
8983 OPERANDS is the vector of operands referred to in the output patterns.
8984 INSTR1 is the output pattern to use for the first constant.
8985 INSTR2 is the output pattern to use for subsequent constants.
8986 IMMED_OP is the index of the constant slot in OPERANDS.
8987 N is the constant value. */
8988 static const char *
8989 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8990 int immed_op, HOST_WIDE_INT n)
8992 #if HOST_BITS_PER_WIDE_INT > 32
8993 n &= 0xffffffff;
8994 #endif
8996 if (n == 0)
8998 /* Quick and easy output. */
8999 operands[immed_op] = const0_rtx;
9000 output_asm_insn (instr1, operands);
9002 else
9004 int i;
9005 const char * instr = instr1;
9007 /* Note that n is never zero here (which would give no output). */
9008 for (i = 0; i < 32; i += 2)
9010 if (n & (3 << i))
9012 operands[immed_op] = GEN_INT (n & (255 << i));
9013 output_asm_insn (instr, operands);
9014 instr = instr2;
9015 i += 6;
9020 return "";
9023 /* Return the appropriate ARM instruction for the operation code.
9024 The returned result should not be overwritten. OP is the rtx of the
9025 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
9026 was shifted. */
9027 const char *
9028 arithmetic_instr (rtx op, int shift_first_arg)
9030 switch (GET_CODE (op))
9032 case PLUS:
9033 return "add";
9035 case MINUS:
9036 return shift_first_arg ? "rsb" : "sub";
9038 case IOR:
9039 return "orr";
9041 case XOR:
9042 return "eor";
9044 case AND:
9045 return "and";
9047 default:
9048 gcc_unreachable ();
9052 /* Ensure valid constant shifts and return the appropriate shift mnemonic
9053 for the operation code. The returned result should not be overwritten.
9054 OP is the rtx code of the shift.
9055 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
9056 shift. */
9057 static const char *
9058 shift_op (rtx op, HOST_WIDE_INT *amountp)
9060 const char * mnem;
9061 enum rtx_code code = GET_CODE (op);
9063 switch (GET_CODE (XEXP (op, 1)))
9065 case REG:
9066 case SUBREG:
9067 *amountp = -1;
9068 break;
9070 case CONST_INT:
9071 *amountp = INTVAL (XEXP (op, 1));
9072 break;
9074 default:
9075 gcc_unreachable ();
9078 switch (code)
9080 case ASHIFT:
9081 mnem = "asl";
9082 break;
9084 case ASHIFTRT:
9085 mnem = "asr";
9086 break;
9088 case LSHIFTRT:
9089 mnem = "lsr";
9090 break;
9092 case ROTATE:
9093 gcc_assert (*amountp != -1);
9094 *amountp = 32 - *amountp;
9096 /* Fall through. */
9098 case ROTATERT:
9099 mnem = "ror";
9100 break;
9102 case MULT:
9103 /* We never have to worry about the amount being other than a
9104 power of 2, since this case can never be reloaded from a reg. */
9105 gcc_assert (*amountp != -1);
9106 *amountp = int_log2 (*amountp);
9107 return "asl";
9109 default:
9110 gcc_unreachable ();
9113 if (*amountp != -1)
9115 /* This is not 100% correct, but follows from the desire to merge
9116 multiplication by a power of 2 with the recognizer for a
9117 shift. >=32 is not a valid shift for "asl", so we must try and
9118 output a shift that produces the correct arithmetical result.
9119 Using lsr #32 is identical except for the fact that the carry bit
9120 is not set correctly if we set the flags; but we never use the
9121 carry bit from such an operation, so we can ignore that. */
9122 if (code == ROTATERT)
9123 /* Rotate is just modulo 32. */
9124 *amountp &= 31;
9125 else if (*amountp != (*amountp & 31))
9127 if (code == ASHIFT)
9128 mnem = "lsr";
9129 *amountp = 32;
9132 /* Shifts of 0 are no-ops. */
9133 if (*amountp == 0)
9134 return NULL;
9137 return mnem;
9140 /* Obtain the shift from the POWER of two. */
9142 static HOST_WIDE_INT
9143 int_log2 (HOST_WIDE_INT power)
9145 HOST_WIDE_INT shift = 0;
9147 while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
9149 gcc_assert (shift <= 31);
9150 shift++;
9153 return shift;
9156 /* Output a .ascii pseudo-op, keeping track of lengths. This is
9157 because /bin/as is horribly restrictive. The judgement about
9158 whether or not each character is 'printable' (and can be output as
9159 is) or not (and must be printed with an octal escape) must be made
9160 with reference to the *host* character set -- the situation is
9161 similar to that discussed in the comments above pp_c_char in
9162 c-pretty-print.c. */
9164 #define MAX_ASCII_LEN 51
9166 void
9167 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
9169 int i;
9170 int len_so_far = 0;
9172 fputs ("\t.ascii\t\"", stream);
9174 for (i = 0; i < len; i++)
9176 int c = p[i];
9178 if (len_so_far >= MAX_ASCII_LEN)
9180 fputs ("\"\n\t.ascii\t\"", stream);
9181 len_so_far = 0;
9184 if (ISPRINT (c))
9186 if (c == '\\' || c == '\"')
9188 putc ('\\', stream);
9189 len_so_far++;
9191 putc (c, stream);
9192 len_so_far++;
9194 else
9196 fprintf (stream, "\\%03o", c);
9197 len_so_far += 4;
9201 fputs ("\"\n", stream);
9204 /* Compute the register save mask for registers 0 through 12
9205 inclusive. This code is used by arm_compute_save_reg_mask. */
9207 static unsigned long
9208 arm_compute_save_reg0_reg12_mask (void)
9210 unsigned long func_type = arm_current_func_type ();
9211 unsigned long save_reg_mask = 0;
9212 unsigned int reg;
9214 if (IS_INTERRUPT (func_type))
9216 unsigned int max_reg;
9217 /* Interrupt functions must not corrupt any registers,
9218 even call clobbered ones. If this is a leaf function
9219 we can just examine the registers used by the RTL, but
9220 otherwise we have to assume that whatever function is
9221 called might clobber anything, and so we have to save
9222 all the call-clobbered registers as well. */
9223 if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
9224 /* FIQ handlers have registers r8 - r12 banked, so
9225 we only need to check r0 - r7, Normal ISRs only
9226 bank r14 and r15, so we must check up to r12.
9227 r13 is the stack pointer which is always preserved,
9228 so we do not need to consider it here. */
9229 max_reg = 7;
9230 else
9231 max_reg = 12;
9233 for (reg = 0; reg <= max_reg; reg++)
9234 if (regs_ever_live[reg]
9235 || (! current_function_is_leaf && call_used_regs [reg]))
9236 save_reg_mask |= (1 << reg);
9238 /* Also save the pic base register if necessary. */
9239 if (flag_pic
9240 && !TARGET_SINGLE_PIC_BASE
9241 && arm_pic_register != INVALID_REGNUM
9242 && current_function_uses_pic_offset_table)
9243 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9245 else
9247 /* In the normal case we only need to save those registers
9248 which are call saved and which are used by this function. */
9249 for (reg = 0; reg <= 10; reg++)
9250 if (regs_ever_live[reg] && ! call_used_regs [reg])
9251 save_reg_mask |= (1 << reg);
9253 /* Handle the frame pointer as a special case. */
9254 if (! TARGET_APCS_FRAME
9255 && ! frame_pointer_needed
9256 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
9257 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
9258 save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
9260 /* If we aren't loading the PIC register,
9261 don't stack it even though it may be live. */
9262 if (flag_pic
9263 && !TARGET_SINGLE_PIC_BASE
9264 && arm_pic_register != INVALID_REGNUM
9265 && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
9266 || current_function_uses_pic_offset_table))
9267 save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9270 /* Save registers so the exception handler can modify them. */
9271 if (current_function_calls_eh_return)
9273 unsigned int i;
9275 for (i = 0; ; i++)
9277 reg = EH_RETURN_DATA_REGNO (i);
9278 if (reg == INVALID_REGNUM)
9279 break;
9280 save_reg_mask |= 1 << reg;
9284 return save_reg_mask;
9287 /* Compute a bit mask of which registers need to be
9288 saved on the stack for the current function. */
9290 static unsigned long
9291 arm_compute_save_reg_mask (void)
9293 unsigned int save_reg_mask = 0;
9294 unsigned long func_type = arm_current_func_type ();
9296 if (IS_NAKED (func_type))
9297 /* This should never really happen. */
9298 return 0;
9300 /* If we are creating a stack frame, then we must save the frame pointer,
9301 IP (which will hold the old stack pointer), LR and the PC. */
9302 if (frame_pointer_needed)
9303 save_reg_mask |=
9304 (1 << ARM_HARD_FRAME_POINTER_REGNUM)
9305 | (1 << IP_REGNUM)
9306 | (1 << LR_REGNUM)
9307 | (1 << PC_REGNUM);
9309 /* Volatile functions do not return, so there
9310 is no need to save any other registers. */
9311 if (IS_VOLATILE (func_type))
9312 return save_reg_mask;
9314 save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
9316 /* Decide if we need to save the link register.
9317 Interrupt routines have their own banked link register,
9318 so they never need to save it.
9319 Otherwise if we do not use the link register we do not need to save
9320 it. If we are pushing other registers onto the stack however, we
9321 can save an instruction in the epilogue by pushing the link register
9322 now and then popping it back into the PC. This incurs extra memory
9323 accesses though, so we only do it when optimizing for size, and only
9324 if we know that we will not need a fancy return sequence. */
9325 if (regs_ever_live [LR_REGNUM]
9326 || (save_reg_mask
9327 && optimize_size
9328 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9329 && !current_function_calls_eh_return))
9330 save_reg_mask |= 1 << LR_REGNUM;
9332 if (cfun->machine->lr_save_eliminated)
9333 save_reg_mask &= ~ (1 << LR_REGNUM);
9335 if (TARGET_REALLY_IWMMXT
9336 && ((bit_count (save_reg_mask)
9337 + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
9339 unsigned int reg;
9341 /* The total number of registers that are going to be pushed
9342 onto the stack is odd. We need to ensure that the stack
9343 is 64-bit aligned before we start to save iWMMXt registers,
9344 and also before we start to create locals. (A local variable
9345 might be a double or long long which we will load/store using
9346 an iWMMXt instruction). Therefore we need to push another
9347 ARM register, so that the stack will be 64-bit aligned. We
9348 try to avoid using the arg registers (r0 -r3) as they might be
9349 used to pass values in a tail call. */
9350 for (reg = 4; reg <= 12; reg++)
9351 if ((save_reg_mask & (1 << reg)) == 0)
9352 break;
9354 if (reg <= 12)
9355 save_reg_mask |= (1 << reg);
9356 else
9358 cfun->machine->sibcall_blocked = 1;
9359 save_reg_mask |= (1 << 3);
9363 return save_reg_mask;
9367 /* Compute a bit mask of which registers need to be
9368 saved on the stack for the current function. */
9369 static unsigned long
9370 thumb_compute_save_reg_mask (void)
9372 unsigned long mask;
9373 unsigned reg;
9375 mask = 0;
9376 for (reg = 0; reg < 12; reg ++)
9377 if (regs_ever_live[reg] && !call_used_regs[reg])
9378 mask |= 1 << reg;
9380 if (flag_pic
9381 && !TARGET_SINGLE_PIC_BASE
9382 && arm_pic_register != INVALID_REGNUM
9383 && current_function_uses_pic_offset_table)
9384 mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9386 /* See if we might need r11 for calls to _interwork_r11_call_via_rN(). */
9387 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
9388 mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
9390 /* LR will also be pushed if any lo regs are pushed. */
9391 if (mask & 0xff || thumb_force_lr_save ())
9392 mask |= (1 << LR_REGNUM);
9394 /* Make sure we have a low work register if we need one.
9395 We will need one if we are going to push a high register,
9396 but we are not currently intending to push a low register. */
9397 if ((mask & 0xff) == 0
9398 && ((mask & 0x0f00) || TARGET_BACKTRACE))
9400 /* Use thumb_find_work_register to choose which register
9401 we will use. If the register is live then we will
9402 have to push it. Use LAST_LO_REGNUM as our fallback
9403 choice for the register to select. */
9404 reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
9406 if (! call_used_regs[reg])
9407 mask |= 1 << reg;
9410 return mask;
9414 /* Return the number of bytes required to save VFP registers. */
9415 static int
9416 arm_get_vfp_saved_size (void)
9418 unsigned int regno;
9419 int count;
9420 int saved;
9422 saved = 0;
9423 /* Space for saved VFP registers. */
9424 if (TARGET_HARD_FLOAT && TARGET_VFP)
9426 count = 0;
9427 for (regno = FIRST_VFP_REGNUM;
9428 regno < LAST_VFP_REGNUM;
9429 regno += 2)
9431 if ((!regs_ever_live[regno] || call_used_regs[regno])
9432 && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
9434 if (count > 0)
9436 /* Workaround ARM10 VFPr1 bug. */
9437 if (count == 2 && !arm_arch6)
9438 count++;
9439 saved += count * 8 + 4;
9441 count = 0;
9443 else
9444 count++;
9446 if (count > 0)
9448 if (count == 2 && !arm_arch6)
9449 count++;
9450 saved += count * 8 + 4;
9453 return saved;
9457 /* Generate a function exit sequence. If REALLY_RETURN is false, then do
9458 everything bar the final return instruction. */
9459 const char *
9460 output_return_instruction (rtx operand, int really_return, int reverse)
9462 char conditional[10];
9463 char instr[100];
9464 unsigned reg;
9465 unsigned long live_regs_mask;
9466 unsigned long func_type;
9467 arm_stack_offsets *offsets;
9469 func_type = arm_current_func_type ();
9471 if (IS_NAKED (func_type))
9472 return "";
9474 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9476 /* If this function was declared non-returning, and we have
9477 found a tail call, then we have to trust that the called
9478 function won't return. */
9479 if (really_return)
9481 rtx ops[2];
9483 /* Otherwise, trap an attempted return by aborting. */
9484 ops[0] = operand;
9485 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
9486 : "abort");
9487 assemble_external_libcall (ops[1]);
9488 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9491 return "";
9494 gcc_assert (!current_function_calls_alloca || really_return);
9496 sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9498 return_used_this_function = 1;
9500 live_regs_mask = arm_compute_save_reg_mask ();
9502 if (live_regs_mask)
9504 const char * return_reg;
9506 /* If we do not have any special requirements for function exit
9507 (e.g. interworking, or ISR) then we can load the return address
9508 directly into the PC. Otherwise we must load it into LR. */
9509 if (really_return
9510 && ! TARGET_INTERWORK)
9511 return_reg = reg_names[PC_REGNUM];
9512 else
9513 return_reg = reg_names[LR_REGNUM];
9515 if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9517 /* There are three possible reasons for the IP register
9518 being saved. 1) a stack frame was created, in which case
9519 IP contains the old stack pointer, or 2) an ISR routine
9520 corrupted it, or 3) it was saved to align the stack on
9521 iWMMXt. In case 1, restore IP into SP, otherwise just
9522 restore IP. */
9523 if (frame_pointer_needed)
9525 live_regs_mask &= ~ (1 << IP_REGNUM);
9526 live_regs_mask |= (1 << SP_REGNUM);
9528 else
9529 gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
9532 /* On some ARM architectures it is faster to use LDR rather than
9533 LDM to load a single register. On other architectures, the
9534 cost is the same. In 26 bit mode, or for exception handlers,
9535 we have to use LDM to load the PC so that the CPSR is also
9536 restored. */
9537 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9538 if (live_regs_mask == (1U << reg))
9539 break;
9541 if (reg <= LAST_ARM_REGNUM
9542 && (reg != LR_REGNUM
9543 || ! really_return
9544 || ! IS_INTERRUPT (func_type)))
9546 sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
9547 (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9549 else
9551 char *p;
9552 int first = 1;
9554 /* Generate the load multiple instruction to restore the
9555 registers. Note we can get here, even if
9556 frame_pointer_needed is true, but only if sp already
9557 points to the base of the saved core registers. */
9558 if (live_regs_mask & (1 << SP_REGNUM))
9560 unsigned HOST_WIDE_INT stack_adjust;
9562 offsets = arm_get_frame_offsets ();
9563 stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9564 gcc_assert (stack_adjust == 0 || stack_adjust == 4);
9566 if (stack_adjust && arm_arch5)
9567 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9568 else
9570 /* If we can't use ldmib (SA110 bug),
9571 then try to pop r3 instead. */
9572 if (stack_adjust)
9573 live_regs_mask |= 1 << 3;
9574 sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9577 else
9578 sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9580 p = instr + strlen (instr);
9582 for (reg = 0; reg <= SP_REGNUM; reg++)
9583 if (live_regs_mask & (1 << reg))
9585 int l = strlen (reg_names[reg]);
9587 if (first)
9588 first = 0;
9589 else
9591 memcpy (p, ", ", 2);
9592 p += 2;
9595 memcpy (p, "%|", 2);
9596 memcpy (p + 2, reg_names[reg], l);
9597 p += l + 2;
9600 if (live_regs_mask & (1 << LR_REGNUM))
9602 sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9603 /* If returning from an interrupt, restore the CPSR. */
9604 if (IS_INTERRUPT (func_type))
9605 strcat (p, "^");
9607 else
9608 strcpy (p, "}");
9611 output_asm_insn (instr, & operand);
9613 /* See if we need to generate an extra instruction to
9614 perform the actual function return. */
9615 if (really_return
9616 && func_type != ARM_FT_INTERWORKED
9617 && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9619 /* The return has already been handled
9620 by loading the LR into the PC. */
9621 really_return = 0;
9625 if (really_return)
9627 switch ((int) ARM_FUNC_TYPE (func_type))
9629 case ARM_FT_ISR:
9630 case ARM_FT_FIQ:
9631 sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9632 break;
9634 case ARM_FT_INTERWORKED:
9635 sprintf (instr, "bx%s\t%%|lr", conditional);
9636 break;
9638 case ARM_FT_EXCEPTION:
9639 sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9640 break;
9642 default:
9643 /* Use bx if it's available. */
9644 if (arm_arch5 || arm_arch4t)
9645 sprintf (instr, "bx%s\t%%|lr", conditional);
9646 else
9647 sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9648 break;
9651 output_asm_insn (instr, & operand);
9654 return "";
9657 /* Write the function name into the code section, directly preceding
9658 the function prologue.
9660 Code will be output similar to this:
9662 .ascii "arm_poke_function_name", 0
9663 .align
9665 .word 0xff000000 + (t1 - t0)
9666 arm_poke_function_name
9667 mov ip, sp
9668 stmfd sp!, {fp, ip, lr, pc}
9669 sub fp, ip, #4
9671 When performing a stack backtrace, code can inspect the value
9672 of 'pc' stored at 'fp' + 0. If the trace function then looks
9673 at location pc - 12 and the top 8 bits are set, then we know
9674 that there is a function name embedded immediately preceding this
9675 location and has length ((pc[-3]) & 0xff000000).
9677 We assume that pc is declared as a pointer to an unsigned long.
9679 It is of no benefit to output the function name if we are assembling
9680 a leaf function. These function types will not contain a stack
9681 backtrace structure, therefore it is not possible to determine the
9682 function name. */
9683 void
9684 arm_poke_function_name (FILE *stream, const char *name)
9686 unsigned long alignlength;
9687 unsigned long length;
9688 rtx x;
9690 length = strlen (name) + 1;
9691 alignlength = ROUND_UP_WORD (length);
9693 ASM_OUTPUT_ASCII (stream, name, length);
9694 ASM_OUTPUT_ALIGN (stream, 2);
9695 x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9696 assemble_aligned_integer (UNITS_PER_WORD, x);
9699 /* Place some comments into the assembler stream
9700 describing the current function. */
9701 static void
9702 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9704 unsigned long func_type;
9706 if (!TARGET_ARM)
9708 thumb_output_function_prologue (f, frame_size);
9709 return;
9712 /* Sanity check. */
9713 gcc_assert (!arm_ccfsm_state && !arm_target_insn);
9715 func_type = arm_current_func_type ();
9717 switch ((int) ARM_FUNC_TYPE (func_type))
9719 default:
9720 case ARM_FT_NORMAL:
9721 break;
9722 case ARM_FT_INTERWORKED:
9723 asm_fprintf (f, "\t%@ Function supports interworking.\n");
9724 break;
9725 case ARM_FT_ISR:
9726 asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9727 break;
9728 case ARM_FT_FIQ:
9729 asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9730 break;
9731 case ARM_FT_EXCEPTION:
9732 asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9733 break;
9736 if (IS_NAKED (func_type))
9737 asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9739 if (IS_VOLATILE (func_type))
9740 asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9742 if (IS_NESTED (func_type))
9743 asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9745 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9746 current_function_args_size,
9747 current_function_pretend_args_size, frame_size);
9749 asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9750 frame_pointer_needed,
9751 cfun->machine->uses_anonymous_args);
9753 if (cfun->machine->lr_save_eliminated)
9754 asm_fprintf (f, "\t%@ link register save eliminated.\n");
9756 if (current_function_calls_eh_return)
9757 asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9759 #ifdef AOF_ASSEMBLER
9760 if (flag_pic)
9761 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9762 #endif
9764 return_used_this_function = 0;
9767 const char *
9768 arm_output_epilogue (rtx sibling)
9770 int reg;
9771 unsigned long saved_regs_mask;
9772 unsigned long func_type;
9773 /* Floats_offset is the offset from the "virtual" frame. In an APCS
9774 frame that is $fp + 4 for a non-variadic function. */
9775 int floats_offset = 0;
9776 rtx operands[3];
9777 FILE * f = asm_out_file;
9778 unsigned int lrm_count = 0;
9779 int really_return = (sibling == NULL);
9780 int start_reg;
9781 arm_stack_offsets *offsets;
9783 /* If we have already generated the return instruction
9784 then it is futile to generate anything else. */
9785 if (use_return_insn (FALSE, sibling) && return_used_this_function)
9786 return "";
9788 func_type = arm_current_func_type ();
9790 if (IS_NAKED (func_type))
9791 /* Naked functions don't have epilogues. */
9792 return "";
9794 if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9796 rtx op;
9798 /* A volatile function should never return. Call abort. */
9799 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9800 assemble_external_libcall (op);
9801 output_asm_insn ("bl\t%a0", &op);
9803 return "";
9806 /* If we are throwing an exception, then we really must be doing a
9807 return, so we can't tail-call. */
9808 gcc_assert (!current_function_calls_eh_return || really_return);
9810 offsets = arm_get_frame_offsets ();
9811 saved_regs_mask = arm_compute_save_reg_mask ();
9813 if (TARGET_IWMMXT)
9814 lrm_count = bit_count (saved_regs_mask);
9816 floats_offset = offsets->saved_args;
9817 /* Compute how far away the floats will be. */
9818 for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9819 if (saved_regs_mask & (1 << reg))
9820 floats_offset += 4;
9822 if (frame_pointer_needed)
9824 /* This variable is for the Virtual Frame Pointer, not VFP regs. */
9825 int vfp_offset = offsets->frame;
9827 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9829 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9830 if (regs_ever_live[reg] && !call_used_regs[reg])
9832 floats_offset += 12;
9833 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9834 reg, FP_REGNUM, floats_offset - vfp_offset);
9837 else
9839 start_reg = LAST_FPA_REGNUM;
9841 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9843 if (regs_ever_live[reg] && !call_used_regs[reg])
9845 floats_offset += 12;
9847 /* We can't unstack more than four registers at once. */
9848 if (start_reg - reg == 3)
9850 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9851 reg, FP_REGNUM, floats_offset - vfp_offset);
9852 start_reg = reg - 1;
9855 else
9857 if (reg != start_reg)
9858 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9859 reg + 1, start_reg - reg,
9860 FP_REGNUM, floats_offset - vfp_offset);
9861 start_reg = reg - 1;
9865 /* Just in case the last register checked also needs unstacking. */
9866 if (reg != start_reg)
9867 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9868 reg + 1, start_reg - reg,
9869 FP_REGNUM, floats_offset - vfp_offset);
9872 if (TARGET_HARD_FLOAT && TARGET_VFP)
9874 int saved_size;
9876 /* The fldmx insn does not have base+offset addressing modes,
9877 so we use IP to hold the address. */
9878 saved_size = arm_get_vfp_saved_size ();
9880 if (saved_size > 0)
9882 floats_offset += saved_size;
9883 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9884 FP_REGNUM, floats_offset - vfp_offset);
9886 start_reg = FIRST_VFP_REGNUM;
9887 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9889 if ((!regs_ever_live[reg] || call_used_regs[reg])
9890 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9892 if (start_reg != reg)
9893 arm_output_fldmx (f, IP_REGNUM,
9894 (start_reg - FIRST_VFP_REGNUM) / 2,
9895 (reg - start_reg) / 2);
9896 start_reg = reg + 2;
9899 if (start_reg != reg)
9900 arm_output_fldmx (f, IP_REGNUM,
9901 (start_reg - FIRST_VFP_REGNUM) / 2,
9902 (reg - start_reg) / 2);
9905 if (TARGET_IWMMXT)
9907 /* The frame pointer is guaranteed to be non-double-word aligned.
9908 This is because it is set to (old_stack_pointer - 4) and the
9909 old_stack_pointer was double word aligned. Thus the offset to
9910 the iWMMXt registers to be loaded must also be non-double-word
9911 sized, so that the resultant address *is* double-word aligned.
9912 We can ignore floats_offset since that was already included in
9913 the live_regs_mask. */
9914 lrm_count += (lrm_count % 2 ? 2 : 1);
9916 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9917 if (regs_ever_live[reg] && !call_used_regs[reg])
9919 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9920 reg, FP_REGNUM, lrm_count * 4);
9921 lrm_count += 2;
9925 /* saved_regs_mask should contain the IP, which at the time of stack
9926 frame generation actually contains the old stack pointer. So a
9927 quick way to unwind the stack is just pop the IP register directly
9928 into the stack pointer. */
9929 gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
9930 saved_regs_mask &= ~ (1 << IP_REGNUM);
9931 saved_regs_mask |= (1 << SP_REGNUM);
9933 /* There are two registers left in saved_regs_mask - LR and PC. We
9934 only need to restore the LR register (the return address), but to
9935 save time we can load it directly into the PC, unless we need a
9936 special function exit sequence, or we are not really returning. */
9937 if (really_return
9938 && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9939 && !current_function_calls_eh_return)
9940 /* Delete the LR from the register mask, so that the LR on
9941 the stack is loaded into the PC in the register mask. */
9942 saved_regs_mask &= ~ (1 << LR_REGNUM);
9943 else
9944 saved_regs_mask &= ~ (1 << PC_REGNUM);
9946 /* We must use SP as the base register, because SP is one of the
9947 registers being restored. If an interrupt or page fault
9948 happens in the ldm instruction, the SP might or might not
9949 have been restored. That would be bad, as then SP will no
9950 longer indicate the safe area of stack, and we can get stack
9951 corruption. Using SP as the base register means that it will
9952 be reset correctly to the original value, should an interrupt
9953 occur. If the stack pointer already points at the right
9954 place, then omit the subtraction. */
9955 if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9956 || current_function_calls_alloca)
9957 asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9958 4 * bit_count (saved_regs_mask));
9959 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9961 if (IS_INTERRUPT (func_type))
9962 /* Interrupt handlers will have pushed the
9963 IP onto the stack, so restore it now. */
9964 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9966 else
9968 /* Restore stack pointer if necessary. */
9969 if (offsets->outgoing_args != offsets->saved_regs)
9971 operands[0] = operands[1] = stack_pointer_rtx;
9972 operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9973 output_add_immediate (operands);
9976 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9978 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9979 if (regs_ever_live[reg] && !call_used_regs[reg])
9980 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9981 reg, SP_REGNUM);
9983 else
9985 start_reg = FIRST_FPA_REGNUM;
9987 for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9989 if (regs_ever_live[reg] && !call_used_regs[reg])
9991 if (reg - start_reg == 3)
9993 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9994 start_reg, SP_REGNUM);
9995 start_reg = reg + 1;
9998 else
10000 if (reg != start_reg)
10001 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
10002 start_reg, reg - start_reg,
10003 SP_REGNUM);
10005 start_reg = reg + 1;
10009 /* Just in case the last register checked also needs unstacking. */
10010 if (reg != start_reg)
10011 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
10012 start_reg, reg - start_reg, SP_REGNUM);
10015 if (TARGET_HARD_FLOAT && TARGET_VFP)
10017 start_reg = FIRST_VFP_REGNUM;
10018 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10020 if ((!regs_ever_live[reg] || call_used_regs[reg])
10021 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10023 if (start_reg != reg)
10024 arm_output_fldmx (f, SP_REGNUM,
10025 (start_reg - FIRST_VFP_REGNUM) / 2,
10026 (reg - start_reg) / 2);
10027 start_reg = reg + 2;
10030 if (start_reg != reg)
10031 arm_output_fldmx (f, SP_REGNUM,
10032 (start_reg - FIRST_VFP_REGNUM) / 2,
10033 (reg - start_reg) / 2);
10035 if (TARGET_IWMMXT)
10036 for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
10037 if (regs_ever_live[reg] && !call_used_regs[reg])
10038 asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
10040 /* If we can, restore the LR into the PC. */
10041 if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
10042 && really_return
10043 && current_function_pretend_args_size == 0
10044 && saved_regs_mask & (1 << LR_REGNUM)
10045 && !current_function_calls_eh_return)
10047 saved_regs_mask &= ~ (1 << LR_REGNUM);
10048 saved_regs_mask |= (1 << PC_REGNUM);
10051 /* Load the registers off the stack. If we only have one register
10052 to load use the LDR instruction - it is faster. */
10053 if (saved_regs_mask == (1 << LR_REGNUM))
10055 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
10057 else if (saved_regs_mask)
10059 if (saved_regs_mask & (1 << SP_REGNUM))
10060 /* Note - write back to the stack register is not enabled
10061 (i.e. "ldmfd sp!..."). We know that the stack pointer is
10062 in the list of registers and if we add writeback the
10063 instruction becomes UNPREDICTABLE. */
10064 print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
10065 else
10066 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
10069 if (current_function_pretend_args_size)
10071 /* Unwind the pre-pushed regs. */
10072 operands[0] = operands[1] = stack_pointer_rtx;
10073 operands[2] = GEN_INT (current_function_pretend_args_size);
10074 output_add_immediate (operands);
10078 /* We may have already restored PC directly from the stack. */
10079 if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
10080 return "";
10082 /* Stack adjustment for exception handler. */
10083 if (current_function_calls_eh_return)
10084 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
10085 ARM_EH_STACKADJ_REGNUM);
10087 /* Generate the return instruction. */
10088 switch ((int) ARM_FUNC_TYPE (func_type))
10090 case ARM_FT_ISR:
10091 case ARM_FT_FIQ:
10092 asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
10093 break;
10095 case ARM_FT_EXCEPTION:
10096 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10097 break;
10099 case ARM_FT_INTERWORKED:
10100 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10101 break;
10103 default:
10104 if (arm_arch5 || arm_arch4t)
10105 asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10106 else
10107 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10108 break;
10111 return "";
10114 static void
10115 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10116 HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
10118 arm_stack_offsets *offsets;
10120 if (TARGET_THUMB)
10122 int regno;
10124 /* Emit any call-via-reg trampolines that are needed for v4t support
10125 of call_reg and call_value_reg type insns. */
10126 for (regno = 0; regno < LR_REGNUM; regno++)
10128 rtx label = cfun->machine->call_via[regno];
10130 if (label != NULL)
10132 switch_to_section (function_section (current_function_decl));
10133 targetm.asm_out.internal_label (asm_out_file, "L",
10134 CODE_LABEL_NUMBER (label));
10135 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
10139 /* ??? Probably not safe to set this here, since it assumes that a
10140 function will be emitted as assembly immediately after we generate
10141 RTL for it. This does not happen for inline functions. */
10142 return_used_this_function = 0;
10144 else
10146 /* We need to take into account any stack-frame rounding. */
10147 offsets = arm_get_frame_offsets ();
10149 gcc_assert (!use_return_insn (FALSE, NULL)
10150 || !return_used_this_function
10151 || offsets->saved_regs == offsets->outgoing_args
10152 || frame_pointer_needed);
10154 /* Reset the ARM-specific per-function variables. */
10155 after_arm_reorg = 0;
10159 /* Generate and emit an insn that we will recognize as a push_multi.
10160 Unfortunately, since this insn does not reflect very well the actual
10161 semantics of the operation, we need to annotate the insn for the benefit
10162 of DWARF2 frame unwind information. */
10163 static rtx
10164 emit_multi_reg_push (unsigned long mask)
10166 int num_regs = 0;
10167 int num_dwarf_regs;
10168 int i, j;
10169 rtx par;
10170 rtx dwarf;
10171 int dwarf_par_index;
10172 rtx tmp, reg;
10174 for (i = 0; i <= LAST_ARM_REGNUM; i++)
10175 if (mask & (1 << i))
10176 num_regs++;
10178 gcc_assert (num_regs && num_regs <= 16);
10180 /* We don't record the PC in the dwarf frame information. */
10181 num_dwarf_regs = num_regs;
10182 if (mask & (1 << PC_REGNUM))
10183 num_dwarf_regs--;
10185 /* For the body of the insn we are going to generate an UNSPEC in
10186 parallel with several USEs. This allows the insn to be recognized
10187 by the push_multi pattern in the arm.md file. The insn looks
10188 something like this:
10190 (parallel [
10191 (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
10192 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
10193 (use (reg:SI 11 fp))
10194 (use (reg:SI 12 ip))
10195 (use (reg:SI 14 lr))
10196 (use (reg:SI 15 pc))
10199 For the frame note however, we try to be more explicit and actually
10200 show each register being stored into the stack frame, plus a (single)
10201 decrement of the stack pointer. We do it this way in order to be
10202 friendly to the stack unwinding code, which only wants to see a single
10203 stack decrement per instruction. The RTL we generate for the note looks
10204 something like this:
10206 (sequence [
10207 (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
10208 (set (mem:SI (reg:SI sp)) (reg:SI r4))
10209 (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
10210 (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
10211 (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
10214 This sequence is used both by the code to support stack unwinding for
10215 exceptions handlers and the code to generate dwarf2 frame debugging. */
10217 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
10218 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
10219 dwarf_par_index = 1;
10221 for (i = 0; i <= LAST_ARM_REGNUM; i++)
10223 if (mask & (1 << i))
10225 reg = gen_rtx_REG (SImode, i);
10227 XVECEXP (par, 0, 0)
10228 = gen_rtx_SET (VOIDmode,
10229 gen_frame_mem (BLKmode,
10230 gen_rtx_PRE_DEC (BLKmode,
10231 stack_pointer_rtx)),
10232 gen_rtx_UNSPEC (BLKmode,
10233 gen_rtvec (1, reg),
10234 UNSPEC_PUSH_MULT));
10236 if (i != PC_REGNUM)
10238 tmp = gen_rtx_SET (VOIDmode,
10239 gen_frame_mem (SImode, stack_pointer_rtx),
10240 reg);
10241 RTX_FRAME_RELATED_P (tmp) = 1;
10242 XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
10243 dwarf_par_index++;
10246 break;
10250 for (j = 1, i++; j < num_regs; i++)
10252 if (mask & (1 << i))
10254 reg = gen_rtx_REG (SImode, i);
10256 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
10258 if (i != PC_REGNUM)
10261 = gen_rtx_SET (VOIDmode,
10262 gen_frame_mem (SImode,
10263 plus_constant (stack_pointer_rtx,
10264 4 * j)),
10265 reg);
10266 RTX_FRAME_RELATED_P (tmp) = 1;
10267 XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
10270 j++;
10274 par = emit_insn (par);
10276 tmp = gen_rtx_SET (VOIDmode,
10277 stack_pointer_rtx,
10278 plus_constant (stack_pointer_rtx, -4 * num_regs));
10279 RTX_FRAME_RELATED_P (tmp) = 1;
10280 XVECEXP (dwarf, 0, 0) = tmp;
10282 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10283 REG_NOTES (par));
10284 return par;
10287 /* Calculate the size of the return value that is passed in registers. */
10288 static int
10289 arm_size_return_regs (void)
10291 enum machine_mode mode;
10293 if (current_function_return_rtx != 0)
10294 mode = GET_MODE (current_function_return_rtx);
10295 else
10296 mode = DECL_MODE (DECL_RESULT (current_function_decl));
10298 return GET_MODE_SIZE (mode);
10301 static rtx
10302 emit_sfm (int base_reg, int count)
10304 rtx par;
10305 rtx dwarf;
10306 rtx tmp, reg;
10307 int i;
10309 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
10310 dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
10312 reg = gen_rtx_REG (XFmode, base_reg++);
10314 XVECEXP (par, 0, 0)
10315 = gen_rtx_SET (VOIDmode,
10316 gen_frame_mem (BLKmode,
10317 gen_rtx_PRE_DEC (BLKmode,
10318 stack_pointer_rtx)),
10319 gen_rtx_UNSPEC (BLKmode,
10320 gen_rtvec (1, reg),
10321 UNSPEC_PUSH_MULT));
10322 tmp = gen_rtx_SET (VOIDmode,
10323 gen_frame_mem (XFmode, stack_pointer_rtx), reg);
10324 RTX_FRAME_RELATED_P (tmp) = 1;
10325 XVECEXP (dwarf, 0, 1) = tmp;
10327 for (i = 1; i < count; i++)
10329 reg = gen_rtx_REG (XFmode, base_reg++);
10330 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
10332 tmp = gen_rtx_SET (VOIDmode,
10333 gen_frame_mem (XFmode,
10334 plus_constant (stack_pointer_rtx,
10335 i * 12)),
10336 reg);
10337 RTX_FRAME_RELATED_P (tmp) = 1;
10338 XVECEXP (dwarf, 0, i + 1) = tmp;
10341 tmp = gen_rtx_SET (VOIDmode,
10342 stack_pointer_rtx,
10343 plus_constant (stack_pointer_rtx, -12 * count));
10345 RTX_FRAME_RELATED_P (tmp) = 1;
10346 XVECEXP (dwarf, 0, 0) = tmp;
10348 par = emit_insn (par);
10349 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10350 REG_NOTES (par));
10351 return par;
10355 /* Return true if the current function needs to save/restore LR. */
10357 static bool
10358 thumb_force_lr_save (void)
10360 return !cfun->machine->lr_save_eliminated
10361 && (!leaf_function_p ()
10362 || thumb_far_jump_used_p ()
10363 || regs_ever_live [LR_REGNUM]);
10367 /* Compute the distance from register FROM to register TO.
10368 These can be the arg pointer (26), the soft frame pointer (25),
10369 the stack pointer (13) or the hard frame pointer (11).
10370 In thumb mode r7 is used as the soft frame pointer, if needed.
10371 Typical stack layout looks like this:
10373 old stack pointer -> | |
10374 ----
10375 | | \
10376 | | saved arguments for
10377 | | vararg functions
10378 | | /
10380 hard FP & arg pointer -> | | \
10381 | | stack
10382 | | frame
10383 | | /
10385 | | \
10386 | | call saved
10387 | | registers
10388 soft frame pointer -> | | /
10390 | | \
10391 | | local
10392 | | variables
10393 locals base pointer -> | | /
10395 | | \
10396 | | outgoing
10397 | | arguments
10398 current stack pointer -> | | /
10401 For a given function some or all of these stack components
10402 may not be needed, giving rise to the possibility of
10403 eliminating some of the registers.
10405 The values returned by this function must reflect the behavior
10406 of arm_expand_prologue() and arm_compute_save_reg_mask().
10408 The sign of the number returned reflects the direction of stack
10409 growth, so the values are positive for all eliminations except
10410 from the soft frame pointer to the hard frame pointer.
10412 SFP may point just inside the local variables block to ensure correct
10413 alignment. */
10416 /* Calculate stack offsets. These are used to calculate register elimination
10417 offsets and in prologue/epilogue code. */
10419 static arm_stack_offsets *
10420 arm_get_frame_offsets (void)
10422 struct arm_stack_offsets *offsets;
10423 unsigned long func_type;
10424 int leaf;
10425 int saved;
10426 HOST_WIDE_INT frame_size;
10428 offsets = &cfun->machine->stack_offsets;
10430 /* We need to know if we are a leaf function. Unfortunately, it
10431 is possible to be called after start_sequence has been called,
10432 which causes get_insns to return the insns for the sequence,
10433 not the function, which will cause leaf_function_p to return
10434 the incorrect result.
10436 to know about leaf functions once reload has completed, and the
10437 frame size cannot be changed after that time, so we can safely
10438 use the cached value. */
10440 if (reload_completed)
10441 return offsets;
10443 /* Initially this is the size of the local variables. It will translated
10444 into an offset once we have determined the size of preceding data. */
10445 frame_size = ROUND_UP_WORD (get_frame_size ());
10447 leaf = leaf_function_p ();
10449 /* Space for variadic functions. */
10450 offsets->saved_args = current_function_pretend_args_size;
10452 offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10454 if (TARGET_ARM)
10456 unsigned int regno;
10458 saved = bit_count (arm_compute_save_reg_mask ()) * 4;
10460 /* We know that SP will be doubleword aligned on entry, and we must
10461 preserve that condition at any subroutine call. We also require the
10462 soft frame pointer to be doubleword aligned. */
10464 if (TARGET_REALLY_IWMMXT)
10466 /* Check for the call-saved iWMMXt registers. */
10467 for (regno = FIRST_IWMMXT_REGNUM;
10468 regno <= LAST_IWMMXT_REGNUM;
10469 regno++)
10470 if (regs_ever_live [regno] && ! call_used_regs [regno])
10471 saved += 8;
10474 func_type = arm_current_func_type ();
10475 if (! IS_VOLATILE (func_type))
10477 /* Space for saved FPA registers. */
10478 for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10479 if (regs_ever_live[regno] && ! call_used_regs[regno])
10480 saved += 12;
10482 /* Space for saved VFP registers. */
10483 if (TARGET_HARD_FLOAT && TARGET_VFP)
10484 saved += arm_get_vfp_saved_size ();
10487 else /* TARGET_THUMB */
10489 saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
10490 if (TARGET_BACKTRACE)
10491 saved += 16;
10494 /* Saved registers include the stack frame. */
10495 offsets->saved_regs = offsets->saved_args + saved;
10496 offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
10497 /* A leaf function does not need any stack alignment if it has nothing
10498 on the stack. */
10499 if (leaf && frame_size == 0)
10501 offsets->outgoing_args = offsets->soft_frame;
10502 return offsets;
10505 /* Ensure SFP has the correct alignment. */
10506 if (ARM_DOUBLEWORD_ALIGN
10507 && (offsets->soft_frame & 7))
10508 offsets->soft_frame += 4;
10510 offsets->locals_base = offsets->soft_frame + frame_size;
10511 offsets->outgoing_args = (offsets->locals_base
10512 + current_function_outgoing_args_size);
10514 if (ARM_DOUBLEWORD_ALIGN)
10516 /* Ensure SP remains doubleword aligned. */
10517 if (offsets->outgoing_args & 7)
10518 offsets->outgoing_args += 4;
10519 gcc_assert (!(offsets->outgoing_args & 7));
10522 return offsets;
10526 /* Calculate the relative offsets for the different stack pointers. Positive
10527 offsets are in the direction of stack growth. */
10529 HOST_WIDE_INT
10530 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10532 arm_stack_offsets *offsets;
10534 offsets = arm_get_frame_offsets ();
10536 /* OK, now we have enough information to compute the distances.
10537 There must be an entry in these switch tables for each pair
10538 of registers in ELIMINABLE_REGS, even if some of the entries
10539 seem to be redundant or useless. */
10540 switch (from)
10542 case ARG_POINTER_REGNUM:
10543 switch (to)
10545 case THUMB_HARD_FRAME_POINTER_REGNUM:
10546 return 0;
10548 case FRAME_POINTER_REGNUM:
10549 /* This is the reverse of the soft frame pointer
10550 to hard frame pointer elimination below. */
10551 return offsets->soft_frame - offsets->saved_args;
10553 case ARM_HARD_FRAME_POINTER_REGNUM:
10554 /* If there is no stack frame then the hard
10555 frame pointer and the arg pointer coincide. */
10556 if (offsets->frame == offsets->saved_regs)
10557 return 0;
10558 /* FIXME: Not sure about this. Maybe we should always return 0 ? */
10559 return (frame_pointer_needed
10560 && cfun->static_chain_decl != NULL
10561 && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10563 case STACK_POINTER_REGNUM:
10564 /* If nothing has been pushed on the stack at all
10565 then this will return -4. This *is* correct! */
10566 return offsets->outgoing_args - (offsets->saved_args + 4);
10568 default:
10569 gcc_unreachable ();
10571 gcc_unreachable ();
10573 case FRAME_POINTER_REGNUM:
10574 switch (to)
10576 case THUMB_HARD_FRAME_POINTER_REGNUM:
10577 return 0;
10579 case ARM_HARD_FRAME_POINTER_REGNUM:
10580 /* The hard frame pointer points to the top entry in the
10581 stack frame. The soft frame pointer to the bottom entry
10582 in the stack frame. If there is no stack frame at all,
10583 then they are identical. */
10585 return offsets->frame - offsets->soft_frame;
10587 case STACK_POINTER_REGNUM:
10588 return offsets->outgoing_args - offsets->soft_frame;
10590 default:
10591 gcc_unreachable ();
10593 gcc_unreachable ();
10595 default:
10596 /* You cannot eliminate from the stack pointer.
10597 In theory you could eliminate from the hard frame
10598 pointer to the stack pointer, but this will never
10599 happen, since if a stack frame is not needed the
10600 hard frame pointer will never be used. */
10601 gcc_unreachable ();
10606 /* Generate the prologue instructions for entry into an ARM function. */
10607 void
10608 arm_expand_prologue (void)
10610 int reg;
10611 rtx amount;
10612 rtx insn;
10613 rtx ip_rtx;
10614 unsigned long live_regs_mask;
10615 unsigned long func_type;
10616 int fp_offset = 0;
10617 int saved_pretend_args = 0;
10618 int saved_regs = 0;
10619 unsigned HOST_WIDE_INT args_to_push;
10620 arm_stack_offsets *offsets;
10622 func_type = arm_current_func_type ();
10624 /* Naked functions don't have prologues. */
10625 if (IS_NAKED (func_type))
10626 return;
10628 /* Make a copy of c_f_p_a_s as we may need to modify it locally. */
10629 args_to_push = current_function_pretend_args_size;
10631 /* Compute which register we will have to save onto the stack. */
10632 live_regs_mask = arm_compute_save_reg_mask ();
10634 ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10636 if (frame_pointer_needed)
10638 if (IS_INTERRUPT (func_type))
10640 /* Interrupt functions must not corrupt any registers.
10641 Creating a frame pointer however, corrupts the IP
10642 register, so we must push it first. */
10643 insn = emit_multi_reg_push (1 << IP_REGNUM);
10645 /* Do not set RTX_FRAME_RELATED_P on this insn.
10646 The dwarf stack unwinding code only wants to see one
10647 stack decrement per function, and this is not it. If
10648 this instruction is labeled as being part of the frame
10649 creation sequence then dwarf2out_frame_debug_expr will
10650 die when it encounters the assignment of IP to FP
10651 later on, since the use of SP here establishes SP as
10652 the CFA register and not IP.
10654 Anyway this instruction is not really part of the stack
10655 frame creation although it is part of the prologue. */
10657 else if (IS_NESTED (func_type))
10659 /* The Static chain register is the same as the IP register
10660 used as a scratch register during stack frame creation.
10661 To get around this need to find somewhere to store IP
10662 whilst the frame is being created. We try the following
10663 places in order:
10665 1. The last argument register.
10666 2. A slot on the stack above the frame. (This only
10667 works if the function is not a varargs function).
10668 3. Register r3, after pushing the argument registers
10669 onto the stack.
10671 Note - we only need to tell the dwarf2 backend about the SP
10672 adjustment in the second variant; the static chain register
10673 doesn't need to be unwound, as it doesn't contain a value
10674 inherited from the caller. */
10676 if (regs_ever_live[3] == 0)
10677 insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
10678 else if (args_to_push == 0)
10680 rtx dwarf;
10682 insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10683 insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
10684 fp_offset = 4;
10686 /* Just tell the dwarf backend that we adjusted SP. */
10687 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10688 plus_constant (stack_pointer_rtx,
10689 -fp_offset));
10690 RTX_FRAME_RELATED_P (insn) = 1;
10691 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10692 dwarf, REG_NOTES (insn));
10694 else
10696 /* Store the args on the stack. */
10697 if (cfun->machine->uses_anonymous_args)
10698 insn = emit_multi_reg_push
10699 ((0xf0 >> (args_to_push / 4)) & 0xf);
10700 else
10701 insn = emit_insn
10702 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10703 GEN_INT (- args_to_push)));
10705 RTX_FRAME_RELATED_P (insn) = 1;
10707 saved_pretend_args = 1;
10708 fp_offset = args_to_push;
10709 args_to_push = 0;
10711 /* Now reuse r3 to preserve IP. */
10712 emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
10716 insn = emit_set_insn (ip_rtx,
10717 plus_constant (stack_pointer_rtx, fp_offset));
10718 RTX_FRAME_RELATED_P (insn) = 1;
10721 if (args_to_push)
10723 /* Push the argument registers, or reserve space for them. */
10724 if (cfun->machine->uses_anonymous_args)
10725 insn = emit_multi_reg_push
10726 ((0xf0 >> (args_to_push / 4)) & 0xf);
10727 else
10728 insn = emit_insn
10729 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10730 GEN_INT (- args_to_push)));
10731 RTX_FRAME_RELATED_P (insn) = 1;
10734 /* If this is an interrupt service routine, and the link register
10735 is going to be pushed, and we are not creating a stack frame,
10736 (which would involve an extra push of IP and a pop in the epilogue)
10737 subtracting four from LR now will mean that the function return
10738 can be done with a single instruction. */
10739 if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10740 && (live_regs_mask & (1 << LR_REGNUM)) != 0
10741 && ! frame_pointer_needed)
10743 rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
10745 emit_set_insn (lr, plus_constant (lr, -4));
10748 if (live_regs_mask)
10750 insn = emit_multi_reg_push (live_regs_mask);
10751 saved_regs += bit_count (live_regs_mask) * 4;
10752 RTX_FRAME_RELATED_P (insn) = 1;
10755 if (TARGET_IWMMXT)
10756 for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10757 if (regs_ever_live[reg] && ! call_used_regs [reg])
10759 insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10760 insn = gen_frame_mem (V2SImode, insn);
10761 insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
10762 RTX_FRAME_RELATED_P (insn) = 1;
10763 saved_regs += 8;
10766 if (! IS_VOLATILE (func_type))
10768 int start_reg;
10770 /* Save any floating point call-saved registers used by this
10771 function. */
10772 if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10774 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10775 if (regs_ever_live[reg] && !call_used_regs[reg])
10777 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10778 insn = gen_frame_mem (XFmode, insn);
10779 insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
10780 RTX_FRAME_RELATED_P (insn) = 1;
10781 saved_regs += 12;
10784 else
10786 start_reg = LAST_FPA_REGNUM;
10788 for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10790 if (regs_ever_live[reg] && !call_used_regs[reg])
10792 if (start_reg - reg == 3)
10794 insn = emit_sfm (reg, 4);
10795 RTX_FRAME_RELATED_P (insn) = 1;
10796 saved_regs += 48;
10797 start_reg = reg - 1;
10800 else
10802 if (start_reg != reg)
10804 insn = emit_sfm (reg + 1, start_reg - reg);
10805 RTX_FRAME_RELATED_P (insn) = 1;
10806 saved_regs += (start_reg - reg) * 12;
10808 start_reg = reg - 1;
10812 if (start_reg != reg)
10814 insn = emit_sfm (reg + 1, start_reg - reg);
10815 saved_regs += (start_reg - reg) * 12;
10816 RTX_FRAME_RELATED_P (insn) = 1;
10819 if (TARGET_HARD_FLOAT && TARGET_VFP)
10821 start_reg = FIRST_VFP_REGNUM;
10823 for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10825 if ((!regs_ever_live[reg] || call_used_regs[reg])
10826 && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10828 if (start_reg != reg)
10829 saved_regs += vfp_emit_fstmx (start_reg,
10830 (reg - start_reg) / 2);
10831 start_reg = reg + 2;
10834 if (start_reg != reg)
10835 saved_regs += vfp_emit_fstmx (start_reg,
10836 (reg - start_reg) / 2);
10840 if (frame_pointer_needed)
10842 /* Create the new frame pointer. */
10843 insn = GEN_INT (-(4 + args_to_push + fp_offset));
10844 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10845 RTX_FRAME_RELATED_P (insn) = 1;
10847 if (IS_NESTED (func_type))
10849 /* Recover the static chain register. */
10850 if (regs_ever_live [3] == 0
10851 || saved_pretend_args)
10852 insn = gen_rtx_REG (SImode, 3);
10853 else /* if (current_function_pretend_args_size == 0) */
10855 insn = plus_constant (hard_frame_pointer_rtx, 4);
10856 insn = gen_frame_mem (SImode, insn);
10859 emit_set_insn (ip_rtx, insn);
10860 /* Add a USE to stop propagate_one_insn() from barfing. */
10861 emit_insn (gen_prologue_use (ip_rtx));
10865 offsets = arm_get_frame_offsets ();
10866 if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10868 /* This add can produce multiple insns for a large constant, so we
10869 need to get tricky. */
10870 rtx last = get_last_insn ();
10872 amount = GEN_INT (offsets->saved_args + saved_regs
10873 - offsets->outgoing_args);
10875 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10876 amount));
10879 last = last ? NEXT_INSN (last) : get_insns ();
10880 RTX_FRAME_RELATED_P (last) = 1;
10882 while (last != insn);
10884 /* If the frame pointer is needed, emit a special barrier that
10885 will prevent the scheduler from moving stores to the frame
10886 before the stack adjustment. */
10887 if (frame_pointer_needed)
10888 insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10889 hard_frame_pointer_rtx));
10893 if (flag_pic && arm_pic_register != INVALID_REGNUM)
10894 arm_load_pic_register (0UL);
10896 /* If we are profiling, make sure no instructions are scheduled before
10897 the call to mcount. Similarly if the user has requested no
10898 scheduling in the prolog. Similarly if we want non-call exceptions
10899 using the EABI unwinder, to prevent faulting instructions from being
10900 swapped with a stack adjustment. */
10901 if (current_function_profile || !TARGET_SCHED_PROLOG
10902 || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
10903 emit_insn (gen_blockage ());
10905 /* If the link register is being kept alive, with the return address in it,
10906 then make sure that it does not get reused by the ce2 pass. */
10907 if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10909 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10910 cfun->machine->lr_save_eliminated = 1;
10914 /* If CODE is 'd', then the X is a condition operand and the instruction
10915 should only be executed if the condition is true.
10916 if CODE is 'D', then the X is a condition operand and the instruction
10917 should only be executed if the condition is false: however, if the mode
10918 of the comparison is CCFPEmode, then always execute the instruction -- we
10919 do this because in these circumstances !GE does not necessarily imply LT;
10920 in these cases the instruction pattern will take care to make sure that
10921 an instruction containing %d will follow, thereby undoing the effects of
10922 doing this instruction unconditionally.
10923 If CODE is 'N' then X is a floating point operand that must be negated
10924 before output.
10925 If CODE is 'B' then output a bitwise inverted value of X (a const int).
10926 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
10927 void
10928 arm_print_operand (FILE *stream, rtx x, int code)
10930 switch (code)
10932 case '@':
10933 fputs (ASM_COMMENT_START, stream);
10934 return;
10936 case '_':
10937 fputs (user_label_prefix, stream);
10938 return;
10940 case '|':
10941 fputs (REGISTER_PREFIX, stream);
10942 return;
10944 case '?':
10945 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10947 if (TARGET_THUMB)
10949 output_operand_lossage ("predicated Thumb instruction");
10950 break;
10952 if (current_insn_predicate != NULL)
10954 output_operand_lossage
10955 ("predicated instruction in conditional sequence");
10956 break;
10959 fputs (arm_condition_codes[arm_current_cc], stream);
10961 else if (current_insn_predicate)
10963 enum arm_cond_code code;
10965 if (TARGET_THUMB)
10967 output_operand_lossage ("predicated Thumb instruction");
10968 break;
10971 code = get_arm_condition_code (current_insn_predicate);
10972 fputs (arm_condition_codes[code], stream);
10974 return;
10976 case 'N':
10978 REAL_VALUE_TYPE r;
10979 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10980 r = REAL_VALUE_NEGATE (r);
10981 fprintf (stream, "%s", fp_const_from_val (&r));
10983 return;
10985 case 'B':
10986 if (GET_CODE (x) == CONST_INT)
10988 HOST_WIDE_INT val;
10989 val = ARM_SIGN_EXTEND (~INTVAL (x));
10990 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10992 else
10994 putc ('~', stream);
10995 output_addr_const (stream, x);
10997 return;
10999 case 'i':
11000 fprintf (stream, "%s", arithmetic_instr (x, 1));
11001 return;
11003 /* Truncate Cirrus shift counts. */
11004 case 's':
11005 if (GET_CODE (x) == CONST_INT)
11007 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
11008 return;
11010 arm_print_operand (stream, x, 0);
11011 return;
11013 case 'I':
11014 fprintf (stream, "%s", arithmetic_instr (x, 0));
11015 return;
11017 case 'S':
11019 HOST_WIDE_INT val;
11020 const char * shift = shift_op (x, &val);
11022 if (shift)
11024 fprintf (stream, ", %s ", shift_op (x, &val));
11025 if (val == -1)
11026 arm_print_operand (stream, XEXP (x, 1), 0);
11027 else
11028 fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
11031 return;
11033 /* An explanation of the 'Q', 'R' and 'H' register operands:
11035 In a pair of registers containing a DI or DF value the 'Q'
11036 operand returns the register number of the register containing
11037 the least significant part of the value. The 'R' operand returns
11038 the register number of the register containing the most
11039 significant part of the value.
11041 The 'H' operand returns the higher of the two register numbers.
11042 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
11043 same as the 'Q' operand, since the most significant part of the
11044 value is held in the lower number register. The reverse is true
11045 on systems where WORDS_BIG_ENDIAN is false.
11047 The purpose of these operands is to distinguish between cases
11048 where the endian-ness of the values is important (for example
11049 when they are added together), and cases where the endian-ness
11050 is irrelevant, but the order of register operations is important.
11051 For example when loading a value from memory into a register
11052 pair, the endian-ness does not matter. Provided that the value
11053 from the lower memory address is put into the lower numbered
11054 register, and the value from the higher address is put into the
11055 higher numbered register, the load will work regardless of whether
11056 the value being loaded is big-wordian or little-wordian. The
11057 order of the two register loads can matter however, if the address
11058 of the memory location is actually held in one of the registers
11059 being overwritten by the load. */
11060 case 'Q':
11061 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11063 output_operand_lossage ("invalid operand for code '%c'", code);
11064 return;
11067 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
11068 return;
11070 case 'R':
11071 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11073 output_operand_lossage ("invalid operand for code '%c'", code);
11074 return;
11077 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
11078 return;
11080 case 'H':
11081 if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11083 output_operand_lossage ("invalid operand for code '%c'", code);
11084 return;
11087 asm_fprintf (stream, "%r", REGNO (x) + 1);
11088 return;
11090 case 'm':
11091 asm_fprintf (stream, "%r",
11092 GET_CODE (XEXP (x, 0)) == REG
11093 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
11094 return;
11096 case 'M':
11097 asm_fprintf (stream, "{%r-%r}",
11098 REGNO (x),
11099 REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
11100 return;
11102 case 'd':
11103 /* CONST_TRUE_RTX means always -- that's the default. */
11104 if (x == const_true_rtx)
11105 return;
11107 if (!COMPARISON_P (x))
11109 output_operand_lossage ("invalid operand for code '%c'", code);
11110 return;
11113 fputs (arm_condition_codes[get_arm_condition_code (x)],
11114 stream);
11115 return;
11117 case 'D':
11118 /* CONST_TRUE_RTX means not always -- i.e. never. We shouldn't ever
11119 want to do that. */
11120 if (x == const_true_rtx)
11122 output_operand_lossage ("instruction never exectued");
11123 return;
11125 if (!COMPARISON_P (x))
11127 output_operand_lossage ("invalid operand for code '%c'", code);
11128 return;
11131 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
11132 (get_arm_condition_code (x))],
11133 stream);
11134 return;
11136 /* Cirrus registers can be accessed in a variety of ways:
11137 single floating point (f)
11138 double floating point (d)
11139 32bit integer (fx)
11140 64bit integer (dx). */
11141 case 'W': /* Cirrus register in F mode. */
11142 case 'X': /* Cirrus register in D mode. */
11143 case 'Y': /* Cirrus register in FX mode. */
11144 case 'Z': /* Cirrus register in DX mode. */
11145 gcc_assert (GET_CODE (x) == REG
11146 && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
11148 fprintf (stream, "mv%s%s",
11149 code == 'W' ? "f"
11150 : code == 'X' ? "d"
11151 : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
11153 return;
11155 /* Print cirrus register in the mode specified by the register's mode. */
11156 case 'V':
11158 int mode = GET_MODE (x);
11160 if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
11162 output_operand_lossage ("invalid operand for code '%c'", code);
11163 return;
11166 fprintf (stream, "mv%s%s",
11167 mode == DFmode ? "d"
11168 : mode == SImode ? "fx"
11169 : mode == DImode ? "dx"
11170 : "f", reg_names[REGNO (x)] + 2);
11172 return;
11175 case 'U':
11176 if (GET_CODE (x) != REG
11177 || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
11178 || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
11179 /* Bad value for wCG register number. */
11181 output_operand_lossage ("invalid operand for code '%c'", code);
11182 return;
11185 else
11186 fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
11187 return;
11189 /* Print an iWMMXt control register name. */
11190 case 'w':
11191 if (GET_CODE (x) != CONST_INT
11192 || INTVAL (x) < 0
11193 || INTVAL (x) >= 16)
11194 /* Bad value for wC register number. */
11196 output_operand_lossage ("invalid operand for code '%c'", code);
11197 return;
11200 else
11202 static const char * wc_reg_names [16] =
11204 "wCID", "wCon", "wCSSF", "wCASF",
11205 "wC4", "wC5", "wC6", "wC7",
11206 "wCGR0", "wCGR1", "wCGR2", "wCGR3",
11207 "wC12", "wC13", "wC14", "wC15"
11210 fprintf (stream, wc_reg_names [INTVAL (x)]);
11212 return;
11214 /* Print a VFP double precision register name. */
11215 case 'P':
11217 int mode = GET_MODE (x);
11218 int num;
11220 if (mode != DImode && mode != DFmode)
11222 output_operand_lossage ("invalid operand for code '%c'", code);
11223 return;
11226 if (GET_CODE (x) != REG
11227 || !IS_VFP_REGNUM (REGNO (x)))
11229 output_operand_lossage ("invalid operand for code '%c'", code);
11230 return;
11233 num = REGNO(x) - FIRST_VFP_REGNUM;
11234 if (num & 1)
11236 output_operand_lossage ("invalid operand for code '%c'", code);
11237 return;
11240 fprintf (stream, "d%d", num >> 1);
11242 return;
11244 default:
11245 if (x == 0)
11247 output_operand_lossage ("missing operand");
11248 return;
11251 switch (GET_CODE (x))
11253 case REG:
11254 asm_fprintf (stream, "%r", REGNO (x));
11255 break;
11257 case MEM:
11258 output_memory_reference_mode = GET_MODE (x);
11259 output_address (XEXP (x, 0));
11260 break;
11262 case CONST_DOUBLE:
11263 fprintf (stream, "#%s", fp_immediate_constant (x));
11264 break;
11266 default:
11267 gcc_assert (GET_CODE (x) != NEG);
11268 fputc ('#', stream);
11269 output_addr_const (stream, x);
11270 break;
11275 #ifndef AOF_ASSEMBLER
11276 /* Target hook for assembling integer objects. The ARM version needs to
11277 handle word-sized values specially. */
11278 static bool
11279 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
11281 if (size == UNITS_PER_WORD && aligned_p)
11283 fputs ("\t.word\t", asm_out_file);
11284 output_addr_const (asm_out_file, x);
11286 /* Mark symbols as position independent. We only do this in the
11287 .text segment, not in the .data segment. */
11288 if (NEED_GOT_RELOC && flag_pic && making_const_table &&
11289 (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
11291 if (GET_CODE (x) == SYMBOL_REF
11292 && (CONSTANT_POOL_ADDRESS_P (x)
11293 || SYMBOL_REF_LOCAL_P (x)))
11294 fputs ("(GOTOFF)", asm_out_file);
11295 else if (GET_CODE (x) == LABEL_REF)
11296 fputs ("(GOTOFF)", asm_out_file);
11297 else
11298 fputs ("(GOT)", asm_out_file);
11300 fputc ('\n', asm_out_file);
11301 return true;
11304 if (arm_vector_mode_supported_p (GET_MODE (x)))
11306 int i, units;
11308 gcc_assert (GET_CODE (x) == CONST_VECTOR);
11310 units = CONST_VECTOR_NUNITS (x);
11312 switch (GET_MODE (x))
11314 case V2SImode: size = 4; break;
11315 case V4HImode: size = 2; break;
11316 case V8QImode: size = 1; break;
11317 default:
11318 gcc_unreachable ();
11321 for (i = 0; i < units; i++)
11323 rtx elt;
11325 elt = CONST_VECTOR_ELT (x, i);
11326 assemble_integer
11327 (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
11330 return true;
11333 return default_assemble_integer (x, size, aligned_p);
11337 /* Add a function to the list of static constructors. */
11339 static void
11340 arm_elf_asm_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
11342 if (!TARGET_AAPCS_BASED)
11344 default_named_section_asm_out_constructor (symbol, priority);
11345 return;
11348 /* Put these in the .init_array section, using a special relocation. */
11349 switch_to_section (ctors_section);
11350 assemble_align (POINTER_SIZE);
11351 fputs ("\t.word\t", asm_out_file);
11352 output_addr_const (asm_out_file, symbol);
11353 fputs ("(target1)\n", asm_out_file);
11355 #endif
11357 /* A finite state machine takes care of noticing whether or not instructions
11358 can be conditionally executed, and thus decrease execution time and code
11359 size by deleting branch instructions. The fsm is controlled by
11360 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
11362 /* The state of the fsm controlling condition codes are:
11363 0: normal, do nothing special
11364 1: make ASM_OUTPUT_OPCODE not output this instruction
11365 2: make ASM_OUTPUT_OPCODE not output this instruction
11366 3: make instructions conditional
11367 4: make instructions conditional
11369 State transitions (state->state by whom under condition):
11370 0 -> 1 final_prescan_insn if the `target' is a label
11371 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
11372 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
11373 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
11374 3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
11375 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
11376 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
11377 (the target insn is arm_target_insn).
11379 If the jump clobbers the conditions then we use states 2 and 4.
11381 A similar thing can be done with conditional return insns.
11383 XXX In case the `target' is an unconditional branch, this conditionalising
11384 of the instructions always reduces code size, but not always execution
11385 time. But then, I want to reduce the code size to somewhere near what
11386 /bin/cc produces. */
11388 /* Returns the index of the ARM condition code string in
11389 `arm_condition_codes'. COMPARISON should be an rtx like
11390 `(eq (...) (...))'. */
11391 static enum arm_cond_code
11392 get_arm_condition_code (rtx comparison)
11394 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
11395 int code;
11396 enum rtx_code comp_code = GET_CODE (comparison);
11398 if (GET_MODE_CLASS (mode) != MODE_CC)
11399 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
11400 XEXP (comparison, 1));
11402 switch (mode)
11404 case CC_DNEmode: code = ARM_NE; goto dominance;
11405 case CC_DEQmode: code = ARM_EQ; goto dominance;
11406 case CC_DGEmode: code = ARM_GE; goto dominance;
11407 case CC_DGTmode: code = ARM_GT; goto dominance;
11408 case CC_DLEmode: code = ARM_LE; goto dominance;
11409 case CC_DLTmode: code = ARM_LT; goto dominance;
11410 case CC_DGEUmode: code = ARM_CS; goto dominance;
11411 case CC_DGTUmode: code = ARM_HI; goto dominance;
11412 case CC_DLEUmode: code = ARM_LS; goto dominance;
11413 case CC_DLTUmode: code = ARM_CC;
11415 dominance:
11416 gcc_assert (comp_code == EQ || comp_code == NE);
11418 if (comp_code == EQ)
11419 return ARM_INVERSE_CONDITION_CODE (code);
11420 return code;
11422 case CC_NOOVmode:
11423 switch (comp_code)
11425 case NE: return ARM_NE;
11426 case EQ: return ARM_EQ;
11427 case GE: return ARM_PL;
11428 case LT: return ARM_MI;
11429 default: gcc_unreachable ();
11432 case CC_Zmode:
11433 switch (comp_code)
11435 case NE: return ARM_NE;
11436 case EQ: return ARM_EQ;
11437 default: gcc_unreachable ();
11440 case CC_Nmode:
11441 switch (comp_code)
11443 case NE: return ARM_MI;
11444 case EQ: return ARM_PL;
11445 default: gcc_unreachable ();
11448 case CCFPEmode:
11449 case CCFPmode:
11450 /* These encodings assume that AC=1 in the FPA system control
11451 byte. This allows us to handle all cases except UNEQ and
11452 LTGT. */
11453 switch (comp_code)
11455 case GE: return ARM_GE;
11456 case GT: return ARM_GT;
11457 case LE: return ARM_LS;
11458 case LT: return ARM_MI;
11459 case NE: return ARM_NE;
11460 case EQ: return ARM_EQ;
11461 case ORDERED: return ARM_VC;
11462 case UNORDERED: return ARM_VS;
11463 case UNLT: return ARM_LT;
11464 case UNLE: return ARM_LE;
11465 case UNGT: return ARM_HI;
11466 case UNGE: return ARM_PL;
11467 /* UNEQ and LTGT do not have a representation. */
11468 case UNEQ: /* Fall through. */
11469 case LTGT: /* Fall through. */
11470 default: gcc_unreachable ();
11473 case CC_SWPmode:
11474 switch (comp_code)
11476 case NE: return ARM_NE;
11477 case EQ: return ARM_EQ;
11478 case GE: return ARM_LE;
11479 case GT: return ARM_LT;
11480 case LE: return ARM_GE;
11481 case LT: return ARM_GT;
11482 case GEU: return ARM_LS;
11483 case GTU: return ARM_CC;
11484 case LEU: return ARM_CS;
11485 case LTU: return ARM_HI;
11486 default: gcc_unreachable ();
11489 case CC_Cmode:
11490 switch (comp_code)
11492 case LTU: return ARM_CS;
11493 case GEU: return ARM_CC;
11494 default: gcc_unreachable ();
11497 case CCmode:
11498 switch (comp_code)
11500 case NE: return ARM_NE;
11501 case EQ: return ARM_EQ;
11502 case GE: return ARM_GE;
11503 case GT: return ARM_GT;
11504 case LE: return ARM_LE;
11505 case LT: return ARM_LT;
11506 case GEU: return ARM_CS;
11507 case GTU: return ARM_HI;
11508 case LEU: return ARM_LS;
11509 case LTU: return ARM_CC;
11510 default: gcc_unreachable ();
11513 default: gcc_unreachable ();
11517 void
11518 arm_final_prescan_insn (rtx insn)
11520 /* BODY will hold the body of INSN. */
11521 rtx body = PATTERN (insn);
11523 /* This will be 1 if trying to repeat the trick, and things need to be
11524 reversed if it appears to fail. */
11525 int reverse = 0;
11527 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11528 taken are clobbered, even if the rtl suggests otherwise. It also
11529 means that we have to grub around within the jump expression to find
11530 out what the conditions are when the jump isn't taken. */
11531 int jump_clobbers = 0;
11533 /* If we start with a return insn, we only succeed if we find another one. */
11534 int seeking_return = 0;
11536 /* START_INSN will hold the insn from where we start looking. This is the
11537 first insn after the following code_label if REVERSE is true. */
11538 rtx start_insn = insn;
11540 /* If in state 4, check if the target branch is reached, in order to
11541 change back to state 0. */
11542 if (arm_ccfsm_state == 4)
11544 if (insn == arm_target_insn)
11546 arm_target_insn = NULL;
11547 arm_ccfsm_state = 0;
11549 return;
11552 /* If in state 3, it is possible to repeat the trick, if this insn is an
11553 unconditional branch to a label, and immediately following this branch
11554 is the previous target label which is only used once, and the label this
11555 branch jumps to is not too far off. */
11556 if (arm_ccfsm_state == 3)
11558 if (simplejump_p (insn))
11560 start_insn = next_nonnote_insn (start_insn);
11561 if (GET_CODE (start_insn) == BARRIER)
11563 /* XXX Isn't this always a barrier? */
11564 start_insn = next_nonnote_insn (start_insn);
11566 if (GET_CODE (start_insn) == CODE_LABEL
11567 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11568 && LABEL_NUSES (start_insn) == 1)
11569 reverse = TRUE;
11570 else
11571 return;
11573 else if (GET_CODE (body) == RETURN)
11575 start_insn = next_nonnote_insn (start_insn);
11576 if (GET_CODE (start_insn) == BARRIER)
11577 start_insn = next_nonnote_insn (start_insn);
11578 if (GET_CODE (start_insn) == CODE_LABEL
11579 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11580 && LABEL_NUSES (start_insn) == 1)
11582 reverse = TRUE;
11583 seeking_return = 1;
11585 else
11586 return;
11588 else
11589 return;
11592 gcc_assert (!arm_ccfsm_state || reverse);
11593 if (GET_CODE (insn) != JUMP_INSN)
11594 return;
11596 /* This jump might be paralleled with a clobber of the condition codes
11597 the jump should always come first */
11598 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11599 body = XVECEXP (body, 0, 0);
11601 if (reverse
11602 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11603 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11605 int insns_skipped;
11606 int fail = FALSE, succeed = FALSE;
11607 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
11608 int then_not_else = TRUE;
11609 rtx this_insn = start_insn, label = 0;
11611 /* If the jump cannot be done with one instruction, we cannot
11612 conditionally execute the instruction in the inverse case. */
11613 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11615 jump_clobbers = 1;
11616 return;
11619 /* Register the insn jumped to. */
11620 if (reverse)
11622 if (!seeking_return)
11623 label = XEXP (SET_SRC (body), 0);
11625 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11626 label = XEXP (XEXP (SET_SRC (body), 1), 0);
11627 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11629 label = XEXP (XEXP (SET_SRC (body), 2), 0);
11630 then_not_else = FALSE;
11632 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11633 seeking_return = 1;
11634 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11636 seeking_return = 1;
11637 then_not_else = FALSE;
11639 else
11640 gcc_unreachable ();
11642 /* See how many insns this branch skips, and what kind of insns. If all
11643 insns are okay, and the label or unconditional branch to the same
11644 label is not too far away, succeed. */
11645 for (insns_skipped = 0;
11646 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11648 rtx scanbody;
11650 this_insn = next_nonnote_insn (this_insn);
11651 if (!this_insn)
11652 break;
11654 switch (GET_CODE (this_insn))
11656 case CODE_LABEL:
11657 /* Succeed if it is the target label, otherwise fail since
11658 control falls in from somewhere else. */
11659 if (this_insn == label)
11661 if (jump_clobbers)
11663 arm_ccfsm_state = 2;
11664 this_insn = next_nonnote_insn (this_insn);
11666 else
11667 arm_ccfsm_state = 1;
11668 succeed = TRUE;
11670 else
11671 fail = TRUE;
11672 break;
11674 case BARRIER:
11675 /* Succeed if the following insn is the target label.
11676 Otherwise fail.
11677 If return insns are used then the last insn in a function
11678 will be a barrier. */
11679 this_insn = next_nonnote_insn (this_insn);
11680 if (this_insn && this_insn == label)
11682 if (jump_clobbers)
11684 arm_ccfsm_state = 2;
11685 this_insn = next_nonnote_insn (this_insn);
11687 else
11688 arm_ccfsm_state = 1;
11689 succeed = TRUE;
11691 else
11692 fail = TRUE;
11693 break;
11695 case CALL_INSN:
11696 /* The AAPCS says that conditional calls should not be
11697 used since they make interworking inefficient (the
11698 linker can't transform BL<cond> into BLX). That's
11699 only a problem if the machine has BLX. */
11700 if (arm_arch5)
11702 fail = TRUE;
11703 break;
11706 /* Succeed if the following insn is the target label, or
11707 if the following two insns are a barrier and the
11708 target label. */
11709 this_insn = next_nonnote_insn (this_insn);
11710 if (this_insn && GET_CODE (this_insn) == BARRIER)
11711 this_insn = next_nonnote_insn (this_insn);
11713 if (this_insn && this_insn == label
11714 && insns_skipped < max_insns_skipped)
11716 if (jump_clobbers)
11718 arm_ccfsm_state = 2;
11719 this_insn = next_nonnote_insn (this_insn);
11721 else
11722 arm_ccfsm_state = 1;
11723 succeed = TRUE;
11725 else
11726 fail = TRUE;
11727 break;
11729 case JUMP_INSN:
11730 /* If this is an unconditional branch to the same label, succeed.
11731 If it is to another label, do nothing. If it is conditional,
11732 fail. */
11733 /* XXX Probably, the tests for SET and the PC are
11734 unnecessary. */
11736 scanbody = PATTERN (this_insn);
11737 if (GET_CODE (scanbody) == SET
11738 && GET_CODE (SET_DEST (scanbody)) == PC)
11740 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11741 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11743 arm_ccfsm_state = 2;
11744 succeed = TRUE;
11746 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11747 fail = TRUE;
11749 /* Fail if a conditional return is undesirable (e.g. on a
11750 StrongARM), but still allow this if optimizing for size. */
11751 else if (GET_CODE (scanbody) == RETURN
11752 && !use_return_insn (TRUE, NULL)
11753 && !optimize_size)
11754 fail = TRUE;
11755 else if (GET_CODE (scanbody) == RETURN
11756 && seeking_return)
11758 arm_ccfsm_state = 2;
11759 succeed = TRUE;
11761 else if (GET_CODE (scanbody) == PARALLEL)
11763 switch (get_attr_conds (this_insn))
11765 case CONDS_NOCOND:
11766 break;
11767 default:
11768 fail = TRUE;
11769 break;
11772 else
11773 fail = TRUE; /* Unrecognized jump (e.g. epilogue). */
11775 break;
11777 case INSN:
11778 /* Instructions using or affecting the condition codes make it
11779 fail. */
11780 scanbody = PATTERN (this_insn);
11781 if (!(GET_CODE (scanbody) == SET
11782 || GET_CODE (scanbody) == PARALLEL)
11783 || get_attr_conds (this_insn) != CONDS_NOCOND)
11784 fail = TRUE;
11786 /* A conditional cirrus instruction must be followed by
11787 a non Cirrus instruction. However, since we
11788 conditionalize instructions in this function and by
11789 the time we get here we can't add instructions
11790 (nops), because shorten_branches() has already been
11791 called, we will disable conditionalizing Cirrus
11792 instructions to be safe. */
11793 if (GET_CODE (scanbody) != USE
11794 && GET_CODE (scanbody) != CLOBBER
11795 && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11796 fail = TRUE;
11797 break;
11799 default:
11800 break;
11803 if (succeed)
11805 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11806 arm_target_label = CODE_LABEL_NUMBER (label);
11807 else
11809 gcc_assert (seeking_return || arm_ccfsm_state == 2);
11811 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11813 this_insn = next_nonnote_insn (this_insn);
11814 gcc_assert (!this_insn
11815 || (GET_CODE (this_insn) != BARRIER
11816 && GET_CODE (this_insn) != CODE_LABEL));
11818 if (!this_insn)
11820 /* Oh, dear! we ran off the end.. give up. */
11821 recog (PATTERN (insn), insn, NULL);
11822 arm_ccfsm_state = 0;
11823 arm_target_insn = NULL;
11824 return;
11826 arm_target_insn = this_insn;
11828 if (jump_clobbers)
11830 gcc_assert (!reverse);
11831 arm_current_cc =
11832 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11833 0), 0), 1));
11834 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11835 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11836 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11837 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11839 else
11841 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11842 what it was. */
11843 if (!reverse)
11844 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11845 0));
11848 if (reverse || then_not_else)
11849 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11852 /* Restore recog_data (getting the attributes of other insns can
11853 destroy this array, but final.c assumes that it remains intact
11854 across this call; since the insn has been recognized already we
11855 call recog direct). */
11856 recog (PATTERN (insn), insn, NULL);
11860 /* Returns true if REGNO is a valid register
11861 for holding a quantity of type MODE. */
11863 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11865 if (GET_MODE_CLASS (mode) == MODE_CC)
11866 return (regno == CC_REGNUM
11867 || (TARGET_HARD_FLOAT && TARGET_VFP
11868 && regno == VFPCC_REGNUM));
11870 if (TARGET_THUMB)
11871 /* For the Thumb we only allow values bigger than SImode in
11872 registers 0 - 6, so that there is always a second low
11873 register available to hold the upper part of the value.
11874 We probably we ought to ensure that the register is the
11875 start of an even numbered register pair. */
11876 return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11878 if (TARGET_HARD_FLOAT && TARGET_MAVERICK
11879 && IS_CIRRUS_REGNUM (regno))
11880 /* We have outlawed SI values in Cirrus registers because they
11881 reside in the lower 32 bits, but SF values reside in the
11882 upper 32 bits. This causes gcc all sorts of grief. We can't
11883 even split the registers into pairs because Cirrus SI values
11884 get sign extended to 64bits-- aldyh. */
11885 return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11887 if (TARGET_HARD_FLOAT && TARGET_VFP
11888 && IS_VFP_REGNUM (regno))
11890 if (mode == SFmode || mode == SImode)
11891 return TRUE;
11893 /* DFmode values are only valid in even register pairs. */
11894 if (mode == DFmode)
11895 return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11896 return FALSE;
11899 if (TARGET_REALLY_IWMMXT)
11901 if (IS_IWMMXT_GR_REGNUM (regno))
11902 return mode == SImode;
11904 if (IS_IWMMXT_REGNUM (regno))
11905 return VALID_IWMMXT_REG_MODE (mode);
11908 /* We allow any value to be stored in the general registers.
11909 Restrict doubleword quantities to even register pairs so that we can
11910 use ldrd. */
11911 if (regno <= LAST_ARM_REGNUM)
11912 return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11914 if (regno == FRAME_POINTER_REGNUM
11915 || regno == ARG_POINTER_REGNUM)
11916 /* We only allow integers in the fake hard registers. */
11917 return GET_MODE_CLASS (mode) == MODE_INT;
11919 /* The only registers left are the FPA registers
11920 which we only allow to hold FP values. */
11921 return (TARGET_HARD_FLOAT && TARGET_FPA
11922 && GET_MODE_CLASS (mode) == MODE_FLOAT
11923 && regno >= FIRST_FPA_REGNUM
11924 && regno <= LAST_FPA_REGNUM);
11928 arm_regno_class (int regno)
11930 if (TARGET_THUMB)
11932 if (regno == STACK_POINTER_REGNUM)
11933 return STACK_REG;
11934 if (regno == CC_REGNUM)
11935 return CC_REG;
11936 if (regno < 8)
11937 return LO_REGS;
11938 return HI_REGS;
11941 if ( regno <= LAST_ARM_REGNUM
11942 || regno == FRAME_POINTER_REGNUM
11943 || regno == ARG_POINTER_REGNUM)
11944 return GENERAL_REGS;
11946 if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11947 return NO_REGS;
11949 if (IS_CIRRUS_REGNUM (regno))
11950 return CIRRUS_REGS;
11952 if (IS_VFP_REGNUM (regno))
11953 return VFP_REGS;
11955 if (IS_IWMMXT_REGNUM (regno))
11956 return IWMMXT_REGS;
11958 if (IS_IWMMXT_GR_REGNUM (regno))
11959 return IWMMXT_GR_REGS;
11961 return FPA_REGS;
11964 /* Handle a special case when computing the offset
11965 of an argument from the frame pointer. */
11967 arm_debugger_arg_offset (int value, rtx addr)
11969 rtx insn;
11971 /* We are only interested if dbxout_parms() failed to compute the offset. */
11972 if (value != 0)
11973 return 0;
11975 /* We can only cope with the case where the address is held in a register. */
11976 if (GET_CODE (addr) != REG)
11977 return 0;
11979 /* If we are using the frame pointer to point at the argument, then
11980 an offset of 0 is correct. */
11981 if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11982 return 0;
11984 /* If we are using the stack pointer to point at the
11985 argument, then an offset of 0 is correct. */
11986 if ((TARGET_THUMB || !frame_pointer_needed)
11987 && REGNO (addr) == SP_REGNUM)
11988 return 0;
11990 /* Oh dear. The argument is pointed to by a register rather
11991 than being held in a register, or being stored at a known
11992 offset from the frame pointer. Since GDB only understands
11993 those two kinds of argument we must translate the address
11994 held in the register into an offset from the frame pointer.
11995 We do this by searching through the insns for the function
11996 looking to see where this register gets its value. If the
11997 register is initialized from the frame pointer plus an offset
11998 then we are in luck and we can continue, otherwise we give up.
12000 This code is exercised by producing debugging information
12001 for a function with arguments like this:
12003 double func (double a, double b, int c, double d) {return d;}
12005 Without this code the stab for parameter 'd' will be set to
12006 an offset of 0 from the frame pointer, rather than 8. */
12008 /* The if() statement says:
12010 If the insn is a normal instruction
12011 and if the insn is setting the value in a register
12012 and if the register being set is the register holding the address of the argument
12013 and if the address is computing by an addition
12014 that involves adding to a register
12015 which is the frame pointer
12016 a constant integer
12018 then... */
12020 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12022 if ( GET_CODE (insn) == INSN
12023 && GET_CODE (PATTERN (insn)) == SET
12024 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
12025 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
12026 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
12027 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
12028 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
12031 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
12033 break;
12037 if (value == 0)
12039 debug_rtx (addr);
12040 warning (0, "unable to compute real location of stacked parameter");
12041 value = 8; /* XXX magic hack */
12044 return value;
12047 #define def_mbuiltin(MASK, NAME, TYPE, CODE) \
12048 do \
12050 if ((MASK) & insn_flags) \
12051 lang_hooks.builtin_function ((NAME), (TYPE), (CODE), \
12052 BUILT_IN_MD, NULL, NULL_TREE); \
12054 while (0)
12056 struct builtin_description
12058 const unsigned int mask;
12059 const enum insn_code icode;
12060 const char * const name;
12061 const enum arm_builtins code;
12062 const enum rtx_code comparison;
12063 const unsigned int flag;
12066 static const struct builtin_description bdesc_2arg[] =
12068 #define IWMMXT_BUILTIN(code, string, builtin) \
12069 { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
12070 ARM_BUILTIN_##builtin, 0, 0 },
12072 IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
12073 IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
12074 IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
12075 IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
12076 IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
12077 IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
12078 IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
12079 IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
12080 IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
12081 IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
12082 IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
12083 IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
12084 IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
12085 IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
12086 IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
12087 IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
12088 IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
12089 IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
12090 IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
12091 IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
12092 IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
12093 IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
12094 IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
12095 IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
12096 IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
12097 IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
12098 IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
12099 IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
12100 IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
12101 IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
12102 IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
12103 IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
12104 IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
12105 IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
12106 IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
12107 IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
12108 IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
12109 IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
12110 IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
12111 IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
12112 IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
12113 IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
12114 IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
12115 IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
12116 IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
12117 IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
12118 IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
12119 IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
12120 IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
12121 IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
12122 IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
12123 IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
12124 IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
12125 IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
12126 IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
12127 IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
12128 IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
12129 IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
12131 #define IWMMXT_BUILTIN2(code, builtin) \
12132 { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
12134 IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
12135 IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
12136 IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
12137 IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
12138 IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
12139 IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
12140 IWMMXT_BUILTIN2 (ashlv4hi3_di, WSLLH)
12141 IWMMXT_BUILTIN2 (ashlv4hi3, WSLLHI)
12142 IWMMXT_BUILTIN2 (ashlv2si3_di, WSLLW)
12143 IWMMXT_BUILTIN2 (ashlv2si3, WSLLWI)
12144 IWMMXT_BUILTIN2 (ashldi3_di, WSLLD)
12145 IWMMXT_BUILTIN2 (ashldi3_iwmmxt, WSLLDI)
12146 IWMMXT_BUILTIN2 (lshrv4hi3_di, WSRLH)
12147 IWMMXT_BUILTIN2 (lshrv4hi3, WSRLHI)
12148 IWMMXT_BUILTIN2 (lshrv2si3_di, WSRLW)
12149 IWMMXT_BUILTIN2 (lshrv2si3, WSRLWI)
12150 IWMMXT_BUILTIN2 (lshrdi3_di, WSRLD)
12151 IWMMXT_BUILTIN2 (lshrdi3_iwmmxt, WSRLDI)
12152 IWMMXT_BUILTIN2 (ashrv4hi3_di, WSRAH)
12153 IWMMXT_BUILTIN2 (ashrv4hi3, WSRAHI)
12154 IWMMXT_BUILTIN2 (ashrv2si3_di, WSRAW)
12155 IWMMXT_BUILTIN2 (ashrv2si3, WSRAWI)
12156 IWMMXT_BUILTIN2 (ashrdi3_di, WSRAD)
12157 IWMMXT_BUILTIN2 (ashrdi3_iwmmxt, WSRADI)
12158 IWMMXT_BUILTIN2 (rorv4hi3_di, WRORH)
12159 IWMMXT_BUILTIN2 (rorv4hi3, WRORHI)
12160 IWMMXT_BUILTIN2 (rorv2si3_di, WRORW)
12161 IWMMXT_BUILTIN2 (rorv2si3, WRORWI)
12162 IWMMXT_BUILTIN2 (rordi3_di, WRORD)
12163 IWMMXT_BUILTIN2 (rordi3, WRORDI)
12164 IWMMXT_BUILTIN2 (iwmmxt_wmacuz, WMACUZ)
12165 IWMMXT_BUILTIN2 (iwmmxt_wmacsz, WMACSZ)
12168 static const struct builtin_description bdesc_1arg[] =
12170 IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
12171 IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
12172 IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
12173 IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
12174 IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
12175 IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
12176 IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
12177 IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
12178 IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
12179 IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
12180 IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
12181 IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
12182 IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
12183 IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
12184 IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
12185 IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
12186 IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
12187 IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
12190 /* Set up all the iWMMXt builtins. This is
12191 not called if TARGET_IWMMXT is zero. */
12193 static void
12194 arm_init_iwmmxt_builtins (void)
12196 const struct builtin_description * d;
12197 size_t i;
12198 tree endlink = void_list_node;
12200 tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
12201 tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
12202 tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
12204 tree int_ftype_int
12205 = build_function_type (integer_type_node,
12206 tree_cons (NULL_TREE, integer_type_node, endlink));
12207 tree v8qi_ftype_v8qi_v8qi_int
12208 = build_function_type (V8QI_type_node,
12209 tree_cons (NULL_TREE, V8QI_type_node,
12210 tree_cons (NULL_TREE, V8QI_type_node,
12211 tree_cons (NULL_TREE,
12212 integer_type_node,
12213 endlink))));
12214 tree v4hi_ftype_v4hi_int
12215 = build_function_type (V4HI_type_node,
12216 tree_cons (NULL_TREE, V4HI_type_node,
12217 tree_cons (NULL_TREE, integer_type_node,
12218 endlink)));
12219 tree v2si_ftype_v2si_int
12220 = build_function_type (V2SI_type_node,
12221 tree_cons (NULL_TREE, V2SI_type_node,
12222 tree_cons (NULL_TREE, integer_type_node,
12223 endlink)));
12224 tree v2si_ftype_di_di
12225 = build_function_type (V2SI_type_node,
12226 tree_cons (NULL_TREE, long_long_integer_type_node,
12227 tree_cons (NULL_TREE, long_long_integer_type_node,
12228 endlink)));
12229 tree di_ftype_di_int
12230 = build_function_type (long_long_integer_type_node,
12231 tree_cons (NULL_TREE, long_long_integer_type_node,
12232 tree_cons (NULL_TREE, integer_type_node,
12233 endlink)));
12234 tree di_ftype_di_int_int
12235 = build_function_type (long_long_integer_type_node,
12236 tree_cons (NULL_TREE, long_long_integer_type_node,
12237 tree_cons (NULL_TREE, integer_type_node,
12238 tree_cons (NULL_TREE,
12239 integer_type_node,
12240 endlink))));
12241 tree int_ftype_v8qi
12242 = build_function_type (integer_type_node,
12243 tree_cons (NULL_TREE, V8QI_type_node,
12244 endlink));
12245 tree int_ftype_v4hi
12246 = build_function_type (integer_type_node,
12247 tree_cons (NULL_TREE, V4HI_type_node,
12248 endlink));
12249 tree int_ftype_v2si
12250 = build_function_type (integer_type_node,
12251 tree_cons (NULL_TREE, V2SI_type_node,
12252 endlink));
12253 tree int_ftype_v8qi_int
12254 = build_function_type (integer_type_node,
12255 tree_cons (NULL_TREE, V8QI_type_node,
12256 tree_cons (NULL_TREE, integer_type_node,
12257 endlink)));
12258 tree int_ftype_v4hi_int
12259 = build_function_type (integer_type_node,
12260 tree_cons (NULL_TREE, V4HI_type_node,
12261 tree_cons (NULL_TREE, integer_type_node,
12262 endlink)));
12263 tree int_ftype_v2si_int
12264 = build_function_type (integer_type_node,
12265 tree_cons (NULL_TREE, V2SI_type_node,
12266 tree_cons (NULL_TREE, integer_type_node,
12267 endlink)));
12268 tree v8qi_ftype_v8qi_int_int
12269 = build_function_type (V8QI_type_node,
12270 tree_cons (NULL_TREE, V8QI_type_node,
12271 tree_cons (NULL_TREE, integer_type_node,
12272 tree_cons (NULL_TREE,
12273 integer_type_node,
12274 endlink))));
12275 tree v4hi_ftype_v4hi_int_int
12276 = build_function_type (V4HI_type_node,
12277 tree_cons (NULL_TREE, V4HI_type_node,
12278 tree_cons (NULL_TREE, integer_type_node,
12279 tree_cons (NULL_TREE,
12280 integer_type_node,
12281 endlink))));
12282 tree v2si_ftype_v2si_int_int
12283 = build_function_type (V2SI_type_node,
12284 tree_cons (NULL_TREE, V2SI_type_node,
12285 tree_cons (NULL_TREE, integer_type_node,
12286 tree_cons (NULL_TREE,
12287 integer_type_node,
12288 endlink))));
12289 /* Miscellaneous. */
12290 tree v8qi_ftype_v4hi_v4hi
12291 = build_function_type (V8QI_type_node,
12292 tree_cons (NULL_TREE, V4HI_type_node,
12293 tree_cons (NULL_TREE, V4HI_type_node,
12294 endlink)));
12295 tree v4hi_ftype_v2si_v2si
12296 = build_function_type (V4HI_type_node,
12297 tree_cons (NULL_TREE, V2SI_type_node,
12298 tree_cons (NULL_TREE, V2SI_type_node,
12299 endlink)));
12300 tree v2si_ftype_v4hi_v4hi
12301 = build_function_type (V2SI_type_node,
12302 tree_cons (NULL_TREE, V4HI_type_node,
12303 tree_cons (NULL_TREE, V4HI_type_node,
12304 endlink)));
12305 tree v2si_ftype_v8qi_v8qi
12306 = build_function_type (V2SI_type_node,
12307 tree_cons (NULL_TREE, V8QI_type_node,
12308 tree_cons (NULL_TREE, V8QI_type_node,
12309 endlink)));
12310 tree v4hi_ftype_v4hi_di
12311 = build_function_type (V4HI_type_node,
12312 tree_cons (NULL_TREE, V4HI_type_node,
12313 tree_cons (NULL_TREE,
12314 long_long_integer_type_node,
12315 endlink)));
12316 tree v2si_ftype_v2si_di
12317 = build_function_type (V2SI_type_node,
12318 tree_cons (NULL_TREE, V2SI_type_node,
12319 tree_cons (NULL_TREE,
12320 long_long_integer_type_node,
12321 endlink)));
12322 tree void_ftype_int_int
12323 = build_function_type (void_type_node,
12324 tree_cons (NULL_TREE, integer_type_node,
12325 tree_cons (NULL_TREE, integer_type_node,
12326 endlink)));
12327 tree di_ftype_void
12328 = build_function_type (long_long_unsigned_type_node, endlink);
12329 tree di_ftype_v8qi
12330 = build_function_type (long_long_integer_type_node,
12331 tree_cons (NULL_TREE, V8QI_type_node,
12332 endlink));
12333 tree di_ftype_v4hi
12334 = build_function_type (long_long_integer_type_node,
12335 tree_cons (NULL_TREE, V4HI_type_node,
12336 endlink));
12337 tree di_ftype_v2si
12338 = build_function_type (long_long_integer_type_node,
12339 tree_cons (NULL_TREE, V2SI_type_node,
12340 endlink));
12341 tree v2si_ftype_v4hi
12342 = build_function_type (V2SI_type_node,
12343 tree_cons (NULL_TREE, V4HI_type_node,
12344 endlink));
12345 tree v4hi_ftype_v8qi
12346 = build_function_type (V4HI_type_node,
12347 tree_cons (NULL_TREE, V8QI_type_node,
12348 endlink));
12350 tree di_ftype_di_v4hi_v4hi
12351 = build_function_type (long_long_unsigned_type_node,
12352 tree_cons (NULL_TREE,
12353 long_long_unsigned_type_node,
12354 tree_cons (NULL_TREE, V4HI_type_node,
12355 tree_cons (NULL_TREE,
12356 V4HI_type_node,
12357 endlink))));
12359 tree di_ftype_v4hi_v4hi
12360 = build_function_type (long_long_unsigned_type_node,
12361 tree_cons (NULL_TREE, V4HI_type_node,
12362 tree_cons (NULL_TREE, V4HI_type_node,
12363 endlink)));
12365 /* Normal vector binops. */
12366 tree v8qi_ftype_v8qi_v8qi
12367 = build_function_type (V8QI_type_node,
12368 tree_cons (NULL_TREE, V8QI_type_node,
12369 tree_cons (NULL_TREE, V8QI_type_node,
12370 endlink)));
12371 tree v4hi_ftype_v4hi_v4hi
12372 = build_function_type (V4HI_type_node,
12373 tree_cons (NULL_TREE, V4HI_type_node,
12374 tree_cons (NULL_TREE, V4HI_type_node,
12375 endlink)));
12376 tree v2si_ftype_v2si_v2si
12377 = build_function_type (V2SI_type_node,
12378 tree_cons (NULL_TREE, V2SI_type_node,
12379 tree_cons (NULL_TREE, V2SI_type_node,
12380 endlink)));
12381 tree di_ftype_di_di
12382 = build_function_type (long_long_unsigned_type_node,
12383 tree_cons (NULL_TREE, long_long_unsigned_type_node,
12384 tree_cons (NULL_TREE,
12385 long_long_unsigned_type_node,
12386 endlink)));
12388 /* Add all builtins that are more or less simple operations on two
12389 operands. */
12390 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12392 /* Use one of the operands; the target can have a different mode for
12393 mask-generating compares. */
12394 enum machine_mode mode;
12395 tree type;
12397 if (d->name == 0)
12398 continue;
12400 mode = insn_data[d->icode].operand[1].mode;
12402 switch (mode)
12404 case V8QImode:
12405 type = v8qi_ftype_v8qi_v8qi;
12406 break;
12407 case V4HImode:
12408 type = v4hi_ftype_v4hi_v4hi;
12409 break;
12410 case V2SImode:
12411 type = v2si_ftype_v2si_v2si;
12412 break;
12413 case DImode:
12414 type = di_ftype_di_di;
12415 break;
12417 default:
12418 gcc_unreachable ();
12421 def_mbuiltin (d->mask, d->name, type, d->code);
12424 /* Add the remaining MMX insns with somewhat more complicated types. */
12425 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
12426 def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
12427 def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
12429 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
12430 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
12431 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
12432 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
12433 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
12434 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
12436 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
12437 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
12438 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
12439 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
12440 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
12441 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
12443 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
12444 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
12445 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
12446 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
12447 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
12448 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
12450 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
12451 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
12452 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
12453 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
12454 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
12455 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
12457 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
12459 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
12460 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
12461 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
12462 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
12464 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
12465 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
12466 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
12467 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
12468 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
12469 def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
12470 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
12471 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
12472 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
12474 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
12475 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
12476 def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
12478 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
12479 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
12480 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
12482 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
12483 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
12484 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
12485 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
12486 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
12487 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
12489 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
12490 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
12491 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
12492 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
12493 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
12494 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
12495 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
12496 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
12497 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
12498 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
12499 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
12500 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
12502 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
12503 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
12504 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
12505 def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
12507 def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
12508 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
12509 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
12510 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
12511 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
12512 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
12513 def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
12516 static void
12517 arm_init_tls_builtins (void)
12519 tree ftype;
12520 tree nothrow = tree_cons (get_identifier ("nothrow"), NULL, NULL);
12521 tree const_nothrow = tree_cons (get_identifier ("const"), NULL, nothrow);
12523 ftype = build_function_type (ptr_type_node, void_list_node);
12524 lang_hooks.builtin_function ("__builtin_thread_pointer", ftype,
12525 ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
12526 NULL, const_nothrow);
12529 static void
12530 arm_init_builtins (void)
12532 arm_init_tls_builtins ();
12534 if (TARGET_REALLY_IWMMXT)
12535 arm_init_iwmmxt_builtins ();
12538 /* Errors in the source file can cause expand_expr to return const0_rtx
12539 where we expect a vector. To avoid crashing, use one of the vector
12540 clear instructions. */
12542 static rtx
12543 safe_vector_operand (rtx x, enum machine_mode mode)
12545 if (x != const0_rtx)
12546 return x;
12547 x = gen_reg_rtx (mode);
12549 emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12550 : gen_rtx_SUBREG (DImode, x, 0)));
12551 return x;
12554 /* Subroutine of arm_expand_builtin to take care of binop insns. */
12556 static rtx
12557 arm_expand_binop_builtin (enum insn_code icode,
12558 tree arglist, rtx target)
12560 rtx pat;
12561 tree arg0 = TREE_VALUE (arglist);
12562 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12563 rtx op0 = expand_normal (arg0);
12564 rtx op1 = expand_normal (arg1);
12565 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12566 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12567 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12569 if (VECTOR_MODE_P (mode0))
12570 op0 = safe_vector_operand (op0, mode0);
12571 if (VECTOR_MODE_P (mode1))
12572 op1 = safe_vector_operand (op1, mode1);
12574 if (! target
12575 || GET_MODE (target) != tmode
12576 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12577 target = gen_reg_rtx (tmode);
12579 gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
12581 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12582 op0 = copy_to_mode_reg (mode0, op0);
12583 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12584 op1 = copy_to_mode_reg (mode1, op1);
12586 pat = GEN_FCN (icode) (target, op0, op1);
12587 if (! pat)
12588 return 0;
12589 emit_insn (pat);
12590 return target;
12593 /* Subroutine of arm_expand_builtin to take care of unop insns. */
12595 static rtx
12596 arm_expand_unop_builtin (enum insn_code icode,
12597 tree arglist, rtx target, int do_load)
12599 rtx pat;
12600 tree arg0 = TREE_VALUE (arglist);
12601 rtx op0 = expand_normal (arg0);
12602 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12603 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12605 if (! target
12606 || GET_MODE (target) != tmode
12607 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12608 target = gen_reg_rtx (tmode);
12609 if (do_load)
12610 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12611 else
12613 if (VECTOR_MODE_P (mode0))
12614 op0 = safe_vector_operand (op0, mode0);
12616 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12617 op0 = copy_to_mode_reg (mode0, op0);
12620 pat = GEN_FCN (icode) (target, op0);
12621 if (! pat)
12622 return 0;
12623 emit_insn (pat);
12624 return target;
12627 /* Expand an expression EXP that calls a built-in function,
12628 with result going to TARGET if that's convenient
12629 (and in mode MODE if that's convenient).
12630 SUBTARGET may be used as the target for computing one of EXP's operands.
12631 IGNORE is nonzero if the value is to be ignored. */
12633 static rtx
12634 arm_expand_builtin (tree exp,
12635 rtx target,
12636 rtx subtarget ATTRIBUTE_UNUSED,
12637 enum machine_mode mode ATTRIBUTE_UNUSED,
12638 int ignore ATTRIBUTE_UNUSED)
12640 const struct builtin_description * d;
12641 enum insn_code icode;
12642 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12643 tree arglist = TREE_OPERAND (exp, 1);
12644 tree arg0;
12645 tree arg1;
12646 tree arg2;
12647 rtx op0;
12648 rtx op1;
12649 rtx op2;
12650 rtx pat;
12651 int fcode = DECL_FUNCTION_CODE (fndecl);
12652 size_t i;
12653 enum machine_mode tmode;
12654 enum machine_mode mode0;
12655 enum machine_mode mode1;
12656 enum machine_mode mode2;
12658 switch (fcode)
12660 case ARM_BUILTIN_TEXTRMSB:
12661 case ARM_BUILTIN_TEXTRMUB:
12662 case ARM_BUILTIN_TEXTRMSH:
12663 case ARM_BUILTIN_TEXTRMUH:
12664 case ARM_BUILTIN_TEXTRMSW:
12665 case ARM_BUILTIN_TEXTRMUW:
12666 icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12667 : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12668 : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12669 : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12670 : CODE_FOR_iwmmxt_textrmw);
12672 arg0 = TREE_VALUE (arglist);
12673 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12674 op0 = expand_normal (arg0);
12675 op1 = expand_normal (arg1);
12676 tmode = insn_data[icode].operand[0].mode;
12677 mode0 = insn_data[icode].operand[1].mode;
12678 mode1 = insn_data[icode].operand[2].mode;
12680 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12681 op0 = copy_to_mode_reg (mode0, op0);
12682 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12684 /* @@@ better error message */
12685 error ("selector must be an immediate");
12686 return gen_reg_rtx (tmode);
12688 if (target == 0
12689 || GET_MODE (target) != tmode
12690 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12691 target = gen_reg_rtx (tmode);
12692 pat = GEN_FCN (icode) (target, op0, op1);
12693 if (! pat)
12694 return 0;
12695 emit_insn (pat);
12696 return target;
12698 case ARM_BUILTIN_TINSRB:
12699 case ARM_BUILTIN_TINSRH:
12700 case ARM_BUILTIN_TINSRW:
12701 icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12702 : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12703 : CODE_FOR_iwmmxt_tinsrw);
12704 arg0 = TREE_VALUE (arglist);
12705 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12706 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12707 op0 = expand_normal (arg0);
12708 op1 = expand_normal (arg1);
12709 op2 = expand_normal (arg2);
12710 tmode = insn_data[icode].operand[0].mode;
12711 mode0 = insn_data[icode].operand[1].mode;
12712 mode1 = insn_data[icode].operand[2].mode;
12713 mode2 = insn_data[icode].operand[3].mode;
12715 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12716 op0 = copy_to_mode_reg (mode0, op0);
12717 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12718 op1 = copy_to_mode_reg (mode1, op1);
12719 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12721 /* @@@ better error message */
12722 error ("selector must be an immediate");
12723 return const0_rtx;
12725 if (target == 0
12726 || GET_MODE (target) != tmode
12727 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12728 target = gen_reg_rtx (tmode);
12729 pat = GEN_FCN (icode) (target, op0, op1, op2);
12730 if (! pat)
12731 return 0;
12732 emit_insn (pat);
12733 return target;
12735 case ARM_BUILTIN_SETWCX:
12736 arg0 = TREE_VALUE (arglist);
12737 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12738 op0 = force_reg (SImode, expand_normal (arg0));
12739 op1 = expand_normal (arg1);
12740 emit_insn (gen_iwmmxt_tmcr (op1, op0));
12741 return 0;
12743 case ARM_BUILTIN_GETWCX:
12744 arg0 = TREE_VALUE (arglist);
12745 op0 = expand_normal (arg0);
12746 target = gen_reg_rtx (SImode);
12747 emit_insn (gen_iwmmxt_tmrc (target, op0));
12748 return target;
12750 case ARM_BUILTIN_WSHUFH:
12751 icode = CODE_FOR_iwmmxt_wshufh;
12752 arg0 = TREE_VALUE (arglist);
12753 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12754 op0 = expand_normal (arg0);
12755 op1 = expand_normal (arg1);
12756 tmode = insn_data[icode].operand[0].mode;
12757 mode1 = insn_data[icode].operand[1].mode;
12758 mode2 = insn_data[icode].operand[2].mode;
12760 if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12761 op0 = copy_to_mode_reg (mode1, op0);
12762 if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12764 /* @@@ better error message */
12765 error ("mask must be an immediate");
12766 return const0_rtx;
12768 if (target == 0
12769 || GET_MODE (target) != tmode
12770 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12771 target = gen_reg_rtx (tmode);
12772 pat = GEN_FCN (icode) (target, op0, op1);
12773 if (! pat)
12774 return 0;
12775 emit_insn (pat);
12776 return target;
12778 case ARM_BUILTIN_WSADB:
12779 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12780 case ARM_BUILTIN_WSADH:
12781 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12782 case ARM_BUILTIN_WSADBZ:
12783 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12784 case ARM_BUILTIN_WSADHZ:
12785 return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12787 /* Several three-argument builtins. */
12788 case ARM_BUILTIN_WMACS:
12789 case ARM_BUILTIN_WMACU:
12790 case ARM_BUILTIN_WALIGN:
12791 case ARM_BUILTIN_TMIA:
12792 case ARM_BUILTIN_TMIAPH:
12793 case ARM_BUILTIN_TMIATT:
12794 case ARM_BUILTIN_TMIATB:
12795 case ARM_BUILTIN_TMIABT:
12796 case ARM_BUILTIN_TMIABB:
12797 icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12798 : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12799 : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12800 : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12801 : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12802 : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12803 : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12804 : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12805 : CODE_FOR_iwmmxt_walign);
12806 arg0 = TREE_VALUE (arglist);
12807 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12808 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12809 op0 = expand_normal (arg0);
12810 op1 = expand_normal (arg1);
12811 op2 = expand_normal (arg2);
12812 tmode = insn_data[icode].operand[0].mode;
12813 mode0 = insn_data[icode].operand[1].mode;
12814 mode1 = insn_data[icode].operand[2].mode;
12815 mode2 = insn_data[icode].operand[3].mode;
12817 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12818 op0 = copy_to_mode_reg (mode0, op0);
12819 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12820 op1 = copy_to_mode_reg (mode1, op1);
12821 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12822 op2 = copy_to_mode_reg (mode2, op2);
12823 if (target == 0
12824 || GET_MODE (target) != tmode
12825 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12826 target = gen_reg_rtx (tmode);
12827 pat = GEN_FCN (icode) (target, op0, op1, op2);
12828 if (! pat)
12829 return 0;
12830 emit_insn (pat);
12831 return target;
12833 case ARM_BUILTIN_WZERO:
12834 target = gen_reg_rtx (DImode);
12835 emit_insn (gen_iwmmxt_clrdi (target));
12836 return target;
12838 case ARM_BUILTIN_THREAD_POINTER:
12839 return arm_load_tp (target);
12841 default:
12842 break;
12845 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12846 if (d->code == (const enum arm_builtins) fcode)
12847 return arm_expand_binop_builtin (d->icode, arglist, target);
12849 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12850 if (d->code == (const enum arm_builtins) fcode)
12851 return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12853 /* @@@ Should really do something sensible here. */
12854 return NULL_RTX;
12857 /* Return the number (counting from 0) of
12858 the least significant set bit in MASK. */
12860 inline static int
12861 number_of_first_bit_set (unsigned mask)
12863 int bit;
12865 for (bit = 0;
12866 (mask & (1 << bit)) == 0;
12867 ++bit)
12868 continue;
12870 return bit;
12873 /* Emit code to push or pop registers to or from the stack. F is the
12874 assembly file. MASK is the registers to push or pop. PUSH is
12875 nonzero if we should push, and zero if we should pop. For debugging
12876 output, if pushing, adjust CFA_OFFSET by the amount of space added
12877 to the stack. REAL_REGS should have the same number of bits set as
12878 MASK, and will be used instead (in the same order) to describe which
12879 registers were saved - this is used to mark the save slots when we
12880 push high registers after moving them to low registers. */
12881 static void
12882 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
12883 unsigned long real_regs)
12885 int regno;
12886 int lo_mask = mask & 0xFF;
12887 int pushed_words = 0;
12889 gcc_assert (mask);
12891 if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12893 /* Special case. Do not generate a POP PC statement here, do it in
12894 thumb_exit() */
12895 thumb_exit (f, -1);
12896 return;
12899 if (ARM_EABI_UNWIND_TABLES && push)
12901 fprintf (f, "\t.save\t{");
12902 for (regno = 0; regno < 15; regno++)
12904 if (real_regs & (1 << regno))
12906 if (real_regs & ((1 << regno) -1))
12907 fprintf (f, ", ");
12908 asm_fprintf (f, "%r", regno);
12911 fprintf (f, "}\n");
12914 fprintf (f, "\t%s\t{", push ? "push" : "pop");
12916 /* Look at the low registers first. */
12917 for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12919 if (lo_mask & 1)
12921 asm_fprintf (f, "%r", regno);
12923 if ((lo_mask & ~1) != 0)
12924 fprintf (f, ", ");
12926 pushed_words++;
12930 if (push && (mask & (1 << LR_REGNUM)))
12932 /* Catch pushing the LR. */
12933 if (mask & 0xFF)
12934 fprintf (f, ", ");
12936 asm_fprintf (f, "%r", LR_REGNUM);
12938 pushed_words++;
12940 else if (!push && (mask & (1 << PC_REGNUM)))
12942 /* Catch popping the PC. */
12943 if (TARGET_INTERWORK || TARGET_BACKTRACE
12944 || current_function_calls_eh_return)
12946 /* The PC is never poped directly, instead
12947 it is popped into r3 and then BX is used. */
12948 fprintf (f, "}\n");
12950 thumb_exit (f, -1);
12952 return;
12954 else
12956 if (mask & 0xFF)
12957 fprintf (f, ", ");
12959 asm_fprintf (f, "%r", PC_REGNUM);
12963 fprintf (f, "}\n");
12965 if (push && pushed_words && dwarf2out_do_frame ())
12967 char *l = dwarf2out_cfi_label ();
12968 int pushed_mask = real_regs;
12970 *cfa_offset += pushed_words * 4;
12971 dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12973 pushed_words = 0;
12974 pushed_mask = real_regs;
12975 for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12977 if (pushed_mask & 1)
12978 dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12983 /* Generate code to return from a thumb function.
12984 If 'reg_containing_return_addr' is -1, then the return address is
12985 actually on the stack, at the stack pointer. */
12986 static void
12987 thumb_exit (FILE *f, int reg_containing_return_addr)
12989 unsigned regs_available_for_popping;
12990 unsigned regs_to_pop;
12991 int pops_needed;
12992 unsigned available;
12993 unsigned required;
12994 int mode;
12995 int size;
12996 int restore_a4 = FALSE;
12998 /* Compute the registers we need to pop. */
12999 regs_to_pop = 0;
13000 pops_needed = 0;
13002 if (reg_containing_return_addr == -1)
13004 regs_to_pop |= 1 << LR_REGNUM;
13005 ++pops_needed;
13008 if (TARGET_BACKTRACE)
13010 /* Restore the (ARM) frame pointer and stack pointer. */
13011 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
13012 pops_needed += 2;
13015 /* If there is nothing to pop then just emit the BX instruction and
13016 return. */
13017 if (pops_needed == 0)
13019 if (current_function_calls_eh_return)
13020 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
13022 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
13023 return;
13025 /* Otherwise if we are not supporting interworking and we have not created
13026 a backtrace structure and the function was not entered in ARM mode then
13027 just pop the return address straight into the PC. */
13028 else if (!TARGET_INTERWORK
13029 && !TARGET_BACKTRACE
13030 && !is_called_in_ARM_mode (current_function_decl)
13031 && !current_function_calls_eh_return)
13033 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
13034 return;
13037 /* Find out how many of the (return) argument registers we can corrupt. */
13038 regs_available_for_popping = 0;
13040 /* If returning via __builtin_eh_return, the bottom three registers
13041 all contain information needed for the return. */
13042 if (current_function_calls_eh_return)
13043 size = 12;
13044 else
13046 /* If we can deduce the registers used from the function's
13047 return value. This is more reliable that examining
13048 regs_ever_live[] because that will be set if the register is
13049 ever used in the function, not just if the register is used
13050 to hold a return value. */
13052 if (current_function_return_rtx != 0)
13053 mode = GET_MODE (current_function_return_rtx);
13054 else
13055 mode = DECL_MODE (DECL_RESULT (current_function_decl));
13057 size = GET_MODE_SIZE (mode);
13059 if (size == 0)
13061 /* In a void function we can use any argument register.
13062 In a function that returns a structure on the stack
13063 we can use the second and third argument registers. */
13064 if (mode == VOIDmode)
13065 regs_available_for_popping =
13066 (1 << ARG_REGISTER (1))
13067 | (1 << ARG_REGISTER (2))
13068 | (1 << ARG_REGISTER (3));
13069 else
13070 regs_available_for_popping =
13071 (1 << ARG_REGISTER (2))
13072 | (1 << ARG_REGISTER (3));
13074 else if (size <= 4)
13075 regs_available_for_popping =
13076 (1 << ARG_REGISTER (2))
13077 | (1 << ARG_REGISTER (3));
13078 else if (size <= 8)
13079 regs_available_for_popping =
13080 (1 << ARG_REGISTER (3));
13083 /* Match registers to be popped with registers into which we pop them. */
13084 for (available = regs_available_for_popping,
13085 required = regs_to_pop;
13086 required != 0 && available != 0;
13087 available &= ~(available & - available),
13088 required &= ~(required & - required))
13089 -- pops_needed;
13091 /* If we have any popping registers left over, remove them. */
13092 if (available > 0)
13093 regs_available_for_popping &= ~available;
13095 /* Otherwise if we need another popping register we can use
13096 the fourth argument register. */
13097 else if (pops_needed)
13099 /* If we have not found any free argument registers and
13100 reg a4 contains the return address, we must move it. */
13101 if (regs_available_for_popping == 0
13102 && reg_containing_return_addr == LAST_ARG_REGNUM)
13104 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
13105 reg_containing_return_addr = LR_REGNUM;
13107 else if (size > 12)
13109 /* Register a4 is being used to hold part of the return value,
13110 but we have dire need of a free, low register. */
13111 restore_a4 = TRUE;
13113 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
13116 if (reg_containing_return_addr != LAST_ARG_REGNUM)
13118 /* The fourth argument register is available. */
13119 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
13121 --pops_needed;
13125 /* Pop as many registers as we can. */
13126 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13127 regs_available_for_popping);
13129 /* Process the registers we popped. */
13130 if (reg_containing_return_addr == -1)
13132 /* The return address was popped into the lowest numbered register. */
13133 regs_to_pop &= ~(1 << LR_REGNUM);
13135 reg_containing_return_addr =
13136 number_of_first_bit_set (regs_available_for_popping);
13138 /* Remove this register for the mask of available registers, so that
13139 the return address will not be corrupted by further pops. */
13140 regs_available_for_popping &= ~(1 << reg_containing_return_addr);
13143 /* If we popped other registers then handle them here. */
13144 if (regs_available_for_popping)
13146 int frame_pointer;
13148 /* Work out which register currently contains the frame pointer. */
13149 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
13151 /* Move it into the correct place. */
13152 asm_fprintf (f, "\tmov\t%r, %r\n",
13153 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
13155 /* (Temporarily) remove it from the mask of popped registers. */
13156 regs_available_for_popping &= ~(1 << frame_pointer);
13157 regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
13159 if (regs_available_for_popping)
13161 int stack_pointer;
13163 /* We popped the stack pointer as well,
13164 find the register that contains it. */
13165 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
13167 /* Move it into the stack register. */
13168 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
13170 /* At this point we have popped all necessary registers, so
13171 do not worry about restoring regs_available_for_popping
13172 to its correct value:
13174 assert (pops_needed == 0)
13175 assert (regs_available_for_popping == (1 << frame_pointer))
13176 assert (regs_to_pop == (1 << STACK_POINTER)) */
13178 else
13180 /* Since we have just move the popped value into the frame
13181 pointer, the popping register is available for reuse, and
13182 we know that we still have the stack pointer left to pop. */
13183 regs_available_for_popping |= (1 << frame_pointer);
13187 /* If we still have registers left on the stack, but we no longer have
13188 any registers into which we can pop them, then we must move the return
13189 address into the link register and make available the register that
13190 contained it. */
13191 if (regs_available_for_popping == 0 && pops_needed > 0)
13193 regs_available_for_popping |= 1 << reg_containing_return_addr;
13195 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
13196 reg_containing_return_addr);
13198 reg_containing_return_addr = LR_REGNUM;
13201 /* If we have registers left on the stack then pop some more.
13202 We know that at most we will want to pop FP and SP. */
13203 if (pops_needed > 0)
13205 int popped_into;
13206 int move_to;
13208 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13209 regs_available_for_popping);
13211 /* We have popped either FP or SP.
13212 Move whichever one it is into the correct register. */
13213 popped_into = number_of_first_bit_set (regs_available_for_popping);
13214 move_to = number_of_first_bit_set (regs_to_pop);
13216 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
13218 regs_to_pop &= ~(1 << move_to);
13220 --pops_needed;
13223 /* If we still have not popped everything then we must have only
13224 had one register available to us and we are now popping the SP. */
13225 if (pops_needed > 0)
13227 int popped_into;
13229 thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13230 regs_available_for_popping);
13232 popped_into = number_of_first_bit_set (regs_available_for_popping);
13234 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
13236 assert (regs_to_pop == (1 << STACK_POINTER))
13237 assert (pops_needed == 1)
13241 /* If necessary restore the a4 register. */
13242 if (restore_a4)
13244 if (reg_containing_return_addr != LR_REGNUM)
13246 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
13247 reg_containing_return_addr = LR_REGNUM;
13250 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
13253 if (current_function_calls_eh_return)
13254 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
13256 /* Return to caller. */
13257 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
13261 void
13262 thumb_final_prescan_insn (rtx insn)
13264 if (flag_print_asm_name)
13265 asm_fprintf (asm_out_file, "%@ 0x%04x\n",
13266 INSN_ADDRESSES (INSN_UID (insn)));
13270 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
13272 unsigned HOST_WIDE_INT mask = 0xff;
13273 int i;
13275 if (val == 0) /* XXX */
13276 return 0;
13278 for (i = 0; i < 25; i++)
13279 if ((val & (mask << i)) == val)
13280 return 1;
13282 return 0;
13285 /* Returns nonzero if the current function contains,
13286 or might contain a far jump. */
13287 static int
13288 thumb_far_jump_used_p (void)
13290 rtx insn;
13292 /* This test is only important for leaf functions. */
13293 /* assert (!leaf_function_p ()); */
13295 /* If we have already decided that far jumps may be used,
13296 do not bother checking again, and always return true even if
13297 it turns out that they are not being used. Once we have made
13298 the decision that far jumps are present (and that hence the link
13299 register will be pushed onto the stack) we cannot go back on it. */
13300 if (cfun->machine->far_jump_used)
13301 return 1;
13303 /* If this function is not being called from the prologue/epilogue
13304 generation code then it must be being called from the
13305 INITIAL_ELIMINATION_OFFSET macro. */
13306 if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
13308 /* In this case we know that we are being asked about the elimination
13309 of the arg pointer register. If that register is not being used,
13310 then there are no arguments on the stack, and we do not have to
13311 worry that a far jump might force the prologue to push the link
13312 register, changing the stack offsets. In this case we can just
13313 return false, since the presence of far jumps in the function will
13314 not affect stack offsets.
13316 If the arg pointer is live (or if it was live, but has now been
13317 eliminated and so set to dead) then we do have to test to see if
13318 the function might contain a far jump. This test can lead to some
13319 false negatives, since before reload is completed, then length of
13320 branch instructions is not known, so gcc defaults to returning their
13321 longest length, which in turn sets the far jump attribute to true.
13323 A false negative will not result in bad code being generated, but it
13324 will result in a needless push and pop of the link register. We
13325 hope that this does not occur too often.
13327 If we need doubleword stack alignment this could affect the other
13328 elimination offsets so we can't risk getting it wrong. */
13329 if (regs_ever_live [ARG_POINTER_REGNUM])
13330 cfun->machine->arg_pointer_live = 1;
13331 else if (!cfun->machine->arg_pointer_live)
13332 return 0;
13335 /* Check to see if the function contains a branch
13336 insn with the far jump attribute set. */
13337 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
13339 if (GET_CODE (insn) == JUMP_INSN
13340 /* Ignore tablejump patterns. */
13341 && GET_CODE (PATTERN (insn)) != ADDR_VEC
13342 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
13343 && get_attr_far_jump (insn) == FAR_JUMP_YES
13346 /* Record the fact that we have decided that
13347 the function does use far jumps. */
13348 cfun->machine->far_jump_used = 1;
13349 return 1;
13353 return 0;
13356 /* Return nonzero if FUNC must be entered in ARM mode. */
13358 is_called_in_ARM_mode (tree func)
13360 gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
13362 /* Ignore the problem about functions whose address is taken. */
13363 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
13364 return TRUE;
13366 #ifdef ARM_PE
13367 return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
13368 #else
13369 return FALSE;
13370 #endif
13373 /* The bits which aren't usefully expanded as rtl. */
13374 const char *
13375 thumb_unexpanded_epilogue (void)
13377 int regno;
13378 unsigned long live_regs_mask = 0;
13379 int high_regs_pushed = 0;
13380 int had_to_push_lr;
13381 int size;
13383 if (return_used_this_function)
13384 return "";
13386 if (IS_NAKED (arm_current_func_type ()))
13387 return "";
13389 live_regs_mask = thumb_compute_save_reg_mask ();
13390 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13392 /* If we can deduce the registers used from the function's return value.
13393 This is more reliable that examining regs_ever_live[] because that
13394 will be set if the register is ever used in the function, not just if
13395 the register is used to hold a return value. */
13396 size = arm_size_return_regs ();
13398 /* The prolog may have pushed some high registers to use as
13399 work registers. e.g. the testsuite file:
13400 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
13401 compiles to produce:
13402 push {r4, r5, r6, r7, lr}
13403 mov r7, r9
13404 mov r6, r8
13405 push {r6, r7}
13406 as part of the prolog. We have to undo that pushing here. */
13408 if (high_regs_pushed)
13410 unsigned long mask = live_regs_mask & 0xff;
13411 int next_hi_reg;
13413 /* The available low registers depend on the size of the value we are
13414 returning. */
13415 if (size <= 12)
13416 mask |= 1 << 3;
13417 if (size <= 8)
13418 mask |= 1 << 2;
13420 if (mask == 0)
13421 /* Oh dear! We have no low registers into which we can pop
13422 high registers! */
13423 internal_error
13424 ("no low registers available for popping high registers");
13426 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
13427 if (live_regs_mask & (1 << next_hi_reg))
13428 break;
13430 while (high_regs_pushed)
13432 /* Find lo register(s) into which the high register(s) can
13433 be popped. */
13434 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13436 if (mask & (1 << regno))
13437 high_regs_pushed--;
13438 if (high_regs_pushed == 0)
13439 break;
13442 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
13444 /* Pop the values into the low register(s). */
13445 thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
13447 /* Move the value(s) into the high registers. */
13448 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13450 if (mask & (1 << regno))
13452 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
13453 regno);
13455 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
13456 if (live_regs_mask & (1 << next_hi_reg))
13457 break;
13461 live_regs_mask &= ~0x0f00;
13464 had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
13465 live_regs_mask &= 0xff;
13467 if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
13469 /* Pop the return address into the PC. */
13470 if (had_to_push_lr)
13471 live_regs_mask |= 1 << PC_REGNUM;
13473 /* Either no argument registers were pushed or a backtrace
13474 structure was created which includes an adjusted stack
13475 pointer, so just pop everything. */
13476 if (live_regs_mask)
13477 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13478 live_regs_mask);
13480 /* We have either just popped the return address into the
13481 PC or it is was kept in LR for the entire function. */
13482 if (!had_to_push_lr)
13483 thumb_exit (asm_out_file, LR_REGNUM);
13485 else
13487 /* Pop everything but the return address. */
13488 if (live_regs_mask)
13489 thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13490 live_regs_mask);
13492 if (had_to_push_lr)
13494 if (size > 12)
13496 /* We have no free low regs, so save one. */
13497 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
13498 LAST_ARG_REGNUM);
13501 /* Get the return address into a temporary register. */
13502 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
13503 1 << LAST_ARG_REGNUM);
13505 if (size > 12)
13507 /* Move the return address to lr. */
13508 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
13509 LAST_ARG_REGNUM);
13510 /* Restore the low register. */
13511 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
13512 IP_REGNUM);
13513 regno = LR_REGNUM;
13515 else
13516 regno = LAST_ARG_REGNUM;
13518 else
13519 regno = LR_REGNUM;
13521 /* Remove the argument registers that were pushed onto the stack. */
13522 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13523 SP_REGNUM, SP_REGNUM,
13524 current_function_pretend_args_size);
13526 thumb_exit (asm_out_file, regno);
13529 return "";
13532 /* Functions to save and restore machine-specific function data. */
13533 static struct machine_function *
13534 arm_init_machine_status (void)
13536 struct machine_function *machine;
13537 machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13539 #if ARM_FT_UNKNOWN != 0
13540 machine->func_type = ARM_FT_UNKNOWN;
13541 #endif
13542 return machine;
13545 /* Return an RTX indicating where the return address to the
13546 calling function can be found. */
13548 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13550 if (count != 0)
13551 return NULL_RTX;
13553 return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13556 /* Do anything needed before RTL is emitted for each function. */
13557 void
13558 arm_init_expanders (void)
13560 /* Arrange to initialize and mark the machine per-function status. */
13561 init_machine_status = arm_init_machine_status;
13563 /* This is to stop the combine pass optimizing away the alignment
13564 adjustment of va_arg. */
13565 /* ??? It is claimed that this should not be necessary. */
13566 if (cfun)
13567 mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
13571 /* Like arm_compute_initial_elimination offset. Simpler because there
13572 isn't an ABI specified frame pointer for Thumb. Instead, we set it
13573 to point at the base of the local variables after static stack
13574 space for a function has been allocated. */
13576 HOST_WIDE_INT
13577 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13579 arm_stack_offsets *offsets;
13581 offsets = arm_get_frame_offsets ();
13583 switch (from)
13585 case ARG_POINTER_REGNUM:
13586 switch (to)
13588 case STACK_POINTER_REGNUM:
13589 return offsets->outgoing_args - offsets->saved_args;
13591 case FRAME_POINTER_REGNUM:
13592 return offsets->soft_frame - offsets->saved_args;
13594 case ARM_HARD_FRAME_POINTER_REGNUM:
13595 return offsets->saved_regs - offsets->saved_args;
13597 case THUMB_HARD_FRAME_POINTER_REGNUM:
13598 return offsets->locals_base - offsets->saved_args;
13600 default:
13601 gcc_unreachable ();
13603 break;
13605 case FRAME_POINTER_REGNUM:
13606 switch (to)
13608 case STACK_POINTER_REGNUM:
13609 return offsets->outgoing_args - offsets->soft_frame;
13611 case ARM_HARD_FRAME_POINTER_REGNUM:
13612 return offsets->saved_regs - offsets->soft_frame;
13614 case THUMB_HARD_FRAME_POINTER_REGNUM:
13615 return offsets->locals_base - offsets->soft_frame;
13617 default:
13618 gcc_unreachable ();
13620 break;
13622 default:
13623 gcc_unreachable ();
13628 /* Generate the rest of a function's prologue. */
13629 void
13630 thumb_expand_prologue (void)
13632 rtx insn, dwarf;
13634 HOST_WIDE_INT amount;
13635 arm_stack_offsets *offsets;
13636 unsigned long func_type;
13637 int regno;
13638 unsigned long live_regs_mask;
13640 func_type = arm_current_func_type ();
13642 /* Naked functions don't have prologues. */
13643 if (IS_NAKED (func_type))
13644 return;
13646 if (IS_INTERRUPT (func_type))
13648 error ("interrupt Service Routines cannot be coded in Thumb mode");
13649 return;
13652 live_regs_mask = thumb_compute_save_reg_mask ();
13653 /* Load the pic register before setting the frame pointer,
13654 so we can use r7 as a temporary work register. */
13655 if (flag_pic && arm_pic_register != INVALID_REGNUM)
13656 arm_load_pic_register (live_regs_mask);
13658 if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
13659 emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
13660 stack_pointer_rtx);
13662 offsets = arm_get_frame_offsets ();
13663 amount = offsets->outgoing_args - offsets->saved_regs;
13664 if (amount)
13666 if (amount < 512)
13668 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13669 GEN_INT (- amount)));
13670 RTX_FRAME_RELATED_P (insn) = 1;
13672 else
13674 rtx reg;
13676 /* The stack decrement is too big for an immediate value in a single
13677 insn. In theory we could issue multiple subtracts, but after
13678 three of them it becomes more space efficient to place the full
13679 value in the constant pool and load into a register. (Also the
13680 ARM debugger really likes to see only one stack decrement per
13681 function). So instead we look for a scratch register into which
13682 we can load the decrement, and then we subtract this from the
13683 stack pointer. Unfortunately on the thumb the only available
13684 scratch registers are the argument registers, and we cannot use
13685 these as they may hold arguments to the function. Instead we
13686 attempt to locate a call preserved register which is used by this
13687 function. If we can find one, then we know that it will have
13688 been pushed at the start of the prologue and so we can corrupt
13689 it now. */
13690 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13691 if (live_regs_mask & (1 << regno)
13692 && !(frame_pointer_needed
13693 && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13694 break;
13696 if (regno > LAST_LO_REGNUM) /* Very unlikely. */
13698 rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13700 /* Choose an arbitrary, non-argument low register. */
13701 reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13703 /* Save it by copying it into a high, scratch register. */
13704 emit_insn (gen_movsi (spare, reg));
13705 /* Add a USE to stop propagate_one_insn() from barfing. */
13706 emit_insn (gen_prologue_use (spare));
13708 /* Decrement the stack. */
13709 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13710 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13711 stack_pointer_rtx, reg));
13712 RTX_FRAME_RELATED_P (insn) = 1;
13713 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
13714 plus_constant (stack_pointer_rtx,
13715 -amount));
13716 RTX_FRAME_RELATED_P (dwarf) = 1;
13717 REG_NOTES (insn)
13718 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13719 REG_NOTES (insn));
13721 /* Restore the low register's original value. */
13722 emit_insn (gen_movsi (reg, spare));
13724 /* Emit a USE of the restored scratch register, so that flow
13725 analysis will not consider the restore redundant. The
13726 register won't be used again in this function and isn't
13727 restored by the epilogue. */
13728 emit_insn (gen_prologue_use (reg));
13730 else
13732 reg = gen_rtx_REG (SImode, regno);
13734 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13736 insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13737 stack_pointer_rtx, reg));
13738 RTX_FRAME_RELATED_P (insn) = 1;
13739 dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
13740 plus_constant (stack_pointer_rtx,
13741 -amount));
13742 RTX_FRAME_RELATED_P (dwarf) = 1;
13743 REG_NOTES (insn)
13744 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13745 REG_NOTES (insn));
13750 if (frame_pointer_needed)
13752 amount = offsets->outgoing_args - offsets->locals_base;
13754 if (amount < 1024)
13755 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13756 stack_pointer_rtx, GEN_INT (amount)));
13757 else
13759 emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
13760 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13761 hard_frame_pointer_rtx,
13762 stack_pointer_rtx));
13763 dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
13764 plus_constant (stack_pointer_rtx, amount));
13765 RTX_FRAME_RELATED_P (dwarf) = 1;
13766 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13767 REG_NOTES (insn));
13770 RTX_FRAME_RELATED_P (insn) = 1;
13773 /* If we are profiling, make sure no instructions are scheduled before
13774 the call to mcount. Similarly if the user has requested no
13775 scheduling in the prolog. Similarly if we want non-call exceptions
13776 using the EABI unwinder, to prevent faulting instructions from being
13777 swapped with a stack adjustment. */
13778 if (current_function_profile || !TARGET_SCHED_PROLOG
13779 || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
13780 emit_insn (gen_blockage ());
13782 cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13783 if (live_regs_mask & 0xff)
13784 cfun->machine->lr_save_eliminated = 0;
13786 /* If the link register is being kept alive, with the return address in it,
13787 then make sure that it does not get reused by the ce2 pass. */
13788 if (cfun->machine->lr_save_eliminated)
13789 emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13793 void
13794 thumb_expand_epilogue (void)
13796 HOST_WIDE_INT amount;
13797 arm_stack_offsets *offsets;
13798 int regno;
13800 /* Naked functions don't have prologues. */
13801 if (IS_NAKED (arm_current_func_type ()))
13802 return;
13804 offsets = arm_get_frame_offsets ();
13805 amount = offsets->outgoing_args - offsets->saved_regs;
13807 if (frame_pointer_needed)
13809 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13810 amount = offsets->locals_base - offsets->saved_regs;
13813 if (amount)
13815 if (amount < 512)
13816 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13817 GEN_INT (amount)));
13818 else
13820 /* r3 is always free in the epilogue. */
13821 rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13823 emit_insn (gen_movsi (reg, GEN_INT (amount)));
13824 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13828 /* Emit a USE (stack_pointer_rtx), so that
13829 the stack adjustment will not be deleted. */
13830 emit_insn (gen_prologue_use (stack_pointer_rtx));
13832 if (current_function_profile || !TARGET_SCHED_PROLOG)
13833 emit_insn (gen_blockage ());
13835 /* Emit a clobber for each insn that will be restored in the epilogue,
13836 so that flow2 will get register lifetimes correct. */
13837 for (regno = 0; regno < 13; regno++)
13838 if (regs_ever_live[regno] && !call_used_regs[regno])
13839 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13841 if (! regs_ever_live[LR_REGNUM])
13842 emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13845 static void
13846 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13848 unsigned long live_regs_mask = 0;
13849 unsigned long l_mask;
13850 unsigned high_regs_pushed = 0;
13851 int cfa_offset = 0;
13852 int regno;
13854 if (IS_NAKED (arm_current_func_type ()))
13855 return;
13857 if (is_called_in_ARM_mode (current_function_decl))
13859 const char * name;
13861 gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
13862 gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
13863 == SYMBOL_REF);
13864 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
13866 /* Generate code sequence to switch us into Thumb mode. */
13867 /* The .code 32 directive has already been emitted by
13868 ASM_DECLARE_FUNCTION_NAME. */
13869 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13870 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13872 /* Generate a label, so that the debugger will notice the
13873 change in instruction sets. This label is also used by
13874 the assembler to bypass the ARM code when this function
13875 is called from a Thumb encoded function elsewhere in the
13876 same file. Hence the definition of STUB_NAME here must
13877 agree with the definition in gas/config/tc-arm.c. */
13879 #define STUB_NAME ".real_start_of"
13881 fprintf (f, "\t.code\t16\n");
13882 #ifdef ARM_PE
13883 if (arm_dllexport_name_p (name))
13884 name = arm_strip_name_encoding (name);
13885 #endif
13886 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13887 fprintf (f, "\t.thumb_func\n");
13888 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13891 if (current_function_pretend_args_size)
13893 /* Output unwind directive for the stack adjustment. */
13894 if (ARM_EABI_UNWIND_TABLES)
13895 fprintf (f, "\t.pad #%d\n",
13896 current_function_pretend_args_size);
13898 if (cfun->machine->uses_anonymous_args)
13900 int num_pushes;
13902 fprintf (f, "\tpush\t{");
13904 num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13906 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13907 regno <= LAST_ARG_REGNUM;
13908 regno++)
13909 asm_fprintf (f, "%r%s", regno,
13910 regno == LAST_ARG_REGNUM ? "" : ", ");
13912 fprintf (f, "}\n");
13914 else
13915 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13916 SP_REGNUM, SP_REGNUM,
13917 current_function_pretend_args_size);
13919 /* We don't need to record the stores for unwinding (would it
13920 help the debugger any if we did?), but record the change in
13921 the stack pointer. */
13922 if (dwarf2out_do_frame ())
13924 char *l = dwarf2out_cfi_label ();
13926 cfa_offset = cfa_offset + current_function_pretend_args_size;
13927 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13931 /* Get the registers we are going to push. */
13932 live_regs_mask = thumb_compute_save_reg_mask ();
13933 /* Extract a mask of the ones we can give to the Thumb's push instruction. */
13934 l_mask = live_regs_mask & 0x40ff;
13935 /* Then count how many other high registers will need to be pushed. */
13936 high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13938 if (TARGET_BACKTRACE)
13940 unsigned offset;
13941 unsigned work_register;
13943 /* We have been asked to create a stack backtrace structure.
13944 The code looks like this:
13946 0 .align 2
13947 0 func:
13948 0 sub SP, #16 Reserve space for 4 registers.
13949 2 push {R7} Push low registers.
13950 4 add R7, SP, #20 Get the stack pointer before the push.
13951 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
13952 8 mov R7, PC Get hold of the start of this code plus 12.
13953 10 str R7, [SP, #16] Store it.
13954 12 mov R7, FP Get hold of the current frame pointer.
13955 14 str R7, [SP, #4] Store it.
13956 16 mov R7, LR Get hold of the current return address.
13957 18 str R7, [SP, #12] Store it.
13958 20 add R7, SP, #16 Point at the start of the backtrace structure.
13959 22 mov FP, R7 Put this value into the frame pointer. */
13961 work_register = thumb_find_work_register (live_regs_mask);
13963 if (ARM_EABI_UNWIND_TABLES)
13964 asm_fprintf (f, "\t.pad #16\n");
13966 asm_fprintf
13967 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13968 SP_REGNUM, SP_REGNUM);
13970 if (dwarf2out_do_frame ())
13972 char *l = dwarf2out_cfi_label ();
13974 cfa_offset = cfa_offset + 16;
13975 dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13978 if (l_mask)
13980 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13981 offset = bit_count (l_mask) * UNITS_PER_WORD;
13983 else
13984 offset = 0;
13986 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13987 offset + 16 + current_function_pretend_args_size);
13989 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13990 offset + 4);
13992 /* Make sure that the instruction fetching the PC is in the right place
13993 to calculate "start of backtrace creation code + 12". */
13994 if (l_mask)
13996 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13997 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13998 offset + 12);
13999 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
14000 ARM_HARD_FRAME_POINTER_REGNUM);
14001 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14002 offset);
14004 else
14006 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
14007 ARM_HARD_FRAME_POINTER_REGNUM);
14008 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14009 offset);
14010 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
14011 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14012 offset + 12);
14015 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
14016 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14017 offset + 8);
14018 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
14019 offset + 12);
14020 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
14021 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
14023 /* Optimization: If we are not pushing any low registers but we are going
14024 to push some high registers then delay our first push. This will just
14025 be a push of LR and we can combine it with the push of the first high
14026 register. */
14027 else if ((l_mask & 0xff) != 0
14028 || (high_regs_pushed == 0 && l_mask))
14029 thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
14031 if (high_regs_pushed)
14033 unsigned pushable_regs;
14034 unsigned next_hi_reg;
14036 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
14037 if (live_regs_mask & (1 << next_hi_reg))
14038 break;
14040 pushable_regs = l_mask & 0xff;
14042 if (pushable_regs == 0)
14043 pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
14045 while (high_regs_pushed > 0)
14047 unsigned long real_regs_mask = 0;
14049 for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
14051 if (pushable_regs & (1 << regno))
14053 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
14055 high_regs_pushed --;
14056 real_regs_mask |= (1 << next_hi_reg);
14058 if (high_regs_pushed)
14060 for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
14061 next_hi_reg --)
14062 if (live_regs_mask & (1 << next_hi_reg))
14063 break;
14065 else
14067 pushable_regs &= ~((1 << regno) - 1);
14068 break;
14073 /* If we had to find a work register and we have not yet
14074 saved the LR then add it to the list of regs to push. */
14075 if (l_mask == (1 << LR_REGNUM))
14077 thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
14078 1, &cfa_offset,
14079 real_regs_mask | (1 << LR_REGNUM));
14080 l_mask = 0;
14082 else
14083 thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
14088 /* Handle the case of a double word load into a low register from
14089 a computed memory address. The computed address may involve a
14090 register which is overwritten by the load. */
14091 const char *
14092 thumb_load_double_from_address (rtx *operands)
14094 rtx addr;
14095 rtx base;
14096 rtx offset;
14097 rtx arg1;
14098 rtx arg2;
14100 gcc_assert (GET_CODE (operands[0]) == REG);
14101 gcc_assert (GET_CODE (operands[1]) == MEM);
14103 /* Get the memory address. */
14104 addr = XEXP (operands[1], 0);
14106 /* Work out how the memory address is computed. */
14107 switch (GET_CODE (addr))
14109 case REG:
14110 operands[2] = adjust_address (operands[1], SImode, 4);
14112 if (REGNO (operands[0]) == REGNO (addr))
14114 output_asm_insn ("ldr\t%H0, %2", operands);
14115 output_asm_insn ("ldr\t%0, %1", operands);
14117 else
14119 output_asm_insn ("ldr\t%0, %1", operands);
14120 output_asm_insn ("ldr\t%H0, %2", operands);
14122 break;
14124 case CONST:
14125 /* Compute <address> + 4 for the high order load. */
14126 operands[2] = adjust_address (operands[1], SImode, 4);
14128 output_asm_insn ("ldr\t%0, %1", operands);
14129 output_asm_insn ("ldr\t%H0, %2", operands);
14130 break;
14132 case PLUS:
14133 arg1 = XEXP (addr, 0);
14134 arg2 = XEXP (addr, 1);
14136 if (CONSTANT_P (arg1))
14137 base = arg2, offset = arg1;
14138 else
14139 base = arg1, offset = arg2;
14141 gcc_assert (GET_CODE (base) == REG);
14143 /* Catch the case of <address> = <reg> + <reg> */
14144 if (GET_CODE (offset) == REG)
14146 int reg_offset = REGNO (offset);
14147 int reg_base = REGNO (base);
14148 int reg_dest = REGNO (operands[0]);
14150 /* Add the base and offset registers together into the
14151 higher destination register. */
14152 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
14153 reg_dest + 1, reg_base, reg_offset);
14155 /* Load the lower destination register from the address in
14156 the higher destination register. */
14157 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
14158 reg_dest, reg_dest + 1);
14160 /* Load the higher destination register from its own address
14161 plus 4. */
14162 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
14163 reg_dest + 1, reg_dest + 1);
14165 else
14167 /* Compute <address> + 4 for the high order load. */
14168 operands[2] = adjust_address (operands[1], SImode, 4);
14170 /* If the computed address is held in the low order register
14171 then load the high order register first, otherwise always
14172 load the low order register first. */
14173 if (REGNO (operands[0]) == REGNO (base))
14175 output_asm_insn ("ldr\t%H0, %2", operands);
14176 output_asm_insn ("ldr\t%0, %1", operands);
14178 else
14180 output_asm_insn ("ldr\t%0, %1", operands);
14181 output_asm_insn ("ldr\t%H0, %2", operands);
14184 break;
14186 case LABEL_REF:
14187 /* With no registers to worry about we can just load the value
14188 directly. */
14189 operands[2] = adjust_address (operands[1], SImode, 4);
14191 output_asm_insn ("ldr\t%H0, %2", operands);
14192 output_asm_insn ("ldr\t%0, %1", operands);
14193 break;
14195 default:
14196 gcc_unreachable ();
14199 return "";
14202 const char *
14203 thumb_output_move_mem_multiple (int n, rtx *operands)
14205 rtx tmp;
14207 switch (n)
14209 case 2:
14210 if (REGNO (operands[4]) > REGNO (operands[5]))
14212 tmp = operands[4];
14213 operands[4] = operands[5];
14214 operands[5] = tmp;
14216 output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
14217 output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
14218 break;
14220 case 3:
14221 if (REGNO (operands[4]) > REGNO (operands[5]))
14223 tmp = operands[4];
14224 operands[4] = operands[5];
14225 operands[5] = tmp;
14227 if (REGNO (operands[5]) > REGNO (operands[6]))
14229 tmp = operands[5];
14230 operands[5] = operands[6];
14231 operands[6] = tmp;
14233 if (REGNO (operands[4]) > REGNO (operands[5]))
14235 tmp = operands[4];
14236 operands[4] = operands[5];
14237 operands[5] = tmp;
14240 output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
14241 output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
14242 break;
14244 default:
14245 gcc_unreachable ();
14248 return "";
14251 /* Output a call-via instruction for thumb state. */
14252 const char *
14253 thumb_call_via_reg (rtx reg)
14255 int regno = REGNO (reg);
14256 rtx *labelp;
14258 gcc_assert (regno < LR_REGNUM);
14260 /* If we are in the normal text section we can use a single instance
14261 per compilation unit. If we are doing function sections, then we need
14262 an entry per section, since we can't rely on reachability. */
14263 if (in_section == text_section)
14265 thumb_call_reg_needed = 1;
14267 if (thumb_call_via_label[regno] == NULL)
14268 thumb_call_via_label[regno] = gen_label_rtx ();
14269 labelp = thumb_call_via_label + regno;
14271 else
14273 if (cfun->machine->call_via[regno] == NULL)
14274 cfun->machine->call_via[regno] = gen_label_rtx ();
14275 labelp = cfun->machine->call_via + regno;
14278 output_asm_insn ("bl\t%a0", labelp);
14279 return "";
14282 /* Routines for generating rtl. */
14283 void
14284 thumb_expand_movmemqi (rtx *operands)
14286 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
14287 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
14288 HOST_WIDE_INT len = INTVAL (operands[2]);
14289 HOST_WIDE_INT offset = 0;
14291 while (len >= 12)
14293 emit_insn (gen_movmem12b (out, in, out, in));
14294 len -= 12;
14297 if (len >= 8)
14299 emit_insn (gen_movmem8b (out, in, out, in));
14300 len -= 8;
14303 if (len >= 4)
14305 rtx reg = gen_reg_rtx (SImode);
14306 emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
14307 emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
14308 len -= 4;
14309 offset += 4;
14312 if (len >= 2)
14314 rtx reg = gen_reg_rtx (HImode);
14315 emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
14316 plus_constant (in, offset))));
14317 emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
14318 reg));
14319 len -= 2;
14320 offset += 2;
14323 if (len)
14325 rtx reg = gen_reg_rtx (QImode);
14326 emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
14327 plus_constant (in, offset))));
14328 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
14329 reg));
14333 void
14334 thumb_reload_out_hi (rtx *operands)
14336 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
14339 /* Handle reading a half-word from memory during reload. */
14340 void
14341 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
14343 gcc_unreachable ();
14346 /* Return the length of a function name prefix
14347 that starts with the character 'c'. */
14348 static int
14349 arm_get_strip_length (int c)
14351 switch (c)
14353 ARM_NAME_ENCODING_LENGTHS
14354 default: return 0;
14358 /* Return a pointer to a function's name with any
14359 and all prefix encodings stripped from it. */
14360 const char *
14361 arm_strip_name_encoding (const char *name)
14363 int skip;
14365 while ((skip = arm_get_strip_length (* name)))
14366 name += skip;
14368 return name;
14371 /* If there is a '*' anywhere in the name's prefix, then
14372 emit the stripped name verbatim, otherwise prepend an
14373 underscore if leading underscores are being used. */
14374 void
14375 arm_asm_output_labelref (FILE *stream, const char *name)
14377 int skip;
14378 int verbatim = 0;
14380 while ((skip = arm_get_strip_length (* name)))
14382 verbatim |= (*name == '*');
14383 name += skip;
14386 if (verbatim)
14387 fputs (name, stream);
14388 else
14389 asm_fprintf (stream, "%U%s", name);
14392 static void
14393 arm_file_end (void)
14395 int regno;
14397 if (! thumb_call_reg_needed)
14398 return;
14400 switch_to_section (text_section);
14401 asm_fprintf (asm_out_file, "\t.code 16\n");
14402 ASM_OUTPUT_ALIGN (asm_out_file, 1);
14404 for (regno = 0; regno < LR_REGNUM; regno++)
14406 rtx label = thumb_call_via_label[regno];
14408 if (label != 0)
14410 targetm.asm_out.internal_label (asm_out_file, "L",
14411 CODE_LABEL_NUMBER (label));
14412 asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
14417 rtx aof_pic_label;
14419 #ifdef AOF_ASSEMBLER
14420 /* Special functions only needed when producing AOF syntax assembler. */
14422 struct pic_chain
14424 struct pic_chain * next;
14425 const char * symname;
14428 static struct pic_chain * aof_pic_chain = NULL;
14431 aof_pic_entry (rtx x)
14433 struct pic_chain ** chainp;
14434 int offset;
14436 if (aof_pic_label == NULL_RTX)
14438 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
14441 for (offset = 0, chainp = &aof_pic_chain; *chainp;
14442 offset += 4, chainp = &(*chainp)->next)
14443 if ((*chainp)->symname == XSTR (x, 0))
14444 return plus_constant (aof_pic_label, offset);
14446 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
14447 (*chainp)->next = NULL;
14448 (*chainp)->symname = XSTR (x, 0);
14449 return plus_constant (aof_pic_label, offset);
14452 void
14453 aof_dump_pic_table (FILE *f)
14455 struct pic_chain * chain;
14457 if (aof_pic_chain == NULL)
14458 return;
14460 asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
14461 PIC_OFFSET_TABLE_REGNUM,
14462 PIC_OFFSET_TABLE_REGNUM);
14463 fputs ("|x$adcons|\n", f);
14465 for (chain = aof_pic_chain; chain; chain = chain->next)
14467 fputs ("\tDCD\t", f);
14468 assemble_name (f, chain->symname);
14469 fputs ("\n", f);
14473 int arm_text_section_count = 1;
14475 /* A get_unnamed_section callback for switching to the text section. */
14477 static void
14478 aof_output_text_section_asm_op (const void *data ATTRIBUTE_UNUSED)
14480 fprintf (asm_out_file, "\tAREA |C$$code%d|, CODE, READONLY",
14481 arm_text_section_count++);
14482 if (flag_pic)
14483 fprintf (asm_out_file, ", PIC, REENTRANT");
14484 fprintf (asm_out_file, "\n");
14487 static int arm_data_section_count = 1;
14489 /* A get_unnamed_section callback for switching to the data section. */
14491 static void
14492 aof_output_data_section_asm_op (const void *data ATTRIBUTE_UNUSED)
14494 fprintf (asm_out_file, "\tAREA |C$$data%d|, DATA\n",
14495 arm_data_section_count++);
14498 /* Implement TARGET_ASM_INIT_SECTIONS.
14500 AOF Assembler syntax is a nightmare when it comes to areas, since once
14501 we change from one area to another, we can't go back again. Instead,
14502 we must create a new area with the same attributes and add the new output
14503 to that. Unfortunately, there is nothing we can do here to guarantee that
14504 two areas with the same attributes will be linked adjacently in the
14505 resulting executable, so we have to be careful not to do pc-relative
14506 addressing across such boundaries. */
14508 static void
14509 aof_asm_init_sections (void)
14511 text_section = get_unnamed_section (SECTION_CODE,
14512 aof_output_text_section_asm_op, NULL);
14513 data_section = get_unnamed_section (SECTION_WRITE,
14514 aof_output_data_section_asm_op, NULL);
14515 readonly_data_section = text_section;
14518 void
14519 zero_init_section (void)
14521 static int zero_init_count = 1;
14523 fprintf (asm_out_file, "\tAREA |C$$zidata%d|,NOINIT\n", zero_init_count++);
14524 in_section = NULL;
14527 /* The AOF assembler is religiously strict about declarations of
14528 imported and exported symbols, so that it is impossible to declare
14529 a function as imported near the beginning of the file, and then to
14530 export it later on. It is, however, possible to delay the decision
14531 until all the functions in the file have been compiled. To get
14532 around this, we maintain a list of the imports and exports, and
14533 delete from it any that are subsequently defined. At the end of
14534 compilation we spit the remainder of the list out before the END
14535 directive. */
14537 struct import
14539 struct import * next;
14540 const char * name;
14543 static struct import * imports_list = NULL;
14545 void
14546 aof_add_import (const char *name)
14548 struct import * new;
14550 for (new = imports_list; new; new = new->next)
14551 if (new->name == name)
14552 return;
14554 new = (struct import *) xmalloc (sizeof (struct import));
14555 new->next = imports_list;
14556 imports_list = new;
14557 new->name = name;
14560 void
14561 aof_delete_import (const char *name)
14563 struct import ** old;
14565 for (old = &imports_list; *old; old = & (*old)->next)
14567 if ((*old)->name == name)
14569 *old = (*old)->next;
14570 return;
14575 int arm_main_function = 0;
14577 static void
14578 aof_dump_imports (FILE *f)
14580 /* The AOF assembler needs this to cause the startup code to be extracted
14581 from the library. Brining in __main causes the whole thing to work
14582 automagically. */
14583 if (arm_main_function)
14585 switch_to_section (text_section);
14586 fputs ("\tIMPORT __main\n", f);
14587 fputs ("\tDCD __main\n", f);
14590 /* Now dump the remaining imports. */
14591 while (imports_list)
14593 fprintf (f, "\tIMPORT\t");
14594 assemble_name (f, imports_list->name);
14595 fputc ('\n', f);
14596 imports_list = imports_list->next;
14600 static void
14601 aof_globalize_label (FILE *stream, const char *name)
14603 default_globalize_label (stream, name);
14604 if (! strcmp (name, "main"))
14605 arm_main_function = 1;
14608 static void
14609 aof_file_start (void)
14611 fputs ("__r0\tRN\t0\n", asm_out_file);
14612 fputs ("__a1\tRN\t0\n", asm_out_file);
14613 fputs ("__a2\tRN\t1\n", asm_out_file);
14614 fputs ("__a3\tRN\t2\n", asm_out_file);
14615 fputs ("__a4\tRN\t3\n", asm_out_file);
14616 fputs ("__v1\tRN\t4\n", asm_out_file);
14617 fputs ("__v2\tRN\t5\n", asm_out_file);
14618 fputs ("__v3\tRN\t6\n", asm_out_file);
14619 fputs ("__v4\tRN\t7\n", asm_out_file);
14620 fputs ("__v5\tRN\t8\n", asm_out_file);
14621 fputs ("__v6\tRN\t9\n", asm_out_file);
14622 fputs ("__sl\tRN\t10\n", asm_out_file);
14623 fputs ("__fp\tRN\t11\n", asm_out_file);
14624 fputs ("__ip\tRN\t12\n", asm_out_file);
14625 fputs ("__sp\tRN\t13\n", asm_out_file);
14626 fputs ("__lr\tRN\t14\n", asm_out_file);
14627 fputs ("__pc\tRN\t15\n", asm_out_file);
14628 fputs ("__f0\tFN\t0\n", asm_out_file);
14629 fputs ("__f1\tFN\t1\n", asm_out_file);
14630 fputs ("__f2\tFN\t2\n", asm_out_file);
14631 fputs ("__f3\tFN\t3\n", asm_out_file);
14632 fputs ("__f4\tFN\t4\n", asm_out_file);
14633 fputs ("__f5\tFN\t5\n", asm_out_file);
14634 fputs ("__f6\tFN\t6\n", asm_out_file);
14635 fputs ("__f7\tFN\t7\n", asm_out_file);
14636 switch_to_section (text_section);
14639 static void
14640 aof_file_end (void)
14642 if (flag_pic)
14643 aof_dump_pic_table (asm_out_file);
14644 arm_file_end ();
14645 aof_dump_imports (asm_out_file);
14646 fputs ("\tEND\n", asm_out_file);
14648 #endif /* AOF_ASSEMBLER */
14650 #ifndef ARM_PE
14651 /* Symbols in the text segment can be accessed without indirecting via the
14652 constant pool; it may take an extra binary operation, but this is still
14653 faster than indirecting via memory. Don't do this when not optimizing,
14654 since we won't be calculating al of the offsets necessary to do this
14655 simplification. */
14657 static void
14658 arm_encode_section_info (tree decl, rtx rtl, int first)
14660 /* This doesn't work with AOF syntax, since the string table may be in
14661 a different AREA. */
14662 #ifndef AOF_ASSEMBLER
14663 if (optimize > 0 && TREE_CONSTANT (decl))
14664 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14665 #endif
14667 /* If we are referencing a function that is weak then encode a long call
14668 flag in the function name, otherwise if the function is static or
14669 or known to be defined in this file then encode a short call flag. */
14670 if (first && DECL_P (decl))
14672 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14673 arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14674 else if (! TREE_PUBLIC (decl))
14675 arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14678 default_encode_section_info (decl, rtl, first);
14680 #endif /* !ARM_PE */
14682 static void
14683 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14685 if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14686 && !strcmp (prefix, "L"))
14688 arm_ccfsm_state = 0;
14689 arm_target_insn = NULL;
14691 default_internal_label (stream, prefix, labelno);
14694 /* Output code to add DELTA to the first argument, and then jump
14695 to FUNCTION. Used for C++ multiple inheritance. */
14696 static void
14697 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14698 HOST_WIDE_INT delta,
14699 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14700 tree function)
14702 static int thunk_label = 0;
14703 char label[256];
14704 int mi_delta = delta;
14705 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14706 int shift = 0;
14707 int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14708 ? 1 : 0);
14709 if (mi_delta < 0)
14710 mi_delta = - mi_delta;
14711 if (TARGET_THUMB)
14713 int labelno = thunk_label++;
14714 ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14715 fputs ("\tldr\tr12, ", file);
14716 assemble_name (file, label);
14717 fputc ('\n', file);
14719 while (mi_delta != 0)
14721 if ((mi_delta & (3 << shift)) == 0)
14722 shift += 2;
14723 else
14725 asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14726 mi_op, this_regno, this_regno,
14727 mi_delta & (0xff << shift));
14728 mi_delta &= ~(0xff << shift);
14729 shift += 8;
14732 if (TARGET_THUMB)
14734 fprintf (file, "\tbx\tr12\n");
14735 ASM_OUTPUT_ALIGN (file, 2);
14736 assemble_name (file, label);
14737 fputs (":\n", file);
14738 assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14740 else
14742 fputs ("\tb\t", file);
14743 assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14744 if (NEED_PLT_RELOC)
14745 fputs ("(PLT)", file);
14746 fputc ('\n', file);
14751 arm_emit_vector_const (FILE *file, rtx x)
14753 int i;
14754 const char * pattern;
14756 gcc_assert (GET_CODE (x) == CONST_VECTOR);
14758 switch (GET_MODE (x))
14760 case V2SImode: pattern = "%08x"; break;
14761 case V4HImode: pattern = "%04x"; break;
14762 case V8QImode: pattern = "%02x"; break;
14763 default: gcc_unreachable ();
14766 fprintf (file, "0x");
14767 for (i = CONST_VECTOR_NUNITS (x); i--;)
14769 rtx element;
14771 element = CONST_VECTOR_ELT (x, i);
14772 fprintf (file, pattern, INTVAL (element));
14775 return 1;
14778 const char *
14779 arm_output_load_gr (rtx *operands)
14781 rtx reg;
14782 rtx offset;
14783 rtx wcgr;
14784 rtx sum;
14786 if (GET_CODE (operands [1]) != MEM
14787 || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14788 || GET_CODE (reg = XEXP (sum, 0)) != REG
14789 || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14790 || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14791 return "wldrw%?\t%0, %1";
14793 /* Fix up an out-of-range load of a GR register. */
14794 output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14795 wcgr = operands[0];
14796 operands[0] = reg;
14797 output_asm_insn ("ldr%?\t%0, %1", operands);
14799 operands[0] = wcgr;
14800 operands[1] = reg;
14801 output_asm_insn ("tmcr%?\t%0, %1", operands);
14802 output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14804 return "";
14807 static rtx
14808 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14809 int incoming ATTRIBUTE_UNUSED)
14811 #if 0
14812 /* FIXME: The ARM backend has special code to handle structure
14813 returns, and will reserve its own hidden first argument. So
14814 if this macro is enabled a *second* hidden argument will be
14815 reserved, which will break binary compatibility with old
14816 toolchains and also thunk handling. One day this should be
14817 fixed. */
14818 return 0;
14819 #else
14820 /* Register in which address to store a structure value
14821 is passed to a function. */
14822 return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14823 #endif
14826 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14828 On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14829 named arg and all anonymous args onto the stack.
14830 XXX I know the prologue shouldn't be pushing registers, but it is faster
14831 that way. */
14833 static void
14834 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14835 enum machine_mode mode ATTRIBUTE_UNUSED,
14836 tree type ATTRIBUTE_UNUSED,
14837 int *pretend_size,
14838 int second_time ATTRIBUTE_UNUSED)
14840 cfun->machine->uses_anonymous_args = 1;
14841 if (cum->nregs < NUM_ARG_REGS)
14842 *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14845 /* Return nonzero if the CONSUMER instruction (a store) does not need
14846 PRODUCER's value to calculate the address. */
14849 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14851 rtx value = PATTERN (producer);
14852 rtx addr = PATTERN (consumer);
14854 if (GET_CODE (value) == COND_EXEC)
14855 value = COND_EXEC_CODE (value);
14856 if (GET_CODE (value) == PARALLEL)
14857 value = XVECEXP (value, 0, 0);
14858 value = XEXP (value, 0);
14859 if (GET_CODE (addr) == COND_EXEC)
14860 addr = COND_EXEC_CODE (addr);
14861 if (GET_CODE (addr) == PARALLEL)
14862 addr = XVECEXP (addr, 0, 0);
14863 addr = XEXP (addr, 0);
14865 return !reg_overlap_mentioned_p (value, addr);
14868 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14869 have an early register shift value or amount dependency on the
14870 result of PRODUCER. */
14873 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14875 rtx value = PATTERN (producer);
14876 rtx op = PATTERN (consumer);
14877 rtx early_op;
14879 if (GET_CODE (value) == COND_EXEC)
14880 value = COND_EXEC_CODE (value);
14881 if (GET_CODE (value) == PARALLEL)
14882 value = XVECEXP (value, 0, 0);
14883 value = XEXP (value, 0);
14884 if (GET_CODE (op) == COND_EXEC)
14885 op = COND_EXEC_CODE (op);
14886 if (GET_CODE (op) == PARALLEL)
14887 op = XVECEXP (op, 0, 0);
14888 op = XEXP (op, 1);
14890 early_op = XEXP (op, 0);
14891 /* This is either an actual independent shift, or a shift applied to
14892 the first operand of another operation. We want the whole shift
14893 operation. */
14894 if (GET_CODE (early_op) == REG)
14895 early_op = op;
14897 return !reg_overlap_mentioned_p (value, early_op);
14900 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14901 have an early register shift value dependency on the result of
14902 PRODUCER. */
14905 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14907 rtx value = PATTERN (producer);
14908 rtx op = PATTERN (consumer);
14909 rtx early_op;
14911 if (GET_CODE (value) == COND_EXEC)
14912 value = COND_EXEC_CODE (value);
14913 if (GET_CODE (value) == PARALLEL)
14914 value = XVECEXP (value, 0, 0);
14915 value = XEXP (value, 0);
14916 if (GET_CODE (op) == COND_EXEC)
14917 op = COND_EXEC_CODE (op);
14918 if (GET_CODE (op) == PARALLEL)
14919 op = XVECEXP (op, 0, 0);
14920 op = XEXP (op, 1);
14922 early_op = XEXP (op, 0);
14924 /* This is either an actual independent shift, or a shift applied to
14925 the first operand of another operation. We want the value being
14926 shifted, in either case. */
14927 if (GET_CODE (early_op) != REG)
14928 early_op = XEXP (early_op, 0);
14930 return !reg_overlap_mentioned_p (value, early_op);
14933 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14934 have an early register mult dependency on the result of
14935 PRODUCER. */
14938 arm_no_early_mul_dep (rtx producer, rtx consumer)
14940 rtx value = PATTERN (producer);
14941 rtx op = PATTERN (consumer);
14943 if (GET_CODE (value) == COND_EXEC)
14944 value = COND_EXEC_CODE (value);
14945 if (GET_CODE (value) == PARALLEL)
14946 value = XVECEXP (value, 0, 0);
14947 value = XEXP (value, 0);
14948 if (GET_CODE (op) == COND_EXEC)
14949 op = COND_EXEC_CODE (op);
14950 if (GET_CODE (op) == PARALLEL)
14951 op = XVECEXP (op, 0, 0);
14952 op = XEXP (op, 1);
14954 return (GET_CODE (op) == PLUS
14955 && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14959 /* We can't rely on the caller doing the proper promotion when
14960 using APCS or ATPCS. */
14962 static bool
14963 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14965 return !TARGET_AAPCS_BASED;
14969 /* AAPCS based ABIs use short enums by default. */
14971 static bool
14972 arm_default_short_enums (void)
14974 return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
14978 /* AAPCS requires that anonymous bitfields affect structure alignment. */
14980 static bool
14981 arm_align_anon_bitfield (void)
14983 return TARGET_AAPCS_BASED;
14987 /* The generic C++ ABI says 64-bit (long long). The EABI says 32-bit. */
14989 static tree
14990 arm_cxx_guard_type (void)
14992 return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14996 /* The EABI says test the least significant bit of a guard variable. */
14998 static bool
14999 arm_cxx_guard_mask_bit (void)
15001 return TARGET_AAPCS_BASED;
15005 /* The EABI specifies that all array cookies are 8 bytes long. */
15007 static tree
15008 arm_get_cookie_size (tree type)
15010 tree size;
15012 if (!TARGET_AAPCS_BASED)
15013 return default_cxx_get_cookie_size (type);
15015 size = build_int_cst (sizetype, 8);
15016 return size;
15020 /* The EABI says that array cookies should also contain the element size. */
15022 static bool
15023 arm_cookie_has_size (void)
15025 return TARGET_AAPCS_BASED;
15029 /* The EABI says constructors and destructors should return a pointer to
15030 the object constructed/destroyed. */
15032 static bool
15033 arm_cxx_cdtor_returns_this (void)
15035 return TARGET_AAPCS_BASED;
15038 /* The EABI says that an inline function may never be the key
15039 method. */
15041 static bool
15042 arm_cxx_key_method_may_be_inline (void)
15044 return !TARGET_AAPCS_BASED;
15047 static void
15048 arm_cxx_determine_class_data_visibility (tree decl)
15050 if (!TARGET_AAPCS_BASED)
15051 return;
15053 /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
15054 is exported. However, on systems without dynamic vague linkage,
15055 \S 3.2.5.6 says that COMDAT class data has hidden linkage. */
15056 if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
15057 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
15058 else
15059 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
15060 DECL_VISIBILITY_SPECIFIED (decl) = 1;
15063 static bool
15064 arm_cxx_class_data_always_comdat (void)
15066 /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
15067 vague linkage if the class has no key function. */
15068 return !TARGET_AAPCS_BASED;
15072 /* The EABI says __aeabi_atexit should be used to register static
15073 destructors. */
15075 static bool
15076 arm_cxx_use_aeabi_atexit (void)
15078 return TARGET_AAPCS_BASED;
15082 void
15083 arm_set_return_address (rtx source, rtx scratch)
15085 arm_stack_offsets *offsets;
15086 HOST_WIDE_INT delta;
15087 rtx addr;
15088 unsigned long saved_regs;
15090 saved_regs = arm_compute_save_reg_mask ();
15092 if ((saved_regs & (1 << LR_REGNUM)) == 0)
15093 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
15094 else
15096 if (frame_pointer_needed)
15097 addr = plus_constant(hard_frame_pointer_rtx, -4);
15098 else
15100 /* LR will be the first saved register. */
15101 offsets = arm_get_frame_offsets ();
15102 delta = offsets->outgoing_args - (offsets->frame + 4);
15105 if (delta >= 4096)
15107 emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
15108 GEN_INT (delta & ~4095)));
15109 addr = scratch;
15110 delta &= 4095;
15112 else
15113 addr = stack_pointer_rtx;
15115 addr = plus_constant (addr, delta);
15117 emit_move_insn (gen_frame_mem (Pmode, addr), source);
15122 void
15123 thumb_set_return_address (rtx source, rtx scratch)
15125 arm_stack_offsets *offsets;
15126 HOST_WIDE_INT delta;
15127 int reg;
15128 rtx addr;
15129 unsigned long mask;
15131 emit_insn (gen_rtx_USE (VOIDmode, source));
15133 mask = thumb_compute_save_reg_mask ();
15134 if (mask & (1 << LR_REGNUM))
15136 offsets = arm_get_frame_offsets ();
15138 /* Find the saved regs. */
15139 if (frame_pointer_needed)
15141 delta = offsets->soft_frame - offsets->saved_args;
15142 reg = THUMB_HARD_FRAME_POINTER_REGNUM;
15144 else
15146 delta = offsets->outgoing_args - offsets->saved_args;
15147 reg = SP_REGNUM;
15149 /* Allow for the stack frame. */
15150 if (TARGET_BACKTRACE)
15151 delta -= 16;
15152 /* The link register is always the first saved register. */
15153 delta -= 4;
15155 /* Construct the address. */
15156 addr = gen_rtx_REG (SImode, reg);
15157 if ((reg != SP_REGNUM && delta >= 128)
15158 || delta >= 1024)
15160 emit_insn (gen_movsi (scratch, GEN_INT (delta)));
15161 emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
15162 addr = scratch;
15164 else
15165 addr = plus_constant (addr, delta);
15167 emit_move_insn (gen_frame_mem (Pmode, addr), source);
15169 else
15170 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
15173 /* Implements target hook vector_mode_supported_p. */
15174 bool
15175 arm_vector_mode_supported_p (enum machine_mode mode)
15177 if ((mode == V2SImode)
15178 || (mode == V4HImode)
15179 || (mode == V8QImode))
15180 return true;
15182 return false;
15185 /* Implement TARGET_SHIFT_TRUNCATION_MASK. SImode shifts use normal
15186 ARM insns and therefore guarantee that the shift count is modulo 256.
15187 DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
15188 guarantee no particular behavior for out-of-range counts. */
15190 static unsigned HOST_WIDE_INT
15191 arm_shift_truncation_mask (enum machine_mode mode)
15193 return mode == SImode ? 255 : 0;
15197 /* Map internal gcc register numbers to DWARF2 register numbers. */
15199 unsigned int
15200 arm_dbx_register_number (unsigned int regno)
15202 if (regno < 16)
15203 return regno;
15205 /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
15206 compatibility. The EABI defines them as registers 96-103. */
15207 if (IS_FPA_REGNUM (regno))
15208 return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
15210 if (IS_VFP_REGNUM (regno))
15211 return 64 + regno - FIRST_VFP_REGNUM;
15213 if (IS_IWMMXT_GR_REGNUM (regno))
15214 return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
15216 if (IS_IWMMXT_REGNUM (regno))
15217 return 112 + regno - FIRST_IWMMXT_REGNUM;
15219 gcc_unreachable ();
15223 #ifdef TARGET_UNWIND_INFO
15224 /* Emit unwind directives for a store-multiple instruction. This should
15225 only ever be generated by the function prologue code, so we expect it
15226 to have a particular form. */
15228 static void
15229 arm_unwind_emit_stm (FILE * asm_out_file, rtx p)
15231 int i;
15232 HOST_WIDE_INT offset;
15233 HOST_WIDE_INT nregs;
15234 int reg_size;
15235 unsigned reg;
15236 unsigned lastreg;
15237 rtx e;
15239 /* First insn will adjust the stack pointer. */
15240 e = XVECEXP (p, 0, 0);
15241 if (GET_CODE (e) != SET
15242 || GET_CODE (XEXP (e, 0)) != REG
15243 || REGNO (XEXP (e, 0)) != SP_REGNUM
15244 || GET_CODE (XEXP (e, 1)) != PLUS)
15245 abort ();
15247 offset = -INTVAL (XEXP (XEXP (e, 1), 1));
15248 nregs = XVECLEN (p, 0) - 1;
15250 reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
15251 if (reg < 16)
15253 /* The function prologue may also push pc, but not annotate it as it is
15254 never restored. We turn this into a stack pointer adjustment. */
15255 if (nregs * 4 == offset - 4)
15257 fprintf (asm_out_file, "\t.pad #4\n");
15258 offset -= 4;
15260 reg_size = 4;
15262 else if (IS_VFP_REGNUM (reg))
15264 /* FPA register saves use an additional word. */
15265 offset -= 4;
15266 reg_size = 8;
15268 else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
15270 /* FPA registers are done differently. */
15271 asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
15272 return;
15274 else
15275 /* Unknown register type. */
15276 abort ();
15278 /* If the stack increment doesn't match the size of the saved registers,
15279 something has gone horribly wrong. */
15280 if (offset != nregs * reg_size)
15281 abort ();
15283 fprintf (asm_out_file, "\t.save {");
15285 offset = 0;
15286 lastreg = 0;
15287 /* The remaining insns will describe the stores. */
15288 for (i = 1; i <= nregs; i++)
15290 /* Expect (set (mem <addr>) (reg)).
15291 Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)). */
15292 e = XVECEXP (p, 0, i);
15293 if (GET_CODE (e) != SET
15294 || GET_CODE (XEXP (e, 0)) != MEM
15295 || GET_CODE (XEXP (e, 1)) != REG)
15296 abort ();
15298 reg = REGNO (XEXP (e, 1));
15299 if (reg < lastreg)
15300 abort ();
15302 if (i != 1)
15303 fprintf (asm_out_file, ", ");
15304 /* We can't use %r for vfp because we need to use the
15305 double precision register names. */
15306 if (IS_VFP_REGNUM (reg))
15307 asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
15308 else
15309 asm_fprintf (asm_out_file, "%r", reg);
15311 #ifdef ENABLE_CHECKING
15312 /* Check that the addresses are consecutive. */
15313 e = XEXP (XEXP (e, 0), 0);
15314 if (GET_CODE (e) == PLUS)
15316 offset += reg_size;
15317 if (GET_CODE (XEXP (e, 0)) != REG
15318 || REGNO (XEXP (e, 0)) != SP_REGNUM
15319 || GET_CODE (XEXP (e, 1)) != CONST_INT
15320 || offset != INTVAL (XEXP (e, 1)))
15321 abort ();
15323 else if (i != 1
15324 || GET_CODE (e) != REG
15325 || REGNO (e) != SP_REGNUM)
15326 abort ();
15327 #endif
15329 fprintf (asm_out_file, "}\n");
15332 /* Emit unwind directives for a SET. */
15334 static void
15335 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
15337 rtx e0;
15338 rtx e1;
15340 e0 = XEXP (p, 0);
15341 e1 = XEXP (p, 1);
15342 switch (GET_CODE (e0))
15344 case MEM:
15345 /* Pushing a single register. */
15346 if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
15347 || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
15348 || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
15349 abort ();
15351 asm_fprintf (asm_out_file, "\t.save ");
15352 if (IS_VFP_REGNUM (REGNO (e1)))
15353 asm_fprintf(asm_out_file, "{d%d}\n",
15354 (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
15355 else
15356 asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
15357 break;
15359 case REG:
15360 if (REGNO (e0) == SP_REGNUM)
15362 /* A stack increment. */
15363 if (GET_CODE (e1) != PLUS
15364 || GET_CODE (XEXP (e1, 0)) != REG
15365 || REGNO (XEXP (e1, 0)) != SP_REGNUM
15366 || GET_CODE (XEXP (e1, 1)) != CONST_INT)
15367 abort ();
15369 asm_fprintf (asm_out_file, "\t.pad #%wd\n",
15370 -INTVAL (XEXP (e1, 1)));
15372 else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
15374 HOST_WIDE_INT offset;
15375 unsigned reg;
15377 if (GET_CODE (e1) == PLUS)
15379 if (GET_CODE (XEXP (e1, 0)) != REG
15380 || GET_CODE (XEXP (e1, 1)) != CONST_INT)
15381 abort ();
15382 reg = REGNO (XEXP (e1, 0));
15383 offset = INTVAL (XEXP (e1, 1));
15384 asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
15385 HARD_FRAME_POINTER_REGNUM, reg,
15386 INTVAL (XEXP (e1, 1)));
15388 else if (GET_CODE (e1) == REG)
15390 reg = REGNO (e1);
15391 asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
15392 HARD_FRAME_POINTER_REGNUM, reg);
15394 else
15395 abort ();
15397 else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
15399 /* Move from sp to reg. */
15400 asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
15402 else
15403 abort ();
15404 break;
15406 default:
15407 abort ();
15412 /* Emit unwind directives for the given insn. */
15414 static void
15415 arm_unwind_emit (FILE * asm_out_file, rtx insn)
15417 rtx pat;
15419 if (!ARM_EABI_UNWIND_TABLES)
15420 return;
15422 if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
15423 return;
15425 pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
15426 if (pat)
15427 pat = XEXP (pat, 0);
15428 else
15429 pat = PATTERN (insn);
15431 switch (GET_CODE (pat))
15433 case SET:
15434 arm_unwind_emit_set (asm_out_file, pat);
15435 break;
15437 case SEQUENCE:
15438 /* Store multiple. */
15439 arm_unwind_emit_stm (asm_out_file, pat);
15440 break;
15442 default:
15443 abort();
15448 /* Output a reference from a function exception table to the type_info
15449 object X. The EABI specifies that the symbol should be relocated by
15450 an R_ARM_TARGET2 relocation. */
15452 static bool
15453 arm_output_ttype (rtx x)
15455 fputs ("\t.word\t", asm_out_file);
15456 output_addr_const (asm_out_file, x);
15457 /* Use special relocations for symbol references. */
15458 if (GET_CODE (x) != CONST_INT)
15459 fputs ("(TARGET2)", asm_out_file);
15460 fputc ('\n', asm_out_file);
15462 return TRUE;
15464 #endif /* TARGET_UNWIND_INFO */
15467 /* Output unwind directives for the start/end of a function. */
15469 void
15470 arm_output_fn_unwind (FILE * f, bool prologue)
15472 if (!ARM_EABI_UNWIND_TABLES)
15473 return;
15475 if (prologue)
15476 fputs ("\t.fnstart\n", f);
15477 else
15478 fputs ("\t.fnend\n", f);
15481 static bool
15482 arm_emit_tls_decoration (FILE *fp, rtx x)
15484 enum tls_reloc reloc;
15485 rtx val;
15487 val = XVECEXP (x, 0, 0);
15488 reloc = INTVAL (XVECEXP (x, 0, 1));
15490 output_addr_const (fp, val);
15492 switch (reloc)
15494 case TLS_GD32:
15495 fputs ("(tlsgd)", fp);
15496 break;
15497 case TLS_LDM32:
15498 fputs ("(tlsldm)", fp);
15499 break;
15500 case TLS_LDO32:
15501 fputs ("(tlsldo)", fp);
15502 break;
15503 case TLS_IE32:
15504 fputs ("(gottpoff)", fp);
15505 break;
15506 case TLS_LE32:
15507 fputs ("(tpoff)", fp);
15508 break;
15509 default:
15510 gcc_unreachable ();
15513 switch (reloc)
15515 case TLS_GD32:
15516 case TLS_LDM32:
15517 case TLS_IE32:
15518 fputs (" + (. - ", fp);
15519 output_addr_const (fp, XVECEXP (x, 0, 2));
15520 fputs (" - ", fp);
15521 output_addr_const (fp, XVECEXP (x, 0, 3));
15522 fputc (')', fp);
15523 break;
15524 default:
15525 break;
15528 return TRUE;
15531 bool
15532 arm_output_addr_const_extra (FILE *fp, rtx x)
15534 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
15535 return arm_emit_tls_decoration (fp, x);
15536 else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
15538 char label[256];
15539 int labelno = INTVAL (XVECEXP (x, 0, 0));
15541 ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
15542 assemble_name_raw (fp, label);
15544 return TRUE;
15546 else if (GET_CODE (x) == CONST_VECTOR)
15547 return arm_emit_vector_const (fp, x);
15549 return FALSE;
15552 #include "gt-arm.h"